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