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

Change object color (not the layer)

8 REPLIES 8
Reply
Message 1 of 9
JFN_KSH
2683 Views, 8 Replies

Change object color (not the layer)

I want to change all pline in drawing to a specific object color (color 253). My code is not working & my brain is not functionning properly today.... Could somebody help...?

(DEFUN C:TR()
  (SETQ ALL_PLINE_IN_DRAWING (ssget "_X" '((0 . "LWPOLYLINE")))
	number_of_pline_in_dwg    (sslength ALL_PLINE_IN_DRAWING)
	entity (ssname ALL_PLINE_IN_DRAWING 1)
	data (entget entity)
	new_LAYER (list(cons 62 253))
	data (append new_LAYER data)
	)
(entmod data)
);END OF DEFUN

 

8 REPLIES 8
Message 2 of 9
Anonymous
in reply to: JFN_KSH

entmod could be rejecting your entity information because it may already have color information and your adding one to it without checking and changing the existing one.

 

I tried not to change too much of your code, Just taking out a couple lines of your code and adding an if condition.  this is one way to do it.

(DEFUN C:TR()
  (SETQ ALL_PLINE_IN_DRAWING (ssget "_X" '((0 . "LWPOLYLINE")))
    number_of_pline_in_dwg    (sslength ALL_PLINE_IN_DRAWING)
    x -1
   )
(while (not (= (setq x (1+ x)) number_of_pline_in_dwg))
(setq entity (ssname ALL_PLINE_IN_DRAWING number_of_pline_in_dwg)
      data (entget entity)
)
(entmod (if (assoc 62 data) (subst (cons 62 253) (assoc 62 data) data) (cons (cons 62 253))))
);while
);END OF DEFUN

 

 

oh, was just editing code for ssget, and ken has mentioned the second part.

Message 3 of 9
Kent1Cooper
in reply to: JFN_KSH


@Anonymous_Francois_Nant wrote:

I want to change all pline in drawing to a specific object color (color 253). My code is not working & my brain is not functionning properly today.... Could somebody help...?

(DEFUN C:TR()
  (SETQ ALL_PLINE_IN_DRAWING (ssget "_X" '((0 . "LWPOLYLINE")))
	number_of_pline_in_dwg    (sslength ALL_PLINE_IN_DRAWING)
	entity (ssname ALL_PLINE_IN_DRAWING 1)
	data (entget entity)
	new_LAYER (list(cons 62 253))
	data (append new_LAYER data)
	)
(entmod data)
);END OF DEFUN

 


That will do something to only one Polyline [the second one in the selection set -- the first is index number 0 in (ssname)].  You can find many examples of routines that step through selection sets in this Forum, to see how to make such a change to all of them.  It would make use of your number_of_pline_in_dwg variable, which as it is you are not using for anything.

 

[I would use a different variable name than new_LAYER for that, but....]

 

I think you should (append) those lists in the other order -- (append data new_LAYER).  Without trying it out in this specific situation, my experience with certain other entity data entries is that if there are two of the same association-number value [here the 62 for color], the later one "wins" the competition, and the earlier one is discarded.  If that's true for color entries, and a Polyline already has a color override, as you have it, it will keep the color it has, because that will be later in the entity data list, and the color-253 entry will "lose."  [It shouldn't make a difference if the Polyline does not have a color override, i.e. is Bylayer, and has no 62-code entry.]

 

If you have Polylines in different spaces [Model and Paper, and/or more than one Paper-space Layout], then to change them all, you need to do something like what you're trying to do, that modifies their entity data [or via VLA Properties].  But to do it to just things in one space, if that's where you are when you do it, it can be much simpler:

 

(command "_.chprop" (ssget "_X" '((0 . "LWPOLYLINE"))) "" "_color" 253 "")

 

Also consider whether you need to account for locked Layers.  If something is on one of those, would you want the routine to leave it alone, or unlock the Layer and change it?

Kent Cooper, AIA
Message 4 of 9
JFN_KSH
in reply to: JFN_KSH

Thanks for the reply. To find the error in my coding I remove the loping coding stuff... that's why it will (just for now) only change one pline. From what i've seen, if an object's color is set "bylayer" it does not contain the (62 . Color #). I tried to append this (62 . Color #) & then entmod it but it doesnt work...

Message 5 of 9
_Tharwat
in reply to: JFN_KSH

Hi Jean .

Try this routine .

(defun c:Test (/ ss i lst)
;; Tharwat 24. Sep. 2013 ;;
(or doc
(setq doc (vla-get-ActiveDocument (vlax-get-acad-object)))
)
(vlax-for l (vla-get-layers doc)
(if (eq :vlax-true (vla-get-lock l))
(progn
(setq lst (cons (vla-get-name l) lst))
(vla-put-lock l :vlax-false)
)
)
)
(if (setq ss (ssget "_X" '((0 . "LWPOLYLINE"))))
(repeat (setq i (sslength ss))
(entmod (append (entget (ssname ss (setq i (1- i))))
(list '(62 . 253))
)
)
)
)
(if lst
(foreach x lst
(vla-put-lock (vla-item (vla-get-layers doc) x) :vlax-true)
)
)
(vla-regen doc AcAllViewports)
(princ)
)
(vl-load-com)
Message 6 of 9
JFN_KSH
in reply to: JFN_KSH

(command "_.chprop" (ssget "_X" '((0 . "LWPOLYLINE"))) "" "_color" 253 "")

 this works great!

Message 7 of 9
Kent1Cooper
in reply to: JFN_KSH


@Anonymous_Francois_Nant wrote:

Thanks for the reply. To find the error in my coding I remove the loping coding stuff... that's why it will (just for now) only change one pline. From what i've seen, if an object's color is set "bylayer" it does not contain the (62 . Color #). I tried to append this (62 . Color #) & then entmod it but it doesnt work...


It works for me, whether or not the object already has a color override, but only when the (62 . Color#) is appended onto the end of the entity data list, not if it's put at the beginning.

 

Also, if you're testing it in something with only one LWPolyline in it, it won't work because it will try to change the second item in a selection set with only one item in it.

Kent Cooper, AIA
Message 8 of 9
JFN_KSH
in reply to: Kent1Cooper

Now I got you! Smiley Happy (at the end of the data list not at the begining). Thanks for the really great support!! My brain now feel's much better...

Message 9 of 9
_Tharwat
in reply to: JFN_KSH

You should account for the locked layers

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

Post to forums  

Autodesk Design & Make Report

”Boost