Modifying AutoLisp to return to previous layer after block insertion

Modifying AutoLisp to return to previous layer after block insertion

moorejam8
Explorer Explorer
767 Views
9 Replies
Message 1 of 10

Modifying AutoLisp to return to previous layer after block insertion

moorejam8
Explorer
Explorer

Hi, we use a LISP file full of these to put in quick key blocks. I unfortunately have essentially no experience coding; can someone help with a piece at the end of this that will automatically run the LAYERP command so that the block inserts on layer 0 but I'm returned to the previous layer I was working on?

 

(DEFUN C:D28 ()(command "-layer""set""0""") (COMMAND "insert" "D28" PAUSE """"))

 

Thank you!

 

Jon

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

Kent1Cooper
Consultant
Consultant

Multiple commands can be included in one (command) function, so just add it:

 

(DEFUN C:D28 () (command "-layer" "set" "0" "") (COMMAND "insert" "D28" PAUSE "" "" "_.layerp"))

 

That assumes your current code is complete to the end of the command, and does not leave you at the Rotation prompt [i.e. that the Block is defined for uniform scaling, and the two "" Enters are for one scale factor of 1 and Rotation of 0].  If that's not the case, and those two "" Enters are for separate X and Y scale factors of 1, another PAUSE for the Rotation would be needed before returning the Layer:

 

(DEFUN C:D28 () (command "-layer" "set" "0" "") (COMMAND "insert" "D28" PAUSE "" "" pause "_.layerp"))

 

Kent Cooper, AIA
0 Likes
Message 3 of 10

moorejam8
Explorer
Explorer

Thank you so much @Kent1Cooper --I needed the second one, but it didn't work. Interestingly, it left the layer set as 0, but the ByLayer color changed to that of the previous layer.

0 Likes
Message 4 of 10

Kent1Cooper
Consultant
Consultant

@moorejam8 wrote:

.... Interestingly, it left the layer set as 0, but the ByLayer color changed to that of the previous layer.


Does it make any difference if you do it this way, with the period prefix [and I included an underscore, which you likely don't need] on the Layer command name?

 

(DEFUN C:D28 () (command "_.layer" "set" "0" "") (COMMAND "insert" "D28" PAUSE "" "" pause "_.layerp"))

 

I'm imagining that maybe you have a differently-defined Layer command, which some overlay programs do.  The period prefix forces it to use AutoCAD's native command instead.  [The underscore makes it work using an English command or option name in AutoCAD for any language.]

 

[By the way, the hyphen prefix, which at the command line gives you the command-line version rather than the dialog-box version, is not needed inside an AutoLisp (command) function, where the command-line version is always the default.]

Kent Cooper, AIA
0 Likes
Message 5 of 10

moorejam8
Explorer
Explorer

That didn't work either. In any case, thank you so much for attempting the assist!

0 Likes
Message 6 of 10

Kent1Cooper
Consultant
Consultant
Accepted solution

I don't see why that shouldn't work -- I'm pretty sure I've used LAYERP successfully in AutoLisp routines before.  But whatever the reason, the other approach would be something like this:

 

(DEFUN C:D28 (/ clay)

  (setq clay (getvar 'clayer)); store current Layer

  (setvar 'clayer "0"); set Layer

  (COMMAND "_.insert" "D28" PAUSE "" "" pause)

  (setvar 'clayer clay); restore previous current Layer

  (princ)

)

 

It could have *error* handling added to ensure the Layer gets reset, and could have other enhancements.

 

Or, don't change the current Layer at all, but Insert the Block on whatever Layer is current, and then Change it to Layer 0:

 

(DEFUN C:D28 (/ clay)

  (command

    "_.insert" "D28" PAUSE "" "" pause

    "_.chprop" "_last" "" "_layer" "0" ""

  )

  (princ)

)

Kent Cooper, AIA
0 Likes
Message 7 of 10

moorejam8
Explorer
Explorer

it's all working now, thanks a ton!

0 Likes
Message 8 of 10

moorejam8
Explorer
Explorer

The only thing I don't understand is in the properties window, it's showing 0 as the current layer, but in practice, anything I do is on the previous layer (as I wanted).

0 Likes
Message 9 of 10

Kent1Cooper
Consultant
Consultant

@moorejam8 wrote:

The only thing I don't understand is in the properties window, it's showing 0 as the current layer, but in practice, anything I do is on the previous layer (as I wanted).


Are you possibly looking at the Properties when you have some object(s) [that are on Layer 0] selected?  It will show its/their Layer, not the current one.

Kent Cooper, AIA
0 Likes
Message 10 of 10

moorejam8
Explorer
Explorer

No, nothing is selected. it shows the correct linetype and color for the layer (and the correct layer is indicated in the layers menu, etc), but general properties says 0.

0 Likes