Catia的二次开发

更新时间:2023-09-29 18:53:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

近的项目也快结束了,第一次接触Catia的二次开发,对于一个以前完全没有学过Catia的人来说,当时感觉这似乎是不可能完成的任务。Catia二次开发多数是以VB进行,网上的资料也偏VB居多,而我们偏偏选择了CAA,使用C++开发。网上关于CAA的二次开发相关资料不多,但是CAA自带了很详细的说明文档,类库,和大量示例程序(虽然我至今也仍未跑成功过它的例子)。现在就准备将项目开发过程中一些认为比较重要的部分记录下来。

在项目中,有一个很重要的功能,就是上传Catia文件。普通的上传文档并不困难,但关键在于Catia文件带有相链接的文档,比如CATProduct文件,所以需要在程序中自动找到Catia文件的相链接的文档。

起初,在帮助文档中找到一篇关于Retrieving a Product's Properties的文章,其中介绍了如何打印一个文档的属性。首先它获得文档的根CATIProduct对象,然后获得它的所有孩子GetAllChildren(),这个方法将获得CATIProduct对象的所有孩子,并且不分层次。然后使用CATIAttributesDescription类来获得CATIProduct的属性名,最后通过CATIInstance对象获得属性名对应的属性值,从CATIProduct获得CATIAttributesDescription和CATIInstance对象的操作如下: CATIProduct *iInstanceProd; ......

CATIAttributesDescription *piAttrDesc = NULL;

iInstanceProd->QueryInterface(IID_CATIAttributesDescription, (void **) &piAttrDesc); CATIInstance *piInstance = NULL;

rc = iInstanceProd->QueryInterface(IID_CATIInstance, (void **) &piInstance);

接着就可以使用CATIAttributesDescription的List()方法将属性获取到一个CATListValCATAttributeInfos对象中,然后就是对CATListValCATAttributeInfos的一个遍历过程。

CATListValCATAttributeInfos attrInfoList; piAttrDesc->List(&attrInfoList); for (int i = 1; i <= attrInfoList.Size(); i++) {

CATAttributeInfos attrInfo = attrInfoList[i];

const CATUnicodeString& propertyName = attrInfo.Name(); //属性名 const CATUnicodeString& valueType = attrInfo.Type()->Name(); //属性类型

CATIValue *pValue = piInstance->GetValue(propertyName); //获得对应属性名的属性值 }

上面方式是在不知道有什么属性的时候将所有属性都获取出来,如果知道属性名称的话,

直接通过CATIInstance的GetValue(..)就可以获取该属性名的属性值,比如零部件号的属性名对应的是PartNumber,还有一些用户自定义的参数也可以同样获取到。到这时,我认为通过它能够获得文档的路径,因为里面有一个属性名DefaultRepSource,就是默认的链接文档源,的确在测试中,通过该属性也能获取部分文档的保存路径,但只限于CATPart文件,而对于CATProduct文件却是NULL,找了很多方法,但是通过使用CATIInstance对象来获取CATProduvt孩子中的CATProduct文件地址还是不行。

如何获得孩子的所有文档路径,只能寻找别的方法。于是发现了CATIxPDMItem这个类,这个类很强大,它可以获得文档的所有属性,还有文档是否需要保存等信息。如何获得CATIxPDMItem的实例,版本不同CAA还是有区别,r_16之上和r_14(r_15没试过)的获取CATIxPDMItem对象方法完全不同,两个版本的CATIxPDMItem的方法也不同,虽然高版本的保留了低版本的方法。至于如何获取CATIxPDMItem对象,r_14版本中需要使用到CATxPDMSession对象,而r-16版本则使用CATxPDMSessionServices的静态方法,这里只说r_16版本的(可以在类库中查找CATIxPDMItem,查看不同版本的获得该对象方法)。

CATIxPDMItem_var mItem; CATDocument *pDoc;

CATxPDMSessionServices::GetItemFromDocument(pDoc,mItem); 以上方法将pDoc中获得CATIxPDMItem对象。 现在看一下CATIxPDMItem的方法:

GetChildren(CATLISTV(CATBaseUnknown_var)&,CATLISTP(CATMathTransformation)这是保留的低版本中的获取孩子的方法

GetChildrenItems(CATListValCATIxPDMItem_var&,CATLISTP(CATMathTransformation) 两个方法都是获得CATIxPDMItem当前下一层的孩子,但是对于再下一层的孩子无法获得,所以如果要获得所有孩子需要递归或者迭代的方法。

GetDocFileName(CATUnicodeString& ) 该方法可以获得文档的保存路径

GetDocId(CATIDocId* ) CATIDocId是一个很基本的包含文档属性的类,通过它也可以获得文档路径,还有文档类型

GetItemType(CATUnicodeString& ) 获得文档类型,由于在r_14版本中并没有该方法,所以在r_14中需要间接的从CATIDocId获得文档类型

GetProperty(CATUnicodeString&,CATUnicodeString& )获得指定属性名的属性值 SetProperty(CATUnicodeString&,CATUnicodeString& ) 设置指定属性名的值 标准的属性名有:

? CN_PART_NUMBER 零部件号 ? CN_REVISION 版本

? CN_DEFINITION 定义 ? CN_NOMENCLATURE ? CN_DESCRIPTIONREF 描述

? CN_SOURCE 文档源,通过它也可以获得文档路径,但是我在尝试文档重定位的时候,并未成功? ? 。。。。。

? 下面是获得所有相联子部件的递归函数 [code]

void GetSource(CATIxPDMItem_var mItem) {

HRESULT rc;

CATUnicodeString strSource; CATUnicodeString strType; CATUnicodeString strName;

CATListValCATIxPDMItem_var oChildrenList;

CATLISTP(CATMathTransformation) oChildrenLocationList ; rc=mItem-> GetChildrenItems( oChildrenList, oChildrenLocationList) ; if(SUCCEEDED (rc)) {

int n=oChildrenList.Size(); for (int i = 1; i <= n; i++) {

CATIxPDMItem_var item=oChildrenList[i]; if(SUCCEEDED(item->GetItemType(strType))) {

item->GetDocFileName(strSource) ; if(strType==\{

cout<<\文件 \}

else if(strType==\{

cout<<\文件 \GetSource(item); }

} } } } [/code]

CATIA CAA 二次开发详细教程(10) 文档操作方法 创建 加载 保存

一、创建(Create the new document)

CATDocument* pDoc = NULL; rc = CATDocumentServices::New(\pDoc) { cout << \creating New document\

Now that the session is opened, you can create a new document using the New static method of CATDocumentServices. This method creates a document and initializes it, allowing it to be loaded and stored and making it editable. In this use case, a predefined document type, \is used as a document type. In interactive mode, this is the name that appears when performing a File/New operation. It is not the file extension, which, in this case, is \

二、打开(Load the document)

CATDocument *pDoc = NULL; rc = CATDocumentServices::Open(argv[1], pDoc); if (SUCCEEDED(rc) && (NULL != pDoc)) { cout << \opened OK\<< endl << flush; } else { cout << \

Now that the session is opened, you can load an existing document using the Open static method of CATDocumentServices. This method needs, as a first parameter, the entire storage path name and document name of the existing document that we want to load into the session. In this use case, we enter this information as an argument to the program. The second parameter of the Open method returns a CATDocument pointer to the document it has loaded. Once the document exists in the session, you can work with objects within it, using specific interfaces external to the ObjectModelerBase framework.

三、获取当前文档

CATFrmEditor * pEditor = GetEditor(); if (NULL != pEditor )

{ cout << \editor\<< endl << flush; return 1; } CATDocument *pDoc = pEditor->GetDocument(); if (NULL != pDoc) { cout << \opened OK\<< endl << flush; } else { cout << \

四、提取根容器(Retrieving the Document Root Container)

CATInit *piInitOnDoc = NULL; rc = pDoc -> QueryInterface (IID_CATInit, (void**) &piInitOnDoc); if (FAILED(rc)) { cout << \endl << flush; return 3; } const CATIdent idCATIContainer = \CATIContainer *piRootContainer = NULL; piRootContainer = (CATIContainer*) piInitOnDoc -> GetRootContainer(idCATIContainer); if (NULL == piRootContainer) { cout << \GetRootContainer\

The document root container is retrieved using the CATInit::GetRootContainer method. The CATIContainer handle retrieved through GetRootContainer will be necessary whenever you want to create or manipulate objects in the document.

五、保存文档(Save the Document) 5.1 另存

rc = CATDocumentServices::SaveAs (*pDoc, argv[1]); if (SUCCEEDED(rc)) { cout << \

本文来源:https://www.bwwdw.com/article/98hd.html

Top