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