Jump to content

Lisp or script for layer and hatch


mangeloooo

Recommended Posts

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 :)

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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 :twisted:

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...