来自python的CATIA宏

时间:2018-04-02 11:42:13

标签: python-2.7 macros catia

我想使用我的宏(.CATScript)打开catia接口,并将宏脚本中列出的更改发送到.CATpart,并将输出作为.stp文件。是否可以使用python来实现这个功能?

Run a Catia macro with a python script中有一个例子,但在我的情况下不起作用。我编辑了如下代码并给它一个运行。

import win32com.client
catapp = win32com.client.Dispatch("CATIA.Application")
catapp.StartCommand('Macro_schweller_model_lsopt.CATScript')

我遇到的错误是

File "C:\FK-Programme\python36-32\Anaconda\Install\lib\site-packages\win32com\client\dynamic.py", line 91, in _GetGoodDispatch
    IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)

com_error: (-2147221005, 'Ungültige Klassenzeichenfolge', None, None)

我的.CATscript看起来像这样

Sub CATMain()

    Dim FileToOpen as String
    Dim partDocument1 As Document
    Dim part1 As Part
    Dim AnglePara As Parameter
    Dim parameters1 As Parameters
    Dim AmplitudePara As Parameter
    Dim WavelengthPara As Parameter

    FileToOpen = "E:\Datei\Results\Optimization\LS_OPT_results\Optimization_model_1\Schweller_fully_corrugated.CATPart"

    Set partDocument1 = CATIA.Documents.Open(FileToOpen)

    Set part1 = partDocument1.Part

    Set parameters1 = part1.Parameters

    Set AnglePara = parameters1.RootParameterSet.DirectParameters.Item("Angle")

    AnglePara.Value = -7

    Set AmplitudePara = parameters1.RootParameterSet.DirectParameters.Item("Amplitude")

    AmplitudePara.Value = 30

    Set WavelengthPara = parameters1.RootParameterSet.DirectParameters.Item("Wavelength")

    WavelengthPara.Value = 30

    CATIA.DisplayFileAlerts = False

    partDocument1.Part.Update

    partDocument1.ExportData "E:\Datei\Results\Optimization\LS_OPT_results\Optimization_model_1\Schweller.stp", "stp" 


End Sub 

1 个答案:

答案 0 :(得分:0)

据我所知,StartCommand方法只能启动分配给工具栏按钮的宏。我建议您改为使用SystemService.ExecuteScript方法,该方法允许您直接运行脚本。然后,您的示例将被修改为如下所示:

import win32com.client
catapp = win32com.client.Dispatch("CATIA.Application")
catapp.SystemService.ExecuteScript(
    # Macro library name/path
    r"C:\Path\To\Directory\Containing\The\Script",
    # Type of macro library (document/directory/VBA project)
    1,  # directory
    # Macro name
    "Macro_schweller_model_lsopt.CATScript",
    # Function name
    "CATMain",
    # Arguments
    tuple(),
)

有关SystemService.ExecuteScript方法的更多信息,请访问http://catiadoc.free.fr/online/interfaces/interface_SystemService.htm#ExecuteScript