1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "FeaturesPlugin_BooleanFill.h"
23 #include <ModelAPI_ResultBody.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_Tools.h>
27 #include <GeomAlgoAPI_Boolean.h>
28 #include <GeomAlgoAPI_MakeShapeCustom.h>
29 #include <GeomAlgoAPI_MakeShapeList.h>
30 #include <GeomAlgoAPI_Partition.h>
31 #include <GeomAlgoAPI_PaveFiller.h>
32 #include <GeomAlgoAPI_ShapeTools.h>
33 #include <GeomAlgoAPI_Tools.h>
35 #include <GeomAPI_Face.h>
36 #include <GeomAPI_ShapeExplorer.h>
37 #include <GeomAPI_ShapeIterator.h>
42 //=================================================================================================
43 FeaturesPlugin_BooleanFill::FeaturesPlugin_BooleanFill()
44 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_FILL)
48 //=================================================================================================
49 void FeaturesPlugin_BooleanFill::execute()
52 ListOfShape anObjects, aTools, anEdgesAndFaces, aPlanes;
53 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
56 AttributeSelectionListPtr anObjectsSelList =
57 selectionList(FeaturesPlugin_Boolean::OBJECT_LIST_ID());
58 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
59 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
60 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
64 ResultPtr aContext = anObjectAttr->context();
65 ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
66 if(aResCompSolidPtr.get()
67 && aResCompSolidPtr->shape()->shapeType() == GeomAPI_Shape::COMPSOLID) {
68 std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
69 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
70 anIt = aCompSolidsObjects.begin();
71 for(; anIt != aCompSolidsObjects.end(); anIt++) {
72 if(anIt->first->isEqual(aContextShape)) {
73 aCompSolidsObjects[anIt->first].push_back(anObject);
77 if(anIt == aCompSolidsObjects.end()) {
78 aCompSolidsObjects[aContextShape].push_back(anObject);
81 anObjects.push_back(anObject);
86 AttributeSelectionListPtr aToolsSelList = selectionList(FeaturesPlugin_Boolean::TOOL_LIST_ID());
87 for(int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
88 AttributeSelectionPtr aToolAttr = aToolsSelList->value(aToolsIndex);
89 GeomShapePtr aTool = aToolAttr->value();
91 // It could be a construction plane.
92 ResultPtr aContext = aToolAttr->context();
93 aPlanes.push_back(aToolAttr->context()->shape());
96 aTools.push_back(aTool);
100 int aResultIndex = 0;
102 if ((anObjects.empty() && aCompSolidsObjects.empty())
103 || (aTools.empty() && aPlanes.empty())) {
104 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
105 setError(aFeatureError);
109 // For solids cut each object with all tools.
110 for(ListOfShape::iterator
111 anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
112 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
113 ListOfShape aListWithObject;
114 aListWithObject.push_back(anObject);
115 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
116 std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
117 GeomShapePtr aResShape;
119 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
120 GeomAlgoAPI_ShapeTools::getBoundingBox(aListWithObject, 1.0);
123 ListOfShape aToolsWithPlanes = aTools;
124 for(ListOfShape::const_iterator anIt = aPlanes.cbegin();
125 anIt != aPlanes.cend();
128 GeomShapePtr aPlane = *anIt;
129 GeomShapePtr aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPlane, aBoundingPoints);
130 std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(
131 new GeomAlgoAPI_MakeShapeCustom);
132 aMkShCustom->addModified(aPlane, aTool);
133 aMakeShapeList->appendAlgo(aMkShCustom);
134 aToolsWithPlanes.push_back(aTool);
137 aBoolAlgo.reset(new GeomAlgoAPI_Partition(aListWithObject, aToolsWithPlanes));
138 aResShape = aBoolAlgo->shape();
139 if (aResShape.get() && aResShape->shapeType() == GeomAPI_Shape::COMPOUND) {
140 int aSubResultsNb = 0;
141 GeomAPI_ShapeIterator anIt(aResShape);
142 for(; anIt.more(); anIt.next()) {
145 if(aSubResultsNb == 1) {
146 anIt.init(aResShape);
148 aResShape = anIt.current();
153 // Checking that the algorithm worked properly.
154 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
159 aMakeShapeList->appendAlgo(aBoolAlgo);
161 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
162 document()->createBody(data(), aResultIndex);
164 ListOfShape aUsedTools = aTools;
165 aUsedTools.insert(aUsedTools.end(), aPlanes.begin(), aPlanes.end());
167 loadNamingDS(aResultBody, anObject, aUsedTools, aResShape, aMakeShapeList);
168 setResult(aResultBody, aResultIndex);
172 // Compsolids handling
173 for(std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
174 anIt = aCompSolidsObjects.begin();
175 anIt != aCompSolidsObjects.end(); anIt++) {
176 std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
177 ListOfShape& aUsedInOperationSolids = anIt->second;
179 // Collecting solids from compsolids which will not be modified in boolean operation.
180 ListOfShape aNotUsedSolids;
181 for(GeomAPI_ShapeExplorer
182 anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
183 std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
184 ListOfShape::iterator anIt = aUsedInOperationSolids.begin();
185 for(; anIt != aUsedInOperationSolids.end(); anIt++) {
186 if(aSolidInCompSolid->isEqual(*anIt)) {
190 if(anIt == aUsedInOperationSolids.end()) {
191 aNotUsedSolids.push_back(aSolidInCompSolid);
195 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
196 std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
198 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
199 GeomAlgoAPI_ShapeTools::getBoundingBox(aUsedInOperationSolids, 1.0);
202 ListOfShape aToolsWithPlanes = aTools;
203 for(ListOfShape::const_iterator anIt = aPlanes.cbegin();
204 anIt != aPlanes.cend();
207 GeomShapePtr aPlane = *anIt;
208 GeomShapePtr aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPlane, aBoundingPoints);
209 std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(
210 new GeomAlgoAPI_MakeShapeCustom);
211 aMkShCustom->addModified(aPlane, aTool);
212 aMakeShapeList->appendAlgo(aMkShCustom);
213 aToolsWithPlanes.push_back(aTool);
216 aBoolAlgo.reset(new GeomAlgoAPI_Partition(aUsedInOperationSolids, aToolsWithPlanes));
218 // Checking that the algorithm worked properly.
219 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
224 aMakeShapeList->appendAlgo(aBoolAlgo);
225 GeomShapePtr aResultShape = aBoolAlgo->shape();
227 // Add result to not used solids from compsolid.
228 if(!aNotUsedSolids.empty()) {
229 ListOfShape aShapesToAdd = aNotUsedSolids;
230 aShapesToAdd.push_back(aBoolAlgo->shape());
231 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
232 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
233 if(!aFillerAlgo->isDone()) {
234 std::string aFeatureError = "Error: PaveFiller algorithm failed.";
235 setError(aFeatureError);
239 aMakeShapeList->appendAlgo(aFillerAlgo);
240 aResultShape = aFillerAlgo->shape();
243 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
244 document()->createBody(data(), aResultIndex);
246 ListOfShape aUsedTools = aTools;
247 aUsedTools.insert(aUsedTools.end(), aPlanes.begin(), aPlanes.end());
249 loadNamingDS(aResultBody,
254 setResult(aResultBody, aResultIndex);
258 // remove the rest results if there were produced in the previous pass
259 removeResults(aResultIndex);