mangeloooo Posted December 17, 2014 Share Posted December 17, 2014 Hello, I'm trying to find out how to write a lisp or script for my needs but I can't find what I'm looking for. It is very basic I think. What I want it to do is: Create a layer with a certain name and color, then select all hatches in the drawing and move them to that layer and then purge the drawing. If you can do this with several drawings automatically it would be a bonus. I guess this is easy for a lot of you but I don't now where to start... English is not my first language but I hope you understand my question Quote Link to comment Share on other sites More sharing options...
Tharwat Posted December 17, 2014 Share Posted December 17, 2014 Welcome to CADTutor Try this program and don't forget to change the name of layer which suits your needs and the color number as well . [ untested codes ] (defun c:Test (/ name color doc sel) ;; Tharwat 17.12.2014 ;; (setq name "Layer" [color=blue];; <= Layer name [/color] color 1 [color=blue];; <= Color of layer[/color] doc (vla-get-activedocument (vlax-get-acad-object)) ) (if (not (tblsearch "LAYER" name)) (entmake (list '(0 . "LAYER") '(100 . "AcDbSymbolTableRecord") '(100 . "AcDbLayerTableRecord") (cons 2 name) (cons 62 color) '(70 . 0) ) ) ) (if (ssget "_X" '((0 . "HATCH"))) (vlax-for x (setq sel (vla-get-ActiveSelectionSet doc)) (if (vlax-write-enabled-p x) (vla-put-layer x name) ) ) ) (if sel (vla-delete sel) ) (repeat 3 (command "_.-purge" "a" "*" "n")) (princ) ) (vl-load-com) Quote Link to comment Share on other sites More sharing options...
ttray33y Posted December 18, 2014 Share Posted December 18, 2014 Or you can mine, load the lisp and type chal to activate, and it will prompt for a new layer name & color, (setq wlayer "MylayerNAME") (setq ccolor "Red") (defun c:chal () (setvar "cmdecho" 1) (setq c_wlayer (getstring (strcat "New layer name <" wlayer ">: ")) c_color (getstring (strcat "\nChoose color <" ccolor ">: ")) ) (if (= c_color "") (setq c_color ccolor) (setq ccolor c_color) ) (if (= c_wlayer "") (setq c_wlayer wlayer) (setq wlayer c_wlayer) ) (if (setq mySet(ssget "X" (list (cons -4 "<OR") (cons 0 "HATCH") (cons -4 "OR>") ))) (progn (command "-layer" "make" wlayer "Color" ccolor "" "") (command "change" mySet "" "p" "la" wlayer "" "") ) ) (princ) ) ;simple routine to change layer and color of an object. cheers Quote Link to comment Share on other sites More sharing options...
mangeloooo Posted December 18, 2014 Author Share Posted December 18, 2014 Thank you very much for your replies. I have tried your codes. The first (Tharwat) I couldn't get to work? Don't know what I did wrong (nothing happens). The second (ttray33y) worked just fine but i would rather not have to write the layer and color every time. I want the layer to be named "K---------H" and have color "254". Perhaps someone can help me with putting it in the first code?? Maybe I did something wrong when i tried to do it myself.... Thank you in advance! Quote Link to comment Share on other sites More sharing options...
Tharwat Posted December 18, 2014 Share Posted December 18, 2014 Did you see the blue colored texts in the program ? Change "layer" to "K---------H" and 1 to 254 And try again Quote Link to comment Share on other sites More sharing options...
mangeloooo Posted December 18, 2014 Author Share Posted December 18, 2014 Thank you for your quick reply. Below I paste how it looks in my notepad-file (.lsp). Still nothing happens. Am I supposed to do something more than just drag and drop the file i ACAD? (defun c:Test (/ name color doc sel) ;; Tharwat 17.12.2014 ;; (setq name "K---------H" ;; color 254 ;; doc (vla-get-activedocument (vlax-get-acad-object)) ) (if (not (tblsearch "LAYER" name)) (entmake (list '(0 . "LAYER") '(100 . "AcDbSymbolTableRecord") '(100 . "AcDbLayerTableRecord") (cons 2 name) (cons 62 color) '(70 . 0) ) ) ) (if (ssget "_X" '((0 . "HATCH"))) (vlax-for x (setq sel (vla-get-ActiveSelectionSet doc)) (if (vlax-write-enabled-p x) (vla-put-layer x name) ) ) ) (if sel (vla-delete sel) ) (repeat 3 (command "_.-purge" "a" "*" "n")) (princ) ) (vl-load-com) Quote Link to comment Share on other sites More sharing options...
Tharwat Posted December 18, 2014 Share Posted December 18, 2014 Do you have the hatch objects in Blocks or separate objects in a drawing ? Upload sample drawing to investigate ( if you can ) . Quote Link to comment Share on other sites More sharing options...
mangeloooo Posted December 18, 2014 Author Share Posted December 18, 2014 No block but there are a lot of separate hatches. I attach a file here to test. test.dwg Quote Link to comment Share on other sites More sharing options...
ttray33y Posted December 18, 2014 Share Posted December 18, 2014 try this stripped version of the above routine. (defun c:chal () (setvar "cmdecho" 1) (if (setq mySet(ssget "X" (list (cons -4 "<OR") (cons 0 "HATCH") (cons -4 "OR>") ))) (progn (command "-layer" "make" "K---------H" "Color" "254" "" "") (command "change" mySet "" "p" "la" "K---------H" "" "") (command "_.-purge" "a" "*" "n") ) ) (princ) (princ "\nCADTutor." ) ;simple routine to change layer and color of an object. Quote Link to comment Share on other sites More sharing options...
mangeloooo Posted December 18, 2014 Author Share Posted December 18, 2014 That worked perfectly ttray33y! Thanks. Just a last question. Is there any way to make the program start without me having to write "chal"? Could save me a few seconds everytime Quote Link to comment Share on other sites More sharing options...
ttray33y Posted December 18, 2014 Share Posted December 18, 2014 That worked perfectly ttray33y! Thanks. Just a last question. Is there any way to make the program start without me having to write "chal"? Could save me a few seconds everytime if your trying to change multiple drawings i would suggest use scriptpro from autodesk, after installing all you need is a script to run the lisp all thru the last drawing. Quote Link to comment Share on other sites More sharing options...
Tharwat Posted December 18, 2014 Share Posted December 18, 2014 I don't know how you are running the codes , but I tried my routine on your attached drawing and it did work as expected . Good luck wit your work . Quote Link to comment Share on other sites More sharing options...
SLW210 Posted December 18, 2014 Share Posted December 18, 2014 Please read the Code Posting Guidelines and edit your post to include the Code in Code Tags. Quote Link to comment Share on other sites More sharing options...
mangeloooo Posted December 18, 2014 Author Share Posted December 18, 2014 Thanks again for your replies. I tried again Tharwat and now i got it to work. I didn't understand the first time that i hade to type "test" to start the commad. Thank you both! Quote Link to comment Share on other sites More sharing options...
BIGAL Posted December 19, 2014 Share Posted December 19, 2014 To autorun a lisp when you manually load it if it has a C:defun then just add the defun call as the last line (c:chal), Tharwat's (c:test) the next time you need it type chal. Likewise if the defun is just named so it uses local variables ie no C: again (chal) as last line Quote Link to comment Share on other sites More sharing options...
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.