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

时间:2017-09-07 12:23:04

标签: python com tlb catia caa

我为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这个指针作为第一个参数。 我应该将myFact分配给产品(如CATScript):

product1 = catapp.ActiveDocument.Product
myFact = product1

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

2 个答案:

答案 0 :(得分:0)

我已经成功地为接口框架创建了* .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实例化接口对象。

答案 1 :(得分:0)

我修好了。我使用了GetCustomerFactory(“ALIAS_NAME”)