1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "FeaturesPlugin_Boolean.h"
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>
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>
48 //=================================================================================================
49 FeaturesPlugin_Boolean::FeaturesPlugin_Boolean(const OperationType theOperationType)
50 : myOperationType(theOperationType)
54 //=================================================================================================
55 void FeaturesPlugin_Boolean::initAttributes()
57 AttributeSelectionListPtr aSelection =
58 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
59 FeaturesPlugin_Boolean::OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
61 aSelection = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
62 FeaturesPlugin_Boolean::TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
64 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), OBJECT_LIST_ID());
65 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TOOL_LIST_ID());
68 //=================================================================================================
69 void FeaturesPlugin_Boolean::initVersion(const int theVersion)
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);
83 //=================================================================================================
84 FeaturesPlugin_Boolean::OperationType FeaturesPlugin_Boolean::operationType()
86 return myOperationType;
89 //=================================================================================================
90 void FeaturesPlugin_Boolean::parentForShape(const GeomShapePtr& theShape,
91 const ResultPtr& theContext,
92 ObjectHierarchy& theShapesHierarchy)
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);
104 bool FeaturesPlugin_Boolean::processAttribute(const std::string& theAttributeName,
105 ObjectHierarchy& theObjects,
106 ListOfShape& thePlanesList)
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);
123 theObjects.AddObject(anObject);
125 ResultPtr aContext = anObjectAttr->context();
126 parentForShape(anObject, aContext, theObjects);
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)
139 if(theBaseShape->isEqual(theResultShape)) {
140 theResultBody->store(theResultShape, false);
144 theResultBody->storeModified(theBaseShape, theResultShape);
146 theResultBody->loadModifiedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::EDGE);
147 theResultBody->loadModifiedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::FACE);
149 theResultBody->loadDeletedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::FACE);
151 for (ListOfShape::const_iterator anIter = theTools.begin();
152 anIter != theTools.end();
155 GeomAPI_Shape::ShapeType aShapeType =
156 (*anIter)->shapeType() <= GeomAPI_Shape::FACE ? GeomAPI_Shape::FACE
157 : GeomAPI_Shape::EDGE;
158 theResultBody->loadModifiedShapes(theMakeShape, *anIter, aShapeType);
160 theResultBody->loadDeletedShapes(theMakeShape, *anIter, GeomAPI_Shape::FACE);
164 //=================================================================================================
165 bool FeaturesPlugin_Boolean::processObject(
166 const GeomAlgoAPI_Tools::BOPType theBooleanType,
167 const GeomShapePtr& theObject,
168 const ListOfShape& theTools,
169 const ListOfShape& thePlanes,
171 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
172 ListOfShape& theResultShapesList,
173 GeomShapePtr theResultCompound)
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;
181 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
182 GeomAlgoAPI_ShapeTools::getBoundingBox(aListWithObject, 1.0);
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);
196 if (theBooleanType == GeomAlgoAPI_Tools::BOOL_PARTITION)
197 aBoolAlgo.reset(new GeomAlgoAPI_Partition(aListWithObject, aToolsWithPlanes));
199 aBoolAlgo.reset(new GeomAlgoAPI_Boolean(aListWithObject,
203 // Checking that the algorithm worked properly.
205 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
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())
217 if (aSubResultsNb == 1) {
218 anIt.init(aResShape);
220 aResShape = anIt.current();
224 aMakeShapeList->appendAlgo(aBoolAlgo);
226 GeomAPI_ShapeIterator aShapeIt(aResShape);
227 if (aShapeIt.more() || aResShape->shapeType() == GeomAPI_Shape::VERTEX) {
228 std::shared_ptr<ModelAPI_ResultBody> aResultBody;
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);
235 else { // create a separate ResultBody
236 aResultBody = document()->createBody(data(), theResultIndex);
238 // tools should be added to the list to fulfill the correct history of modification
239 aListWithObject.insert(aListWithObject.end(), theTools.begin(), theTools.end());
241 ListOfShape aUsedTools = theTools;
242 aUsedTools.insert(aUsedTools.end(), thePlanes.begin(), thePlanes.end());
244 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
249 setResult(aResultBody, theResultIndex);
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);
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,
272 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
273 ListOfShape& theResultShapesList,
274 GeomShapePtr theResultCompound)
276 ListOfShape aUsedInOperationSolids;
277 ListOfShape aNotUsedSolids;
278 theCompsolidHierarchy.SplitCompound(theCompsolid, aUsedInOperationSolids, aNotUsedSolids);
280 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
282 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
283 GeomAlgoAPI_ShapeTools::getBoundingBox(aUsedInOperationSolids, 1.0);
286 ListOfShape aToolsWithPlanes = theTools;
287 for (ListOfShape::const_iterator anIt = thePlanes.begin(); anIt != thePlanes.end(); ++anIt)
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);
298 std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
299 if (theBooleanType == GeomAlgoAPI_Tools::BOOL_PARTITION)
300 aBoolAlgo.reset(new GeomAlgoAPI_Partition(aUsedInOperationSolids, aToolsWithPlanes));
302 aBoolAlgo.reset(new GeomAlgoAPI_Boolean(aUsedInOperationSolids,
306 // Checking that the algorithm worked properly.
308 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
313 aMakeShapeList->appendAlgo(aBoolAlgo);
314 GeomShapePtr aResultShape = aBoolAlgo->shape();
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);
328 aMakeShapeList->appendAlgo(aFillerAlgo);
329 aResultShape = aFillerAlgo->shape();
332 GeomAPI_ShapeIterator aShapeIt(aResultShape);
333 if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX)
335 std::shared_ptr<ModelAPI_ResultBody> aResultBody;
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);
342 else { // create a separate ResultBody
343 aResultBody = document()->createBody(data(), theResultIndex);
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());
350 ListOfShape aUsedTools = theTools;
351 aUsedTools.insert(aUsedTools.end(), thePlanes.begin(), thePlanes.end());
353 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
358 setResult(aResultBody, theResultIndex);
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);
372 //=================================================================================================
373 bool FeaturesPlugin_Boolean::processCompound(
374 const GeomAlgoAPI_Tools::BOPType theBooleanType,
375 const ObjectHierarchy& theCompoundHierarchy,
376 const GeomShapePtr& theCompound,
377 const ListOfShape& theTools,
379 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
380 ListOfShape& theResultShapesList,
381 GeomShapePtr theResultCompound)
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();
392 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
393 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(
394 new GeomAlgoAPI_Boolean(aUsedInOperationShapes,
398 // Checking that the algorithm worked properly.
400 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
405 aMakeShapeList->appendAlgo(aBoolAlgo);
406 GeomShapePtr aResultShape = aBoolAlgo->shape();
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());
417 aShapesForResult.push_back(aResultShape);
420 if (aShapesForResult.size() == 1) {
421 aResultShape = aShapesForResult.front();
424 aResultShape = GeomAlgoAPI_CompoundBuilder::compound(aShapesForResult);
428 GeomAPI_ShapeIterator aShapeIt(aResultShape);
429 if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX) {
430 std::shared_ptr<ModelAPI_ResultBody> aResultBody;
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);
437 else { // create a separate ResultBody
438 aResultBody = document()->createBody(data(), theResultIndex);
440 ListOfShape aCompoundList;
441 aCompoundList.push_back(theCompound);
442 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
447 setResult(aResultBody, theResultIndex);
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);
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)
468 ListOfShape aCompounds;
469 theObjectsHierarchy.CompoundsOfUnusedObjects(aCompounds);
470 theToolsHierarchy.CompoundsOfUnusedObjects(aCompounds);
472 GeomShapePtr aResultShape = theResult;
473 if (!aCompounds.empty()) {
474 aResultShape = aCompounds.front();
475 aCompounds.pop_front();
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());
484 aBuilder->add(aResultShape, theResult);
486 theMakeShapeList->appendAlgo(aBuilder);
491 //=================================================================================================
492 void FeaturesPlugin_Boolean::storeResult(
493 const ListOfShape& theObjects,
494 const ListOfShape& theTools,
495 const GeomShapePtr theResultShape,
497 std::shared_ptr<GeomAlgoAPI_MakeShapeList> theMakeShapeList,
498 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList)
503 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
504 document()->createBody(data(), theResultIndex);
506 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
511 setResult(aResultBody, theResultIndex++);
514 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
515 aRBA.resultBody = aResultBody;
516 aRBA.baseShape = theObjects.front();
517 for (std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>::iterator
518 aRBAIt = theResultBaseAlgoList.begin();
519 aRBAIt != theResultBaseAlgoList.end(); ++aRBAIt) {
520 theMakeShapeList->appendAlgo(aRBAIt->makeShape);
522 aRBA.makeShape = theMakeShapeList;
523 theResultBaseAlgoList.clear();
524 theResultBaseAlgoList.push_back(aRBA);
527 //=================================================================================================
528 int FeaturesPlugin_Boolean::version()
530 AttributeIntegerPtr aVersionAttr = integer(VERSION_ID());
532 if (aVersionAttr && aVersionAttr->isInitialized())
533 aVersion = aVersionAttr->value();
537 //=================================================================================================
539 void FeaturesPlugin_Boolean::ObjectHierarchy::AddObject(const GeomShapePtr& theObject)
541 myObjects.push_back(theObject);
544 void FeaturesPlugin_Boolean::ObjectHierarchy::AddParent(const GeomShapePtr& theShape,
545 const GeomShapePtr& theParent)
547 myParent[theShape] = theParent;
549 MapShapeToIndex::iterator aFound = myParentIndices.find(theParent);
550 size_t anIndex = myParentIndices.size();
551 if (aFound == myParentIndices.end()) {
552 myParentIndices[theParent] = anIndex;
553 mySubshapes.push_back(ShapeAndSubshapes(theParent, ListOfShape()));
555 anIndex = aFound->second;
557 mySubshapes[anIndex].second.push_back(theShape);
560 GeomShapePtr FeaturesPlugin_Boolean::ObjectHierarchy::Parent(const GeomShapePtr& theShape,
561 bool theMarkProcessed)
563 MapShapeToParent::const_iterator aFound = myParent.find(theShape);
564 GeomShapePtr aParent;
565 if (aFound != myParent.end()) {
566 aParent = aFound->second;
567 if (theMarkProcessed) {
568 // mark the parent and all its subs as processed by Boolean algorithm
569 myProcessedObjects.insert(aParent);
570 const ListOfShape& aSubs = mySubshapes[myParentIndices[aParent]].second;
571 for (ListOfShape::const_iterator anIt = aSubs.begin(); anIt != aSubs.end(); ++anIt)
572 myProcessedObjects.insert(*anIt);
578 void FeaturesPlugin_Boolean::ObjectHierarchy::ObjectsByType(
579 ListOfShape& theShapesByType,
580 ListOfShape& theOtherShapes,
581 const GeomAPI_Shape::ShapeType theMinType,
582 const GeomAPI_Shape::ShapeType theMaxType) const
584 if (theMinType > theMaxType)
585 return ObjectsByType(theShapesByType, theOtherShapes, theMaxType, theMinType);
587 // no need to select objects if whole range is specified
588 if (theMinType == GeomAPI_Shape::COMPOUND && theMaxType == GeomAPI_Shape::SHAPE) {
589 theShapesByType.insert(theShapesByType.end(), myObjects.begin(), myObjects.end());
593 for (ListOfShape::const_iterator anIt = myObjects.begin(); anIt != myObjects.end(); ++anIt) {
594 GeomAPI_Shape::ShapeType aType = (*anIt)->shapeType();
595 if (aType >= theMinType && aType <= theMaxType)
596 theShapesByType.push_back(*anIt);
598 theOtherShapes.push_back(*anIt);
603 void FeaturesPlugin_Boolean::ObjectHierarchy::SplitCompound(const GeomShapePtr& theCompShape,
604 ListOfShape& theUsed,
605 ListOfShape& theNotUsed) const
610 MapShapeToIndex::const_iterator aFoundIndex = myParentIndices.find(theCompShape);
611 if (aFoundIndex == myParentIndices.end())
612 return; // no such shape
614 theUsed = mySubshapes[aFoundIndex->second].second;
616 aSubsSet.insert(theUsed.begin(), theUsed.end());
618 for (GeomAPI_ShapeIterator anExp(theCompShape); anExp.more(); anExp.next()) {
619 GeomShapePtr aCurrent = anExp.current();
620 if (aSubsSet.find(aCurrent) == aSubsSet.end())
621 theNotUsed.push_back(aCurrent);
625 bool FeaturesPlugin_Boolean::ObjectHierarchy::IsEmpty() const
627 return myObjects.empty();
630 void FeaturesPlugin_Boolean::ObjectHierarchy::CompoundsOfUnusedObjects(
631 ListOfShape& theDestination) const
633 SetOfShape aUsedObjects;
634 aUsedObjects.insert(myObjects.begin(), myObjects.end());
636 for (std::vector<ShapeAndSubshapes>::const_iterator anIt = mySubshapes.begin();
637 anIt != mySubshapes.end(); ++anIt) {
638 MapShapeToParent::const_iterator aParent = myParent.find(anIt->first);
639 if ((aParent == myParent.end() || !aParent->second) &&
640 anIt->first->shapeType() == GeomAPI_Shape::COMPOUND) {
641 // this is a top-level compound
642 GeomShapePtr aCompound = collectUnusedSubs(anIt->first, aUsedObjects);
643 // add to destination non-empty compounds only
645 theDestination.push_back(aCompound);
650 GeomShapePtr FeaturesPlugin_Boolean::ObjectHierarchy::collectUnusedSubs(
651 GeomShapePtr theTopLevelCompound,
652 const SetOfShape& theUsed) const
654 GeomShapePtr aResult = theTopLevelCompound->emptyCopied();
655 bool isResultEmpty = true;
657 for (GeomAPI_ShapeIterator aSub(theTopLevelCompound); aSub.more(); aSub.next()) {
658 GeomShapePtr aCurrent = aSub.current();
659 if (theUsed.find(aCurrent) != theUsed.end())
660 continue; // already used
662 MapShapeToIndex::const_iterator aFoundIndex = myParentIndices.find(aCurrent);
663 if (aCurrent->shapeType() > GeomAPI_Shape::COMPOUND ||
664 aFoundIndex == myParentIndices.end()) {
665 bool isAddShape = true;
666 // check compsolid is fully unused in the Boolean operation
667 if (aCurrent->shapeType() == GeomAPI_Shape::COMPSOLID) {
668 for (GeomAPI_ShapeIterator anIt(aCurrent); isAddShape && anIt.more(); anIt.next())
669 isAddShape = theUsed.find(anIt.current()) == theUsed.end();
672 if (isAddShape) { // low-level shape, add it
673 GeomAlgoAPI_ShapeBuilder::add(aResult, aCurrent);
674 isResultEmpty = false;
677 GeomShapePtr aCompound = collectUnusedSubs(aCurrent, theUsed);
679 GeomAlgoAPI_ShapeBuilder::add(theTopLevelCompound, aCompound);
680 isResultEmpty = false;
684 return isResultEmpty ? GeomShapePtr() : aResult;
688 FeaturesPlugin_Boolean::ObjectHierarchy::Iterator FeaturesPlugin_Boolean::ObjectHierarchy::Begin()
690 return Iterator(this);
693 FeaturesPlugin_Boolean::ObjectHierarchy::Iterator FeaturesPlugin_Boolean::ObjectHierarchy::End()
695 return Iterator(this, false);
698 FeaturesPlugin_Boolean::ObjectHierarchy::Iterator::Iterator(
699 FeaturesPlugin_Boolean::ObjectHierarchy* theHierarchy, bool isBegin)
700 : myHierarchy(theHierarchy)
703 myObject = myHierarchy->myObjects.begin();
704 SkipAlreadyProcessed();
706 myObject = myHierarchy->myObjects.end();
709 void FeaturesPlugin_Boolean::ObjectHierarchy::Iterator::SkipAlreadyProcessed()
711 while (myObject != myHierarchy->myObjects.end() &&
712 myHierarchy->myProcessedObjects.find(*myObject) != myHierarchy->myProcessedObjects.end())
716 bool FeaturesPlugin_Boolean::ObjectHierarchy::Iterator::operator==(const Iterator& theOther) const
718 return myObject == theOther.myObject;
721 bool FeaturesPlugin_Boolean::ObjectHierarchy::Iterator::operator!=(const Iterator& theOther) const
723 return !operator==(theOther);
726 FeaturesPlugin_Boolean::ObjectHierarchy::Iterator&
727 FeaturesPlugin_Boolean::ObjectHierarchy::Iterator::operator++()
730 SkipAlreadyProcessed();
734 FeaturesPlugin_Boolean::ObjectHierarchy::Iterator
735 FeaturesPlugin_Boolean::ObjectHierarchy::Iterator::operator++(int)
738 aCurrent.myHierarchy = myHierarchy;
739 aCurrent.myObject = myObject;
747 GeomShapePtr FeaturesPlugin_Boolean::ObjectHierarchy::Iterator::operator*() const
749 myHierarchy->myProcessedObjects.insert(*myObject);