]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_BodyBuilder.cpp
Salome HOME
Update of the ExportToGEOM test
[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     if (theRoot.IsSame(aNewShape) || (theResultShape &&
389         (!theResultShape->isSubShape(*anIt, false) || theResultShape->isSame(*anIt)))) {
390       ListOfShape::iterator aRemoveIt = anIt++;
391       theShapes.erase(aRemoveIt);
392     } else {
393       GeomAPI_Shape::ShapeType aType = (*anIt)->shapeType();
394       if (aType < aKeepShapeType) {
395         // found a shape with lesser shape type => remove all previous shapes
396         aKeepShapeType = aType;
397         theShapes.erase(theShapes.begin(), anIt);
398         ++anIt;
399       } else if (aType > aKeepShapeType) {
400         // shapes with greater shape type should be removed from the list
401         ListOfShape::iterator aRemoveIt = anIt++;
402         theShapes.erase(aRemoveIt);
403       } else
404         ++anIt;
405     }
406   }
407 }
408
409 // returns an ancestor shape-type thaty used for naming-definition of the sub-type
410 TopAbs_ShapeEnum typeOfAncestor(const TopAbs_ShapeEnum theSubType) {
411   if (theSubType == TopAbs_VERTEX)
412     return TopAbs_EDGE;
413   if (theSubType == TopAbs_EDGE)
414     return TopAbs_FACE;
415   return TopAbs_VERTEX; // bad case
416 }
417
418 void Model_BodyBuilder::loadAndOrientModifiedShapes (
419   GeomAlgoAPI_MakeShape* theMS,
420   std::shared_ptr<GeomAPI_Shape>  theShapeIn,
421   const int  theKindOfShape,
422   const int  theTag,
423   const std::string& theName,
424   GeomAPI_DataMapOfShapeShape& theSubShapes,
425   const bool theIsStoreSeparate,
426   const bool theIsStoreAsGenerated)
427 {
428   int anIndex = 1;
429   int aTag = theTag;
430   bool isBuilt = !theName.empty();
431   std::string aName = theName;
432   std::ostringstream aStream;
433   GeomShapePtr aResultShape = shape();
434   TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
435   TopTools_MapOfShape aView;
436   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
437   TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
438   for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
439     const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
440     if (!aView.Add(aRoot)) continue;
441
442     bool aNotInTree =
443       TNaming_Tool::NamedShape(aRoot, aData->shapeLab()).IsNull();
444     if (aNotInTree && !theIsStoreSeparate) {
445       // there is no sense to write history if old shape does not exist in the document
446       continue; // but if it is stored separately, it will be builded as a primitive
447     }
448     ListOfShape aList;
449     std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
450     aRShape->setImpl((new TopoDS_Shape(aRoot)));
451     theMS->modified(aRShape, aList);
452     if (!theIsStoreSeparate)
453       keepTopLevelShapes(aList, aRoot, aResultShape);
454     // sort the list of images before naming
455     GeomAlgoAPI_SortListOfShapes::sort(aList);
456
457     // to trace situation where several objects are produced by one parent (#2317)
458     int aSameParentShapes = -1;
459     std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator
460       anIt = aList.begin(), aLast = aList.end();
461     for (; anIt != aLast; anIt++) {
462       TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
463       if (theSubShapes.isBound(*anIt)) {
464         std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
465         aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
466       }
467       GeomShapePtr aGeomNewShape(new GeomAPI_Shape());
468       aGeomNewShape->setImpl(new TopoDS_Shape(aNewShape));
469       if(!aRoot.IsSame(aNewShape) && aResultShape->isSubShape(aGeomNewShape, false) &&
470          !aResultShape->isSame(*anIt)) { // to avoid put of same shape on main label and sub
471         int aBuilderTag = aTag;
472         if (!theIsStoreSeparate) {
473           aSameParentShapes++;
474         } else if (aNotInTree) { // check this new shape can not be represented as
475           // a sub-shape of higher level sub-shapes
476           TopAbs_ShapeEnum aNewType = aNewShape.ShapeType();
477           TopAbs_ShapeEnum anAncestorType = typeOfAncestor(aNewType);
478           if (anAncestorType != TopAbs_VERTEX) {
479             bool aFound = false;
480             TopoDS_Shape aResultTShape = aResultShape->impl<TopoDS_Shape>();
481             TopExp_Explorer anAncestorExp(aResultTShape, anAncestorType);
482             for(; anAncestorExp.More() && !aFound; anAncestorExp.Next()) {
483               if (aResultTShape.IsSame(anAncestorExp.Current()))
484                 continue;
485               TopExp_Explorer aSubExp(anAncestorExp.Current(), aNewType);
486               for(; aSubExp.More(); aSubExp.Next()) {
487                 if (aNewShape.IsSame(aSubExp.Current())) {
488                   aFound = true;
489                   break;
490                 }
491               }
492             }
493             if (aFound) {
494               continue; // not need to store this shape in the BRep structure
495             }
496           }
497         }
498
499         static const int THE_ANCHOR_TAG = 100000;
500         int aCurShapeType = (int)((*anIt)->shapeType());
501         bool needSuffix = false; // suffix for the name based on the shape type
502         if (aSameParentShapes > 0) { // store in other label
503           aBuilderTag = THE_ANCHOR_TAG - aSameParentShapes * 10 - aCurShapeType;
504           needSuffix = true;
505         } else if (aCurShapeType != theKindOfShape) {
506           // modified shape has different type => set another tag
507           // to avoid shapes of different types on the same label
508           aBuilderTag = THE_ANCHOR_TAG - aCurShapeType;
509           needSuffix = true;
510         }
511         std::string aSuffix;
512         if (needSuffix) {
513           switch (aCurShapeType) {
514           case GeomAPI_Shape::VERTEX: aSuffix = "_v"; break;
515           case GeomAPI_Shape::EDGE:   aSuffix = "_e"; break;
516           case GeomAPI_Shape::FACE:   aSuffix = "_f"; break;
517           default: break;
518           }
519         }
520
521         if(theIsStoreAsGenerated) {
522           // Here we store shapes as generated, to avoid problem when one parent shape produce
523           // several child shapes. In this case naming could not determine which shape to select.
524           builder(aBuilderTag)->Generated(aRoot, aNewShape);
525         } else if (aNotInTree) {
526           // not in tree -> store as primitive (stored as separated)
527           builder(aBuilderTag)->Generated(aNewShape);
528         } else if (aNewShape.ShapeType() > aRoot.ShapeType()) {
529            // if lower-level type is produced, make it as generated
530           builder(aBuilderTag)->Generated(aRoot, aNewShape);
531         } else {
532           builder(aBuilderTag)->Modify(aRoot, aNewShape);
533         }
534         if(isBuilt) {
535           aStream.str(std::string());
536           aStream.clear();
537           aStream << theName;
538           if(theIsStoreSeparate)
539              aStream << "_" << anIndex++;
540
541           if (aSameParentShapes > 0) {
542             aStream.str(std::string());
543             aStream.clear();
544             aStream << aName << "_" << aSameParentShapes << "divided";
545           }
546
547           aStream << aSuffix;
548           buildName(aBuilderTag, aStream.str());
549         }
550         if(theIsStoreSeparate) {
551           aTag++;
552         }
553       } else if (aResultShape->isSame(*anIt)) {
554         // keep the modification evolution on the root level (2241 - history propagation issue)
555         if(theIsStoreAsGenerated) {
556           builder(0)->Generated(aRoot, aNewShape);
557         } else {
558           builder(0)->Modify(aRoot, aNewShape);
559         }
560       }
561     }
562   }
563 }
564
565 void Model_BodyBuilder::loadAndOrientGeneratedShapes (
566   GeomAlgoAPI_MakeShape* theMS,
567   std::shared_ptr<GeomAPI_Shape>  theShapeIn,
568   const int  theKindOfShape,
569   const int  theTag,
570   const std::string& theName,
571   GeomAPI_DataMapOfShapeShape& theSubShapes)
572 {
573   TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
574   TopTools_MapOfShape aView;
575   bool isBuilt = !theName.empty();
576   TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
577   for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
578     const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
579     if (!aView.Add(aRoot)) continue;
580     //if (TNaming_Tool::NamedShape(aRoot, builder(theTag)->NamedShape()->Label()).IsNull())
581     //  continue; // there is no sense to write history if old shape does not exist in the document
582     ListOfShape aList;
583     std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
584     aRShape->setImpl((new TopoDS_Shape(aRoot)));
585     theMS->generated(aRShape, aList);
586     keepTopLevelShapes(aList, aRoot);
587     std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator
588       anIt = aList.begin(), aLast = aList.end();
589     for (; anIt != aLast; anIt++) {
590       TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
591       if (theSubShapes.isBound(*anIt)) {
592         std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
593         aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
594       }
595       if (!aRoot.IsSame (aNewShape)) {
596         builder(theTag)->Generated(aRoot,aNewShape);
597         if(isBuilt)
598           buildName(theTag, theName);
599       }
600       TopAbs_ShapeEnum aGenShapeType = aNewShape.ShapeType();
601       if(aGenShapeType == TopAbs_WIRE || aGenShapeType == TopAbs_SHELL) {
602         TopAbs_ShapeEnum anExplodeShapeType =
603           aGenShapeType == TopAbs_WIRE ? TopAbs_EDGE : TopAbs_FACE;
604         const TDF_Label aLabel = builder(theTag)->NamedShape()->Label();
605         int aTag = 1;
606         std::shared_ptr<Model_Document> aDoc =
607           std::dynamic_pointer_cast<Model_Document>(document());
608         for(TopExp_Explorer anExp(aNewShape, anExplodeShapeType); anExp.More(); anExp.Next()) {
609           TDF_Label aChildLabel = aLabel.FindChild(aTag);
610           TNaming_Builder aBuilder(aChildLabel);
611           aBuilder.Generated(aRoot, anExp.Current());
612           TCollection_AsciiString aChildName =
613             TCollection_AsciiString((theName + "_").c_str()) + aTag;
614           TDataStd_Name::Set(aChildLabel, aChildName.ToCString());
615           aTag++;
616         }
617       }
618     }
619   }
620 }
621
622 //=======================================================================
623 int getDangleShapes(const TopoDS_Shape&           theShapeIn,
624   const TopAbs_ShapeEnum        theGeneratedFrom,
625   TopTools_DataMapOfShapeShape& theDangles)
626 {
627   theDangles.Clear();
628   TopTools_IndexedDataMapOfShapeListOfShape subShapeAndAncestors;
629   TopAbs_ShapeEnum GeneratedTo;
630   if (theGeneratedFrom == TopAbs_FACE) GeneratedTo = TopAbs_EDGE;
631   else if (theGeneratedFrom == TopAbs_EDGE) GeneratedTo = TopAbs_VERTEX;
632   else return Standard_False;
633   TopExp::MapShapesAndAncestors(theShapeIn, GeneratedTo, theGeneratedFrom, subShapeAndAncestors);
634   for (Standard_Integer i = 1; i <= subShapeAndAncestors.Extent(); i++) {
635     const TopoDS_Shape& mayBeDangle = subShapeAndAncestors.FindKey(i);
636     const TopTools_ListOfShape& ancestors = subShapeAndAncestors.FindFromIndex(i);
637     if (ancestors.Extent() == 1) theDangles.Bind(ancestors.First(), mayBeDangle);
638   }
639   return theDangles.Extent();
640 }
641
642 //=======================================================================
643 void loadGeneratedDangleShapes(
644   const TopoDS_Shape&      theShapeIn,
645   const TopAbs_ShapeEnum   theGeneratedFrom,
646   TNaming_Builder *        theBuilder)
647 {
648   TopTools_DataMapOfShapeShape dangles;
649   if (!getDangleShapes(theShapeIn, theGeneratedFrom, dangles)) return;
650   TopTools_DataMapIteratorOfDataMapOfShapeShape itr(dangles);
651   for (; itr.More(); itr.Next())
652     theBuilder->Generated(itr.Key(), itr.Value());
653 }
654
655 //=======================================================================
656 void Model_BodyBuilder::loadNextLevels(std::shared_ptr<GeomAPI_Shape> theShape,
657   const std::string& theName, int&  theTag)
658 {
659   if(theShape->isNull()) return;
660   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
661   std::string aName;
662   if (aShape.ShapeType() == TopAbs_SOLID) {
663     TopExp_Explorer expl(aShape, TopAbs_FACE);
664     for (; expl.More(); expl.Next()) {
665       builder(theTag)->Generated(expl.Current());
666       TCollection_AsciiString aStr(theTag);
667       aName = theName + aStr.ToCString();
668       buildName(theTag, aName);
669       theTag++;
670     }
671   }
672   else if (aShape.ShapeType() == TopAbs_SHELL || aShape.ShapeType() == TopAbs_FACE) {
673     // load faces and all the free edges
674     TopTools_IndexedMapOfShape Faces;
675     TopExp::MapShapes(aShape, TopAbs_FACE, Faces);
676     if (Faces.Extent() > 1 || (aShape.ShapeType() == TopAbs_SHELL && Faces.Extent() == 1)) {
677       TopExp_Explorer expl(aShape, TopAbs_FACE);
678       for (; expl.More(); expl.Next()) {
679         builder(theTag)->Generated(expl.Current());
680         TCollection_AsciiString aStr(theTag);
681         aName = theName + aStr.ToCString();
682         buildName(theTag, aName);
683         theTag++;
684       }
685     }
686     TopTools_IndexedDataMapOfShapeListOfShape anEdgeAndNeighbourFaces;
687     TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, anEdgeAndNeighbourFaces);
688     for (Standard_Integer i = 1; i <= anEdgeAndNeighbourFaces.Extent(); i++)
689     {
690       const TopTools_ListOfShape& aLL = anEdgeAndNeighbourFaces.FindFromIndex(i);
691       if (aLL.Extent() < 2) {
692         if (BRep_Tool::Degenerated(TopoDS::Edge(anEdgeAndNeighbourFaces.FindKey(i))))
693           continue;
694         builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
695         TCollection_AsciiString aStr(theTag);
696         aName = theName + aStr.ToCString();
697         buildName(theTag, aName);
698         theTag++;
699       } else {
700         TopTools_ListIteratorOfListOfShape anIter(aLL);
701         const TopoDS_Face& aFace = TopoDS::Face(anIter.Value());
702         anIter.Next();
703         if(aFace.IsEqual(anIter.Value())) {
704           builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
705           TCollection_AsciiString aStr(theTag);
706           aName = theName + aStr.ToCString();
707           buildName(theTag, aName);
708           theTag++;
709         }
710       }
711     }
712   } else if (aShape.ShapeType() == TopAbs_WIRE) {
713     TopTools_IndexedMapOfShape Edges;
714     BRepTools::Map3DEdges(aShape, Edges);
715     if (Edges.Extent() == 1) {
716       builder(theTag++)->Generated(Edges.FindKey(1));
717       TopExp_Explorer expl(aShape, TopAbs_VERTEX);
718       for (; expl.More(); expl.Next()) {
719         builder(theTag)->Generated(expl.Current());
720         TCollection_AsciiString aStr(theTag);
721         aName = theName + aStr.ToCString();
722         buildName(theTag, aName);
723         theTag++;
724       }
725     } else {
726       TopExp_Explorer expl(aShape, TopAbs_EDGE);
727       for (; expl.More(); expl.Next()) {
728         builder(theTag)->Generated(expl.Current());
729         TCollection_AsciiString aStr(theTag);
730         aName = theName + aStr.ToCString();
731         buildName(theTag, aName);
732         theTag++;
733       }
734       // and load generated vertices.
735       TopTools_DataMapOfShapeShape generated;
736       if (getDangleShapes(aShape, TopAbs_EDGE, generated))
737       {
738         TNaming_Builder* pBuilder = builder(theTag++);
739         loadGeneratedDangleShapes(aShape, TopAbs_EDGE, pBuilder);
740       }
741     }
742   } else if (aShape.ShapeType() == TopAbs_EDGE) {
743     TopExp_Explorer expl(aShape, TopAbs_VERTEX);
744     for (; expl.More(); expl.Next()) {
745       builder(theTag)->Generated(expl.Current());
746       TCollection_AsciiString aStr(theTag);
747       aName = theName + aStr.ToCString();
748       buildName(theTag, aName);
749       theTag++;
750     }
751   }
752 }
753
754 //=======================================================================
755 int findAmbiguities(const TopoDS_Shape&           theShapeIn,
756   TopTools_ListOfShape&   theList)
757 {
758   theList.Clear();
759   // edges -> ancestor faces list
760   TopTools_IndexedDataMapOfShapeListOfShape aSubShapeAndAncestors;
761   TopExp::MapShapesAndAncestors(theShapeIn, TopAbs_EDGE, TopAbs_FACE, aSubShapeAndAncestors);
762   // keeps the shapes which are already in the resulting list
763   TopTools_MapOfShape alreadyThere;
764   // map from faces identifier (combination of hash-codes) to list of edges produced such ID
765   NCollection_DataMap<int, NCollection_List<TopoDS_Shape> > aFacesIDs;
766
767   TopTools_IndexedDataMapOfShapeListOfShape::Iterator anAncestorsIter(aSubShapeAndAncestors);
768   for (; anAncestorsIter.More(); anAncestorsIter.Next()) {
769     const TopTools_ListOfShape& ancestors = anAncestorsIter.Value();
770     if (ancestors.Extent() < 2)
771       continue;
772     Standard_Integer anID = 0;
773     for(TopTools_ListIteratorOfListOfShape aFaceIt(ancestors); aFaceIt.More(); aFaceIt.Next()) {
774       anID ^= HashCode(aFaceIt.ChangeValue(), 1990657); // Pierpont prime
775     }
776     if (aFacesIDs.IsBound(anID)) { // there found same edge, check they really have same faces
777       const NCollection_List<TopoDS_Shape>& aSameFaces1 =
778         aSubShapeAndAncestors.FindFromKey(anAncestorsIter.Key());
779       NCollection_List<TopoDS_Shape>::Iterator aSameEdge(aFacesIDs.ChangeFind(anID));
780       for(; aSameEdge.More(); aSameEdge.Next()) {
781         const NCollection_List<TopoDS_Shape>& aSameFaces2 =
782           aSubShapeAndAncestors.FindFromKey(aSameEdge.Value());
783         if (aSameFaces2.Extent() != aSameFaces1.Extent()) // the number of faces is different
784           break;
785
786         NCollection_List<TopoDS_Shape>::Iterator aFaceIter1(aSameFaces1);
787         for(; aFaceIter1.More(); aFaceIter1.Next()) {
788           NCollection_List<TopoDS_Shape>::Iterator aFaceIter2(aSameFaces2);
789           for(; aFaceIter2.More(); aFaceIter2.Next()) {
790             if (aFaceIter1.Value().IsSame(aFaceIter2.Value()))
791               break;
792           }
793           if (!aFaceIter2.More()) // aFaceIter1 contains a face, which is not in aFaceIter2
794             break;
795         }
796         if (!aFaceIter1.More()) { // all the faces are same => put to the result
797           if (alreadyThere.Add(aSameEdge.Value()))
798             theList.Append(aSameEdge.Value());
799           if (alreadyThere.Add(anAncestorsIter.Key()))
800             theList.Append(anAncestorsIter.Key());
801         }
802       }
803     } else { // ID is unique, just add this edge
804       aFacesIDs.Bind(anID, NCollection_List<TopoDS_Shape>());
805     }
806     aFacesIDs.ChangeFind(anID).Append(anAncestorsIter.Key()); // add to the list anyway
807   }
808   return theList.Extent();
809 }
810
811 //=======================================================================
812 void Model_BodyBuilder::loadFirstLevel(
813   std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int&  theTag)
814 {
815   if(theShape->isNull()) return;
816   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
817   std::string aName;
818   if (aShape.ShapeType() == TopAbs_COMPOUND || aShape.ShapeType() == TopAbs_COMPSOLID) {
819     TopoDS_Iterator itr(aShape);
820     for (; itr.More(); itr.Next()) {
821       builder(theTag)->Generated(itr.Value());
822       TCollection_AsciiString aStr(theTag);
823       aName = theName + aStr.ToCString();
824       buildName(theTag, aName);
825       if(!theName.empty()) buildName(theTag, aName);
826       theTag++;
827       if (itr.Value().ShapeType() == TopAbs_COMPOUND ||
828         itr.Value().ShapeType() == TopAbs_COMPSOLID)
829       {
830         std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
831         itrShape->setImpl(new TopoDS_Shape(itr.Value()));
832         loadFirstLevel(itrShape, theName, theTag);
833       } else {
834         std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
835         itrShape->setImpl(new TopoDS_Shape(itr.Value()));
836         loadNextLevels(itrShape, theName, theTag);
837       }
838     }
839   } else {
840     std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
841     itrShape->setImpl(new TopoDS_Shape(aShape));
842     loadNextLevels(itrShape, theName, theTag);
843   }
844   TopTools_ListOfShape   aList;
845   if(findAmbiguities(aShape, aList)) {
846     TopTools_ListIteratorOfListOfShape it(aList);
847     for (; it.More(); it.Next(),theTag++) {
848       builder(theTag)->Generated(it.Value());
849       TCollection_AsciiString aStr(theTag);
850       aName = theName + aStr.ToCString();
851       buildName(theTag, aName);
852     }
853   }
854 }
855
856 //=======================================================================
857 void Model_BodyBuilder::loadDisconnectedEdges(
858   std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int&  theTag)
859 {
860   if(theShape->isNull()) return;
861   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
862   TopTools_DataMapOfShapeListOfShape edgeNaborFaces;
863   TopTools_ListOfShape empty;
864   TopExp_Explorer explF(aShape, TopAbs_FACE);
865   for (; explF.More(); explF.Next()) {
866     const TopoDS_Shape& aFace = explF.Current();
867     TopExp_Explorer explV(aFace, TopAbs_EDGE);
868     for (; explV.More(); explV.Next()) {
869       const TopoDS_Shape& anEdge = explV.Current();
870       if (!edgeNaborFaces.IsBound(anEdge)) edgeNaborFaces.Bind(anEdge, empty);
871       Standard_Boolean faceIsNew = Standard_True;
872       TopTools_ListIteratorOfListOfShape itrF(edgeNaborFaces.Find(anEdge));
873       for (; itrF.More(); itrF.Next()) {
874         if (itrF.Value().IsSame(aFace)) {
875           faceIsNew = Standard_False;
876           break;
877         }
878       }
879       if (faceIsNew)
880         edgeNaborFaces.ChangeFind(anEdge).Append(aFace);
881     }
882   }
883
884   TopTools_MapOfShape anEdgesToDelete;
885   TopExp_Explorer anEx(aShape,TopAbs_EDGE);
886   std::string aName;
887   for(;anEx.More();anEx.Next()) {
888     Standard_Boolean aC0 = Standard_False;
889     TopoDS_Shape anEdge1 = anEx.Current();
890     if (edgeNaborFaces.IsBound(anEdge1)) {
891       const TopTools_ListOfShape& aList1 = edgeNaborFaces.Find(anEdge1);
892       if (aList1.Extent()<2) continue;
893       TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(edgeNaborFaces);
894       for (; itr.More(); itr.Next()) {
895         TopoDS_Shape anEdge2 = itr.Key();
896         if(anEdgesToDelete.Contains(anEdge2)) continue;
897         if (anEdge1.IsSame(anEdge2)) continue;
898         const TopTools_ListOfShape& aList2 = itr.Value();
899         // compare lists of the neighbour faces of edge1 and edge2
900         if (aList1.Extent() == aList2.Extent()) {
901           Standard_Integer aMatches = 0;
902           for(TopTools_ListIteratorOfListOfShape aLIter1(aList1);aLIter1.More();aLIter1.Next())
903             for(TopTools_ListIteratorOfListOfShape aLIter2(aList2);aLIter2.More();aLIter2.Next())
904               if (aLIter1.Value().IsSame(aLIter2.Value())) aMatches++;
905           if (aMatches == aList1.Extent()) {
906             aC0=Standard_True;
907             builder(theTag)->Generated(anEdge2);
908             anEdgesToDelete.Add(anEdge2);
909             TCollection_AsciiString aStr(theTag);
910             aName = theName + aStr.ToCString();
911             buildName(theTag, aName);
912             theTag++;
913           }
914         }
915       }
916       TopTools_MapIteratorOfMapOfShape itDelete(anEdgesToDelete);
917       for(;itDelete.More();itDelete.Next())
918         edgeNaborFaces.UnBind(itDelete.Key());
919       edgeNaborFaces.UnBind(anEdge1);
920     }
921     if (aC0) {
922       builder(theTag)->Generated(anEdge1);
923       TCollection_AsciiString aStr(theTag);
924       aName = theName + aStr.ToCString();
925       buildName(theTag, aName);
926       theTag++;
927     }
928   }
929 }
930
931 void Model_BodyBuilder::loadDisconnectedVertexes(std::shared_ptr<GeomAPI_Shape> theShape,
932                                                  const std::string& theName, int&  theTag)
933 {
934   if(theShape->isNull()) return;
935   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
936   TopTools_DataMapOfShapeListOfShape vertexNaborEdges;
937   TopTools_ListOfShape empty;
938   TopExp_Explorer explF(aShape, TopAbs_EDGE);
939   for (; explF.More(); explF.Next()) {
940     const TopoDS_Shape& anEdge = explF.Current();
941     TopExp_Explorer explV(anEdge, TopAbs_VERTEX);
942     for (; explV.More(); explV.Next()) {
943       const TopoDS_Shape& aVertex = explV.Current();
944       if (!vertexNaborEdges.IsBound(aVertex)) vertexNaborEdges.Bind(aVertex, empty);
945       Standard_Boolean faceIsNew = Standard_True;
946       TopTools_ListIteratorOfListOfShape itrF(vertexNaborEdges.Find(aVertex));
947       for (; itrF.More(); itrF.Next()) {
948         if (itrF.Value().IsSame(anEdge)) {
949           faceIsNew = Standard_False;
950           break;
951         }
952       }
953       if (faceIsNew) {
954         vertexNaborEdges.ChangeFind(aVertex).Append(anEdge);
955       }
956     }
957   }
958   std::string aName;
959   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(vertexNaborEdges);
960   for (; itr.More(); itr.Next()) {
961     const TopTools_ListOfShape& naborEdges = itr.Value();
962     if (naborEdges.Extent() < 2) {
963       builder(theTag)->Generated(itr.Key());
964       TCollection_AsciiString aStr(theTag);
965       aName = theName + aStr.ToCString();
966       buildName(theTag, aName);
967       theTag++;
968     }
969   }
970 }
971
972 std::shared_ptr<GeomAPI_Shape> Model_BodyBuilder::shape()
973 {
974   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
975   if (aData) {
976     TDF_Label aShapeLab = aData->shapeLab();
977     Handle(TDF_Reference) aRef;
978     if (aShapeLab.FindAttribute(TDF_Reference::GetID(), aRef)) {
979       aShapeLab = aRef->Get();
980     }
981     Handle(TNaming_NamedShape) aName;
982     if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
983       TopoDS_Shape aShape = aName->Get();
984       if (!aShape.IsNull()) {
985         std::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
986         aRes->setImpl(new TopoDS_Shape(aShape));
987         return aRes;
988       }
989     }
990   }
991   return std::shared_ptr<GeomAPI_Shape>();
992 }
993
994 bool Model_BodyBuilder::isLatestEqual(const std::shared_ptr<GeomAPI_Shape>& theShape)
995 {
996   if (theShape.get()) {
997     TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
998     std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
999     if (aData) {
1000       TDF_Label& aShapeLab = aData->shapeLab();
1001       Handle(TNaming_NamedShape) aName;
1002       if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
1003         TopoDS_Shape aLatest = TNaming_Tool::CurrentShape(aName);
1004         if (aLatest.IsNull())
1005           return false;
1006         if (aLatest.IsEqual(aShape))
1007           return true;
1008         // check sub-shapes for comp-solids:
1009         for (TopExp_Explorer anExp(aShape, aLatest.ShapeType()); anExp.More(); anExp.Next()) {
1010           if (aLatest.IsEqual(anExp.Current()))
1011             return true;
1012         }
1013       }
1014     }
1015   }
1016   return false;
1017 }