我想对多个CAD文件(例如,一个文件夹中的所有文件)运行一个autoLISP。基本上,打开文件(DWG),运行LISP (包括,保存文件),然后关闭。我是LISP新手,但对Python不是新手。
可以使用Python运行批处理吗?我知道如何用Python程序打开文件,但不知道如何运行LISP。或者,有人知道如何使用LISP运行批处理吗?
到目前为止,我发现的解决方案涉及第三方软件和C#。另外,我正在运行AutoCAD-MEP 2018和Python 3.5。
发布于 2018-02-15 07:57:11
根据我的经验,批处理多个文件的最佳方式是使用AutoCAD脚本文件(.scr
)。
该脚本仅用于打开每个图形,加载并运行适当的AutoLISP程序,然后保存并关闭图形,然后移动到下一个图形文件。
由于AutoLISP在文档名称空间中运行,因此当另一个图形变为活动状态时,求值将停止;但是,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脚本文件的更多信息,我将this basic tutorial放在了AutoCAD脚本周围。
记住上面的内容,下一步就是自动构建脚本文件本身(而不是手动编写几乎相同的行)。
为此,有几个现有的应用程序:众所周知的ScriptPro,不久前我还创建了自己的Script Writer应用程序,它提供了一个基本界面,允许用户键入脚本文件的第一行,然后由程序构造剩下的行。
为了提供一个现有的示例,我的Batch Attribute Editor应用程序也基于这种使用AutoLISP应用程序构造AutoCAD脚本文件的技术,然后使用该脚本文件在几个选定的图形上计算AutoLISP函数。
简而言之,尽管您特别说明了如何使用Python来执行此任务,但我认为在这种情况下没有必要这样做,因为一个非常简单的脚本文件(.scr
)就足够了。
发布于 2018-09-04 06:57:26
实际上,我已经使用comtype在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()
https://stackoverflow.com/questions/48794935
复制相似问题