More About the Lock in Layers Tip for AutoCAD18 Oct, 2004
- The use of Layer Make instead of Layer Set allows for the layer that does not already exist, but it does not allow for a layer that is turned off, or frozen, or even viewport frozen. The use of Tool pallettes comes closest to working properly under those circumstances, but leaves the user in the unusual state of drawing in an invisible layer!
- Most of the LISP-based tips will leave the user without any command prompts if the CMDECHO system variable happens to be set to 0.
- Most of the LISP-based tips will get confused if the user deviates from the default command sequence by using some of the available subcommands. This is a problem common to most menu macros, except at the end where the command can usually be left "hanging" for the user to finish off.
- If the user hits Esc during most of the LISP-based tips, the Dimension layer will be left current.
- In most of the LISP-based tips, if the command executes successfully, but then the user performs an Undo, the object will be removed, but the Dimension layer will be left current.
- Most of the LISP-based tips work only for a specific command and layer. A general solution would reduce the amount of typing and maintenance required if you want to do this throughout your custom menus.
This routine (http://new.cadalyst.com/code/LayCmd.lsp) copes with the above problems. Place it where AutoCAD can find it and arrange for it to be loaded using whatever method you prefer -- for example, ACADDOC.LSP, an mnl file, the Appload command's Startup Suite, and so forth. This adds two commands to AutoCAD: LayCmd and LayCmds. They work similarly, with the simple LayCmd command allowing for only a single command, and the more complex LayCmds command allowing for multiple commands. These commands can then be used in various menu macros.
Here is a menu macro to draw a line or lines in layer Fred. When the Line command is finished, the original layer will be restored.
Simple, eh? To be more pedantically correct, it should look like this:
The underscore and dot are there for the same reason as they should appear in normal menu macros: to ensure the macro will still work if the AutoCAD is non-English and/or the Line command has been redefined. Here's a macro to draw a linear dimension on layer Dims:
The only tricky thing you need to remember is that the layer name must be followed by a semicolon (;) to force an Enter. You can't use a space because layer names can contain spaces, thus:
^C^CLayCmd Dim Layer Name;_.DimLinear
If you need to enter more than one command or command option, you will need to use the LayCmds command instead. Then things get a little more complicated. If you wanted a LayCmds version of our LayCmd line macro, it would look like this:
Here is a menu macro to draw a single line, then a circle at the end of that line at a user-specified radius, all on layer Lollipop:
Some items in the macro need explanation here:
- The LayCmds command, after asking for your layer, repeatedly asks for a command. The macro must always end in an exclaimation mark (!) followed by a semicolon (;) for Enter. This tells the LayCmds command that you have finished entering commands.
- Unlike normal macros, we can't use the backslash (\) to indicate a pause, because that will just stop the menu macro while it is trying to receive the list of commands. Instead, use the slash (/). To convert an existing macro to a LayCmds macro, replace each "\" with "/;" (note the semicolon for Enter).
- Unlike normal macros, we can't use ^C within LayCmds to perform a Cancel, because that will just cancel the LayCmds command. Instead, use ~C. To convert an existing macro to a LayCmds macro, replace each "^C" with "~C;" (note the semicolon for Enter).
For example, a normal menu macro like this:^C^C_.Line;\\;_.Circle;@
*^C^C_.Line;\\^C_.Circle;@;\becomes a LayCmds macro like this:
*^C^CLayCmds Lollipop;_.Line;/;/;~C;_.Circle;@;/;!;Although LayCmds will accept one or more spaces immediately followed by a semicolon and converts each space to an Enter, you are probably better off using semicolons because it makes it easier to count the Enters.
Here are the situations the routine will cope with:
- If the specified layer does not exist, it will be created.
- If the specified layer is off, it will be turned on. It is not turned off again at the end of the command.
- If the specified layer is frozen, it will be thawed. It is not frozen again at the end of the command.
- If the specified layer is frozen in the current viewport, it will be thawed in the current viewport. It is not viewport frozen again at the end of the command.
- Commands can be left hanging for the user to finish, no matter what prompt sequence they follow. Only when the command is finished will the original layer be restored.
- If the command is cancelled, the routine will clean up after itself, restoring the original layer.
- The routine will handle Undo correctly, undoing the whole command rather than just part of it.
- Prompts will be displayed correctly, regardless of the CMDECHO setting.
Here are some things that the routine will not cope with:
- In common with all LISP routines, it may be just about possible to prevent the routine from cleaning up properly after itself if you hit Esc twice extremely quickly.
- If you get the macro prompts in the wrong order, these are just passed on to the command prompt, so AutoCAD (and you) will probably get confused.
- If a layer does not exist, it will be created using AutoCAD's defaults (for example, color 7). If you want to force specific layers to use specific settings when they are created, you will need some more code. I will leave that as an exercise for the reader.
- If a LayCmds macro does not end in a semicolon (;) to force an Enter, the command will be left hanging and the user will need to press Enter before anything happens.
- If you need a command sequence that would normally involve a slash, semicolon or exclaimation mark, that cannot be included in a LayCmds macro.
- The routine assumes you have Undo turned on in the normal way. It is possible to disable it using Undo Control, but this is so uncommon I haven't bothered checking for it.
- I haven't allowed for entering LISP code into the macros. DIESEL in menu macros should work, though.
- You may wish to consider handling the other properties (for example, color, linetype, and so forth.). I will leave that as another exercise for the reader.
In her easy-to-follow, friendly style, long-time Cadalyst contributing editor and Autodesk Technical Evangelist Lynn Allen guides you through a new feature or time-saving trick in every episode of her popular AutoCAD video tips. Subscribe to the free Cadalyst Video Picks newsletter and we'll notify you every time a new video tip is published. All exclusively from Cadalyst!