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

1.6k views Asked by At

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?

2

There are 2 answers

0
LaTB On BEST ANSWER

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

3
Karthick On

I have successfully created the *.tlb for the interface framework without any issue. I have also created the implementation framework which it derives from CATBaseObject and kept TIE mode as created interface.

CAAIAVbCalling.idl:

interface CAAIAVbCalling : CATIABase 
{
     HRESULT NewStrFun(in CATBSTR  istr,
        out  /*IDLRETVAL*/ CATBSTR  ostr );
};

Method implementation:

HRESULT __stdcall CAAEVbCallingComp::NewStrFun(CATBSTR  istr,CATBSTR  *ostr )
{

    cout << "CAAEVbCallingComp::NewStrFun" << endl;

return S_OK;
}

I have added the created *.tlb under reference in VBEditor. I am unable to instantiate interface object from Vbscript.