Run a Catia macro with a python script

2019-05-26 12:47发布

问题:

I was looking for some help with handling Catia V5 with a python script from the windows PowerShell. I need help building a script that tells Catia to run a macro which I already recorded.

Also, some help to find a command that closes or doesn't let message boxes appear would be greatly appreciated.

回答1:

If I understand correctly, you're trying to run a recorded CATIA macro (.catvba?) and call it from Python which is called by PowerShell. I'll assume your PowerShell calling Python is working as intended.

Here's one way to bridge the gap between Python and CATIA VBA:

  1. Bind your CATIA macro to a custom toolbar icon, you'll notice when you hover your mouse over the icon that the name of the macro will appear in the bottom right corner of CATIA, e.g. "c:Your_macro_name".

  2. Once you are at this stage you can call the macro from Python with:

    import win32com.client
    catapp = win32com.client.Dispatch('CATIA.Application')
    catapp.StartCommand('Your_macro_name')
    

(code credit to Automate CATIA V5 with Python)

This should call your CATIA macro (under its toolbar name).

Also, to suppress some of the messages that come up in CATIA, try starting your VBA code with:

CATIA.RefreshDisplay = False
CATIA.DisplayFileAlerts = False

Hope this helps!