1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Model_ResultBody.cpp
4 // Created: 08 Jul 2014
5 // Author: Mikhail PONIKAROV
7 #include <Model_BodyBuilder.h>
9 #include <Model_Data.h>
10 #include <Model_Document.h>
11 #include <TNaming_Builder.hxx>
12 #include <TNaming_NamedShape.hxx>
13 #include <TNaming_Iterator.hxx>
14 #include <TNaming_Tool.hxx>
15 #include <TDataStd_Name.hxx>
16 #include <TDataStd_Integer.hxx>
18 #include <TopoDS_Face.hxx>
19 #include <TDF_ChildIterator.hxx>
20 #include <TDF_Reference.hxx>
21 #include <TopTools_MapOfShape.hxx>
22 #include <TopExp_Explorer.hxx>
23 #include <TopTools_ListOfShape.hxx>
24 #include <TopTools_ListIteratorOfListOfShape.hxx>
25 #include <TopTools_DataMapOfShapeListOfShape.hxx>
26 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
27 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
28 #include <TopTools_MapIteratorOfMapOfShape.hxx>
29 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
30 #include <TopTools_IndexedMapOfShape.hxx>
31 #include <TopTools_DataMapOfShapeShape.hxx>
33 #include <BRepTools.hxx>
34 #include <BRep_Tool.hxx>
35 #include <GeomAPI_Shape.h>
36 #include <GeomAlgoAPI_MakeShape.h>
37 #include <Config_PropManager.h>
39 //#include <TCollection_AsciiString.hxx>
40 //#include <TDF_Tool.hxx>
41 //#define DEB_IMPORT 1
43 Model_BodyBuilder::Model_BodyBuilder(ModelAPI_Object* theOwner)
44 : ModelAPI_BodyBuilder(theOwner)
48 // Converts evolution of naming shape to selection evelution and back to avoid
49 // naming support on the disabled results. Deeply in the labels tree, recursively.
50 static void evolutionToSelectionRec(TDF_Label theLab, const bool theFlag) {
51 std::list<std::pair<TopoDS_Shape, TopoDS_Shape> > aShapePairs; // to store old and new shapes
52 Handle(TNaming_NamedShape) aName;
54 if (theLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
55 TNaming_Evolution aNSEvol = aName->Evolution();
56 if ((aNSEvol == TNaming_SELECTED && theFlag) ||
57 (aNSEvol != TNaming_SELECTED && !theFlag)) { // nothing to do, it is already correct
60 anEvolution = (int)(aNSEvol);
62 Handle(TDataStd_Integer) anAttrEvol;
63 if (theLab.FindAttribute(TDataStd_Integer::GetID(), anAttrEvol)) {
64 anEvolution = anAttrEvol->Get();
67 TDataStd_Integer::Set(theLab, anEvolution);
70 for(TNaming_Iterator anIter(aName); anIter.More(); anIter.Next()) {
71 aShapePairs.push_back(std::pair<TopoDS_Shape, TopoDS_Shape>
72 (anIter.OldShape(), anIter.NewShape()));
76 TNaming_Builder aBuilder(theLab);
77 TNaming_Evolution anEvol = (TNaming_Evolution)(anEvolution);
78 std::list<std::pair<TopoDS_Shape, TopoDS_Shape> >::iterator aPairsIter = aShapePairs.begin();
79 for(; aPairsIter != aShapePairs.end(); aPairsIter++) {
80 if (theFlag) { // disabled => make selection
81 aBuilder.Select(aPairsIter->second, aPairsIter->first);
82 } else if (anEvol == TNaming_GENERATED) {
83 aBuilder.Generated(aPairsIter->first, aPairsIter->second);
84 } else if (anEvol == TNaming_MODIFY) {
85 aBuilder.Modify(aPairsIter->first, aPairsIter->second);
86 } else if (anEvol == TNaming_DELETE) {
87 aBuilder.Delete(aPairsIter->first);
88 } else if (anEvol == TNaming_PRIMITIVE) {
89 aBuilder.Generated(aPairsIter->second);
90 } else if (anEvol == TNaming_SELECTED) {
91 aBuilder.Select(aPairsIter->second, aPairsIter->first);
94 // recursive call for all sub-labels
95 TDF_ChildIterator anIter(theLab, Standard_False);
96 for(; anIter.More(); anIter.Next()) {
97 evolutionToSelectionRec(anIter.Value(), theFlag);
101 void Model_BodyBuilder::evolutionToSelection(const bool theFlag)
103 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
104 if (!aData) // unknown case
106 TDF_Label& aShapeLab = aData->shapeLab();
107 evolutionToSelectionRec(aShapeLab, theFlag);
110 void Model_BodyBuilder::store(const std::shared_ptr<GeomAPI_Shape>& theShape,
111 const bool theIsStoreSameShapes)
113 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
115 TDF_Label& aShapeLab = aData->shapeLab();
118 // store the new shape as primitive
119 TNaming_Builder aBuilder(aShapeLab);
122 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
124 return; // null shape inside
126 if(!theIsStoreSameShapes) {
127 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aShape, aShapeLab);
128 if(!aNS.IsNull() && !aNS->IsEmpty()) {
129 // This shape is already in document, store reference instead of shape;
130 const TDF_Label aFoundLabel = aNS->Label();
131 TDF_Reference::Set(aShapeLab, aFoundLabel);
132 aShapeLab.ForgetAttribute(TNaming_NamedShape::GetID());
137 aBuilder.Generated(aShape);
139 aShapeLab.ForgetAttribute(TDF_Reference::GetID());
140 if(!aBuilder.NamedShape()->IsEmpty()) {
141 Handle(TDataStd_Name) anAttr;
142 if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
143 std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
145 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
146 aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
153 void Model_BodyBuilder::storeGenerated(const std::shared_ptr<GeomAPI_Shape>& theFromShape,
154 const std::shared_ptr<GeomAPI_Shape>& theToShape)
156 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
158 TDF_Label& aShapeLab = aData->shapeLab();
161 // store the new shape as primitive
162 TNaming_Builder aBuilder(aShapeLab);
163 if (!theFromShape || !theToShape)
165 TopoDS_Shape aShapeBasis = theFromShape->impl<TopoDS_Shape>();
166 if (aShapeBasis.IsNull())
167 return; // null shape inside
168 TopoDS_Shape aShapeNew = theToShape->impl<TopoDS_Shape>();
169 if (aShapeNew.IsNull())
170 return; // null shape inside
171 aBuilder.Generated(aShapeBasis, aShapeNew);
173 if(!aBuilder.NamedShape()->IsEmpty()) {
174 Handle(TDataStd_Name) anAttr;
175 if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
176 std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
178 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
179 aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
186 void Model_BodyBuilder::storeModified(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
187 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const int theDecomposeSolidsTag)
189 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
191 TDF_Label& aShapeLab = aData->shapeLab();
194 // store the new shape as primitive
195 TNaming_Builder aBuilder(aShapeLab);
196 if (!theOldShape || !theNewShape)
198 TopoDS_Shape aShapeOld = theOldShape->impl<TopoDS_Shape>();
199 if (aShapeOld.IsNull())
200 return; // null shape inside
201 TopoDS_Shape aShapeNew = theNewShape->impl<TopoDS_Shape>();
202 if (aShapeNew.IsNull())
203 return; // null shape inside
204 aBuilder.Modify(aShapeOld, aShapeNew);
205 if (theDecomposeSolidsTag && aShapeNew.ShapeType() == TopAbs_COMPOUND) { // make sub elements as subs
207 // register name if it is possible
208 TCollection_AsciiString aName;
209 if(!aBuilder.NamedShape()->IsEmpty()) {
210 Handle(TDataStd_Name) anAttr;
211 if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
212 aName = TCollection_AsciiString(anAttr->Get()).ToCString();
216 TopoDS_Iterator aSubIter(aShapeNew);
217 for(int aTag = theDecomposeSolidsTag; aSubIter.More(); aSubIter.Next()) {
218 const TopoDS_Shape& aShape = aSubIter.Value();
219 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aShape, aShapeLab);
220 if(!aNS.IsNull() && !aNS->IsEmpty()) {
221 // This shape is already in document, don't add it.
224 TNaming_Builder aSubBuilder(aShapeLab.FindChild(aTag++));
225 aSubBuilder.Generated(aSubIter.Value());
226 if(!aName.IsEmpty()) {
227 TCollection_AsciiString aShapeType = aShape.ShapeType() == TopAbs_EDGE ? "_Edge_" :
228 aShape.ShapeType() == TopAbs_FACE ? "_Face_" :
229 aShape.ShapeType() == TopAbs_SOLID ? "_Solid_" : "_Shape_";
230 std::string aSolidName =
231 (aName + aShapeType + TCollection_AsciiString(aTag - theDecomposeSolidsTag)).ToCString();
232 std::shared_ptr<Model_Document> aDoc =
233 std::dynamic_pointer_cast<Model_Document>(document());
234 aDoc->addNamingName(aSubBuilder.NamedShape()->Label(), aSolidName);
235 TDataStd_Name::Set(aSubBuilder.NamedShape()->Label(), aSolidName.c_str());
242 void Model_BodyBuilder::storeWithoutNaming(const std::shared_ptr<GeomAPI_Shape>& theShape)
244 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
249 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
251 return; // null shape inside
252 TNaming_Builder aBuilder(aData->shapeLab());
253 aBuilder.Select(aShape, aShape);
257 void Model_BodyBuilder::clean()
259 std::vector<TNaming_Builder*>::iterator aBuilder = myBuilders.begin();
260 for(; aBuilder != myBuilders.end(); aBuilder++)
265 Model_BodyBuilder::~Model_BodyBuilder()
270 TNaming_Builder* Model_BodyBuilder::builder(const int theTag)
272 if (myBuilders.size() <= (unsigned int)theTag) {
273 myBuilders.insert(myBuilders.end(), theTag - myBuilders.size() + 1, NULL);
275 if (!myBuilders[theTag]) {
276 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
277 myBuilders[theTag] = new TNaming_Builder(aData->shapeLab().FindChild(theTag));
278 //TCollection_AsciiString entry;//
279 //TDF_Tool::Entry(aData->shapeLab().FindChild(theTag), entry);
280 //cout << "Label = " <<entry.ToCString() <<endl;
282 return myBuilders[theTag];
285 void Model_BodyBuilder::buildName(const int theTag, const std::string& theName)
287 std::string aName = data()->name() + "/" + theName;
288 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
289 aDoc->addNamingName(builder(theTag)->NamedShape()->Label(), aName);
290 TDataStd_Name::Set(builder(theTag)->NamedShape()->Label(),aName.c_str());
292 void Model_BodyBuilder::generated(
293 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
295 TopoDS_Shape aShape = theNewShape->impl<TopoDS_Shape>();
296 builder(theTag)->Generated(aShape);
298 buildName(theTag, theName);
301 void Model_BodyBuilder::generated(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
302 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
304 TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
305 TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
306 builder(theTag)->Generated(anOldShape, aNewShape);
308 buildName(theTag, theName);
309 TopAbs_ShapeEnum aGenShapeType = aNewShape.ShapeType();
310 if(aGenShapeType == TopAbs_WIRE || aGenShapeType == TopAbs_SHELL) {
311 TopAbs_ShapeEnum anExplodeShapeType = aGenShapeType == TopAbs_WIRE ? TopAbs_EDGE : TopAbs_FACE;
312 const TDF_Label aLabel = builder(theTag)->NamedShape()->Label();
314 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
315 for(TopExp_Explorer anExp(aNewShape, anExplodeShapeType); anExp.More(); anExp.Next()) {
316 TDF_Label aChildLabel = aLabel.FindChild(aTag);
317 TNaming_Builder aBuilder(aChildLabel);
318 aBuilder.Generated(anOldShape, anExp.Current());
319 TCollection_AsciiString aChildName = TCollection_AsciiString((data()->name() + "/" + theName + "_").c_str()) + aTag;
320 aDoc->addNamingName(aChildLabel, aChildName.ToCString());
321 TDataStd_Name::Set(aChildLabel, aChildName.ToCString());
328 void Model_BodyBuilder::modified(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
329 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
331 TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
332 TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
333 builder(theTag)->Modify(anOldShape, aNewShape);
335 buildName(theTag, theName);
338 void Model_BodyBuilder::deleted(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
341 TopoDS_Shape aShape = theOldShape->impl<TopoDS_Shape>();
342 builder(theTag)->Delete(aShape);
345 void Model_BodyBuilder::loadDeletedShapes (GeomAlgoAPI_MakeShape* theMS,
346 std::shared_ptr<GeomAPI_Shape> theShapeIn,
347 const int theKindOfShape,
350 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
351 TopTools_MapOfShape aView;
352 TopExp_Explorer ShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
353 for (; ShapeExplorer.More(); ShapeExplorer.Next ()) {
354 const TopoDS_Shape& aRoot = ShapeExplorer.Current ();
355 if (!aView.Add(aRoot)) continue;
356 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
357 aRShape->setImpl((new TopoDS_Shape(aRoot)));
358 if (theMS->isDeleted (aRShape)) {
359 builder(theTag)->Delete(aRoot);
364 void Model_BodyBuilder::loadAndOrientModifiedShapes (
365 GeomAlgoAPI_MakeShape* theMS,
366 std::shared_ptr<GeomAPI_Shape> theShapeIn,
367 const int theKindOfShape,
369 const std::string& theName,
370 GeomAPI_DataMapOfShapeShape& theSubShapes,
371 const bool theIsStoreSeparate)
375 bool isBuilt = !theName.empty();
376 std::string aName = theName;
377 std::ostringstream aStream;
378 GeomShapePtr aResultShape = shape();
379 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
380 TopTools_MapOfShape aView;
381 TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
382 for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
383 const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
384 if (!aView.Add(aRoot)) continue;
386 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
387 aRShape->setImpl((new TopoDS_Shape(aRoot)));
388 theMS->modified(aRShape, aList);
389 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = aList.begin(), aLast = aList.end();
390 for (; anIt != aLast; anIt++) {
391 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
392 if (theSubShapes.isBound(*anIt)) {
393 std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
394 aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
396 GeomShapePtr aGeomNewShape(new GeomAPI_Shape());
397 aGeomNewShape->setImpl(new TopoDS_Shape(aNewShape));
398 if(!aRoot.IsSame(aNewShape) && aResultShape->isSubShape(aGeomNewShape)) {
399 builder(aTag)->Modify(aRoot,aNewShape);
401 if(theIsStoreSeparate) {
402 aStream.str(std::string());
404 aStream << theName << "_" << anIndex++;
405 aName = aStream.str();
407 buildName(aTag, aName);
409 if(theIsStoreSeparate) {
417 void Model_BodyBuilder::loadAndOrientGeneratedShapes (
418 GeomAlgoAPI_MakeShape* theMS,
419 std::shared_ptr<GeomAPI_Shape> theShapeIn,
420 const int theKindOfShape,
422 const std::string& theName,
423 GeomAPI_DataMapOfShapeShape& theSubShapes)
425 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
426 TopTools_MapOfShape aView;
427 bool isBuilt = !theName.empty();
428 TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
429 for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
430 const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
431 if (!aView.Add(aRoot)) continue;
433 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
434 aRShape->setImpl((new TopoDS_Shape(aRoot)));
435 theMS->generated(aRShape, aList);
436 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = aList.begin(), aLast = aList.end();
437 for (; anIt != aLast; anIt++) {
438 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
439 if (theSubShapes.isBound(*anIt)) {
440 std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
441 aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
443 if (!aRoot.IsSame (aNewShape)) {
444 builder(theTag)->Generated(aRoot,aNewShape);
446 buildName(theTag, theName);
448 TopAbs_ShapeEnum aGenShapeType = aNewShape.ShapeType();
449 if(aGenShapeType == TopAbs_WIRE || aGenShapeType == TopAbs_SHELL) {
450 TopAbs_ShapeEnum anExplodeShapeType = aGenShapeType == TopAbs_WIRE ? TopAbs_EDGE : TopAbs_FACE;
451 const TDF_Label aLabel = builder(theTag)->NamedShape()->Label();
453 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
454 for(TopExp_Explorer anExp(aNewShape, anExplodeShapeType); anExp.More(); anExp.Next()) {
455 TDF_Label aChildLabel = aLabel.FindChild(aTag);
456 TNaming_Builder aBuilder(aChildLabel);
457 aBuilder.Generated(aRoot, anExp.Current());
458 TCollection_AsciiString aChildName = TCollection_AsciiString((data()->name() + "/" + theName + "_").c_str()) + aTag;
459 aDoc->addNamingName(aChildLabel, aChildName.ToCString());
460 TDataStd_Name::Set(aChildLabel, aChildName.ToCString());
468 //=======================================================================
469 int getDangleShapes(const TopoDS_Shape& theShapeIn,
470 const TopAbs_ShapeEnum theGeneratedFrom,
471 TopTools_DataMapOfShapeShape& theDangles)
474 TopTools_IndexedDataMapOfShapeListOfShape subShapeAndAncestors;
475 TopAbs_ShapeEnum GeneratedTo;
476 if (theGeneratedFrom == TopAbs_FACE) GeneratedTo = TopAbs_EDGE;
477 else if (theGeneratedFrom == TopAbs_EDGE) GeneratedTo = TopAbs_VERTEX;
478 else return Standard_False;
479 TopExp::MapShapesAndAncestors(theShapeIn, GeneratedTo, theGeneratedFrom, subShapeAndAncestors);
480 for (Standard_Integer i = 1; i <= subShapeAndAncestors.Extent(); i++) {
481 const TopoDS_Shape& mayBeDangle = subShapeAndAncestors.FindKey(i);
482 const TopTools_ListOfShape& ancestors = subShapeAndAncestors.FindFromIndex(i);
483 if (ancestors.Extent() == 1) theDangles.Bind(ancestors.First(), mayBeDangle);
485 return theDangles.Extent();
488 //=======================================================================
489 void loadGeneratedDangleShapes(
490 const TopoDS_Shape& theShapeIn,
491 const TopAbs_ShapeEnum theGeneratedFrom,
492 TNaming_Builder * theBuilder)
494 TopTools_DataMapOfShapeShape dangles;
495 if (!getDangleShapes(theShapeIn, theGeneratedFrom, dangles)) return;
496 TopTools_DataMapIteratorOfDataMapOfShapeShape itr(dangles);
497 for (; itr.More(); itr.Next())
498 theBuilder->Generated(itr.Key(), itr.Value());
501 //=======================================================================
502 void Model_BodyBuilder::loadNextLevels(std::shared_ptr<GeomAPI_Shape> theShape,
503 const std::string& theName, int& theTag)
505 if(theShape->isNull()) return;
506 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
508 if (aShape.ShapeType() == TopAbs_SOLID) {
509 TopExp_Explorer expl(aShape, TopAbs_FACE);
510 for (; expl.More(); expl.Next()) {
511 builder(theTag)->Generated(expl.Current());
512 TCollection_AsciiString aStr(theTag);
513 aName = theName + aStr.ToCString();
514 buildName(theTag, aName);
518 else if (aShape.ShapeType() == TopAbs_SHELL || aShape.ShapeType() == TopAbs_FACE) {
519 // load faces and all the free edges
520 TopTools_IndexedMapOfShape Faces;
521 TopExp::MapShapes(aShape, TopAbs_FACE, Faces);
522 if (Faces.Extent() > 1 || (aShape.ShapeType() == TopAbs_SHELL && Faces.Extent() == 1)) {
523 TopExp_Explorer expl(aShape, TopAbs_FACE);
524 for (; expl.More(); expl.Next()) {
525 builder(theTag)->Generated(expl.Current());
526 TCollection_AsciiString aStr(theTag);
527 aName = theName + aStr.ToCString();
528 buildName(theTag, aName);
532 TopTools_IndexedDataMapOfShapeListOfShape anEdgeAndNeighbourFaces;
533 TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, anEdgeAndNeighbourFaces);
534 for (Standard_Integer i = 1; i <= anEdgeAndNeighbourFaces.Extent(); i++)
536 const TopTools_ListOfShape& aLL = anEdgeAndNeighbourFaces.FindFromIndex(i);
537 if (aLL.Extent() < 2) {
538 if (BRep_Tool::Degenerated(TopoDS::Edge(anEdgeAndNeighbourFaces.FindKey(i))))
540 builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
541 TCollection_AsciiString aStr(theTag);
542 aName = theName + aStr.ToCString();
543 buildName(theTag, aName);
546 TopTools_ListIteratorOfListOfShape anIter(aLL);
547 const TopoDS_Face& aFace = TopoDS::Face(anIter.Value());
549 if(aFace.IsEqual(anIter.Value())) {
550 builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
551 TCollection_AsciiString aStr(theTag);
552 aName = theName + aStr.ToCString();
553 buildName(theTag, aName);
558 } else if (aShape.ShapeType() == TopAbs_WIRE) {
559 TopTools_IndexedMapOfShape Edges;
560 BRepTools::Map3DEdges(aShape, Edges);
561 if (Edges.Extent() == 1) {
562 builder(++theTag)->Generated(Edges.FindKey(1));
563 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
564 for (; expl.More(); expl.Next()) {
565 builder(theTag)->Generated(expl.Current());
566 TCollection_AsciiString aStr(theTag);
567 aName = theName + aStr.ToCString();
568 buildName(theTag, aName);
572 TopExp_Explorer expl(aShape, TopAbs_EDGE);
573 for (; expl.More(); expl.Next()) {
574 builder(theTag)->Generated(expl.Current());
575 TCollection_AsciiString aStr(theTag);
576 aName = theName + aStr.ToCString();
577 buildName(theTag, aName);
580 // and load generated vertices.
581 TopTools_DataMapOfShapeShape generated;
582 if (getDangleShapes(aShape, TopAbs_EDGE, generated))
584 TNaming_Builder* pBuilder = builder(theTag++);
585 loadGeneratedDangleShapes(aShape, TopAbs_EDGE, pBuilder);
588 } else if (aShape.ShapeType() == TopAbs_EDGE) {
589 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
590 for (; expl.More(); expl.Next()) {
591 builder(theTag)->Generated(expl.Current());
592 TCollection_AsciiString aStr(theTag);
593 aName = theName + aStr.ToCString();
594 buildName(theTag, aName);
600 //=======================================================================
601 int findAmbiguities(const TopoDS_Shape& theShapeIn,
602 TopTools_ListOfShape& theList)
606 TopTools_IndexedDataMapOfShapeListOfShape subShapeAndAncestors;
607 TopAbs_ShapeEnum aTS(TopAbs_EDGE);
608 TopAbs_ShapeEnum aTA(TopAbs_FACE);
609 TopTools_MapOfShape aMap1, aMap2; // map1 - for edge ancestors; map2 - for keys => edges
610 TopTools_ListOfShape aKeyList;
611 TopExp::MapShapesAndAncestors(theShapeIn, aTS, aTA, subShapeAndAncestors);
612 for (Standard_Integer i = 1; i <= subShapeAndAncestors.Extent(); i++) {
613 const TopoDS_Shape& aKeyEdge1 = subShapeAndAncestors.FindKey(i);
614 const TopTools_ListOfShape& ancestors1 = subShapeAndAncestors.FindFromIndex(i);
616 TopTools_ListIteratorOfListOfShape it(ancestors1);
617 for(;it.More();it.Next()) aMap1.Add(it.Value()); // fill map with key ancestors => aKey1
618 for (Standard_Integer j = 1; j <= subShapeAndAncestors.Extent(); j++) {
619 if (i == j) continue;
620 const TopoDS_Shape& aKeyEdge2 = subShapeAndAncestors.FindKey(j);
621 const TopTools_ListOfShape& ancestors2 = subShapeAndAncestors.FindFromIndex(j);
622 if(ancestors1.Extent() == ancestors2.Extent() && ancestors1.Extent() > 1) {
623 int aNum (ancestors2.Extent());
624 TopTools_ListIteratorOfListOfShape it(ancestors2);
625 for(;it.More();it.Next())
626 if(aMap1.Contains(it.Value())) aNum--;
628 if(aMap2.Add(aKeyEdge1))
629 aKeyList.Append(aKeyEdge1);
630 if(aMap2.Add(aKeyEdge2))
631 aKeyList.Append(aKeyEdge2);
634 } // at the end ==> List of edges to be named in addition
636 aNumEdges = aKeyList.Extent();
638 theList.Assign(aKeyList);
642 //=======================================================================
643 void Model_BodyBuilder::loadFirstLevel(
644 std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
646 if(theShape->isNull()) return;
647 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
649 if (aShape.ShapeType() == TopAbs_COMPOUND || aShape.ShapeType() == TopAbs_COMPSOLID) {
650 TopoDS_Iterator itr(aShape);
651 for (; itr.More(); itr.Next(),theTag++) {
652 builder(theTag)->Generated(itr.Value());
653 TCollection_AsciiString aStr(theTag);
654 aName = theName + aStr.ToCString();
655 buildName(theTag, aName);
656 if(!theName.empty()) buildName(theTag, aName);
657 if (itr.Value().ShapeType() == TopAbs_COMPOUND ||
658 itr.Value().ShapeType() == TopAbs_COMPSOLID)
660 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
661 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
662 loadFirstLevel(itrShape, theName, theTag);
664 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
665 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
666 loadNextLevels(itrShape, theName, theTag);
670 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
671 itrShape->setImpl(new TopoDS_Shape(aShape));
672 loadNextLevels(itrShape, theName, theTag);
674 TopTools_ListOfShape aList;
675 if(findAmbiguities(aShape, aList)) {
676 TopTools_ListIteratorOfListOfShape it(aList);
677 for (; it.More(); it.Next(),theTag++) {
678 builder(theTag)->Generated(it.Value());
679 TCollection_AsciiString aStr(theTag);
680 aName = theName + aStr.ToCString();
681 buildName(theTag, aName);
686 //=======================================================================
687 void Model_BodyBuilder::loadDisconnectedEdges(
688 std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
690 if(theShape->isNull()) return;
691 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
692 TopTools_DataMapOfShapeListOfShape edgeNaborFaces;
693 TopTools_ListOfShape empty;
694 TopExp_Explorer explF(aShape, TopAbs_FACE);
695 for (; explF.More(); explF.Next()) {
696 const TopoDS_Shape& aFace = explF.Current();
697 TopExp_Explorer explV(aFace, TopAbs_EDGE);
698 for (; explV.More(); explV.Next()) {
699 const TopoDS_Shape& anEdge = explV.Current();
700 if (!edgeNaborFaces.IsBound(anEdge)) edgeNaborFaces.Bind(anEdge, empty);
701 Standard_Boolean faceIsNew = Standard_True;
702 TopTools_ListIteratorOfListOfShape itrF(edgeNaborFaces.Find(anEdge));
703 for (; itrF.More(); itrF.Next()) {
704 if (itrF.Value().IsSame(aFace)) {
705 faceIsNew = Standard_False;
710 edgeNaborFaces.ChangeFind(anEdge).Append(aFace);
714 /* TopTools_IndexedDataMapOfShapeListOfShape aDM;
715 TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, aDM);
716 for(int i=1; i <= aDM.Extent(); i++) {
717 if(aDM.FindFromIndex(i).Extent() > 1) continue;
718 if (BRep_Tool::Degenerated(TopoDS::Edge(aDM.FindKey(i))))
720 builder(theTag)->Generated(aDM.FindKey(i));
721 TCollection_AsciiString aStr(theTag);
722 std::string aName = theName + aStr.ToCString();
723 buildName(theTag, aName);
726 BRepTools::Write(aDM.FindKey(i), aName.c_str());
731 TopTools_MapOfShape anEdgesToDelete;
732 TopExp_Explorer anEx(aShape,TopAbs_EDGE);
734 for(;anEx.More();anEx.Next()) {
735 Standard_Boolean aC0 = Standard_False;
736 TopoDS_Shape anEdge1 = anEx.Current();
737 if (edgeNaborFaces.IsBound(anEdge1)) {
738 const TopTools_ListOfShape& aList1 = edgeNaborFaces.Find(anEdge1);
739 if (aList1.Extent()<2) continue;
740 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(edgeNaborFaces);
741 for (; itr.More(); itr.Next()) {
742 TopoDS_Shape anEdge2 = itr.Key();
743 if(anEdgesToDelete.Contains(anEdge2)) continue;
744 if (anEdge1.IsSame(anEdge2)) continue;
745 const TopTools_ListOfShape& aList2 = itr.Value();
746 // compare lists of the neighbour faces of edge1 and edge2
747 if (aList1.Extent() == aList2.Extent()) {
748 Standard_Integer aMatches = 0;
749 for(TopTools_ListIteratorOfListOfShape aLIter1(aList1);aLIter1.More();aLIter1.Next())
750 for(TopTools_ListIteratorOfListOfShape aLIter2(aList2);aLIter2.More();aLIter2.Next())
751 if (aLIter1.Value().IsSame(aLIter2.Value())) aMatches++;
752 if (aMatches == aList1.Extent()) {
754 builder(theTag)->Generated(anEdge2);
755 anEdgesToDelete.Add(anEdge2);
756 TCollection_AsciiString aStr(theTag);
757 aName = theName + aStr.ToCString();
758 buildName(theTag, aName);
763 TopTools_MapIteratorOfMapOfShape itDelete(anEdgesToDelete);
764 for(;itDelete.More();itDelete.Next())
765 edgeNaborFaces.UnBind(itDelete.Key());
766 edgeNaborFaces.UnBind(anEdge1);
769 builder(theTag)->Generated(anEdge1);
770 TCollection_AsciiString aStr(theTag);
771 aName = theName + aStr.ToCString();
772 buildName(theTag, aName);
778 void Model_BodyBuilder::loadDisconnectedVertexes(std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
780 if(theShape->isNull()) return;
781 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
782 TopTools_DataMapOfShapeListOfShape vertexNaborEdges;
783 TopTools_ListOfShape empty;
784 TopExp_Explorer explF(aShape, TopAbs_EDGE);
785 for (; explF.More(); explF.Next()) {
786 const TopoDS_Shape& anEdge = explF.Current();
787 TopExp_Explorer explV(anEdge, TopAbs_VERTEX);
788 for (; explV.More(); explV.Next()) {
789 const TopoDS_Shape& aVertex = explV.Current();
790 if (!vertexNaborEdges.IsBound(aVertex)) vertexNaborEdges.Bind(aVertex, empty);
791 Standard_Boolean faceIsNew = Standard_True;
792 TopTools_ListIteratorOfListOfShape itrF(vertexNaborEdges.Find(aVertex));
793 for (; itrF.More(); itrF.Next()) {
794 if (itrF.Value().IsSame(anEdge)) {
795 faceIsNew = Standard_False;
800 vertexNaborEdges.ChangeFind(aVertex).Append(anEdge);
805 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(vertexNaborEdges);
806 for (; itr.More(); itr.Next()) {
807 const TopTools_ListOfShape& naborEdges = itr.Value();
808 if (naborEdges.Extent() < 2) {
809 builder(theTag)->Generated(itr.Key());
810 TCollection_AsciiString aStr(theTag);
811 aName = theName + aStr.ToCString();
812 buildName(theTag, aName);
818 std::shared_ptr<GeomAPI_Shape> Model_BodyBuilder::shape()
820 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
822 TDF_Label aShapeLab = aData->shapeLab();
823 Handle(TDF_Reference) aRef;
824 if (aShapeLab.FindAttribute(TDF_Reference::GetID(), aRef)) {
825 aShapeLab = aRef->Get();
827 Handle(TNaming_NamedShape) aName;
828 if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
829 TopoDS_Shape aShape = aName->Get();
830 if (!aShape.IsNull()) {
831 std::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
832 aRes->setImpl(new TopoDS_Shape(aShape));
837 return std::shared_ptr<GeomAPI_Shape>();
840 bool Model_BodyBuilder::isLatestEqual(const std::shared_ptr<GeomAPI_Shape>& theShape)
842 if (theShape.get()) {
843 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
844 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
846 TDF_Label& aShapeLab = aData->shapeLab();
847 Handle(TNaming_NamedShape) aName;
848 if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
849 TopoDS_Shape aLatest = TNaming_Tool::CurrentShape(aName);
850 if (aLatest.IsNull())
852 if (aLatest.IsEqual(aShape))
854 // check sub-shapes for comp-solids:
855 for (TopExp_Explorer anExp(aShape, aLatest.ShapeType()); anExp.More(); anExp.Next()) {
856 if (aLatest.IsEqual(anExp.Current()))