Just notes from Opie A commentary on AutoCAD and Civil 3d

Executing an AutoLISP routine from a Script

Do you need to run a script on multiple AutoCAD drawings? One can do that with ScriptPro.

But what if you need to rename all of the layers in a drawing? If you only use a script file, you wouldn’t be able to loop through all of the layers in the drawing. Also, you would be unable to use the -RENAME command at the command line as it does not allow for wildcard replacements.

You will need to add an additional programming language to assist your script. A fairly easy language would be AutoLISP. You could also use other more powerful languages, such as .NET or ObjectARX. I feel the more powerful languages may be a bit much when developing a script for quick edits on multiple drawings.

Now, over on the Autodesk forums, BlackBox shared an AutoLISP routine to add a prefix to all of the layers. For this example, we would want to copy all of the code and save it in a file where we can find it with a .lsp extension. Knowing where we saved it will allow us to tell the script where to find this file. We will also need to know the name of the AutoLISP command, which is LayPrefix, to add to our script.

Adding this line to the script will load the file into your current session of AutoCAD. The double back slashes in the code are necessary as the back slash is a control character.

(load "X:\\Path\\To\\Saved\\File\\LayPrefix.lsp")

Once our routine is loaded, we can then add the name of the routine, LayPrefix, to our script and follow it with the prefix we wish to add to the layer names.

LayPrefix
EXISTING-

We can now run our script across multiple drawings with the help of ScriptPro and AutoLISP.

If you liked this post, you can share it with your followers or follow me on Twitter!

Similar Posts