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