使用Python批处理运行autoLISP

2024-09-28 22:42:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我想对多个CAD文件(例如文件夹中的所有文件)运行autoLISP。基本上,打开文件(DWG),运行LISP(包括保存文件)并关闭。我不熟悉LISP,但不太熟悉Python。在

可以用Python运行批处理吗?我知道如何用Python程序打开文件,但不知道如何运行LISP。或者,有人知道如何用LISP运行批处理吗?在

到目前为止,我发现的解决方案包括第三方软件和C。另外,我正在运行AutoCAD MEP 2018和Python 3.5。在


Tags: 文件程序文件夹软件解决方案lispcadmep
2条回答

实际上,我已经用python2.7使用comtypes实现了。

以下是测试用例的代码:

#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()

根据我的经验,批处理多个文件的最佳方法是使用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,并使用AutoCADSCRIPT命令从空白的新图形中运行。

当然,还可以加入额外的错误检查,例如在评估之前检查AutoLISP程序是否已成功加载等

有关AutoCAD脚本文件的详细信息,我将围绕AutoCAD脚本的this basic tutorial放在一起。


考虑到上述情况,下一步是自动构建脚本文件本身(而不是手动编写几乎相同的行)。

为此,有几个现有的应用程序:ScriptPro是一个很常见的应用程序,不久前我还创建了我自己的Script Writer应用程序,它提供了一个基本接口,允许用户键入脚本文件的第一行,程序构造其余的行。

为了提供一个现有的示例,我的Batch Attribute Editor应用程序还基于使用AutoLISP应用程序构造AutoCAD脚本文件的技术,然后使用该文件在多个选定图形上计算AutoLISP函数。


简言之,尽管您特别声明了使用Python来执行此任务,但我认为在这种情况下不需要这样做,因为一个非常简单的脚本文件(.scr)就足够了。

相关问题 更多 >