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