Salome HOME
Implementation of Open/Save and Break-Link functionality on SHAPER shapes
[modules/shaper.git] / src / Model / Model_BodyBuilder.cpp
1 // Copyright (C) 2014-2019  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include <Model_BodyBuilder.h>
21
22 #include <Model_Data.h>
23 #include <Model_Document.h>
24 #include <ModelAPI_Session.h>
25 #include <TNaming_Builder.hxx>
26 #include <TNaming_NamedShape.hxx>
27 #include <TNaming_Iterator.hxx>
28 #include <TNaming_Tool.hxx>
29 #include <TNaming_SameShapeIterator.hxx>
30 #include <TDataStd_Name.hxx>
31 #include <TDataStd_Integer.hxx>
32 #include <TDataStd_ExtStringList.hxx>
33 #include <TopoDS.hxx>
34 #include <TopoDS_Face.hxx>
35 #include <TDF_ChildIterator.hxx>
36 #include <TDF_ChildIDIterator.hxx>
37 #include <TDF_Reference.hxx>
38 #include <TDF_Tool.hxx>
39 #include <TopTools_MapOfShape.hxx>
40 #include <TopExp_Explorer.hxx>
41 #include <TopTools_ListOfShape.hxx>
42 #include <TopTools_ListIteratorOfListOfShape.hxx>
43 #include <TopTools_DataMapOfShapeListOfShape.hxx>
44 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
45 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
46 #include <TopTools_MapIteratorOfMapOfShape.hxx>
47 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
48 #include <TopTools_IndexedMapOfShape.hxx>
49 #include <TopTools_DataMapOfShapeShape.hxx>
50 #include <TopExp.hxx>
51 #include <BRepTools.hxx>
52 #include <BRep_Tool.hxx>
53 #include <BRepTools_History.hxx>
54 #include <GeomAPI_Shape.h>
55 #include <GeomAPI_ShapeExplorer.h>
56 #include <GeomAlgoAPI_MakeShape.h>
57 #include <GeomAlgoAPI_SortListOfShapes.h>
58 #include <Config_PropManager.h>
59 // DEB
60 //#include <TCollection_AsciiString.hxx>
61 //#define DEB_IMPORT 1
62
63 /// reference to the shape in external document: sting list attribute identifier
64 static const Standard_GUID kEXTERNAL_SHAPE_REF("9aa5dd14-6d34-4a8d-8786-05842fd7bbbd");
65
66 static const int INVALID_TAG            = -1;
67 static const int GENERATED_VERTICES_TAG = 1;
68 static const int GENERATED_EDGES_TAG    = 2;
69 static const int GENERATED_FACES_TAG    = 3;
70 static const int MODIFIED_VERTICES_TAG  = 4;
71 static const int MODIFIED_EDGES_TAG     = 5;
72 static const int MODIFIED_FACES_TAG     = 6;
73 static const int DELETED_TAG            = 7;
74 static const int PRIMITIVES_START_TAG   = 11;
75
76 static int getGenerationTag(const TopoDS_Shape& theShape) {
77   TopAbs_ShapeEnum aShapeType = theShape.ShapeType();
78   switch (aShapeType) {
79     case TopAbs_VERTEX: return GENERATED_VERTICES_TAG;
80     case TopAbs_EDGE:   return GENERATED_EDGES_TAG;
81     case TopAbs_FACE:   return GENERATED_FACES_TAG;
82   }
83
84   return INVALID_TAG;
85 }
86
87 static int getModificationTag(const TopoDS_Shape& theShape) {
88   TopAbs_ShapeEnum aShapeType = theShape.ShapeType();
89   switch (aShapeType) {
90     case TopAbs_VERTEX: return MODIFIED_VERTICES_TAG;
91     case TopAbs_EDGE:   return MODIFIED_EDGES_TAG;
92     case TopAbs_FACE:   return MODIFIED_FACES_TAG;
93   }
94
95   return INVALID_TAG;
96 }
97
98 static bool isAlreadyStored(const TNaming_Builder* theBuilder,
99                             const TopoDS_Shape& theOldShape,
100                             const TopoDS_Shape& theNewShape)
101 {
102   for (TNaming_Iterator aNamingIt(theBuilder->NamedShape());
103     aNamingIt.More();
104     aNamingIt.Next())
105   {
106     if (aNamingIt.NewShape().IsSame(theNewShape)
107       && aNamingIt.OldShape().IsSame(theOldShape))
108     {
109       return true;
110     }
111   }
112
113   return false;
114 }
115
116 Model_BodyBuilder::Model_BodyBuilder(ModelAPI_Object* theOwner)
117 : ModelAPI_BodyBuilder(theOwner),
118   myFreePrimitiveTag(PRIMITIVES_START_TAG)
119 {
120 }
121
122 /// Checks that shape is presented in the tree with not-selection evolution
123 /// In theOriginalLabel it returns label where NS of old sub-shape is stored
124 static bool isShapeInTree(const TDF_Label& theAccess1, const TDF_Label& theAccess2,
125   TopoDS_Shape theShape, TDF_Label& theOriginalLabel)
126 {
127   bool aResult = TNaming_Tool::HasLabel(theAccess1, theShape);
128   if (aResult) { //check evolution and a label of this shape
129     for(TNaming_SameShapeIterator aShapes(theShape, theAccess1); aShapes.More(); aShapes.Next())
130     {
131       static Handle(TNaming_NamedShape) aNS;
132       if (aShapes.Label().FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
133         if (aNS->Evolution() != TNaming_SELECTED) {
134           theOriginalLabel = aNS->Label();
135           return true;
136         }
137       }
138     }
139   }
140   if (!theAccess2.IsNull()) {
141     static const TDF_Label anEmpty;
142     return isShapeInTree(theAccess2, anEmpty, theShape, theOriginalLabel);
143   }
144   return false;
145 }
146
147 /// Stores entry to the external label in the entries list at this label
148 static void storeExternalReference(const TDF_Label& theExternal, const TDF_Label theThis)
149 {
150   // store information about the external document reference to restore old shape on open
151   if (!theExternal.IsNull() && !theExternal.Root().IsEqual(theThis.Root())) {
152     Handle(TDataStd_ExtStringList) anEntries;
153     if (!theThis.FindAttribute(kEXTERNAL_SHAPE_REF, anEntries)) {
154       anEntries = TDataStd_ExtStringList::Set(theThis, kEXTERNAL_SHAPE_REF);
155     }
156     TCollection_AsciiString anEntry;
157     TDF_Tool::Entry(theExternal, anEntry);
158     // check it already contains this entry
159     TDataStd_ListOfExtendedString::Iterator anIter(anEntries->List());
160     for(; anIter.More(); anIter.Next())
161       if (anIter.Value() == anEntry)
162         break;
163     if (!anIter.More()) {
164       anEntries->Append(anEntry);
165     }
166   }
167 }
168
169 void Model_BodyBuilder::store(const GeomShapePtr& theShape,
170                               const bool theIsStoreSameShapes)
171 {
172   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
173   if (aData) {
174     TDF_Label aShapeLab = aData->shapeLab();
175     // clean builders
176     clean();
177     // store the new shape as primitive
178     TNaming_Builder aBuilder(aShapeLab);
179     if (!theShape)
180       return;  // bad shape
181     TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
182     if (aShape.IsNull())
183       return;  // null shape inside
184
185     if(!theIsStoreSameShapes) {
186       Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aShape, aShapeLab);
187       // the last condition is for the issue 2751 : existing shape may be found in compound-NS
188       if(!aNS.IsNull() && !aNS->IsEmpty() && aNS->Get().IsSame(aShape)) {
189         // This shape is already in document, store reference instead of shape;
190         const TDF_Label aFoundLabel = aNS->Label();
191         TDF_Reference::Set(aShapeLab, aFoundLabel);
192         aShapeLab.ForgetAttribute(TNaming_NamedShape::GetID());
193         return;
194       }
195     }
196
197     aBuilder.Generated(aShape);
198     // register name
199     aShapeLab.ForgetAttribute(TDF_Reference::GetID());
200     if(!aBuilder.NamedShape()->IsEmpty()) {
201       Handle(TDataStd_Name) anAttr;
202       if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
203         std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
204         if(!aName.empty()) {
205           std::shared_ptr<Model_Document> aDoc =
206             std::dynamic_pointer_cast<Model_Document>(document());
207           aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
208         }
209       }
210     }
211   }
212 }
213
214 void Model_BodyBuilder::storeGenerated(const GeomShapePtr& theFromShape,
215   const GeomShapePtr& theToShape, const bool theIsCleanStored)
216 {
217   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
218   if (aData) {
219     TDF_Label aShapeLab = aData->shapeLab();
220     // clean builders
221     if (theIsCleanStored)
222       clean();
223     TNaming_Builder* aBuilder = builder(0);
224     if (!theFromShape || !theToShape)
225       return;  // bad shape
226     TopoDS_Shape aShapeBasis = theFromShape->impl<TopoDS_Shape>();
227     if (aShapeBasis.IsNull())
228       return;  // null shape inside
229     TopoDS_Shape aShapeNew = theToShape->impl<TopoDS_Shape>();
230     if (aShapeNew.IsNull())
231       return;  // null shape inside
232
233     // There is no sense to write history if old shape does not exist in the document.
234     TDF_Label anAccess2 = std::dynamic_pointer_cast<Model_Document>(
235       ModelAPI_Session::get()->moduleDocument())->generalLabel();
236     TDF_Label anOriginalLabel;
237     if (!isShapeInTree(aData->shapeLab(), anAccess2, aShapeBasis, anOriginalLabel)) {
238       if (aBuilder->NamedShape()->Get().IsNull()) { // store as primitive if alone anyway
239         aBuilder->Generated(aShapeNew);
240       }
241     } else {
242       if (aBuilder->NamedShape()->Evolution() == TNaming_PRIMITIVE) { // erase primitive before
243         myBuilders.erase(0);
244         aBuilder = builder(0);
245       }
246
247       aBuilder->Generated(aShapeBasis, aShapeNew);
248       // store information about the external document reference to restore old shape on open
249       storeExternalReference(anOriginalLabel, aBuilder->NamedShape()->Label());
250     }
251
252     // register name
253     if(!aBuilder->NamedShape()->IsEmpty()) {
254       Handle(TDataStd_Name) anAttr;
255       if(aBuilder->NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
256         std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
257         if(!aName.empty()) {
258           std::shared_ptr<Model_Document> aDoc =
259             std::dynamic_pointer_cast<Model_Document>(document());
260           aDoc->addNamingName(aBuilder->NamedShape()->Label(), aName);
261         }
262       }
263     }
264   }
265 }
266
267 void Model_BodyBuilder::storeGenerated(const std::list<GeomShapePtr>& theFromShapes,
268   const GeomShapePtr& theToShape, const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
269 {
270   bool aStored = false;
271   std::list<GeomShapePtr>::const_iterator anOldIter = theFromShapes.cbegin();
272   for (; anOldIter != theFromShapes.cend(); anOldIter++) {
273     bool aStore = (*anOldIter)->isCompound() || (*anOldIter)->isShell() || (*anOldIter)->isWire();
274     if (!aStore) {
275       ListOfShape aNews; // check this old really generates theToShape
276       theMakeShape->generated(*anOldIter, aNews);
277       ListOfShape::iterator aNewIter = aNews.begin();
278       for (; aNewIter != aNews.end(); aNewIter++) {
279         if (theToShape->isSame(*aNewIter))
280           break;
281       }
282       aStore = aNewIter != aNews.end();
283     }
284     if (aStore) {
285       storeGenerated(*anOldIter, theToShape, !aStored);
286       TNaming_Builder* aBuilder = builder(0);
287       aStored = !aBuilder->NamedShape()->IsEmpty();
288     }
289   }
290   if (!aStored) { // store as PRIMITIVE, but clean in any way
291     store(theToShape);
292     return;
293   }
294 }
295
296 TNaming_Builder* Model_BodyBuilder::builder(const int theTag)
297 {
298   std::map<int, TNaming_Builder*>::iterator aFind = myBuilders.find(theTag);
299   if (aFind == myBuilders.end()) {
300     std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
301     myBuilders[theTag] = new TNaming_Builder(
302       theTag == 0 ? aData->shapeLab() : aData->shapeLab().FindChild(theTag));
303     aFind = myBuilders.find(theTag);
304   }
305   return aFind->second;
306 }
307
308 void Model_BodyBuilder::storeModified(const GeomShapePtr& theOldShape,
309                                       const GeomShapePtr& theNewShape,
310                                       const bool theIsCleanStored)
311 {
312   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
313   if (aData) {
314     // clean builders
315     if (theIsCleanStored) clean();
316     TNaming_Builder* aBuilder = builder(0);
317     if (!theOldShape || !theNewShape)
318       return;  // bad shape
319     TopoDS_Shape aShapeOld = theOldShape->impl<TopoDS_Shape>();
320     if (aShapeOld.IsNull())
321       return;  // null shape inside
322     TopoDS_Shape aShapeNew = theNewShape->impl<TopoDS_Shape>();
323     if (aShapeNew.IsNull())
324       return;  // null shape inside
325
326     // There is no sense to write history if old shape does not exist in the document.
327     TDF_Label anAccess2 = std::dynamic_pointer_cast<Model_Document>(
328       ModelAPI_Session::get()->moduleDocument())->generalLabel();
329     TDF_Label anOriginalLabel;
330     if (!isShapeInTree(aData->shapeLab(), anAccess2, aShapeOld, anOriginalLabel)) {
331       if (aBuilder->NamedShape()->Get().IsNull()) { // store as primitive if alone anyway
332         aBuilder->Generated(aShapeNew);
333       }
334     } else {
335       if (aBuilder->NamedShape()->Evolution() == TNaming_PRIMITIVE) { // erase primitive before
336         myBuilders.erase(0);
337         aBuilder = builder(0);
338       }
339
340       aBuilder->Modify(aShapeOld, aShapeNew);
341       // store information about the external document reference to restore old shape on open
342       storeExternalReference(anOriginalLabel, aBuilder->NamedShape()->Label());
343     }
344
345     if(!aBuilder->NamedShape()->IsEmpty()) {
346       Handle(TDataStd_Name) anAttr;
347       if(aBuilder->NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(), anAttr)) {
348         std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
349         if(!aName.empty()) {
350           std::shared_ptr<Model_Document> aDoc =
351             std::dynamic_pointer_cast<Model_Document>(document());
352           aDoc->addNamingName(aBuilder->NamedShape()->Label(), aName);
353         }
354       }
355     }
356   }
357 }
358
359 void Model_BodyBuilder::storeModified(const std::list<GeomShapePtr>& theOldShapes,
360   const GeomShapePtr& theNewShape, const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
361 {
362   bool aStored = false;
363   std::list<GeomShapePtr>::const_iterator anOldIter = theOldShapes.cbegin();
364   for(; anOldIter != theOldShapes.cend(); anOldIter++) {
365     // compounds may cause crash if call "modified"
366     bool aStore = (*anOldIter)->isCompound() || (*anOldIter)->isShell() || (*anOldIter)->isWire();
367     if (!aStore) {
368       ListOfShape aNews; // check this old really modifies theNewShape
369       theMakeShape->modified(*anOldIter, aNews);
370       ListOfShape::iterator aNewIter = aNews.begin();
371       for(; aNewIter != aNews.end(); aNewIter++) {
372         if (theNewShape->isSame(*aNewIter))
373           break;
374       }
375       aStore = aNewIter != aNews.end();
376     }
377     if (aStore) {
378       storeModified(*anOldIter, theNewShape, !aStored);
379       TNaming_Builder* aBuilder = builder(0);
380       aStored = !aBuilder->NamedShape()->IsEmpty();
381     }
382   }
383   if (!aStored) {
384     // check the new shape is already in the tree, so, no need to store primitive, just reference
385     std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
386     if (aData.get()) {
387       TDF_Label aShapeLab = aData->shapeLab();
388       TopoDS_Shape aShapeNew = theNewShape->impl<TopoDS_Shape>();
389       Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aShapeNew, aShapeLab);
390       // the last condition is for the issue 2751 : existing shape may be found in compound-NS
391       if (!aNS.IsNull() && !aNS->IsEmpty() && aNS->Get().IsSame(aShapeNew)) {
392         // This shape is already in document, store reference instead of shape;
393         const TDF_Label aFoundLabel = aNS->Label();
394         TDF_Reference::Set(aShapeLab, aFoundLabel);
395         myBuilders.erase(0);
396         aShapeLab.ForgetAttribute(TNaming_NamedShape::GetID());
397         return;
398       }
399     }
400     store(theNewShape); // store as PRIMITIVE, but clean in any way
401     return;
402   }
403 }
404
405 void Model_BodyBuilder::clean()
406 {
407   TDF_Label aLab = std::dynamic_pointer_cast<Model_Data>(data())->shapeLab();
408   if (aLab.IsNull())
409     return;
410   std::map<int, TNaming_Builder*>::iterator aBuilder = myBuilders.begin();
411   for(; aBuilder != myBuilders.end(); aBuilder++) {
412     Handle(TNaming_NamedShape) aNS = aBuilder->second->NamedShape();
413     delete aBuilder->second;
414     if (!aNS.IsNull() && !aNS->Label().IsNull())
415       aNS->Label().ForgetAttribute(TNaming_NamedShape::GetID());
416   }
417   myBuilders.clear();
418   myPrimitivesNamesIndexMap.clear();
419   // remove the old reference (if any)
420   aLab.ForgetAttribute(TDF_Reference::GetID());
421   myFreePrimitiveTag = PRIMITIVES_START_TAG;
422   TDF_ChildIDIterator anEntriesIter(aLab, kEXTERNAL_SHAPE_REF, true);
423   for(; anEntriesIter.More(); anEntriesIter.Next()) {
424     anEntriesIter.Value()->Label().ForgetAttribute(kEXTERNAL_SHAPE_REF);
425   }
426 }
427
428 void Model_BodyBuilder::cleanCash()
429 {
430   myPrimitivesNamesIndexMap.clear();
431 }
432
433 Model_BodyBuilder::~Model_BodyBuilder()
434 {
435   clean();
436 }
437
438 void Model_BodyBuilder::buildName(const int theTag, const std::string& theName)
439 {
440   std::string aName = theName;
441   std::string aPrefix = "";
442   switch (theTag) {
443     case GENERATED_VERTICES_TAG: aPrefix = aName.empty() ? "Generated_Vertex" : "GV:"; break;
444     case GENERATED_EDGES_TAG:    aPrefix = aName.empty() ? "Generated_Edge"   : "GE:"; break;
445     case GENERATED_FACES_TAG:    aPrefix = aName.empty() ? "Generated_Face"   : "GF:"; break;
446     case MODIFIED_VERTICES_TAG:  aPrefix = aName.empty() ? "Modified_Vertex"  : "MV:"; break;
447     case MODIFIED_EDGES_TAG:     aPrefix = aName.empty() ? "Modified_Edge"    : "ME:"; break;
448     case MODIFIED_FACES_TAG:     aPrefix = aName.empty() ? "Modified_Face"    : "MF:"; break;
449   }
450   aName.insert(0, aPrefix);
451
452   TDataStd_Name::Set(builder(theTag)->NamedShape()->Label(), aName.c_str());
453 }
454 bool Model_BodyBuilder::generated(const GeomShapePtr& theNewShape,
455                                   const std::string& theName,
456                                   const bool theCheckIsInResult)
457 {
458   GeomShapePtr aResultShape = shape();
459   if (theCheckIsInResult) {
460     bool aNewShapeIsNotInResultShape = !aResultShape->isSubShape(theNewShape, false);
461     if (aNewShapeIsNotInResultShape) {
462       return false;
463     }
464   }
465
466   TopoDS_Shape aShape = theNewShape->impl<TopoDS_Shape>();
467   builder(myFreePrimitiveTag)->Generated(aShape);
468   if (!theName.empty()) {
469     std::string aName = theName;
470     if (myPrimitivesNamesIndexMap.find(theName) != myPrimitivesNamesIndexMap.end()) {
471       IndexTags& anIndexTags = myPrimitivesNamesIndexMap.find(theName)->second;
472       aName += "_" + std::to_string(++(anIndexTags.index));
473       anIndexTags.tags.push_back(myFreePrimitiveTag);
474       if (anIndexTags.index == 2) {
475         buildName(anIndexTags.tags.front(), theName + "_1");
476       }
477     }
478     else {
479       IndexTags anIndexTags;
480       anIndexTags.index = 1;
481       anIndexTags.tags.push_back(myFreePrimitiveTag);
482       myPrimitivesNamesIndexMap[theName] = anIndexTags;
483     }
484
485     buildName(myFreePrimitiveTag, aName);
486   }
487   ++myFreePrimitiveTag;
488   return true;
489 }
490
491 void Model_BodyBuilder::generated(const GeomShapePtr& theOldShape,
492                                   const GeomShapePtr& theNewShape,
493                                   const std::string& theName)
494 {
495   TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
496   TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
497   TopAbs_ShapeEnum aNewShapeType = aNewShape.ShapeType();
498   int aTag;
499   if (aNewShapeType == TopAbs_WIRE || aNewShapeType == TopAbs_SHELL) {
500     // TODO: This is a workaround. New shape should be only vertex, edge or face.
501     TopAbs_ShapeEnum aShapeTypeToExplore = aNewShapeType == TopAbs_WIRE ? TopAbs_EDGE : TopAbs_FACE;
502     aTag = TopAbs_WIRE ? GENERATED_EDGES_TAG : GENERATED_FACES_TAG;
503     for (TopExp_Explorer anExp(aNewShape, aShapeTypeToExplore); anExp.More(); anExp.Next()) {
504       builder(aTag)->Generated(anOldShape, anExp.Current());
505     }
506     buildName(aTag, theName);
507   } else {
508     aTag = getGenerationTag(aNewShape);
509     if (aTag == INVALID_TAG) return;
510     builder(aTag)->Generated(anOldShape, aNewShape);
511     buildName(aTag, theName);
512   }
513 }
514
515 void Model_BodyBuilder::modified(const GeomShapePtr& theOldShape,
516                                  const GeomShapePtr& theNewShape,
517                                  const std::string& theName)
518 {
519   TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
520   TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
521   int aTag = getModificationTag(aNewShape);
522   if (aTag == INVALID_TAG) return;
523   builder(aTag)->Modify(anOldShape, aNewShape);
524   buildName(aTag, theName);
525 }
526
527 void Model_BodyBuilder::loadDeletedShapes(const GeomMakeShapePtr& theAlgo,
528                                           const GeomShapePtr& theOldShape,
529                                           const GeomAPI_Shape::ShapeType theShapeTypeToExplore,
530                                           const GeomShapePtr& theShapesToExclude)
531 {
532   TopTools_MapOfShape anAlreadyProcessedShapes;
533   GeomShapePtr aResultShape = shape();
534   for (GeomAPI_ShapeExplorer anExp(theOldShape, theShapeTypeToExplore);
535        anExp.more();
536        anExp.next())
537   {
538     GeomShapePtr anOldSubShape = anExp.current();
539     const TopoDS_Shape& anOldSubShape_ = anOldSubShape->impl<TopoDS_Shape>();
540     if (!anAlreadyProcessedShapes.Add(anOldSubShape_)
541         || !theAlgo->isDeleted(anOldSubShape)
542         || aResultShape->isSubShape(anOldSubShape, false)
543         || (theShapesToExclude.get() && theShapesToExclude->isSubShape(anOldSubShape, false)))
544     {
545       continue;
546     }
547
548     ListOfShape aNewShapes;
549     if (BRepTools_History::IsSupportedType(anOldSubShape_)) { // to avoid crash in #2572
550       theAlgo->modified(anOldSubShape, aNewShapes);
551     }
552
553     if (aNewShapes.size() == 0
554         || (aNewShapes.size() == 1 && aNewShapes.front()->isSame(anOldSubShape))) {
555       builder(DELETED_TAG)->Delete(anOldSubShape_);
556     }
557   }
558 }
559
560 // Keep only the shapes with minimal shape type
561 static void keepTopLevelShapes(ListOfShape& theShapes,
562                                const TopoDS_Shape& theRoot,
563                                const GeomShapePtr& theResultShape = GeomShapePtr())
564 {
565   GeomAPI_Shape::ShapeType aKeepShapeType = GeomAPI_Shape::SHAPE;
566   ListOfShape::iterator anIt = theShapes.begin();
567   while (anIt != theShapes.end()) {
568     TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
569     bool aSkip = aNewShape.IsNull() ||
570       (aNewShape.ShapeType() == TopAbs_EDGE && BRep_Tool::Degenerated(TopoDS::Edge(aNewShape)));
571     if (aSkip || theRoot.IsSame(aNewShape) || (theResultShape &&
572         (!theResultShape->isSubShape(*anIt, false) || theResultShape->isSame(*anIt)))) {
573       ListOfShape::iterator aRemoveIt = anIt++;
574       theShapes.erase(aRemoveIt);
575     } else {
576       GeomAPI_Shape::ShapeType aType = (*anIt)->shapeType();
577       if (aType < aKeepShapeType) {
578         // found a shape with lesser shape type => remove all previous shapes
579         aKeepShapeType = aType;
580         theShapes.erase(theShapes.begin(), anIt);
581         ++anIt;
582       } else if (aType > aKeepShapeType) {
583         // shapes with greater shape type should be removed from the list
584         ListOfShape::iterator aRemoveIt = anIt++;
585         theShapes.erase(aRemoveIt);
586       } else
587         ++anIt;
588     }
589   }
590 }
591
592 void Model_BodyBuilder::loadModifiedShapes(const GeomMakeShapePtr& theAlgo,
593                                            const GeomShapePtr& theOldShape,
594                                            const GeomAPI_Shape::ShapeType theShapeTypeToExplore,
595                                            const std::string& theName)
596 {
597   GeomShapePtr aResultShape = shape();
598   GeomShapePtr aShapeToExplore = theOldShape;
599   if (theAlgo->isNewShapesCollected(theOldShape, theShapeTypeToExplore)) {
600     // use optimized set of old shapes for this
601     GeomShapePtr aCompound = theAlgo->oldShapesForNew(theOldShape,
602                                                       aResultShape,
603                                                       theShapeTypeToExplore);
604     if (aCompound.get()) aShapeToExplore = aCompound;
605   }
606
607   TopTools_MapOfShape anAlreadyProcessedShapes;
608   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
609   for (GeomAPI_ShapeExplorer anOldShapeExp(aShapeToExplore, theShapeTypeToExplore);
610        anOldShapeExp.more();
611        anOldShapeExp.next())
612   {
613     GeomShapePtr anOldSubShape = anOldShapeExp.current();
614     const TopoDS_Shape& anOldSubShape_ = anOldSubShape->impl<TopoDS_Shape>();
615
616     // There is no sense to write history if shape already processed
617     // or old shape does not exist in the document.
618     bool anOldSubShapeAlreadyProcessed = !anAlreadyProcessedShapes.Add(anOldSubShape_);
619     TDF_Label anAccess2 = std::dynamic_pointer_cast<Model_Document>(
620       ModelAPI_Session::get()->moduleDocument())->generalLabel();
621     TDF_Label anOriginalLabel;
622     bool anOldSubShapeNotInTree =
623       !isShapeInTree(aData->shapeLab(), anAccess2, anOldSubShape_, anOriginalLabel);
624     if (anOldSubShapeAlreadyProcessed || anOldSubShapeNotInTree) {
625       continue;
626     }
627
628     // Get new shapes.
629     ListOfShape aNewShapes;
630     theAlgo->modified(anOldSubShape, aNewShapes);
631
632     for (ListOfShape::const_iterator aNewShapesIt = aNewShapes.cbegin();
633          aNewShapesIt != aNewShapes.cend();
634          ++aNewShapesIt)
635     {
636       GeomShapePtr aNewShape = *aNewShapesIt;
637       const TopoDS_Shape& aNewShape_ = aNewShape->impl<TopoDS_Shape>();
638       bool isGenerated = anOldSubShape_.ShapeType() != aNewShape_.ShapeType();
639
640       bool aNewShapeIsSameAsOldShape = anOldSubShape->isSame(aNewShape);
641       bool aNewShapeIsNotInResultShape = !aResultShape->isSubShape(aNewShape, false);
642       if (aNewShapeIsSameAsOldShape || aNewShapeIsNotInResultShape)
643         continue;
644
645       if (aResultShape->isSame(aNewShape))
646         continue; // it is stored on the root level (2241 - history propagation issue)
647
648       int aTag = isGenerated ? getGenerationTag(aNewShape_) : getModificationTag(aNewShape_);
649       TNaming_Builder*aBuilder = builder(aTag);
650       if (isAlreadyStored(aBuilder, anOldSubShape_, aNewShape_))
651         continue; // new shape was already stored.
652
653       buildName(aTag, theName);
654       isGenerated ? aBuilder->Generated(anOldSubShape_, aNewShape_)
655                   : aBuilder->Modify(anOldSubShape_, aNewShape_);
656       // store information about the external document reference to restore old shape on open
657       storeExternalReference(anOriginalLabel, aBuilder->NamedShape()->Label());
658     }
659   }
660 }
661
662 void Model_BodyBuilder::loadGeneratedShapes(const GeomMakeShapePtr& theAlgo,
663                                             const GeomShapePtr& theOldShape,
664                                             const GeomAPI_Shape::ShapeType theShapeTypeToExplore,
665                                             const std::string& theName,
666                                             const bool theSaveOldIfNotInTree)
667 {
668   GeomShapePtr aResultShape = shape();
669   TopTools_MapOfShape anAlreadyProcessedShapes;
670   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
671   for (GeomAPI_ShapeExplorer anOldShapeExp(theOldShape, theShapeTypeToExplore);
672        anOldShapeExp.more();
673        anOldShapeExp.next())
674   {
675     GeomShapePtr anOldSubShape = anOldShapeExp.current();
676     const TopoDS_Shape& anOldSubShape_ = anOldSubShape->impl<TopoDS_Shape>();
677
678     // There is no sense to write history if shape already processed
679     // or old shape does not exist in the document.
680     bool anOldSubShapeAlreadyProcessed = !anAlreadyProcessedShapes.Add(anOldSubShape_);
681     TDF_Label anAccess2 = std::dynamic_pointer_cast<Model_Document>(
682       ModelAPI_Session::get()->moduleDocument())->generalLabel();
683     TDF_Label anOriginalLabel;
684     bool anOldSubShapeNotInTree =
685       !isShapeInTree(aData->shapeLab(), anAccess2, anOldSubShape_, anOriginalLabel);
686     if (anOldSubShapeAlreadyProcessed || anOldSubShapeNotInTree) {
687       if (theSaveOldIfNotInTree) {
688         std::string aSelectionName = theName + "Selected";
689         generated(anOldSubShape, aSelectionName, false);
690       } else
691         continue;
692     }
693
694     // Get new shapes.
695     ListOfShape aNewShapes;
696     theAlgo->generated(anOldSubShape, aNewShapes);
697
698     keepTopLevelShapes(aNewShapes, anOldSubShape_);
699
700     for (ListOfShape::const_iterator aNewShapesIt = aNewShapes.cbegin();
701          aNewShapesIt != aNewShapes.cend();
702          ++aNewShapesIt)
703     {
704       GeomShapePtr aNewShape = *aNewShapesIt;
705       const TopoDS_Shape& aNewShape_ = aNewShape->impl<TopoDS_Shape>();
706
707       bool aNewShapeIsSameAsOldShape = anOldSubShape->isSame(aNewShape);
708       bool aNewShapeIsNotInResultShape = !aResultShape->isSubShape(aNewShape, false);
709       if (aNewShapeIsSameAsOldShape || aNewShapeIsNotInResultShape) {
710         continue;
711       }
712
713       if (aResultShape->isSame(aNewShape))
714         continue; // it is stored on the root level
715
716       TopAbs_ShapeEnum aNewShapeType = aNewShape_.ShapeType();
717       if (aNewShapeType == TopAbs_WIRE || aNewShapeType == TopAbs_SHELL) {
718         // TODO: This is a workaround. New shape should be only edge or face.
719         TopAbs_ShapeEnum aShapeTypeToExplore = aNewShapeType == TopAbs_WIRE ? TopAbs_EDGE
720                                                                             : TopAbs_FACE;
721         int aTag = TopAbs_WIRE ? GENERATED_EDGES_TAG : GENERATED_FACES_TAG;
722         for (TopExp_Explorer anExp(aNewShape_, aShapeTypeToExplore); anExp.More(); anExp.Next()) {
723           builder(aTag)->Generated(anOldSubShape_, anExp.Current());
724           // store information about the external document reference to restore old shape on open
725           storeExternalReference(anOriginalLabel, builder(aTag)->NamedShape()->Label());
726         }
727         buildName(aTag, theName);
728       } else {
729         int aTag = getGenerationTag(aNewShape_);
730         if (aTag == INVALID_TAG) return;
731         builder(aTag)->Generated(anOldSubShape_, aNewShape_);
732         buildName(aTag, theName);
733         // store information about the external document reference to restore old shape on open
734         storeExternalReference(anOriginalLabel, builder(aTag)->NamedShape()->Label());
735       }
736     }
737   }
738 }
739
740 // LCOV_EXCL_START
741 //=======================================================================
742 int getDangleShapes(const TopoDS_Shape&           theShapeIn,
743   const TopAbs_ShapeEnum        theGeneratedFrom,
744   TopTools_DataMapOfShapeShape& theDangles)
745 {
746   theDangles.Clear();
747   TopTools_IndexedDataMapOfShapeListOfShape subShapeAndAncestors;
748   TopAbs_ShapeEnum GeneratedTo;
749   if (theGeneratedFrom == TopAbs_FACE) GeneratedTo = TopAbs_EDGE;
750   else if (theGeneratedFrom == TopAbs_EDGE) GeneratedTo = TopAbs_VERTEX;
751   else return Standard_False;
752   TopExp::MapShapesAndAncestors(theShapeIn, GeneratedTo, theGeneratedFrom, subShapeAndAncestors);
753   for (Standard_Integer i = 1; i <= subShapeAndAncestors.Extent(); i++) {
754     const TopoDS_Shape& mayBeDangle = subShapeAndAncestors.FindKey(i);
755     const TopTools_ListOfShape& ancestors = subShapeAndAncestors.FindFromIndex(i);
756     if (ancestors.Extent() == 1) theDangles.Bind(ancestors.First(), mayBeDangle);
757   }
758   return theDangles.Extent();
759 }
760
761 //=======================================================================
762 void loadGeneratedDangleShapes(
763   const TopoDS_Shape&      theShapeIn,
764   const TopAbs_ShapeEnum   theGeneratedFrom,
765   TNaming_Builder *        theBuilder)
766 {
767   TopTools_DataMapOfShapeShape dangles;
768   if (!getDangleShapes(theShapeIn, theGeneratedFrom, dangles)) return;
769   TopTools_DataMapIteratorOfDataMapOfShapeShape itr(dangles);
770   for (; itr.More(); itr.Next())
771     theBuilder->Generated(itr.Key(), itr.Value());
772 }
773 // LCOV_EXCL_STOP
774
775 //=======================================================================
776 void Model_BodyBuilder::loadNextLevels(GeomShapePtr theShape,
777                                        const std::string& theName)
778 {
779   if(theShape->isNull()) return;
780   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
781   std::string aName;
782   if (aShape.ShapeType() == TopAbs_SOLID) {
783     TopExp_Explorer expl(aShape, TopAbs_FACE);
784     for (; expl.More(); expl.Next()) {
785       builder(myFreePrimitiveTag)->Generated(expl.Current());
786       TCollection_AsciiString aStr(myFreePrimitiveTag - PRIMITIVES_START_TAG + 1);
787       aName = theName + "_" + aStr.ToCString();
788       buildName(myFreePrimitiveTag, aName);
789       ++myFreePrimitiveTag;
790     }
791   }
792   else if (aShape.ShapeType() == TopAbs_SHELL || aShape.ShapeType() == TopAbs_FACE) {
793     // load faces and all the free edges
794     TopTools_IndexedMapOfShape Faces;
795     TopExp::MapShapes(aShape, TopAbs_FACE, Faces);
796     if (Faces.Extent() > 1 || (aShape.ShapeType() == TopAbs_SHELL && Faces.Extent() == 1)) {
797       TopExp_Explorer expl(aShape, TopAbs_FACE);
798       for (; expl.More(); expl.Next()) {
799         builder(myFreePrimitiveTag)->Generated(expl.Current());
800         TCollection_AsciiString aStr(myFreePrimitiveTag - PRIMITIVES_START_TAG + 1);
801         aName = theName + "_" + aStr.ToCString();
802         buildName(myFreePrimitiveTag, aName);
803         ++myFreePrimitiveTag;
804       }
805     }
806     TopTools_IndexedDataMapOfShapeListOfShape anEdgeAndNeighbourFaces;
807     TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, anEdgeAndNeighbourFaces);
808     for (Standard_Integer i = 1; i <= anEdgeAndNeighbourFaces.Extent(); i++)
809     {
810       const TopTools_ListOfShape& aLL = anEdgeAndNeighbourFaces.FindFromIndex(i);
811       if (aLL.Extent() < 2) {
812         if (BRep_Tool::Degenerated(TopoDS::Edge(anEdgeAndNeighbourFaces.FindKey(i))))
813           continue;
814         builder(myFreePrimitiveTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
815         TCollection_AsciiString aStr(myFreePrimitiveTag - PRIMITIVES_START_TAG + 1);
816         aName = theName + "_" + aStr.ToCString();
817         buildName(myFreePrimitiveTag, aName);
818         ++myFreePrimitiveTag;
819       } else {
820         TopTools_ListIteratorOfListOfShape anIter(aLL);
821         const TopoDS_Face& aFace = TopoDS::Face(anIter.Value());
822         anIter.Next();
823         if(aFace.IsEqual(anIter.Value())) {
824           builder(myFreePrimitiveTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
825           TCollection_AsciiString aStr(myFreePrimitiveTag - PRIMITIVES_START_TAG + 1);
826           aName = theName + "_" + aStr.ToCString();
827           buildName(myFreePrimitiveTag, aName);
828           ++myFreePrimitiveTag;
829         }
830       }
831     }
832   } else if (aShape.ShapeType() == TopAbs_WIRE) {
833     TopTools_IndexedMapOfShape Edges;
834     BRepTools::Map3DEdges(aShape, Edges);
835     if (Edges.Extent() == 1) {
836       builder(myFreePrimitiveTag++)->Generated(Edges.FindKey(1));
837       TopExp_Explorer expl(aShape, TopAbs_VERTEX);
838       for (; expl.More(); expl.Next()) {
839         builder(myFreePrimitiveTag)->Generated(expl.Current());
840         TCollection_AsciiString aStr(myFreePrimitiveTag - PRIMITIVES_START_TAG + 1);
841         aName = theName + "_" + aStr.ToCString();
842         buildName(myFreePrimitiveTag, aName);
843         ++myFreePrimitiveTag;
844       }
845     } else {
846       TopExp_Explorer expl(aShape, TopAbs_EDGE);
847       for (; expl.More(); expl.Next()) {
848         builder(myFreePrimitiveTag)->Generated(expl.Current());
849         TCollection_AsciiString aStr(myFreePrimitiveTag - PRIMITIVES_START_TAG + 1);
850         aName = theName + "_" + aStr.ToCString();
851         buildName(myFreePrimitiveTag, aName);
852         ++myFreePrimitiveTag;
853       }
854       // and load generated vertices.
855       TopTools_DataMapOfShapeShape generated;
856       if (getDangleShapes(aShape, TopAbs_EDGE, generated))
857       {
858         TNaming_Builder* pBuilder = builder(myFreePrimitiveTag++);
859         loadGeneratedDangleShapes(aShape, TopAbs_EDGE, pBuilder);
860       }
861     }
862   } else if (aShape.ShapeType() == TopAbs_EDGE) {
863     TopExp_Explorer expl(aShape, TopAbs_VERTEX);
864     for (; expl.More(); expl.Next()) {
865       builder(myFreePrimitiveTag)->Generated(expl.Current());
866       TCollection_AsciiString aStr(myFreePrimitiveTag - PRIMITIVES_START_TAG + 1);
867       aName = theName + "_" + aStr.ToCString();
868       buildName(myFreePrimitiveTag, aName);
869       ++myFreePrimitiveTag;
870     }
871   }
872 }
873
874 //=======================================================================
875 int findAmbiguities(const TopoDS_Shape&           theShapeIn,
876   TopTools_ListOfShape&   theList)
877 {
878   theList.Clear();
879   // edges -> ancestor faces list
880   TopTools_IndexedDataMapOfShapeListOfShape aSubShapeAndAncestors;
881   TopExp::MapShapesAndAncestors(theShapeIn, TopAbs_EDGE, TopAbs_FACE, aSubShapeAndAncestors);
882   // keeps the shapes which are already in the resulting list
883   TopTools_MapOfShape alreadyThere;
884   // map from faces identifier (combination of hash-codes) to list of edges produced such ID
885   NCollection_DataMap<int, NCollection_List<TopoDS_Shape> > aFacesIDs;
886
887   TopTools_IndexedDataMapOfShapeListOfShape::Iterator anAncestorsIter(aSubShapeAndAncestors);
888   for (; anAncestorsIter.More(); anAncestorsIter.Next()) {
889     const TopTools_ListOfShape& ancestors = anAncestorsIter.Value();
890     if (ancestors.Extent() < 2)
891       continue;
892     Standard_Integer anID = 0;
893     for(TopTools_ListIteratorOfListOfShape aFaceIt(ancestors); aFaceIt.More(); aFaceIt.Next()) {
894       anID ^= HashCode(aFaceIt.ChangeValue(), 1990657); // Pierpont prime
895     }
896     if (aFacesIDs.IsBound(anID)) { // there found same edge, check they really have same faces
897       const NCollection_List<TopoDS_Shape>& aSameFaces1 =
898         aSubShapeAndAncestors.FindFromKey(anAncestorsIter.Key());
899       NCollection_List<TopoDS_Shape>::Iterator aSameEdge(aFacesIDs.ChangeFind(anID));
900       for(; aSameEdge.More(); aSameEdge.Next()) {
901         const NCollection_List<TopoDS_Shape>& aSameFaces2 =
902           aSubShapeAndAncestors.FindFromKey(aSameEdge.Value());
903         if (aSameFaces2.Extent() != aSameFaces1.Extent()) // the number of faces is different
904           break;
905
906         NCollection_List<TopoDS_Shape>::Iterator aFaceIter1(aSameFaces1);
907         for(; aFaceIter1.More(); aFaceIter1.Next()) {
908           NCollection_List<TopoDS_Shape>::Iterator aFaceIter2(aSameFaces2);
909           for(; aFaceIter2.More(); aFaceIter2.Next()) {
910             if (aFaceIter1.Value().IsSame(aFaceIter2.Value()))
911               break;
912           }
913           if (!aFaceIter2.More()) // aFaceIter1 contains a face, which is not in aFaceIter2
914             break;
915         }
916         if (!aFaceIter1.More()) { // all the faces are same => put to the result
917           if (alreadyThere.Add(aSameEdge.Value()))
918             theList.Append(aSameEdge.Value());
919           if (alreadyThere.Add(anAncestorsIter.Key()))
920             theList.Append(anAncestorsIter.Key());
921         }
922       }
923     } else { // ID is unique, just add this edge
924       aFacesIDs.Bind(anID, NCollection_List<TopoDS_Shape>());
925     }
926     aFacesIDs.ChangeFind(anID).Append(anAncestorsIter.Key()); // add to the list anyway
927   }
928   return theList.Extent();
929 }
930
931 //=======================================================================
932 void Model_BodyBuilder::loadFirstLevel(GeomShapePtr theShape, const std::string& theName)
933 {
934   if(theShape->isNull()) return;
935   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
936   std::string aName;
937   if (aShape.ShapeType() == TopAbs_COMPOUND || aShape.ShapeType() == TopAbs_COMPSOLID) {
938     TopoDS_Iterator itr(aShape);
939     for (; itr.More(); itr.Next()) {
940       builder(myFreePrimitiveTag)->Generated(itr.Value());
941       TCollection_AsciiString aStr(myFreePrimitiveTag - PRIMITIVES_START_TAG + 1);
942       aName = theName + "_" + aStr.ToCString();
943       buildName(myFreePrimitiveTag, aName);
944       ++myFreePrimitiveTag;
945       if (itr.Value().ShapeType() == TopAbs_COMPOUND ||
946         itr.Value().ShapeType() == TopAbs_COMPSOLID)
947       {
948         GeomShapePtr itrShape(new GeomAPI_Shape());
949         itrShape->setImpl(new TopoDS_Shape(itr.Value()));
950         loadFirstLevel(itrShape, theName);
951       } else {
952         GeomShapePtr itrShape(new GeomAPI_Shape());
953         itrShape->setImpl(new TopoDS_Shape(itr.Value()));
954         loadNextLevels(itrShape, theName);
955       }
956     }
957   } else {
958     GeomShapePtr itrShape(new GeomAPI_Shape());
959     itrShape->setImpl(new TopoDS_Shape(aShape));
960     loadNextLevels(itrShape, theName);
961   }
962   TopTools_ListOfShape   aList;
963   if(findAmbiguities(aShape, aList)) {
964     TopTools_ListIteratorOfListOfShape it(aList);
965     for (; it.More(); it.Next(), ++myFreePrimitiveTag) {
966       builder(myFreePrimitiveTag)->Generated(it.Value());
967       TCollection_AsciiString aStr(myFreePrimitiveTag - PRIMITIVES_START_TAG + 1);
968       aName = theName + "_" + aStr.ToCString();
969       buildName(myFreePrimitiveTag, aName);
970     }
971   }
972 }
973
974 GeomShapePtr Model_BodyBuilder::shape()
975 {
976   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
977   if (aData && aData->isValid()) {
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         GeomShapePtr aRes(new GeomAPI_Shape);
988         aRes->setImpl(new TopoDS_Shape(aShape));
989         return aRes;
990       }
991     }
992   }
993   return GeomShapePtr();
994 }