Change selected object to a specific layer or set current layer that specific layer

Change selected object to a specific layer or set current layer that specific layer

kibitotato
Advocate Advocate
1,240 Views
5 Replies
Message 1 of 6

Change selected object to a specific layer or set current layer that specific layer

kibitotato
Advocate
Advocate

Hi there, 

I spend a couple of hours shearching on the web some thing that can improve my productivity a lot. But I wasnt able to find the answer so thats because Im writing here now.

 

The point is I would like to have a LISP that can do 2 things.

 

Move selected objects to a specific layer.

Or set that specific layer the current layer.

So, for example. If I type W1, I want to move selected objects to layer 1, or If there is no selection, set L1 the current layer.

If I type W2, I want to move selected objects to layer 2, or If there is no selection, set Layer 2 the current layer.

If I type W3, I want to move selected objects to layer 3, or If there is no selection, set Layer 3 the current layer.

...

 

 

Is it possible????

Thanks to everyone that can help!!

 

 

0 Likes
Accepted solutions (1)
1,241 Views
5 Replies
Replies (5)
Message 2 of 6

Kent1Cooper
Consultant
Consultant
Accepted solution

I have long had a command to do that for Layer 0:

 

(defun C:L0 ()
  ; put pre-selected object(s) [if any] on Layer 0, otherwise set current Layer to 0
  (if (ssget "_I")
    (command "_.chprop" "_layer" "0" ""); then
    (command "_.layer" "_thaw" "0" "_set" "0" ""); else
  ); if
  (princ)
)

 

Adjustable for any Layer you choose, with appropriate change in command name.

 

[By the way, some would think you need to also ensure the Layer is on, but one advantage of using a Layer command to set it is that if it's off, it turns it on in the process.  The (setvar 'clayer "0") approach does not do that, so the Layer could be current but off.]

Kent Cooper, AIA
0 Likes
Message 3 of 6

Sea-Haven
Mentor
Mentor

If your happy with Wxxxx then yes can make the layer if it does not exist, the layer name can be typed as any combo W1 WA WWALL and so on the 1st W in layer name is removed, I have used this method to create Circles, fillets and offsets normally with numeric values, eg type C12 is a circle with a radius of 12. Here is the code you can have  a go at making a Wxxx version.

; Enter the filet radius as part of a command line entry f100, O234 for offset, c123.45 for circle, P123 for pline width
; original code and methology by Alan H
; assistance and code that worked by Lee-Mac
; OCT 2015
; Edited by Mhupp Sep 2022

((lambda nil
  (vl-load-com)
  (foreach obj (cdar (vlr-reactors :vlr-command-reactor))
    (if (= "fillet-reactor" (vlr-data obj))
      (vlr-remove obj)
    )
  )
  (vlr-command-reactor "fillet-reactor" '((:vlr-unknowncommand . fillet-reactor-callback)))
 )
)

(defun plwid (/ oldwidth)
  (setq oldwidth (getvar 'plinewid))
  (setvar 'plinewid num)
  (vla-sendcommand fillet-reactor-acdoc "_.pline ")
  (setvar 'plinewid oldwidth)
)
(defun filletrad ()
  (setvar 'filletrad num)
  (vla-sendcommand fillet-reactor-acdoc "_.fillet ")
)
(defun makecirc ()
  (setvar 'circlerad num)
  (vla-sendcommand fillet-reactor-acdoc "_.Circle ")
)
(defun offdist ()
  (setvar 'offsetdist num)
  (vla-sendcommand fillet-reactor-acdoc "_.Offset ")
)
(defun fillet-reactor-callback (obj com / num)
  (setq com (car com))
  (cond
    ((and
      (eq (strcase (substr com 1 1)) "F")
      (numberp (setq num (distof (substr com 2))))
      (<= 0.0 num)
     )  ; and
      (filletrad)
    )

    ((and
      (eq (strcase (substr com 1 1)) "C")
      (numberp (setq num (distof (substr com 2))))
      (<= 0.0 num)
     )  ;and
      (makecirc)
    )

    ((and
      (eq (strcase (substr com 1 1)) "O")
      (numberp (setq num (distof (substr com 2))))
      (<= 0.0 num)
     )  ; and
      (offdist)
    )
    ((and
      (eq (strcase (substr com 1 1)) "P")
      (numberp (setq num (distof (substr com 2))))
      (<= 0.0 num)
     )  ; and
      (plwid)
    )
  )     ; master cond
)       ; defun

(or fillet-reactor-acdoc 
    (setq fillet-reactor-acdoc (vla-get-activedocument (vlax-get-acad-object)))
)

 

0 Likes
Message 4 of 6

kibitotato
Advocate
Advocate
Im not able to make it work properly.
O0.15 for example doesnt work for me
0 Likes
Message 5 of 6

Sea-Haven
Mentor
Mentor

Ok you found the 1 hiccup in error trapping command line input, if you do 0.01 it wont work, the decimal is detected as part of the error not the whole string. So to get around this you can enter for say Circle, c1-5 this means a circle of 1.5 radius you just have to get used to using "-" instead of a decimal point. Have a look at the code again its built in.

Message 6 of 6

kibitotato
Advocate
Advocate
thanks, it works
0 Likes