1

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 
Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
  • I have no idea about VBA, but could the `_` be a problem in the file names - even though, they're not forbidden apparently (https://msdn.microsoft.com/en-us/vba/language-reference-vba/articles/visual-basic-naming-rules)? Also, did you search for `Ungültige Klassenzeichenfolge` and the english translation? – Kalle Richter Apr 02 '18 at 12:07
  • Ungültige Klassenzeichenfolge translates to invalid class string – Simulationeng_sam Apr 02 '18 at 12:20
  • Is your question answered ? – FabioSpaghetti Aug 31 '18 at 12:16

1 Answers1

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.

  • Hi @Olle-vidner, I will call macros that I have recorded via python. What do I have to enter as function name? What does "The name of the function to invoke" mean? – Shahram Shinshaawh Jul 23 '20 at 17:53
  • @ShahramShinshaawh, the procedure should be mostly the same. "The name of the function to invoke" is the name of the function or subroutine _within_ the macro file that you want to run. When you record macros interactively in CATIA, this is usually `CATMain`. Please consider posting a new question with some more context to your problem, and maybe I can help you. – Olle Vidner Jul 28 '20 at 16:01