XDE: Build Hierarchical assembly structure

hello,

i have created several shapes and added them to my document with

Handle(XCAFDoc_ShapeTool) myAssembly = XCAFDoc_DocumentTool::ShapeTool(m_hDoc->Main());
TDF_Label lblNewPart = myAssembly->NewShape();
myAssembly->AddComponent(lblNewPart, lblShape, TopLoc_Location(transf));

Now I want to group the created Labels in a hierarchical structure. I tried to do this with

myAssembly->AddComponent(lblParent, lblNewPart, TopLoc_Location());
myAssembly->UpdateAssembly(lblParent);

but somehow this doesn't work, if lblParent is already part of another assembly. Is there another way to build a hierarchical assembly structure in xde?

Regards

Göran

Pawel's picture

Hello Göran,

if you add bottom-up then you don't have to update assemblies. Maybe this code snippet will help...

TopoDS_Shape s1 = BRepPrimAPI_MakeBox(gp_Pnt(0, 0, 0), gp_Pnt(10, 30, 20));
TopoDS_Shape s2 = BRepPrimAPI_MakeBox(gp_Pnt(50, 50, 50), gp_Pnt(10, 20, 20));
TopoDS_Shape s3 = BRepPrimAPI_MakeBox(gp_Pnt(0, 0, 0), gp_Pnt(-10, -20, -20));
TopoDS_Shape s4 = BRepBuilderAPI_MakeEdge(gp_Lin(gp_Pnt(0,0,100), gp_Dir(1, 0, 0)), gp_Pnt(-100, 0, 100), gp_Pnt(-50,0,100));

//============
gp_Trsf t0/*identity*/, t1, t2, t3;
t1.SetTranslation(gp_Vec(-25, 0, 0));
t2.SetTranslation(gp_Vec( 25, 0, 0));
t3.SetTranslation(gp_Vec( 25, 50, 0));

TopLoc_Location location0(t0);
TopLoc_Location location1(t1);
TopLoc_Location location2(t2);
TopLoc_Location location3(t3);

//===========
TDF_Label lab1 = XCAFDoc_DocumentTool::ShapeTool (aDoc->Main ())->NewShape();
XCAFDoc_DocumentTool::ShapeTool (aDoc->Main ())->SetShape(lab1, s1);
TDataStd_Name::Set(lab1, "Box1");

TDF_Label lab2 = XCAFDoc_DocumentTool::ShapeTool (aDoc->Main ())->NewShape();
XCAFDoc_DocumentTool::ShapeTool (aDoc->Main ())->SetShape(lab2, s2);
TDataStd_Name::Set(lab2, "Box2");

TDF_Label lab3 = XCAFDoc_DocumentTool::ShapeTool (aDoc->Main ())->NewShape();
XCAFDoc_DocumentTool::ShapeTool (aDoc->Main ())->SetShape(lab3, s3);
TDataStd_Name::Set(lab3, "Box3");

TDF_Label lab4 = XCAFDoc_DocumentTool::ShapeTool (aDoc->Main ())->NewShape();
XCAFDoc_DocumentTool::ShapeTool (aDoc->Main ())->SetShape(lab4, s4);
TDataStd_Name::Set(lab4, "Edge4");

TDF_Label labelA02 = XCAFDoc_DocumentTool::ShapeTool (aDoc->Main ())->NewShape();
TDataStd_Name::Set(labelA02, "ASSEMBLY02");

TDF_Label labelA01 = XCAFDoc_DocumentTool::ShapeTool (aDoc->Main ())->NewShape();
TDataStd_Name::Set(labelA01, "ASSEMBLY01");

TDF_Label labelA0 = XCAFDoc_DocumentTool::ShapeTool (aDoc->Main ())->NewShape();
TDataStd_Name::Set(labelA0, "ASSEMBLY");

TDF_Label component03 = XCAFDoc_DocumentTool::ShapeTool (aDoc->Main ())->AddComponent(labelA0, lab2, location1);
TDF_Label component04 = XCAFDoc_DocumentTool::ShapeTool (aDoc->Main ())->AddComponent(labelA0, lab2, location2);
TDF_Label component05 = XCAFDoc_DocumentTool::ShapeTool (aDoc->Main ())->AddComponent(labelA0, lab4, location0);

XCAFDoc_DocumentTool::ShapeTool (labelA02)->AddComponent(labelA02, lab3, location1);
XCAFDoc_DocumentTool::ShapeTool (labelA02)->AddComponent(labelA02, lab3, location2);
XCAFDoc_DocumentTool::ShapeTool (labelA02)->AddComponent(labelA02, lab1, location2);
TDF_Label componentA02 = XCAFDoc_DocumentTool::ShapeTool (labelA01)->AddComponent(labelA01, labelA02, location1);
//
TDF_Label component11 = XCAFDoc_DocumentTool::ShapeTool (labelA01)->AddComponent(labelA01, lab1, location2);
XCAFDoc_DocumentTool::ShapeTool (labelA0)->AddComponent(labelA0, labelA01, location2);

Quantity_Color blue(0,0,1, Quantity_TOC_RGB);
Quantity_Color green(0,1,0, Quantity_TOC_RGB);

XCAFDoc_DocumentTool::ColorTool (labelA0)->AddColor(blue);
XCAFDoc_DocumentTool::ColorTool (labelA0)->AddColor(green);
XCAFDoc_DocumentTool::ColorTool (labelA0)->SetColor(component05, blue, XCAFDoc_ColorGen);
XCAFDoc_DocumentTool::ColorTool (labelA0)->SetColor(component03, green, XCAFDoc_ColorGen);
XCAFDoc_DocumentTool::ColorTool (labelA0)->SetColor(component04, blue, XCAFDoc_ColorGen);

Pawel

Göran Barz's picture

Thank you for your help, I corrected my code now. The problem was that I built the assembly structure top-down and open cascade seemed to have a problem if an assembly was modified after adding it to another assembly.

Pawel's picture

AFAIR building the assembly top-down will also work as long as you do the updating (on each assembly level)

Pawel