0

我为 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 文档 在此处输入图像描述

此外,我创建了 Visual Studio VB项目,添加了参考->COM->Type Libraries(我的 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。我真的很沮丧。有人可以帮我

4

2 回答 2

0

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

于 2017-09-26T11:01:13.300 回答
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 实例化接口对象。

于 2017-09-20T08:43:39.923 回答