Jump to content

Simple colour change lisp


gazzalp

Recommended Posts

Guys, i quite often have to draw lines on a certain layer, but then have to change the colour of half of them as i go, so rather then select the line, go up to the colour drop down and select the colour, i want a lisp where i can select the line, type in the number that represents the colour (ie: 1 = red, 2 = yellow) and it will change the line. I dont want it to change the colour of the layer so that everything i draw from then on will be that colour, but just change the colour of any entities i currently have selected. Im sure it would be a pretty easy lisp. one thing im not sure about is if i could have all the colours in one lisp, or if id need a new lisp for each colour.

 

Thanks for your help

Link to comment
Share on other sites

  • Replies 33
  • Created
  • Last Reply

Top Posters In This Topic

  • Lee Mac

    14

  • totzky

    7

  • gazzalp

    4

  • Baber62

    3

Don't know anythin about lisps but if you wan't you can just change one lines color and use matchprop to do the other ones in the same color.

The problem is if you want an other color you have to do it over again, but its still faster!

If you do this thing allot like you say then maybe a lisp be better!

Link to comment
Share on other sites

I know about the match properties command, but it still takes longer then i want - id have to go up to the toolbar, select the button, select the entity i want the line matched to, then select the line. And as you said if nothing is already that colour then that wont work. A lisp however will only require me to touch one button on the keyboard.

Link to comment
Share on other sites

Here is simple 1. Change the term layer_name to layer that you want to change. It will allow to select LINEs on that LAyer only. As many as you want:

 

[b][color=BLACK]([/color][/b]defun c:clc [b][color=FUCHSIA]([/color][/b]/ ss clr[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]and [b][color=NAVY]([/color][/b]setq ss [b][color=MAROON]([/color][/b]ssget [b][color=GREEN]([/color][/b]list [b][color=BLUE]([/color][/b]cons 0 [color=#2f4f4f]"LINE"[/color][b][color=BLUE])[/color][/b]
                            [b][color=BLUE]([/color][/b]cons 8 layer_name[b][color=BLUE])[/color][/b]
                            [b][color=BLUE]([/color][/b]if [b][color=RED]([/color][/b]getvar [color=#2f4f4f]"CTAB"[/color][b][color=RED])[/color][/b]
                                [b][color=RED]([/color][/b]cons 410 [b][color=PURPLE]([/color][/b]getvar [color=#2f4f4f]"CTAB"[/color][b][color=PURPLE])[/color][/b][b][color=RED])[/color][/b]
                                [b][color=RED]([/color][/b]cons 67 [b][color=PURPLE]([/color][/b]- 1 [b][color=TEAL]([/color][/b]getvar [color=#2f4f4f]"TILEMODE"[/color][b][color=TEAL])[/color][/b][b][color=PURPLE])[/color][/b][b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
      [b][color=NAVY]([/color][/b]while [b][color=MAROON]([/color][/b]not clr[b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]setq clr [b][color=GREEN]([/color][/b]acad_colordlg 1[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
      [b][color=NAVY]([/color][/b]command [color=#2f4f4f]"_.CHPROP"[/color] ss [color=#2f4f4f]""[/color] [color=#2f4f4f]"_C"[/color] clr [color=#2f4f4f]""[/color][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]

 [b][color=FUCHSIA]([/color][/b]prin1[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

-David

Link to comment
Share on other sites

One courtesy of CAB:

 

(defun c:cc (/ usercol ss)
 (while (not (setq usercol (acad_colordlg 256))))
 (prompt "Select all object to change to one color.")
 (if (setq ss (ssget))
   (command "_.ChProp" ss "" "_C" usercol ""))
 (princ))

Link to comment
Share on other sites

Thanks Lee, not exactly what i wanted. rather than the autocad window coming up and asking what colour i wanted id like to be able to select a line, type "1" and have autocad change the line colour to 1 (red). Same for colour 2 up to about 8. Is this possible?

Link to comment
Share on other sites

Yes, very possible:

 

(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))

Link to comment
Share on other sites

I think what the OP would like to do is highlight an object, then type the number of the color to change to. No prompts.

 

So you would need to have lisps loaded "c:1.lsp c:2.lsp etc" that would run on the pre-selected (highlighted) objects.

Link to comment
Share on other sites

I think what the OP would like to do is highlight an object, then type the number of the color to change to. No prompts.

 

So you would need to have lisps loaded "c:1.lsp c:2.lsp etc" that would run on the pre-selected (highlighted) objects.

 

 

Hmm.. possibly. I was under the impression he/she was satisfied to run the LISP then type the colour?

 

You may be right Carl.

Link to comment
Share on other sites

Maybe something like this

Type LC to load the command

 

 

;;Description

;; Define multiple commands to change an objects color
;; EG 		C:1 Changes the object color to 1
;;		C:2 Changes the object color to 2...

(defun c:lc ()

 ;;Add or remove color integers from the li_clr
 ;;Every valid color found in li_clt will be define as a command
  
 (setq li_clr (list 1 2 3 4 5 6 7 );This assigns colors 1 - 8 as a command
 

 (foreach n li_clr

   (if
     ;;If the the color is a valid acad color
     
     (<= 0 n 256)
     (progn

;convert the color to a string
(setq int (itoa n))

;Step 3 - Evaluate the expression & define the command

(eval

  ;Step 2 - Convert the command string to an expression
  (read

    ;Step 1 - Define the command
    (strcat
      "(defun c:" int " () (command  \"chprop\" (ssget) \"\" \"c\""  int " \"\"))"))
  )
)
     )
   )
 )


 

(

 

Thats very useful Lee, couldn't quiet figure out a clean way of coding the test

 

Regards,

 

Jammie

Link to comment
Share on other sites

Here's my contribution, example lisp to change selected items to color 1 (red):

 

(defun c:1 ()
  (if 
     (setq p1 (cadr (ssgetfirst)))
     (progn (sssetfirst) (command "chprop" p1 "" "C" 1 ""))
  )
   (princ)
)

Link to comment
Share on other sites

Dear Friends,

Please take a look at what I've done. It certainly needs improvement.

 

(defun c:cc (/ testOBJ surface 3dsolid oldUCS colchoice ss )
 (defun *error* (msg)
       (if (= (getvar "cmdecho") 0) (setvar "cmdecho" 1))
   (princ)
   ); end error function

;Select target color loop.   
  (while (not (setq colchoice (acad_colordlg 256))))

;Select Entity for color change loop.  
  (while (not 
       (setq ss (entsel (strcat"\nCustom command change color; target color number " 
               "<" (rtos colchoice) ">" ":"))
           );end setq
           );end not
           );end while

;Test Object selected is surface or 3dsolid. 
   (setq testOBJ (entget (car ss)))
   (if (= (cdr (assoc 0 testOBJ)) "POLYLINE")
       (setq surface ss));if object is surface
   
   (if (= (cdr (assoc 0 testOBJ)) "3DSOLID")
       (progn
       (setq 3dsolid ss);if object is solid
       (princ "\nHow will I do this?") ;;;<<<---------How?
       (quit)
       );end progn
       );end if

(setvar "cmdecho" 0)
;If Object selected is surface and color choice is ByBlock.
   (if (and (/= surface nil) 
            (or (= colchoice 0) (= colchoice "ByBlock"))
          );end and        
           (progn
           (vl-cmdf "_change" surface ""  "_p" "_c" "ByBlock" "")
           (quit)
           );end progn
           );end if

;If Object selected is surface and color choice is ByLayer.
   (if (and (/= surface nil) 
            (or (= colchoice 256) (= colchoice "ByLayer"))
          );end and        
           (progn
           (vl-cmdf "_change" surface ""  "_p" "_c" "ByLayer" "")
           (quit)
           );end progn
           );end if

;;If Object selected is surface and color choice is in the range 1-255.
   (if (and (/= surface nil) (< colchoice 256) 
          );end and        
           (progn
           (vl-cmdf "_change" surface ""  "_p" "_c" colchoice "")
           (quit)
           );end progn
           );end if

;Mark Current UCS.     
       (if (not (tblsearch "UCS" "oldUCS")) 
       (setq oldUCS (command "_.ucs" "_s" "oldUCS" )); do this
       (setq oldUCS (command "_.ucs" "_s" "oldUCS" "_yes")); else do this
       );end if
   
;If Colchoice is ByBlock.
  (if (or (= colchoice 0) (= colchoice "ByBlock"))     
   (progn
      (command "_.ucs" "_ob" ss)
      (vl-cmdf "_change" ss ""  "_p" "_c" "ByBlock" "")
      (command "_.ucs" "_p" )
      (command "_.ucs" "_Del" "oldUCS") 
      (setvar "cmdecho" 1)
      (quit)     
       );end progn
          );end if
 
;If colchoice is ByLayer.
  (if (or (= colchoice 256) (= colchoice "ByLayer")) 
   (progn
      (command "_.ucs" "_ob" ss)
      (vl-cmdf "_change" ss ""  "_p" "_c" "ByLayer" "")
          (command "_.ucs" "_P")
              );end progn
       );end if
 
;If colchoice is in the range 1 to 255.
  
   (if (< colchoice 256) 
       (progn
       (command "_.ucs" "_ob" ss)
       (vl-cmdf "_change" ss ""  "_p" "_c" colchoice "")
              (command "_.ucs" "_P")
            );end progn         
             );end if
   
   
   (command "_.ucs" "_Del" "oldUCS") 
   (setvar "cmdecho" 1) 
(princ)
  );end function cc

Link to comment
Share on other sites

Do you really need that much coding just to change the colour... :sweat:

Not really, its just my way of saying that Carl's code does not seem to work with objects belonging to different ucs. Also, with 3d solid.

Anybody have any ideas on how to do these?

Link to comment
Share on other sites

Here's one I found on this forum some time ago, and seems to be what you're looking for.

The author and source location is in the code box.

I changed the name to CC (red text), but otherwise it's as posted.

 

Steve

 

 

 
;by rkmcswain/6th Feb 2006
;http://www.cadtutor.net/forum/showpost.php?p=34278&postcount=2
;
;Color number can be considered a command.
;Enter color number, pick your items; Enter.
;If you have Right-click set to repeat last command, it works that way as well.
;Does not do BYLAYER or BYBLOCK

(defun [color=red][b]CC[/b][/color] (color / ss1)
 (setq ss1 (ssget))
 (command "._change" ss1 "" "p" "c" color "" )
)
(setq i 1)
(while (<= i 254)
 (eval (read (strcat "(defun c:" (itoa i) (chr 40)(chr 41) "([color=red][b]CC[/b][/color] " (itoa i) "))")))
 (setq i (1+ i))
)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...