CAA:遍歷CATPart內(nèi)多個PartBody下拓撲

對于一個CATPart內(nèi)多個PartBody避消,可以采用如下方式遍歷其拓撲下翎;經(jīng)測試混稽,可以處理STP內(nèi)參數(shù)與非參數(shù)拓撲采驻;并對其中的平面著藍色审胚,Cylinder 著紅色

image.png

以下代碼修改自InstallRootDirectory\CAAMechanicalModeler.edu\CAAMmrFeatureTopoBRep

// COPYRIGHT DASSAULT SYSTEMES 2000
//============================================================================================
// 
// Mission         : Recolor fillets and planar faces in a Part document
//
//
// Illustrates     : o Document loading in session 
//                   o Access to part within document
//                   o Access to hierarchy of features within part, 
//                     filtering on fillets
//                   o Access to topology associated to features
//                   o Graphical modification of this topology
//                   o Direct access to all topological cells linked to part, 
//                     filtering on planar faces
//                   o Graphical modification of these faces
//                   o "save as" of the modified document
//
//==========================================================================
// How to execute :
//
//   mkrun -c "CAAMmrFeatureTopoBRep  FileNameIn FileNameOut"
//
//   where    FileNameIn  : The complete name of a Part document
//            FileNameOut : The complete name to saveas FileNameIn                         
//
//   ex:
//
//   CAAMmrFeatureTopoBRep $WSROOT/CAAMechanicalModeler.edu/InputData/CAAMmrPart.CATPart
//                             PartModified.CATPart
//
//   In the current directory, PartModified.CATPart will be created
//
//============================================================================================

// ObjectModelerBase Framework
#include "CATDocument.h"
#include "CATSessionServices.h"     // To Create,delete a session
#include "CATDocumentServices.h" 
#include "CATInit.h"
#include "CATIContainer.h"
#include "CATSession.h"

// ObjectSpecsModeler Framework
#include "CATIDescendants.h"
#include "CATISpecObject.h"
#include "CATLISTV_CATISpecObject.h"

// NewTopologicalObjects Framework
#include "CATBody.h"
#include "CATCell.h"

// GeometricObjects Framework
#include "CATGeometry.h"
#include "CATSurface.h"
#include "CATFace.h" 
#include "CATSolidCylinder.h"               // Geometric cylinder
#include "CATCylinder.h"
#include "CATCGMJournalList.h"  


// MechanicalModeler Framework
#include "CATIPrtContainer.h"
#include "CATIMfGeometryAccess.h"
#include "CATMfBRepDecode.h"
#include "CATIPrtPart.h"
#include "CATIPartRequest.h"
#include "CATIBodyRequest.h"

// MecModInterfaces Framework
#include "CATIBRepAccess.h"
#include "CATIGeometricalElement.h"

// Visualization Framework
#include "CATIVisProperties.h"            // To change faces color
#include "CATVisPropertiesValues.h"

// System Framework
#include "CATBaseUnknown.h"

// 
#include <iostream.h> 

void printParametersInputError() 
{
    cout <<"CAAMmrFeatureTopoBRep FileNameIn FileNameOut" << endl;
    cout <<"Where FileNameIn is the complete path of a CATPart document" <<endl;
    cout <<" and FileNameOut, the complete path of the output document" <<endl;
    cout <<"For FileNameIn, you can use the following Part: " ;
    cout <<" $WSROOT/CAAMechanicalModeler.edu/InputData/CAAMmrPart.CATPart"<< endl;
}

int main(int iArgc,
         char **iArgv) 
                      
{
  // return code
  int rcode = 0;

  //
  // 1- Checks number of arguments
  //
  if( 3!=iArgc )
  {
      printParametersInputError();
      return 1;
  }

  cout << "The CAAMmrFeatureTopoBRep use case begins" << endl;

  //
  // 2- Creates the session
  //
  char *pSessionName = "Sample session";
  CATSession *pSession = NULL;
  HRESULT rc = Create_Session(pSessionName, pSession) ;
  if( FAILED(rc) )
  {
      cout << "can not open session" << endl;
      return 1 ;
  }
  
  //
  // 3- Loads the input document
  //
  CATDocument *pDoc = NULL;
  rc= CATDocumentServices::OpenDocument(iArgv[1], pDoc) ;

  if( FAILED(rc) )
  {
      cout <<"Error in opening the document: " << iArgv[1] << endl ;
      return 1;
  }
  cout <<"   " << iArgv[1] << " is opened" << endl;

  CATInit *pDocAsInit = NULL;
  rc= pDoc->QueryInterface(IID_CATInit, (void**)&pDocAsInit) ;
  if( FAILED(rc) )
  {
      cout << "Error, the document does not implement CATInit"<< endl;
      return 1;
  }
        
  //
  // 4- Gets root container of the document
  //
  CATIPrtContainer *pSpecContainer = NULL ;
  pSpecContainer = (CATIPrtContainer*)pDocAsInit->GetRootContainer("CATIPrtContainer");

  pDocAsInit->Release();
  pDocAsInit = NULL ;

  if( NULL == pSpecContainer )
  {
      cout <<"Error, the root container is NULL" << endl;
      return 1;
  }

  //
  // 5- Retrieves the MechanicalPart of the document
  //
  CATIPrtPart_var spPart ( pSpecContainer->GetPart() );
  if ( NULL_var == spPart )
  {
      cout <<"Error, the MechanicalPart is NULL" << endl;
      return 1;
  }

  pSpecContainer->Release();
  pSpecContainer = NULL ;

  ////
  //// 6- Colorizes in green all the fillet features
  ////

  //// 
  //// 6-1 Gets the fillet features
  ////
  //CATLISTV(CATISpecObject_var) filletFeatures;
  //CATIDescendants *pPartAsDescendant = NULL;
  //rc = spPart->QueryInterface(IID_CATIDescendants, (void**)&pPartAsDescendant) ;
  //if( FAILED(rc) )
  //{
     // cout <<" The MechanicalPart does not implement CATIDescendant" << endl;
     // return 1;
  //}
  //        
  //pPartAsDescendant->GetAllChildren("CATIFillet", filletFeatures);

  //for(int currentFillet=1; currentFillet<=filletFeatures.Size(); currentFillet++)
  //{
  //   const CATISpecObject_var& currentfilletFeature = filletFeatures[currentFillet];
  //   if ( NULL_var != currentfilletFeature )
  //   {
  //      //
  //      // 6-2 Gets the BRep associated with the fillet
  //      //
  //      CATIMfGeometryAccess *pFilletAsGeomAccess = NULL;
  //      if (NULL_var != currentfilletFeature)
  //         rc= currentfilletFeature->QueryInterface(IID_CATIMfGeometryAccess,
  //                                                    (void**)&pFilletAsGeomAccess) ;
  //      else
  //         rc = E_FAIL;
  //            
  //      if ( SUCCEEDED(rc) )
        //{
  //         CATLISTV(CATBaseUnknown_var) filletBReps;
  //         pFilletAsGeomAccess->GetBReps(filletBReps);

  //         // 6-3 Colorizes BRep 
  //         for(int currentBRep=1; currentBRep<=filletBReps.Size(); currentBRep++)
        //   {
  //            CATIVisProperties *pFilletBrepAsGraphics = 0;
  //            const CATBaseUnknown_var& currentfilletBRep = filletBReps[currentBRep];
  //            if (NULL_var != currentfilletBRep)
  //               rc =  currentfilletBRep->QueryInterface(IID_CATIVisProperties,
  //                                                            (void**)&pFilletBrepAsGraphics) ;
  //            else
  //               rc = E_FAIL;

  //            if ( SUCCEEDED(rc) )
        //    {                
        //        CATVisPropertiesValues color;
  //                color.SetColor(0, 255, 0); // green
  //                pFilletBrepAsGraphics->SetPropertiesAtt(color, CATVPColor, CATVPMesh);

  //                pFilletBrepAsGraphics->Release();
  //                pFilletBrepAsGraphics = NULL ;
        //    }
        //   }
  //         pFilletAsGeomAccess->Release();
  //         pFilletAsGeomAccess = NULL ;
        //}
     //}
  // }
  // pPartAsDescendant->Release();
  // pPartAsDescendant = NULL ;
          
   //
   // 7- Colorizes in blue the planar faces of the result associated with  
   //    the main Part body

   //
   // 7-1 Retrieves the main body
   //
   CATIPartRequest_var spPartRequest = spPart ;
   if ( NULL_var == spPartRequest )
   {
       cout <<"Error on CATIPartRequest" << endl;
       return 1;
   }


    CATIPartRequest *pPartAsRequest = 0;
    rc = spPart->QueryInterface(IID_CATIPartRequest, (void**)&pPartAsRequest) ;
    if ( FAILED(rc) )
    {
        cout <<" Error in retrieving a QI on CATIPartRequest" << endl;
        return 1 ;
    }

    const CATUnicodeString stdContext(" "); // Sets the context for topo bodies lookup 

    // 7-1 Extracts the lists of its Body Features
    CATLISTV(CATBaseUnknown_var) BodyList;  
    pPartAsRequest->GetSolidBodies(stdContext, BodyList);
    //cout <<"   The list of Body Features:" ;
    //PrintGeometricalFeaturesSetsResult(BodyList);


   
    //7-2 Retrieves the feature holding the result of the main body
    //    It's the first element of the list returned by GetResults
    for(int currentBody=1; currentBody<=BodyList.Size(); currentBody++)
    {
        CATBaseUnknown_var spMainPartBody ;
        spMainPartBody = BodyList[currentBody];
        //rc = spPartRequest->GetMainBody("",spMainPartBody);
        //if ( FAILED(rc) || ( NULL_var == spMainPartBody) )
        //{
        //  cout <<"Error with GetMainBody" << endl;
        //  return 1;
        //}

        CATIBodyRequest_var spMainPartBodyBodyRequest = spMainPartBody ;
        if ( NULL_var == spMainPartBodyBodyRequest )
        {
             cout <<"Error with CATIBodyRequest_var on the main Part body" << endl;
            return 1;
        }

        CATLISTV(CATBaseUnknown_var) ListResult ;
        rc = spMainPartBodyBodyRequest->GetResults("",ListResult);
        if ( FAILED(rc) || (0 == ListResult.Size() ))
        {
             cout <<"Error with GetResults" << endl;
            return 1;
        }

  
   //
   // 7-3 Retrieves a CATIGeometricalElement pointer on the feature result
   //
    
        //CATIGeometricalElement_var spFeatureResultGeomElem = BodyList[currentBody] ;
        CATIGeometricalElement_var spFeatureResultGeomElem = ListResult[1] ;
        if ( NULL_var == spFeatureResultGeomElem )
        {
            cout <<"Error, the feature result does not implement CATIGeometricalElement" << endl;
            return 1;
        }

        //
        // 7-4 Retrieves the CATBody associating with the feature result
        //
        CATBody_var spPartTopoBody = spFeatureResultGeomElem->GetBodyResult();
        if ( NULL_var == spPartTopoBody )
        {
            cout <<"Error, the body returned by the GetSolid method is NULL" << endl;
            return 1;
        }

        //
        // 7-5 Gets all two dimensional topo cells representing the Part body
        //
        CATLISTP(CATCell) cells;
        spPartTopoBody->GetAllCells(cells, 2);


        //
        // 7-6 Processes the two dimensional topo cells 
        //
        for(int currentCell=1; currentCell<=cells.Size(); currentCell++)
        {
            CATCell_var pCell ( cells[currentCell] );
            if ( NULL_var != pCell )
            {
                CATGeometry *pGeometry = pCell->GetGeometry();

                if ( (NULL != pGeometry) && (pGeometry->IsATypeOf(CATPlaneType)) )
                {
                    CATIBRepAccess_var spBRepAccess ( CATBRepDecode(pCell, spFeatureResultGeomElem) );
                    if( NULL_var!=spBRepAccess )
                    {
                        //
                        // 7-7 Colorizes BRep
                        //
                        CATIVisProperties *pBRepAccessAsGraphics = 0;
                        rc= spBRepAccess->QueryInterface(IID_CATIVisProperties,
                                                                (void**)&pBRepAccessAsGraphics) ;
                        if ( SUCCEEDED(rc) )
                        {
                            CATVisPropertiesValues color;
                            color.SetColor(0, 0, 255); // Blue
                            pBRepAccessAsGraphics->SetPropertiesAtt(color, CATVPColor, CATVPMesh);
                      
                            pBRepAccessAsGraphics->Release();
                            pBRepAccessAsGraphics = NULL ;
                        }
                    }
                }
            }
        }

        //
        // 7-5 Gets all two dimensional topo cells representing the Part body
        //
        //CATLISTP(CATCell) cells;
        spPartTopoBody->GetAllCells(cells, 2);

        //cout<<cells.Size()<<endl;
        //
        // 7-6 Processes the two dimensional topo cells 
        //

        //CATFace *piFace = (CATFace*) (faceList[k]);
        //if (NULL == piFace) return (1);
        //CATSurface *piSurface = piFace->GetSurface();
        //if (piSurface->IsAKindOf(CATCylinder::ClassName()))
        //  piRodCylinder = (CATCylinder*) piSurface;



        for(int currentCell=1; currentCell<=cells.Size(); currentCell++)
        {
            CATCell_var pCell ( cells[currentCell] );
            if ( NULL_var != pCell )
            {
                CATGeometry *pGeometry = pCell->GetGeometry();
                //CATPCircleType
                //CATGeometryType
                //CATSurfaceType

                CATFace *piFace = (CATFace*) (cells[currentCell]);
                if (NULL == piFace) return (1);
                CATSurface *piSurface = piFace->GetSurface();
                if (piSurface->IsAKindOf(CATCylinder::ClassName()))
                //  piRodCylinder = (CATCylinder*) piSurface;
                //if ( (NULL != pGeometry) && (pGeometry->IsATypeOf(CATSurfaceType)) && (!pGeometry->IsATypeOf(CATPlaneType)))
                {
                    CATIBRepAccess_var spBRepAccess ( CATBRepDecode(pCell, spFeatureResultGeomElem) );
                    if( NULL_var!=spBRepAccess )
                    {
                        //
                        // 7-7 Colorizes BRep
                        //
                        CATIVisProperties *pBRepAccessAsGraphics = 0;
                        rc= spBRepAccess->QueryInterface(IID_CATIVisProperties,
                                                                (void**)&pBRepAccessAsGraphics) ;
                        if ( SUCCEEDED(rc) )
                        {
                            CATVisPropertiesValues color;
                            color.SetColor(255, 0, 0); // Red
                            pBRepAccessAsGraphics->SetPropertiesAtt(color, CATVPColor, CATVPMesh);
                      
                            pBRepAccessAsGraphics->Release();
                            pBRepAccessAsGraphics = NULL ;
                        }
                    }
                }
            }
        }
    }
   //
   // 8- Saves the document
   //
   // @CSC-Filter UNER
   rc = CATDocumentServices::SaveAs(*pDoc, iArgv[2]);
   if (S_OK == rc)
   {
     cout << "   The "<< iArgv[2] ;
     cout << " document contains the modified Part" << endl ;
   }
   else
   {
     cout << "Error in saving document "<< iArgv[2] << endl;
     return 1;
   }   
 
   //
   // 9- Closes the document
   //
   rc = CATDocumentServices::Remove(*pDoc);
   if (FAILED(rc))
   {
     cout << "Error in closing document" << endl ;
     return 1;
   }
   pDoc = NULL ;

   //
   // 10- Deletes all documents in session  
   //
   rc = Delete_Session(pSessionName);
   if (FAILED(rc))
   {
     cout << "Error in deleting session" << endl ;
     return 1;
   }  
   
   cout << "The CAAMmrFeatureTopoBRep use case is ended" << endl;

   return 0;
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市礼旅,隨后出現(xiàn)的幾起案子膳叨,更是在濱河造成了極大的恐慌,老刑警劉巖痘系,帶你破解...
    沈念sama閱讀 212,599評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件菲嘴,死亡現(xiàn)場離奇詭異,居然都是意外死亡汰翠,警方通過查閱死者的電腦和手機龄坪,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,629評論 3 385
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來奴璃,“玉大人悉默,你說我怎么就攤上這事」赌拢” “怎么了抄课?”我有些...
    開封第一講書人閱讀 158,084評論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長雳旅。 經(jīng)常有香客問我跟磨,道長,這世上最難降的妖魔是什么攒盈? 我笑而不...
    開封第一講書人閱讀 56,708評論 1 284
  • 正文 為了忘掉前任抵拘,我火速辦了婚禮,結(jié)果婚禮上型豁,老公的妹妹穿的比我還像新娘僵蛛。我一直安慰自己秕重,他們只是感情好檐蚜,可當我...
    茶點故事閱讀 65,813評論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著只盹,像睡著了一般衣形。 火紅的嫁衣襯著肌膚如雪驼侠。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 50,021評論 1 291
  • 那天谆吴,我揣著相機與錄音倒源,去河邊找鬼。 笑死句狼,一個胖子當著我的面吹牛笋熬,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播腻菇,決...
    沈念sama閱讀 39,120評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼突诬,長吁一口氣:“原來是場噩夢啊……” “哼苫拍!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起旺隙,我...
    開封第一講書人閱讀 37,866評論 0 268
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎骏令,沒想到半個月后蔬捷,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,308評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡榔袋,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,633評論 2 327
  • 正文 我和宋清朗相戀三年周拐,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片凰兑。...
    茶點故事閱讀 38,768評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡妥粟,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出吏够,到底是詐尸還是另有隱情勾给,我是刑警寧澤,帶...
    沈念sama閱讀 34,461評論 4 333
  • 正文 年R本政府宣布锅知,位于F島的核電站播急,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏售睹。R本人自食惡果不足惜桩警,卻給世界環(huán)境...
    茶點故事閱讀 40,094評論 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望昌妹。 院中可真熱鬧捶枢,春花似錦、人聲如沸飞崖。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,850評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蚜厉。三九已至长已,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間昼牛,已是汗流浹背术瓮。 一陣腳步聲響...
    開封第一講書人閱讀 32,082評論 1 267
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留贰健,地道東北人胞四。 一個月前我還...
    沈念sama閱讀 46,571評論 2 362
  • 正文 我出身青樓,卻偏偏與公主長得像伶椿,于是被迫代替她去往敵國和親辜伟。 傳聞我的和親對象是個殘疾皇子氓侧,可洞房花燭夜當晚...
    茶點故事閱讀 43,666評論 2 350