Jump to content
hanskes

change layer name based on original layer name

Recommended Posts

hanskes

I am looking for a script that can change the layer of an entity. It must be checked if the layer name ends with -I -W or -E and change to eg same-layername-W, if the layer does not end with -I, -W or -E the entire layer name must be changed to architectural-W. If a layer does not exist, it must be created and the color of layers ending with -W is always 10. A same script for changing to -I (color 7) and to -E (color 102). Any thoughts?.

Share this post


Link to post
Share on other sites
ronjonp
Posted (edited)

Have you searched for renaming layers? This is a very common question. See if you can find something close and maybe someone will help.

Google @ Here

Google @ TheSwamp

Google @ erwhere

Edited by ronjonp

Share this post


Link to post
Share on other sites
hanskes

I have done that. I am not a programmer so it is not easy to stick the right pieces of code together .. I have found this and modified it, but the other entities (that do not meet (-4 . "<or")(8 . "*?-I") (8 . "*?-W") (8 . "*?-E") (-4 . "or>") ) must be renamed to architectural-W, and this succeeds not me.

code i have:

(defun c:laag-E ( / enx idx lay sel e i l n s x )
   (if (setq sel (ssget "_:L" '((-4 . "<or")(8 . "*?-I") (8 . "*?-W") (8 . "*?-E") (-4 . "or>") ))) 
   
       (repeat (setq idx (sslength sel)) 
           (setq enx (entget (ssname sel (setq idx (1- idx)))) 
                 lay (assoc 8 enx) 
           )
           (entmod (subst (cons 8 (matchlayer (cdr lay) (substr (cdr lay) 1 (- (strlen (cdr lay)) 2)))) lay enx))   
                                                                                                                   
       )                                                                                                        
   )  
	(if (setq s (ssget "_P" '((8 . "~*-E")))) 
		   (repeat (setq i (sslength s)) 
			   (setq x (entget (ssname s (setq i (1- i)))) 
					 l (assoc 8 x) 
					 n (strcat (cdr l) "-E") 
			   )
			   (if (not (tblsearch "layer" n)) 
				   (progn                                                                           
					   (setq e (entget (tblobjname "layer" (cdr l)))) 
					   (entmake (subst (cons 2 n) (assoc 2 e) (subst '(62 . 102) (assoc 62 e) e)))    
																									
				   )
			   )
			   (entmod (subst (cons 8 n) l x)) 
		   )
		 		     
	   )	
   (princ)
   (command "_purge" "la" "" "n")
)

 

Share this post


Link to post
Share on other sites
BIGAL

Is it worth running a BYlayer on all the objects before changing name. Then only change the layer table. No sample dwg to look at.

Share this post


Link to post
Share on other sites
hanskes

Good point! Changed the code. Any thoughts about the  other entities (that do not match ( (-4 . "<or")(8 . "*?-I") (8 . "*?-W") (8 . "*?-E") (-4 . "or>") ) and must be renamed to architectural-W? I don't know how to achieve that.

 

(defun c:laag-E ( / enx idx lay sel e i l n s x )
  (command "._CHANGE" (ssget "_:L" ) "" "Properties" "Color" "256" "")
   (if (setq sel (ssget "_P" '((-4 . "<or")(8 . "*?-I") (8 . "*?-W") (8 . "*?-E") (-4 . "or>") ))) 
   
       (repeat (setq idx (sslength sel)) 
           (setq enx (entget (ssname sel (setq idx (1- idx)))) 
                 lay (assoc 8 enx) 
           )
           (entmod (subst (cons 8 (matchlayer (cdr lay) (substr (cdr lay) 1 (- (strlen (cdr lay)) 2)))) lay enx))   
                                                                                                                   
       )                                                                                                        
   )  
	(if (setq s (ssget "_P" '((8 . "~*-E")))) 
		   (repeat (setq i (sslength s)) 
			   (setq x (entget (ssname s (setq i (1- i)))) 
					 l (assoc 8 x) 
					 n (strcat (cdr l) "-E") 
			   )
			   (if (not (tblsearch "layer" n)) 
				   (progn                                                                           
					   (setq e (entget (tblobjname "layer" (cdr l)))) 
					   (entmake (subst (cons 2 n) (assoc 2 e) (subst '(62 . 102) (assoc 62 e) e)))    
																									
				   )
			   )
			   (entmod (subst (cons 8 n) l x)) 
		   )
		 		     
	   )	
   (princ)
   (command "_purge" "la" "" "n")
)

 

Quote 

 

Share this post


Link to post
Share on other sites
ronjonp
On 8/30/2019 at 12:00 AM, hanskes said:

It must be checked if the layer name ends with -I -W or -E

The filter you want for this looks like this: 

(setq s (ssget "_X" '((8 . "*-[IWE]"))))

And the opposite of the filter above is this:

(setq s (ssget "_X" '((8 . "~*-[IWE]"))))

 

Edited by ronjonp

Share this post


Link to post
Share on other sites
hanskes

ssget "_X"  makes a selection of all entities in a drawing. I want to select entities myself and then rename layers of those entities that end with -I, -W or -E differently from entities whose layer name does not end with -I, -W or -E.

Share this post


Link to post
Share on other sites
ronjonp
3 minutes ago, hanskes said:

ssget "_X"  makes a selection of all entities in a drawing. I want to select entities myself and then rename layers of those entities that end with -I, -W or -E differently from entities whose layer name does not end with -I, -W or -E.

Yes I know that but the filter is still relevant. 😉 If you want to do this in one selection you'll need to open up the filter and apply the logic within the loop.

Share this post


Link to post
Share on other sites
ronjonp
On 8/30/2019 at 12:00 AM, hanskes said:

if the layer name ends with -I -W or -E and change to eg same-layername-W

Does this mean you are changing -I and -E to -W ? Or is it SameName-I, SameName-E & SameName-W ? So in the end your drawing should only have 4 layers total ( assuming no xrefs and not counting layer 0 or defpoints )?

 

Share this post


Link to post
Share on other sites
hanskes

There are many different layers in a dwg that end with -i or -e or -w (or none of these, usually imported layers from another dwg). Layers ending in -w represent changes made by buyers of a home. layers ending in -e are for additions and -i for information purposes. It often happens that entities are changed during the process from, for example, -i to -w. I want to make a simple button for this. The selected items that do not have one of these suffix must be given a completely new name, for example architectural-w.

Share this post


Link to post
Share on other sites
ronjonp

Try this .. does not do anything for color of layers -I or -E since we're changing them all to -W

(defun c:foo (/ el jl n nn s)
  ;; RJP » 2019-09-04
  (cond
    ((setq s (ssget ":L"))
     (setq jl "Architectural-W")
     (foreach x	(vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))
       (cond
	 ((wcmatch (strcase (setq n (cdr (assoc 8 (setq el (entget x)))))) "*-[IEW]")
	  (entmod (append el (list (cons 8 (setq nn (strcat (substr n 1 (1- (strlen n))) "W"))))))
	  (entmod (append (entget (tblobjname "layer" nn)) '((62 . 10))))
	 )
	 ((entmod (append el (list (cons 8 jl)))))
       )
     )
     (and (setq el (tblobjname "layer" jl)) (entmod (append (entget el) '((62 . 10)))))
    )
  )
  (princ)
)
(vl-load-com)

 

Share this post


Link to post
Share on other sites
hanskes

exactly what I was looking for, thanks !!
Is it possible to also change the color of the selected entities to bylayer?

 

 

Edited by hanskes

Share this post


Link to post
Share on other sites
hanskes

found it out:

(defun c:laag-w (/ el jl n nn s)
  ;; RJP » 2019-09-04
  (command "._CHANGE" (ssget "_:L" ) "" "Properties" "Color" "256" "")
  (cond
    ((setq s (ssget "_P"))
     (setq jl "00-bouwkundig-W")
     (foreach x	(vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))
       (cond
	 ((wcmatch (strcase (setq n (cdr (assoc 8 (setq el (entget x)))))) "*-[IEW]")
	  (entmod (append el (list (cons 8 (setq nn (strcat (substr n 1 (1- (strlen n))) "W"))))))
	  (entmod (append (entget (tblobjname "layer" nn)) '((62 . 10))))
	 )
	 ((entmod (append el (list (cons 8 jl)))))
       )
     )
     (and (setq el (tblobjname "layer" jl)) (entmod (append (entget el) '((62 . 10)))))
    )
  )
  (princ)
)
(vl-load-com)

 

Share this post


Link to post
Share on other sites
ronjonp
On 9/7/2019 at 1:50 PM, hanskes said:

found it out:


(defun c:laag-w (/ el jl n nn s)
  ;; RJP » 2019-09-04
  (command "._CHANGE" (ssget "_:L" ) "" "Properties" "Color" "256" "")
  (cond
    ((setq s (ssget "_P"))
     (setq jl "00-bouwkundig-W")
     (foreach x	(vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))
       (cond
	 ((wcmatch (strcase (setq n (cdr (assoc 8 (setq el (entget x)))))) "*-[IEW]")
	  (entmod (append el (list (cons 8 (setq nn (strcat (substr n 1 (1- (strlen n))) "W"))))))
	  (entmod (append (entget (tblobjname "layer" nn)) '((62 . 10))))
	 )
	 ((entmod (append el (list (cons 8 jl)))))
       )
     )
     (and (setq el (tblobjname "layer" jl)) (entmod (append (entget el) '((62 . 10)))))
    )
  )
  (princ)
)
(vl-load-com)

 

Glad you got it sorted :) .. it can also be done like so:

(defun c:foo (/ el jl n nn s)
  ;; RJP » 2019-09-04
  (cond
    ((setq s (ssget ":L"))
     (setq jl "Architectural-W")
     (foreach x	(vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))
       (entmod (append (entget x) '((62 . 256))))
       (cond
	 ((wcmatch (strcase (setq n (cdr (assoc 8 (setq el (entget x)))))) "*-[IEW]")
	  (entmod (append el (list (cons 8 (setq nn (strcat (substr n 1 (1- (strlen n))) "W"))))))
	  (entmod (append (entget (tblobjname "layer" nn)) '((62 . 10))))
	 )
	 ((entmod (append el (list (cons 8 jl)))))
       )
     )
     (and (setq el (tblobjname "layer" jl)) (entmod (append (entget el) '((62 . 10)))))
    )
  )
  (princ)
)
(vl-load-com)

 

Share this post


Link to post
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
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

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