admin 发表于 2018-1-10 16:48:55

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

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




//
// 3- Loads the input document
//
CATDocument *pDoc = NULL;
rc= CATDocumentServices::OpenDocument(iArgv, pDoc) ;

if( FAILED(rc) )
{
    cout <<"Error in opening the document: " << iArgv << endl ;
    return 1;
}
cout <<"   " << iArgv << " 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;
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"<<endl;
    return 1;
}

CATLISTP(CATIBRepAccess) ListBreps;
        CATIBRepAccess * Brep = NULL;
CATBaseUnknown * Unk = NULL;
   
        int compt = 1;
for(; compt <= nbfaces; compt++)
{
                Unk = breps;
    Brep = (CATIBRepAccess * )Unk;
    if (NULL != Brep)
    {
      Brep->AddRef();
      ListBreps.Append(Brep);
    }
        }
breps.RemoveAll();
nbfaces = ListBreps.Size();
if (0 == nbfaces)
{
    cout<<"Error, there is no face in the BRepAccess List"<<endl;
    return 1;
}
else
{
    cout<<"There is(are) "<<nbfaces<<" face(s) on the part"<<endl;
}

admin 发表于 2018-1-10 16:50:30


//
//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<<" G "<<retrieveGreen<<" B "<<retrieveBlue<<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;
    }
    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<<" G "<<retrieveGreen<<" B "<<retrieveBlue<<endl;
}

//
//10 - Setting Applicative Attributes
//

//
//10 - 1 creating new attributes
cout<<"--------------------------"<<endl;
cout<<"Creating applicative attributes for different faces"<<endl;
CATListOfCATUnicodeString **AttrNameListToSet = new CATListOfCATUnicodeString*;
        CATCkeListOfParm *AttrValueListToSet = new CATCkeListOfParm;
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 = new CATListOfCATUnicodeString (3);
      AttrValueListToSet   = new CATLISTV(CATBaseUnknown_var)(3);

      //The first attribute will be the number of the face
      AttrNameListToSet ->Append("FACE_NUMBER");
      CATICkeParm_var tempcke1 = factory -> CreateInteger ("FACE_NUMBER",compt);
      AttrValueListToSet->Append(tempcke1);
      
      cout<<" FACE_NUMBER with value "<<compt;

      //the second will be the color
      AttrNameListToSet ->Append("COLOR");
      CATICkeParm_var tempcke2 = factory -> CreateString ("COLOR","Red");
      AttrValueListToSet->Append(tempcke2);
      
      cout<<" COLOR with value Red";
      
      //the third is x
      AttrNameListToSet ->Append("X");
      CATICkeParm_var tempcke3 = factory -> CreateReal ("X",x);
      AttrValueListToSet->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 = new CATListOfCATUnicodeString (2);
      AttrValueListToSet   = new CATLISTV(CATBaseUnknown_var)(2);

      //The first attribute will be the number of the face
      AttrNameListToSet ->Append("FACE_NUMBER");
      CATICkeParm_var tempcke1 = factory -> CreateInteger ("FACE_NUMBER",compt);
      AttrValueListToSet->Append(tempcke1);

      cout<<" FACE_NUMBER with value "<<compt;

      //the second will be the color
      AttrNameListToSet ->Append("COLOR");
      if (2 == x)
      {
      CATICkeParm_var tempcke2 = factory -> CreateString ("COLOR","Green");
      AttrValueListToSet->Append(tempcke2);
      
      cout<<" COLOR with value Green";
      }
      if (0 == x)
      {
      CATICkeParm_var tempcke2 = factory -> CreateString ("COLOR","Blue");
      AttrValueListToSet->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;
    }
    cout<<endl;
}
else
{
    cout<<"Applicative attributes successfully set"<<endl;
}

//10 - 3 cleaning
for(compt=1; compt<=nbfaces;compt++)
        {
                AttrNameListToSet->RemoveAll(); delete AttrNameListToSet;AttrNameListToSet = NULL;
                AttrValueListToSet->RemoveAll();delete AttrValueListToSet; AttrValueListToSet = 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;
CATCkeListOfParmAttrValList = new CATListValCATBaseUnknown_var;
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);
      tempCke = (AttrValList);

      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.ConvertToChar()<<" = "<<((CATICkeParm_var)( tempCke ))->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.RemoveAll();
    AttrValList.RemoveAll();
}
delete [] AttributNameList;        AttributNameList = NULL;
delete [] AttrValList;          AttrValList = NULL;

for (i = 1;i<= nbfaces;i++)
{
    Brep = ListBreps ;
    if (NULL != Brep)
    {
      Brep->Release();
    }
}
ListBreps.RemoveAll();
页: [1]
查看完整版本: Catia二次开发源码分享: 对面进行颜色修改和添加属性