[英]Batch run autoLISP with Python
我想對多個CAD文件(例如,文件夾中的所有文件)運行autoLISP。 基本上,打開文件(DWG),運行LISP(包括保存文件)並關閉。 我是LISP的新手,但不熟悉Python。
是否可以使用Python運行批處理? 我知道如何使用Python中的程序打開文件,但不知道如何運行LISP。 或者,有人知道如何使用LISP運行批處理嗎?
到目前為止,我發現的解決方案涉及第三方軟件和C#。 另外,我正在運行AutoCAD-MEP 2018和Python 3.5。
以我的經驗,批處理多個文件的最佳方法是使用AutoCAD腳本文件( .scr
)。
該腳本僅用於打開每個圖形,加載並運行適當的AutoLISP程序,然后保存並關閉圖形,然后再移至下一個圖形文件。
由於AutoLISP在Document名稱空間中運行,因此當另一個圖形變為活動狀態時,評估將停止; 但是,AutoCAD腳本文件將繼續運行,直到發出腳本中的所有命令或腳本中止為止。
這種腳本的基本結構為:
_.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
...
可以將以上內容另存為MyScript.scr
並使用AutoCAD SCRIPT
命令在空白的新圖形中運行。
當然,還可以合並其他錯誤檢查,例如在評估之前檢查AutoLISP程序是否已成功加載等。
有關一般上AutoCAD腳本文件的更多信息,我整理了有關AutoCAD腳本的基本教程 。
考慮到以上所述,下一步是自動構建腳本文件本身(與手動寫入幾乎相同的每一行相反)。
為此,有幾個現有的應用程序:ScriptPro是眾所周知的,而且我不久前還創建了自己的Script Writer應用程序,它提供了一個基本界面,允許用戶鍵入Script文件和程序的第一行。構建其余部分。
為了提供現有示例,我的批處理屬性編輯器應用程序也基於使用AutoLISP應用程序來構造AutoCAD腳本文件的技術,該文件隨后可用於評估多個選定圖形上的AutoLISP功能。
簡而言之,盡管您明確聲明使用Python來執行此任務,但我認為在這種情況下這不是必需的,因為非常簡單的腳本文件( .scr
)就足夠了。
我實際上是使用comtypes在python 2.7中實現的 。
這是測試用例的代碼:
#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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.