Anuncios

The Autodesk Community Forums has a new look. Read more about what's changed on the Community Announcements board.

Looking for some help to write a Lisp routine

vporrash141089
Advocate

Looking for some help to write a Lisp routine

vporrash141089
Advocate
Advocate

Hi all!

 

Recently we have made  some modifications where I work and now we need to do extra work and very repetitively for example I need to open 100 different drawings one by one and select a hatch and change it from color red to bylayer which is magenta color 6. also I need to select a line/polyline which is in color green and need to manually change it to color 82.

 

I found this lisp by Lee Mac that does something similar but not quite what I need.

 

(defun c:cc (/ usercol ss)
 (while (or (not (setq usercol (getint "\nSpecify Colour Code >  ")))
        (not (<= 0 usercol 256))))
 (prompt "Select all object to change to one color.")
 (if (setq ss (ssget))
   (command "_.ChProp" ss "" "_C" usercol ""))
 (princ))?

 

This ask for user input to determine the desired color and then requests to select the entity and does it perfectly but I need my tool to do it automatically since it is 2 exact processes.

 

To recap I need help with a lisp that will

 

  • Select hatch any hatch but if the hatch name is needed I can provide it does not need to get into blocks just the one hatch.
  • Select green lines/polylines and change color to 82.
  • After this process is done I run my plot script.

I will very much appreciate all help provided or any guidance on how I can modify the lisp above to do as I need.

 

Thanks in advance,

Victor P.

0 Me gusta
Responder
Soluciones aceptadas (2)
1.024 Vistas
8 Respuestas
Respuestas (8)

Tomislav.Golubovic
Advisor
Advisor

If you can write it as a .SCR file, you can use my Batching Routine to process all of the drawings with your .SCR

 

https://technexus.com.au/index.php/downloads/

0 Me gusta

Kent1Cooper
Consultant
Consultant

@vporrash141089 wrote:

.... I need to open 100 different drawings one by one and select a hatch and change it from color red to bylayer which is magenta color 6. also I need to select a line/polyline which is in color green and need to manually change it to color 82.

....


 

If you're going to select manually, and know which things you're after, you could just build the color assignments in:

 

(defun c:cc (/ ss)
 (prompt "Select Hatch object(s) to change to ByLayer color.")
 (if (setq ss (ssget))
   (command "_.ChProp" ss "" "_C" "ByLayer" ""))
 (prompt "Select Line/Polyline object(s) to change to color 82.")
 (if (setq ss (ssget))
   (command "_.ChProp" ss "" "_C" 82 ""))

 (princ)

)

 

Assuming that by red and green you mean specifically color numbers 1 and 3 [not other possibilities such as 10 and 90], you could prevent yourself from picking the wrong thing(s) with filtering in the (ssget) functions:

 

(defun c:cc (/ ss)
 (prompt "Select Hatch object(s) to change to ByLayer color.")
 (if (setq ss (ssget '((0 . "HATCH") (62 . 1)))); only red Hatch object(s)
   (command "_.ChProp" ss "" "_C" "ByLayer" ""))
 (prompt "Select Line/Polyline object(s) to change to color 82.")
 (if (setq ss (ssget '((0 . "*LINE") (62 . 3)))); only green Line/Polyline object(s)

   (command "_.ChProp" ss "" "_C" 82 ""))

 (princ)

)

 

[In the second half, that would also accept Splines, Xlines & Mlines, but with some more code could restrict itself to only Lines or Polylines.]  And you could have it restricted to a single object in each case, and you could have it forbid selection if the Layer is locked, and various other enhancements.

Kent Cooper, AIA
0 Me gusta

vporrash141089
Advocate
Advocate

Hi Kent,

 

First of all I really appreciate your response.

 

I have tried it and here is what I think, I does what I need however since this is a repetitive process over and over for hundreds of files I was thinking on the lisp actually selecting the hatch without the user having to do it manually.

 

Same thing for the second portion of the code. If there is only one hatch and one green line element shouldn't there be a way to have the code select it and change it for me?

 

Thanks again,

Victor.

0 Me gusta

Kent1Cooper
Consultant
Consultant
Solución aceptada

@vporrash141089 wrote:

.... If there is only one hatch and one green line element shouldn't there be a way to have the code select it and change it for me?

....


 

Yes.  You can remove the prompts to the User, and put an "_X" into the (ssget) functions to have the routine look for itself:

(defun c:cc (/ ss)
 (if (setq ss (ssget "_X" '((0 . "HATCH") (62 . 1)))); only red Hatch object(s)
   (command "_.ChProp" ss "" "_C" "ByLayer" ""))
 (if (setq ss (ssget "_X" '((0 . "*LINE") (62 . 3)))); only green Line/Polyline object(s)
   (command "_.ChProp" ss "" "_C" 82 ""))
 (princ)
)

 

The X means look through the entire drawing, so this depends on your being correct that there's only one of each such object, or you could have things changed that you don't want.

 

Another thing to be aware of:  The X in (ssget) will see things anywhere in the drawing, but the CHPROP command will see only things in the current space.  If this might be run in a drawing in which the target objects may not be in the current space, that can be accounted for with some different code.

 

[Same caveat as before about *LINE objects.]

Kent Cooper, AIA

vporrash141089
Advocate
Advocate

Hi Kent,

 

Works perfect, you have no idea how much this helps.

 

I know this may be a really frequent question but how does one learn LISP do you have to be a programmer I'd like to learn how to create this just like the one you came up with, can you point me in the right direction?

 

-Books

-Online courses

-Become a Programmer professionally

 

How did you get to this level?

 

Thank you again so much! 

0 Me gusta

Kent1Cooper
Consultant
Consultant

@vporrash141089 wrote:

... how does one learn LISP do you have to be a programmer I'd like to learn how to create this ….

-Books

-Online courses

....

How did you get to this level?

....


 

Go over to the Customization Forum and Search for words like learn, training, tutorial, books, etc. -- lots of suggestions, links, etc.  I personally learned everything I know from poking around there, seeing what others have done, looking into the AutoLisp Reference, and trial and error.

Kent Cooper, AIA

vporrash141089
Advocate
Advocate

Hi @Kent1Cooper ,  thanks for the advice and all your help, I just wanted to provide some details on how the code has worked out.

 

It works 99.9% of the time and it does it really smoothly, however I've ran into minor issues I probably should have mentioned from the beginning.

 

I will start with the least important  and most rare of two issues. I was plotting my file to PDF and we have some text that is 99% of the time color white, this color will plot black but 1% of the time it's 255,255,255 which after researching is a truecolor instead of an index color. So I tried doing this:

(if (setq ss (ssget "_X" '((0 . "HATCH") (62 . 1)))); only red Hatch object(s)

(command "_.ChProp" ss "" "_C" "ByLayer" ""))

(if (setq ss (ssget "_X" '((0 . "*LINE") (62 . 3)))); only green Line/Polyline object(s)

(command "_.ChProp" ss "" "_C" 82 ""))

(if (setq sstext (ssget "_X" '((0 . "TEXT") (255,255,255)))); 255 text to color 7

(command "_.ChProp" ss "" "_C" 7 ""))

 

Evidently the blue portion is mine and doesn't work basically I found out I could not get color 255 to plot so it was white text with white background.

 

The other and more frequent issue that I forgot at the beginning was that we have some files with a title on color yellow (2) and this title is a block entity with attributes the layer is magenta but the attribute is color (2) I need a similir routine that changes all "Block " "Text" "Attributes" from color 2 to color 52.

 

I found this code researching this forum and found:

 

(defun C:ATTEC (/ ss) ;Attribute Edit Color
(command "_-attedit" "_y" "" "" "" PAUSE "" "_c" PAUSE "_n")
(princ)

BTW Credits to @ВeekeeCZ 

 

Which is exactly what I need but same as we started requires user input and I want to automate the process, would you know how I can accomplish what I have tried to explain?

 

Any help will be very much appreciated.

 

Thanks,

Victor P.

0 Me gusta

ВeekeeCZ
Consultant
Consultant
Solución aceptada

@vporrash141089 wrote:

...

(if (setq sstext (ssget "_X" '((0 . "TEXT") (255,255,255)))); 255 text to color 7

(command "_.ChProp" ss "" "_C" 7 "")) ...


 

For TrueColors it's different code and also a different way of the description...

(if (setq sstext (ssget "_X" '((0 . "TEXT") (420 . 16777215))))
  (command "_.ChProp" sstext "" "_C" 7 ""))

Next time, if you want to know the different color, set it to some object and use this line

(entget (car (entsel)))

and grab the code of 420.

 

The second one is a bit more advanced, so here is a function for that:

(defun BlockAttributeColorReplace (old new / ss i ent)
  (if (setq ss (ssget "_A" (list '(0 . "INSERT") '(66 . 1) (cons 410 (getvar 'CTAB)))))
    (repeat (setq i (sslength ss))
      (setq ent (setq blk (ssname ss (setq i (1- i)))))
      (while (/= "SEQEND" (cdr (assoc 0 (entget (setq ent (entnext ent))))))
        (if (= old (cdr (assoc 62 (entget (setq ent (entnext ent))))))
          (command "_.-attedit" "_yes" "" "" "" ent "_color" new "_next"))))))

The usage:

(BlockAttributeColorReplace 2 52)

 

0 Me gusta