Search code examples
pythonautocadautolisp

Batch run autoLISP with Python


I want to run an autoLISP on multiple CAD files (e.g. all files in a folder). Basically, open file (DWG), run LISP (including, save file) and close. I'm new to LISP, but less new to Python.

Is it possible to run the batch with Python? I know how to open a file with a program in Python, but not how to run the LISP. Alternatively, anybody know how to run the batch with LISP?

The solutions I've found so far involve third party software and C#. Also, I'm running AutoCAD-MEP 2018 and Python 3.5.


Solution

  • In my experience, the best way to batch-process multiple files is using an AutoCAD Script file (.scr).

    The Script is merely used to open each drawing, load & run an appropriate AutoLISP program, and then save & close the drawing, before moving onto the next drawing file.

    Since AutoLISP runs in the Document namespace, evaluation ceases when another drawing becomes active; however, an AutoCAD Script file will continue to run until all commands in the script have been issued, or the script has been aborted.


    The basic structure of such a Script would be:

    _.open C:\Drawing1.dwg (load "MyProgram.lsp" nil) (c:MyCommand) _.qsave _.close
    _.open C:\Drawing2.dwg (load "MyProgram.lsp" nil) (c:MyCommand) _.qsave _.close
    _.open C:\Drawing3.dwg (load "MyProgram.lsp" nil) (c:MyCommand) _.qsave _.close
    ...
    

    The above could be saved as MyScript.scr and run from within a blank new drawing using the AutoCAD SCRIPT command.

    Of course, additional error checking could also be incorporated, such as checking whether the AutoLISP program has loaded successfully prior to evaluation etc.

    For more information on AutoCAD Script files in general, I have put together this basic tutorial surrounding AutoCAD Scripts.


    With the above in mind, the next step is automating the construction of the Script file itself (as opposed to writing each near identical line manually).

    For this, there are several existing applications: ScriptPro is quite commonly known, and I have also created my own Script Writer application some time ago, which provides a basic interface to allow the user to type the first line of the Script file and the program construct the rest.

    To offer an existing example, my Batch Attribute Editor application is also predicated on this technique of using an AutoLISP application to construct an AutoCAD Script file, which is then used to evaluate an AutoLISP function on several selected drawings.


    In short, although you specifically state the use of Python to perform this task, I don't believe this is necessary in this circumstance, as a very simple Script file (.scr) will suffice.