Tamim Posted January 16 Posted January 16 I would like to request your assistance in creating an AutoLISP script for AutoCAD that automates the color change of blocks based on user input. The task involves selecting blocks with the same name, after which the user will provide a number (e.g., 17, 16, or 15). This number will represent the panel group size, and the color of the blocks should be automatically adjusted accordingly, with sequential color assignments (e.g., red for the first group, yellow for the next, etc.). Quote
BIGAL Posted January 16 Posted January 16 It is alwas best with requests like this to post a sample dwg, should be doable. Quote
BIGAL Posted January 18 Posted January 18 (edited) I started to do something then realised doing it wrong, just ask how many columns, how many rows, then draw the panels with different colors. No indication of colors required so just goes up 1 each time ran, if more than 1 Block this can be added as a choice and the panel size will be in accordance with that block. So this is very much version one. ; https://www.cadtutor.net/forum/topic/95951-autolisp-script-for-block-color-change-based-on-input/ ; an attempt at draw solor panels in different colors. ; By AlanH Jan 2025 (defun c:wow ( / ans cols gap ht newpt oldsnap pt wid) (setq oldsnap (getvar 'osmode)) (setvar 'osmode 0) (command "snap" "OFF") (command "GRID" "OFF") (if (= col nil)(setq col 1)) (if (not AH:getvalsm)(load "Multi Getvals.lsp")) (setq ans (AH:getvalsm (list "Enter values " "Rows" 5 4 "3" "columns " 5 4 "10" "Gap" 5 4 "20"))) (setq rows (atoi (nth 0 ans))) (setq cols (atoi (nth 1 ans))) (setq gap (atof (nth 2 ans))) (setq wid 1134.0) (setq ht 2278.0) (setq pt (getpoint "\nPick lower left point ")) (repeat Rows (setq newpt pt) (repeat cols (command "-insert" "560-jkm" "s" 1 newpt 0.0) (vlax-put (vlax-ename->vla-object (entlast)) 'Color col) (setq newpt (mapcar '+ newpt (list (+ wid gap) 0.0 0.0))) ) (setq pt (mapcar '+ pt (list 0.0 (+ ht gap) 0.0))) (setq col (1+ col)) ) (setvar 'osmode oldsnap) (princ) ) (c:wow) For this to work edited Block 560-jkm to be vertical and lower left is at 0,0. You need to save multi getvals in a support path as its autoloaded. Else put full path in (load "C:\\mylips\\Multi getvals.lsp") Multi GETVALS.lsp Edited January 18 by BIGAL Quote
Tamim Posted January 20 Author Posted January 20 On 18/01/2025 at 10:08, BIGAL said: I started to do something then realised doing it wrong, just ask how many columns, how many rows, then draw the panels with different colors. No indication of colors required so just goes up 1 each time ran, if more than 1 Block this can be added as a choice and the panel size will be in accordance with that block. So this is very much version one. ; https://www.cadtutor.net/forum/topic/95951-autolisp-script-for-block-color-change-based-on-input/ ; an attempt at draw solor panels in different colors. ; By AlanH Jan 2025 (defun c:wow ( / ans cols gap ht newpt oldsnap pt wid) (setq oldsnap (getvar 'osmode)) (setvar 'osmode 0) (command "snap" "OFF") (command "GRID" "OFF") (if (= col nil)(setq col 1)) (if (not AH:getvalsm)(load "Multi Getvals.lsp")) (setq ans (AH:getvalsm (list "Enter values " "Rows" 5 4 "3" "columns " 5 4 "10" "Gap" 5 4 "20"))) (setq rows (atoi (nth 0 ans))) (setq cols (atoi (nth 1 ans))) (setq gap (atof (nth 2 ans))) (setq wid 1134.0) (setq ht 2278.0) (setq pt (getpoint "\nPick lower left point ")) (repeat Rows (setq newpt pt) (repeat cols (command "-insert" "560-jkm" "s" 1 newpt 0.0) (vlax-put (vlax-ename->vla-object (entlast)) 'Color col) (setq newpt (mapcar '+ newpt (list (+ wid gap) 0.0 0.0))) ) (setq pt (mapcar '+ pt (list 0.0 (+ ht gap) 0.0))) (setq col (1+ col)) ) (setvar 'osmode oldsnap) (princ) ) (c:wow) For this to work edited Block 560-jkm to be vertical and lower left is at 0,0. You need to save multi getvals in a support path as its autoloaded. Else put full path in (load "C:\\mylips\\Multi getvals.lsp") Multi GETVALS.lsp 2.99 kB · 28 downloads Thank you for the program. However, the block is placed horizontally, but I need it to be placed vertically.pls advice Quote
Tamim Posted January 20 Author Posted January 20 (edited) After loading the "Multi GETVALS.lsp" application using APPLOAD, the comment name "Multi GETVALS" does not appear in the list. Please help resolve this issue Sample02.dwg Edited January 20 by Tamim Quote
BIGAL Posted January 20 Posted January 20 Save Multi getvals.lsp to a support directory or change the (load "c:\\yourdirectoryname\\muiti getvals.sp") as I autoload it, no need to use Appload. I did change your block I used bedit and rotated it 90 degrees and moved lower left to 0,0 then code should work. Do you have different size panels these can be added. So can do horizontal or vertical. Quote
Tamim Posted January 21 Author Posted January 21 5 hours ago, BIGAL said: Save Multi getvals.lsp to a support directory or change the (load "c:\\yourdirectoryname\\muiti getvals.sp") as I autoload it, no need to use Appload. I did change your block I used bedit and rotated it 90 degrees and moved lower left to 0,0 then code should work. Do you have different size panels these can be added. So can do horizontal or vertical. Thanks for the advice. The panel size hasn’t changed, but we’ll decide between horizontal or vertical based on the available area. We prefer vertical, but if there’s no space, we’ll go with horizontal. waiting for your valuable feedback Quote
BIGAL Posted January 21 Posted January 21 (edited) Thats ok as I said first go at solution, you can ask hor or ver then the two size values will be swapped and the code should still work, with the block rotated also on insertion. (setq horver (getstring "\nEnter H or V ")) (if (or (= horver "V")(= horver "v")) (setq wid 1134.0 ht 2278.0) (setq wid 2278.0 ht 1134.0) ) (if (or (= horver "V") (= horver "v")) (command "-insert" "560-jkm" "s" 1 newpt 0.0) (command "-insert" "560-jkm" "s" 1 newpt 90.0) ) Not tested. Just me would make a custom DCL so asks for hor & ver as well. Will see if can find time to make. Edited January 21 by BIGAL Quote
BIGAL Posted January 22 Posted January 22 The way I make custom dcl's is that I use say Multi getvals to make a dcl file, I change the code. (setq fo (open (setq fname (vl-filename-mktemp "" "" ".dcl")) "w")) (setq fo (open (setq fname "D"\\acadtemp\\dcltest.dcl") "w")) Then I can see the dcl code I would then add a new row which would be Radio buttons for the "Hor" or "Ver" as choices. Lastly I use a lisp by rlx to convert the dcl code to lisp code and add to the program. Ok an easy answer here is Multi radio buttons.lsp look at the top of code and you will see a Yes or No option change to say HOR & VER. This will return the string "HOR" or "VER" so use with the IF. Multi radio buttons.lsp 1 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.