Coloring box faces in a Solid

Hi Folks,

I have a problem with Coloring a box faces in a Solid. I build a solid box with 6 face and i want to coloring all faces with a different color, but with simple AIS_Shape functionality i can color only the solid. I tried to use XCAF module and with ColorTool add all face different color, with the following code:

//------------------------ Initilization phase ---------------------------------

// first create the mViewer from an OpenGL Graphic dirver like in the samples

....

    mAISContext = new AIS_InteractiveContext (mViewer);
    mAISContext->SetAutomaticHilight (Standard_True);
    mAISContext->SetIsoNumber (0);

    mView->TriedronDisplay (Aspect_TOTP_LEFT_LOWER, Quantity_NOC_SKYBLUE, 0.1, V3d_ZBUFFER);
    gp_Dir normal (0.0, 0.0, 1.0);
    gp_Ax3 ax3 (gp_Pnt (0.0, 0.0, 0.0), normal); //aPlane.Location (), aPlane.Axis ().Direction ()

    mViewer->SetPrivilegedPlane (ax3);

    // Get root label of OCAF document
    TDF_Label root;
    root = mOCAFDoc->GetData ()->Root ();
    // Adding V3d_Viewer to OCAF document

    Handle (XCAFPrs_Driver) aPrsDriver = new XCAFPrs_Driver ();
    TPrsStd_DriverTable::Get ();
    TPrsStd_DriverTable::Get ()->InitStandardDrivers ();
    TPrsStd_DriverTable::Get ()->AddDriver (XCAFPrs_Driver::GetID (), aPrsDriver);

    if (!root.FindAttribute (TPrsStd_AISViewer::GetID (), mViewAttr)) {
        TPrsStd_AISViewer::New (root, mAISContext);
        DBASSERT (root.FindAttribute (TPrsStd_AISViewer::GetID (), mViewAttr));
    }

  mShapeTool =  XCAFDoc_DocumentTool::ShapeTool (root);
  mColorTool =  XCAFDoc_DocumentTool::ColorTool (root);

// ----------------------------------------------- TopoDS creating phase -------------------------------------------

        TopoDS_Face FACE1, FACE2, FACE3, FACE4, FACE5, FACE6;

// build edges with BRepBuilderAPI_MakeEdge then,

//buil dfaces with BRepBuilderAPI_MakeFace. FACE1, FACE2, FACE3, FACE4, FACE5, FACE6 and add to the ShapeTool

        mShapeTool->AddShape (FACE);

// and give a different color to each

mColorTool->SetColor (FACE1, Quantity_Color (red / 255.0, green / 255.0, blue / 255.0, Quantity_TOC_RGB), XCAFDoc_ColorSurf);

mColorTool->SetColor (FACE2, Quantity_Color (red / 255.0, green / 255.0, blue / 255.0, Quantity_TOC_RGB), XCAFDoc_ColorSurf);

....

mColorTool->SetColor (FACE5, Quantity_Color (red / 255.0, green / 255.0, blue / 255.0, Quantity_TOC_RGB), XCAFDoc_ColorSurf);

mColorTool->SetColor (FACE6, Quantity_Color (red / 255.0, green / 255.0, blue / 255.0, Quantity_TOC_RGB), XCAFDoc_ColorSurf);

// then build the Shell and the Solid and add to the ShapeTool

// ----------------------------------------------- Visualization phase -------------------------------------------------------------------------

// 1. If i use AIS_Context for visualization, only the Solid color will be visualized

       Handle (AIS_Shape) aComp = new AIS_Shape (SOLID);
        aComp->SetColor (Quantity_NOC_GREEN);
    //    display in the context
         mContext->Display (aComp, Standard_False);

// and update it

2. the other methode to Display with XCAF and a TPrsStd_AISPresentation

    Handle (XCAFDoc_ShapeTool) myAssembly = XCAFDoc_DocumentTool::ShapeTool (mOCAFDoc->GetData ()->Root ());
    Handle (XCAFPrs_Driver) aPrsDriver;
    TPrsStd_DriverTable::Get ()->FindDriver (XCAFPrs_Driver::GetID (), aPrsDriver);

    TDF_LabelSequence freeShapes;
    myAssembly->GetFreeShapes (freeShapes);
    for (Standard_Integer i = 1; i <= freeShapes.Length (); i++) {
        Handle (TPrsStd_AISPresentation) prs;
        if (!freeShapes.Value (i).FindAttribute (TPrsStd_AISPresentation::GetID (), prs)) {
            prs = TPrsStd_AISPresentation::Set (freeShapes.Value (i), XCAFPrs_Driver::GetID ());

            prs->Display (Standard_False);
        }
    }
    TPrsStd_AISViewer::Update (mOCAFDoc->GetData ()->Root ());

In first case all faces will be Quantity_NOC_GREEN color so it's wrong.

In the second case the faces will be Visualizes with the given color, because myAssembly->GetFreeShapes (freeShapes); give back all the TopoDS_Shapes what was add to the ShapeTool, and it will be Displayed with TPrsStd_AISPresentation. It is good but if I have a big model with thousands of faces then it will be very slow.

So I would like a single Display and colored solid.

Somebody can help me?

Best regars,

Sándor.

 

 

Biro Sandor's picture

Hi Folk,

I managed to solve this problem. First thing you need XCAFPrs_AISObject instead of AIS_Shape, and second you must put the faces of the solid in SubShape structure of the solid. And then the problem is solved.

Now I face the problem that if I want to color the edges too, then not all the edges was colored.

Best regards,

Sándor

Guido van Hilst not specified's picture

Hi Sandor,

Why dont you use TopExp_Explorer to loop over the faces and create an AIS_Shape for each fce?

TopExp_Explorer ex = TopExp_Explorer(SOLID, TopAbs_FACE, , opAbs_SHAPE);

            while (ex.More())
            {
          
                aComp = new AIS_Shape (ex.Current());
                aComp->SetColor (Quantity_NOC_GREEN);
                //display in the context
                mContext->Display (aComp, Standard_False);

                ex.Next();
            }

Biro Sandor's picture

Hi,

Thank you for your answer, yes I can do it, but if I have thousands of faces, then it will be very slow. So it is not the optimal version.

Thank you,

Sándor.

Patrik Mueller's picture

Hi Sándor

Another possible solution would be using AIS_ColoredShape. With AIS_ColoredShape, you wouldn't have a dependency on OCAF.

Greets,

Patrik

Biro Sandor's picture

Hi,

Thank you for your advice, I must investigate this functionality. A question than, how can I set the faces color?

Best regards,

Sándor.

UPDATED:

Now I see the AIS_ColoredShape class has:    void SetCustomColor (const TopoDS_Shape&   theShape, const Quantity_Color& theColor) method, witch is for this kind of propose. Thank you again.