lisp or defun command for changing color of selected object

lisp or defun command for changing color of selected object

vandenoosterkamp
Collaborator Collaborator
114 Views
9 Replies
Message 1 of 10

lisp or defun command for changing color of selected object

vandenoosterkamp
Collaborator
Collaborator

I'd like to be able to slect an object, type q1 for changing the color of that object to red and also qs change it back to bylayer. I tried

 

(defun c:q1 (/) (command "chprop" "color" "1" "") (princ))

 

but I have no clue why this does not work because when typing it all out in commandline after selecting does works.  Any help would be appreciated a LOT 😄

0 Likes
Accepted solutions (1)
115 Views
9 Replies
Replies (9)
Message 2 of 10

paullimapa
Mentor
Mentor
Accepted solution

try this:

(defun c:q1 (/ ss) (if (setq ss (ssget "_:L"))(command "_.Chprop" ss "" "_Color" 1 ""))(princ))

Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 3 of 10

vandenoosterkamp
Collaborator
Collaborator

Dear Paul,

 

thank you!!!!

0 Likes
Message 4 of 10

paullimapa
Mentor
Mentor

you are welcome...cheers!!!


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 5 of 10

vandenoosterkamp
Collaborator
Collaborator

For anyone wanting the qs also

 

(defun c:qs (/ ss) (if (setq ss (ssget "_:L"))(command "_.Chprop" ss "" "_Color" "bylayer" ""))(princ))

 

I keep on telling myself for 25 years now that I need to learn lisp language one day 😄

0 Likes
Message 6 of 10

Kent1Cooper
Consultant
Consultant

@vandenoosterkamp wrote:

I'd like to be able to slect an object, [then] type q1 for changing the color ....

.... when typing it all out in commandline after selecting ....


Those command definitions in Messages 2 & 5 require you to invoke the command before selecting.  But you can have it accept pre-selection of objects, i.e. you can invoke the command after selecting [or before, as desired].

(defun C:Q1 ()
  (command "_.chprop"
    (cond
      ((ssget "_:L-I")); pre-selection
      ((ssget "_:L")); prompt to select
    ); cond
    "" "_color" 1 ""
  ); command
  (prin1)
)

If you already have anything pre-selected when you call the command, it will just go ahead and change the color [except of anything among the selection on locked Layers, since pre-selection might include locked things], the same as with other editing commands that accept pre-selection.  If you don't have anything selected, it will ask you to select, and then change the color [again, as with other editing commands].  No variable is needed to hold the selection.

And similarly with the ByLayer reset version, though the SETBYLAYER command can do that for you [you could have a QS command that basically just calls SETBYLAYER with SETBYLAYERMODE setting and prompt responses built in].

Kent Cooper, AIA
0 Likes
Message 7 of 10

Kent1Cooper
Consultant
Consultant

Come to think of it, a function that takes an argument, with command names defined to call that function and supply the argument, will save a lot of code duplication, especially if you need a large number of different colors.

(defun QC (col); function with argument
  (command "_.chprop"
    (cond
      ((ssget "_:L-I")); pre-selection
      ((ssget "_:L")); prompt to select
    ); cond
    "" "_color" col ""
  ); command
  (prin1)
)

(defun C:Q1 () (QC 1)) ; red

(defun C:Q2 () (QC 2)) ; yellow

(defun C:QS () (QC "Bylayer"))

That just contains commands for red, yellow and ByLayer, but make as many commands as you want.  The "working" code is needed only once.

Put that into a file called, for example, ColorChanger.lsp, and an (autoload) function in something like acaddoc.lsp can make all the commands available in any drawing, without actually loading the file until one of them is called for:

(autoload "ColorChanger" '("Q1" "Q2" "QS"))
Kent Cooper, AIA
0 Likes
Message 8 of 10

paullimapa
Mentor
Mentor

Curious, with pickfirst set to 1, I don't need to use: 

(ssget "_:L-I")

This works just fine:

(ssget "_:L")

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 9 of 10

Kent1Cooper
Consultant
Consultant

@paullimapa wrote:

Curious, with pickfirst set to 1, I don't need to use: 

(ssget "_:L-I")

This works just fine:

(ssget "_:L")

That is curious.  The I is for Implied selection [i.e. whether there is/are any pre-selected object(s)], so it would not have occurred to me to not check for that [the first condition in the (cond) function].

But sure enough, it's not necessary [if PICKFIRST = 1] to evaluate whether or not anything is pre-selected.  So your version would not prompt for selection every time, as I assumed.  But that also means no selection set variable is required, and this is all that's needed [lightly tested]:

(defun C:Q1 ()
  (command "_.chprop" (ssget "_:L") "" "_color" 1 "")
  (prin1)
)

 

Kent Cooper, AIA
0 Likes
Message 10 of 10

Sea-Haven
Mentor
Mentor

Just a comment I have a reactor program and what it does is traps an error from the Unable to recognize command "L1" so you could load one program with the reactor and then just type "L1" it will then ask to select object. if you want say Cyan you type "L4" it would only work with index colors as a start but could use RGB, as they are 9 characters long index is max 3.

 

Change color is in it. So is draw circle, Fillet and offset.

 

PLEASE NOTE you will see an error message but ignore. 

0 Likes