void GetAllShapes(const TopoDS_Shape& shape, std::vector& compounds, std::vector& solids) { if (shape.ShapeType() == TopAbs_COMPOUND) { compounds.push_back(shape); TopoDS_Iterator it; for (it.Initialize(shape); it.More(); it.Next()) { TopoDS_Shape Sub0 = it.Value(); GetAllShapes(Sub0, compounds, solids); } } else if (shape.ShapeType() == TopAbs_SOLID) { solids.push_back(shape); } } void GetAllShapes(Handle(TDocStd_Document) doc, std::shared_ptr stepCtrlReader, std::vector& compounds, std::vector& solids) { int num = stepCtrlReader->Reader().NbShapes(); for (int i = 1; i <= num; i++) { TopoDS_Shape shape = stepCtrlReader->Reader().Shape(i); GetAllShapes(shape, compounds, solids); } } void MapShapesToEntities(Handle(TDocStd_Document) doc, std::shared_ptr stepCtrlReader) { std::unordered_map shapeNameMap; std::vector compounds; std::vector solids; GetAllShapes(doc, stepCtrlReader, compounds, solids); const Handle(Transfer_TransientProcess)& TP = stepCtrlReader->Reader().WS()->TransferReader()->TransientProcess(); Handle(StepData_StepModel) aStepModel = Handle(StepData_StepModel)::DownCast(TP->Model()); const Interface_Graph& Graph = stepCtrlReader->Reader().WS()->Graph(); const Handle(XCAFDoc_ShapeTool) shapeTool = XCAFDoc_DocumentTool::ShapeTool(doc->Main()); const XCAFDoc_DataMapOfShapeLabel& shapeMap = stepCtrlReader->GetShapeLabelMap(); std::vector labelNames; // Iterate over all the entities in the model Interface_EntityIterator entityIter(aStepModel->Entities()); for (entityIter.Start(); entityIter.More(); entityIter.Next()) { Handle(TCollection_HAsciiString) name; const Handle(Standard_Transient) entity = entityIter.Value(); if (entity->IsKind(STANDARD_TYPE(StepBasic_ProductDefinition))) { Handle(StepBasic_ProductDefinition) aPDef = Handle(StepBasic_ProductDefinition)::DownCast(entity); Handle(StepBasic_Product) Prod = (!aPDef->Formation().IsNull() ? aPDef->Formation()->OfProduct() : NULL); if (Prod.IsNull()) name = new TCollection_HAsciiString; else if (!Prod->Name().IsNull() && Prod->Name()->UsefullLength() > 0) name = Prod->Name(); else if (!Prod->Id().IsNull()) name = Prod->Id(); else name = new TCollection_HAsciiString; Standard_Integer index = TP->MapIndex(aPDef); if (index > 0) { TopoDS_Shape theShape; Handle(Transfer_Binder) binder = TP->MapItem(index); TopoDS_Shape S = TransferBRep::ShapeResult(binder); // find whether this is compund or solid TopAbs_ShapeEnum type = S.ShapeType(); if (type == TopAbs_COMPOUND) { // verify that it is in the compounds vector auto iter = std::find_if(compounds.begin(), compounds.end(), [&S](const TopoDS_Shape& shape) { return shape.IsSame(S); }); if (iter != compounds.end()) { // Handle compound theShape = S; } } else if (type == TopAbs_SOLID) { // Handle solid auto iter = std::find_if(solids.begin(), solids.end(), [&S](const TopoDS_Shape& shape) { return shape.IsSame(S); }); if (iter != solids.end()) { // Handle solid theShape = S; } } Handle(StepShape_ShapeDefinitionRepresentation) aSDR; Interface_EntityIterator entIt = Graph.TypedSharings(aPDef, STANDARD_TYPE(StepShape_ShapeDefinitionRepresentation)); for (entIt.Start(); entIt.More(); entIt.Next()) { const Handle(Standard_Transient)& aReferer = entIt.Value(); aSDR = Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(aReferer); if (!aSDR.IsNull()) break; } if (aSDR.IsNull()) continue; // Access shape representation Handle(StepShape_ShapeRepresentation) aShapeRepr = Handle(StepShape_ShapeRepresentation)::DownCast(aSDR->UsedRepresentation()); if (aShapeRepr.IsNull()) continue; // Access representation items NCollection_Sequence aReprItems; collectRepresentationItems(Graph, aShapeRepr, aReprItems); if (aReprItems.Length() == 0) continue; StepRepr_SequenceOfRepresentationItem aMSBSeq; StepRepr_SequenceOfRepresentationItem aSBSMSeq; // Iterate over the top level representation items collecting the // topological containers to expand for (Standard_Integer i = 1; i <= aReprItems.Length(); ++i) { const Handle(StepRepr_RepresentationItem)& aTRepr = aReprItems.Value(i); if (aTRepr->IsKind(STANDARD_TYPE(StepShape_ManifoldSolidBrep))) aMSBSeq.Append(aTRepr); else if (aTRepr->IsKind(STANDARD_TYPE(StepShape_ShellBasedSurfaceModel))) aSBSMSeq.Append(aTRepr); } // Expand Manifold Solid BReps for (Standard_Integer i = 1; i <= aMSBSeq.Length(); ++i) { const Handle(StepRepr_RepresentationItem)& Repr = aMSBSeq.Value(i); Handle(StepShape_ManifoldSolidBrep) aMSB = Handle(StepShape_ManifoldSolidBrep)::DownCast(Repr); if (aMSB.IsNull()) continue; // Access the name Handle(TCollection_HAsciiString) aName = aMSB->Name(); shapeNameMap[aName->ToCString()] = theShape; } } } } }