Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

a lisp to make all entities color "by layer"

26 REPLIES 26
Reply
Message 1 of 27
mikecharbel
14476 Views, 26 Replies

a lisp to make all entities color "by layer"

hello i have an xref that i want to be in color 8 i open it and put all layers in color 8 , then selected all objects and put it by layer but the problem is that i need to enter each block to put its components color by layer so i was wondering if there any lisp that can put all the drawing entities colors by layer , blocks components included thanks in advance
26 REPLIES 26
Message 2 of 27
p_mcknight
in reply to: mikecharbel

(defun c:test1 ( / obj vlobj )
  ;Start with regular entities
  (setq obj (entnext))
  (while obj
    (setq vlobj (vlax-ename->vla-object obj))
    (vla-put-color vlobj 5)
    (setq obj (entnext obj))
    )
  ;Process entities within blocks
  (setq mainBlock (tblnext "block" t))
  (while mainBlock
    (setq childEnt (cdr (assoc -2 mainBlock)))
    (while childEnt
      (setq vlobj (vlax-ename->vla-object childEnt))
      (vla-put-color vlobj 256)
      (setq childEnt (entnext childEnt))
      );end while
    (setq mainBlock (tblnext "block"))
    )
  (command ".regen")
  (princ)  
  )

Message 3 of 27
p_mcknight
in reply to: p_mcknight

The above post didn't have all of the variables called as local.  See corrected below.

 

(defun c:test1 ( / obj vlobj mainBlock childEnt vlobj)
  ;Start with regular entities
  (setq obj (entnext))
  (while obj
    (setq vlobj (vlax-ename->vla-object obj))
    (vla-put-color vlobj 5)
    (setq obj (entnext obj))
    )
  ;Process entities within blocks
  (setq mainBlock (tblnext "block" t))
  (while mainBlock
    (setq childEnt (cdr (assoc -2 mainBlock)))
    (while childEnt
      (setq vlobj (vlax-ename->vla-object childEnt))
      (vla-put-color vlobj 256)
      (setq childEnt (entnext childEnt))
      );end while
    (setq mainBlock (tblnext "block"))
    )
  (command ".regen")
  (princ)  
  )

Message 4 of 27
Kent1Cooper
in reply to: mikecharbel

Put some group of words like "change all Block entities" or "all entities in Blocks to Bylayer" or the like into the Search window, and you should find various threads about similar things.  There are routines that change the color of all entities in all Blocks to Bylayer, and/or that move all entities in Blocks to Layer 0, etc., so if there isn't one that does exactly what you want, there certainly are those that would, with some slight adjustment.

Kent Cooper, AIA
Message 5 of 27
mikecharbel
in reply to: Kent1Cooper

thanks a lot )

Message 6 of 27
mikecharbel
in reply to: mikecharbel

it doesnt work ( ; error: Automation Error. On locked layer

and some by layer objects became on blue !!!

Message 7 of 27
dbroad
in reply to: mikecharbel

Mike,

Won't the command SETBYLAYER work for you?  If not, then you might be approaching the problem wrong.  Xref's need to be changed externally if the colors are embedded into their blocks.  IOW, you will need an xref that has had SETBYLAYER run on it including blocks.  Then attach that XREF and put it on the layer you want.  VISRETAIN only affects layer settings, not the XREF database.

Architect, Registered NC, VA, SC, & GA.
Message 8 of 27
mikecharbel
in reply to: dbroad

SETBYLAYER is exactly what i was searching for

thank you very much )

Message 9 of 27
mikecharbel
in reply to: mikecharbel

hy , is there a way to affect dimensions and dimensions inside blocks also ?

thank you

Message 10 of 27
Kent1Cooper
in reply to: mikecharbel


@mikecharbel wrote:

hy , is there a way to affect dimensions and dimensions inside blocks also ?

thank you


This seems to do that, in limited testing:

 

;;  AllColorBylayer.lsp [command name: ACB]
;;  To change the Color of ALL entities in the drawing, including those nested in
;;    Block definitions [but not Xrefs] and Dimension/Leader parts, to ByLayer.
;;  Kent Cooper, 27 February 2014, expanding on some elements by p_mcknight

 

(vl-load-com)
(defun C:ACB ; = All to Color Bylayer
  (/ cb ent obj blk subent)
  (defun cb () ; = force Color(s) to Bylayer
    (setq obj (vlax-ename->vla-object ent))
    (vla-put-color obj 256); ByLayer
    (if (wcmatch (vla-get-ObjectName obj) "*Dimension,*Leader")
      (foreach prop '(DimensionLineColor ExtensionLineColor TextColor)
        ;; not all such entity types have all 3 properties, but all have at least one
        (if (vlax-property-available-p obj prop)
          (vlax-put obj prop 256); ByLayer
        ); if
      ); foreach
    ); if
  ); defun -- cb
;;  Top-level entities:
  (setq ent (entnext))
  (while ent
    (cb)
    (setq ent (entnext ent))
  ); while
;;  Nested entities in this drawing's Block definitions:
  (setq blk (tblnext "block" t))
  (while blk
    (if (= (logand 20 (cdr (assoc 70 blk))) 0); not an Xref [4] or Xref-dependent [16]
      (progn
        (setq ent (cdr (assoc -2 blk)))
        (while ent
          (cb)
          (setq ent (entnext ent))
        ); while
      ); progn
    ); if
    (setq blk (tblnext "block"))
  ); while
  (command "_.regenall")
  (princ)
); defun

Kent Cooper, AIA
Message 11 of 27
Lee_Mac
in reply to: Kent1Cooper

To set the object colour to bylayer for all objects (including nested objects), I would suggest something along the lines of:

(more block exceptions may be required)

 

(defun c:acb ( / d )
    (vlax-for b (vla-get-blocks (setq d (vla-get-activedocument (vlax-get-acad-object))))
        (if (and (= :vlax-false (vla-get-isxref b)) (not (wcmatch (vla-get-name b) "`*D*,_*")))
            (vlax-for o b (vl-catch-all-apply 'vla-put-color (list o acbylayer)))
        )
    )
    (vla-regen d acallviewports)
    (princ)
)
(vl-load-com) (princ)

 

I would not advise changing the colour of dimension lines & extension lines using Kent's code, since these colour changes will appear as Dimension Style overrides attached as xdata to each object, which could result in confusion should you wish to alter the Dimension Style properties at a later date and wonder why your dimensions aren't changing without performing a dimension update.

 

Instead, if you wish to change the colour of objects constituting Dimensions, MLeaders, Tables (such as dimension extension lines, arrows, table grid lines, table cell text) I would recommend altering these properties through the relevant styles (note that you would need to use the ActiveX copyfrom method in order to alter the dimension style).

 

However, this would require substantially more code to achieve...

Message 12 of 27
dbroad
in reply to: Lee_Mac

I agree with Lee that it may be problematic to globally change dimensions since some dimensions could have deliberate style overrides.  It would also involve extensive programming to remove colors from other special objects (mtext, tablestyles, mleader styles, aec objects, etc).

 

This would be a modest step past Lee's

(defun c:test (/ doc ods)
  (setq doc (vla-get-activedocument (vlax-get-acad-object)))
  ;;fix all dimstyles
  (setq ods (getvar "dimstyle"))
  (vlax-for d (vla-get-dimstyles doc)
    (vla-put-activedimstyle doc d)
    (foreach n '("dimclrt" "dimclrd" "dimclre")
      (setvar n 256)
    )
    (vla-copyfrom d doc)
  )
  ;;need more code to fix mleader styles, and table styles.  Consider stripmtext by others.
					;.........
  ;;fix all objects (does not include mtext color encoding)
  (vlax-for b (vla-get-blocks doc)
    (if	(= (vla-get-isxref b) :vlax-false)
      (vlax-for	n b
	(if (wcmatch (vla-get-objectname n) "*Dim*")
	  (vla-put-stylename n (vla-get-stylename n))
	  (vl-catch-all-apply 'vla-put-color (list n 256))
	))))
  (setvar "dimstyle" ods)
  (vla-regen doc acallviewports)
)

 

Architect, Registered NC, VA, SC, & GA.
Message 13 of 27
Lee_Mac
in reply to: dbroad

Nice extension to the code Doug Smiley Happy

Message 14 of 27
dbroad
in reply to: Lee_Mac

Thanks Lee.

Architect, Registered NC, VA, SC, & GA.
Message 15 of 27
Kent1Cooper
in reply to: Lee_Mac


@Lee_Mac wrote:

.... 

I would not advise changing the colour of dimension lines & extension lines using Kent's code, since these colour changes will appear as Dimension Style overrides attached as xdata to each object, which could result in confusion should you wish to alter the Dimension Style properties at a later date and wonder why your dimensions aren't changing without performing a dimension update.

 

Instead, ... I would recommend altering these properties through the relevant styles ....


That, by itself, will not always work.  The text, extension lines and dimension lines in Dimensions can have color overrides, which merely changing their colors in the Style definition will not overcome.  And as far as I can tell, those color-assignable parts don't have any kind of "ByDimensionStyle" color value that could be assigned.  I suspect that it would require changing the colors in the Style definition and stripping away any color-related extended-data entries.  [I'm not sure how to do that, never having had occasion to, but I imagine it can be done.]  But you wouldn't want to remove all extended data, because that would change other non-color-related aspects, notably dimension- and/or extension-line suppressions.  That's also why you wouldn't want to use Dim Update, but in any case you can't do that with Dimensions in Blocks.

 

In light of the original question, I suspect the drawings they want to do this to are something like backgrounds [such as floor plans] very likely from someone else, in which case there would be none of the described confusion, for several reasons.

Kent Cooper, AIA
Message 16 of 27
Lee_Mac
in reply to: Kent1Cooper

I think you missed my point Kent -

 

Obviously altering only the Dimension Style properties would not affect those dimension objects which have Dimension Style overrides applied, however, using this method would not only reveal those dimensions with style colour overrides (since these would remain unaltered), but furthermore, one would assume that such overrides had been applied for a particular reason, and shouldn't be wiped out by your code.

 

Nevertheless, regardless of the above point, my earlier post was making the point that additional dimension style overrides should not be introduced (as would result from using your code), as this is likely to cause subsequent confusion should the Dimension Style colours need to be globally amended.

 

Lee

 

Message 17 of 27
Anonymous
in reply to: mikecharbel

Use the commant 'SETBYLAYER'

Message 18 of 27
luisf_torres
in reply to: Kent1Cooper

I tried you code but still doesn't change colors by layers inside blocks & dimensions...

😞

Message 19 of 27
Kent1Cooper
in reply to: luisf_torres


@luisf_torres wrote:

I tried you code but still doesn't change colors by layers inside blocks & dimensions...


I just tried it again, and it worked, including on non-Bylayer-colored parts of a Dimension nested inside a Block.  Is there any kind of message?  Are certain kinds of things changing but not others?  Bear in mind that it makes everything Bylayer, but leaves it on the Layer it was drawn on, so [for example] parts of Blocks drawn on different Layers will stay on their original Layers and have the colors of those Layers, not the color of the Layer that the Block is inserted on.  If something like that is the problem you're having, there are routines on this Forum and elsewhere to do things like change all objects in Block definitions to Layer 0 and color Bylayer, and assorted other variations.  If you post a sample drawing with before and after parts, or separate before and after drawings, with some explanatory information, it may be possible to tell what's not working as you expect.

Kent Cooper, AIA
Message 20 of 27
owitzki
in reply to: Kent1Cooper

Hi Kent,

 

I found this lisp (changing color entities bylayers) it works in some drawings but somehow in this particular attached drawing doesn't.

 

Also found stripmtext (smt), this routine is also very helpful same as yours (acb). I prefer to use yours because I only need colors to set all bylayers.

 

Thanks a lot anyways! 🙂

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost