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