PLM之家 UG NX11.0二次开发(ufun和NXOpen C++)网络培训火热报名

PLM之家-上海点团信息科技有限公司工业4.0数字化软件官方知识管理平台

查看: 80|回复: 1

[源码分享] Catia二次开发源码分享: 对面进行颜色修改和添加属性

[复制链接]
  • TA的每日心情
    无聊
    11 小时前
  • 签到天数: 103 天

    连续签到: 9 天

    [LV.6]常住居民II

    1919

    主题

    3193

    帖子

    5万

    积分

    管理员

    PLM之家mildcat

    Rank: 9Rank: 9Rank: 9

    积分
    57817
    发表于 2018-1-10 16:48:55 | 显示全部楼层 |阅读模式

    请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!

    您需要 登录 才可以下载或查看,没有帐号?QQ关联注册

    x

    Catia二次开发源码分享: 对面进行颜色修改和添加属性

    : y5 u4 I2 D; ^3 p S# O , H$ {+ m; t' [' D7 H9 ?( h& u& H& I3 i7 w( ]0 d+ a 2 t# [/ @; } w) G/ x, E" n' a/ s - n% H" U% \; S6 d
    [C] 纯文本查看 复制代码
    //
      // 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 - Retrieves BRepAccess of all faces
      //
    
      //6 - 1 Retrieving the feature holding the result of the main body
      //
      //get the part
      CATBaseUnknown_var spMainPartBody ;
      CATLISTV(CATBaseUnknown_var) ListResult ;
    
      CATIPartRequest_var spPartRequest = spPart ;
      if ( NULL_var == spPartRequest )
      {
        cout <<"Error on CATIPartRequest" << endl;
        return 1;
      }
      
      //get the main tool
      rc = spPartRequest->GetMainBody("",spMainPartBody);
      if ( FAILED(rc) || ( NULL_var == spMainPartBody) )
      {
        cout <<"Error with GetMainBody" << endl;
        return 1;
      }
    
      //get its associated body
      CATIBodyRequest_var spMainPartBodyRequest = spMainPartBody;
      if ( NULL_var == spMainPartBodyRequest )
      {
        cout <<"Error, spMainPartBodyRequest is NULL" << endl;
        return 1;
      }
    
      //Retrieves the feature holding the result of the main body
      //It's the first element of the list returned by GetResults (CAA documentation)
      rc = spMainPartBodyRequest->GetResults( "", ListResult) ;
      if (!SUCCEEDED(rc) || 0>= ListResult.Size())
      {
        cout <<"Error with GetResults" << endl;
    	  return 1;
      }
        
      CATIGeometricalElement_var spFeatureResultGeomElem = ListResult[1];
      if ( NULL_var == spFeatureResultGeomElem )
      {
        cout <<"Error, spFeatureResultGeomElem is NULL" << endl;
        return 1;
      }
    
      //retrieves its shape
      CATIShapeFeatureBody_var shapefeat = spFeatureResultGeomElem;
      if ( NULL_var == shapefeat )
      {
        cout <<"Error, the Shape Feature is NULL" << endl;
        return 1;
      }
    
      //retrieves the feature associated to the BodyOUT
      CATISpecObject_var FeatureSolid = shapefeat->GetResultOUT();
      if ( NULL_var == FeatureSolid )
      {
        cout <<"Error, the Feature Solid is NULL" << endl;
        return 1;
      }
    
    
      //6 - 2 retrieving its associated geometry
      //
    
      CATIMfGeometryAccess_var geoAccess = FeatureSolid;
      if (NULL_var == geoAccess)
      {
        cout <<"Error, the geometry access is NULL" << endl;
        return 1;
      }
    
      //6 - 3 retrieving BRepAccess from geometry
      //
      CATLISTV(CATBaseUnknown_var) breps;
      int nbfaces = geoAccess -> GetBReps(breps);
      if (0 == nbfaces)
      {
        cout<<"Error, there is no face associated to the geometry"<AddRef();
          ListBreps.Append(Brep);
        }
    	}
      breps.RemoveAll();
      nbfaces = ListBreps.Size();
      if (0 == nbfaces)
      {
        cout<<"Error, there is no face in the BRepAccess List"<
    上海点团信息科技有限公司,承接NX,CATIA,CREO等CAx软件,Teamcenter,3D Experience等PLM软件,工业4.0数字化软件的实施开发培训,详情QQ 939801026 Tel 18301858168 网址 www.diantuankj.com
    回复

    使用道具 举报

  • TA的每日心情
    无聊
    11 小时前
  • 签到天数: 103 天

    连续签到: 9 天

    [LV.6]常住居民II

    1919

    主题

    3193

    帖子

    5万

    积分

    管理员

    PLM之家mildcat

    Rank: 9Rank: 9Rank: 9

    积分
    57817
     楼主| 发表于 2018-1-10 16:50:30 | 显示全部楼层
    [C] 纯文本查看 复制代码
      //
      //7 - retrieving and displaying current faces colors
      //
      cout<<"--------------------------"<<endl;
      cout<<"Retrieving current colors"<<endl;
      CATListOfInt retrieveRed,retrieveGreen,retrieveBlue;
       
      rc = CATMmrApplicativeAttributes::GetColorsOnFaces( ListBreps, retrieveRed,retrieveGreen,retrieveBlue);
      if (E_FAIL == rc )
      {
        cout<<"Error, an error occured while retrieving current part colors"<<endl;
        return 1;
      }
    
      for (compt =1;compt<=nbfaces;compt++)
      {
        cout<<"for the "<<compt<<" face, associated color components are : R "<<retrieveRed[compt]<<" G "<<retrieveGreen[compt]<<" B "<<retrieveBlue[compt]<<endl;
      }
    
      //
      //8 - Setting a new Color on all faces
      //
    
      //we are to color the faces with different color according their position : three by three will have the same color
      cout<<"--------------------------"<<endl;
      cout<<"Setting new colors"<<endl;
      
      CATListOfInt newRed,newGreen,newBlue,FailedIndex;
      int x=0;
      for (compt = 1;compt<= nbfaces;compt++)
      {
        x = compt%3;
        int red(0),green(0),blue(0);
        if (1 == x)
          red = 255;
        if (2 == x)
          green = 255;
        if (0 == x)
          blue = 255;
        newRed.Append(red);
        newGreen.Append(green);
        newBlue.Append (blue);
        cout<<"Setting R "<<red<<" G "<<green<<" B "<<blue<<" on the "<<compt<<" face"<<endl;
      }
    
      rc = CATMmrApplicativeAttributes::SetColorsOnFaces(ListBreps,newRed,newGreen,newBlue,FailedIndex);
      if (E_FAIL == rc)
      {
        cout<<"Error, setting new color on faces failed for all faces"<<endl;
        return 1;
      }
      else if (S_FALSE == rc )
      {
        int nberrors = FailedIndex.Size();
        cout<<"Error, setting new color on faces fails for faces :";
        for (compt = 1;compt<= FailedIndex.Size(); compt++)
        {
          cout<<" "<<FailedIndex[compt];
        }
        cout<<endl;
      }
      else 
        cout<<"Colors successfully set on every faces"<<endl;
      //
      //9 - retrieving colors we have just set
      //
      cout<<"--------------------------"<<endl;
      cout<<"Retrieving new colors"<<endl;
      rc = CATMmrApplicativeAttributes::GetColorsOnFaces( ListBreps, retrieveRed,retrieveGreen,retrieveBlue);
      if (E_FAIL == rc )
      {
        cout<<"Error, an error occured while retrieving current part colors"<<endl;
        return 1;
      }
    
      for (compt =1;compt<=nbfaces;compt++)
      {
        cout<<"for the "<<compt<<" face, associated color components are : R "<<retrieveRed[compt]<<" G "<<retrieveGreen[compt]<<" B "<<retrieveBlue[compt]<<endl;
      }
    
      //
      //10 - Setting Applicative Attributes
      //
    
      //
      //10 - 1 creating new attributes 
      cout<<"--------------------------"<<endl;
      cout<<"Creating applicative attributes for different faces"<<endl;
      CATListOfCATUnicodeString **AttrNameListToSet = new CATListOfCATUnicodeString*[nbfaces];
    	CATCkeListOfParm *AttrValueListToSet = new CATCkeListOfParm[nbfaces];
      CATICkeParmFactory_var factory = CATCkeGlobalFunctions::GetVolatileFactory();
      for (compt = 1;compt<= nbfaces;compt++)
      {
        cout<<"Creating ";
        x = compt%3;
        //for each face we create a new list of apllicative attributes
        //an applicative attribute is made of a Name and a value
        if (1 == x)
        {
          AttrNameListToSet [compt-1] = new CATListOfCATUnicodeString (3);
          AttrValueListToSet[compt-1]   = new CATLISTV(CATBaseUnknown_var)(3);
    
          //The first attribute will be the number of the face
          AttrNameListToSet [compt-1]->Append("FACE_NUMBER");
          CATICkeParm_var tempcke1 = factory -> CreateInteger ("FACE_NUMBER",compt);
          AttrValueListToSet[compt-1]->Append(tempcke1);
          
          cout<<" FACE_NUMBER with value "<<compt; 
    
          //the second will be the color
          AttrNameListToSet [compt-1]->Append("COLOR");
          CATICkeParm_var tempcke2 = factory -> CreateString ("COLOR","Red");
          AttrValueListToSet[compt-1]->Append(tempcke2);
          
          cout<<" COLOR with value Red";
          
          //the third is x
          AttrNameListToSet [compt-1]->Append("X");
          CATICkeParm_var tempcke3 = factory -> CreateReal ("X",x);
          AttrValueListToSet[compt-1]->Append(tempcke3);
          
          cout<<" X with value "<<x;
          cout<<" for face nb "<<compt<<endl;
        }
        else 
        //just to show you you can have different size of list
        {
          AttrNameListToSet [compt-1] = new CATListOfCATUnicodeString (2);
          AttrValueListToSet[compt-1]   = new CATLISTV(CATBaseUnknown_var)(2);
    
          //The first attribute will be the number of the face
          AttrNameListToSet [compt-1]->Append("FACE_NUMBER");
          CATICkeParm_var tempcke1 = factory -> CreateInteger ("FACE_NUMBER",compt);
          AttrValueListToSet[compt-1]->Append(tempcke1);
    
          cout<<" FACE_NUMBER with value "<<compt; 
    
          //the second will be the color
          AttrNameListToSet [compt-1]->Append("COLOR");
          if (2 == x)
          {
            CATICkeParm_var tempcke2 = factory -> CreateString ("COLOR","Green");
            AttrValueListToSet[compt-1]->Append(tempcke2);
            
            cout<<" COLOR with value Green";
          }
          if (0 == x)
          {
            CATICkeParm_var tempcke2 = factory -> CreateString ("COLOR","Blue");
            AttrValueListToSet[compt-1]->Append(tempcke2);
            
            cout<<" COLOR with value Blue";
          }
    
          cout<<" on face nb "<<compt<<endl;
    
        }
      }
    
      //10 - 2 Setting the applicative attributes
      cout<<endl<<"Setting applicative attributes on different faces"<<endl;
      FailedIndex.RemoveAll();
      rc=CATMmrApplicativeAttributes::SetAttributesOnFaces(ListBreps, AttrNameListToSet,AttrValueListToSet,FailedIndex);
      if (E_FAIL == rc)
      {
        cout<<"Error, we failed to set applicative attributes on all faces"<<endl;
      }
      else if (S_FALSE == rc)
      {
        int nberrors = FailedIndex.Size();
        cout<<"Error, setting applicative attriutes on faces fails for faces :";
        for (compt = 1;compt<= FailedIndex.Size(); compt++)
        {
          cout<<" "<<FailedIndex[compt];
        }
        cout<<endl;
      }
      else
      {
        cout<<"Applicative attributes successfully set"<<endl;
      }
    
      //10 - 3 cleaning
      for  (compt=1; compt<=nbfaces;compt++)
    	{
    		AttrNameListToSet[compt-1]->RemoveAll(); delete AttrNameListToSet[compt-1];  AttrNameListToSet[compt-1] = NULL;
    		AttrValueListToSet[compt-1]->RemoveAll();delete AttrValueListToSet[compt-1]; AttrValueListToSet[compt-1] = NULL;
    	}
    
    	delete [] AttrNameListToSet; AttrNameListToSet = NULL;
    	delete [] AttrValueListToSet;AttrValueListToSet = NULL;
    
      //
      //11 - Retrieving the applicative attributes we have just set
      //
      cout<<"--------------------------"<<endl;
      cout<<"Getting applicative attributes on faces"<<endl;
      //
      //11 - 1 Getting attributes
      CATListOfCATUnicodeString * AttributNameList = new CATListOfCATUnicodeString[nbfaces];
      CATCkeListOfParm  AttrValList = new CATListValCATBaseUnknown_var[nbfaces];
      rc = CATMmrApplicativeAttributes::GetAttributesOnFaces(ListBreps, AttributNameList, AttrValList );
      if (SUCCEEDED(rc))
      {
        CATListOfCATUnicodeString templiststring;
        CATListValCATBaseUnknown_var tempCke;
        for (compt=1;compt<=nbfaces;compt++)
        {
          //retrieve a list a position k (arrays start at position zero).
          templiststring =  (AttributNameList)[compt-1];
          tempCke = (AttrValList)[compt-1];
    
          int nbAttributes = templiststring.Size();
          int nbValues     = tempCke.Size();
    
          //the list must have the same size! If not, we jump to the next face
          if (nbAttributes != nbValues)
          {
            cout<<"Error on face "<<compt<<" : nbAttributes and nbValues are different"<<endl;
            continue;
          }
          else
          {
            cout<<"For face "<<compt<<" there is(are) "<<nbAttributes<<" attribute(s) that is(are) : "<<endl;
          }
          for (int i=1;i<=nbAttributes;i++)
          {
            cout<<"    - "<<templiststring[i].ConvertToChar()<<" = "<<((CATICkeParm_var)( tempCke[i] ))->Content().ConvertToChar()<<endl;
          }
          templiststring.RemoveAll();
          tempCke.RemoveAll();
    
        }
      }
      else if (E_FAIL == rc)
      {
        cout<<"Error, applicative attributes couldn't be retrieved"<<endl;
        return 1;
      }
      //
      //11 - 2 Cleaning List:
      int i = 0;
      for  (i=0; i < nbfaces;i++)
      {
        AttributNameList[i].RemoveAll();
        AttrValList[i].RemoveAll();
      }
      delete [] AttributNameList;	AttributNameList = NULL;
      delete [] AttrValList;	    AttrValList = NULL;
    
      for (i = 1;i<= nbfaces;i++)
      {
        Brep = ListBreps [i];
        if (NULL != Brep)
        {
          Brep->Release();
        }
      }
      ListBreps.RemoveAll();

    6 ^1 R$ n6 L& _: A8 F8 Q  d0 {
    上海点团信息科技有限公司,承接NX,CATIA,CREO等CAx软件,Teamcenter,3D Experience等PLM软件,工业4.0数字化软件的实施开发培训,详情QQ 939801026 Tel 18301858168 网址 www.diantuankj.com
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 登录 | QQ关联注册

    本版积分规则

    <

    QQ|小黑屋|手机版|Archiver|PLM之家-工业4.0数字化驱动之家 ( 沪ICP备13003373-3

    GMT+8, 2018-1-19 21:02 , Processed in 0.134443 second(s), 28 queries .

    快速回复 返回顶部 返回列表