Jump to content

change layer name based on original layer name


hanskes

Recommended Posts

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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 

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

 

Link to comment
Share on other sites

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

 

 

Edited by hanskes
Link to comment
Share on other sites

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)

 

Link to comment
Share on other sites

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)

 

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