Quick change selected object layer (name in command line)

Quick change selected object layer (name in command line)

Angelswing91
Contributor Contributor
2,974 Views
9 Replies
Message 1 of 10

Quick change selected object layer (name in command line)

Angelswing91
Contributor
Contributor

hi all, 

I want to move selected object to a specific layer (by writing its name) and not current one by only using the command line. How would i need to modify this code?

;---------------------------CLAY.LSP------------------------------------------
; by
; BILL CARROLL
;-----------------------------------------------------------------------------

;-----------------------------------------------------------------------------
; This routine changes entities to the current layer of a drawing.
; It will also switch layers before selecting the entities.
;
;-----------------------------------------------------------------------------

(DEFUN C:82 ()
(SETQ CMDECHO "0")
(PRINT "THIS COMMAND WILL MOVE OBJECTS TO THE CURRENT LAYER.")
(PRINT "PRESS <CTRL-C> TO QUIT")
(TIMING 400) ;Pause
;
(SETQ CHOICE (GETSTRING "\nCHANGE LAYERS? (Y/cr):"))
(IF (OR (= 89 (ASCII CHOICE)) (= 121 (ASCII CHOICE)))
(CHANGELAY) (CHANGEFUN))
)
;

(DEFUN CHANGELAY () ;Function to switch layers
(SETQ OLDLAY (GETVAR "CLAYER"))
(SETQ NEWLAY (GETSTRING "NEXT LAYER :"))
(COMMAND "LAYER" "S" NEWLAY "" )
(CHANGEFUN)
(SETQ MESS "CHANGES DONE, ")
(SETQ CHOICE (GETSTRING "\nRESTORE OLD LAYER? (Y/cr):"))
(IF (OR (= 89 (ASCII CHOICE)) (= 121 (ASCII CHOICE)))
(RESTORLAY) (SETQ DUMMY () ))
(PRINC MESS)(PRINC "CURRENT LAYER IS : ")(SETQ MESS NEWLAY)
)
;

(DEFUN RESTORLAY () ;Function to restore layer
(COMMAND "LAYER" "S" OLDLAY "" )
(SETQ NEWLAY OLDLAY)
)
;

(DEFUN CHANGEFUN () ;Function to select and change entities' layers
(SETQ SSET (SSGET)) ;Use Autocad selection set prompt
(SETQ CURRLAY (GETVAR "CLAYER"))
(COMMAND "CHANGE" SSET "" "P" "LA" CURRLAY "") ;Autocad Change command
(SETQ MESS "CHANGED")
)
;

(DEFUN TIMING (numtimes) ;Pause loop
(REPEAT numtimes
(1+ 5))
)
;



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

Kent1Cooper
Consultant
Consultant
Accepted solution

So much of that is specific to current-Layer aspects, and therefore irrelevant to what you want to do, that I don't think modifying it is the best approach.  Just start over -- for example, in simplest terms, this could be all you really need:

 

(defun C:WHATEVER ()

  (command "_.chprop" (ssget) "" "_layer" pause "")

)

Kent Cooper, AIA
0 Likes
Message 3 of 10

Sea-Haven
Mentor
Mentor

Agree with Kent can do a select then pick a object on destination layer as one way but new layer object must exist.

 

Another is to use a reactor which looks at a error has occurred eg type  Lmynewlayer will get an error message, but it is possible to trap this error and pull it apart getting the destination layer, but it has an overhead. I use F123 will do the fillet command with a radius of 123, same with o12 is offset of 12, c34 is circle RAD 34. Any numerical value can be used no hard coded values.

 

I think for this task not worthwhile adding a reactor so have not posted code, but has been posted before.

 

Using layer translate may be better.

 

https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/using-layer-translator-by-lisp/td-p/...

0 Likes
Message 4 of 10

SPORTE0000
Contributor
Contributor

Lots of different ways to skin that cat.  Here is an example route you could take.  Beware its untested.

 

    (defun c:QUICKNDIRTYTEST ( 
                              /
                              acadDocument 
                              ssBlks ICOUNT IMAX xent MASTER_LAYER 
                              ); 
      (vl-load-com)
      (setq acadDocument (vla-get-activedocument (vlax-get-acad-object)))
      (PRINT "\n Choose Layer to seek Polyline from: ")
      (SETQ MASTER_LAYER (cdr (assoc 8 (ENTGET (CAR (ENTSEL))))))
      (if (setq ssblk (ssget ))
          (progn
            (setq ICOUNT 0
                  IMAX (SSlength ssblk)
            )
            (while (< ICOUNT IMAX) 
                (setq xent (ssname ssblk ICOUNT))     
                (vlax-PUT-property  (vlax-ename->vla-object xent ) 'LAYER MASTER_LAYER)
                (SETQ ICOUNT (1+ ICOUNT)) 
            ); end while
          );END PROGN
        );END IF
      (PRINC);HUSH 
    );END DEFUN

 

0 Likes
Message 5 of 10

Sea-Haven
Mentor
Mentor

No need for Print

 

(SETQ MASTER_LAYER (cdr (assoc 8 (ENTGET (CAR (ENTSEL "\n Choose Layer to seek Polyline from: "))))))

0 Likes
Message 6 of 10

pbejse
Mentor
Mentor

@Angelswing91 wrote:

I want to move selected object to a specific layer (by writing its name) and not current one by only using the command line. 


Please make us understand @Angelswing91, why on the command line? if the layer already exist it is far easier to select the target layer on the layer ribbon panel or toolbar and even in Properties dialog. UNLESS the target layer does not exist, then an extra step is needed to create the layer, but since you are utilizing lisp codes , I'm sure you already have a Load layer routine that ensures the layer will exists.

 

What's the real reason for wanting the  command line prompt over the other options?

 

 

 

 

 

 

 

 

 

0 Likes
Message 7 of 10

ВeekeeCZ
Consultant
Consultant

I totally understand that such a tool would be very useful and very fast. When you have let's say 50+ layers, it's much faster just type a name... I do love a build-in feature that by simple typing you can set a current layer. Not really sure why they did not add support for changing layer property in the same manner when you have some objects pre-selected.... it would make a sense to me. I would love it!

 

You should try it too! See HERE  Type the layer name or just a mid-part of it...  to make it current. Super cool feature! 

You might want to adjust INPUTSEARCHOPTIONS.

0 Likes
Message 8 of 10

pbejse
Mentor
Mentor

@ВeekeeCZ wrote:

I totally understand that such a tool would be very useful and very fast. When you have let's say 50+ layers, it's much faster just type a name..


Since you put it that way , yeah typing in the layer name on the command prompt is way easier. Select the objects 

then type Laycur /LayerP.  Dont even need  clay.lsp after all.

 

Indeed it is cool. 🙂

 

 

0 Likes
Message 9 of 10

Angelswing91
Contributor
Contributor

I totally understand that such a tool would be very useful and very fast. When you have let's say 50+ layers, it's much faster just type a name...   Exactly!!! 
ro.png

~ 400 layers with short names. I really like 

0 Likes
Message 10 of 10

pbejse
Mentor
Mentor

@Angelswing91 wrote:

.. I really like 


 

Clearly its that simple 👍

 

 

0 Likes