PDA

View Full Version : Change pastel colors to alternative colors by layer & elements & blocks



Jairame
07-21-2010, 04:06 PM
Here's my problem:
I have a series of 518 drawings. I'll be using scriptpro to run through them all and apply a LISP routine to do the following:

Loop through all of the layers in the drawing, and if they are yellow, cyan or magenta, change them to green, blue and red respectively. I also need it to run through any and all elements in the drawing to do the same thing and also to (command "burst") all blocks to apply the same color change to them. Make sense?

I have this code for bursting the blocks:


(defun c:bust ()
;(setvar "qaflags" 1)
(setq AllBlocks (ssget "X" (list (cons 0 "INSERT"))))
(while (/= AllBlocks nil)
(progn
(sssetfirst nil AllBlocks)
(c:burst)
(setq AllBlocks (ssget "X" (list (cons 0 "INSERT"))))
(setq doc (vla-get-ActiveDocument (vlax-get-acad-object)))
(vla-SendCommand doc (chr 27))
);progn
);while
(princ)
);defun

And I have this code (only works for one color, not all 3) for changing the elements in the drawing (only works in either paper or modelspace, not both which I need it to do):


(defun c:pastel ()
(setq ylo (ssget "X" ' ((62 . 2))))
(while (/= ylo nil)
(progn
(command "_.change" ylo "" "p" "color" "green" "")
(setq ylo (ssget "X" ' ((62 . 2))))
);progn
);while
(setq cya (ssget "X" ' ((62 . 4))))
(while (/= cya nil)
(progn
(command "_.change" cya "" "p" "color" "blue" "")
(setq cya (ssget "X" ' ((62 . 4))))
);progn
);while
(setq mag (ssget "X" ' ((62 . 6))))
(while (/= mag nil)
(progn
(command "_.change" mag "" "p" "color" "red" "")
(setq mag (ssget "X" ' ((62 . 6))))
);progn
);while
(setq doc (vla-get-ActiveDocument (vlax-get-acad-object)))
(vla-SendCommand doc (chr 27))
(princ)
);defun

Anyone want to help me out? It would be much appreciated.

Zortar
07-22-2010, 02:35 PM
First, is the "burst" part of your code working? It wasn't clear if it was.

Next, regarding the entity color changes I have a question: you are searching for entities with a DXF code 62 color but are you also making sure the layer definitions in the drawings are changed so that the colors are correct. That way all of the entities which are color BYLAYER will also get updated?

I guess I need to know: is your question on how to get it to work in both spaces or to help you figure out if your code is working?

tkmashl
07-22-2010, 02:44 PM
are you trying to change the LAYER color or the ENTITY color? Im usually prefer color assigned bylayer, not so much by entity..

Jairame
07-22-2010, 02:54 PM
I got the burst part of my code working successfully. That code only works in either paper or model space, not both.
I'm looking to change all of the colors, preferably ByLayer but entity would work as well.

Ideally I'd want something to loop through all of the layers, if the layer color is yellow, change it to green, if it's cyan, change it to blue, and if it's magenta, change it to red. It needs to change the colors in Blocks as well, that's why I was using Bust to do that.

Thanks for your help!

Zortar
07-22-2010, 03:13 PM
Here's how I would approach this in concept: first go through all of the layers in the drawing:
(setq layers (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))))

and set the color for each base upon a conditional statement (if its yellow change it to green, etc.). This will fix all of your layers and so any entities that are color=BYLAYER will be taken care of.

Now for the entities that are not BYLAYER they are either in blocks or not. I tend not to explode blocks because you lose attribute data and its just bad practice. I would cycle throught the block definitions like with the layers but using (vla-get-blocks) and search within the block definitions for entity colors like you are doing in your code and changing them using ENTMOD.

Finally, search through the drawing for all entities (you can bypass the INSERT entities as they should follow the block definition changes made above) and ENTMOD their DXF 62 values. That's how I would do it. I just don't have the time right now to write the code and post it. Good luck.

Jairame
07-22-2010, 04:38 PM
Ok this is what I have so far for the layers, it works well except I get this error
error: bad argument type: lentityp nil



(defun c:Pastel3 ( / col def a )

(setq col '((2 . 3) (4 . 5) (6 . 1)))

(while (setq def (tblnext "LAYER" (not def)))
(if (setq a (assoc (abs (cdr (assoc 62 def))) col))
(LM:ColourChange (tblobjname "LAYER" (cdr (assoc 2 def))) (cdr a))
)
)

(mapcar
(function
(lambda ( entry )
(if (setq ss (ssget "_X" (list (cons 62 (car entry)))))
(
(lambda ( x )
(while (setq e (ssname ss (setq x (1+ x))))
(LM:ColourChange e (cdr entry))
)
)
-1
)
)
)
)
col
)

(princ)
)


(defun LM:ColourChange ( ent col / el )
(entupd
(cdr
(assoc -1
(entmod
(if (assoc 62 (setq el (entget ent)))
(subst
(cons 62 col) (assoc 62 el) el
)
(append el (list (cons 62 col)))
)
)
)
)
)
)