Jump to content

Layer selection


monk

Recommended Posts

Is there a cleaner / quicker way of achieving this;

 

(defun JHSETCOL1 ( /  )
(if (not JHCOL1)
		(setq JHCOL1 "7")
		)

(command "-layer" "C" JHCOL1 "*inv*" "")
(command "-layer" "C" JHCOL1 "*dim*" "")
(command "-layer" "C" JHCOL1 "*alias*" "")
	
 (princ)
 
)

 

Essentially I want to change the colour of a selection of layers using wildcards. I have a DCL box which you enter the colour "JHCOL1"

 

More interested in the layer selection side of lisp using wildcards...

Link to comment
Share on other sites

You can match multiple wildcard patterns by separating the patterns using commas, e.g.:

(defun jhsetcol1 ( col )
   (if (null col) (setq col 7))
   (command "_.-layer" "_c" col "*inv*,*dim*,*alias*" "")
   (princ)
)

(jhsetcol1 3)

Alternatively, using Vanilla AutoLISP without using the -LAYER command:

(defun jhsetcol2 ( col / def dxf enx lay )
   (if (null col) (setq col 7))
   (while (setq lay (tblnext "layer" (null lay)))
       (if (wcmatch (setq lay (cdr (assoc 2 lay))) "*inv*,*dim*,*alias*")
           (progn
               (setq enx (entget (tblobjname "layer" lay))
                     dxf (assoc 62 enx)
               )
               (entmod (subst (cons 62 (if (minusp (cdr dxf)) (- col) col)) dxf enx))
           )
       )
   )
   (princ)
)

(jhsetcol2 3)

Or, using Visual LISP:

(defun jhsetcol3 ( col )
   (if (null col) (setq col 7))
   (vlax-for lay (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
       (if (wcmatch (vla-get-name lay) "*inv*,*dim*,*alias*")
           (vla-put-color lay col)
       )
   )
   (princ)
)
(vl-load-com) (princ)

(jhsetcol3 3)

Note that the use of wcmatch in the above examples is case-sensitive.

Link to comment
Share on other sites

Wow, Lee that's about as in depth as you could want!

 

Perfect!

 

I think I vastly need to expand my code knowledge base!

Link to comment
Share on other sites

Tried your Visual lisp method, I now need to learn Visual lisp! Looks good!

 

The Visual LISP route may be more concise, but the Vanilla AutoLISP method will be faster - I would also suggest modifying the example VL code I've posted above to accept the VLA Document Object as an argument so that this object and the Application object needn't be repeatedly retrieved for multiple calls of the function.

Link to comment
Share on other sites

I would also suggest modifying the example VL code I've posted above to accept the VLA Document Object as an argument so that this object and the Application object needn't be repeatedly retrieved for multiple calls of the function.

 

After you replied at the weekend, I started looking up VL on afralisp, it started to make sense but I need to delve deeper when I have time away from my day job!

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