CAA开发之文档操作
CAA开发之文档操作CAA开发中经常会遇到对模型文件的创建 ,加载 ,删除 和保存等操作,项目开发中可针对通用功能对基本方法进行封装。本文梳理文档操作的基本方法,并举例对部分方法进行封装,仅供参考。
CAA中对文档操作的基本步骤如下:
创建会话 - creating the session
新建文档 -creating a new document
调用接口 -Query Interface
获取根容器 -retrieving the root container
保存文档- saving the document
删除文档 -removing the document
删除会话 - deleting the session
上述步骤详细说明参考百库全书技术文章 Creating a New Document。
文档(CATDocument)的操作主要依赖于CATDocumentServices 类,该类服务于文档的所有基本操作方法。通常用于批处理中对文档进行不可见的操作,如创建,打开,保存等。
下文将对各步骤进行补充说明。
1 文档创建(Create the new document)
文档的创建通常有以下几种方式。可比较CATIA中文件菜单项
New:新建一个空文档
NewFrom:新建自
新建文档代码:
CATDocument* pDoc = NULL;
HRESULT rc = CATDocumentServices::New("Part",pDoc);
if (NULL != pDoc)
{
cout << "New document created OK" << endl << flush;
}
else
{
cout << "ERROR in creating New document" << endl << flush;
return 2;
}
2 文档加载(Load the document)
文档加载即打开现有文档,主要方法包括
CATIIniInteractiveSession 中Open方法:打开文档并可视化显示
CATDocumentServices 中的 OpenDocument方法:打开文档,一般批处理执行
Open方法:
CATDocument *pInstrumentDoc = NULL;
CATIIniInteractiveSession *piSession=NULL;
CATSession *pSession = NULL;
rc = ::GetPtrSession(pSession);
rc = pSession->QueryInterface(IID_CATIIniInteractiveSession, (void**) &piSession);
CATIEditor *piEditor=NULL;
rc = piSession->Open(PathName,FALSE,&piEditor);
CATFrmEditor * pFrmEditor = piEditor->GetEditor();
pInstrumentDoc = pFrmEditor->GetDocument();
......
OpenDcument方法:
CATDocument *pDoc = NULL;
CATUnicodeString storageName = "XXXX"; //文档保存路径名
rc = CATDocumentServices::OpenDocument (storageName ,pDoc);
if (SUCCEEDED(rc) && (NULL != pDoc))
{
cout << "Document opened OK" << endl << flush;
}
else
{
cout << "ERROR in opening an existing document" << endl << flush;
return 2;
}
3 获取当前文档
获取当前文档,即获取文档指针*pDoc.上述方法都能获取该指针,但通常对文档操作是基于当前环境的,尤其是交互操作过程中,获取该文档指针之后才能对模型特征进行后续处理。获取当前编辑环境下文档指针方法如下,
CATFrmEditor * pEditor = GetEditor();
if (NULL != pEditor )
{
cout << "Editor got OK" << endl << flush;
}
else
{
cout << "ERROR in getting the current editor" << endl << flush;
return 1;
}
CATDocument *pDoc = pEditor->GetDocument();
if (NULL != pDoc)
{
cout << "Document opened OK" << endl << flush;
}
else
{
cout << "ERROR in opening an existing document" << endl << flush;
return 2;
}
该指针的获取还有其他方式,如通过CATILinkableObject 类获取。
4 保存文档(Save the Document)
4.1 另存:即保存为新模型
CATUnicodeString savePath = "XXXX"; //文档保存路径
rc = CATDocumentServices::SaveAs (*pDoc, savePath );
if (SUCCEEDED(rc))
{
cout << "Document saved OK" << endl << flush;
}
else
{
cout << "ERROR in saving document" << endl << flush;
return 5;
}
4.2 保存:不更新保存路径
rc = CATDocumentServices::Save (*pDoc);
if (SUCCEEDED(rc))
{
cout << "Document saved OK" << endl << flush;
}
else
{
cout << "ERROR in saving document" << endl << flush;
return 3;
}
5 删除(Remove the document)
rc = CATDocumentServices::Remove (*pDoc);
if (SUCCEEDED(rc))
{
cout << "Document removed OK" << endl << flush;
}
else
{
cout << "ERROR in removing document" << endl << flush;
return 6;
}
方法封装
上述是对文档操作的基本方法,在实际项目开发中,该类操作使用比较频繁,通常只需要获取相应的指针即可,如对本地模型的特征操作,我们需要加载本地模型文档——获取其文档指针——获取其容器——获取其根节点。每次操作都是这关键的几个步骤,不同之处在于模型的本地路径,以及相应的模型在模型树上的根节点。因此,我们可以对文档加载方法进行封装,即输入设置为文档路径,输出为该模型在模型树上的根节点指针。如“打开文件”操作,封装成OpenCATFile方法如下
输入:
CATUnicodeString PathName //文档(模型)的路径
输出:
CATIProduct ** opiSonProd //文档(模型)的指针
辅助功能函数:
获取当前模型树根节点
模型加载后可视化
刷新根节点
----->打开文件
CATBoolean XXXFunction::OpenCATFile(CATUnicodeString PathName,CATIProduct ** opiSonProd)
{
HRESULT rc=E_FAIL;
CATBaseUnknown * pRootProduct = NULL;
CATIProduct *piRootProduct = NULL;
rc = XXXFunction::GetProductRoot(&pRootProduct);//获取当前模型树根节点
if (SUCCEEDED(rc))
{
rc = pRootProduct->QueryInterface(IID_CATIProduct, (void**) &piRootProduct);
if (SUCCEEDED(rc)&&NULL!= piRootProduct)
{
CATDocument *pInstrumentDoc = NULL;
CATIIniInteractiveSession *piSession=NULL;
CATSession *pSession = NULL;
rc = ::GetPtrSession(pSession);
rc = pSession->QueryInterface(IID_CATIIniInteractiveSession, (void**) &piSession);
CATIEditor *piEditor=NULL;
rc = CATDocumentServices::OpenDocument(PathName, pInstrumentDoc) ;
if ( SUCCEEDED(rc) && ( NULL !=pInstrumentDoc))
{ cout <<"The document" <<PathName<< " is opened" << endl ; }
else
{ MessageBox(NULL,"模型路径输入错误,请重输!","通知消息“,NULL); }
CATIProduct *piInstanceProd=NULL;
rc = XXXFunction::AddExternalComponent(piRootProduct,pInstrumentDoc,&piInstanceProd);//模型可视化,即将模型添加到当前模型树下
if ( FAILED(rc) ) return FALSE;
// rc = piSession->Close(piEditor);
* opiSonProd=piInstanceProd;
XXXFunction::UpdateProductRoot(); //更新根节点
}
}
return TRUE;
}
----->加载模型可视化
HRESULT XXXFunction::AddExternalComponent(CATIProduct *ipiFatherProd, CATDocument *iDocument,CATIProduct ** opiSonProd)
{
//AddExternalComponent(piRootProduct,pInstrumentDoc,&piInstanceProdSP);
//AddExternalComponent全局函数所需的三个参数,
//被**的product document的root product,CATIProduct类型
//将要**的part document,CATDocument类型
//the product instance of the imported document
//注意要**的document也要检索它的root product
cout<<"添加组件执行"<<endl;
HRESULT rc = E_FAIL;
if (! ipiFatherProd) return E_FAIL;
if (! opiSonProd) return E_FAIL;
if ( NULL != iDocument)
{
// Get RootProduct of the document to import.
CATIDocRoots *piDocRootsOnDoc = NULL;
rc = iDocument->QueryInterface(IID_CATIDocRoots,
(void**) &piDocRootsOnDoc);
if ( FAILED(rc) ) return rc;
CATListValCATBaseUnknown_var *pRootProducts = piDocRootsOnDoc->GiveDocRoots();
CATIProduct_var spRootProduct = NULL_var;
if (NULL != pRootProducts)
{
if (pRootProducts->Size())
{
spRootProduct = (*pRootProducts);
delete pRootProducts;
pRootProducts = NULL;
}
piDocRootsOnDoc->Release();
piDocRootsOnDoc=NULL;
CATIProduct_var spProduct = NULL_var;
if (NULL_var != spRootProduct)
{
spProduct = ipiFatherProd->AddProduct(spRootProduct);
cout<<"NULL_var != spRootProduct"<<endl;
}
else
{
CATUnicodeString docName = iDocument-> StorageName();
ipiFatherProd->AddShapeRepresentation(CATUnicodeString("Model"),docName);
}
if (NULL_var != spProduct)
{
rc = spProduct->QueryInterface(IID_CATIProduct,(void**)opiSonProd);
if (FAILED(rc)||NULL==(*opiSonProd)) return E_FAIL;
}
}
}
return rc;
}
----->刷新模型根节点
void XXXFunction::UpdateProductRoot()
{
//*******************************important**************************************
CATFrmLayout * pLayout = CATFrmLayout::GetCurrentLayout();
CATFrmWindow * pWindow = pLayout->GetCurrentWindow();
CATFrmEditor * pEditor = pWindow->GetEditor();
CATDocument* pDoc = pEditor->GetDocument();
//特征对象已经创建完毕,需要对产品根节点发送消息,更新产品模型导航树
//首先,获取该当前活动文档的产品根节点
CATIProduct *piRootProduct = NULL;
CATIDocRoots* piDocRootsOnDoc = NULL;
HRESULT rc = pDoc -> QueryInterface(IID_CATIDocRoots,(void**) &piDocRootsOnDoc);
if (SUCCEEDED(rc))
{
// Retrieve the root product which is the first element of root elements
CATListValCATBaseUnknown_var* pRootProducts = piDocRootsOnDoc -> GiveDocRoots();
piDocRootsOnDoc -> Release();
piDocRootsOnDoc = NULL;
CATBaseUnknown *pBaseRootProduct = NULL;
if (pRootProducts && pRootProducts->Size())
{
pBaseRootProduct = (*pRootProducts);
delete pRootProducts;
pRootProducts = NULL;
if (NULL == pBaseRootProduct) return;
rc = pBaseRootProduct -> QueryInterface(IID_CATIProduct,(void**) &piRootProduct);
if (FAILED(rc)) return;
//对模型树进行刷新
CATISpecObject_var pPartRenew = (CATISpecObject_var)piRootProduct;//对零件进行刷新
pPartRenew->Update();
}
}
else return;
//----------------------------------------------------------------
//给产品根节点发送消息,通知其更新三维可视化模型和产品结构特征树。
CATIModelEvents_var spEvents = piRootProduct;
CATModify ModifyEvent(piRootProduct);
spEvents -> Dispatch (ModifyEvent);
//Update the graph view
CATIRedrawEvent_var spRedraw = piRootProduct;
spRedraw -> Redraw();
piRootProduct -> Release();
piRootProduct = NULL;
}
页:
[1]