Batch Run Autolisp With Python
Solution 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.
Solution 2:
I've actually made it with python 2.7 using comtypes.
Here's the code of the test case:
#Import needed modulesimport 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"
'''defmain():
#1- Get the AutoCAD instancetry:
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 fileprint"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 expressionprint"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()
Post a Comment for "Batch Run Autolisp With Python"