Regroup shapes under one parent assembly

Lingkan

CAD practitioner
Hello, I'm reading multiple shapes using STEPCAFControl_Reader API into the document which results in several shapes being created under root.

Main()
|- shape_1
|- shape_2
|- shape_...

Now I want to regroup these shapes under one parent assembly knot:

Main()
|- MyParentAssembly
|- shape_1
|- shape_2
|- shape_...

For now i tried to collect the shapes into a compound which sort of works, but seems to copy the shapes into a new assmbly instead of regrouping them. Also properties like color and name aren't preserved using this approach (see attached pictures).

1. Read Shapes into document (applying transformation and color)

C++:
bool App::LoadSTEPWithTransformAndColor(
    const std::string& filename, const gp_Trsf& transformation,
    const std::string& colorStr) {
  // Record the state before transfer
  std::set<std::string, std::less<>> existingShapes;
  TDF_LabelSequence allShapesBefore;
  Handle(XCAFDoc_ShapeTool) ST = m_doc->GetShapeTool();
  ST->GetFreeShapes(allShapesBefore);
  for (Standard_Integer i = 1; i <= allShapesBefore.Length(); i++) {
    TDF_Label label = allShapesBefore.Value(i);
    TCollection_AsciiString entry;
    TDF_Tool::Entry(label, entry);
    existingShapes.emplace(entry.ToCString());
  }

  // Perform the transfer
  if (!m_doc->LoadSTEP(filename)) {
    return false;
  }

  // Identify new shapes
  TDF_LabelSequence allShapesAfter;
  ST->GetFreeShapes(allShapesAfter);
  for (Standard_Integer i = 1; i <= allShapesAfter.Length(); i++) {
    TDF_Label label = allShapesAfter.Value(i);
    TCollection_AsciiString entry;
    TDF_Tool::Entry(label, entry);
    if (existingShapes.find(entry.ToCString()) == existingShapes.end()) {
      // This is a new shape
      TopoDS_Shape shape = XCAFDoc_ShapeTool::GetShape(label);
      // ... apply transformation ...
      BRepBuilderAPI_Transform brepTrsf(shape, transformation);
      TopoDS_Shape transformedShape = brepTrsf.Shape();
      // ... add color ...
      if (!colorStr.empty()) {
        Quantity_ColorRGBA color;
        Quantity_ColorRGBA::ColorFromHex(colorStr.c_str(), color);
        m_doc->SetColor(label, color, true);
      }
      // ... and add it to the document
      ST->SetShape(label, transformedShape);
    }
  }

  return true;
}

2. Try regroup

C++:
Handle(XCAFDoc_ShapeTool) ST = m_doc->GetShapeTool();
TDF_LabelSequence labels;
ST->GetFreeShapes(labels);

TopoDS_Compound C;
BRep_Builder B;
B.MakeCompound(C);
for (int i = 1; i <= labels.Length(); ++i) {
    TopoDS_Shape const& S = XCAFDoc_ShapeTool::GetShape(labels(i));
    B.Add(C, S);
}
ST->AddShape(C, true);

I appreciate your help!
 

Attachments

  • 2.PNG
    2.PNG
    160.5 KB · Views: 3
  • 1.PNG
    1.PNG
    157.7 KB · Views: 3

Lingkan

CAD practitioner
I resolved this issue by not setting the shape in the `LoadSTEPWithTransformAndColor()` and instead collecting all the shapes and add them alltogether afterwards.
 
Top