Salome HOME
Merge branch 'Dev_0.7.1' of newgeom:newgeom into Dev_0.7.1
[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 void Model_ResultBody::loadFirstLevel(
440                      std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int&  theTag)
441 {
442   if(theShape->isNull()) return;
443   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>(); 
444   std::string aName;
445   if (aShape.ShapeType() == TopAbs_COMPOUND || aShape.ShapeType() == TopAbs_COMPSOLID) {
446     TopoDS_Iterator itr(aShape);
447     for (; itr.More(); itr.Next(),theTag++) {
448           builder(theTag)->Generated(itr.Value());
449           TCollection_AsciiString aStr(theTag);
450           aName = theName + aStr.ToCString();
451           buildName(theTag, aName);
452           if(!theName.empty()) buildName(theTag, aName);
453       if (itr.Value().ShapeType() == TopAbs_COMPOUND || 
454                   itr.Value().ShapeType() == TopAbs_COMPSOLID) 
455           {
456                 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
457         itrShape->setImpl(new TopoDS_Shape(itr.Value()));
458             loadFirstLevel(itrShape, theName, theTag);
459       } else {
460                 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
461         itrShape->setImpl(new TopoDS_Shape(itr.Value()));               
462                 loadNextLevels(itrShape, theName, theTag);
463           }
464     }
465   } else {
466     std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
467     itrShape->setImpl(new TopoDS_Shape(aShape));
468         loadNextLevels(itrShape, theName, theTag); 
469   }
470 }
471
472 //=======================================================================
473 void Model_ResultBody::loadDisconnectedEdges(
474                      std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int&  theTag)
475 {
476   if(theShape->isNull()) return;
477   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();  
478   TopTools_DataMapOfShapeListOfShape edgeNaborFaces;
479   TopTools_ListOfShape empty;
480   TopExp_Explorer explF(aShape, TopAbs_FACE);
481   for (; explF.More(); explF.Next()) {
482     const TopoDS_Shape& aFace = explF.Current();
483     TopExp_Explorer explV(aFace, TopAbs_EDGE);
484     for (; explV.More(); explV.Next()) {
485       const TopoDS_Shape& anEdge = explV.Current();
486       if (!edgeNaborFaces.IsBound(anEdge)) edgeNaborFaces.Bind(anEdge, empty);
487       Standard_Boolean faceIsNew = Standard_True;
488       TopTools_ListIteratorOfListOfShape itrF(edgeNaborFaces.Find(anEdge));
489       for (; itrF.More(); itrF.Next()) {
490             if (itrF.Value().IsSame(aFace)) {
491             faceIsNew = Standard_False;
492             break;
493                 }
494           }
495       if (faceIsNew) 
496             edgeNaborFaces.ChangeFind(anEdge).Append(aFace);      
497         }
498   }
499
500 /*  TopTools_IndexedDataMapOfShapeListOfShape aDM;
501   TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, aDM);
502   for(int i=1; i <= aDM.Extent(); i++) {
503         if(aDM.FindFromIndex(i).Extent() > 1) continue;
504         if (BRep_Tool::Degenerated(TopoDS::Edge(aDM.FindKey(i))))
505      continue;
506         builder(theTag)->Generated(aDM.FindKey(i));
507     TCollection_AsciiString aStr(theTag);
508         std::string aName = theName + aStr.ToCString();
509         buildName(theTag, aName);
510 #ifdef DEB_IMPORT
511         aName +=  + ".brep";
512         BRepTools::Write(aDM.FindKey(i), aName.c_str());
513 #endif
514         theTag++;
515   }
516 */
517   TopTools_MapOfShape anEdgesToDelete;
518   TopExp_Explorer anEx(aShape,TopAbs_EDGE); 
519   std::string aName;
520   for(;anEx.More();anEx.Next()) {
521     Standard_Boolean aC0 = Standard_False;
522     TopoDS_Shape anEdge1 = anEx.Current();
523     if (edgeNaborFaces.IsBound(anEdge1)) {
524       const TopTools_ListOfShape& aList1 = edgeNaborFaces.Find(anEdge1);
525       if (aList1.Extent()<2) continue;
526       TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(edgeNaborFaces);
527       for (; itr.More(); itr.Next()) {
528             TopoDS_Shape anEdge2 = itr.Key();
529             if(anEdgesToDelete.Contains(anEdge2)) continue;
530             if (anEdge1.IsSame(anEdge2)) continue;
531             const TopTools_ListOfShape& aList2 = itr.Value();
532             // compare lists of the neighbour faces of edge1 and edge2
533             if (aList1.Extent() == aList2.Extent()) {
534             Standard_Integer aMatches = 0;
535             for(TopTools_ListIteratorOfListOfShape aLIter1(aList1);aLIter1.More();aLIter1.Next())
536               for(TopTools_ListIteratorOfListOfShape aLIter2(aList2);aLIter2.More();aLIter2.Next())
537                 if (aLIter1.Value().IsSame(aLIter2.Value())) aMatches++;
538                 if (aMatches == aList1.Extent()) {
539                   aC0=Standard_True;
540                           builder(theTag)->Generated(anEdge2);
541                   anEdgesToDelete.Add(anEdge2);
542                           TCollection_AsciiString aStr(theTag);
543                           aName = theName + aStr.ToCString();
544                   buildName(theTag, aName);
545                           theTag++;
546                         }
547                 }
548           }      
549       TopTools_MapIteratorOfMapOfShape itDelete(anEdgesToDelete);
550       for(;itDelete.More();itDelete.Next()) 
551             edgeNaborFaces.UnBind(itDelete.Key());      
552       edgeNaborFaces.UnBind(anEdge1);
553         }
554     if (aC0) {
555           builder(theTag)->Generated(anEdge1);
556           TCollection_AsciiString aStr(theTag);
557           aName = theName + aStr.ToCString();
558           buildName(theTag, aName);      
559           theTag++;
560         }
561   }  
562 }
563
564 void Model_ResultBody::loadDisconnectedVertexes(std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int&  theTag)
565 {
566   if(theShape->isNull()) return;
567   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();  
568   TopTools_DataMapOfShapeListOfShape vertexNaborEdges;
569   TopTools_ListOfShape empty;
570   TopExp_Explorer explF(aShape, TopAbs_EDGE);
571   for (; explF.More(); explF.Next()) {
572     const TopoDS_Shape& anEdge = explF.Current();
573     TopExp_Explorer explV(anEdge, TopAbs_VERTEX);
574     for (; explV.More(); explV.Next()) {
575       const TopoDS_Shape& aVertex = explV.Current();
576       if (!vertexNaborEdges.IsBound(aVertex)) vertexNaborEdges.Bind(aVertex, empty);
577       Standard_Boolean faceIsNew = Standard_True;
578       TopTools_ListIteratorOfListOfShape itrF(vertexNaborEdges.Find(aVertex));
579       for (; itrF.More(); itrF.Next()) {
580         if (itrF.Value().IsSame(anEdge)) {
581           faceIsNew = Standard_False;
582           break;
583         }
584       }
585       if (faceIsNew) {
586         vertexNaborEdges.ChangeFind(aVertex).Append(anEdge);
587       }
588     }
589   }
590   std::string aName;
591   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(vertexNaborEdges);
592   for (; itr.More(); itr.Next()) {
593     const TopTools_ListOfShape& naborEdges = itr.Value();
594     if (naborEdges.Extent() < 2) {              
595                 builder(theTag)->Generated(itr.Key());
596                 TCollection_AsciiString aStr(theTag);
597             aName = theName + aStr.ToCString();
598             buildName(theTag, aName);    
599                 theTag++;
600         }
601   }
602 }