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_BooleanCommon.h"
23 #include <ModelAPI_ResultBody.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_AttributeString.h>
26 #include <ModelAPI_Tools.h>
28 #include <GeomAlgoAPI_Boolean.h>
29 #include <GeomAlgoAPI_MakeShapeCustom.h>
30 #include <GeomAlgoAPI_MakeShapeList.h>
31 #include <GeomAlgoAPI_ShapeTools.h>
32 #include <GeomAPI_Face.h>
33 #include <GeomAPI_ShapeIterator.h>
34 #include <GeomAPI_ShapeExplorer.h>
35 #include <GeomAlgoAPI_PaveFiller.h>
36 #include <GeomAlgoAPI_CompoundBuilder.h>
39 //==================================================================================================
40 FeaturesPlugin_BooleanCommon::FeaturesPlugin_BooleanCommon()
41 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_COMMON)
45 //==================================================================================================
46 void FeaturesPlugin_BooleanCommon::initAttributes()
48 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
50 data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
51 data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
54 //==================================================================================================
55 void FeaturesPlugin_BooleanCommon::execute()
57 ListOfShape anObjects, aTools, aPlanes;
58 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
59 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompoundObjects;
61 bool isSimpleMode = false;
63 AttributeStringPtr aCreationMethodAttr = string(CREATION_METHOD());
64 if (aCreationMethodAttr.get()
65 && aCreationMethodAttr->value() == CREATION_METHOD_SIMPLE()) {
70 AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECT_LIST_ID());
71 for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
72 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
73 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
74 if (!anObject.get()) {
77 ResultPtr aContext = anObjectAttr->context();
78 ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
79 if (aResCompSolidPtr.get())
81 std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
82 GeomAPI_Shape::ShapeType aShapeType = aResCompSolidPtr->shape()->shapeType();
83 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>& aMap =
84 aShapeType == GeomAPI_Shape::COMPSOLID ? aCompSolidsObjects : aCompoundObjects;
86 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
88 for (; anIt != aMap.end(); anIt++) {
89 if (anIt->first->isEqual(aContextShape)) {
90 aMap[anIt->first].push_back(anObject);
94 if (anIt == aMap.end()) {
95 aMap[aContextShape].push_back(anObject);
100 anObjects.push_back(anObject);
106 AttributeSelectionListPtr aToolsSelList = selectionList(FeaturesPlugin_Boolean::TOOL_LIST_ID());
107 for (int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
108 AttributeSelectionPtr aToolAttr = aToolsSelList->value(aToolsIndex);
109 GeomShapePtr aTool = aToolAttr->value();
111 // It could be a construction plane.
112 ResultPtr aContext = aToolAttr->context();
113 aPlanes.push_back(aToolAttr->context()->shape());
115 aTools.push_back(aTool);
120 if ((anObjects.empty() && aCompSolidsObjects.empty())
121 || (!isSimpleMode && aTools.empty() && aPlanes.empty())) {
122 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
123 setError(aFeatureError);
127 int aResultIndex = 0;
128 GeomAlgoAPI_MakeShapeList aMakeShapeList;
129 GeomAPI_DataMapOfShapeShape aMapOfShapes;
133 ListOfShape::iterator anObjectsIt = anObjects.begin();
134 GeomShapePtr aShape = *anObjectsIt;
135 for (++anObjectsIt; anObjectsIt != anObjects.end(); ++anObjectsIt) {
136 std::shared_ptr<GeomAlgoAPI_Boolean> aCommonAlgo(
137 new GeomAlgoAPI_Boolean(aShape,
139 GeomAlgoAPI_Boolean::BOOL_COMMON));
141 if (!aCommonAlgo->isDone()) {
142 std::string aFeatureError = "Error: An algorithm failed.";
143 setError(aFeatureError);
146 if (aCommonAlgo->shape()->isNull()) {
147 static const std::string aShapeError = "Error: Resulting shape is Null.";
148 setError(aShapeError);
151 if (!aCommonAlgo->isValid()) {
152 std::string aFeatureError = "Error: Resulting shape is not valid.";
153 setError(aFeatureError);
157 aShape = aCommonAlgo->shape();
158 aMakeShapeList.appendAlgo(aCommonAlgo);
159 aMapOfShapes.merge(aCommonAlgo->mapOfSubShapes());
162 GeomAPI_ShapeIterator aShapeIt(aShape);
163 if (aShapeIt.more() || aShape->shapeType() == GeomAPI_Shape::VERTEX) {
164 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
165 document()->createBody(data(), aResultIndex);
167 loadNamingDS(aResultBody, anObjects.front(), anObjects, aShape, aMakeShapeList, aMapOfShapes);
168 setResult(aResultBody, aResultIndex);
172 for (ListOfShape::iterator anObjectsIt = anObjects.begin();
173 anObjectsIt != anObjects.end();
176 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
177 ListOfShape aListWithObject;
178 aListWithObject.push_back(anObject);
179 GeomAlgoAPI_MakeShapeList aMakeShapeList;
180 std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
181 GeomShapePtr aResShape;
183 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
184 GeomAlgoAPI_ShapeTools::getBoundingBox(aListWithObject, 1.0);
187 ListOfShape aToolsWithPlanes = aTools;
188 for (ListOfShape::const_iterator anIt = aPlanes.cbegin();
189 anIt != aPlanes.cend();
191 GeomShapePtr aPlane = *anIt;
192 GeomShapePtr aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPlane, aBoundingPoints);
193 std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(
194 new GeomAlgoAPI_MakeShapeCustom);
195 aMkShCustom->addModified(aPlane, aTool);
196 aMakeShapeList.appendAlgo(aMkShCustom);
197 aToolsWithPlanes.push_back(aTool);
200 aBoolAlgo.reset(new GeomAlgoAPI_Boolean(aListWithObject,
202 GeomAlgoAPI_Boolean::BOOL_COMMON));
203 aResShape = aBoolAlgo->shape();
205 // Checking that the algorithm worked properly.
206 if (!aBoolAlgo->isDone()) {
207 static const std::string aFeatureError = "Error: Boolean algorithm failed.";
208 setError(aFeatureError);
211 if (aResShape->isNull()) {
212 static const std::string aShapeError = "Error: Resulting shape is Null.";
213 setError(aShapeError);
216 if (!aBoolAlgo->isValid()) {
217 std::string aFeatureError = "Error: Resulting shape is not valid.";
218 setError(aFeatureError);
222 aMakeShapeList.appendAlgo(aBoolAlgo);
224 GeomAPI_ShapeIterator aShapeIt(aResShape);
225 if (aShapeIt.more() || aResShape->shapeType() == GeomAPI_Shape::VERTEX) {
226 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
227 document()->createBody(data(), aResultIndex);
229 loadNamingDS(aResultBody, anObject, aTools, aResShape,
230 aMakeShapeList, *(aBoolAlgo->mapOfSubShapes()));
231 setResult(aResultBody, aResultIndex);
236 // Compsolids handling
237 for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
238 anIt = aCompSolidsObjects.begin();
239 anIt != aCompSolidsObjects.end();
242 std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
243 ListOfShape& aUsedInOperationSolids = anIt->second;
245 // Collecting solids from compsolids which will not be modified in boolean operation.
246 ListOfShape aNotUsedSolids;
247 for (GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID);
251 std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
252 ListOfShape::iterator aUsedIt = aUsedInOperationSolids.begin();
253 for (; aUsedIt != aUsedInOperationSolids.end(); aUsedIt++) {
254 if (aSolidInCompSolid->isEqual(*aUsedIt)) {
258 if (aUsedIt == aUsedInOperationSolids.end()) {
259 aNotUsedSolids.push_back(aSolidInCompSolid);
263 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
264 std::shared_ptr<GeomAlgoAPI_Boolean> aCommonAlgo(
265 new GeomAlgoAPI_Boolean(aUsedInOperationSolids,
267 GeomAlgoAPI_Boolean::BOOL_COMMON));
269 // Checking that the algorithm worked properly.
270 if (!aCommonAlgo->isDone()) {
271 static const std::string aFeatureError = "Error: Boolean algorithm failed.";
272 setError(aFeatureError);
275 if (aCommonAlgo->shape()->isNull()) {
276 static const std::string aShapeError = "Error: Resulting shape is Null.";
277 setError(aShapeError);
280 if (!aCommonAlgo->isValid()) {
281 std::string aFeatureError = "Error: Resulting shape is not valid.";
282 setError(aFeatureError);
286 aMakeShapeList->appendAlgo(aCommonAlgo);
287 GeomAPI_DataMapOfShapeShape aMapOfShapes;
288 aMapOfShapes.merge(aCommonAlgo->mapOfSubShapes());
289 GeomShapePtr aResultShape = aCommonAlgo->shape();
291 // Add result to not used solids from compsolid.
292 if (!aNotUsedSolids.empty()) {
293 ListOfShape aShapesToAdd = aNotUsedSolids;
294 aShapesToAdd.push_back(aCommonAlgo->shape());
295 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
296 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
297 if (!aFillerAlgo->isDone()) {
298 std::string aFeatureError = "Error: PaveFiller algorithm failed.";
299 setError(aFeatureError);
303 aMakeShapeList->appendAlgo(aFillerAlgo);
304 aMapOfShapes.merge(aFillerAlgo->mapOfSubShapes());
305 aResultShape = aFillerAlgo->shape();
308 GeomAPI_ShapeIterator aShapeIt(aResultShape);
309 if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX)
311 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
312 document()->createBody(data(), aResultIndex);
314 loadNamingDS(aResultBody,
320 setResult(aResultBody, aResultIndex);
325 // Compounds handling
326 for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
327 anIt = aCompoundObjects.begin();
328 anIt != aCompoundObjects.end();
331 std::shared_ptr<GeomAPI_Shape> aCompound = anIt->first;
332 ListOfShape& aUsedInOperationShapes = anIt->second;
334 // Collecting shapes from compound which will not be modified in boolean operation.
335 ListOfShape aNotUsedShapes;
336 for (GeomAPI_ShapeIterator aCompIt(aCompound);
340 std::shared_ptr<GeomAPI_Shape> aShapeInCompound = aCompIt.current();
341 ListOfShape::iterator aUsedIt = aUsedInOperationShapes.begin();
342 for (; aUsedIt != aUsedInOperationShapes.end(); aUsedIt++) {
343 if (aShapeInCompound->isEqual(*aUsedIt)) {
347 if (aUsedIt == aUsedInOperationShapes.end()) {
348 aNotUsedShapes.push_back(aShapeInCompound);
352 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
353 std::shared_ptr<GeomAlgoAPI_Boolean> aCommonAlgo(
354 new GeomAlgoAPI_Boolean(aUsedInOperationShapes,
356 GeomAlgoAPI_Boolean::BOOL_COMMON));
358 // Checking that the algorithm worked properly.
359 if (!aCommonAlgo->isDone()) {
360 static const std::string aFeatureError = "Error: Boolean algorithm failed.";
361 setError(aFeatureError);
364 if (aCommonAlgo->shape()->isNull()) {
365 static const std::string aShapeError = "Error: Resulting shape is Null.";
366 setError(aShapeError);
369 if (!aCommonAlgo->isValid()) {
370 std::string aFeatureError = "Error: Resulting shape is not valid.";
371 setError(aFeatureError);
375 aMakeShapeList->appendAlgo(aCommonAlgo);
376 GeomAPI_DataMapOfShapeShape aMapOfShapes;
377 aMapOfShapes.merge(aCommonAlgo->mapOfSubShapes());
378 GeomShapePtr aResultShape = aCommonAlgo->shape();
380 // Add result to not used shape from compound.
381 if (!aNotUsedShapes.empty()) {
382 ListOfShape aShapesForResult = aNotUsedShapes;
383 if (aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
384 for (GeomAPI_ShapeIterator aResultIt(aResultShape); aResultIt.more(); aResultIt.next()) {
385 aShapesForResult.push_back(aResultIt.current());
389 aShapesForResult.push_back(aResultShape);
392 if (aShapesForResult.size() == 1) {
393 aResultShape = aShapesForResult.front();
396 aResultShape = GeomAlgoAPI_CompoundBuilder::compound(aShapesForResult);
400 GeomAPI_ShapeIterator aShapeIt(aResultShape);
401 if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX) {
402 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
403 document()->createBody(data(), aResultIndex);
405 loadNamingDS(aResultBody,
411 setResult(aResultBody, aResultIndex);
418 // remove the rest results if there were produced in the previous pass
419 removeResults(aResultIndex);
422 //==================================================================================================
423 void FeaturesPlugin_BooleanCommon::loadNamingDS(ResultBodyPtr theResultBody,
424 const GeomShapePtr theBaseShape,
425 const ListOfShape& theTools,
426 const GeomShapePtr theResultShape,
427 GeomAlgoAPI_MakeShape& theMakeShape,
428 GeomAPI_DataMapOfShapeShape& theMapOfShapes)
431 if (theBaseShape->isEqual(theResultShape)) {
432 theResultBody->store(theResultShape, false);
434 const int aModifyVTag = 1;
435 const int aModifyETag = 2;
436 const int aModifyFTag = 3;
437 const int aDeletedTag = 4;
438 /// sub solids will be placed at labels 5, 6, etc. if result is compound of solids
439 const int aSubsolidsTag = 5;
441 theResultBody->storeModified(theBaseShape, theResultShape, aSubsolidsTag);
443 const std::string aModVName = "Modified_Vertex";
444 const std::string aModEName = "Modified_Edge";
445 const std::string aModFName = "Modified_Face";
447 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::VERTEX,
448 aModifyVTag, aModVName, theMapOfShapes, false,
450 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::EDGE,
451 aModifyETag, aModEName, theMapOfShapes, false,
453 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE,
454 aModifyFTag, aModFName, theMapOfShapes, false,
457 theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape,
458 GeomAPI_Shape::VERTEX, aDeletedTag);
459 theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape,
460 GeomAPI_Shape::EDGE, aDeletedTag);
461 theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape,
462 GeomAPI_Shape::FACE, aDeletedTag);
464 for (ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++)
466 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::VERTEX,
467 aModifyVTag, aModVName, theMapOfShapes, false,
470 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::EDGE,
471 aModifyETag, aModEName, theMapOfShapes, false,
474 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE,
475 aModifyFTag, aModFName, theMapOfShapes, false,
478 theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::VERTEX, aDeletedTag);
479 theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::EDGE, aDeletedTag);
480 theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE, aDeletedTag);