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