CATIA macro from python

丶灬走出姿态 提交于 2019-12-08 05:36:02

问题


I would like to use my macro (.CATScript) to open the catia interface and make the changes listed in the macro script to the .CATpart and give output as .stp file. Is it possible to use python to realise this function?

There was an example in Run a Catia macro with a python script, but it didn't work in my case. I edited the code as below and gave it a run.

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

The error I had was

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)

My .CATscript looks like this

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:


The StartCommand method can (as far as I know) only start macros assigned to toolbar buttons. I advise you to instead use the SystemService.ExecuteScript method, which allows you to run the script directly. Your example would then be modified to look something like this:

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

More information on the SystemService.ExecuteScript method is available at http://catiadoc.free.fr/online/interfaces/interface_SystemService.htm#ExecuteScript.



来源:https://stackoverflow.com/questions/49610628/catia-macro-from-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!