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_BooleanFill.h"
22 #include <ModelAPI_ResultBody.h>
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_Tools.h>
26 #include <GeomAlgoAPI_Boolean.h>
27 #include <GeomAlgoAPI_MakeShapeCustom.h>
28 #include <GeomAlgoAPI_MakeShapeList.h>
29 #include <GeomAlgoAPI_Partition.h>
30 #include <GeomAlgoAPI_PaveFiller.h>
31 #include <GeomAlgoAPI_ShapeTools.h>
32 #include <GeomAlgoAPI_Tools.h>
34 #include <GeomAPI_Face.h>
35 #include <GeomAPI_ShapeExplorer.h>
36 #include <GeomAPI_ShapeIterator.h>
41 //=================================================================================================
42 FeaturesPlugin_BooleanFill::FeaturesPlugin_BooleanFill()
43 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_FILL)
47 //=================================================================================================
48 void FeaturesPlugin_BooleanFill::execute()
51 ListOfShape anObjects, aTools, anEdgesAndFaces, aPlanes;
52 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
55 AttributeSelectionListPtr anObjectsSelList =
56 selectionList(FeaturesPlugin_Boolean::OBJECT_LIST_ID());
57 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
58 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
59 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
63 ResultPtr aContext = anObjectAttr->context();
64 ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
65 if(aResCompSolidPtr.get()
66 && aResCompSolidPtr->shape()->shapeType() == GeomAPI_Shape::COMPSOLID) {
67 std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
68 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
69 anIt = aCompSolidsObjects.begin();
70 for(; anIt != aCompSolidsObjects.end(); anIt++) {
71 if(anIt->first->isEqual(aContextShape)) {
72 aCompSolidsObjects[anIt->first].push_back(anObject);
76 if(anIt == aCompSolidsObjects.end()) {
77 aCompSolidsObjects[aContextShape].push_back(anObject);
80 anObjects.push_back(anObject);
85 AttributeSelectionListPtr aToolsSelList = selectionList(FeaturesPlugin_Boolean::TOOL_LIST_ID());
86 for(int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
87 AttributeSelectionPtr aToolAttr = aToolsSelList->value(aToolsIndex);
88 GeomShapePtr aTool = aToolAttr->value();
90 // It could be a construction plane.
91 ResultPtr aContext = aToolAttr->context();
92 aPlanes.push_back(aToolAttr->context()->shape());
95 aTools.push_back(aTool);
101 if ((anObjects.empty() && aCompSolidsObjects.empty())
102 || (aTools.empty() && aPlanes.empty())) {
103 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
104 setError(aFeatureError);
108 // For solids cut each object with all tools.
109 for(ListOfShape::iterator
110 anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
111 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
112 ListOfShape aListWithObject;
113 aListWithObject.push_back(anObject);
114 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
115 std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
116 GeomShapePtr aResShape;
118 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
119 GeomAlgoAPI_ShapeTools::getBoundingBox(aListWithObject, 1.0);
122 ListOfShape aToolsWithPlanes = aTools;
123 for(ListOfShape::const_iterator anIt = aPlanes.cbegin();
124 anIt != aPlanes.cend();
127 GeomShapePtr aPlane = *anIt;
128 GeomShapePtr aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPlane, aBoundingPoints);
129 std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(
130 new GeomAlgoAPI_MakeShapeCustom);
131 aMkShCustom->addModified(aPlane, aTool);
132 aMakeShapeList->appendAlgo(aMkShCustom);
133 aToolsWithPlanes.push_back(aTool);
136 aBoolAlgo.reset(new GeomAlgoAPI_Partition(aListWithObject, aToolsWithPlanes));
137 aResShape = aBoolAlgo->shape();
138 if (aResShape.get() && aResShape->shapeType() == GeomAPI_Shape::COMPOUND) {
139 int aSubResultsNb = 0;
140 GeomAPI_ShapeIterator anIt(aResShape);
141 for(; anIt.more(); anIt.next()) {
144 if(aSubResultsNb == 1) {
145 anIt.init(aResShape);
147 aResShape = anIt.current();
152 // Checking that the algorithm worked properly.
153 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
158 aMakeShapeList->appendAlgo(aBoolAlgo);
160 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
161 document()->createBody(data(), aResultIndex);
163 ListOfShape aUsedTools = aTools;
164 aUsedTools.insert(aUsedTools.end(), aPlanes.begin(), aPlanes.end());
166 loadNamingDS(aResultBody, anObject, aUsedTools, aResShape, aMakeShapeList);
167 setResult(aResultBody, aResultIndex);
171 // Compsolids handling
172 for(std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
173 anIt = aCompSolidsObjects.begin();
174 anIt != aCompSolidsObjects.end(); anIt++) {
175 std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
176 ListOfShape& aUsedInOperationSolids = anIt->second;
178 // Collecting solids from compsolids which will not be modified in boolean operation.
179 ListOfShape aNotUsedSolids;
180 for(GeomAPI_ShapeExplorer
181 anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
182 std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
183 ListOfShape::iterator anIt = aUsedInOperationSolids.begin();
184 for(; anIt != aUsedInOperationSolids.end(); anIt++) {
185 if(aSolidInCompSolid->isEqual(*anIt)) {
189 if(anIt == aUsedInOperationSolids.end()) {
190 aNotUsedSolids.push_back(aSolidInCompSolid);
194 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
195 std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
197 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
198 GeomAlgoAPI_ShapeTools::getBoundingBox(aUsedInOperationSolids, 1.0);
201 ListOfShape aToolsWithPlanes = aTools;
202 for(ListOfShape::const_iterator anIt = aPlanes.cbegin();
203 anIt != aPlanes.cend();
206 GeomShapePtr aPlane = *anIt;
207 GeomShapePtr aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPlane, aBoundingPoints);
208 std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(
209 new GeomAlgoAPI_MakeShapeCustom);
210 aMkShCustom->addModified(aPlane, aTool);
211 aMakeShapeList->appendAlgo(aMkShCustom);
212 aToolsWithPlanes.push_back(aTool);
215 aBoolAlgo.reset(new GeomAlgoAPI_Partition(aUsedInOperationSolids, aToolsWithPlanes));
217 // Checking that the algorithm worked properly.
218 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
223 aMakeShapeList->appendAlgo(aBoolAlgo);
224 GeomShapePtr aResultShape = aBoolAlgo->shape();
226 // Add result to not used solids from compsolid.
227 if(!aNotUsedSolids.empty()) {
228 ListOfShape aShapesToAdd = aNotUsedSolids;
229 aShapesToAdd.push_back(aBoolAlgo->shape());
230 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
231 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
232 if(!aFillerAlgo->isDone()) {
233 std::string aFeatureError = "Error: PaveFiller algorithm failed.";
234 setError(aFeatureError);
238 aMakeShapeList->appendAlgo(aFillerAlgo);
239 aResultShape = aFillerAlgo->shape();
242 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
243 document()->createBody(data(), aResultIndex);
245 ListOfShape aUsedTools = aTools;
246 aUsedTools.insert(aUsedTools.end(), aPlanes.begin(), aPlanes.end());
248 loadNamingDS(aResultBody,
253 setResult(aResultBody, aResultIndex);
257 // remove the rest results if there were produced in the previous pass
258 removeResults(aResultIndex);