For all issues regarding the Forums use, please, refer to the Forum Rules.

Our Solutions

Need professional assistance?
Consider our:

Support Offerings

 

Need to speed up your development?
Have a look at our:

Samples & Tools

 

Need some functionality extending standard OCCT capabilities?
Check out our:

Adv. Components

Related pages

OpenCascade returns repeated names from STEP

Leonardo Ghizoni's picture
Forums: 

Hello,

I'm writing a code to read the names of all the solids in a STEP model and export them to stl separate files. So far I succeeded, but there's a strange behavior on the outputs: some of the solids return the same name. I've of course checked the STEP file, and the names do not repeat.

Here's my complete code:

#include <iostream>
#include <vector>
#include <string>
#include <sys/types.h>
#include <dirent.h>
#include <map>

#include <STEPCAFControl_Reader.hxx>
#include <StepRepr_Representation.hxx>
#include <XCAFApp_Application.hxx>
#include <XCAFDoc_DocumentTool.hxx>
#include <XCAFDoc_ShapeTool.hxx>
#include <XCAFDoc_ColorTool.hxx>
#include <Quantity_Color.hxx>
#include <Resource_Unicode.hxx>
#include <TDF_ChildIterator.hxx>
#include <TDocStd_Document.hxx>
#include <TDF_LabelSequence.hxx>
#include <TDataStd_Name.hxx>
#include <BRepMesh_IncrementalMesh.hxx>
#include <BRepBuilderAPI_Transform.hxx>
#include <Interface_Static.hxx>
#include <StlAPI_Writer.hxx>

using namespace std;

int main()
{
    int mkdir;
    string Dir = ".../Directory/";
    string FileName = "Module.step";
    string inFile   = Dir+FileName;//+stepFile;
    string command = "mkdir "+Dir+"STL";
    const char *Command = command.c_str();
    mkdir = system(Command);

    // Create Document
    Handle(TDocStd_Document) aDoc;
    Handle(XCAFApp_Application) anApp = XCAFApp_Application::GetApplication();
    anApp->NewDocument(FileName.c_str(),aDoc);

    // Create additional log file
    STEPCAFControl_Reader aReader;
    aReader.ReadFile(inFile.c_str());
    aReader.Transfer(aDoc);

    if(!aReader.Transfer(aDoc)){
        cout<<"ERROR: Could NOT read the STEP file."<<endl;
    }

    aReader.SetNameMode(true);
    aReader.SetColorMode(true);
    aReader.SetMatMode(true);

    Handle(XCAFDoc_ShapeTool) l_ShapeTool = XCAFDoc_DocumentTool::ShapeTool(aDoc->Main());
    Handle(XCAFDoc_ColorTool) l_ColorTool = XCAFDoc_DocumentTool::ColorTool(aDoc->Main());

    // Shape Extraction & Conversion

    TDF_LabelSequence FreeShapes;
    l_ShapeTool->GetShapes(FreeShapes);
    TDF_LabelSequence shapes;
    l_ShapeTool->GetComponents(FreeShapes.Value(1),shapes,true);

    TCollection_ExtendedString extstr_shapes[shapes.Length()];
    string shapes_names[shapes.Length()];

    vector<float> RedVector;
    vector<float> GreenVector;
    vector<float> BlueVector;

    for(Standard_Integer i=1; i<=shapes.Length();i++){

        TDF_Label label = shapes.Value(i);

        TopoDS_Shape shape;
        l_ShapeTool->GetShape(label,shape);

        // Color Try-Out Declaration
        //TDF_Label l_ColorLabel;
        //Quantity_Color aColor;

        TopAbs_ShapeEnum type = shape.ShapeType();
        if(type == TopAbs_SOLID){

            //BRepBuilderAPI_Transform xform(shape,transformation,Standard_True);
            //shape = xform.Shape();
            Handle(TDataStd_Name) name;
            if(label.FindAttribute(TDataStd_Name::GetID(),name)){
                //TCollection_ExtendedString extstr_shapes = name->Get();
                extstr_shapes[i] = name->Get();
                shapes_names[i] = TCollection_AsciiString(extstr_shapes[i]).ToCString();
                //string shapes_names = TCollection_AsciiString(extstr_shapes).ToCString();
                //cout<<extstr_shapes[i]<<endl;
                //cout<<shapes_names[i]<<endl;
            }

            // Shape Meshing and conversion to STL

            BRepMesh_IncrementalMesh BMesh(shape,0.1,Standard_True);
            StlAPI_Writer STLwriter;
            string Filename;
            //Filename = Dir+"STL/("+to_string(i)+") "+shapes_names[i]+".stl";
            Filename = Dir+"STL/"+shapes_names[i]+".stl";
            STLwriter.Write(shape,Filename.c_str());
        }
    }

    cout<<"Shape         Color"<<endl;
    for(Standard_Integer i=0; i < shapes.Length(); i++){
        cout<<shapes_names[i]<<endl;
    }

    return 0;
}

Here's my output:

M2x10_TX6_6

M2x10_TX6_13

castor-v020

M2x10_TX6_14

M2x10_TX6_7

castor-v020

castor-v020

M2x10_TX6_11

M2x10_TX6_2

M2x10_TX6_12

M2x10_TX6

castor-v020

castor-v020

castor-v020

castor-v020

castor-v020

M2x10_TX6_4

M2x10_TX6_8

castor-v020

castor-v020

Sshield_top

castor-v020

M2x10_TX6_10

castor-v020

castor-v020

M2x10_TX6_9

Sshield_bottom

M2x10_TX6_1

M2x10_TX6_5

castor-v020

castor-v020

M2x10_TX6_3

The "castor-v**" is actually changing in the STEP file, as you can see in the figure attached.

I would really appreciate help. I simply cannot find an error that results in repeated names :(

Thanks in advance.

Attachments: 
Kirill Gavrilov's picture

Could you attach a STEP file?

Leonardo Ghizoni's picture

The step file exceeds the limit of 10MB, so here it is a shareable link: https://drive.google.com/open?id=13M0hRh4J82zrJeWiquOdv-uRLZ-ZjAr3

It's the same file of the results I posted before. Please let me know if you can access it, and thanks again!

Cheers,

/Leonardo.

Leonardo Ghizoni's picture

I have already tried replying, but trying again.

Here's the link for the STEP file: https://drive.google.com/open?id=13M0hRh4J82zrJeWiquOdv-uRLZ-ZjAr3

Thanks a lot for the help.

/Leonardo,

Kirill Gavrilov's picture

The "castor-v**" is actually changing in the STEP file, as you can see in the figure attached. 

No, they don't - I see no "castor-v023" entities in CAD Assistant nor any such string in STEP file itself.
Look like a different STEP model or issues with STEP export.

Kenji Hara's picture

Leonardo said "I've of course checked the STEP file,", but I think he did it on FreeCAD.

The original STEP file provided by Leonardo actually contains duplicated names ​'​castor-v020'.

But, when I open the file in FreeCAD, the duplicated solid names will be renumbered automatically because FreeCAD disallow to set same name for two or more shapes. I guess that would be the root cause.

Attachments: 

Contact us

Attention: DO NOT use this form for all kind of request for the removal of your personal data. Please follow this link for such cases.