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