如何使用自己创建的 CATIA V5 自动化接口 Python?

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

我为 CATIA V5 创建了自己的自动化接口。我的接口实现了一个 CAA 接口。 下面是 SetComment 方法的示例实现。 CAAInterface 是一个假名

// 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;
}

我在我的 CATIA 环境中使用 CATScript 测试了它:

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"

而且效果很好。 对应的CATIA文档 enter image description here

此外,我创建了 Visual Studio VB 项目,添加了引用->COM->类型库(我的 CATIA V5 MyXYZAutInterf。如果 CATIA 是 运行,我可以看到)。

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

而且效果也很好。

现在我想将我的自动化界面与 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_

但现在我无法使用 myFact。 如果我这样做:

myFact.SetComment(shape1, "comment")

我收到 错误:需要一个 COM this 指针作为第一个参数。 我应该将 myFact 分配给产品(如 CATScript):

product1 = catapp.ActiveDocument.Product
myFact = product1

但我也收到 错误:unknown.SetComment。 我真的很沮丧。有人可以帮助我吗,?

我已经成功地为接口框架创建了 *.tlb,没有任何问题。我还创建了它从 CATBaseObject 派生的实现框架,并将 TIE 模式保留为创建的接口。

CAAIAVbCalling.idl:

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

方法实现:

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

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

return S_OK;
}

我已经在VBEditor 的引用下添加了创建的*.tlb。我无法从 Vbscript 实例化接口对象。

我修好了。我使用了 GetCustomerFactory("ALIAS_NAME")