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