0

I've recently started doing a bit of Python coding, using VS Code, to create macros / applications for CATIA (using it's COM interface). In CATIA itself, I can use the included VBA editor to write macros, and in that editor I get full "intellisense" as expected. But when working in VS Code, there is no intellisense for the CATIA COM objects when editing the code, and so it is necessary to keep consulting the API documentation for objects types, methods, properties, enums constants etc ...

Based on research I've done, I believe it is possible to get intellisense working for these CATIA COM objects, but I've so far failed to actually get it working - hence I am here (for intelligence of the ACTUAL kind, and not the artificial kind!).

Here's a snippet of code to connect to a runing instance of the CATIA application:

from win32com.client.gencache import EnsureDispatch

CATIA = EnsureDispatch("CATIA.Application")
oActiveWindow = CATIA.ActiveWindow

My full python code works as expected at runtime. I'm just trying to get intellisense working in VS Code for easier coding.

I have the same issue if I try to work with Excel using win32com. After xlApp = EnsureDispatch("Excel.Application") intellisense doesn't present any methods / properties for xlApp.

If anybody can offer some guidance it would be much appreciated!

The code snippet I provided results in the required CATIA registered type libraries getting converted to python classes in %LOCALAPPDATA%\Temp\gen_py\3.13. Based on the research I've done, if I create a .env file in my python project folder and set PYTHONPATH to this folder path, then intellisense should work ... but it's not for me.

0

1 Answer 1

0

I found a solution for this. The solution involves using win32com.client.gencache.EnsureDispatch for runtime and comtypes.gen for intellisense during coding. In case it's useful for anybody else, here it is:

Requirements

Python package requirements: win32com and comtypes.

Example for working with Excel

In your python venv (virtual environment), run the following code once to build a cache of COM type library wrappers in the venv ...

from comtypes.client import GetModule
GetModule(r"C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE")

This will generate COM object type wrappers in .venv\Lib\site-packages\comtypes\gen .

Then, in your main python program file, you can do this ...

from typing import TYPE_CHECKING, cast
from win32com.client import gencache

if TYPE_CHECKING:
    from comtypes.gen import Excel as ExcelTypes

excel = cast("ExcelTypes._Application", gencache.EnsureDispatch("Excel.Application"))
ws = cast("ExcelTypes._Worksheet", excel.ActiveSheet)
ws.Range("A1").Value = "Hello Excel"

Example for CATIA (3DEXPERIENCE)

comtypes.client.GetModule can use the CATIA TypeLibrary (.tlb) files if you have them ...

from comtypes.client import GetModule
GetModule(r"C:\Program Files\Dassault Systemes\B427_Cloud\win_b64\code\bin\InfTypeLib.tlb")
GetModule(r"C:\Program Files\Dassault Systemes\B427_Cloud\win_b64\code\bin\VPMEditorContextIDLTypeLib.tlb")
GetModule(r"C:\Program Files\Dassault Systemes\B427_Cloud\win_b64\code\bin\PLMModelerBaseIDLTypeLib.tlb")

You would need to refer to the provided "C:\Program Files\Dassault Systemes\B427_Cloud\win_b64\code\bin\DSYAutomation.chm" documentation to know which TLB files to use.

Again, this will generate COM object type wrappers in .venv\Lib\site-packages\comtypes\gen .

Then, in your main python program file, you can do this ...

from com3dx import *
from typing import TYPE_CHECKING, cast

if TYPE_CHECKING:
    from comtypes.gen import INFITF
    from comtypes.gen import VPMEditorContextIDL
    from comtypes.gen import PLMModelerBaseIDL

CATIA = cast("INFITF.Application", get3dxClient(False,False))
oEditor = CATIA.ActiveEditor
oPLMProductService = cast("VPMEditorContextIDL.PLMProductService", oEditor.GetService("PLMProductService"))
oPLMEntity = cast("PLMModelerBaseIDL.PLMEntity", oPLMProductService.EditedContent.GetItem(1))
print(oPLMEntity.GetAttributeValue("V_Name"))

Note: the com3dx module is one provided with the CATIA software install. CATIA = cast("INFITF.Application", get3dxClient(False,False)) can be replaced with CATIA = cast("INFITF.Application", win32com.client.gencache.EnsureDispatch("CATIA.Application"))

I know the CATIA example is probably not of interest to many people out there, but thought I would include it anyway! If there are any experienced python win32com programmers out there who can offer constructive feedback on this approach then please do.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.