Salome HOME
Fix for the issue #686: now compounds produced by the Boolean operations can be corre...
[modules/shaper.git] / src / Model / Model_ResultBody.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        Model_ResultBody.cpp
4 // Created:     08 Jul 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include <Model_ResultBody.h>
8 #include <Model_Data.h>
9 #include <Model_Document.h>
10 #include <ModelAPI_AttributeIntArray.h>
11 #include <TNaming_Builder.hxx>
12 #include <TNaming_NamedShape.hxx>
13 #include <TNaming_Iterator.hxx>
14 #include <TDataStd_Name.hxx>
15 #include <TDataStd_Integer.hxx>
16 #include <TopoDS.hxx>
17 #include <TopoDS_Face.hxx>
18 #include <TDF_ChildIterator.hxx>
19 #include <TopTools_MapOfShape.hxx>
20 #include <TopExp_Explorer.hxx>
21 #include <TopTools_ListOfShape.hxx>
22 #include <TopTools_ListIteratorOfListOfShape.hxx>
23 #include <TopTools_DataMapOfShapeListOfShape.hxx>
24 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
25 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
26 #include <TopTools_MapIteratorOfMapOfShape.hxx>
27 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
28 #include <TopTools_IndexedMapOfShape.hxx>
29 #include <TopTools_DataMapOfShapeShape.hxx>
30 #include <TopExp.hxx>
31 #include <BRepTools.hxx>
32 #include <BRep_Tool.hxx>
33 #include <GeomAPI_Shape.h>
34 #include <GeomAlgoAPI_MakeShape.h>
35 #include <Config_PropManager.h>
36 // DEB
37 //#include <TCollection_AsciiString.hxx>
38 //#include <TDF_Tool.hxx>
39 //#define DEB_IMPORT 1
40
41 Model_ResultBody::Model_ResultBody()
42 {
43   myIsDisabled = true; // by default it is not initialized and false to be after created
44   setIsConcealed(false);
45 }
46
47 void Model_ResultBody::initAttributes()
48 {
49   // append the color attribute. It is empty, the attribute will be filled by a request
50   DataPtr aData = data();
51   aData->addAttribute(COLOR_ID(), ModelAPI_AttributeIntArray::typeId());
52 }
53
54 void Model_ResultBody::colorConfigInfo(std::string& theSection, std::string& theName,
55   std::string& theDefault)
56 {
57   theSection = "Visualization";
58   theName = "result_body_color";
59   theDefault = DEFAULT_COLOR();
60 }
61
62 // Converts evolution of naming shape to selection evelution and back to avoid
63 // naming support on the disabled results. Deeply in the labels tree, recursively.
64 static void EvolutionToSelection(TDF_Label theLab, const bool theFlag) {
65   std::list<std::pair<TopoDS_Shape, TopoDS_Shape> > aShapePairs; // to store old and new shapes
66   Handle(TNaming_NamedShape) aName;
67   int anEvolution = -1;
68   if (theLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
69     TNaming_Evolution aNSEvol = aName->Evolution();
70     if ((aNSEvol == TNaming_SELECTED && theFlag) ||
71         (aNSEvol != TNaming_SELECTED && !theFlag)) { // nothing to do, it is already correct
72       return;
73     }
74     anEvolution = (int)(aNSEvol);
75     if (!theFlag) {
76       Handle(TDataStd_Integer) anAttrEvol;
77       if (theLab.FindAttribute(TDataStd_Integer::GetID(), anAttrEvol)) {
78         anEvolution = anAttrEvol->Get();
79       }
80     } else {
81       TDataStd_Integer::Set(theLab, anEvolution);
82     }
83
84     for(TNaming_Iterator anIter(aName); anIter.More(); anIter.Next()) {
85       aShapePairs.push_back(std::pair<TopoDS_Shape, TopoDS_Shape>
86         (anIter.OldShape(), anIter.NewShape()));
87     }
88   }
89   // create new
90   TNaming_Builder aBuilder(theLab);
91   TNaming_Evolution anEvol = (TNaming_Evolution)(anEvolution);
92   std::list<std::pair<TopoDS_Shape, TopoDS_Shape> >::iterator aPairsIter = aShapePairs.begin();
93   for(; aPairsIter != aShapePairs.end(); aPairsIter++) {
94     if (theFlag) { // disabled => make selection
95       aBuilder.Select(aPairsIter->first, aPairsIter->second);
96     } else if (anEvol == TNaming_GENERATED) {
97       aBuilder.Generated(aPairsIter->first, aPairsIter->second);
98     } else if (anEvol == TNaming_MODIFY) {
99       aBuilder.Modify(aPairsIter->first, aPairsIter->second);
100     } else if (anEvol == TNaming_DELETE) {
101       aBuilder.Delete(aPairsIter->first);
102     } else if (anEvol == TNaming_PRIMITIVE) {
103       aBuilder.Generated(aPairsIter->second);
104     }
105   }
106   // recursive call for all sub-labels
107   TDF_ChildIterator anIter(theLab, Standard_False);
108   for(; anIter.More(); anIter.Next()) {
109     EvolutionToSelection(anIter.Value(), theFlag);
110   }
111 }
112
113 bool Model_ResultBody::setDisabled(std::shared_ptr<ModelAPI_Result> theThis, const bool theFlag)
114 {
115   bool aChanged = ModelAPI_ResultBody::setDisabled(theThis, theFlag);
116   if (aChanged) { // state is changed, so modifications are needed
117     std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
118     if (!aData) // unknown case
119       return aChanged;
120     TDF_Label& aShapeLab = aData->shapeLab();
121     EvolutionToSelection(aShapeLab, theFlag);
122   }
123   return aChanged;
124 }
125
126 void Model_ResultBody::store(const std::shared_ptr<GeomAPI_Shape>& theShape)
127 {
128   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
129   if (aData) {
130     TDF_Label& aShapeLab = aData->shapeLab();
131     // clean builders
132     clean();   
133     // store the new shape as primitive
134     TNaming_Builder aBuilder(aShapeLab);
135     if (!theShape)
136       return;  // bad shape
137     TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
138     if (aShape.IsNull())
139       return;  // null shape inside
140
141     aBuilder.Generated(aShape); 
142     // register name
143     if(!aBuilder.NamedShape()->IsEmpty()) {
144       Handle(TDataStd_Name) anAttr;
145       if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
146         std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
147         if(!aName.empty()) {
148           std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
149           aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
150         }
151       }
152     }
153   }
154 }
155
156 void Model_ResultBody::storeGenerated(const std::shared_ptr<GeomAPI_Shape>& theFromShape,
157   const std::shared_ptr<GeomAPI_Shape>& theToShape)
158 {
159   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
160   if (aData) {
161     TDF_Label& aShapeLab = aData->shapeLab();
162     // clean builders
163     clean();   
164     // store the new shape as primitive
165     TNaming_Builder aBuilder(aShapeLab);
166     if (!theFromShape || !theToShape)
167       return;  // bad shape
168     TopoDS_Shape aShapeBasis = theFromShape->impl<TopoDS_Shape>();
169     if (aShapeBasis.IsNull())
170       return;  // null shape inside
171     TopoDS_Shape aShapeNew = theToShape->impl<TopoDS_Shape>();
172     if (aShapeNew.IsNull())
173       return;  // null shape inside
174     aBuilder.Generated(aShapeBasis, aShapeNew);
175     // register name
176     if(!aBuilder.NamedShape()->IsEmpty()) {
177       Handle(TDataStd_Name) anAttr;
178       if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
179         std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
180         if(!aName.empty()) {
181           std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
182           aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
183         }
184       }
185     }
186   }
187 }
188
189 void Model_ResultBody::storeModified(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
190   const std::shared_ptr<GeomAPI_Shape>& theNewShape, const int theDecomposeSolidsTag)
191 {
192   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
193   if (aData) {
194     TDF_Label& aShapeLab = aData->shapeLab();
195     // clean builders
196     clean();   
197     // store the new shape as primitive
198     TNaming_Builder aBuilder(aShapeLab);
199     if (!theOldShape || !theNewShape)
200       return;  // bad shape
201     TopoDS_Shape aShapeOld = theOldShape->impl<TopoDS_Shape>();
202     if (aShapeOld.IsNull())
203       return;  // null shape inside
204     TopoDS_Shape aShapeNew = theNewShape->impl<TopoDS_Shape>();
205     if (aShapeNew.IsNull())
206       return;  // null shape inside
207     aBuilder.Modify(aShapeOld, aShapeNew);
208     if (theDecomposeSolidsTag && aShapeNew.ShapeType() == TopAbs_COMPOUND) { // make sub elements as subs
209
210       // register name if it is possible
211       TCollection_AsciiString aName;
212       if(!aBuilder.NamedShape()->IsEmpty()) {
213         Handle(TDataStd_Name) anAttr;
214         if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
215           aName = TCollection_AsciiString(anAttr->Get()).ToCString();
216         }
217       }
218
219       TopoDS_Iterator aSubIter(aShapeNew);
220       for(int aTag = theDecomposeSolidsTag; aSubIter.More(); aSubIter.Next()) {
221         TNaming_Builder aSubBuilder(aShapeLab.FindChild(aTag++));
222         aSubBuilder.Generated(aSubIter.Value());
223         if(!aName.IsEmpty()) {
224           std::string aSolidName = 
225             (aName + "_Solid_" + TCollection_AsciiString(aTag - theDecomposeSolidsTag)).ToCString(); 
226           std::shared_ptr<Model_Document> aDoc = 
227             std::dynamic_pointer_cast<Model_Document>(document());
228           aDoc->addNamingName(aSubBuilder.NamedShape()->Label(), aSolidName);
229           TDataStd_Name::Set(aSubBuilder.NamedShape()->Label(), aSolidName.c_str());
230         }
231       }
232     }
233   }
234 }
235
236 std::shared_ptr<GeomAPI_Shape> Model_ResultBody::shape()
237 {
238   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
239   if (aData) {
240     TDF_Label& aShapeLab = aData->shapeLab();
241     Handle(TNaming_NamedShape) aName;
242     if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
243       TopoDS_Shape aShape = aName->Get();
244       if (!aShape.IsNull()) {
245         std::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
246         aRes->setImpl(new TopoDS_Shape(aShape));
247         return aRes;
248       }
249     }
250   }
251   return std::shared_ptr<GeomAPI_Shape>();
252 }
253
254 void Model_ResultBody::clean()
255 {
256   std::vector<TNaming_Builder*>::iterator aBuilder = myBuilders.begin();
257   for(; aBuilder != myBuilders.end(); aBuilder++)
258     delete *aBuilder;
259   myBuilders.clear();
260 }
261
262 Model_ResultBody::~Model_ResultBody()
263 {
264   clean();
265 }
266
267 TNaming_Builder* Model_ResultBody::builder(const int theTag)
268 {
269   if (myBuilders.size() <= (unsigned int)theTag) {
270     myBuilders.insert(myBuilders.end(), theTag - myBuilders.size() + 1, NULL);
271   }
272   if (!myBuilders[theTag]) {
273     std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
274     myBuilders[theTag] = new TNaming_Builder(aData->shapeLab().FindChild(theTag));
275     //TCollection_AsciiString entry;//
276     //TDF_Tool::Entry(aData->shapeLab().FindChild(theTag), entry);
277     //cout << "Label = " <<entry.ToCString() <<endl;
278   }
279   return myBuilders[theTag];
280 }
281
282 void Model_ResultBody::buildName(const int theTag, const std::string& theName)
283 {
284   std::string aName = data()->name() + "/" + theName; 
285   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
286   aDoc->addNamingName(builder(theTag)->NamedShape()->Label(), aName);
287   TDataStd_Name::Set(builder(theTag)->NamedShape()->Label(),aName.c_str());
288 }
289 void Model_ResultBody::generated(
290   const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
291 {
292   TopoDS_Shape aShape = theNewShape->impl<TopoDS_Shape>();
293   builder(theTag)->Generated(aShape);
294   if(!theName.empty()) 
295     buildName(theTag, theName);
296 }
297
298 void Model_ResultBody::generated(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
299   const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
300 {
301   TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
302   TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
303   builder(theTag)->Generated(anOldShape, aNewShape);
304   if(!theName.empty()) 
305     buildName(theTag, theName);
306 }
307
308
309 void Model_ResultBody::modified(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
310   const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
311 {
312   TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
313   TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
314   builder(theTag)->Modify(anOldShape, aNewShape);
315   if(!theName.empty()) 
316     buildName(theTag, theName);
317 }
318
319 void Model_ResultBody::deleted(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
320   const int theTag)
321 {
322   TopoDS_Shape aShape = theOldShape->impl<TopoDS_Shape>();
323   builder(theTag)->Delete(aShape);
324 }
325
326 void Model_ResultBody::loadDeletedShapes (GeomAlgoAPI_MakeShape* theMS,
327   std::shared_ptr<GeomAPI_Shape>  theShapeIn,
328   const int  theKindOfShape,
329   const int  theTag)
330 {
331   TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
332   TopTools_MapOfShape aView;
333   TopExp_Explorer ShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
334   for (; ShapeExplorer.More(); ShapeExplorer.Next ()) {
335     const TopoDS_Shape& aRoot = ShapeExplorer.Current ();
336     if (!aView.Add(aRoot)) continue;
337     std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
338     aRShape->setImpl((new TopoDS_Shape(aRoot)));
339     if (theMS->isDeleted (aRShape)) {
340       builder(theTag)->Delete(aRoot);
341     }
342   }
343 }
344
345 void Model_ResultBody::loadAndOrientModifiedShapes (
346   GeomAlgoAPI_MakeShape* theMS,
347   std::shared_ptr<GeomAPI_Shape>  theShapeIn,
348   const int  theKindOfShape,
349   const int  theTag,
350   const std::string& theName,
351   GeomAPI_DataMapOfShapeShape& theSubShapes)
352 {
353   TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
354   TopTools_MapOfShape aView;
355   bool isBuilt = theName.empty();
356   TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
357   for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
358     const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
359     if (!aView.Add(aRoot)) continue;
360     ListOfShape aList;
361     std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
362     aRShape->setImpl((new TopoDS_Shape(aRoot)));
363     theMS->modified(aRShape, aList);
364     std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = aList.begin(), aLast = aList.end();
365     for (; anIt != aLast; anIt++) {
366       TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();     
367       if (theSubShapes.isBound(*anIt)) {
368         std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
369         aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
370       }
371       if (!aRoot.IsSame (aNewShape)) {
372         builder(theTag)->Modify(aRoot,aNewShape);
373         if(!isBuilt) 
374           buildName(theTag, theName);           
375       }
376     }
377   }
378 }
379
380 void Model_ResultBody::loadAndOrientGeneratedShapes (
381   GeomAlgoAPI_MakeShape* theMS,
382   std::shared_ptr<GeomAPI_Shape>  theShapeIn,
383   const int  theKindOfShape,
384   const int  theTag,
385   const std::string& theName,
386   GeomAPI_DataMapOfShapeShape& theSubShapes)
387 {
388   TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
389   TopTools_MapOfShape aView;
390   bool isBuilt = theName.empty();
391   TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
392   for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
393     const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
394     if (!aView.Add(aRoot)) continue;
395     ListOfShape aList;
396     std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
397     aRShape->setImpl((new TopoDS_Shape(aRoot)));
398     theMS->generated(aRShape, aList);
399     std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = aList.begin(), aLast = aList.end();
400     for (; anIt != aLast; anIt++) {
401       TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();     
402       if (theSubShapes.isBound(*anIt)) {
403         std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
404         aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
405       }
406       if (!aRoot.IsSame (aNewShape)) {
407         builder(theTag)->Generated(aRoot,aNewShape);
408         if(!isBuilt) 
409           buildName(theTag, theName);   
410       }
411     }
412   }
413 }
414
415 //=======================================================================
416 int getDangleShapes(const TopoDS_Shape&           theShapeIn, 
417   const TopAbs_ShapeEnum        theGeneratedFrom,
418   TopTools_DataMapOfShapeShape& theDangles) 
419 {
420   theDangles.Clear();
421   TopTools_IndexedDataMapOfShapeListOfShape subShapeAndAncestors;
422   TopAbs_ShapeEnum GeneratedTo;
423   if (theGeneratedFrom == TopAbs_FACE) GeneratedTo = TopAbs_EDGE;
424   else if (theGeneratedFrom == TopAbs_EDGE) GeneratedTo = TopAbs_VERTEX;
425   else return Standard_False;
426   TopExp::MapShapesAndAncestors(theShapeIn, GeneratedTo, theGeneratedFrom, subShapeAndAncestors);
427   for (Standard_Integer i = 1; i <= subShapeAndAncestors.Extent(); i++) {
428     const TopoDS_Shape& mayBeDangle = subShapeAndAncestors.FindKey(i);
429     const TopTools_ListOfShape& ancestors = subShapeAndAncestors.FindFromIndex(i);
430     if (ancestors.Extent() == 1) theDangles.Bind(ancestors.First(), mayBeDangle);
431   }
432   return theDangles.Extent();
433 }
434
435 //=======================================================================
436 void loadGeneratedDangleShapes(
437   const TopoDS_Shape&      theShapeIn,
438   const TopAbs_ShapeEnum   theGeneratedFrom,
439   TNaming_Builder *        theBuilder)
440 {
441   TopTools_DataMapOfShapeShape dangles;
442   if (!getDangleShapes(theShapeIn, theGeneratedFrom, dangles)) return;
443   TopTools_DataMapIteratorOfDataMapOfShapeShape itr(dangles);
444   for (; itr.More(); itr.Next()) 
445     theBuilder->Generated(itr.Key(), itr.Value());
446 }
447
448 //=======================================================================
449 void Model_ResultBody::loadNextLevels(std::shared_ptr<GeomAPI_Shape> theShape, 
450   const std::string& theName, int&  theTag)
451 {
452   if(theShape->isNull()) return;
453   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();    
454   std::string aName;
455   if (aShape.ShapeType() == TopAbs_SOLID) {                 
456     TopExp_Explorer expl(aShape, TopAbs_FACE);
457     for (; expl.More(); expl.Next()) {  
458       builder(theTag)->Generated(expl.Current()); 
459       TCollection_AsciiString aStr(theTag);
460       aName = theName + aStr.ToCString();
461       buildName(theTag, aName);
462       theTag++;
463     }
464   }
465   else if (aShape.ShapeType() == TopAbs_SHELL || aShape.ShapeType() == TopAbs_FACE) {
466     // load faces and all the free edges
467     TopTools_IndexedMapOfShape Faces;
468     TopExp::MapShapes(aShape, TopAbs_FACE, Faces);
469     if (Faces.Extent() > 1 || (aShape.ShapeType() == TopAbs_SHELL && Faces.Extent() == 1)) {
470       TopExp_Explorer expl(aShape, TopAbs_FACE);
471       for (; expl.More(); expl.Next()) {
472         builder(theTag)->Generated(expl.Current());          
473         TCollection_AsciiString aStr(theTag);
474         aName = theName + aStr.ToCString();
475         buildName(theTag, aName);
476         theTag++;
477       }
478     }
479     TopTools_IndexedDataMapOfShapeListOfShape anEdgeAndNeighbourFaces;
480     TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, anEdgeAndNeighbourFaces);
481     for (Standard_Integer i = 1; i <= anEdgeAndNeighbourFaces.Extent(); i++) 
482     {
483       const TopTools_ListOfShape& aLL = anEdgeAndNeighbourFaces.FindFromIndex(i);
484       if (aLL.Extent() < 2) {
485         if (BRep_Tool::Degenerated(TopoDS::Edge(anEdgeAndNeighbourFaces.FindKey(i))))
486           continue;
487         builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
488         TCollection_AsciiString aStr(theTag);
489         aName = theName + aStr.ToCString();
490         buildName(theTag, aName);
491         theTag++;
492       } else {
493         TopTools_ListIteratorOfListOfShape anIter(aLL);
494         const TopoDS_Face& aFace = TopoDS::Face(anIter.Value());
495         anIter.Next();
496         if(aFace.IsEqual(anIter.Value())) {
497           builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
498           TCollection_AsciiString aStr(theTag);
499           aName = theName + aStr.ToCString();
500           buildName(theTag, aName);
501           theTag++;
502         }
503       }
504     }
505   } else if (aShape.ShapeType() == TopAbs_WIRE) {
506     TopTools_IndexedMapOfShape Edges;
507     BRepTools::Map3DEdges(aShape, Edges);
508     if (Edges.Extent() == 1) {
509       builder(++theTag)->Generated(Edges.FindKey(1));
510       TopExp_Explorer expl(aShape, TopAbs_VERTEX);
511       for (; expl.More(); expl.Next()) {
512         builder(theTag)->Generated(expl.Current());
513         TCollection_AsciiString aStr(theTag);
514         aName = theName + aStr.ToCString();
515         buildName(theTag, aName);
516         theTag++;
517       }
518     } else {
519       TopExp_Explorer expl(aShape, TopAbs_EDGE); 
520       for (; expl.More(); expl.Next()) {        
521         builder(theTag)->Generated(expl.Current());
522         TCollection_AsciiString aStr(theTag);
523         aName = theName + aStr.ToCString();
524         buildName(theTag, aName);
525         theTag++;
526       }   
527       // and load generated vertices.
528       TopTools_DataMapOfShapeShape generated;
529       if (getDangleShapes(aShape, TopAbs_EDGE, generated)) 
530       {
531         TNaming_Builder* pBuilder = builder(theTag++);
532         loadGeneratedDangleShapes(aShape, TopAbs_EDGE, pBuilder);  
533       }
534     }
535   } else if (aShape.ShapeType() == TopAbs_EDGE) {
536     TopExp_Explorer expl(aShape, TopAbs_VERTEX);
537     for (; expl.More(); expl.Next()) {      
538       builder(theTag)->Generated(expl.Current());
539       TCollection_AsciiString aStr(theTag);
540       aName = theName + aStr.ToCString();
541       buildName(theTag, aName);
542       theTag++;
543     }
544   }
545 }
546
547 //=======================================================================
548 int findAmbiguities(const TopoDS_Shape&           theShapeIn,                                   
549   TopTools_ListOfShape&   theList) 
550 {
551   int aNumEdges(0);
552   theList.Clear();
553   TopTools_IndexedDataMapOfShapeListOfShape subShapeAndAncestors;
554   TopAbs_ShapeEnum aTS(TopAbs_EDGE);
555   TopAbs_ShapeEnum aTA(TopAbs_FACE);
556   TopTools_MapOfShape aMap1, aMap2; // map1 - for edge ancestors; map2 - for keys => edges
557   TopTools_ListOfShape aKeyList;
558   TopExp::MapShapesAndAncestors(theShapeIn, aTS, aTA, subShapeAndAncestors);
559   for (Standard_Integer i = 1; i <= subShapeAndAncestors.Extent(); i++) {
560     const TopoDS_Shape& aKeyEdge1 = subShapeAndAncestors.FindKey(i);
561     const TopTools_ListOfShape& ancestors1 = subShapeAndAncestors.FindFromIndex(i);
562     aMap1.Clear();
563     TopTools_ListIteratorOfListOfShape it(ancestors1);
564     for(;it.More();it.Next()) aMap1.Add(it.Value()); // fill map with key ancestors => aKey1
565     for (Standard_Integer j = 1; j <= subShapeAndAncestors.Extent(); j++) {
566       if (i == j) continue;
567       const TopoDS_Shape& aKeyEdge2 = subShapeAndAncestors.FindKey(j);
568       const TopTools_ListOfShape& ancestors2 = subShapeAndAncestors.FindFromIndex(j);
569       if(ancestors1.Extent() == ancestors2.Extent() && ancestors1.Extent() > 1) {
570         int aNum (ancestors2.Extent());
571         TopTools_ListIteratorOfListOfShape it(ancestors2);
572         for(;it.More();it.Next()) 
573           if(aMap1.Contains(it.Value())) aNum--;
574         if(aNum == 0) {
575           if(aMap2.Add(aKeyEdge1)) 
576             aKeyList.Append(aKeyEdge1);
577           if(aMap2.Add(aKeyEdge2))
578             aKeyList.Append(aKeyEdge2);
579         }
580       }
581     } // at the end ==> List of edges to be named in addition   
582   }
583   aNumEdges = aKeyList.Extent();
584   if(aNumEdges)
585     theList.Assign(aKeyList);   
586   return aNumEdges; 
587 }
588
589 //=======================================================================
590 void Model_ResultBody::loadFirstLevel(
591   std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int&  theTag)
592 {
593   if(theShape->isNull()) return;
594   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>(); 
595   std::string aName;
596   if (aShape.ShapeType() == TopAbs_COMPOUND || aShape.ShapeType() == TopAbs_COMPSOLID) {
597     TopoDS_Iterator itr(aShape);
598     for (; itr.More(); itr.Next(),theTag++) {
599       builder(theTag)->Generated(itr.Value());
600       TCollection_AsciiString aStr(theTag);
601       aName = theName + aStr.ToCString();
602       buildName(theTag, aName);
603       if(!theName.empty()) buildName(theTag, aName);
604       if (itr.Value().ShapeType() == TopAbs_COMPOUND || 
605         itr.Value().ShapeType() == TopAbs_COMPSOLID) 
606       {
607         std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
608         itrShape->setImpl(new TopoDS_Shape(itr.Value()));
609         loadFirstLevel(itrShape, theName, theTag);
610       } else {
611         std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
612         itrShape->setImpl(new TopoDS_Shape(itr.Value()));               
613         loadNextLevels(itrShape, theName, theTag);
614       }
615     }
616   } else {
617     std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
618     itrShape->setImpl(new TopoDS_Shape(aShape));
619     loadNextLevels(itrShape, theName, theTag); 
620   }
621   TopTools_ListOfShape   aList;
622   if(findAmbiguities(aShape, aList)) {
623     TopTools_ListIteratorOfListOfShape it(aList);
624     for (; it.More(); it.Next(),theTag++) {
625       builder(theTag)->Generated(it.Value());
626       TCollection_AsciiString aStr(theTag);
627       aName = theName + aStr.ToCString();
628       buildName(theTag, aName);
629     }
630   }
631 }
632
633 //=======================================================================
634 void Model_ResultBody::loadDisconnectedEdges(
635   std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int&  theTag)
636 {
637   if(theShape->isNull()) return;
638   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();  
639   TopTools_DataMapOfShapeListOfShape edgeNaborFaces;
640   TopTools_ListOfShape empty;
641   TopExp_Explorer explF(aShape, TopAbs_FACE);
642   for (; explF.More(); explF.Next()) {
643     const TopoDS_Shape& aFace = explF.Current();
644     TopExp_Explorer explV(aFace, TopAbs_EDGE);
645     for (; explV.More(); explV.Next()) {
646       const TopoDS_Shape& anEdge = explV.Current();
647       if (!edgeNaborFaces.IsBound(anEdge)) edgeNaborFaces.Bind(anEdge, empty);
648       Standard_Boolean faceIsNew = Standard_True;
649       TopTools_ListIteratorOfListOfShape itrF(edgeNaborFaces.Find(anEdge));
650       for (; itrF.More(); itrF.Next()) {
651         if (itrF.Value().IsSame(aFace)) {
652           faceIsNew = Standard_False;
653           break;
654         }
655       }
656       if (faceIsNew) 
657         edgeNaborFaces.ChangeFind(anEdge).Append(aFace);      
658     }
659   }
660
661   /*  TopTools_IndexedDataMapOfShapeListOfShape aDM;
662   TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, aDM);
663   for(int i=1; i <= aDM.Extent(); i++) {
664   if(aDM.FindFromIndex(i).Extent() > 1) continue;
665   if (BRep_Tool::Degenerated(TopoDS::Edge(aDM.FindKey(i))))
666   continue;
667   builder(theTag)->Generated(aDM.FindKey(i));
668   TCollection_AsciiString aStr(theTag);
669   std::string aName = theName + aStr.ToCString();
670   buildName(theTag, aName);
671   #ifdef DEB_IMPORT
672   aName +=  + ".brep";
673   BRepTools::Write(aDM.FindKey(i), aName.c_str());
674   #endif
675   theTag++;
676   }
677   */
678   TopTools_MapOfShape anEdgesToDelete;
679   TopExp_Explorer anEx(aShape,TopAbs_EDGE); 
680   std::string aName;
681   for(;anEx.More();anEx.Next()) {
682     Standard_Boolean aC0 = Standard_False;
683     TopoDS_Shape anEdge1 = anEx.Current();
684     if (edgeNaborFaces.IsBound(anEdge1)) {
685       const TopTools_ListOfShape& aList1 = edgeNaborFaces.Find(anEdge1);
686       if (aList1.Extent()<2) continue;
687       TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(edgeNaborFaces);
688       for (; itr.More(); itr.Next()) {
689         TopoDS_Shape anEdge2 = itr.Key();
690         if(anEdgesToDelete.Contains(anEdge2)) continue;
691         if (anEdge1.IsSame(anEdge2)) continue;
692         const TopTools_ListOfShape& aList2 = itr.Value();
693         // compare lists of the neighbour faces of edge1 and edge2
694         if (aList1.Extent() == aList2.Extent()) {
695           Standard_Integer aMatches = 0;
696           for(TopTools_ListIteratorOfListOfShape aLIter1(aList1);aLIter1.More();aLIter1.Next())
697             for(TopTools_ListIteratorOfListOfShape aLIter2(aList2);aLIter2.More();aLIter2.Next())
698               if (aLIter1.Value().IsSame(aLIter2.Value())) aMatches++;
699           if (aMatches == aList1.Extent()) {
700             aC0=Standard_True;
701             builder(theTag)->Generated(anEdge2);
702             anEdgesToDelete.Add(anEdge2);
703             TCollection_AsciiString aStr(theTag);
704             aName = theName + aStr.ToCString();
705             buildName(theTag, aName);
706             theTag++;
707           }
708         }
709       }      
710       TopTools_MapIteratorOfMapOfShape itDelete(anEdgesToDelete);
711       for(;itDelete.More();itDelete.Next()) 
712         edgeNaborFaces.UnBind(itDelete.Key());      
713       edgeNaborFaces.UnBind(anEdge1);
714     }
715     if (aC0) {
716       builder(theTag)->Generated(anEdge1);
717       TCollection_AsciiString aStr(theTag);
718       aName = theName + aStr.ToCString();
719       buildName(theTag, aName);  
720       theTag++;
721     }
722   }  
723 }
724
725 void Model_ResultBody::loadDisconnectedVertexes(std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int&  theTag)
726 {
727   if(theShape->isNull()) return;
728   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();  
729   TopTools_DataMapOfShapeListOfShape vertexNaborEdges;
730   TopTools_ListOfShape empty;
731   TopExp_Explorer explF(aShape, TopAbs_EDGE);
732   for (; explF.More(); explF.Next()) {
733     const TopoDS_Shape& anEdge = explF.Current();
734     TopExp_Explorer explV(anEdge, TopAbs_VERTEX);
735     for (; explV.More(); explV.Next()) {
736       const TopoDS_Shape& aVertex = explV.Current();
737       if (!vertexNaborEdges.IsBound(aVertex)) vertexNaborEdges.Bind(aVertex, empty);
738       Standard_Boolean faceIsNew = Standard_True;
739       TopTools_ListIteratorOfListOfShape itrF(vertexNaborEdges.Find(aVertex));
740       for (; itrF.More(); itrF.Next()) {
741         if (itrF.Value().IsSame(anEdge)) {
742           faceIsNew = Standard_False;
743           break;
744         }
745       }
746       if (faceIsNew) {
747         vertexNaborEdges.ChangeFind(aVertex).Append(anEdge);
748       }
749     }
750   }
751   std::string aName;
752   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(vertexNaborEdges);
753   for (; itr.More(); itr.Next()) {
754     const TopTools_ListOfShape& naborEdges = itr.Value();
755     if (naborEdges.Extent() < 2) {              
756       builder(theTag)->Generated(itr.Key());
757       TCollection_AsciiString aStr(theTag);
758       aName = theName + aStr.ToCString();
759       buildName(theTag, aName);  
760       theTag++;
761     }
762   }
763 }