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 <ModelAPI_AttributeIntArray.h>
12 #include <TNaming_Builder.hxx>
13 #include <TNaming_NamedShape.hxx>
14 #include <TNaming_Iterator.hxx>
15 #include <TNaming_Tool.hxx>
16 #include <TDataStd_Name.hxx>
17 #include <TDataStd_Integer.hxx>
19 #include <TopoDS_Face.hxx>
20 #include <TDF_ChildIterator.hxx>
21 #include <TDF_Reference.hxx>
22 #include <TopTools_MapOfShape.hxx>
23 #include <TopExp_Explorer.hxx>
24 #include <TopTools_ListOfShape.hxx>
25 #include <TopTools_ListIteratorOfListOfShape.hxx>
26 #include <TopTools_DataMapOfShapeListOfShape.hxx>
27 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
28 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
29 #include <TopTools_MapIteratorOfMapOfShape.hxx>
30 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
31 #include <TopTools_IndexedMapOfShape.hxx>
32 #include <TopTools_DataMapOfShapeShape.hxx>
34 #include <BRepTools.hxx>
35 #include <BRep_Tool.hxx>
36 #include <GeomAPI_Shape.h>
37 #include <GeomAlgoAPI_MakeShape.h>
38 #include <Config_PropManager.h>
40 //#include <TCollection_AsciiString.hxx>
41 //#include <TDF_Tool.hxx>
42 //#define DEB_IMPORT 1
44 Model_BodyBuilder::Model_BodyBuilder(ModelAPI_Object* theOwner)
45 : ModelAPI_BodyBuilder(theOwner)
49 // Converts evolution of naming shape to selection evelution and back to avoid
50 // naming support on the disabled results. Deeply in the labels tree, recursively.
51 static void evolutionToSelectionRec(TDF_Label theLab, const bool theFlag) {
52 std::list<std::pair<TopoDS_Shape, TopoDS_Shape> > aShapePairs; // to store old and new shapes
53 Handle(TNaming_NamedShape) aName;
55 if (theLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
56 TNaming_Evolution aNSEvol = aName->Evolution();
57 if ((aNSEvol == TNaming_SELECTED && theFlag) ||
58 (aNSEvol != TNaming_SELECTED && !theFlag)) { // nothing to do, it is already correct
61 anEvolution = (int)(aNSEvol);
63 Handle(TDataStd_Integer) anAttrEvol;
64 if (theLab.FindAttribute(TDataStd_Integer::GetID(), anAttrEvol)) {
65 anEvolution = anAttrEvol->Get();
68 TDataStd_Integer::Set(theLab, anEvolution);
71 for(TNaming_Iterator anIter(aName); anIter.More(); anIter.Next()) {
72 aShapePairs.push_back(std::pair<TopoDS_Shape, TopoDS_Shape>
73 (anIter.OldShape(), anIter.NewShape()));
77 TNaming_Builder aBuilder(theLab);
78 TNaming_Evolution anEvol = (TNaming_Evolution)(anEvolution);
79 std::list<std::pair<TopoDS_Shape, TopoDS_Shape> >::iterator aPairsIter = aShapePairs.begin();
80 for(; aPairsIter != aShapePairs.end(); aPairsIter++) {
81 if (theFlag) { // disabled => make selection
82 aBuilder.Select(aPairsIter->second, aPairsIter->first);
83 } else if (anEvol == TNaming_GENERATED) {
84 aBuilder.Generated(aPairsIter->first, aPairsIter->second);
85 } else if (anEvol == TNaming_MODIFY) {
86 aBuilder.Modify(aPairsIter->first, aPairsIter->second);
87 } else if (anEvol == TNaming_DELETE) {
88 aBuilder.Delete(aPairsIter->first);
89 } else if (anEvol == TNaming_PRIMITIVE) {
90 aBuilder.Generated(aPairsIter->second);
91 } else if (anEvol == TNaming_SELECTED) {
92 aBuilder.Select(aPairsIter->second, aPairsIter->first);
95 // recursive call for all sub-labels
96 TDF_ChildIterator anIter(theLab, Standard_False);
97 for(; anIter.More(); anIter.Next()) {
98 evolutionToSelectionRec(anIter.Value(), theFlag);
102 void Model_BodyBuilder::evolutionToSelection(const bool theFlag)
104 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
105 if (!aData) // unknown case
107 TDF_Label& aShapeLab = aData->shapeLab();
108 evolutionToSelectionRec(aShapeLab, theFlag);
111 void Model_BodyBuilder::store(const std::shared_ptr<GeomAPI_Shape>& theShape,
112 const bool theIsStoreSameShapes)
114 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
116 TDF_Label& aShapeLab = aData->shapeLab();
119 // store the new shape as primitive
120 TNaming_Builder aBuilder(aShapeLab);
123 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
125 return; // null shape inside
127 if(!theIsStoreSameShapes) {
128 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aShape, aShapeLab);
129 if(!aNS.IsNull() && !aNS->IsEmpty()) {
130 // This shape is already in document, store reference instead of shape;
131 const TDF_Label aFoundLabel = aNS->Label();
132 TDF_Reference::Set(aShapeLab, aFoundLabel);
133 aShapeLab.ForgetAttribute(TNaming_NamedShape::GetID());
138 aBuilder.Generated(aShape);
140 aShapeLab.ForgetAttribute(TDF_Reference::GetID());
141 if(!aBuilder.NamedShape()->IsEmpty()) {
142 Handle(TDataStd_Name) anAttr;
143 if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
144 std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
146 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
147 aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
154 void Model_BodyBuilder::storeGenerated(const std::shared_ptr<GeomAPI_Shape>& theFromShape,
155 const std::shared_ptr<GeomAPI_Shape>& theToShape)
157 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
159 TDF_Label& aShapeLab = aData->shapeLab();
162 // store the new shape as primitive
163 TNaming_Builder aBuilder(aShapeLab);
164 if (!theFromShape || !theToShape)
166 TopoDS_Shape aShapeBasis = theFromShape->impl<TopoDS_Shape>();
167 if (aShapeBasis.IsNull())
168 return; // null shape inside
169 TopoDS_Shape aShapeNew = theToShape->impl<TopoDS_Shape>();
170 if (aShapeNew.IsNull())
171 return; // null shape inside
172 aBuilder.Generated(aShapeBasis, aShapeNew);
174 if(!aBuilder.NamedShape()->IsEmpty()) {
175 Handle(TDataStd_Name) anAttr;
176 if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
177 std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
179 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
180 aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
187 void Model_BodyBuilder::storeModified(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
188 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const int theDecomposeSolidsTag)
190 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
192 TDF_Label& aShapeLab = aData->shapeLab();
195 // store the new shape as primitive
196 TNaming_Builder aBuilder(aShapeLab);
197 if (!theOldShape || !theNewShape)
199 TopoDS_Shape aShapeOld = theOldShape->impl<TopoDS_Shape>();
200 if (aShapeOld.IsNull())
201 return; // null shape inside
202 TopoDS_Shape aShapeNew = theNewShape->impl<TopoDS_Shape>();
203 if (aShapeNew.IsNull())
204 return; // null shape inside
205 aBuilder.Modify(aShapeOld, aShapeNew);
206 if (theDecomposeSolidsTag && aShapeNew.ShapeType() == TopAbs_COMPOUND) { // make sub elements as subs
208 // register name if it is possible
209 TCollection_AsciiString aName;
210 if(!aBuilder.NamedShape()->IsEmpty()) {
211 Handle(TDataStd_Name) anAttr;
212 if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
213 aName = TCollection_AsciiString(anAttr->Get()).ToCString();
217 TopoDS_Iterator aSubIter(aShapeNew);
218 for(int aTag = theDecomposeSolidsTag; aSubIter.More(); aSubIter.Next()) {
219 const TopoDS_Shape& aShape = aSubIter.Value();
220 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aShape, aShapeLab);
221 if(!aNS.IsNull() && !aNS->IsEmpty()) {
222 // This shape is already in document, don't add it.
225 TNaming_Builder aSubBuilder(aShapeLab.FindChild(aTag++));
226 aSubBuilder.Generated(aSubIter.Value());
227 if(!aName.IsEmpty()) {
228 TCollection_AsciiString aShapeType = aShape.ShapeType() == TopAbs_EDGE ? "_Edge_" :
229 aShape.ShapeType() == TopAbs_FACE ? "_Face_" :
230 aShape.ShapeType() == TopAbs_SOLID ? "_Solid_" : "_Shape_";
231 std::string aSolidName =
232 (aName + aShapeType + TCollection_AsciiString(aTag - theDecomposeSolidsTag)).ToCString();
233 std::shared_ptr<Model_Document> aDoc =
234 std::dynamic_pointer_cast<Model_Document>(document());
235 aDoc->addNamingName(aSubBuilder.NamedShape()->Label(), aSolidName);
236 TDataStd_Name::Set(aSubBuilder.NamedShape()->Label(), aSolidName.c_str());
243 void Model_BodyBuilder::storeWithoutNaming(const std::shared_ptr<GeomAPI_Shape>& theShape)
245 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
250 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
252 return; // null shape inside
253 TNaming_Builder aBuilder(aData->shapeLab());
254 aBuilder.Select(aShape, aShape);
258 void Model_BodyBuilder::clean()
260 std::vector<TNaming_Builder*>::iterator aBuilder = myBuilders.begin();
261 for(; aBuilder != myBuilders.end(); aBuilder++)
266 Model_BodyBuilder::~Model_BodyBuilder()
271 TNaming_Builder* Model_BodyBuilder::builder(const int theTag)
273 if (myBuilders.size() <= (unsigned int)theTag) {
274 myBuilders.insert(myBuilders.end(), theTag - myBuilders.size() + 1, NULL);
276 if (!myBuilders[theTag]) {
277 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
278 myBuilders[theTag] = new TNaming_Builder(aData->shapeLab().FindChild(theTag));
279 //TCollection_AsciiString entry;//
280 //TDF_Tool::Entry(aData->shapeLab().FindChild(theTag), entry);
281 //cout << "Label = " <<entry.ToCString() <<endl;
283 return myBuilders[theTag];
286 void Model_BodyBuilder::buildName(const int theTag, const std::string& theName)
288 std::string aName = data()->name() + "/" + theName;
289 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
290 aDoc->addNamingName(builder(theTag)->NamedShape()->Label(), aName);
291 TDataStd_Name::Set(builder(theTag)->NamedShape()->Label(),aName.c_str());
293 void Model_BodyBuilder::generated(
294 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
296 TopoDS_Shape aShape = theNewShape->impl<TopoDS_Shape>();
297 builder(theTag)->Generated(aShape);
299 buildName(theTag, theName);
302 void Model_BodyBuilder::generated(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
303 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
305 TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
306 TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
307 builder(theTag)->Generated(anOldShape, aNewShape);
309 buildName(theTag, theName);
313 void Model_BodyBuilder::modified(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
314 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
316 TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
317 TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
318 builder(theTag)->Modify(anOldShape, aNewShape);
320 buildName(theTag, theName);
323 void Model_BodyBuilder::deleted(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
326 TopoDS_Shape aShape = theOldShape->impl<TopoDS_Shape>();
327 builder(theTag)->Delete(aShape);
330 void Model_BodyBuilder::loadDeletedShapes (GeomAlgoAPI_MakeShape* theMS,
331 std::shared_ptr<GeomAPI_Shape> theShapeIn,
332 const int theKindOfShape,
335 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
336 TopTools_MapOfShape aView;
337 TopExp_Explorer ShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
338 for (; ShapeExplorer.More(); ShapeExplorer.Next ()) {
339 const TopoDS_Shape& aRoot = ShapeExplorer.Current ();
340 if (!aView.Add(aRoot)) continue;
341 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
342 aRShape->setImpl((new TopoDS_Shape(aRoot)));
343 if (theMS->isDeleted (aRShape)) {
344 builder(theTag)->Delete(aRoot);
349 void Model_BodyBuilder::loadAndOrientModifiedShapes (
350 GeomAlgoAPI_MakeShape* theMS,
351 std::shared_ptr<GeomAPI_Shape> theShapeIn,
352 const int theKindOfShape,
354 const std::string& theName,
355 GeomAPI_DataMapOfShapeShape& theSubShapes,
356 const bool theIsStoreSeparate)
359 std::string aName = theName;
360 std::ostringstream aStream;
362 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
363 TopTools_MapOfShape aView;
364 bool isBuilt = theName.empty();
365 TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
366 for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
367 const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
368 if (!aView.Add(aRoot)) continue;
370 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
371 aRShape->setImpl((new TopoDS_Shape(aRoot)));
372 theMS->modified(aRShape, aList);
373 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = aList.begin(), aLast = aList.end();
374 for (; anIt != aLast; anIt++) {
375 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
376 if (theSubShapes.isBound(*anIt)) {
377 std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
378 aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
380 if (!aRoot.IsSame (aNewShape)) {
381 builder(aTag)->Modify(aRoot,aNewShape);
383 if(theIsStoreSeparate) {
384 aStream.str(std::string());
386 aStream << theName << "_" << anIndex++;
387 aName = aStream.str();
389 buildName(aTag, aName);
391 if(theIsStoreSeparate) {
399 void Model_BodyBuilder::loadAndOrientGeneratedShapes (
400 GeomAlgoAPI_MakeShape* theMS,
401 std::shared_ptr<GeomAPI_Shape> theShapeIn,
402 const int theKindOfShape,
404 const std::string& theName,
405 GeomAPI_DataMapOfShapeShape& theSubShapes)
407 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
408 TopTools_MapOfShape aView;
409 bool isBuilt = theName.empty();
410 TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
411 for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
412 const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
413 if (!aView.Add(aRoot)) continue;
415 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
416 aRShape->setImpl((new TopoDS_Shape(aRoot)));
417 theMS->generated(aRShape, aList);
418 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = aList.begin(), aLast = aList.end();
419 for (; anIt != aLast; anIt++) {
420 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
421 if (theSubShapes.isBound(*anIt)) {
422 std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
423 aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
425 if (!aRoot.IsSame (aNewShape)) {
426 builder(theTag)->Generated(aRoot,aNewShape);
428 buildName(theTag, theName);
434 //=======================================================================
435 int getDangleShapes(const TopoDS_Shape& theShapeIn,
436 const TopAbs_ShapeEnum theGeneratedFrom,
437 TopTools_DataMapOfShapeShape& theDangles)
440 TopTools_IndexedDataMapOfShapeListOfShape subShapeAndAncestors;
441 TopAbs_ShapeEnum GeneratedTo;
442 if (theGeneratedFrom == TopAbs_FACE) GeneratedTo = TopAbs_EDGE;
443 else if (theGeneratedFrom == TopAbs_EDGE) GeneratedTo = TopAbs_VERTEX;
444 else return Standard_False;
445 TopExp::MapShapesAndAncestors(theShapeIn, GeneratedTo, theGeneratedFrom, subShapeAndAncestors);
446 for (Standard_Integer i = 1; i <= subShapeAndAncestors.Extent(); i++) {
447 const TopoDS_Shape& mayBeDangle = subShapeAndAncestors.FindKey(i);
448 const TopTools_ListOfShape& ancestors = subShapeAndAncestors.FindFromIndex(i);
449 if (ancestors.Extent() == 1) theDangles.Bind(ancestors.First(), mayBeDangle);
451 return theDangles.Extent();
454 //=======================================================================
455 void loadGeneratedDangleShapes(
456 const TopoDS_Shape& theShapeIn,
457 const TopAbs_ShapeEnum theGeneratedFrom,
458 TNaming_Builder * theBuilder)
460 TopTools_DataMapOfShapeShape dangles;
461 if (!getDangleShapes(theShapeIn, theGeneratedFrom, dangles)) return;
462 TopTools_DataMapIteratorOfDataMapOfShapeShape itr(dangles);
463 for (; itr.More(); itr.Next())
464 theBuilder->Generated(itr.Key(), itr.Value());
467 //=======================================================================
468 void Model_BodyBuilder::loadNextLevels(std::shared_ptr<GeomAPI_Shape> theShape,
469 const std::string& theName, int& theTag)
471 if(theShape->isNull()) return;
472 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
474 if (aShape.ShapeType() == TopAbs_SOLID) {
475 TopExp_Explorer expl(aShape, TopAbs_FACE);
476 for (; expl.More(); expl.Next()) {
477 builder(theTag)->Generated(expl.Current());
478 TCollection_AsciiString aStr(theTag);
479 aName = theName + aStr.ToCString();
480 buildName(theTag, aName);
484 else if (aShape.ShapeType() == TopAbs_SHELL || aShape.ShapeType() == TopAbs_FACE) {
485 // load faces and all the free edges
486 TopTools_IndexedMapOfShape Faces;
487 TopExp::MapShapes(aShape, TopAbs_FACE, Faces);
488 if (Faces.Extent() > 1 || (aShape.ShapeType() == TopAbs_SHELL && Faces.Extent() == 1)) {
489 TopExp_Explorer expl(aShape, TopAbs_FACE);
490 for (; expl.More(); expl.Next()) {
491 builder(theTag)->Generated(expl.Current());
492 TCollection_AsciiString aStr(theTag);
493 aName = theName + aStr.ToCString();
494 buildName(theTag, aName);
498 TopTools_IndexedDataMapOfShapeListOfShape anEdgeAndNeighbourFaces;
499 TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, anEdgeAndNeighbourFaces);
500 for (Standard_Integer i = 1; i <= anEdgeAndNeighbourFaces.Extent(); i++)
502 const TopTools_ListOfShape& aLL = anEdgeAndNeighbourFaces.FindFromIndex(i);
503 if (aLL.Extent() < 2) {
504 if (BRep_Tool::Degenerated(TopoDS::Edge(anEdgeAndNeighbourFaces.FindKey(i))))
506 builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
507 TCollection_AsciiString aStr(theTag);
508 aName = theName + aStr.ToCString();
509 buildName(theTag, aName);
512 TopTools_ListIteratorOfListOfShape anIter(aLL);
513 const TopoDS_Face& aFace = TopoDS::Face(anIter.Value());
515 if(aFace.IsEqual(anIter.Value())) {
516 builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
517 TCollection_AsciiString aStr(theTag);
518 aName = theName + aStr.ToCString();
519 buildName(theTag, aName);
524 } else if (aShape.ShapeType() == TopAbs_WIRE) {
525 TopTools_IndexedMapOfShape Edges;
526 BRepTools::Map3DEdges(aShape, Edges);
527 if (Edges.Extent() == 1) {
528 builder(++theTag)->Generated(Edges.FindKey(1));
529 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
530 for (; expl.More(); expl.Next()) {
531 builder(theTag)->Generated(expl.Current());
532 TCollection_AsciiString aStr(theTag);
533 aName = theName + aStr.ToCString();
534 buildName(theTag, aName);
538 TopExp_Explorer expl(aShape, TopAbs_EDGE);
539 for (; expl.More(); expl.Next()) {
540 builder(theTag)->Generated(expl.Current());
541 TCollection_AsciiString aStr(theTag);
542 aName = theName + aStr.ToCString();
543 buildName(theTag, aName);
546 // and load generated vertices.
547 TopTools_DataMapOfShapeShape generated;
548 if (getDangleShapes(aShape, TopAbs_EDGE, generated))
550 TNaming_Builder* pBuilder = builder(theTag++);
551 loadGeneratedDangleShapes(aShape, TopAbs_EDGE, pBuilder);
554 } else if (aShape.ShapeType() == TopAbs_EDGE) {
555 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
556 for (; expl.More(); expl.Next()) {
557 builder(theTag)->Generated(expl.Current());
558 TCollection_AsciiString aStr(theTag);
559 aName = theName + aStr.ToCString();
560 buildName(theTag, aName);
566 //=======================================================================
567 int findAmbiguities(const TopoDS_Shape& theShapeIn,
568 TopTools_ListOfShape& theList)
572 TopTools_IndexedDataMapOfShapeListOfShape subShapeAndAncestors;
573 TopAbs_ShapeEnum aTS(TopAbs_EDGE);
574 TopAbs_ShapeEnum aTA(TopAbs_FACE);
575 TopTools_MapOfShape aMap1, aMap2; // map1 - for edge ancestors; map2 - for keys => edges
576 TopTools_ListOfShape aKeyList;
577 TopExp::MapShapesAndAncestors(theShapeIn, aTS, aTA, subShapeAndAncestors);
578 for (Standard_Integer i = 1; i <= subShapeAndAncestors.Extent(); i++) {
579 const TopoDS_Shape& aKeyEdge1 = subShapeAndAncestors.FindKey(i);
580 const TopTools_ListOfShape& ancestors1 = subShapeAndAncestors.FindFromIndex(i);
582 TopTools_ListIteratorOfListOfShape it(ancestors1);
583 for(;it.More();it.Next()) aMap1.Add(it.Value()); // fill map with key ancestors => aKey1
584 for (Standard_Integer j = 1; j <= subShapeAndAncestors.Extent(); j++) {
585 if (i == j) continue;
586 const TopoDS_Shape& aKeyEdge2 = subShapeAndAncestors.FindKey(j);
587 const TopTools_ListOfShape& ancestors2 = subShapeAndAncestors.FindFromIndex(j);
588 if(ancestors1.Extent() == ancestors2.Extent() && ancestors1.Extent() > 1) {
589 int aNum (ancestors2.Extent());
590 TopTools_ListIteratorOfListOfShape it(ancestors2);
591 for(;it.More();it.Next())
592 if(aMap1.Contains(it.Value())) aNum--;
594 if(aMap2.Add(aKeyEdge1))
595 aKeyList.Append(aKeyEdge1);
596 if(aMap2.Add(aKeyEdge2))
597 aKeyList.Append(aKeyEdge2);
600 } // at the end ==> List of edges to be named in addition
602 aNumEdges = aKeyList.Extent();
604 theList.Assign(aKeyList);
608 //=======================================================================
609 void Model_BodyBuilder::loadFirstLevel(
610 std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
612 if(theShape->isNull()) return;
613 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
615 if (aShape.ShapeType() == TopAbs_COMPOUND || aShape.ShapeType() == TopAbs_COMPSOLID) {
616 TopoDS_Iterator itr(aShape);
617 for (; itr.More(); itr.Next(),theTag++) {
618 builder(theTag)->Generated(itr.Value());
619 TCollection_AsciiString aStr(theTag);
620 aName = theName + aStr.ToCString();
621 buildName(theTag, aName);
622 if(!theName.empty()) buildName(theTag, aName);
623 if (itr.Value().ShapeType() == TopAbs_COMPOUND ||
624 itr.Value().ShapeType() == TopAbs_COMPSOLID)
626 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
627 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
628 loadFirstLevel(itrShape, theName, theTag);
630 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
631 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
632 loadNextLevels(itrShape, theName, theTag);
636 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
637 itrShape->setImpl(new TopoDS_Shape(aShape));
638 loadNextLevels(itrShape, theName, theTag);
640 TopTools_ListOfShape aList;
641 if(findAmbiguities(aShape, aList)) {
642 TopTools_ListIteratorOfListOfShape it(aList);
643 for (; it.More(); it.Next(),theTag++) {
644 builder(theTag)->Generated(it.Value());
645 TCollection_AsciiString aStr(theTag);
646 aName = theName + aStr.ToCString();
647 buildName(theTag, aName);
652 //=======================================================================
653 void Model_BodyBuilder::loadDisconnectedEdges(
654 std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
656 if(theShape->isNull()) return;
657 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
658 TopTools_DataMapOfShapeListOfShape edgeNaborFaces;
659 TopTools_ListOfShape empty;
660 TopExp_Explorer explF(aShape, TopAbs_FACE);
661 for (; explF.More(); explF.Next()) {
662 const TopoDS_Shape& aFace = explF.Current();
663 TopExp_Explorer explV(aFace, TopAbs_EDGE);
664 for (; explV.More(); explV.Next()) {
665 const TopoDS_Shape& anEdge = explV.Current();
666 if (!edgeNaborFaces.IsBound(anEdge)) edgeNaborFaces.Bind(anEdge, empty);
667 Standard_Boolean faceIsNew = Standard_True;
668 TopTools_ListIteratorOfListOfShape itrF(edgeNaborFaces.Find(anEdge));
669 for (; itrF.More(); itrF.Next()) {
670 if (itrF.Value().IsSame(aFace)) {
671 faceIsNew = Standard_False;
676 edgeNaborFaces.ChangeFind(anEdge).Append(aFace);
680 /* TopTools_IndexedDataMapOfShapeListOfShape aDM;
681 TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, aDM);
682 for(int i=1; i <= aDM.Extent(); i++) {
683 if(aDM.FindFromIndex(i).Extent() > 1) continue;
684 if (BRep_Tool::Degenerated(TopoDS::Edge(aDM.FindKey(i))))
686 builder(theTag)->Generated(aDM.FindKey(i));
687 TCollection_AsciiString aStr(theTag);
688 std::string aName = theName + aStr.ToCString();
689 buildName(theTag, aName);
692 BRepTools::Write(aDM.FindKey(i), aName.c_str());
697 TopTools_MapOfShape anEdgesToDelete;
698 TopExp_Explorer anEx(aShape,TopAbs_EDGE);
700 for(;anEx.More();anEx.Next()) {
701 Standard_Boolean aC0 = Standard_False;
702 TopoDS_Shape anEdge1 = anEx.Current();
703 if (edgeNaborFaces.IsBound(anEdge1)) {
704 const TopTools_ListOfShape& aList1 = edgeNaborFaces.Find(anEdge1);
705 if (aList1.Extent()<2) continue;
706 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(edgeNaborFaces);
707 for (; itr.More(); itr.Next()) {
708 TopoDS_Shape anEdge2 = itr.Key();
709 if(anEdgesToDelete.Contains(anEdge2)) continue;
710 if (anEdge1.IsSame(anEdge2)) continue;
711 const TopTools_ListOfShape& aList2 = itr.Value();
712 // compare lists of the neighbour faces of edge1 and edge2
713 if (aList1.Extent() == aList2.Extent()) {
714 Standard_Integer aMatches = 0;
715 for(TopTools_ListIteratorOfListOfShape aLIter1(aList1);aLIter1.More();aLIter1.Next())
716 for(TopTools_ListIteratorOfListOfShape aLIter2(aList2);aLIter2.More();aLIter2.Next())
717 if (aLIter1.Value().IsSame(aLIter2.Value())) aMatches++;
718 if (aMatches == aList1.Extent()) {
720 builder(theTag)->Generated(anEdge2);
721 anEdgesToDelete.Add(anEdge2);
722 TCollection_AsciiString aStr(theTag);
723 aName = theName + aStr.ToCString();
724 buildName(theTag, aName);
729 TopTools_MapIteratorOfMapOfShape itDelete(anEdgesToDelete);
730 for(;itDelete.More();itDelete.Next())
731 edgeNaborFaces.UnBind(itDelete.Key());
732 edgeNaborFaces.UnBind(anEdge1);
735 builder(theTag)->Generated(anEdge1);
736 TCollection_AsciiString aStr(theTag);
737 aName = theName + aStr.ToCString();
738 buildName(theTag, aName);
744 void Model_BodyBuilder::loadDisconnectedVertexes(std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
746 if(theShape->isNull()) return;
747 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
748 TopTools_DataMapOfShapeListOfShape vertexNaborEdges;
749 TopTools_ListOfShape empty;
750 TopExp_Explorer explF(aShape, TopAbs_EDGE);
751 for (; explF.More(); explF.Next()) {
752 const TopoDS_Shape& anEdge = explF.Current();
753 TopExp_Explorer explV(anEdge, TopAbs_VERTEX);
754 for (; explV.More(); explV.Next()) {
755 const TopoDS_Shape& aVertex = explV.Current();
756 if (!vertexNaborEdges.IsBound(aVertex)) vertexNaborEdges.Bind(aVertex, empty);
757 Standard_Boolean faceIsNew = Standard_True;
758 TopTools_ListIteratorOfListOfShape itrF(vertexNaborEdges.Find(aVertex));
759 for (; itrF.More(); itrF.Next()) {
760 if (itrF.Value().IsSame(anEdge)) {
761 faceIsNew = Standard_False;
766 vertexNaborEdges.ChangeFind(aVertex).Append(anEdge);
771 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(vertexNaborEdges);
772 for (; itr.More(); itr.Next()) {
773 const TopTools_ListOfShape& naborEdges = itr.Value();
774 if (naborEdges.Extent() < 2) {
775 builder(theTag)->Generated(itr.Key());
776 TCollection_AsciiString aStr(theTag);
777 aName = theName + aStr.ToCString();
778 buildName(theTag, aName);
784 std::shared_ptr<GeomAPI_Shape> Model_BodyBuilder::shape()
786 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
788 TDF_Label aShapeLab = aData->shapeLab();
789 Handle(TDF_Reference) aRef;
790 if (aShapeLab.FindAttribute(TDF_Reference::GetID(), aRef)) {
791 aShapeLab = aRef->Get();
793 Handle(TNaming_NamedShape) aName;
794 if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
795 TopoDS_Shape aShape = aName->Get();
796 if (!aShape.IsNull()) {
797 std::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
798 aRes->setImpl(new TopoDS_Shape(aShape));
803 return std::shared_ptr<GeomAPI_Shape>();
806 bool Model_BodyBuilder::isLatestEqual(const std::shared_ptr<GeomAPI_Shape>& theShape)
808 if (theShape.get()) {
809 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
810 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
812 TDF_Label& aShapeLab = aData->shapeLab();
813 Handle(TNaming_NamedShape) aName;
814 if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
815 TopoDS_Shape aLatest = TNaming_Tool::CurrentShape(aName);
816 if (aLatest.IsNull())
818 if (aLatest.IsEqual(aShape))
820 // check sub-shapes for comp-solids:
821 for (TopExp_Explorer anExp(aShape, aLatest.ShapeType()); anExp.More(); anExp.Next()) {
822 if (aLatest.IsEqual(anExp.Current()))