AutoCAD Script & Lisp Confirmation & Understanding

AutoCAD Script & Lisp Confirmation & Understanding

kieran.leadbetter
Advocate Advocate
2,071 Views
28 Replies
Message 1 of 29

AutoCAD Script & Lisp Confirmation & Understanding

kieran.leadbetter
Advocate
Advocate

Hello, So this is a lot, and if anyone can help me I would be really really grateful, so thank you in advance.

So recently I have been looking into Lisps and Scripts for AutoCAD, trying to automate some repetitive tasks which I understand can be automated over a large batch of drawings, I simply need to learn how to write these up and use them first.

While looking into the Lisps and Scripts, I seem to understand them that Lisps are like importable, more powerful commands for AutoCAD, say I want to Delete every hatch, layout or specific block. The lisp is able to find and delete these quickly, or even if I have a Variable Setup which will change the PSLT,MSLT,LTS and FILLMODEs etc to different values. All useful things for quickly setting up and deleting from new and existing drawings.

I've came to understand how to briefly use Lisps in singular drawings, using commands and assigning the lisps to commands with (defun C:InsertCommandNameHere ()

So the next stage is to try and use this lisp across multiple drawings, I've found a program by ESurvey called CAD_Batch_Runner.exe, haven't installed it yet however I already feel like an issue with the Defun command will occur, asking me to type in every command before I can actually use it. I also tried to installed ScriptPro 2.0 but my computer doesn't support it for some reason, ideally I would need a Batch-Lisp Runner and/or a Batch-Script Runner that is free

How would I convert a script like this;

(defun C:VariableSetup ()
(command "MSLTSCALE" 0 )
(command "PSLTSCALE" 0 )
(command "LTSCALE" 15 )
(command "FILLMODE" 1 )
(command "PDFFRAME" 1 )
(command "IMAGEFRAME" 1 )
)

into a scriptable format, and as a second one, how would I convert this Lisp which deletes all hatching into a script


Next is my understanding and issues with Scripts. I understand them to function similar to AutoCAD Macro Commands, inputting keyboard functions and possible using lisps inside themselves by typing the lisp command? However I can not get any script I have to work correctly, and I am unsure if scripts are written the same as LISPS, do they have a different formatting and if so, does anyone know the specific names for Lisp Formatting and Script Formatting so I can research these further? Whenever I use my scripts also, it always gives me a blank type box or error. This is my current script I found to disable one of my layers, but it does not work;


Does anyone have any idea how I can use a Lisp across multiple drawings, or am I limited to using Scripts as my batch processing. And alongside this, is it possible to link my Scripts to Lisps, so that the script can access my premade lisps instead of converting every Lisp to an executable Script Format?

If you have read all this, thank you, and if you are wanting to help me, also thank you so so much.



0 Likes
2,072 Views
28 Replies
Replies (28)
Message 2 of 29

Kent1Cooper
Consultant
Consultant

Start with looking at the >Customization Reference<, which has entries for the various approaches.  That should explain most of what you need to know as well as we can here.  If you run into something you don't understand, maybe we can give further explanation.

 

EDIT:  A few further comments:

 

"I am unsure if scripts are written the same as LISPS"

They are not.  An example:  Enter is "" in an AutoLisp (command) function, ; in a command macro, and just a new line in a Script.  Another:  Spaces often mean nothing in AutoLisp [you can use as many as you want in visual formatting without affecting functionality], but in a command macro or a Script they are read the same as when you press the space bar in manual operation.  I hope the reference I linked to can clarify the differences.

 

"This is my current script I found to disable one of my layers...."

There doesn't seem to be any "this" to look at.

 

A Script or command macro can include certain AutoLisp functions, and can use a command name defined in an AutoLisp file if you ensure it is loaded, for example with a (load) function right in the Script, or by an acaddoc.lsp file which runs upon opening or creating every drawing.

Kent Cooper, AIA
Message 3 of 29

paullimapa
Mentor
Mentor

@Kent1Cooper has already given you excellent feedback.

Here's my 2 cent:

LISP files can only be applied to the current drawing that's opened in the AutoCAD session.

SCRIPT files can include commands that opens a drawing, run as you say a macro sequence of commands, save or close the drawing before opening another drawing to run more commands.

As you mentioned, programs like Script Pro, can be used to apply LISP or Script files to run on multiple drawings.  Unfortunately, Autodesk decided to stop supporting and developing this for 64 bit OS. So we have to look elsewhere like CAD_Batch_Runner.exe for similar functionality. What I don't like about CAD_Batch_Runner.exe is that you have to go through a registration process to download the free app. Also the app does not support long file names. So when you have drawings longer than 8 characters, it'll truncate the file name using the old fashioned "~" symbol.  Lastly, after adding the drawings onto the list, if you change your mind, you cannot remove certain selected drawings.  You may want to try another program which is also free to download but runs inside AutoCAD:

https://www.cadig.com/products/autocad-script-pro.php

Finally, if you only want to apply a sequence of commands that fits onto a single line to all drawings in a selected folder, you can also try Lee Mac's free Script Writer: http://www.lee-mac.com/scriptwriter.html


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 4 of 29

kieran.leadbetter
Advocate
Advocate
Hello, sorry for the late reply, and thank you for clarifying a few things for me it helps a lot, thats really useful to know so that I can adjust and fix some of my lisps when needed understanding when and how to use the Enter command.

I think I've managed to get the layer lisp to work, however once the lisp is finished, the command prompt options box still remains open, interupting the next lisp to come up, what line should I input into my lisp to make it close the comand options:

(defun C:DisableA200 ()
(command "-LAYER" "OFF" "A200_Demolitions")
(command "-LAYER" "OFF" "A200 Demolitions")
(princ) ;clean exit
)


It's a very basic lisp I know but the format seems to work for the things i need like adjusting pslt, mslt, etc. Any idea what the function of the "(princ) ;clean exit" line is for? I've seen it occur on multiple other lisps, so I've just been adding it before the end as an insurance thing to make sure it works.

Thank you again for reading all of that and replying

0 Likes
Message 5 of 29

paullimapa
Mentor
Mentor

You can combine the layer commands into one like this and use “” to do a return to finish off the layer command:

C:DisableA200 () (command "-LAYER" "OFF" "A200_Demolitions,A200 Demolitions” “”)  (princ) ;clean exit )


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 6 of 29

kieran.leadbetter
Advocate
Advocate

Hello again, Thank you once again in advance for your help.


I agree with the parts of CAD_Batch_Runner.exe that you've mentioned, and the issue of only removing a singular drawing from the list will definetly catch me up on future project mistakes I can already tell.

The program creates the script aspects to open and close all the drawings and run the lisps in the series I input them though which is the exact feature I'm looking for to create a batch process, which I understand AutoScript does, but it doesn't allow me to use Lisp Files in the same manner.


I have installed AutoScript onto cad and it looks like the exact thing I need to do everything I want and very easy to use also so thank you for showing me that last time, but the Script File part I dont know how to use, as I am currently creating multiple Lisps of which would be put into a singular script with CAD_Batch_Runner.exe as the batch-lisp-script hybrid file that it creates for me? Unsure of the correct term to call that.


Where would I find a program which would merge my lisps and somehow input them into a singular script, which CAD_Batch_Runner.exe does currently but links it directly to the drawing files also, making the script a one time use script that AutoScript won't be able to reuse?

Once again, Thank you

0 Likes
Message 7 of 29

paullimapa
Mentor
Mentor

It is very easy to modify or create a script file using Notepad to include lisp functions. As long as those lisp commands do not bring up a dialog window requiring user input, then any lisp command can be included in a script file. For example assuming your lisp function is saved in AutoCAD support path and named as DisableA200.lsp in the script file just add the following lines to run it:

; load Lisp

(load”DisableA200”)

; run code

DisableA200

; lisp completed


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 8 of 29

kieran.leadbetter
Advocate
Advocate
Perfect, I get what @Kent1Cooper means by the "" now also, thank you both

I had to change it slightly as once the command is put in for 'A200_DEMOLITIONS", it goes to the options menu again and retypes -Layer but should say "Off" Then the next layer A200 DEMOLITIONS, this one currently works because it inputs another command afterwards for the next layer, any idea how to tidy it up onto one command?

(defun C:DisableA200 ()
(command "-LAYER" "OFF" "A200_Demolitions" “”)
(command "-LAYER" "OFF" "A200 Demolitions" “”)
(princ) ;clean exit
)
0 Likes
Message 9 of 29

kieran.leadbetter
Advocate
Advocate
Okay, I think I understand that, thank you


Say if I had 3 Lisps called: LispA, LispB, LispC, how would I lay it out into a notepad for script.

And would I need the "; load Lisp" line? Does the ';' make the lisp bring up text in the command prompt telling you what it is doing?
0 Likes
Message 10 of 29

paullimapa
Mentor
Mentor

Did you try the combination i posted

(command "-LAYER" "OFF" "A200_Demolitions,A200 Demolitions” “”)


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 11 of 29

kieran.leadbetter
Advocate
Advocate
Yea, it creates an error because once A200_Demolitions is typed, it returns to the options prompt area, requiring me to type Off again before the next layer
0 Likes
Message 12 of 29

paullimapa
Mentor
Mentor

In a script file any line starting with a semicolon ; means a comment and will not be executed so it’s really for reference only and I always like to end Script file with it so I know there’s a return to complete the last command in the script. To run one lisp after another is simple to repeat:

(load”lisp1”)

lisp1

(load”lisp2”)

lisp2

(load”lisp3”)

lisp3

this assumes there are no additional requests from any of the lisp functions ran


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 13 of 29

kieran.leadbetter
Advocate
Advocate
Awesome,

Where it says (load"lisp1"), is the lisp1 inside the "" the file name to load inside ther LoadApplications, and then the lisp1 on the second line the equivilant of Defun 'C:InsertCommandNameHere ()' in order to actually activate the lisp command.
0 Likes
Message 14 of 29

Kent1Cooper
Consultant
Consultant

Some of the Enter "" things in some posts have smart quotes [differentiating opening and closing quotes -- little 6's and little 9's].  Can't have those in AutoLisp.

Kent Cooper, AIA
Message 15 of 29

kieran.leadbetter
Advocate
Advocate
Cool, and in an Autolisp, a simple space is the same as the "" yea? Like an empty space on an excel block
0 Likes
Message 16 of 29

Kent1Cooper
Consultant
Consultant

@paullimapa wrote:

Did you try the combination i posted

(command "-LAYER" "OFF" "A200_Demolitions,A200 Demolitions” “”)


That is the error I mentioned.  But also, in a (command) function you don't need the hyphen prefix to avoid the dialog box, and you can use wildcards as in the (wcmatch) function, and cover both of those together with the ? wildcard for any character:

(command "_.layer" "_off" "A200?Demolitions" "")

Kent Cooper, AIA
Message 17 of 29

Kent1Cooper
Consultant
Consultant

@kieran.leadbetter wrote:
Cool, and in an Autolisp, a simple space is the same as the "" yea? Like an empty space on an excel block

No.  In a command macro, yes, in places where it would be equivalent in manual command-line usage.  [It gets more complicated when you use Layer or Block names with spaces in them.]  But AutoLisp can have all sorts of spaces that don't have any effect, often used for indentation in visual formatting of code.

Kent Cooper, AIA
Message 18 of 29

kieran.leadbetter
Advocate
Advocate
OH COOL, just searched up what a wildcard is, how a ? would represent a random character but a * would represent a random word

Does that mean that I could create a layer disable code that would look for the A200 at the start of each layer name

Something like this

(command "_.layer" "_off" "A200?*" "")

So that it would disable all layers with A200 at the start?

Also when you have typed the _. is this the equivalent to the - symbol? Is that another AutoLisp thing I would need to watch out for

Can this be inputted into any Lisp file, or specifically an AutoLisp
0 Likes
Message 19 of 29

kieran.leadbetter
Advocate
Advocate
Okay, I think I get it, just to confirm something aswell, is there 3 different languages for autocad lisp and scripting things

1. Scripting Language (What will initial a series of lisps commands across multiple drawings)
2. Lisp Language (A series of commands functioning similar to Macros)
3. AutoLisp (Similar to Lisp Language, but certain characters function differently)?

Whats better to learn for more functionality and reliability, Lisp or AutoLisp
0 Likes
Message 20 of 29

Kent1Cooper
Consultant
Consultant

@kieran.leadbetter wrote:
.... Any idea what the function of the "(princ) ;clean exit" line is for? ....

An AutoLisp routine returns to the Command line whatever is returned by the last thing in it.  The (command) function always returns nil.  A lot of people don't like to see that after a routine that ends with such a function, because it suggests that it didn't work right, or something.  The (princ) function with no arguments returns nothing at all [as do its siblings (print) and (prin1), but for some reason (princ) is by far the most used for this purpose].  So if a routine's operation ends with something that returns nil, putting that (princ) at the end means the nil return is not the last thing that happens, so it doesn't appear at the Command line.  It's sometimes referred to as "exiting quietly" instead of as a "clean exit."  It has no effect on the operation of a routine, and is not necessary at all, if you don't mind seeing the nil return.

Kent Cooper, AIA