谢谢saeba指导!获益匪浅!下面是CODE,请指导!
int ViewTestCmd:isPlayCatiaModel(CATUnicodeString DocumentName)
{
if (NULL!= _pTheModelToDisplay)//_pTheModelToDisplayOld
{
_p3DViewer->RemoveRep (_pTheModelToDisplay);
// _p3DViewer在"ViewTestCmd.h"中定义过CATNavigation3DViewer* _p3DViewer;
}
HRESULT rc;
CATUnicodeString oName ;
cout <<"Importing an existing CATProduct under the root product"<<endl;
//1.打开文档
CATDocument *pDoc= NULL;
rc = CATDocumentServices::Open(DocumentName,pDoc); ///DocumentName为要显示的装配文档
if ( FAILED(rc) || (NULL==pDoc) )
{
cout <<"opening a CATProduct document failed"<<endl;
return 1;
}
// 2.获取根产品:
CATIDocRoots *piDocRootsOnDoc = NULL;
rc = pDoc->QueryInterface(IID_CATIDocRoots,(void**) &piDocRootsOnDoc);
if ( FAILED(rc) )
{
cout <<"access to the root product failed"<<endl;
return 2;
}
CATListValCATBaseUnknown_var *pRootProducts=piDocRootsOnDoc->GiveDocRoots();
CATIProduct_var spRootProduct = NULL_var;
if (NULL != pRootProducts)
{
if (pRootProducts->Size())
{
spRootProduct = (*pRootProducts)[1];
delete pRootProducts;
pRootProducts = NULL;
}
}
else
{
cout<<"NULL != pRootProducts"<<endl;
}
piDocRootsOnDoc->Release();
piDocRootsOnDoc =NULL;
if (NULL_var == spRootProduct)
{
cout << " ERROR: No root product !! " << endl << flush;
return 3;
}
CATIProduct_var piProductOnRoot = NULL_var;
rc = spRootProduct->QueryInterface(IID_CATIProduct,
(void**) &piProductOnRoot);
if ( FAILED(rc) )
{
cout<< "QueryInterface IID_CATIProduct failed !"<<endl;
return 4;
}
else
{
////最终得到了piProductOnRoot
piProductOnRoot->GetPrdInstanceName(oName);
cout<<"pCurrentProduct InstanceName:"<<oName.ConvertToChar()<<endl;
}
CATListValCATBaseUnknown_var* pProductList = piProductOnRoot->GetAllChildren();
int numberOfProducts = pProductList->Size();
cout<<"pProductList->Size():"<<numberOfProducts<<endl;
//3.获取形状描述
CATILinkableObject_var spLinkableOnShapeRep = NULL_var;
if (SUCCEEDED(piProductOnRoot->GetShapeRep(spLinkableOnShapeRep)))
{
cout<<"piProductOnRoot GetShapeRep Succeed!!"<<endl;
}
else
{
cout<<"piProductOnRoot GetShapeRep Failed!!"<<endl;
}
CATVisManager* pVisManager = CATVisManager::GetVisManager();
if ( NULL == pVisManager )
{
cout <<" ERROR by retrieving the CATVisManager instance" << endl;
return 5;
}
for (int i = 1; i <= numberOfProducts; i++)
{
CATIProduct* piProductOnProduct = NULL;
if (SUCCEEDED((*pProductList)[i]->QueryInterface(IID_CATIProduct,(void**)&piProductOnProduct)))
{
if (SUCCEEDED(piProductOnProduct->GetShapeRep(spLinkableOnShapeRep)))
{
cout<<"GetShapeRep Succeed!!"<<endl;
CATPathElement* RootObjectPath=new CATPathElement(spLinkableOnShapeRep);
///------------------------------------------------------
list<IID> ListIVisu3d;
IID visu3d = IID_CATI3DGeoVisu ;
ListIVisu3d.fastadd(&visu3d);
CAT3DViewpoint * pVP = new CAT3DViewpoint();
///将上面四行代码放至FOR循环前面,则可显示装配中的两个零件,
//但加入两个零件后AttachTo方法失效
rc = pVisManager->AttachTo ( RootObjectPath, pVP, ListIVisu3d);
if ( FAILED(rc) )
{
cout <<" ERROR in the AttachTo method" << endl;
return 6;
}
CATI3DGeoVisu * pIVisuOnRoot =NULL ;
rc = spLinkableOnShapeRep->QueryInterface(IID_CATI3DGeoVisu,
(void **) & pIVisuOnRoot);
if ( SUCCEEDED(rc) )
{
CATRep * pRep = pIVisuOnRoot->GiveRep();
if ( NULL != pRep )
{
CAT3DRep * p3DRep = (CAT3DRep *) pRep;
_pTheModelToDisplay=(CAT3DBagRep *)p3DRep;
CAT3DBoundingSphere pBe = p3DRep->GetBoundingElement();
float radius = pBe.GetRadius();
cout <<" The radius of the bounding box = " << radius << endl;
}
pIVisuOnRoot->Release();
pIVisuOnRoot = NULL ;
}
else
{
cout <<" ERROR to retrieve the CATI3DGeoVisu interface" << endl;
return 7;
}
if ( (NULL != _p3DViewer) && ( NULL != _pTheModelToDisplay))
{
_p3DViewer->AddRep((CAT3DRep*)_pTheModelToDisplay);
}
}
}
}
///4.显示模型
if ( NULL != _p3DViewer)
{
_p3DViewer->Draw();
cout<<"_p3DViewer->Draw()!!"<<endl;
}
//pVisuManager->DetachFrom(_pRootObjectPath, pVP);
//CATDlgNotify * pPromptBox = new CATDlgNotify((CATApplicationFrame::GetApplicationFrame())->GetMainWindow(), "Right", CATDlgNfyInformation);
//pPromptBox->SetTitle(pOccurringError->GetNLSSource());
//pPromptBox->SetText("All have been down,ok!!!");
//pPromptBox->SetVisibility(CATDlgShow);
else
{
cout<<"(NULL == _p3DViewer) Failed!!"<<endl;
return 8;
}
rc = CATDocumentServices::Remove (*pDoc);
if ( FAILED(rc) )
{
cout <<"Deleting the *pDoc failed"<<endl;
return 9;
}
return 0;
} |