how to display colors when step file is loaded (display drawing colors)

Hi,

I can't get the colors loaded from the step file when using the reader.

If i use the standard reader it displays the stepfile without colors :

 STEPControl_Reader Reader;
    Reader.ReadFile("delta_complete.step");
    std::cout << Reader.TransferRoots() << " Reader roots transferred." << std::endl;
    TopoDS_Shape shape=Reader.OneShape();
    Handle(AIS_Shape) ais_shape=new AIS_Shape(shape)
    m_aisContext->Display(ais_shape,Standard_False);

If i use the STEP-CAF-Control-Step reader. It will quit the application when extracting the reader to the doc.

It gives a segmentation fault using linux.

Is there a nice code snipped that shows how to read in a step file including displaying colors?

 

 

 

michelwijnja_165292's picture

For info. I can compile the mayo application without problem's it shows the colors correct. So i should work.

The founder of mayo has helped me already with the rotating function. But he asked me to ask here the question about previewing colors.

The code of mayo is for me too complex to find out how he exactly retrieve's the colors.

I hope to find a example (code snippet) that can be used with qt c++. My first opencascade project is on github. It's opensouce, non payed. Just for learing purposes.

https://github.com/grotius-cnc/Skynet_Robot_Control_Rtos_Ethercat

If i use this code :

     Handle(TDocStd_Document) doc;

     OSD::SetSignal(false); // should be called ones at application startup
     STEPCAFControl_Reader aReader;
     try
     {
       OCC_CATCH_SIGNALS // necessary for redirecting signals on Linux
       if (aReader.ReadFile ("robot/kuka_base.step") != IFSelect_RetDone) {  }
       if (!aReader.Transfer (doc)) {  }
     }
     catch (Standard_Failure const& theFailure)
     {
       std::cerr << "STEP import failed: " << theFailure.GetMessageString() << "\n";

     }

The error is :

*** Abort *** an exception was raised, but no catch was found.
	... The exception is:SIGSEGV 'segmentation violation' detected. Address 1a8
Press <RETURN> to close this window...


michelwijnja_165292's picture

This code :

     Handle(TDocStd_Document) doc;

     OSD::SetSignal(false); // should be called ones at application startup
     STEPCAFControl_Reader aReader;
     try
     {
       OCC_CATCH_SIGNALS // necessary for redirecting signals on Linux
       if (aReader.ReadFile ("robot/kuka_base.step") != IFSelect_RetDone) {  }
       if (!aReader.Transfer (doc)) {  }
     }
     catch (Standard_Failure const& theFailure)
     {
       std::cerr << "STEP import failed: " << theFailure.GetMessageString() << "\n";

     }

outputs an error :

*** Abort *** an exception was raised, but no catch was found.
	... The exception is:SIGSEGV 'segmentation violation' detected. Address 1a8
Press <RETURN> to close this window...


Is this an opencascade bug?

Manon Jubert's picture

Hi,

I also have the same problem.

There is a code which do not quit with the class STEPCAFControl_Reader :

STEPCAFControl_Reader caf_reader;
caf_reader.SetColorMode (true);
caf_reader.SetMatMode (true);
IFSelect_ReturnStatus error = caf_reader.ReadFile (i_filename.c_str());

STEPControl_Reader reader = caf_reader.Reader();

 But the color don't appear in the viewer. I don't know how to get the read color, and give it in the context.

Manon

michelwijnja_165292's picture

Hi Manon,

After today i can read colors from a step file. But they are different when displayed in freecad or mayo.

Orange in freecad is Grey in my preview. You can look at my latest code on github, channel grotius-cnc.

There is a repro called "Skynet Robot Control Rtos Ethercat", the latest pre-release file of today is a zip file with the code to load the stepfile. Take a look. This week i will extend the code.

I will look to read the stepfile myself, outside of the cascade technology. I think this is much quicker to get the correct information from the stepfile.

If you have questions, you can start a issue there.

Good luck with coding !!

michelwijnja_165292's picture

I finally get it working. After visiting a chinese website wich had a stepfile example with more debug output.

My problem was that the stepfile was a kind of corrupt. I had it downloaded and the colors seems oke in freecad. But that

was not the case. My advise is, always change the colors of the step file into something different, like red, or cyan, etc.

The color orange or yellow is used by opencascade when there are no colors defined in the shape, So chancing the colors if you want to test the stepfile import is quite important and can you safe hours.

Here is my code, tested ok for importing colors. In my case i only care about colors for the shapes at the moment.

The code snipped :

void C3DWidget::readStepXde(const std::string& theStepName)
{
    Handle(TDocStd_Document) aDoc;
    Handle(XCAFApp_Application) anApp = XCAFApp_Application::GetApplication();
    anApp->NewDocument("MDTV-XCAF", aDoc);

    STEPCAFControl_Reader aStepReader;
    aStepReader.SetColorMode(true);
    aStepReader.SetNameMode(true);
    aStepReader.SetLayerMode(true);
    aStepReader.SetPropsMode(true);

    aStepReader.ReadFile (theStepName.c_str());
    aStepReader.Transfer (aDoc);

    TDF_Label aRootLabel = aDoc->Main();

    aShapeTool = XCAFDoc_DocumentTool::ShapeTool(aRootLabel);
    aColorTool = XCAFDoc_DocumentTool::ColorTool(aRootLabel);

    visit(aRootLabel);
}

void C3DWidget::visit(const TDF_Label& theLabel)
{
    theLabel.EntryDump(std::cout);
    Quantity_Color col;

    Handle(TDataStd_Name) aName;
    if (theLabel.FindAttribute(TDataStd_Name::GetID(), aName))
    {
        std::cout << "  Name: " << aName->Get() << std::endl;
    }

    if (!aColorTool->IsSet(theLabel, XCAFDoc_ColorGen))
    {
        Quantity_Color aColor;
        aColorTool->GetColor(theLabel, aColor);

        std::cout << "  Color: " << Quantity_Color::StringName(aColor.Name()) << std::endl;
    }

    if (aShapeTool->IsShape(theLabel))
    {
        TopoDS_Shape aShape;
        //if a shape can be made from the current label, look for the color of that label:
        if(aShapeTool->GetShape(theLabel, aShape)){
            std::cout<<"made a shape"<<std::endl;

            aColorTool->GetColor(aShape,XCAFDoc_ColorSurf,col);
        }

        Handle(AIS_Shape) ais_Shape = new AIS_Shape(aShape);
        ais_Shape->SetColor(col);
        ais_Shape->SetDisplayMode(AIS_Shaded);
        ais_Shape->Attributes()->SetFaceBoundaryDraw(true);
        ais_Shape->Attributes()->SetFaceBoundaryAspect(
                    new Prs3d_LineAspect(Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1.));
        ais_Shape->Attributes()->SetIsoOnTriangulation(true);
        m_context->Display(ais_Shape,Standard_False);

    }

    for (TDF_ChildIterator c(theLabel); c.More(); c.Next())
    {
        visit(c.Value());
    }
}

I wanna say to the opencascade web administatrators.

Thanks for the help !  And why not post a snipped like this in the past decade?

Kirill Gavrilov's picture

You can use XCAFPrs_AISObject interactive object to display a shape label with colors.
Colors might be assigned in different way in XCAF document, and some could be lost in your code snippet (e.g. colors assigned to individual sub-faces).
Your iterator also tries to display recursively all children labels of shape type - I'm not sure this will not have side effects.
Normally, only children of XCAFDoc_ShapeTool::IsAssembly() labels should be explored deeper, not every label.

Check XCAFPrs_DocumentExplorer class and source code of XDisplay (XDEDRAW.cxx) command as typical examples of XCAF document explorer.

Manon Jubert's picture

Thanks Michel,

I've tried your solution but it doesn't work for all step model. Some of them are loaded two times. One time with right color and a second time just with the yellow color.

I will try with XCAFPrs_AISObject maybe it will work fine.