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"
21 #include "FeaturesPlugin_Tools.h"
23 #include <ModelAPI_ResultBody.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_Tools.h>
27 #include <GeomAlgoAPI_Boolean.h>
28 #include <GeomAlgoAPI_CompoundBuilder.h>
29 #include <GeomAlgoAPI_MakeShapeCustom.h>
30 #include <GeomAlgoAPI_MakeShapeList.h>
31 #include <GeomAlgoAPI_Partition.h>
32 #include <GeomAlgoAPI_PaveFiller.h>
33 #include <GeomAlgoAPI_ShapeTools.h>
34 #include <GeomAlgoAPI_Tools.h>
36 #include <GeomAPI_Face.h>
37 #include <GeomAPI_ShapeExplorer.h>
38 #include <GeomAPI_ShapeIterator.h>
43 //=================================================================================================
44 FeaturesPlugin_BooleanFill::FeaturesPlugin_BooleanFill()
45 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_FILL)
49 //=================================================================================================
50 void FeaturesPlugin_BooleanFill::execute()
53 ListOfShape anObjects, aTools, anEdgesAndFaces, aPlanes;
54 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
57 AttributeSelectionListPtr anObjectsSelList =
58 selectionList(FeaturesPlugin_Boolean::OBJECT_LIST_ID());
59 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
60 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
61 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
65 ResultPtr aContext = anObjectAttr->context();
66 ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
67 if(aResCompSolidPtr.get()
68 && aResCompSolidPtr->shape()->shapeType() == GeomAPI_Shape::COMPSOLID) {
69 std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
70 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
71 anIt = aCompSolidsObjects.begin();
72 for(; anIt != aCompSolidsObjects.end(); anIt++) {
73 if(anIt->first->isEqual(aContextShape)) {
74 aCompSolidsObjects[anIt->first].push_back(anObject);
78 if(anIt == aCompSolidsObjects.end()) {
79 aCompSolidsObjects[aContextShape].push_back(anObject);
82 anObjects.push_back(anObject);
87 AttributeSelectionListPtr aToolsSelList = selectionList(FeaturesPlugin_Boolean::TOOL_LIST_ID());
88 for(int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
89 AttributeSelectionPtr aToolAttr = aToolsSelList->value(aToolsIndex);
90 GeomShapePtr aTool = aToolAttr->value();
92 // It could be a construction plane.
93 ResultPtr aContext = aToolAttr->context();
94 aPlanes.push_back(aToolAttr->context()->shape());
97 aTools.push_back(aTool);
101 int aResultIndex = 0;
103 if ((anObjects.empty() && aCompSolidsObjects.empty())
104 || (aTools.empty() && aPlanes.empty())) {
105 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
106 setError(aFeatureError);
110 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo> aResultBaseAlgoList;
111 ListOfShape aResultShapesList;
113 // For solids cut each object with all tools.
114 for(ListOfShape::iterator
115 anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
116 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
117 ListOfShape aListWithObject;
118 aListWithObject.push_back(anObject);
119 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
120 std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
121 GeomShapePtr aResShape;
123 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
124 GeomAlgoAPI_ShapeTools::getBoundingBox(aListWithObject, 1.0);
127 ListOfShape aToolsWithPlanes = aTools;
128 for(ListOfShape::const_iterator anIt = aPlanes.cbegin();
129 anIt != aPlanes.cend();
132 GeomShapePtr aPlane = *anIt;
133 GeomShapePtr aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPlane, aBoundingPoints);
134 std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(
135 new GeomAlgoAPI_MakeShapeCustom);
136 aMkShCustom->addModified(aPlane, aTool);
137 aMakeShapeList->appendAlgo(aMkShCustom);
138 aToolsWithPlanes.push_back(aTool);
141 aBoolAlgo.reset(new GeomAlgoAPI_Partition(aListWithObject, aToolsWithPlanes));
142 aResShape = aBoolAlgo->shape();
143 if (aResShape.get() && aResShape->shapeType() == GeomAPI_Shape::COMPOUND) {
144 int aSubResultsNb = 0;
145 GeomAPI_ShapeIterator anIt(aResShape);
146 for(; anIt.more(); anIt.next()) {
149 if(aSubResultsNb == 1) {
150 anIt.init(aResShape);
152 aResShape = anIt.current();
157 // Checking that the algorithm worked properly.
158 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
163 aMakeShapeList->appendAlgo(aBoolAlgo);
165 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
166 document()->createBody(data(), aResultIndex);
168 // tools should be added to the list to fulfill the correct history of modification
169 aListWithObject.insert(aListWithObject.end(), aTools.begin(), aTools.end());
171 ListOfShape aUsedTools = aTools;
172 aUsedTools.insert(aUsedTools.end(), aPlanes.begin(), aPlanes.end());
174 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aListWithObject, aUsedTools,
175 aMakeShapeList, aResShape);
176 setResult(aResultBody, aResultIndex);
179 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
180 aRBA.resultBody = aResultBody;
181 aRBA.baseShape = anObject;
182 aRBA.makeShape = aMakeShapeList;
183 aResultBaseAlgoList.push_back(aRBA);
184 aResultShapesList.push_back(aResShape);
187 // Compsolids handling
188 for(std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
189 anIt = aCompSolidsObjects.begin();
190 anIt != aCompSolidsObjects.end(); anIt++) {
191 std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
192 ListOfShape& aUsedInOperationSolids = anIt->second;
194 // Collecting solids from compsolids which will not be modified in boolean operation.
195 ListOfShape aNotUsedSolids;
196 for(GeomAPI_ShapeExplorer
197 anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
198 std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
199 ListOfShape::iterator anIt = aUsedInOperationSolids.begin();
200 for(; anIt != aUsedInOperationSolids.end(); anIt++) {
201 if(aSolidInCompSolid->isEqual(*anIt)) {
205 if(anIt == aUsedInOperationSolids.end()) {
206 aNotUsedSolids.push_back(aSolidInCompSolid);
210 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
211 std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
213 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
214 GeomAlgoAPI_ShapeTools::getBoundingBox(aUsedInOperationSolids, 1.0);
217 ListOfShape aToolsWithPlanes = aTools;
218 for(ListOfShape::const_iterator anIt = aPlanes.cbegin();
219 anIt != aPlanes.cend();
222 GeomShapePtr aPlane = *anIt;
223 GeomShapePtr aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPlane, aBoundingPoints);
224 std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(
225 new GeomAlgoAPI_MakeShapeCustom);
226 aMkShCustom->addModified(aPlane, aTool);
227 aMakeShapeList->appendAlgo(aMkShCustom);
228 aToolsWithPlanes.push_back(aTool);
231 aBoolAlgo.reset(new GeomAlgoAPI_Partition(aUsedInOperationSolids, aToolsWithPlanes));
233 // Checking that the algorithm worked properly.
234 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
239 aMakeShapeList->appendAlgo(aBoolAlgo);
240 GeomShapePtr aResultShape = aBoolAlgo->shape();
242 // Add result to not used solids from compsolid.
243 if(!aNotUsedSolids.empty()) {
244 ListOfShape aShapesToAdd = aNotUsedSolids;
245 aShapesToAdd.push_back(aBoolAlgo->shape());
246 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
247 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
248 if(!aFillerAlgo->isDone()) {
249 std::string aFeatureError = "Error: PaveFiller algorithm failed.";
250 setError(aFeatureError);
254 aMakeShapeList->appendAlgo(aFillerAlgo);
255 aResultShape = aFillerAlgo->shape();
258 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
259 document()->createBody(data(), aResultIndex);
261 ListOfShape aUsedTools = aTools;
262 aUsedTools.insert(aUsedTools.end(), aPlanes.begin(), aPlanes.end());
264 ListOfShape aBaseShapes;
265 aBaseShapes.push_back(aCompSolid);
266 // tools should be added to the list to fulfill the correct history of modification
267 aBaseShapes.insert(aBaseShapes.end(), aTools.begin(), aTools.end());
269 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShapes, aUsedTools,
270 aMakeShapeList, aResultShape);
271 setResult(aResultBody, aResultIndex);
274 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
275 aRBA.resultBody = aResultBody;
276 aRBA.baseShape = aCompSolid;
277 aRBA.makeShape = aMakeShapeList;
278 aResultBaseAlgoList.push_back(aRBA);
279 aResultShapesList.push_back(aResultShape);
282 // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
283 // result shape has been deleted, but in another it was modified or stayed.
284 GeomShapePtr aResultShapesCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
285 FeaturesPlugin_Tools::loadDeletedShapes(aResultBaseAlgoList, aTools, aResultShapesCompound);
287 // remove the rest results if there were produced in the previous pass
288 removeResults(aResultIndex);