2

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.

Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
  • 1
    Please show us what did you try. – smartrahat Jan 17 '16 at 00:42
  • Well I tried using the method of import win32com.client o=win32com.client.Dispatch("Object.Name") However it didn't work. But like an hour later after giving it much thought I realised that the macro recorded was not a file itself but like an item inside the CATPart, so after investigating a little I saw I could save the macros as CATScript. I did and then opened it using os.startfile('Path\to\Macro') And seems to be working... However I'm open to better solutions... – Javier Parrondo Jan 18 '16 at 01:08

1 Answers1

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!

pvphan
  • 26
  • 3
  • So much better than what I did!! This works like a charm. I had managed to find a workaround by saving the macro as a .CATScript and then calling it (that worked because these files are set to oopen with Catia by default) but your solution works way better, thanks! – Javier Parrondo Mar 27 '16 at 21:17
  • i'm glad it worked out! and thanks for letting me know about the .CATScript method! – pvphan Mar 29 '16 at 01:48
  • 1
    you should change satapp to catapp in your code, the StackExchange does not allow to correct errors less than 6 characters – FabioSpaghetti Aug 31 '18 at 13:41
  • hi im going to write a function in python that create a macro catvbs file and run that macro. Could you guys help me, how can i import a macro file to CATIA macro libraries and run it via python? – Shahram Shinshaawh Jul 18 '20 at 09:39