Tuesday, August 15, 2006

Using Scripts to Automate Layer settings

Scripts are used to automate repetitive tasks in AutoCAD. A good example for this would be modifying layer settings.

If you can type it, you can Script it. This means that you'll need to become familiar with the command line versions of several AutoCAD commands. To run the command line version of a dialog driven command, like LAYER, you prefix the command with a dash: -LAYER

So here's a typical scenario: You attach an architectural floor plan background to your drawing. Now you want to set all or some of the layers to a screened Color or Plot Style. You also want to Freeze all the architectural background Layers except those germane to your discipline. If the architect later adds a Layer to their background, and it now inconveniently shows up in your drawing, you can simply rerun the Script to Freeze it, restoring your drawing to the desired display state.

Use Notepad (or your favorite text editor) to create the Script ALAY.SCR; it must have the extension .SCR to be recognized as an AutoCAD Script. Enter the following lines (or similar):

LAYER C 51
*|A-*
C 2
*|A-AREA-IDEN
F
*|A-*
T
*|A-AREA-IDEN*,*|A-DOOR-*,*|A-WALL-FULL*,etc.

Let's look at the Script line by line.

  • Line 1 executes the Layer command, specifies the Color option and selects Color 51.
  • Line 2 uses wildcards to apply Color 51 to all Layers that are both Xref dependent (includes a "|") and start with A-.
  • Lines 3 and 4 similarly reset the Room Number Layer to Color 2.
  • Lines 5 and 6 Freeze all Xref Layers that begin with A-.
  • The last two lines then Thaw all the Layers we want to display in our background, using wildcards where appropriate.

Some notes on our script:

  • Execute the Script by typing SCRIPT and selecting ALAY.SCR from the Open dialog, or drag-and-drop the Script from Explorer, or put the Script on a Tool Palette.
  • Notice that I didn't use -LAYER in the script; AutoCAD is smart enough to use the command line version of commands when called from a Script.
  • Wildcards can be asterisks or question marks. Asterisks match multiple characters and question marks match a single character.
  • Spaces act as enters in Scripts (similar to how they do at the command line), except where AutoCAD expects text entry then spaces are allowed (as in Layer names). That's why our Layer name entries are all on separate lines.
  • The Layer command needs an additional Enter to exit the command, so be sure to include an extra blank line at the bottom of your Script.

6 Comments:

At 8:19 AM , Blogger Jennifer Grande said...

Ward,
You mentioned putting a script on a tool palette - how do you do this? I haven't been able to figure out how.

 
At 1:21 PM , Blogger Ward Romberger said...

Jennifer

The easiest way is to copy another command already on your tool palette, or drag an entity like a piece of text onto your tool palette to create a new command, then edit that command. Change the Command string to be:

SCRIPT c:/folder/script.scr

where the drive letter, folder and script name all point to your script location. Make sure that you use forward slashes in lieu of backslashes in your path.

Rename the tool and change the description and image to get the desired look.

 
At 10:08 AM , Blogger Jennifer Grande said...

Thanks, it worked. One important thing to note: the names of the folders in your path can not include spaces. I had to move my script out of my "CAD stuff" folder to get it to work.

 
At 1:19 PM , Blogger Ward Romberger said...

Great. I'm glad that worked for you. FYI, you can use spaces in your file/folder names (although I think you're wise in opting not to), you simply have to quote the string:

SCRIPT "c:/cad stuff/my script.scr"

 
At 12:35 AM , Blogger Trevor Goodchild said...

Hi,
That all sounds easy, but, how do I put something like dimensions to the dim layer if I'm working in 0? I'd guess I'd have to select my dimensions, then hit the script button, hmm, are there control characters I can use to make this automatic?

 
At 11:28 AM , Anonymous digital signature certificate said...

An interesting topic Using Scripts to Automate Layer settings.I was able to do this.Thanks to Jenniefer as she pointed that the names of the folders in your path can not include spaces.So thanks to both of you for helping.

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home