Salome HOME
Useful commits from master and V8_5_0
[modules/shaper.git] / src / Model / Model_BodyBuilder.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <Model_BodyBuilder.h>
22
23 #include <Model_Data.h>
24 #include <Model_Document.h>
25 #include <TNaming_Builder.hxx>
26 #include <TNaming_NamedShape.hxx>
27 #include <TNaming_Iterator.hxx>
28 #include <TNaming_Tool.hxx>
29 #include <TDataStd_Name.hxx>
30 #include <TDataStd_Integer.hxx>
31 #include <TopoDS.hxx>
32 #include <TopoDS_Face.hxx>
33 #include <TDF_ChildIterator.hxx>
34 #include <TDF_ChildIDIterator.hxx>
35 #include <TDF_Reference.hxx>
36 #include <TopTools_MapOfShape.hxx>
37 #include <TopExp_Explorer.hxx>
38 #include <TopTools_ListOfShape.hxx>
39 #include <TopTools_ListIteratorOfListOfShape.hxx>
40 #include <TopTools_DataMapOfShapeListOfShape.hxx>
41 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
42 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
43 #include <TopTools_MapIteratorOfMapOfShape.hxx>
44 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
45 #include <TopTools_IndexedMapOfShape.hxx>
46 #include <TopTools_DataMapOfShapeShape.hxx>
47 #include <TopExp.hxx>
48 #include <BRepTools.hxx>
49 #include <BRep_Tool.hxx>
50 #include <GeomAPI_Shape.h>
51 #include <GeomAlgoAPI_MakeShape.h>
52 #include <GeomAlgoAPI_SortListOfShapes.h>
53 #include <Config_PropManager.h>
54 // DEB
55 //#include <TCollection_AsciiString.hxx>
56 //#include <TDF_Tool.hxx>
57 //#define DEB_IMPORT 1
58
59 Model_BodyBuilder::Model_BodyBuilder(ModelAPI_Object* theOwner)
60 : ModelAPI_BodyBuilder(theOwner)
61 {
62 }
63
64 // Converts evolution of naming shape to selection evelution and back to avoid
65 // naming support on the disabled results. Deeply in the labels tree, recursively.
66 static void evolutionToSelectionRec(TDF_Label theLab, const bool theFlag) {
67   std::list<std::pair<TopoDS_Shape, TopoDS_Shape> > aShapePairs; // to store old and new shapes
68   Handle(TNaming_NamedShape) aName;
69   int anEvolution = -1;
70   if (theLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
71     TNaming_Evolution aNSEvol = aName->Evolution();
72     if ((aNSEvol == TNaming_SELECTED && theFlag) ||
73         (aNSEvol != TNaming_SELECTED && !theFlag)) { // nothing to do, it is already correct
74       return;
75     }
76     anEvolution = (int)(aNSEvol);
77     if (!theFlag) {
78       Handle(TDataStd_Integer) anAttrEvol;
79       if (theLab.FindAttribute(TDataStd_Integer::GetID(), anAttrEvol)) {
80         anEvolution = anAttrEvol->Get();
81       }
82     } else {
83       TDataStd_Integer::Set(theLab, anEvolution);
84     }
85
86     for(TNaming_Iterator anIter(aName); anIter.More(); anIter.Next()) {
87       // iterator goes in reversed order relatively to the Builder, to, make the list reversed
88       aShapePairs.push_front(std::pair<TopoDS_Shape, TopoDS_Shape>
89         (anIter.OldShape(), anIter.NewShape()));
90     }
91
92     // create new
93     TNaming_Builder aBuilder(theLab);
94     TNaming_Evolution anEvol = (TNaming_Evolution)(anEvolution);
95     std::list<std::pair<TopoDS_Shape, TopoDS_Shape> >::iterator aPairsIter = aShapePairs.begin();
96     for(; aPairsIter != aShapePairs.end(); aPairsIter++) {
97       if (theFlag) { // disabled => make selection
98         if (anEvolution == TNaming_DELETE) // issue 2274 : don't put too many same null shapes
99           aBuilder.Select(aPairsIter->first, aPairsIter->first);
100         else if (anEvolution == TNaming_PRIMITIVE)
101           aBuilder.Select(aPairsIter->second, aPairsIter->second);
102         else
103           aBuilder.Select(aPairsIter->second, aPairsIter->first);
104       } else if (anEvol == TNaming_GENERATED) {
105         aBuilder.Generated(aPairsIter->first, aPairsIter->second);
106       } else if (anEvol == TNaming_MODIFY) {
107         aBuilder.Modify(aPairsIter->first, aPairsIter->second);
108       } else if (anEvol == TNaming_DELETE) {
109         aBuilder.Delete(aPairsIter->first);
110       } else if (anEvol == TNaming_PRIMITIVE) {
111         aBuilder.Generated(aPairsIter->second);
112       } else if (anEvol == TNaming_SELECTED) {
113         aBuilder.Select(aPairsIter->second, aPairsIter->first);
114       }
115     }
116   }
117   // recursive call for all sub-labels
118   TDF_ChildIterator anIter(theLab, Standard_False);
119   for(; anIter.More(); anIter.Next()) {
120     evolutionToSelectionRec(anIter.Value(), theFlag);
121   }
122 }
123
124 void Model_BodyBuilder::evolutionToSelection(const bool theFlag)
125 {
126   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
127   if (!aData) // unknown case
128     return;
129   TDF_Label& aShapeLab = aData->shapeLab();
130   evolutionToSelectionRec(aShapeLab, theFlag);
131 }
132
133 void Model_BodyBuilder::store(const std::shared_ptr<GeomAPI_Shape>& theShape,
134                               const bool theIsStoreSameShapes)
135 {
136   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
137   if (aData) {
138     TDF_Label& aShapeLab = aData->shapeLab();
139     // clean builders
140     clean();
141     // store the new shape as primitive
142     TNaming_Builder aBuilder(aShapeLab);
143     if (!theShape)
144       return;  // bad shape
145     TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
146     if (aShape.IsNull())
147       return;  // null shape inside
148
149     if(!theIsStoreSameShapes) {
150       Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aShape, aShapeLab);
151       if(!aNS.IsNull() && !aNS->IsEmpty()) {
152         // This shape is already in document, store reference instead of shape;
153         const TDF_Label aFoundLabel = aNS->Label();
154         TDF_Reference::Set(aShapeLab, aFoundLabel);
155         aShapeLab.ForgetAttribute(TNaming_NamedShape::GetID());
156         return;
157       }
158     }
159
160     aBuilder.Generated(aShape);
161     // register name
162     aShapeLab.ForgetAttribute(TDF_Reference::GetID());
163     if(!aBuilder.NamedShape()->IsEmpty()) {
164       Handle(TDataStd_Name) anAttr;
165       if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
166         std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
167         if(!aName.empty()) {
168           std::shared_ptr<Model_Document> aDoc =
169             std::dynamic_pointer_cast<Model_Document>(document());
170           aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
171         }
172       }
173     }
174   }
175 }
176
177 void Model_BodyBuilder::storeGenerated(const std::shared_ptr<GeomAPI_Shape>& theFromShape,
178   const std::shared_ptr<GeomAPI_Shape>& theToShape)
179 {
180   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
181   if (aData) {
182     TDF_Label& aShapeLab = aData->shapeLab();
183     // clean builders
184     clean();
185     // store the new shape as primitive
186     TNaming_Builder aBuilder(aShapeLab);
187     if (!theFromShape || !theToShape)
188       return;  // bad shape
189     TopoDS_Shape aShapeBasis = theFromShape->impl<TopoDS_Shape>();
190     if (aShapeBasis.IsNull())
191       return;  // null shape inside
192     TopoDS_Shape aShapeNew = theToShape->impl<TopoDS_Shape>();
193     if (aShapeNew.IsNull())
194       return;  // null shape inside
195     aBuilder.Generated(aShapeBasis, aShapeNew);
196     // register name
197     if(!aBuilder.NamedShape()->IsEmpty()) {
198       Handle(TDataStd_Name) anAttr;
199       if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
200         std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
201         if(!aName.empty()) {
202           std::shared_ptr<Model_Document> aDoc =
203             std::dynamic_pointer_cast<Model_Document>(document());
204           aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
205         }
206       }
207     }
208   }
209 }
210
211 void Model_BodyBuilder::storeModified(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
212   const std::shared_ptr<GeomAPI_Shape>& theNewShape, const int theDecomposeSolidsTag)
213 {
214   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
215   if (aData) {
216     TDF_Label& aShapeLab = aData->shapeLab();
217     // clean builders
218     if (theDecomposeSolidsTag != -2)
219       clean();
220     // store the new shape as primitive
221     TNaming_Builder aBuilder(aShapeLab);
222     if (!theOldShape || !theNewShape)
223       return;  // bad shape
224     TopoDS_Shape aShapeOld = theOldShape->impl<TopoDS_Shape>();
225     if (aShapeOld.IsNull())
226       return;  // null shape inside
227     TopoDS_Shape aShapeNew = theNewShape->impl<TopoDS_Shape>();
228     if (aShapeNew.IsNull())
229       return;  // null shape inside
230     aBuilder.Modify(aShapeOld, aShapeNew);
231     if(!aBuilder.NamedShape()->IsEmpty()) {
232       Handle(TDataStd_Name) anAttr;
233       if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
234         std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
235         if(!aName.empty()) {
236           std::shared_ptr<Model_Document> aDoc =
237             std::dynamic_pointer_cast<Model_Document>(document());
238           aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
239         }
240       }
241     }
242   }
243 }
244
245 void  Model_BodyBuilder::storeWithoutNaming(const std::shared_ptr<GeomAPI_Shape>& theShape)
246 {
247   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
248   if (aData) {
249     clean();
250     if (!theShape.get())
251       return; // bad shape
252     TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
253     if (aShape.IsNull())
254       return;  // null shape inside
255     TNaming_Builder aBuilder(aData->shapeLab());
256     aBuilder.Select(aShape, aShape);
257   }
258 }
259
260 void Model_BodyBuilder::clean()
261 {
262   TDF_Label aLab = std::dynamic_pointer_cast<Model_Data>(data())->shapeLab();
263   if (aLab.IsNull())
264     return;
265   std::map<int, TNaming_Builder*>::iterator aBuilder = myBuilders.begin();
266   for(; aBuilder != myBuilders.end(); aBuilder++) {
267     delete aBuilder->second;
268     // clear also shapes on cleaned sub-labels (#2241)
269     Handle(TNaming_NamedShape) aNS;
270     if (aLab.FindChild(aBuilder->first).FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
271       aNS->Clear();
272     }
273   }
274   myBuilders.clear();
275   // remove the old reference (if any)
276   aLab.ForgetAttribute(TDF_Reference::GetID());
277 }
278
279 Model_BodyBuilder::~Model_BodyBuilder()
280 {
281   clean();
282 }
283
284 TNaming_Builder* Model_BodyBuilder::builder(const int theTag)
285 {
286   std::map<int, TNaming_Builder*>::iterator aFind = myBuilders.find(theTag);
287   if (aFind == myBuilders.end()) {
288     std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
289     myBuilders[theTag] = new TNaming_Builder(
290       theTag == 0 ? aData->shapeLab() : aData->shapeLab().FindChild(theTag));
291     aFind = myBuilders.find(theTag);
292   }
293   return aFind->second;
294 }
295
296 void Model_BodyBuilder::buildName(const int theTag, const std::string& theName)
297 {
298   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
299   //aDoc->addNamingName(builder(theTag)->NamedShape()->Label(), theName);
300   TDataStd_Name::Set(builder(theTag)->NamedShape()->Label(), theName.c_str());
301 }
302 void Model_BodyBuilder::generated(
303   const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
304 {
305   TopoDS_Shape aShape = theNewShape->impl<TopoDS_Shape>();
306   builder(theTag)->Generated(aShape);
307   if(!theName.empty())
308     buildName(theTag, theName);
309 }
310
311 void Model_BodyBuilder::generated(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
312   const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
313 {
314   TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
315   TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
316   builder(theTag)->Generated(anOldShape, aNewShape);
317   if(!theName.empty())
318     buildName(theTag, theName);
319   TopAbs_ShapeEnum aGenShapeType = aNewShape.ShapeType();
320   if(aGenShapeType == TopAbs_WIRE || aGenShapeType == TopAbs_SHELL) {
321     TopAbs_ShapeEnum anExplodeShapeType = aGenShapeType == TopAbs_WIRE ? TopAbs_EDGE : TopAbs_FACE;
322     const TDF_Label aLabel = builder(theTag)->NamedShape()->Label();
323     int aTag = 1;
324     std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
325     for(TopExp_Explorer anExp(aNewShape, anExplodeShapeType); anExp.More(); anExp.Next()) {
326       TDF_Label aChildLabel = aLabel.FindChild(aTag);
327       TNaming_Builder aBuilder(aChildLabel);
328       aBuilder.Generated(anOldShape, anExp.Current());
329       TCollection_AsciiString aChildName = TCollection_AsciiString((theName + "_").c_str()) + aTag;
330       //aDoc->addNamingName(aChildLabel, aChildName.ToCString());
331       TDataStd_Name::Set(aChildLabel, aChildName.ToCString());
332       aTag++;
333     }
334   }
335 }
336
337
338 void Model_BodyBuilder::modified(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
339   const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
340 {
341   TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
342   TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
343   builder(theTag)->Modify(anOldShape, aNewShape);
344   if(!theName.empty())
345     buildName(theTag, theName);
346 }
347
348 void Model_BodyBuilder::deleted(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
349   const int theTag)
350 {
351   TopoDS_Shape aShape = theOldShape->impl<TopoDS_Shape>();
352   builder(theTag)->Delete(aShape);
353 }
354
355 void Model_BodyBuilder::loadDeletedShapes (GeomAlgoAPI_MakeShape* theMS,
356   std::shared_ptr<GeomAPI_Shape>  theShapeIn,
357   const int  theKindOfShape,
358   const int  theTag)
359 {
360   TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
361   TopTools_MapOfShape aView;
362   TopExp_Explorer ShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
363   GeomShapePtr aResultShape = shape();
364   for (; ShapeExplorer.More(); ShapeExplorer.Next ()) {
365     const TopoDS_Shape& aRoot = ShapeExplorer.Current ();
366     if (!aView.Add(aRoot)) continue;
367     std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
368     aRShape->setImpl((new TopoDS_Shape(aRoot)));
369     if (theMS->isDeleted (aRShape)) {
370       if (!aResultShape->isSubShape(aRShape, false)) {
371           ListOfShape aHist;
372           theMS->modified(aRShape, aHist);
373           if (aHist.size() == 0 || (aHist.size() == 1 && aHist.front()->isSame(aRShape)))
374             builder(theTag)->Delete(aRoot);
375       }
376     }
377   }
378 }
379
380 // Keep only the shapes with minimal shape type
381 static void keepTopLevelShapes(ListOfShape& theShapes, const TopoDS_Shape& theRoot,
382   const GeomShapePtr& theResultShape = GeomShapePtr())
383 {
384   GeomAPI_Shape::ShapeType aKeepShapeType = GeomAPI_Shape::SHAPE;
385   ListOfShape::iterator anIt = theShapes.begin();
386   while (anIt != theShapes.end()) {
387     TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
388     bool aSkip = aNewShape.IsNull() ||
389       (aNewShape.ShapeType() == TopAbs_EDGE && BRep_Tool::Degenerated(TopoDS::Edge(aNewShape)));
390     if (aSkip || theRoot.IsSame(aNewShape) || (theResultShape &&
391         (!theResultShape->isSubShape(*anIt, false) || theResultShape->isSame(*anIt)))) {
392       ListOfShape::iterator aRemoveIt = anIt++;
393       theShapes.erase(aRemoveIt);
394     } else {
395       GeomAPI_Shape::ShapeType aType = (*anIt)->shapeType();
396       if (aType < aKeepShapeType) {
397         // found a shape with lesser shape type => remove all previous shapes
398         aKeepShapeType = aType;
399         theShapes.erase(theShapes.begin(), anIt);
400         ++anIt;
401       } else if (aType > aKeepShapeType) {
402         // shapes with greater shape type should be removed from the list
403         ListOfShape::iterator aRemoveIt = anIt++;
404         theShapes.erase(aRemoveIt);
405       } else
406         ++anIt;
407     }
408   }
409 }
410
411 // returns an ancestor shape-type thaty used for naming-definition of the sub-type
412 TopAbs_ShapeEnum typeOfAncestor(const TopAbs_ShapeEnum theSubType) {
413   if (theSubType == TopAbs_VERTEX)
414     return TopAbs_EDGE;
415   if (theSubType == TopAbs_EDGE)
416     return TopAbs_FACE;
417   return TopAbs_VERTEX; // bad case
418 }
419
420 void Model_BodyBuilder::loadAndOrientModifiedShapes (
421   GeomAlgoAPI_MakeShape* theMS,
422   std::shared_ptr<GeomAPI_Shape>  theShapeIn,
423   const int  theKindOfShape,
424   const int  theTag,
425   const std::string& theName,
426   GeomAPI_DataMapOfShapeShape& theSubShapes,
427   const bool theIsStoreSeparate,
428   const bool theIsStoreAsGenerated)
429 {
430   int anIndex = 1;
431   int aTag = theTag;
432   bool isBuilt = !theName.empty();
433   std::string aName = theName;
434   std::ostringstream aStream;
435   GeomShapePtr aResultShape = shape();
436   TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
437   TopTools_MapOfShape aView;
438   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
439   TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
440   for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
441     const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
442     if (!aView.Add(aRoot)) continue;
443
444     bool aNotInTree =
445       TNaming_Tool::NamedShape(aRoot, aData->shapeLab()).IsNull();
446     if (aNotInTree && !theIsStoreSeparate) {
447       // there is no sense to write history if old shape does not exist in the document
448       continue; // but if it is stored separately, it will be builded as a primitive
449     }
450     ListOfShape aList;
451     std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
452     aRShape->setImpl((new TopoDS_Shape(aRoot)));
453     theMS->modified(aRShape, aList);
454     if (!theIsStoreSeparate)
455       keepTopLevelShapes(aList, aRoot, aResultShape);
456     // sort the list of images before naming
457     GeomAlgoAPI_SortListOfShapes::sort(aList);
458
459     // to trace situation where several objects are produced by one parent (#2317)
460     int aSameParentShapes = -1;
461     std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator
462       anIt = aList.begin(), aLast = aList.end();
463     for (; anIt != aLast; anIt++) {
464       TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
465       if (theSubShapes.isBound(*anIt)) {
466         std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
467         aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
468       }
469       GeomShapePtr aGeomNewShape(new GeomAPI_Shape());
470       aGeomNewShape->setImpl(new TopoDS_Shape(aNewShape));
471       if(!aRoot.IsSame(aNewShape) && aResultShape->isSubShape(aGeomNewShape, false) &&
472          !aResultShape->isSame(*anIt)) { // to avoid put of same shape on main label and sub
473         int aBuilderTag = aTag;
474         if (!theIsStoreSeparate) {
475           aSameParentShapes++;
476         } else if (aNotInTree) { // check this new shape can not be represented as
477           // a sub-shape of higher level sub-shapes
478           TopAbs_ShapeEnum aNewType = aNewShape.ShapeType();
479           TopAbs_ShapeEnum anAncestorType = typeOfAncestor(aNewType);
480           if (anAncestorType != TopAbs_VERTEX) {
481             bool aFound = false;
482             TopoDS_Shape aResultTShape = aResultShape->impl<TopoDS_Shape>();
483             TopExp_Explorer anAncestorExp(aResultTShape, anAncestorType);
484             for(; anAncestorExp.More() && !aFound; anAncestorExp.Next()) {
485               if (aResultTShape.IsSame(anAncestorExp.Current()))
486                 continue;
487               TopExp_Explorer aSubExp(anAncestorExp.Current(), aNewType);
488               for(; aSubExp.More(); aSubExp.Next()) {
489                 if (aNewShape.IsSame(aSubExp.Current())) {
490                   aFound = true;
491                   break;
492                 }
493               }
494             }
495             if (aFound) {
496               continue; // not need to store this shape in the BRep structure
497             }
498           }
499         }
500
501         static const int THE_ANCHOR_TAG = 100000;
502         int aCurShapeType = (int)((*anIt)->shapeType());
503         bool needSuffix = false; // suffix for the name based on the shape type
504         if (aSameParentShapes > 0) { // store in other label
505           aBuilderTag = THE_ANCHOR_TAG - aSameParentShapes * 10 - aCurShapeType;
506           needSuffix = true;
507         } else if (aCurShapeType != theKindOfShape) {
508           // modified shape has different type => set another tag
509           // to avoid shapes of different types on the same label
510           aBuilderTag = THE_ANCHOR_TAG - aCurShapeType;
511           needSuffix = true;
512         }
513         std::string aSuffix;
514         if (needSuffix) {
515           switch (aCurShapeType) {
516           case GeomAPI_Shape::VERTEX: aSuffix = "_v"; break;
517           case GeomAPI_Shape::EDGE:   aSuffix = "_e"; break;
518           case GeomAPI_Shape::FACE:   aSuffix = "_f"; break;
519           default: break;
520           }
521         }
522
523         if(theIsStoreAsGenerated) {
524           // Here we store shapes as generated, to avoid problem when one parent shape produce
525           // several child shapes. In this case naming could not determine which shape to select.
526           builder(aBuilderTag)->Generated(aRoot, aNewShape);
527         } else if (aNotInTree) {
528           // not in tree -> store as primitive (stored as separated)
529           builder(aBuilderTag)->Generated(aNewShape);
530         } else if (aNewShape.ShapeType() > aRoot.ShapeType()) {
531            // if lower-level type is produced, make it as generated
532           builder(aBuilderTag)->Generated(aRoot, aNewShape);
533         } else {
534           builder(aBuilderTag)->Modify(aRoot, aNewShape);
535         }
536         if(isBuilt) {
537           aStream.str(std::string());
538           aStream.clear();
539           aStream << theName;
540           if(theIsStoreSeparate)
541              aStream << "_" << anIndex++;
542
543           if (aSameParentShapes > 0) {
544             aStream.str(std::string());
545             aStream.clear();
546             aStream << aName << "_" << aSameParentShapes << "divided";
547           }
548
549           aStream << aSuffix;
550           buildName(aBuilderTag, aStream.str());
551         }
552         if(theIsStoreSeparate) {
553           aTag++;
554         }
555       } else if (aResultShape->isSame(*anIt)) {
556         // keep the modification evolution on the root level (2241 - history propagation issue)
557         if(theIsStoreAsGenerated) {
558           builder(0)->Generated(aRoot, aNewShape);
559         } else {
560           builder(0)->Modify(aRoot, aNewShape);
561         }
562       }
563     }
564   }
565 }
566
567 void Model_BodyBuilder::loadAndOrientGeneratedShapes (
568   GeomAlgoAPI_MakeShape* theMS,
569   std::shared_ptr<GeomAPI_Shape>  theShapeIn,
570   const int  theKindOfShape,
571   const int  theTag,
572   const std::string& theName,
573   GeomAPI_DataMapOfShapeShape& theSubShapes)
574 {
575   TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
576   TopTools_MapOfShape aView;
577   bool isBuilt = !theName.empty();
578   TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
579   for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
580     const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
581     if (!aView.Add(aRoot)) continue;
582     //if (TNaming_Tool::NamedShape(aRoot, builder(theTag)->NamedShape()->Label()).IsNull())
583     //  continue; // there is no sense to write history if old shape does not exist in the document
584     ListOfShape aList;
585     std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
586     aRShape->setImpl((new TopoDS_Shape(aRoot)));
587     theMS->generated(aRShape, aList);
588     keepTopLevelShapes(aList, aRoot);
589     std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator
590       anIt = aList.begin(), aLast = aList.end();
591     for (; anIt != aLast; anIt++) {
592       TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
593       if (theSubShapes.isBound(*anIt)) {
594         std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
595         aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
596       }
597       if (!aRoot.IsSame (aNewShape)) {
598         builder(theTag)->Generated(aRoot,aNewShape);
599         if(isBuilt)
600           buildName(theTag, theName);
601       }
602       TopAbs_ShapeEnum aGenShapeType = aNewShape.ShapeType();
603       if(aGenShapeType == TopAbs_WIRE || aGenShapeType == TopAbs_SHELL) {
604         TopAbs_ShapeEnum anExplodeShapeType =
605           aGenShapeType == TopAbs_WIRE ? TopAbs_EDGE : TopAbs_FACE;
606         const TDF_Label aLabel = builder(theTag)->NamedShape()->Label();
607         int aTag = 1;
608         std::shared_ptr<Model_Document> aDoc =
609           std::dynamic_pointer_cast<Model_Document>(document());
610         for(TopExp_Explorer anExp(aNewShape, anExplodeShapeType); anExp.More(); anExp.Next()) {
611           TDF_Label aChildLabel = aLabel.FindChild(aTag);
612           TNaming_Builder aBuilder(aChildLabel);
613           aBuilder.Generated(aRoot, anExp.Current());
614           TCollection_AsciiString aChildName =
615             TCollection_AsciiString((theName + "_").c_str()) + aTag;
616           TDataStd_Name::Set(aChildLabel, aChildName.ToCString());
617           aTag++;
618         }
619       }
620     }
621   }
622 }
623
624 //=======================================================================
625 int getDangleShapes(const TopoDS_Shape&           theShapeIn,
626   const TopAbs_ShapeEnum        theGeneratedFrom,
627   TopTools_DataMapOfShapeShape& theDangles)
628 {
629   theDangles.Clear();
630   TopTools_IndexedDataMapOfShapeListOfShape subShapeAndAncestors;
631   TopAbs_ShapeEnum GeneratedTo;
632   if (theGeneratedFrom == TopAbs_FACE) GeneratedTo = TopAbs_EDGE;
633   else if (theGeneratedFrom == TopAbs_EDGE) GeneratedTo = TopAbs_VERTEX;
634   else return Standard_False;
635   TopExp::MapShapesAndAncestors(theShapeIn, GeneratedTo, theGeneratedFrom, subShapeAndAncestors);
636   for (Standard_Integer i = 1; i <= subShapeAndAncestors.Extent(); i++) {
637     const TopoDS_Shape& mayBeDangle = subShapeAndAncestors.FindKey(i);
638     const TopTools_ListOfShape& ancestors = subShapeAndAncestors.FindFromIndex(i);
639     if (ancestors.Extent() == 1) theDangles.Bind(ancestors.First(), mayBeDangle);
640   }
641   return theDangles.Extent();
642 }
643
644 //=======================================================================
645 void loadGeneratedDangleShapes(
646   const TopoDS_Shape&      theShapeIn,
647   const TopAbs_ShapeEnum   theGeneratedFrom,
648   TNaming_Builder *        theBuilder)
649 {
650   TopTools_DataMapOfShapeShape dangles;
651   if (!getDangleShapes(theShapeIn, theGeneratedFrom, dangles)) return;
652   TopTools_DataMapIteratorOfDataMapOfShapeShape itr(dangles);
653   for (; itr.More(); itr.Next())
654     theBuilder->Generated(itr.Key(), itr.Value());
655 }
656
657 //=======================================================================
658 void Model_BodyBuilder::loadNextLevels(std::shared_ptr<GeomAPI_Shape> theShape,
659   const std::string& theName, int&  theTag)
660 {
661   if(theShape->isNull()) return;
662   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
663   std::string aName;
664   if (aShape.ShapeType() == TopAbs_SOLID) {
665     TopExp_Explorer expl(aShape, TopAbs_FACE);
666     for (; expl.More(); expl.Next()) {
667       builder(theTag)->Generated(expl.Current());
668       TCollection_AsciiString aStr(theTag);
669       aName = theName + aStr.ToCString();
670       buildName(theTag, aName);
671       theTag++;
672     }
673   }
674   else if (aShape.ShapeType() == TopAbs_SHELL || aShape.ShapeType() == TopAbs_FACE) {
675     // load faces and all the free edges
676     TopTools_IndexedMapOfShape Faces;
677     TopExp::MapShapes(aShape, TopAbs_FACE, Faces);
678     if (Faces.Extent() > 1 || (aShape.ShapeType() == TopAbs_SHELL && Faces.Extent() == 1)) {
679       TopExp_Explorer expl(aShape, TopAbs_FACE);
680       for (; expl.More(); expl.Next()) {
681         builder(theTag)->Generated(expl.Current());
682         TCollection_AsciiString aStr(theTag);
683         aName = theName + aStr.ToCString();
684         buildName(theTag, aName);
685         theTag++;
686       }
687     }
688     TopTools_IndexedDataMapOfShapeListOfShape anEdgeAndNeighbourFaces;
689     TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, anEdgeAndNeighbourFaces);
690     for (Standard_Integer i = 1; i <= anEdgeAndNeighbourFaces.Extent(); i++)
691     {
692       const TopTools_ListOfShape& aLL = anEdgeAndNeighbourFaces.FindFromIndex(i);
693       if (aLL.Extent() < 2) {
694         if (BRep_Tool::Degenerated(TopoDS::Edge(anEdgeAndNeighbourFaces.FindKey(i))))
695           continue;
696         builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
697         TCollection_AsciiString aStr(theTag);
698         aName = theName + aStr.ToCString();
699         buildName(theTag, aName);
700         theTag++;
701       } else {
702         TopTools_ListIteratorOfListOfShape anIter(aLL);
703         const TopoDS_Face& aFace = TopoDS::Face(anIter.Value());
704         anIter.Next();
705         if(aFace.IsEqual(anIter.Value())) {
706           builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
707           TCollection_AsciiString aStr(theTag);
708           aName = theName + aStr.ToCString();
709           buildName(theTag, aName);
710           theTag++;
711         }
712       }
713     }
714   } else if (aShape.ShapeType() == TopAbs_WIRE) {
715     TopTools_IndexedMapOfShape Edges;
716     BRepTools::Map3DEdges(aShape, Edges);
717     if (Edges.Extent() == 1) {
718       builder(theTag++)->Generated(Edges.FindKey(1));
719       TopExp_Explorer expl(aShape, TopAbs_VERTEX);
720       for (; expl.More(); expl.Next()) {
721         builder(theTag)->Generated(expl.Current());
722         TCollection_AsciiString aStr(theTag);
723         aName = theName + aStr.ToCString();
724         buildName(theTag, aName);
725         theTag++;
726       }
727     } else {
728       TopExp_Explorer expl(aShape, TopAbs_EDGE);
729       for (; expl.More(); expl.Next()) {
730         builder(theTag)->Generated(expl.Current());
731         TCollection_AsciiString aStr(theTag);
732         aName = theName + aStr.ToCString();
733         buildName(theTag, aName);
734         theTag++;
735       }
736       // and load generated vertices.
737       TopTools_DataMapOfShapeShape generated;
738       if (getDangleShapes(aShape, TopAbs_EDGE, generated))
739       {
740         TNaming_Builder* pBuilder = builder(theTag++);
741         loadGeneratedDangleShapes(aShape, TopAbs_EDGE, pBuilder);
742       }
743     }
744   } else if (aShape.ShapeType() == TopAbs_EDGE) {
745     TopExp_Explorer expl(aShape, TopAbs_VERTEX);
746     for (; expl.More(); expl.Next()) {
747       builder(theTag)->Generated(expl.Current());
748       TCollection_AsciiString aStr(theTag);
749       aName = theName + aStr.ToCString();
750       buildName(theTag, aName);
751       theTag++;
752     }
753   }
754 }
755
756 //=======================================================================
757 int findAmbiguities(const TopoDS_Shape&           theShapeIn,
758   TopTools_ListOfShape&   theList)
759 {
760   theList.Clear();
761   // edges -> ancestor faces list
762   TopTools_IndexedDataMapOfShapeListOfShape aSubShapeAndAncestors;
763   TopExp::MapShapesAndAncestors(theShapeIn, TopAbs_EDGE, TopAbs_FACE, aSubShapeAndAncestors);
764   // keeps the shapes which are already in the resulting list
765   TopTools_MapOfShape alreadyThere;
766   // map from faces identifier (combination of hash-codes) to list of edges produced such ID
767   NCollection_DataMap<int, NCollection_List<TopoDS_Shape> > aFacesIDs;
768
769   TopTools_IndexedDataMapOfShapeListOfShape::Iterator anAncestorsIter(aSubShapeAndAncestors);
770   for (; anAncestorsIter.More(); anAncestorsIter.Next()) {
771     const TopTools_ListOfShape& ancestors = anAncestorsIter.Value();
772     if (ancestors.Extent() < 2)
773       continue;
774     Standard_Integer anID = 0;
775     for(TopTools_ListIteratorOfListOfShape aFaceIt(ancestors); aFaceIt.More(); aFaceIt.Next()) {
776       anID ^= HashCode(aFaceIt.ChangeValue(), 1990657); // Pierpont prime
777     }
778     if (aFacesIDs.IsBound(anID)) { // there found same edge, check they really have same faces
779       const NCollection_List<TopoDS_Shape>& aSameFaces1 =
780         aSubShapeAndAncestors.FindFromKey(anAncestorsIter.Key());
781       NCollection_List<TopoDS_Shape>::Iterator aSameEdge(aFacesIDs.ChangeFind(anID));
782       for(; aSameEdge.More(); aSameEdge.Next()) {
783         const NCollection_List<TopoDS_Shape>& aSameFaces2 =
784           aSubShapeAndAncestors.FindFromKey(aSameEdge.Value());
785         if (aSameFaces2.Extent() != aSameFaces1.Extent()) // the number of faces is different
786           break;
787
788         NCollection_List<TopoDS_Shape>::Iterator aFaceIter1(aSameFaces1);
789         for(; aFaceIter1.More(); aFaceIter1.Next()) {
790           NCollection_List<TopoDS_Shape>::Iterator aFaceIter2(aSameFaces2);
791           for(; aFaceIter2.More(); aFaceIter2.Next()) {
792             if (aFaceIter1.Value().IsSame(aFaceIter2.Value()))
793               break;
794           }
795           if (!aFaceIter2.More()) // aFaceIter1 contains a face, which is not in aFaceIter2
796             break;
797         }
798         if (!aFaceIter1.More()) { // all the faces are same => put to the result
799           if (alreadyThere.Add(aSameEdge.Value()))
800             theList.Append(aSameEdge.Value());
801           if (alreadyThere.Add(anAncestorsIter.Key()))
802             theList.Append(anAncestorsIter.Key());
803         }
804       }
805     } else { // ID is unique, just add this edge
806       aFacesIDs.Bind(anID, NCollection_List<TopoDS_Shape>());
807     }
808     aFacesIDs.ChangeFind(anID).Append(anAncestorsIter.Key()); // add to the list anyway
809   }
810   return theList.Extent();
811 }
812
813 //=======================================================================
814 void Model_BodyBuilder::loadFirstLevel(
815   std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int&  theTag)
816 {
817   if(theShape->isNull()) return;
818   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
819   std::string aName;
820   if (aShape.ShapeType() == TopAbs_COMPOUND || aShape.ShapeType() == TopAbs_COMPSOLID) {
821     TopoDS_Iterator itr(aShape);
822     for (; itr.More(); itr.Next()) {
823       builder(theTag)->Generated(itr.Value());
824       TCollection_AsciiString aStr(theTag);
825       aName = theName + aStr.ToCString();
826       buildName(theTag, aName);
827       if(!theName.empty()) buildName(theTag, aName);
828       theTag++;
829       if (itr.Value().ShapeType() == TopAbs_COMPOUND ||
830         itr.Value().ShapeType() == TopAbs_COMPSOLID)
831       {
832         std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
833         itrShape->setImpl(new TopoDS_Shape(itr.Value()));
834         loadFirstLevel(itrShape, theName, theTag);
835       } else {
836         std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
837         itrShape->setImpl(new TopoDS_Shape(itr.Value()));
838         loadNextLevels(itrShape, theName, theTag);
839       }
840     }
841   } else {
842     std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
843     itrShape->setImpl(new TopoDS_Shape(aShape));
844     loadNextLevels(itrShape, theName, theTag);
845   }
846   TopTools_ListOfShape   aList;
847   if(findAmbiguities(aShape, aList)) {
848     TopTools_ListIteratorOfListOfShape it(aList);
849     for (; it.More(); it.Next(),theTag++) {
850       builder(theTag)->Generated(it.Value());
851       TCollection_AsciiString aStr(theTag);
852       aName = theName + aStr.ToCString();
853       buildName(theTag, aName);
854     }
855   }
856 }
857
858 //=======================================================================
859 void Model_BodyBuilder::loadDisconnectedEdges(
860   std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int&  theTag)
861 {
862   if(theShape->isNull()) return;
863   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
864   TopTools_DataMapOfShapeListOfShape edgeNaborFaces;
865   TopTools_ListOfShape empty;
866   TopExp_Explorer explF(aShape, TopAbs_FACE);
867   for (; explF.More(); explF.Next()) {
868     const TopoDS_Shape& aFace = explF.Current();
869     TopExp_Explorer explV(aFace, TopAbs_EDGE);
870     for (; explV.More(); explV.Next()) {
871       const TopoDS_Shape& anEdge = explV.Current();
872       if (!edgeNaborFaces.IsBound(anEdge)) edgeNaborFaces.Bind(anEdge, empty);
873       Standard_Boolean faceIsNew = Standard_True;
874       TopTools_ListIteratorOfListOfShape itrF(edgeNaborFaces.Find(anEdge));
875       for (; itrF.More(); itrF.Next()) {
876         if (itrF.Value().IsSame(aFace)) {
877           faceIsNew = Standard_False;
878           break;
879         }
880       }
881       if (faceIsNew)
882         edgeNaborFaces.ChangeFind(anEdge).Append(aFace);
883     }
884   }
885
886   TopTools_MapOfShape anEdgesToDelete;
887   TopExp_Explorer anEx(aShape,TopAbs_EDGE);
888   std::string aName;
889   for(;anEx.More();anEx.Next()) {
890     Standard_Boolean aC0 = Standard_False;
891     TopoDS_Shape anEdge1 = anEx.Current();
892     if (edgeNaborFaces.IsBound(anEdge1)) {
893       const TopTools_ListOfShape& aList1 = edgeNaborFaces.Find(anEdge1);
894       if (aList1.Extent()<2) continue;
895       TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(edgeNaborFaces);
896       for (; itr.More(); itr.Next()) {
897         TopoDS_Shape anEdge2 = itr.Key();
898         if(anEdgesToDelete.Contains(anEdge2)) continue;
899         if (anEdge1.IsSame(anEdge2)) continue;
900         const TopTools_ListOfShape& aList2 = itr.Value();
901         // compare lists of the neighbour faces of edge1 and edge2
902         if (aList1.Extent() == aList2.Extent()) {
903           Standard_Integer aMatches = 0;
904           for(TopTools_ListIteratorOfListOfShape aLIter1(aList1);aLIter1.More();aLIter1.Next())
905             for(TopTools_ListIteratorOfListOfShape aLIter2(aList2);aLIter2.More();aLIter2.Next())
906               if (aLIter1.Value().IsSame(aLIter2.Value())) aMatches++;
907           if (aMatches == aList1.Extent()) {
908             aC0=Standard_True;
909             builder(theTag)->Generated(anEdge2);
910             anEdgesToDelete.Add(anEdge2);
911             TCollection_AsciiString aStr(theTag);
912             aName = theName + aStr.ToCString();
913             buildName(theTag, aName);
914             theTag++;
915           }
916         }
917       }
918       TopTools_MapIteratorOfMapOfShape itDelete(anEdgesToDelete);
919       for(;itDelete.More();itDelete.Next())
920         edgeNaborFaces.UnBind(itDelete.Key());
921       edgeNaborFaces.UnBind(anEdge1);
922     }
923     if (aC0) {
924       builder(theTag)->Generated(anEdge1);
925       TCollection_AsciiString aStr(theTag);
926       aName = theName + aStr.ToCString();
927       buildName(theTag, aName);
928       theTag++;
929     }
930   }
931 }
932
933 void Model_BodyBuilder::loadDisconnectedVertexes(std::shared_ptr<GeomAPI_Shape> theShape,
934                                                  const std::string& theName, int&  theTag)
935 {
936   if(theShape->isNull()) return;
937   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
938   TopTools_DataMapOfShapeListOfShape vertexNaborEdges;
939   TopTools_ListOfShape empty;
940   TopExp_Explorer explF(aShape, TopAbs_EDGE);
941   for (; explF.More(); explF.Next()) {
942     const TopoDS_Shape& anEdge = explF.Current();
943     TopExp_Explorer explV(anEdge, TopAbs_VERTEX);
944     for (; explV.More(); explV.Next()) {
945       const TopoDS_Shape& aVertex = explV.Current();
946       if (!vertexNaborEdges.IsBound(aVertex)) vertexNaborEdges.Bind(aVertex, empty);
947       Standard_Boolean faceIsNew = Standard_True;
948       TopTools_ListIteratorOfListOfShape itrF(vertexNaborEdges.Find(aVertex));
949       for (; itrF.More(); itrF.Next()) {
950         if (itrF.Value().IsSame(anEdge)) {
951           faceIsNew = Standard_False;
952           break;
953         }
954       }
955       if (faceIsNew) {
956         vertexNaborEdges.ChangeFind(aVertex).Append(anEdge);
957       }
958     }
959   }
960   std::string aName;
961   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(vertexNaborEdges);
962   for (; itr.More(); itr.Next()) {
963     const TopTools_ListOfShape& naborEdges = itr.Value();
964     if (naborEdges.Extent() < 2) {
965       builder(theTag)->Generated(itr.Key());
966       TCollection_AsciiString aStr(theTag);
967       aName = theName + aStr.ToCString();
968       buildName(theTag, aName);
969       theTag++;
970     }
971   }
972 }
973
974 std::shared_ptr<GeomAPI_Shape> Model_BodyBuilder::shape()
975 {
976   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
977   if (aData) {
978     TDF_Label aShapeLab = aData->shapeLab();
979     Handle(TDF_Reference) aRef;
980     if (aShapeLab.FindAttribute(TDF_Reference::GetID(), aRef)) {
981       aShapeLab = aRef->Get();
982     }
983     Handle(TNaming_NamedShape) aName;
984     if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
985       TopoDS_Shape aShape = aName->Get();
986       if (!aShape.IsNull()) {
987         std::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
988         aRes->setImpl(new TopoDS_Shape(aShape));
989         return aRes;
990       }
991     }
992   }
993   return std::shared_ptr<GeomAPI_Shape>();
994 }
995
996 bool Model_BodyBuilder::isLatestEqual(const std::shared_ptr<GeomAPI_Shape>& theShape)
997 {
998   if (theShape.get()) {
999     TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
1000     std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
1001     if (aData) {
1002       TDF_Label& aShapeLab = aData->shapeLab();
1003       Handle(TNaming_NamedShape) aName;
1004       if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
1005         TopoDS_Shape aLatest = TNaming_Tool::CurrentShape(aName);
1006         if (aLatest.IsNull())
1007           return false;
1008         if (aLatest.IsEqual(aShape))
1009           return true;
1010         // check sub-shapes for comp-solids:
1011         for (TopExp_Explorer anExp(aShape, aLatest.ShapeType()); anExp.More(); anExp.Next()) {
1012           if (aLatest.IsEqual(anExp.Current()))
1013             return true;
1014         }
1015       }
1016     }
1017   }
1018   return false;
1019 }