Get properties and set current

Get properties and set current

M.Lindell
Enthusiast Enthusiast
562 Views
6 Replies
Message 1 of 7

Get properties and set current

M.Lindell
Enthusiast
Enthusiast

Hi, Can I get some help with this.
I have this lisp that gets layer, color and linetype from selected object and sets them current.

It works partially. If the selected object has manually changed color and linetype the lisp finds them and sets current correctly. But if the color and linetype are "ByLayer" the lisp doesn't work.

What is wrong with it? I can't get it to work. (Lisp-code is still quite unfamiliar for me)

 

(defun c:GETPROP ()
  (setq obj (car (entsel "\nSelect an object: ")))
  (setq ent (entget obj))
  
  ;; Extract linetype, layer, and color
  (setq linetype (cdr (assoc 6 ent)))
  (setq layer (cdr (assoc 8 ent)))
  (setq color (cdr (assoc 62 ent)))
  
  ;; Set linetype, layer, and color as current
  (command "-linetype" "S" linetype "")
  (command "CLAYER" layer "")
  (command "CECOLOR" color "")
)

(princ)

 

 

0 Likes
Accepted solutions (2)
563 Views
6 Replies
Replies (6)
Message 2 of 7

johnyDFFXO
Advocate
Advocate
Accepted solution

ent does not contain linetype or color information if is by layer. it returns nil in that case.

 

(setq linetype (cdr (assoc 6 ent)))
(if (not linetype) (setq linetype "ByLayer"))
(setq layer (cdr (assoc 8 ent)))
(setq color (cdr (assoc 62 ent)))
(if (not color) (setq color "ByLayer"))

 

also, the clayer and cecolor setting does not require the last ""

0 Likes
Message 3 of 7

M.Lindell
Enthusiast
Enthusiast

@johnyDFFXO 
Oh it was that simple. Thank you very much.

0 Likes
Message 4 of 7

Kent1Cooper
Consultant
Consultant

Rather than extract those properties into variables and then use the variables to set the current-entity properties, I would suggest you set them directly.  And here's another way to do it, using the System Variable names themselves rather than the command-line equivalents, and using (cond) instead of the (if (not... approach:

(setq
  obj (car (entsel "\nSelect an object: "))
  ent (entget obj)
)
(setvar 'celtype (cond ((cdr (assoc 6 ent))) ("ByLayer")))
(setvar 'clayer (cdr (assoc 8 ent)))
(setvar 'cecolor (cond ((cdr (assoc 62 ent))) ("ByLayer")))

If you want something that sets far more properties current [e.g. lineweight, thickness, Text Style and height for text-type things, Polyline width and in some cases originating command, etc., etc.], and starts the appropriate command for you or offers options when more than one command could make it [since presumably you want to set such things only because you want Make More of the same thing], try the MM command in MakeMore.lsp, >here<.

 

Kent Cooper, AIA
0 Likes
Message 5 of 7

M.Lindell
Enthusiast
Enthusiast
Thanks for your suggestion.
I tried your code but "cecolor" stayed "ByLayer" even if the selected object had something else. Weird...
But I got a working lisp that does exactly what I need so I wouldn't want you to waste your time on this.

I'll definitely check out that "MakeMore".
0 Likes
Message 6 of 7

Kent1Cooper
Consultant
Consultant
Accepted solution

@M.Lindell wrote:
... "cecolor" stayed "ByLayer" even if the selected object had something else. ....

I had forgotten that CECOLOR wants a text string, not a number, even for numerical values, because of the possibility of using color names like "red," etc.  This works for me:

 

(setvar 'celtype (cond ((cdr (assoc 6 ent))) ("ByLayer")))
(setvar 'clayer (cdr (assoc 8 ent)))
(setvar 'cecolor (cond ((assoc 62 ent) (itoa (cdr (assoc 62 ent)))) ("ByLayer")))

 

 

Kent Cooper, AIA
0 Likes
Message 7 of 7

M.Lindell
Enthusiast
Enthusiast
Yep. Works perfect.
Thank you
0 Likes