Read STEP shapes with names -- assemblies are not compounds?

Read STEP shapes with names -- assemblies are not compounds?

I give the complete code below, where

readOceStep(const std::string& f)

reads the step file into a document, then

analyzeLabel(TDF_Label& lbl)

analyzes the labels recursively. I am applying this to the
standard step file, as1-oc-214.stp.

My problem is that it does down only one level. I.e., it
gets the children of the top level shape (which is an assembly),
but not the children of the children.

Strangely (to me), the next level shapes are compounds but not
assemblies. Why is that?

This seems to correspond with the fact that they have subshapes
(which I know to be solids), but they do not have children. I.e.,
the iterator,

TopoDS_Iterator anIt(shape);

sees multiple subshapes, but neither

TDF_ChildIterator childIter = TDF_ChildIterator(lbl);

nor

XCAFDoc_ShapeTool::GetSubShapes(lbl, subLabels);

find any children or subshapes.

Can someone tell me what I did wrong? Did I not transfer enough
document information? Something else?

Thanks......JRC

// std includes
#include

// OCC includes
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

/**
* Analyze a label, printing out its label, name, number of subshapes
*
* @param lbl the label to be analyzed
*/
void analyzeLabel(TDF_Label& lbl) {

// Get name
std::cout std::cout Handle(TDataStd_Name) nm;
Handle_TDF_Attribute att;
if (lbl.FindAttribute(TDataStd_Name::GetID(), att)) {
nm = Handle(TDataStd_Name)::DownCast(att);
std::cout Get() }

// Get shape
TopoDS_Shape shape;
XCAFDoc_ShapeTool::GetShape(lbl, shape);

// Determine whether an assembly
if (XCAFDoc_ShapeTool::IsAssembly(lbl)) {
std::cout }
else if (XCAFDoc_ShapeTool::IsCompound(lbl)) {
std::cout }
// If not, see if it has a color
else {
std::cout // Get the color
Handle_XCAFDoc_ColorTool colorTool = XCAFDoc_ColorTool::Set(lbl);
if (colorTool->IsSet(shape, XCAFDoc_ColorGen)) {
std::cout Quantity_Color aColor;
colorTool->GetColor(shape, XCAFDoc_ColorGen, aColor);
}
else if (colorTool->IsSet(shape, XCAFDoc_ColorSurf)) {
std::cout Quantity_Color aColor;
colorTool->GetColor(shape, XCAFDoc_ColorSurf, aColor);
}
else if (colorTool->IsSet(shape, XCAFDoc_ColorCurv)) {
std::cout Quantity_Color aColor;
colorTool->GetColor(shape, XCAFDoc_ColorCurv, aColor);
}
else {
std::cout }
}

// Write shape type
switch (shape.ShapeType()) {
case TopAbs_COMPOUND: {
std::cerr break;
}
case TopAbs_COMPSOLID: {
std::cerr break;
}
case TopAbs_SOLID: {
std::cerr break;
}
default: {
std::cerr break;
}
}

// Count subshapes
TopoDS_Iterator anIt(shape);
int ns = 0;
for (; anIt.More(); anIt.Next()) {
++ns;
}
std::cout

// Count children
TDF_ChildIterator childIter = TDF_ChildIterator(lbl);
int nc = 0;
for (; childIter.More(); childIter.Next()) {
++nc;
}
std::cout

// Get subshapes
TDF_LabelSequence subLabels;
XCAFDoc_ShapeTool::GetSubShapes(lbl, subLabels);
unsigned int nsubs = subLabels.Length();
std::cout

// Analyze children
childIter.Initialize(lbl);
for (; childIter.More(); childIter.Next()) {
TDF_Label childLabel = childIter.Value();
analyzeLabel(childLabel);
}

}

/**
* Use OpenCascade to read a step file
*
* http://opencascade.blogspot.fr/2008/12/adding-colors-and-names-to-your_02.html
* "Look at source code of STEPCAFControl ... to copy XDE behavior."
*/
void readOceStep(const std::string& f) {

// Set up the reader and the document
STEPCAFControl_Reader reader;
reader.ReadFile((Standard_CString) f.c_str());
Handle(TDocStd_Document) doc;
Handle(XCAFApp_Application) anApp = XCAFApp_Application::GetApplication();
anApp->NewDocument("MDTV-XCAF", doc);

// Transfer file to a document
if (! reader.Transfer(doc)) {
std::cout return;
}
std::cout

// Query document for shapes and associate colors
TDF_Label mainLabel = doc->Main();
std::cout Handle(XCAFDoc_ShapeTool) rootNode = XCAFDoc_DocumentTool::ShapeTool(
doc->Main());
TDF_LabelSequence labels;
rootNode->GetFreeShapes(labels); // root assemblies and components
unsigned int nRoots = labels.Length();
std::cout for (unsigned int i = 1; i TDF_Label label = labels.Value(i);
analyzeLabel(label);
}

}

Super Dan's picture

Hi, John,I have the same problem with you,and I download FreeCAD from http://free-cad.sourceforge.net,which declare the ability of reading Step files.I try to import an 3-level assembly,like :
-----assembly
--------component1
---------------part1_1
---------------part2_1
...
---------------part2_n

--------component2
---------------part2_1
---------------part2_2
...
---------------part2_n

--------component3
...

the import result is like :
-----Unnamed
----------part1_1
----------part1_2
...
----------part1_n
----------part2_1
----------part3_2
...
----------part2_n

Have you solve this problem by now?

Viral R's picture

In your function readOceStep()

try using GetShapes instead of using GetFreeShapes

rootNode->GetShapes(labels);