4

In order to read all shapes within my step file, I have succeed to extract all shapes using STEPControl_reader. Now I want to find a way (OCAF/XDE ?) to extract a tree view containing which shape is contained by another one.

Could anyone give some pointers to examples extracting those informations in step file using OCAF or XDE. I have a difficulty to understand the official document because it has a lack of examples.

EDIT: By using the Mayo project: You can use the XdeDocumentItem::XdeDocumentItem(...) constructor and recursively creating the nodes.

XdeDocumentItem::XdeDocumentItem(const Handle_TDocStd_Document &doc)
: m_cafDoc(doc),
  m_shapeTool(XCAFDoc_DocumentTool::ShapeTool(doc->Main())),
  m_colorTool(XCAFDoc_DocumentTool::ColorTool(doc->Main())){
this->rebuildAssemblyTree();}

The method rebuildAssemblyTree is like that:

for (const TDF_Label& rootLabel : this->topLevelFreeShapes())
    this->deepBuildAssemblyTree(0, rootLabel);
Valimo Ral
  • 381
  • 2
  • 15
  • I found an example here to extract the tree information from Mayo project [Mayo](https://github.com/fougue/mayo) I also noticed that this code cannot extract the product name like FreeCAD does. – Valimo Ral Dec 11 '18 at 09:52
  • Do you have any update on this topic? I am also interested in generating the tree view of a step file. Thank you. – Bub Espinja Feb 18 '19 at 09:10

1 Answers1

0

You may use class XCAFPrs_DocumentExplorer for stack-alike traverse within OCCT 7.4.0+, and the following code snippet (based on XDisplay Draw Harness command) for traverse with recursive function calls:

  //! Handle document root shapes.
  int traverseDocument (const Handle(TDocStd_Document)& theDoc)
  {
    TDF_LabelSequence aLabels;
    XCAFDoc_DocumentTool::ShapeTool (theDoc->Main())->GetFreeShapes (aLabels);
    for (TDF_LabelSequence::Iterator aLabIter (myLabels); aLabIter.More(); aLabIter.Next())
    {
      const TDF_Label& aLabel = aLabIter.Value();
      if (traverseLabel (aLabel, "", TopLoc_Location()) == 1)
      {
        return 1;
      }
    }
    return 0;
  }

  //! Handle single label.
  int traverseLabel (const TDF_Label& theLabel,
                     const TCollection_AsciiString& theNamePrefix,
                     const TopLoc_Location& theLoc)
  {
    TCollection_AsciiString aName;
    {
      Handle(TDataStd_Name) aNodeName;
      if (theLabel.FindAttribute (TDataStd_Name::GetID(), aNodeName))
      {
        aName = aNodeName->Get(); // instance name
      }
      if (aName.IsEmpty())
      {
        TDF_Label aRefLabel;
        if (XCAFDoc_ShapeTool::GetReferredShape (theLabel, aRefLabel)
         && aRefLabel.FindAttribute (TDataStd_Name::GetID(), aNodeName))
        {
          aName = aNodeName->Get(); // product name
        }
      }
    }
    aName = theNamePrefix + aName;

    TDF_Label aRefLabel = theLabel;
    XCAFDoc_ShapeTool::GetReferredShape (theLabel, aRefLabel);
    if (XCAFDoc_ShapeTool::IsAssembly (aRefLabel))
    {
      aName += "/";
      const TopLoc_Location aLoc = theLoc * XCAFDoc_ShapeTool::GetLocation (theLabel);
      for (TDF_ChildIterator aChildIter (aRefLabel); aChildIter.More(); aChildIter.Next())
      {
        if (traverseLabel (aChildIter.Value(), aName, aLoc) == 1)
        {
          return 1;
        }
      }
      return 0;
    }
    std::cout << aName << " ";
    return 0;
  }
gkv311
  • 2,612
  • 1
  • 10
  • 11