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