Moving entire layer(s) to a new defined layer by selection an object

Moving entire layer(s) to a new defined layer by selection an object

sam_safinia
Advocate Advocate
963 Views
12 Replies
Message 1 of 13

Moving entire layer(s) to a new defined layer by selection an object

sam_safinia
Advocate
Advocate

another challange for me!

 

I am going to move entire selected layer(s) of my drawing to a new predefined layer by selection just an object. Here is a lisp that I found (thanks dear author!) which is very useful but it creates new layer by adding suffix to selected object's layer name. I want to set the layer name on my routine, i.e. "CONTOURS" and move entire entities of selected layer(s) into it.

 

(defun c:laymv ( / ssobjlist ss_obj ss_obj_list obj )
;;;;;SUBROUTINES;;;;;
(defun ssobjlist (sset / zaehler objlist) 
  (setq zaehler (1- (sslength sset)))
  (while (>= zaehler 0)
    (if (< zaehler (1- (sslength sset)))
      (setq objlist (cons (ssname sset zaehler) objlist))
      (setq objlist (list (ssname sset zaehler)))
    ) ;Ende if
    (setq zaehler (1- zaehler))
  ) ;Ende While
  objlist
) ;Ende defun

;;;;;MAIN ROUTINE;;;;;
 (setq ss_obj (ssget "_:L" '((-4 . "<not")(8 . "0,Defpoints")(-4 . "not>"))))
 (if ss_obj
  (progn
   (command "_.undo" "_begin")
   (setq ss_obj_list (ssobjlist ss_obj))
;;;;;;;;;;;;;; Here would be the change?   
  (foreach obj ss_obj_list
    (setq obj_lay (cdr (assoc 8 (entget obj))))
    (if (not (wcmatch obj_lay "*_EX"))
     (progn
      (command "_.rename" "_la" obj_lay (strcat obj_lay "_EX"))
      (command "_.layer" "_co" 3 (strcat obj_lay "_EX") "")
      (princ (strcat "\nLayer \"" obj_lay "\" rename to: \"" obj_lay "_EX\""))
     );progn
    );if
;;;;;;;;;;;;;;;;;;;;;; );foreach (command "_.undo" "_end") );progn (princ "\nNothing selected, try again") );if (princ) )

Thanks

0 Likes
Accepted solutions (1)
964 Views
12 Replies
Replies (12)
Message 2 of 13

Kent1Cooper
Consultant
Consultant

I don't think you need any code for this.  LAYMRG [= LAYer MeRGe] lets you select just an object to define the Layer [or an object each on multiple Layers] you want to move to another, or to type in the Layer name, and the same for the target Layer [i.e., you can still do it even if there are no objects on the appropriate Layer(s) currently in view].

Kent Cooper, AIA
0 Likes
Message 3 of 13

sam_safinia
Advocate
Advocate

Yes that is true. This approach is just good for a single drawing modification but if I have couple of drawings, I do prefer to do it by code and instantly.

0 Likes
Message 4 of 13

Ajilal.Vijayan
Advisor
Advisor

try by changing the code into something like this.

   ;;;;;;;;;;;;;; Here would be the change? 
   (setq new_lay "CONTOURS")  
  (foreach obj ss_obj_list
    (setq obj_lay (cdr (assoc 8 (entget obj))))
    (if (not (wcmatch obj_lay new_lay))
     (progn
      (command "_.rename" "_la" obj_lay new_lay)
      (command "_.layer" "_co" 3 new_lay "")
      (princ (strcat "\nLayer \"" obj_lay "\" rename to: \"" new_lay))
     );progn
	 (princ "Layer name is same, nothing changed");
    );if

 

0 Likes
Message 5 of 13

sam_safinia
Advocate
Advocate

I found this lisp here (by Scot Harris) which is very close to my needs. The only change that I want to do is to disable user input layer naming and approval. I have set of layer name. Any suggestion? Thanks

0 Likes
Message 6 of 13

Kent1Cooper
Consultant
Consultant

@s_plant wrote:

I found this lisp.... The only change that I want to do is to disable user input layer naming and approval. I have set of layer name. ....


In simplest terms, try changing this part:

 

(if (not usermel) (setq usermel "0") );if
(setq a (strcase (getstring (strcat
  "Move Entire Layer \nEnter destination layer <" usermel ">: "))))
(if (= (strlen a) 0) (setq a usermel) );if

 

to this:

 

(setq a "YourLayerName")

 

It could be simplified further if you know the Layer will always already exist in the drawing, and will already be frozen if that's what you're after.  [And in some other ways -- there are some unnecessary elements there -- but first try the simple change.]

Kent Cooper, AIA
0 Likes
Message 7 of 13

sam_safinia
Advocate
Advocate

Thanks Kent, we are getting close. Now how can I change the color to "3" LT to "Center" and LW "0.25"?

Purge is not working?why?!!

 

(defun c:LEM ( / a s )
 (defun *error* (msg) (setvar "cmdecho" 1)
                      (setq a nil s nil)(princ) );end**
 (graphscr)
 (setq a "ABC")
        (if (setq a "ABC")
            (progn
             (setvar "cmdecho" 0)
            (command ".layer" "n" a "")
            (if (/= (substr (getvar "clayer") 1 2) (substr a 1 2))
                (command ".layer" "thaw" a "") );if
            (setvar "cmdecho" 1) );progn
           (setq a (strcase (getstring (strcat
                     "\n" a " invalid. Enter destination layer: ")))) );if
      (setq usermel a) ;progn
 (if (tblsearch "layer" usermel)
  (progn
    (while
   (setq a (entget (car (entsel "\nSelect an object: "))))
   (setq s (ssget "x" (list (cons 8 (cdr (assoc 8 a))))))
   (setvar "cmdecho" 0)
   (command ".chprop" s "" "la" usermel "")
   (setvar "cmdecho" 1)
   (princ (strcat "\n" (itoa (sslength s)) " object(s) on layer "
                  (cdr (assoc 8 a)) " moved to layer " usermel "."))
     ) ; while
      );progn		  
       );if
	   ;;;;  Purge is not working?!   ;;;;;
    (command "_.purge" "_all" "" "_no")
  (setq a nil s nil)
  (princ)
  ) ;_ end of defun
0 Likes
Message 8 of 13

ВeekeeCZ
Consultant
Consultant

Maybe try it like this. (Using Kent's suggestion on "LAYMRG" command)

 

Spoiler
(defun C:Merge2ABC ( / ss l li)
  
  (setvar 'CMDECHO 0)
  (if (and (not (command "_.-LAYER" "_M" (setq l "ABC") ""))
	   (princ "\nSelect object to merge into \"ABC\" layer ")
	   (setq ss (ssget)))
    (repeat (setq len (sslength ss))
      (if (and (tblsearch "layer" (setq li (cdr (assoc 8 (entget (ssname ss (setq len (1- len))))))))
	       (/= li l)
	       (/= li "0")
	       (/= li "Defpoints")
	       )
	(command "_.-LAYER" "_U" (cdr (assoc 8 (entget (ssname ss 0)))) ""
		 "_.LAYMRG" "_N" li ""
		            "_N" l "_Y"))))
  (setvar 'CMDECHO 1)
  (princ)
)
0 Likes
Message 9 of 13

Kent1Cooper
Consultant
Consultant

@s_plant wrote:

.... Now how can I change the color to "3" LT to "Center" and LW "0.25"?

Purge is not working?why?!!

 

 

 

You have a choice about how to handle the color/linetype/lineweight part.  You could add the color/linetype/lineweight to the Layer definition, and force all of those to be ByLayer for each object before moving it to that Layer.  Or you could force those as overrides on each object.

 

Purge may not be working because there may be things on that Layer nested in Block definitions -- that's one of the advantages of LAYMRG, which will change those, too, and will eliminate the old Layer so you don't need to Purge it.  Or it may still be the current Layer, which you can't Purge -- use the Make option instead of the New option in the Layer command to take care of that.

Kent Cooper, AIA
0 Likes
Message 10 of 13

sam_safinia
Advocate
Advocate

Thanks Kent for the advice.

 

BeeKeeCZ, thanks for you neat code. There is only one issue with this code which is fine with my modified lisp. When I change text style to layer "0" (I removed the exception part (/= li "0") ) it does not move text objects to the ABC layer.any idea?

 

(defun C:Merge2ABC ( / ss l li)
  
  (setvar 'CMDECHO 0)
  (if (and (not (command "_.-LAYER" "_M" (setq l "ABC") ""))
	   (princ "\nSelect object to merge into \"ABC\" layer ")
	   (setq ss (ssget)))
    (repeat (setq len (sslength ss))
      (if (and (tblsearch "layer" (setq li (cdr (assoc 8 (entget (ssname ss (setq len (1- len))))))))
	       (/= li l)
	    ;;   (/= li "0")
	       (/= li "Defpoints")
	       )
	(command "_.-LAYER" "_U" (cdr (assoc 8 (entget (ssname ss 0)))) ""
		 "_.LAYMRG" "_N" li ""
		            "_N" l "_Y"))))
  (setvar 'CMDECHO 1)
  (princ)
)
0 Likes
Message 11 of 13

Kent1Cooper
Consultant
Consultant

@s_plant wrote:

.... 

BeeKeeCZ, thanks for you neat code. There is only one issue with this code which is fine with my modified lisp. When I change text style to layer "0" (I removed the exception part (/= li "0") ) it does not move text objects to the ABC layer.any idea?

 


I would assume that's because LAYMRG Purges the source Layer, and it's not going to let you do that with Layer 0.  If you want to do things on Layer 0, you're going to need to use an approach that moves things one way or another, without getting rid of the source Layer, rather than by way of LAYMRG.  Presumably you could have a routine that checks for the source Layer's name first, and if it's not Layer 0, uses LAYMRG, but if it is, uses CHPROP or (subst)/(entmod) or (vla-put-Layer) or some such thing on all the objects on that Layer.

Kent Cooper, AIA
0 Likes
Message 12 of 13

ВeekeeCZ
Consultant
Consultant
Accepted solution

Ok, try this code. I improved the code with a couple of Kent's suggestions...

 

(defun C:Merge2ABC ( / ss l i ssi li)
  
  (setvar 'CMDECHO 0)
  
  (if (and (setq l "ABC")
	   (not (command "_.-LAYER" "_M" l "_C" "3" "" "_L" "Center" "" "_LW" "0.25" "" ""))
	   (princ (strcat "\nSelect object to merge into " l " layer "))
	   (setq ss (ssget ":L"))
      )
    (repeat (setq i (sslength ss))
      (if (and (tblsearch "layer" (setq li (cdr (assoc 8 (entget (ssname ss (setq i (1- i))))))))
	       (/= li l)
	  )
	(if (wcmatch li "0,Defpoints")
	  (if (setq ssi (ssget "_X" (list (cons 8 li))))
	    (command "_.LAYCUR" ssi ""))
	  (command "_.LAYMRG" "_N" li "" "_N" l	"_Y")))))

  (setvar 'CMDECHO 1)
  (princ)
)
Message 13 of 13

sam_safinia
Advocate
Advocate

Great job BeeKeeCZ and it's working perefectly! thanks alot

0 Likes