Jump to content

Modify lisp to ignore locked layers.


XIJIANGWOO

Recommended Posts

Hello Everyone,

 

I have a lisp that I would like to use but I do not want it to affect locked layers. Could someone point me in the right direction on how to modify this lisp? I'm starting to get familiar with lisp programming but this code looks different than what I have been using. This lisp simply takes all text and puts it on a new layer called "Layer"-Text. I have one layer of text that I do not want to change. I'm hoping to just keep the layer locked while the lisp runs.

 

As always, any help is greatly appreciated.

 

Also does anyone know any good references on lisp for AutoCAD?

 

ToText.lsp

 

(defun c:ToText (/ la name lk ss new);; Move all *Text and *Leaders to a text layer, matching primary layer (eg. ALAN -> ALAN-TEXT);; Includes objects on locked layers;; Layer created if doesn't exist (also takes on color of primary layer);; Alan J. Thompson, 08.19.10(vl-load-com)(vlax-for l (setq la (vla-get-layers(cond (*AcadDoc*)((setq *AcadDoc* (vla-get-activedocument (vlax-get-acad-object)))))))(or (wcmatch (strcase (setq name (vla-get-name l))) "*-TEXT,*$*")(if (setq ss (ssget "_X" (list '(0 . "*LEADER,*TEXT") (cons 8 name))))(progn(and (setq lk (eq :vlax-true (vla-get-lock l))) (vla-put-lock l :vlax-false))(or (tblsearch "LAYER" (setq new (strcat name "-TEXT")))(vla-put-color (vla-add la new) (vla-get-color l)))(vlax-for x (setq ss (vla-get-activeselectionset *AcadDoc*))(vl-catch-all-apply (function vla-put-layer) (list x new)))(vla-delete ss)(and lk (vla-put-lock l :vlax-true))))))(princ)) 

Link to comment
Share on other sites

Here is another way to write it:

 

(defun c:test ( / acdoc aclay acsel name ) (vl-load-com)

 (setq acdoc (vla-get-activedocument (vlax-get-acad-object))
       aclay (vla-get-layers acdoc)
 )  
 (vlax-for layer aclay
   (cond
     ( (eq :vlax-true (vla-get-lock layer))
     )
     ( (wcmatch (strcase (setq name (vla-get-name layer))) "*-TEXT")
     )
     ( (ssget "_X" (list (cons 0 "*LEADER,*TEXT") (cons 8 name)))

       (if (null (tblsearch "LAYER" (setq name (strcat name "-TEXT"))))
         (vla-put-color (vla-add aclay name) (vla-get-color layer))
       )
       (vlax-for obj (setq acsel (vla-get-activeselectionset acdoc))
         (vl-catch-all-apply 'vla-put-layer (list obj name))
       )
       (vla-delete acsel)
     )
   )
 )
 (princ)
)

Link to comment
Share on other sites

Just for kicks this one will copy all properties of the original layer using an ObjectDBX Document (except if the original layer was "0"):

 

(defun c:test ( / acapp acdoc aclay acsel acver dbdoc dblay name new ) (vl-load-com)
 ;; © Lee Mac 2011

 (setq acapp (vlax-get-acad-object)
       acdoc (vla-get-activedocument acapp)
       aclay (vla-get-layers acdoc)
       dbdoc (vla-getinterfaceobject acapp
               (if (< (setq acver (atoi (getvar 'ACADVER))) 16)
                 "ObjectDBX.AxDbDocument" (strcat "ObjectDBX.AxDbDocument." (itoa acver))
               )
             )
       dblay (vla-get-layers dbdoc)
 )
 (vlax-for layer aclay
   (cond
     ( (eq :vlax-true (vla-get-lock layer))
     )
     ( (wcmatch (strcase (setq name (vla-get-name layer))) "*-TEXT")
     )
     ( (ssget "_X" (list (cons 0 "*LEADER,*TEXT") (cons 8 name)))

       (if (null (tblsearch "LAYER" (strcat name "-TEXT")))
         (if (eq "0" name)
           (vla-put-color (vla-add aclay (strcat name "-TEXT")) (vla-get-color layer))
           (progn
             (vlax-invoke acdoc 'copyobjects (list layer) dblay)
             (vla-put-name (setq new (vla-item dblay name)) (strcat name "-TEXT"))
             (vlax-invoke dbdoc 'copyobjects (list new) aclay)
           )
         )
       )
       (vlax-for obj (setq acsel (vla-get-activeselectionset acdoc))
         (vl-catch-all-apply 'vla-put-layer (list obj (strcat name "-TEXT")))
       )
       (vla-delete acsel)
     )
   )
 )
 (vlax-release-object dbdoc)
 (princ)
)

Link to comment
Share on other sites

Thanks Lee, Those both are excelllent ! What would I change if I wanted to have the layers to be named "TEXT-*" instead of "*-TEXT" ? I think it's easier to have the text layers grouped together when the layer properties manager is being used.

 

And where did you learn lisp? Was it self taught? I really want to learn myself.

 

Thanks for everything !

 

Xi

Link to comment
Share on other sites

Thanks Lee, Those both are excelllent !

 

Thanks, you're very welcome Xi :)

 

What would I change if I wanted to have the layers to be named "TEXT-*" instead of "*-TEXT" ? I think it's easier to have the text layers grouped together when the layer properties manager is being used.

 

In the wcmatch expression, change:

 

"*-TEXT"

 

to:

 

"TEXT-*"

 

And change every occurrence of:

 

(strcat name "-TEXT")

 

to:

 

(strcat "TEXT-" name)

 

(Note: do not change the ssget filter list expression "*LEADER,*TEXT").

 

And where did you learn lisp? Was it self taught? I really want to learn myself.

 

Yes, my LISP knowledge is all self-taught - mostly learning from both the forums, Visual LISP Help Documentation and a little experimenting. :)

Link to comment
Share on other sites

Yes, my LISP knowledge is all self-taught - mostly learning from both the forums, Visual LISP Help Documentation and a little experimenting. :)
IMO that's the "best" way to learn any programming language: have some reference material, some samples, and some piers for when you get stuck. Then trial-n-error is the "real" teacher.
Link to comment
Share on other sites

IMO that's the "best" way to learn any programming language: have some reference material, some samples, and some piers for when you get stuck. Then trial-n-error is the "real" teacher.

 

I definitely agree. On the same topic, this article is an interesting read (posted at theSwamp recently).

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...