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

2024-05-15 12:57:05 发布

您现在位置:Python中文网/ 问答频道 /正文

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

// 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进行了测试:

^{pr2}$

而且效果很好。 相应的CATIA文件 enter image description here

另外,我创建了visualstudioVB项目,添加了引用->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.SetComment(shape1, "comment")

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

product1 = catapp.ActiveDocument.Product
myFact = product1

但我也得到了错误:未知.SetComment. 我真的很沮丧。有人能帮帮我吗,?在


Tags: comasproductimportspart1dimpartcatia
2条回答

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

在caiavbcalling.idl公司名称:

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

方法实现:

^{pr2}$

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

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

相关问题 更多 >