Change all elements to 1 layer, while retaining properties

Change all elements to 1 layer, while retaining properties

curiouslymind3d
Observer Observer
823 Views
10 Replies
Message 1 of 11

Change all elements to 1 layer, while retaining properties

curiouslymind3d
Observer
Observer

Hi,

 

I currently have a LISP that changes all objects within a file to a new layer. The problem is that I have hundreds of objects, hundreds of files, and many of the properties are set to bylayer. This then causes the colours, lineweights, and linestyles to all change after running my LISP.

 

Is there a way to retain all of the properties without having to manually change hundreds of objects from bylayer to the specific property?

 

Thanks all 🙂

 

0 Likes
824 Views
10 Replies
Replies (10)
Message 2 of 11

Moshe-A
Mentor
Mentor

@curiouslymind3d hi,

 

Have you thought of WBLOCK these objects, bring them back to that layer as xref. i think this is more quicker and you can turn freeze\thaw this layer\xref with a click.

 

Moshe

 

0 Likes
Message 3 of 11

Kent1Cooper
Consultant
Consultant

A start would be BEL0CL.lsp, >here<.  It's for things in Blocks, and later in the thread is one to include nested Blocks, but if it looks promising, it can easily be made to do the same for everything in a drawing, and for some other Layer.  Look elsewhere in that topic for variations.

Kent Cooper, AIA
0 Likes
Message 4 of 11

pbejse
Mentor
Mentor

@curiouslymind3d wrote:

Is there a way to retain all of the properties without having to manually change hundreds of objects from bylayer to the specific property?


Yes its doable.

For any entity having "not by layer" properties will be retain? or the object will be assigned to another layer name inheriting the fudge properties?

For example:
3 LINE entities under "KERB" layer, the properties for 1 of them is "BYLAYER", the other one has BLUE as color and HIDDEN as linetype, the last one has CYAN as color and HIDDEN as linetype.


What will happen then? Create a layer "KERB-2"  with BLUE and HIDDEN properties just in case "KERB" layer is GREEN and CONTINUOUS?  and the same applies for the other one? 


@curiouslymind3d wrote:

I currently have a LISP that changes all objects within a file to a new layer. The problem is that I have hundreds of objects, hundreds of files, and many of the properties are set to bylayer. This then causes the colours, lineweights, and linestyles to all change after running my LISP.


We can however modify your existing lisp program and change the part where it assigns BYLAYER to all entities, this is far more easier.  

EDIT: As per topic title, you wanted it 1 Layer for all? and what will that layer be?

 

Right now it assigns layer "0" to all objects.

(defun c:LayerZero (/ aDoc layers resetTo aclayer status setTo layeritem objectLayer)  
(vl-load-com)
(setq aDoc    (vla-get-ActiveDocument (vlax-get-acad-object))
      aclayer (vla-get-name (vla-get-ActiveLayer adoc))
      setTo '("Color" "Linetype" "Lineweight")
      status  '("LayerOn" "lock" "Freeze" )
)
  (vlax-for itm	(vla-get-layers adoc)
    (setq resetTo (cons
		    (append (list itm
				  (mapcar '(lambda (p)
					     (vlax-get itm p)
					   )
					  (cons "Name" (append setTo status))
				  )
			    )
		    )
		    resetTo
		  )
    )
    (mapcar '(lambda (r rn)
		       (vlax-put itm r rn) )
	    status (if (eq (Vla-get-name itm) aclayer)
			   '(-1 0) '(-1 0 0 ))
	  )
    
    )
      (vlax-for	blk (vla-get-blocks aDoc)
	(cond
	  ((minusp (vlax-get blk 'isXref)))
	  ((vlax-for h blk
	     (cond
	       ((null (vlax-write-enabled-p h)))
	       ((eq (vla-get-ObjectName h) "AcDbBlockReference")
			(Vla-put-layer h "0")
		)
	       ((setq objectLayer
		       (assoc (vla-get-layer h)
			      (mapcar 'cadr resetTo)
		       )
		)
		(mapcar	'(lambda (p v c / cv )
				   (cond
				     ((not (vlax-property-available-p h p)))
				     ((/= (setq cv (vlax-get h p)) c)
				      (vlax-put h p cv)
				     )
				     ((vlax-put h p v))
				   )
			   )
			setTo (Cdr objectLayer) '( 256 "ByLayer" "ByLayer" )

		)
		(Vla-put-layer h "0")
	       )
	     )
	   )
	  )
	)
      )
      (vla-regen aDoc acAllViewports)
      (princ)
      )

command: LayerZero

 

HTH

 

0 Likes
Message 5 of 11

john.uhden
Mentor
Mentor

@curiouslymind3d ,

Ah, but things like color and linetype are specific properties and are usually ByLayer.

So do you mean to gather the properties of the original layer and apply them as overrides to the objects after changing their layers all to one (that is unless the object already has overridden properties)?

John F. Uhden

0 Likes
Message 6 of 11

curiouslymind3d
Observer
Observer

Yes that is correct, I need to apply the overrides within the properties rather than bylayer. Is there a method for this?

0 Likes
Message 7 of 11

curiouslymind3d
Observer
Observer

I haven't yet, I'll look further into this. Thanks!

0 Likes
Message 8 of 11

curiouslymind3d
Observer
Observer

Thanks Kent, will have a look

0 Likes
Message 9 of 11

EnM4st3r
Advocate
Advocate

i also have one that puts them on layer 0.

but this does not change some dimension colors and similar things.
Also it doesnt take the transparency value from the layer.

(defun c:layZ (/ layerswap doc)
  ;| Sets object layer by string. Creates if neccessary
    @Param obj \<vla-obj>
    @Param str \<string> name of the layer the object should be set to.
    @Returns ?
  |;
  (defun layerswap (obj str / makelayer doc oldlayer oldlock)
    (defun makelayer (str / layers)
      (setq layers (vla-get-layers doc))
      (if (not (vl-catch-all-error-p (vl-catch-all-apply 'vla-item (list layers str))))
        t
        (vla-add layers str)
      )
    )
    (setq doc (vla-get-activedocument (vlax-get-acad-object)))
    (if (makelayer str)
    (progn 
      (setq oldlayer (vla-item (vla-get-layers doc)(vla-get-layer obj)))       
      (setq oldlock (vla-get-lock oldlayer))
      (vla-put-lock oldlayer :vlax-false)    
      (vla-put-layer obj str)    
      (vla-put-lock oldlayer oldlock)
    )
    )
  )
  
  (setq doc (vla-get-activedocument (vlax-get-acad-object)))
  (vlax-for item (vla-get-modelspace doc) ; goes trough every drawing object in model area
    (mapcar '(lambda (property layervalue)
                (if (and (vlax-property-available-p item property t) ;checks if the item has the property
                          (= layervalue (vlax-get-property item property)) ;checks if item is ByLayer
                    )
                    (vlax-put-property item property (vlax-get-property (vla-item (vla-get-layers doc)(vla-get-layer item)) property)) ;gets layer property and sets it to item
                )
              )
              '("Color" "LineType" "LineWeight"); Property
              '(256 "ByLayer" -1); Layervalue     
    )
    (layerswap item "0") ; sets obj layer to 0
  )
)
0 Likes
Message 10 of 11

pbejse
Mentor
Mentor

@curiouslymind3d 
Have you looked at the code at post #4, only thing the code has not considered is the target layer name, for now its using layer "0", we can add a prompt for the layer name or we can hardcode it, just let us know if the code met your requirement to retain the objects properties of each entity.

0 Likes
Message 11 of 11

john.uhden
Mentor
Mentor

@curiouslymind3d ,

Yes, there is a method for that.

Suppose the object is currently on layer "A" and you want to change it to layer "B"."

If some of the object's properties are ByLayer, then the way to keep their color, linetype, etc. as is
is to hardwire those properties to the values of its current layer "A."

Now suppose layer "B" has the same color as layer "A" then you wouldn't have to hardwire its color, but if layer "B" is being created as part of the process, then its color would be white by default and maybe you would want to change layer "B's" color afterward.  So you might prefer to hardwire the properties to guarantee that their appearance remains the same after being traded.

Another way would be to change layer "B's" properties to those of layer "A", but since it is most likely that not all objects are on layer "A" then layer "B" would suffer numerous genetic alterations and the last object, say on layer "Z", would win.

My guess is that you would prefer to hardwire, except that future property changes would have to be made on an object by object basis, including those nested within blocks.

Then there is the topic of layout viewport overrides which would become meaningless if everything is hardwired.

You must decide.

John F. Uhden

0 Likes