Batch run autoLISP with Python

佐手、 提交于 2020-04-16 05:30:12

问题


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.


回答1:


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.




回答2:


I've actually made it with python 2.7 using comtypes.

Here's the code of the test case:

#Import needed modules
import os
import comtypes.client
from comtypes import COMError
from comtypes.client import CreateObject, GetModule, GetActiveObject

#Uncomment it if you need to load these type libraries.
'''
#Load all needed type libraries
GetModule("C:/Windows/System32/stdole2.tlb")
import comtypes.gen.stdole as ole
print "stdole2 successfully loaded"
GetModule("C:/Program Files/Common Files/Autodesk Shared/acax20enu.tlb")
import comtypes.gen._4E3F492A_FB57_4439_9BF0_1567ED84A3A9_0_1_0 as acax
print "acax20enu successfully loaded"
GetModule("C:/Program Files/Common Files/Autodesk Shared/AcSmComponents20.tlb")
import comtypes.gen._ED125AFF_6294_4BE4_81E2_B98DCBBA214E_0_1_0 as AcSm
print "AcSmComponents20 successfully loaded"
'''

def main():
    #1- Get the AutoCAD instance
    try:
        acad = GetActiveObject("AutoCAD.Application.20")
        print "AutoCAD is Active"
        print "########"
    except(OSError, COMError): #If AutoCAD isn't running, run it
        acad = CreateObject("AutoCAD.Application.20",dynamic=True)
        print "AutoCAD is successfuly Opened"
        print "########"

    #2- Get the paths to the lisp file and the dwg file
    directory_name = "E:\\Dir1\\Dir2" #replace it with a real path, use "\\" as directory delimiters.
    '''
    Note that "\\" is transformed automatically to "\", & in order to comply with
    the AutoLISP "load" function, every "\" must be transformed again to "/".
    '''

    temp=""
    for char in directory_name:
        if char == "\\":
            temp += "/"
        else:
            temp += char
    directory_name = temp
    filename = directory_name + "/TestDWG.dwg"
    lispfile = directory_name + "/linedraw.lsp"

    #3- Open the drawing file
    print "Opening Drawing File ..."
    doc = acad.Documents.Open(filename)
    print "Drawing is successsfuly Opened"
    print "########"

    #4- Construct the AutoLISP expression that loads AutoLISP files
    command_str = '(load ' + '"' + lispfile + '")' + " "

    #5-Execute the AutoLISP expression
    print "Sending AutoLISP Expression ..."
    print "Expression: " + command_str
    doc.SendCommand("(setq *LOAD_SECURITY_STATE* (getvar 'SECURELOAD)) ")
    doc.SendCommand("(setvar \"SECURELOAD\" 0) ")
    doc.SendCommand(command_str)
    doc.SendCommand("(setvar \"SECURELOAD\" *LOAD_SECURITY_STATE*) ")
    print "AutoLISP Expression is successfuly sent"
    print "########"

    #6- Save and Close the drawing file and AutoCAD application
    doc.Save()
    doc.Close()
    acad.Quit()

    print "Process Finished"
    print "__________"

if __name__ == '__main__':
    main()


来源:https://stackoverflow.com/questions/48794935/batch-run-autolisp-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!