Jump to content

How to select a layer with his name and put it bylayer?


bono05

Recommended Posts

Hello,

 

My problem is that i need to put all layers to color 8 (for this no problem). But after this i need to set only one layer back to Bylayer.

 

My idea is to have a lisp where i can set his name (not by click or selection).

 

I hope can someone help me.

 

Thanks! ;)

Link to comment
Share on other sites

 
(defun BackToBYLayer (Lay)
(if (and
         (tblsearch "LAYER" Lay)
         (ssget "_X" (list (cons 8 Lay))))
(command "_Chprop" "P" "" "Color" "Bylayer" "")
   )(princ)
   )

 

USAGE:

(BackToBYLayer "LayerName")

Edited by pBe
Link to comment
Share on other sites

Or perhaps this one to work with entities inside blocks as well:

(vl-load-com)

(defun BackToByLayer2 (name / blk eo)
 (vlax-for blk (vla-get-Blocks (vla-get-ActiveDocument (vlax-get-acad-object)))
   (vlax-for eo blk
     (if (eq (strcase name) (strcase (vla-get-Layer eo)))
       (vl-catch-all-apply 'vla-put-Color (list eo 256))
     )
   )
 )
)

Link to comment
Share on other sites

Or perhaps this one to work with entities inside blocks as well:
(vl-load-com)

(defun BackToByLayer2 (name / blk eo)
(vlax-for blk (vla-get-Blocks (vla-get-ActiveDocument (vlax-get-acad-object)))
(vlax-for eo blk
(if (eq (strcase name) (strcase (vla-get-Layer eo)))
(vl-catch-all-apply 'vla-put-Color (list eo 256))
)
)
)
)

 

I see you notice the how first code behaves with Blocks too Irneb... Good one :thumbsup:

Link to comment
Share on other sites

I see you notice the how first code behaves with Blocks too Irneb... Good one :thumbsup:
Yep, I've learned that a while back on AUGI. But it's always a good idea to share :notworthy:
Link to comment
Share on other sites

 
(defun BackToBYLayer (Lay)
(if (and
         (tblsearch "LAYER" Lay)
         (ssget "_X" (list (cons 8 Lay))))
(command "_Chprop" "P" "" "Color" "Bylayer" "")
   )(princ)
   )

 

USAGE:

(BackToBYLayer "LayerName")

 

 

Hello,

 

Back from a long Week-end...

 

So i tried your code but what's de command? Because "backtobylayer" doesn't working with me?

 

Many thanks.

Link to comment
Share on other sites

Put the following code into your command line and hit enter and when it selects entities make it manually Bylayer. :)

 

(sssetfirst nil (ssget "_x" '((62 . )))

 

 

Tharwat

Link to comment
Share on other sites

with that snippet, you need to isolate the the specific layers first before doing that Tharwat

Hence the 'Layername" argument

 

(but nevertheless a useful bit) :)

Link to comment
Share on other sites

First:

(BackToBYLayer "LayerName");

Second:

use Irnebs code. it works well even with blocks

(BackToByLayer2 "LayerName")

 

 

Sorry but this is the result:

 

Command: (BackToBYLayer "A-_0041--_T-_N_NUMEROS DES LOCAUX NOUVEAU")

; error: AutoCAD rejected function: invalid table function argument(s):

"A-_0041--_T-_N_NUMEROS DES LOCAUX NOUVEAU" "A-_0041--_T-_N_NUMEROS DES LOCAUX

NOUVEAU"

Link to comment
Share on other sites

Put the following code into your command line and hit enter and when it selects entities make it manually Bylayer. :)

 

(sssetfirst nil (ssget "_x" '((62 . )))

Tharwat

 

 

And result for this one:

 

 

Command: (sssetfirst nil (ssget "_x" '((62 . 8))))

(nil )

Link to comment
Share on other sites

Tharwat, I think you misunderstood the idea: The OP wanted to change everything on a particular layer to be ByLayer, he's already set that layer's colour to 8.

 

Your idea is quite good otherwise, it also shows a way of using wildcard matching so the user need not enter the entire layername. In bobo's example he could've entered only A-_0041* to get all layers starting with A-_0041 in their name.

 

So here's how I'd do it:

(defun SelectByLayer (name /)
 (sssetfirst nil (ssget "_x" (list (cons 8 "LayerName"))))
)

(defun c:SelectByLayer (/ LName)
 (if (setq LName (getstring t "Type the layer's name (or wildcard) to select: "))
   (progn
     (SelectByLayer LName)
     (prompt (strcat "All entities on layer \"" LName "\" has been selected."))
   )
 )
 (princ)
)

(defun c:BackToLayer2 (/ LName)
 (if (setq LName (getstring t "Type the layer's name (or wildcard) of which to set to ByLayer: "))
   (progn
     (BackToByLayer2 LName)
     (prompt (strcat "All entities (even nested in blocks) on layer \""
                     LName
                     "\" has been set to color ByLayer."
             )
     )
   )
 )
 (princ)
)

(defun BackToByLayer2 (name / blk eo)
 (setq name (strcase name))
 (vlax-for blk (vla-get-Blocks (vla-get-ActiveDocument (vlax-get-acad-object)))
   (vlax-for eo blk
     (if (wcmatch (strcase (vla-get-Layer eo)) name)
       (vl-catch-all-apply 'vla-put-Color (list eo 256))
     )
   )
 )
)

Note the c: functions define commands to call the normal functions after asking the user to type in the name.

Link to comment
Share on other sites

Tharwat, I think you misunderstood the idea: The OP wanted to change everything on a particular layer to be ByLayer, he's already set that layer's colour to 8.

 

Your idea is quite good otherwise, it also shows a way of using wildcard matching so the user need not enter the entire layername. In bobo's example he could've entered only A-_0041* to get all layers starting with A-_0041 in their name.

 

So here's how I'd do it:

(defun SelectByLayer (name /)
 (sssetfirst nil (ssget "_x" (list (cons 8 "LayerName"))))
)

(defun c:SelectByLayer (/ LName)
 (if (setq LName (getstring t "Type the layer's name (or wildcard) to select: "))
   (progn
     (SelectByLayer LName)
     (prompt (strcat "All entities on layer \"" LName "\" has been selected."))
   )
 )
 (princ)
)

(defun c:BackToLayer2 (/ LName)
 (if (setq LName (getstring t "Type the layer's name (or wildcard) of which to set to ByLayer: "))
   (progn
     (BackToByLayer2 LName)
     (prompt (strcat "All entities (even nested in blocks) on layer \""
                     LName
                     "\" has been set to color ByLayer."
             )
     )
   )
 )
 (princ)
)

(defun BackToByLayer2 (name / blk eo)
 (setq name (strcase name))
 (vlax-for blk (vla-get-Blocks (vla-get-ActiveDocument (vlax-get-acad-object)))
   (vlax-for eo blk
     (if (wcmatch (strcase (vla-get-Layer eo)) name)
       (vl-catch-all-apply 'vla-put-Color (list eo 256))
     )
   )
 )
)

Note the c: functions define commands to call the normal functions after asking the user to type in the name.

 

 

Great!!! This one working for me.

 

Thanks to all!!!!!! :D

Link to comment
Share on other sites

Glad it works for you!

 

Here's a further enhancement:

(defun SelectByLayer (name /)
 (sssetfirst nil (ssget "_x" (list (cons 8 "LayerName"))))
)

(defun c:SelectByLayer (/)
 (if (or (not (eq (setq LName (getstring t "Type the layer's name (or wildcard) to select or <Enter to Select>: ")) ""))
         (setq LName (PickLayers))
     )
   (progn
     (SelectByLayer LName)
     (prompt (strcat "All entities on layer \"" LName "\" has been selected."))
   )
 )
 (princ)
)

(defun PickLayers (/ ss n lay LName)
 (prompt "\nSelect sample entities on the layers you want. ")
 (if (setq ss (ssget))
   (progn
     (setq n (sslength ss))
     (while (>= (setq n (1- n)) 0)
       (setq lay (cdr (assoc 8 (entget (ssname ss n)))))
       (if (not (member lay LName))
         (setq LName (cons lay LName))
       )
     )
     (setq lay LName
           LName ""
     )
     (foreach l lay
       (setq LName (strcat LName "," l))
     )
     (setq LName (substr LName 2))
   )
 )
 LName
)

(defun c:BackToLayer2 (/ LName)
 (if (or (not (eq (setq LName (getstring t "Type the layer's name (or wildcard) of which to set to ByLayer or <Enter to Select>: ")) ""))
         (setq LName (PickLayers))
     )
   (progn
     (BackToByLayer2 LName)
     (prompt (strcat "All entities (even nested in blocks) on layer \""
                     LName
                     "\" has been set to color ByLayer."
             )
     )
   )
 )
 (princ)
)

(defun BackToByLayer2 (name / blk eo)
 (setq name (strcase name))
 (vlax-for blk (vla-get-Blocks (vla-get-ActiveDocument (vlax-get-acad-object)))
   (vlax-for eo blk
     (if (wcmatch (strcase (vla-get-Layer eo)) name)
       (vl-catch-all-apply 'vla-put-Color (list eo 256))
     )
   )
 )
)

Allows you to select some entities on layers instead of typing in each layer name. It should work with multiple layers at once as well.

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