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