]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_VersionedBoolean.cpp
Salome HOME
ba7d81ba10fde9a70dc34be77d59341be6fdd247
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_VersionedBoolean.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 "FeaturesPlugin_VersionedBoolean.h"
21
22 #include <ModelAPI_Data.h>
23 #include <ModelAPI_Document.h>
24 #include <ModelAPI_AttributeReference.h>
25 #include <ModelAPI_AttributeInteger.h>
26 #include <ModelAPI_ResultBody.h>
27 #include <ModelAPI_AttributeSelectionList.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_Validator.h>
30 #include <ModelAPI_Tools.h>
31
32 #include <GeomAlgoAPI_Boolean.h>
33 #include <GeomAlgoAPI_CompoundBuilder.h>
34 #include <GeomAlgoAPI_MakeShapeCustom.h>
35 #include <GeomAlgoAPI_MakeShapeList.h>
36 #include <GeomAlgoAPI_Partition.h>
37 #include <GeomAlgoAPI_PaveFiller.h>
38 #include <GeomAlgoAPI_ShapeBuilder.h>
39 #include <GeomAlgoAPI_ShapeTools.h>
40 #include <GeomAlgoAPI_Tools.h>
41 #include <GeomAPI_Face.h>
42 #include <GeomAPI_ShapeExplorer.h>
43 #include <GeomAPI_ShapeIterator.h>
44
45 #include <algorithm>
46 #include <map>
47
48 //=================================================================================================
49 void FeaturesPlugin_VersionedBoolean::initVersion(const int theVersion,
50                                                   const AttributePtr theObjectsAttr,
51                                                   const AttributePtr theToolsAttr)
52 {
53   AttributePtr aVerAttr = data()->addAttribute(VERSION_ID(), ModelAPI_AttributeInteger::typeId());
54   aVerAttr->setIsArgument(false);
55   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), VERSION_ID());
56   if (!integer(VERSION_ID())->isInitialized() &&
57       (!theObjectsAttr || !theObjectsAttr->isInitialized()) &&
58       (!theToolsAttr || !theToolsAttr->isInitialized())) {
59     // this is a newly created feature (not read from file),
60     // so, initialize the latest version
61     integer(VERSION_ID())->setValue(theVersion);
62   }
63 }
64
65 //=================================================================================================
66 void FeaturesPlugin_VersionedBoolean::parentForShape(const GeomShapePtr& theShape,
67                                             const ResultPtr& theContext,
68                                             ObjectHierarchy& theShapesHierarchy)
69 {
70   ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(theContext);
71   if (aResCompSolidPtr.get()) {
72     std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
73     if (aContextShape->shapeType() <= GeomAPI_Shape::COMPSOLID) {
74       theShapesHierarchy.AddParent(theShape, aContextShape);
75       parentForShape(aContextShape, aResCompSolidPtr, theShapesHierarchy);
76     }
77   }
78 }
79
80 bool FeaturesPlugin_VersionedBoolean::processAttribute(const std::string& theAttributeName,
81                                               ObjectHierarchy& theObjects,
82                                               ListOfShape& thePlanesList)
83 {
84   AttributeSelectionListPtr anObjectsSelList = selectionList(theAttributeName);
85   for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
86     AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
87     GeomShapePtr anObject = anObjectAttr->value();
88     if (!anObject.get()) {
89       // It could be a construction plane.
90       ResultPtr aContext = anObjectAttr->context();
91       anObject = anObjectAttr->context()->shape();
92       if (anObject.get()) {
93         thePlanesList.push_back(anObject);
94         continue;
95       } else
96         return false;
97     }
98
99     theObjects.AddObject(anObject);
100
101     ResultPtr aContext = anObjectAttr->context();
102     parentForShape(anObject, aContext, theObjects);
103   }
104   return true;
105 }
106
107 //=================================================================================================
108 bool FeaturesPlugin_VersionedBoolean::processObject(
109     const GeomAlgoAPI_Tools::BOPType theBooleanType,
110     const GeomShapePtr& theObject,
111     const ListOfShape& theTools,
112     const ListOfShape& thePlanes,
113     int& theResultIndex,
114     std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
115     ListOfShape& theResultShapesList,
116     GeomShapePtr theResultCompound)
117 {
118   ListOfShape aListWithObject;
119   aListWithObject.push_back(theObject);
120   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
121   std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
122   GeomShapePtr aResShape;
123
124   std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
125       GeomAlgoAPI_ShapeTools::getBoundingBox(aListWithObject, 1.0);
126
127   // Resize planes.
128   ListOfShape aToolsWithPlanes = theTools;
129   for (ListOfShape::const_iterator anIt = thePlanes.begin(); anIt != thePlanes.end(); ++anIt) {
130     GeomShapePtr aPlane = *anIt;
131     GeomShapePtr aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPlane, aBoundingPoints);
132     std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(
133         new GeomAlgoAPI_MakeShapeCustom);
134     aMkShCustom->addModified(aPlane, aTool);
135     aMakeShapeList->appendAlgo(aMkShCustom);
136     aToolsWithPlanes.push_back(aTool);
137   }
138
139   if (theBooleanType == GeomAlgoAPI_Tools::BOOL_PARTITION)
140     aBoolAlgo.reset(new GeomAlgoAPI_Partition(aListWithObject, aToolsWithPlanes));
141   else
142     aBoolAlgo.reset(new GeomAlgoAPI_Boolean(aListWithObject,
143                                             aToolsWithPlanes,
144                                             theBooleanType));
145
146   // Checking that the algorithm worked properly.
147   std::string anError;
148   if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
149     setError(anError);
150     return false;
151   }
152
153   aResShape = aBoolAlgo->shape();
154   if (aResShape.get() && aResShape->shapeType() == GeomAPI_Shape::COMPOUND) {
155     int aSubResultsNb = 0;
156     GeomAPI_ShapeIterator anIt(aResShape);
157     for (; anIt.more(); anIt.next())
158       ++aSubResultsNb;
159
160     if (aSubResultsNb == 1) {
161       anIt.init(aResShape);
162       if (anIt.more())
163         aResShape = anIt.current();
164     }
165   }
166
167   aMakeShapeList->appendAlgo(aBoolAlgo);
168
169   GeomAPI_ShapeIterator aShapeIt(aResShape);
170   if (aShapeIt.more() || aResShape->shapeType() == GeomAPI_Shape::VERTEX) {
171     std::shared_ptr<ModelAPI_ResultBody> aResultBody;
172
173     if (theResultCompound) { // store BOP result to the compound
174       std::shared_ptr<GeomAlgoAPI_ShapeBuilder> aBuilder(new GeomAlgoAPI_ShapeBuilder);
175       aBuilder->add(theResultCompound, aResShape);
176       aMakeShapeList->appendAlgo(aBuilder);
177     }
178     else { // create a separate ResultBody
179       aResultBody = document()->createBody(data(), theResultIndex);
180
181       // tools should be added to the list to fulfill the correct history of modification
182       aListWithObject.insert(aListWithObject.end(), theTools.begin(), theTools.end());
183
184       ListOfShape aUsedTools = theTools;
185       aUsedTools.insert(aUsedTools.end(), thePlanes.begin(), thePlanes.end());
186
187       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
188                                                aListWithObject,
189                                                aUsedTools,
190                                                aMakeShapeList,
191                                                aResShape);
192       setResult(aResultBody, theResultIndex);
193       ++theResultIndex;
194     }
195
196
197     FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
198     aRBA.resultBody = aResultBody;
199     aRBA.baseShape = theObject;
200     aRBA.makeShape = aMakeShapeList;
201     theResultBaseAlgoList.push_back(aRBA);
202     theResultShapesList.push_back(aResShape);
203   }
204   return true;
205 }
206
207 //=================================================================================================
208 bool FeaturesPlugin_VersionedBoolean::processCompsolid(
209     const GeomAlgoAPI_Tools::BOPType theBooleanType,
210     const ObjectHierarchy& theCompsolidHierarchy,
211     const GeomShapePtr& theCompsolid,
212     const ListOfShape& theTools,
213     const ListOfShape& thePlanes,
214     int& theResultIndex,
215     std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
216     ListOfShape& theResultShapesList,
217     GeomShapePtr theResultCompound)
218 {
219   ListOfShape aUsedInOperationSolids;
220   ListOfShape aNotUsedSolids;
221   theCompsolidHierarchy.SplitCompound(theCompsolid, aUsedInOperationSolids, aNotUsedSolids);
222
223   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
224
225   std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
226       GeomAlgoAPI_ShapeTools::getBoundingBox(aUsedInOperationSolids, 1.0);
227
228   // Resize planes.
229   ListOfShape aToolsWithPlanes = theTools;
230   for (ListOfShape::const_iterator anIt = thePlanes.begin(); anIt != thePlanes.end(); ++anIt)
231   {
232     GeomShapePtr aPlane = *anIt;
233     GeomShapePtr aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPlane, aBoundingPoints);
234     std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(
235       new GeomAlgoAPI_MakeShapeCustom);
236     aMkShCustom->addModified(aPlane, aTool);
237     aMakeShapeList->appendAlgo(aMkShCustom);
238     aToolsWithPlanes.push_back(aTool);
239   }
240
241   std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
242   if (theBooleanType == GeomAlgoAPI_Tools::BOOL_PARTITION)
243     aBoolAlgo.reset(new GeomAlgoAPI_Partition(aUsedInOperationSolids, aToolsWithPlanes));
244   else
245     aBoolAlgo.reset(new GeomAlgoAPI_Boolean(aUsedInOperationSolids,
246                                             aToolsWithPlanes,
247                                             theBooleanType));
248
249   // Checking that the algorithm worked properly.
250   std::string anError;
251   if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
252     setError(anError);
253     return false;
254   }
255
256   aMakeShapeList->appendAlgo(aBoolAlgo);
257   GeomShapePtr aResultShape = aBoolAlgo->shape();
258
259   // Add result to not used solids from compsolid.
260   if (!aNotUsedSolids.empty()) {
261     ListOfShape aShapesToAdd = aNotUsedSolids;
262     aShapesToAdd.push_back(aBoolAlgo->shape());
263     std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
264         new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
265     if (!aFillerAlgo->isDone()) {
266       std::string aFeatureError = "Error: PaveFiller algorithm failed.";
267       setError(aFeatureError);
268       return false;
269     }
270
271     aMakeShapeList->appendAlgo(aFillerAlgo);
272     aResultShape = aFillerAlgo->shape();
273   }
274
275   GeomAPI_ShapeIterator aShapeIt(aResultShape);
276   if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX)
277   {
278     std::shared_ptr<ModelAPI_ResultBody> aResultBody;
279
280     if (theResultCompound) { // store BOP result to the compound
281       std::shared_ptr<GeomAlgoAPI_ShapeBuilder> aBuilder(new GeomAlgoAPI_ShapeBuilder);
282       aBuilder->add(theResultCompound, aResultShape);
283       aMakeShapeList->appendAlgo(aBuilder);
284     }
285     else { // create a separate ResultBody
286       aResultBody = document()->createBody(data(), theResultIndex);
287
288       ListOfShape aCompSolidList;
289       aCompSolidList.push_back(theCompsolid);
290       // tools should be added to the list to fulfill the correct history of modification
291       aCompSolidList.insert(aCompSolidList.end(), theTools.begin(), theTools.end());
292
293       ListOfShape aUsedTools = theTools;
294       aUsedTools.insert(aUsedTools.end(), thePlanes.begin(), thePlanes.end());
295
296       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
297                                                aCompSolidList,
298                                                aUsedTools,
299                                                aMakeShapeList,
300                                                aResultShape);
301       setResult(aResultBody, theResultIndex);
302       ++theResultIndex;
303     }
304
305     FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
306     aRBA.resultBody = aResultBody;
307     aRBA.baseShape = theCompsolid;
308     aRBA.makeShape = aMakeShapeList;
309     theResultBaseAlgoList.push_back(aRBA);
310     theResultShapesList.push_back(aResultShape);
311   }
312   return true;
313 }
314
315 //=================================================================================================
316 bool FeaturesPlugin_VersionedBoolean::processCompound(
317     const GeomAlgoAPI_Tools::BOPType theBooleanType,
318     const ObjectHierarchy& theCompoundHierarchy,
319     const GeomShapePtr& theCompound,
320     const ListOfShape& theTools,
321     int& theResultIndex,
322     std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
323     ListOfShape& theResultShapesList,
324     GeomShapePtr theResultCompound)
325 {
326   ListOfShape aUsedInOperationShapes;
327   ListOfShape aNotUsedShapes;
328   theCompoundHierarchy.SplitCompound(theCompound, aUsedInOperationShapes, aNotUsedShapes);
329   if (theResultCompound) {
330     // Not necessary to keep all subs of the current compound,
331     // all unused solids are already stored in the result compound.
332     aNotUsedShapes.clear();
333   }
334
335   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
336   std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(
337       new GeomAlgoAPI_Boolean(aUsedInOperationShapes,
338                               theTools,
339                               theBooleanType));
340
341   // Checking that the algorithm worked properly.
342   std::string anError;
343   if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
344     setError(anError);
345     return false;
346   }
347
348   aMakeShapeList->appendAlgo(aBoolAlgo);
349   GeomShapePtr aResultShape = aBoolAlgo->shape();
350
351   // Add result to not used shape from compound.
352   if (!aNotUsedShapes.empty()) {
353     ListOfShape aShapesForResult = aNotUsedShapes;
354     if (aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
355       for (GeomAPI_ShapeIterator aResultIt(aResultShape); aResultIt.more(); aResultIt.next()) {
356         aShapesForResult.push_back(aResultIt.current());
357       }
358     }
359     else {
360       aShapesForResult.push_back(aResultShape);
361     }
362
363     if (aShapesForResult.size() == 1) {
364       aResultShape = aShapesForResult.front();
365     }
366     else {
367       aResultShape = GeomAlgoAPI_CompoundBuilder::compound(aShapesForResult);
368     }
369   }
370
371   GeomAPI_ShapeIterator aShapeIt(aResultShape);
372   if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX) {
373     std::shared_ptr<ModelAPI_ResultBody> aResultBody;
374
375     if (theResultCompound) { // store BOP result to the compound
376       std::shared_ptr<GeomAlgoAPI_ShapeBuilder> aBuilder(new GeomAlgoAPI_ShapeBuilder);
377       aBuilder->add(theResultCompound, aResultShape);
378       aMakeShapeList->appendAlgo(aBuilder);
379     }
380     else { // create a separate ResultBody
381       aResultBody = document()->createBody(data(), theResultIndex);
382
383       ListOfShape aCompoundList;
384       aCompoundList.push_back(theCompound);
385       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
386                                                aCompoundList,
387                                                theTools,
388                                                aMakeShapeList,
389                                                aResultShape);
390       setResult(aResultBody, theResultIndex);
391       ++theResultIndex;
392     }
393
394     FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
395     aRBA.resultBody = aResultBody;
396     aRBA.baseShape = theCompound;
397     aRBA.makeShape = aMakeShapeList;
398     theResultBaseAlgoList.push_back(aRBA);
399     theResultShapesList.push_back(aResultShape);
400   }
401   return true;
402 }
403
404 //==================================================================================================
405 GeomShapePtr FeaturesPlugin_VersionedBoolean::keepUnusedSubsOfCompound(
406     const GeomShapePtr& theResult,
407     const ObjectHierarchy& theObjectsHierarchy,
408     const ObjectHierarchy& theToolsHierarchy,
409     std::shared_ptr<GeomAlgoAPI_MakeShapeList> theMakeShapeList)
410 {
411   ListOfShape aCompounds;
412   theObjectsHierarchy.CompoundsOfUnusedObjects(aCompounds);
413   theToolsHierarchy.CompoundsOfUnusedObjects(aCompounds);
414
415   GeomShapePtr aResultShape = theResult;
416   if (!aCompounds.empty()) {
417     aResultShape = aCompounds.front();
418     aCompounds.pop_front();
419
420     std::shared_ptr<GeomAlgoAPI_ShapeBuilder> aBuilder(new GeomAlgoAPI_ShapeBuilder);
421     for (ListOfShape::iterator anIt = aCompounds.begin(); anIt != aCompounds.end(); ++anIt) {
422       for (GeomAPI_ShapeIterator aSub(*anIt); aSub.more(); aSub.next())
423         aBuilder->add(aResultShape, aSub.current());
424     }
425
426     if (theResult)
427       aBuilder->add(aResultShape, theResult);
428
429     theMakeShapeList->appendAlgo(aBuilder);
430   }
431   return aResultShape;
432 }
433
434 //=================================================================================================
435 int FeaturesPlugin_VersionedBoolean::version()
436 {
437   AttributeIntegerPtr aVersionAttr = integer(VERSION_ID());
438   int aVersion = 0;
439   if (aVersionAttr && aVersionAttr->isInitialized())
440     aVersion = aVersionAttr->value();
441   return aVersion;
442 }
443
444 //=================================================================================================
445
446 void FeaturesPlugin_VersionedBoolean::ObjectHierarchy::AddObject(const GeomShapePtr& theObject)
447 {
448   myObjects.push_back(theObject);
449 }
450
451 void FeaturesPlugin_VersionedBoolean::ObjectHierarchy::AddParent(const GeomShapePtr& theShape,
452                                                         const GeomShapePtr& theParent)
453 {
454   myParent[theShape] = theParent;
455
456   MapShapeToIndex::iterator aFound = myParentIndices.find(theParent);
457   size_t anIndex = myParentIndices.size();
458   if (aFound == myParentIndices.end()) {
459     myParentIndices[theParent] = anIndex;
460     mySubshapes.push_back(ShapeAndSubshapes(theParent, ListOfShape()));
461   } else
462     anIndex = aFound->second;
463
464   mySubshapes[anIndex].second.push_back(theShape);
465 }
466
467 GeomShapePtr FeaturesPlugin_VersionedBoolean::ObjectHierarchy::Parent(const GeomShapePtr& theShape,
468                                                              bool theMarkProcessed)
469 {
470   MapShapeToParent::const_iterator aFound = myParent.find(theShape);
471   GeomShapePtr aParent;
472   if (aFound != myParent.end()) {
473     aParent = aFound->second;
474     if (theMarkProcessed) {
475       // mark the parent and all its subs as processed by Boolean algorithm
476       myProcessedObjects.insert(aParent);
477       const ListOfShape& aSubs = mySubshapes[myParentIndices[aParent]].second;
478       for (ListOfShape::const_iterator anIt = aSubs.begin(); anIt != aSubs.end(); ++anIt)
479         myProcessedObjects.insert(*anIt);
480     }
481   }
482   return aParent;
483 }
484
485 void FeaturesPlugin_VersionedBoolean::ObjectHierarchy::ObjectsByType(
486     ListOfShape& theShapesByType,
487     ListOfShape& theOtherShapes,
488     const GeomAPI_Shape::ShapeType theMinType,
489     const GeomAPI_Shape::ShapeType theMaxType) const
490 {
491   if (theMinType > theMaxType)
492     return ObjectsByType(theShapesByType, theOtherShapes, theMaxType, theMinType);
493
494   // no need to select objects if whole range is specified
495   if (theMinType == GeomAPI_Shape::COMPOUND && theMaxType == GeomAPI_Shape::SHAPE) {
496     theShapesByType.insert(theShapesByType.end(), myObjects.begin(), myObjects.end());
497     return;
498   }
499
500   for (ListOfShape::const_iterator anIt = myObjects.begin(); anIt != myObjects.end(); ++anIt) {
501     GeomAPI_Shape::ShapeType aType = (*anIt)->shapeType();
502     if (aType >= theMinType && aType <= theMaxType)
503       theShapesByType.push_back(*anIt);
504     else
505       theOtherShapes.push_back(*anIt);
506   }
507 }
508
509
510 void FeaturesPlugin_VersionedBoolean::ObjectHierarchy::SplitCompound(const GeomShapePtr& theCompShape,
511                                                             ListOfShape& theUsed,
512                                                             ListOfShape& theNotUsed) const
513 {
514   theUsed.clear();
515   theNotUsed.clear();
516
517   MapShapeToIndex::const_iterator aFoundIndex = myParentIndices.find(theCompShape);
518   if (aFoundIndex == myParentIndices.end())
519     return; // no such shape
520
521   theUsed = mySubshapes[aFoundIndex->second].second;
522   SetOfShape aSubsSet;
523   aSubsSet.insert(theUsed.begin(), theUsed.end());
524
525   for (GeomAPI_ShapeIterator anExp(theCompShape); anExp.more(); anExp.next()) {
526     GeomShapePtr aCurrent = anExp.current();
527     if (aSubsSet.find(aCurrent) == aSubsSet.end())
528       theNotUsed.push_back(aCurrent);
529   }
530 }
531
532 bool FeaturesPlugin_VersionedBoolean::ObjectHierarchy::IsEmpty() const
533 {
534   return myObjects.empty();
535 }
536
537 void FeaturesPlugin_VersionedBoolean::ObjectHierarchy::CompoundsOfUnusedObjects(
538     ListOfShape& theDestination) const
539 {
540   SetOfShape aUsedObjects;
541   aUsedObjects.insert(myObjects.begin(), myObjects.end());
542
543   for (std::vector<ShapeAndSubshapes>::const_iterator anIt = mySubshapes.begin();
544        anIt != mySubshapes.end(); ++anIt) {
545     MapShapeToParent::const_iterator aParent = myParent.find(anIt->first);
546     if ((aParent == myParent.end() || !aParent->second) &&
547          anIt->first->shapeType() ==  GeomAPI_Shape::COMPOUND) {
548       // this is a top-level compound
549       GeomShapePtr aCompound = collectUnusedSubs(anIt->first, aUsedObjects);
550       // add to destination non-empty compounds only
551       if (aCompound)
552         theDestination.push_back(aCompound);
553     }
554   }
555 }
556
557 GeomShapePtr FeaturesPlugin_VersionedBoolean::ObjectHierarchy::collectUnusedSubs(
558     GeomShapePtr theTopLevelCompound,
559     const SetOfShape& theUsed) const
560 {
561   GeomShapePtr aResult = theTopLevelCompound->emptyCopied();
562   bool isResultEmpty = true;
563
564   for (GeomAPI_ShapeIterator aSub(theTopLevelCompound); aSub.more(); aSub.next()) {
565     GeomShapePtr aCurrent = aSub.current();
566     if (theUsed.find(aCurrent) != theUsed.end())
567       continue; // already used
568
569     MapShapeToIndex::const_iterator aFoundIndex = myParentIndices.find(aCurrent);
570     if (aCurrent->shapeType() > GeomAPI_Shape::COMPOUND ||
571         aFoundIndex == myParentIndices.end()) {
572       bool isAddShape = true;
573       // check compsolid is fully unused in the Boolean operation
574       if (aCurrent->shapeType() == GeomAPI_Shape::COMPSOLID) {
575         for (GeomAPI_ShapeIterator anIt(aCurrent); isAddShape && anIt.more(); anIt.next())
576           isAddShape = theUsed.find(anIt.current()) == theUsed.end();
577       }
578
579       if (isAddShape) { // low-level shape, add it
580         GeomAlgoAPI_ShapeBuilder::add(aResult, aCurrent);
581         isResultEmpty = false;
582       }
583     } else {
584       GeomShapePtr aCompound = collectUnusedSubs(aCurrent, theUsed);
585       if (aCompound) {
586         GeomAlgoAPI_ShapeBuilder::add(theTopLevelCompound, aCompound);
587         isResultEmpty = false;
588       }
589     }
590   }
591   return isResultEmpty ? GeomShapePtr() : aResult;
592 }
593
594
595 FeaturesPlugin_VersionedBoolean::ObjectHierarchy::Iterator FeaturesPlugin_VersionedBoolean::ObjectHierarchy::Begin()
596 {
597   return Iterator(this);
598 }
599
600 FeaturesPlugin_VersionedBoolean::ObjectHierarchy::Iterator FeaturesPlugin_VersionedBoolean::ObjectHierarchy::End()
601 {
602   return Iterator(this, false);
603 }
604
605 FeaturesPlugin_VersionedBoolean::ObjectHierarchy::Iterator::Iterator(
606     FeaturesPlugin_VersionedBoolean::ObjectHierarchy* theHierarchy, bool isBegin)
607   : myHierarchy(theHierarchy)
608 {
609   if (isBegin) {
610     myObject = myHierarchy->myObjects.begin();
611     SkipAlreadyProcessed();
612   } else
613     myObject = myHierarchy->myObjects.end();
614 }
615
616 void FeaturesPlugin_VersionedBoolean::ObjectHierarchy::Iterator::SkipAlreadyProcessed()
617 {
618   while (myObject != myHierarchy->myObjects.end() &&
619          myHierarchy->myProcessedObjects.find(*myObject) != myHierarchy->myProcessedObjects.end())
620     ++myObject;
621 }
622
623 bool FeaturesPlugin_VersionedBoolean::ObjectHierarchy::Iterator::operator==(const Iterator& theOther) const
624 {
625   return myObject == theOther.myObject;
626 }
627
628 bool FeaturesPlugin_VersionedBoolean::ObjectHierarchy::Iterator::operator!=(const Iterator& theOther) const
629 {
630   return !operator==(theOther);
631 }
632
633 FeaturesPlugin_VersionedBoolean::ObjectHierarchy::Iterator&
634 FeaturesPlugin_VersionedBoolean::ObjectHierarchy::Iterator::operator++()
635 {
636   ++myObject;
637   SkipAlreadyProcessed();
638   return *this;
639 }
640
641 FeaturesPlugin_VersionedBoolean::ObjectHierarchy::Iterator
642 FeaturesPlugin_VersionedBoolean::ObjectHierarchy::Iterator::operator++(int)
643 {
644   Iterator aCurrent;
645   aCurrent.myHierarchy = myHierarchy;
646   aCurrent.myObject = myObject;
647
648   // increase iterator
649   operator++();
650
651   return aCurrent;
652 }
653
654 GeomShapePtr FeaturesPlugin_VersionedBoolean::ObjectHierarchy::Iterator::operator*() const
655 {
656   myHierarchy->myProcessedObjects.insert(*myObject);
657   return *myObject;
658 }