Search code examples
pythoncomtlbcatiacaa

How to use own created Automation Interface for CATIA V5 with Python?


I created own Automation Interface for CATIA V5. My interface implements one CAA interface. Here is the example implementation of SetComment method. CAAInterface is a fake name

// MyXYZClass : SetComment
HRESULT MyXYZClass::SetComment( CATISpecObject_var ispObject, const 
CATBSTR &irComment )
{
  CAAInterface_var spInfo = ispObject;
  if( !!spInfo )
  {
    CATUnicodeString commentToSet;
    commentToSet.BuildFromBSTR( irComment ); 
    spInfo->SetComment( commentToSet );
  }
  return S_OK;
}

I tested that with CATScript inside of my CATIA Environment:

Sub CATMain()
' retrieve ASMPRODUCT of Part or Product
Dim myPrd As Product
Set myPrd = CATIA.ActiveDocument.Product
' Retrieve My Factory of Document
Dim myFact As MyFactoryVB
Set myFact = myPrd
' Retrieve Object as part
Dim myObject As AnyObject
Set myObject = CATIA.ActiveDocument.Part
' SetComment
myFact.SetComment myObject, "comment"

And it worked perfectly. Corresponding CATIA document enter image description here

Additionally I created Visual Studio VB project, added reference->COM->Type Libraries (my CATIA V5 MyXYZAutInterf. If CATIA is running, I can see it).

Imports System.Runtime.InteropServices
Imports MyXYZAutInterf
Imports MECMOD
Imports ProductStructureTypeLib
' attach catia
Sub Main()
    ' retrieve ASMPRODUCT of Part or Product
    Dim product As Product
    product = CATIA.ActiveDocument.Product
    ' Retrieve My Factory of Document
    Dim myFact As MyFactoryVB
    myFact = product
    ' Retrieve Object as part
    Dim part1 As Part
    part1 = CATIA.ActiveDocument.Part
    ' Find object by Name
    Dim myObject As AnyObject
    myObject = part1.FindObjectByName("Pad.1")
    ' SetComment
    myFact.SetComment(myObject, "comment")
End Sub

And it worked perfectly too.

Now I want use my automation interface with Python

# First I generated wrapper code for my type library
import sys
if not hasattr(sys, "frozen"):
    from comtypes.client import GetModule
    GetModule("C:/..//MyXYZTypeLib.tlb")

#load my module
from comtypes.gen import MyXYZAutInterf as myModul 
# myModul -> MyFactoryVB -- <unbound method MyFactoryVB.SetComment>

# Connecting to windows COM 
catapp = win32com.client.Dispatch("CATIA.Application")
documents1 = catapp.Documents
partDocument1 = documents1.Item("Part.CATPart")
part1 = partDocument1.Part
bodies1 = part1.Bodies
body1 = bodies1.Item("PartBody")
shapes1 = body1.Shapes
shape1 = shapes1.Item("Pad.1")

myFact = myModul.MyFactoryVB()
# now I can see all my implemented methods under _methods_

But now I am not able to use myFact. If I do:

myFact.SetComment(shape1, "comment")

I get error: Expected a COM this pointer as first argument. I should to assign myFact to product (like CATScript):

product1 = catapp.ActiveDocument.Product
myFact = product1

But I get error too: unknown.SetComment. I'm realy frustrated. Can somebody help me, please?


Solution

  • I fixed it. I used GetCustomerFactory("ALIAS_NAME")