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