1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: FeaturesPlugin_Boolean.cpp
4 // Created: 02 Sept 2014
5 // Author: Vitaly SMETANNIKOV
7 #include "FeaturesPlugin_Boolean.h"
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_Document.h>
11 #include <ModelAPI_AttributeReference.h>
12 #include <ModelAPI_AttributeInteger.h>
13 #include <ModelAPI_ResultBody.h>
14 #include <ModelAPI_AttributeSelectionList.h>
15 #include <ModelAPI_Session.h>
16 #include <ModelAPI_Validator.h>
17 #include <ModelAPI_Tools.h>
19 #include <GeomAlgoAPI_Boolean.h>
20 #include <GeomAlgoAPI_MakeShapeList.h>
21 #include <GeomAlgoAPI_PaveFiller.h>
22 #include <GeomAlgoAPI_ShapeTools.h>
23 #include <GeomAPI_ShapeExplorer.h>
27 //=================================================================================================
28 FeaturesPlugin_Boolean::FeaturesPlugin_Boolean()
32 //=================================================================================================
33 void FeaturesPlugin_Boolean::initAttributes()
35 data()->addAttribute(FeaturesPlugin_Boolean::TYPE_ID(), ModelAPI_AttributeInteger::typeId());
37 AttributeSelectionListPtr aSelection =
38 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
39 FeaturesPlugin_Boolean::OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
40 // extrusion works with faces always
41 aSelection->setSelectionType("SOLID");
43 aSelection = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
44 FeaturesPlugin_Boolean::TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
45 // extrusion works with faces always
46 aSelection->setSelectionType("SOLID");
48 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), OBJECT_LIST_ID());
49 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TOOL_LIST_ID());
52 //=================================================================================================
53 std::shared_ptr<GeomAPI_Shape> FeaturesPlugin_Boolean::getShape(const std::string& theAttrName)
55 std::shared_ptr<ModelAPI_AttributeReference> aObjRef = std::dynamic_pointer_cast<
56 ModelAPI_AttributeReference>(data()->attribute(theAttrName));
58 std::shared_ptr<ModelAPI_ResultBody> aConstr = std::dynamic_pointer_cast<
59 ModelAPI_ResultBody>(aObjRef->value());
61 return aConstr->shape();
63 return std::shared_ptr<GeomAPI_Shape>();
66 //=================================================================================================
67 void FeaturesPlugin_Boolean::execute()
69 // Getting operation type.
70 std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
71 ModelAPI_AttributeInteger>(data()->attribute(FeaturesPlugin_Boolean::TYPE_ID()));
74 GeomAlgoAPI_Boolean::OperationType aType = (GeomAlgoAPI_Boolean::OperationType)aTypeAttr->value();
76 ListOfShape anObjects, aTools, anEdgesAndFaces;
77 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
80 AttributeSelectionListPtr anObjectsSelList = selectionList(FeaturesPlugin_Boolean::OBJECT_LIST_ID());
81 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
82 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
83 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
87 ResultPtr aContext = anObjectAttr->context();
88 ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(aContext);
89 if(aResCompSolidPtr.get()) {
90 std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
91 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
92 for(; anIt != aCompSolidsObjects.end(); anIt++) {
93 if(anIt->first->isEqual(aContextShape)) {
94 aCompSolidsObjects[anIt->first].push_back(anObject);
98 if(anIt == aCompSolidsObjects.end()) {
99 aCompSolidsObjects[aContextShape].push_back(anObject);
102 if(anObject->shapeType() == GeomAPI_Shape::EDGE ||
103 anObject->shapeType() == GeomAPI_Shape::FACE) {
104 anEdgesAndFaces.push_back(anObject);
106 anObjects.push_back(anObject);
112 AttributeSelectionListPtr aToolsSelList = selectionList(FeaturesPlugin_Boolean::TOOL_LIST_ID());
113 for(int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
114 AttributeSelectionPtr aToolAttr = aToolsSelList->value(aToolsIndex);
115 std::shared_ptr<GeomAPI_Shape> aTool = aToolAttr->value();
119 if(aTool->shapeType() == GeomAPI_Shape::EDGE ||
120 aTool->shapeType() == GeomAPI_Shape::FACE) {
121 anEdgesAndFaces.push_back(aTool);
123 aTools.push_back(aTool);
127 int aResultIndex = 0;
130 case GeomAlgoAPI_Boolean::BOOL_CUT:
131 case GeomAlgoAPI_Boolean::BOOL_COMMON:{
132 if((anObjects.empty() && aCompSolidsObjects.empty()) || aTools.empty()) {
133 std::string aFeatureError = "Not enough objects for boolean operation";
134 setError(aFeatureError);
138 // For solids cut each object with all tools.
139 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
140 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
141 ListOfShape aListWithObject;
142 aListWithObject.push_back(anObject);
143 GeomAlgoAPI_Boolean aBoolAlgo(aListWithObject, aTools, aType);
145 // Checking that the algorithm worked properly.
146 if(!aBoolAlgo.isDone()) {
147 static const std::string aFeatureError = "Boolean algorithm failed";
148 setError(aFeatureError);
151 if(aBoolAlgo.shape()->isNull()) {
152 static const std::string aShapeError = "Resulting shape is Null";
153 setError(aShapeError);
156 if(!aBoolAlgo.isValid()) {
157 std::string aFeatureError = "Warning: resulting shape is not valid";
158 setError(aFeatureError);
162 if(GeomAlgoAPI_ShapeTools::volume(aBoolAlgo.shape()) > 1.e-7) {
163 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
164 loadNamingDS(aResultBody, anObject, aTools, aBoolAlgo.shape(), aBoolAlgo, *aBoolAlgo.mapOfSubShapes().get());
165 setResult(aResultBody, aResultIndex);
170 // Compsolids handling
171 for(std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
172 anIt != aCompSolidsObjects.end(); anIt++) {
173 std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
174 ListOfShape& aUsedInOperationSolids = anIt->second;
176 // Collecting solids from compsolids which will not be modified in boolean operation.
177 ListOfShape aNotUsedSolids;
178 for(GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
179 std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
180 ListOfShape::iterator anIt = aUsedInOperationSolids.begin();
181 for(; anIt != aUsedInOperationSolids.end(); anIt++) {
182 if(aSolidInCompSolid->isEqual(*anIt)) {
186 if(anIt == aUsedInOperationSolids.end()) {
187 aNotUsedSolids.push_back(aSolidInCompSolid);
191 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(new GeomAlgoAPI_Boolean(aUsedInOperationSolids, aTools, aType));
193 // Checking that the algorithm worked properly.
194 if(!aBoolAlgo->isDone()) {
195 static const std::string aFeatureError = "Boolean algorithm failed";
196 setError(aFeatureError);
199 if(aBoolAlgo->shape()->isNull()) {
200 static const std::string aShapeError = "Resulting shape is Null";
201 setError(aShapeError);
204 if(!aBoolAlgo->isValid()) {
205 std::string aFeatureError = "Warning: resulting shape is not valid";
206 setError(aFeatureError);
210 GeomAlgoAPI_MakeShapeList aMakeShapeList;
211 aMakeShapeList.appendAlgo(aBoolAlgo);
212 GeomAPI_DataMapOfShapeShape aMapOfShapes;
213 aMapOfShapes.merge(aBoolAlgo->mapOfSubShapes());
215 // Add result to not used solids from compsolid.
216 ListOfShape aShapesToAdd = aNotUsedSolids;
217 aShapesToAdd.push_back(aBoolAlgo->shape());
218 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
219 if(!aFillerAlgo->isDone()) {
220 std::string aFeatureError = "PaveFiller algorithm failed";
221 setError(aFeatureError);
225 aMakeShapeList.appendAlgo(aFillerAlgo);
226 aMapOfShapes.merge(aFillerAlgo->mapOfSubShapes());
228 if(GeomAlgoAPI_ShapeTools::volume(aFillerAlgo->shape()) > 1.e-7) {
229 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
230 loadNamingDS(aResultBody, aCompSolid, aTools, aFillerAlgo->shape(), aMakeShapeList, aMapOfShapes);
231 setResult(aResultBody, aResultIndex);
237 case GeomAlgoAPI_Boolean::BOOL_FUSE: {
238 if((anObjects.size() + aTools.size() + aCompSolidsObjects.size() + anEdgesAndFaces.size()) < 2) {
239 std::string aFeatureError = "Not enough objects for boolean operation";
240 setError(aFeatureError);
244 // Collecting all solids which will be fused.
245 ListOfShape aSolidsToFuse;
246 aSolidsToFuse.insert(aSolidsToFuse.end(), anObjects.begin(), anObjects.end());
247 aSolidsToFuse.insert(aSolidsToFuse.end(), aTools.begin(), aTools.end());
249 // Collecting solids from compsolids which will not be modified in boolean operation and will be added to result.
250 ListOfShape aShapesToAdd;
251 for(std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
252 anIt != aCompSolidsObjects.end(); anIt++) {
253 std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
254 ListOfShape& aUsedInOperationSolids = anIt->second;
255 aSolidsToFuse.insert(aSolidsToFuse.end(), aUsedInOperationSolids.begin(), aUsedInOperationSolids.end());
257 // Collect solids from compsolid which will not be modified in boolean operation.
258 for(GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
259 std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
260 ListOfShape::iterator anIt = aUsedInOperationSolids.begin();
261 for(; anIt != aUsedInOperationSolids.end(); anIt++) {
262 if(aSolidInCompSolid->isEqual(*anIt)) {
266 if(anIt == aUsedInOperationSolids.end()) {
267 aShapesToAdd.push_back(aSolidInCompSolid);
272 ListOfShape anOriginalShapes = aSolidsToFuse;
273 anOriginalShapes.insert(anOriginalShapes.end(), aShapesToAdd.begin(), aShapesToAdd.end());
275 // Cut edges and faces(if we have any) with solids.
276 GeomAlgoAPI_MakeShapeList aMakeShapeList;
277 GeomAPI_DataMapOfShapeShape aMapOfShapes;
278 std::shared_ptr<GeomAPI_Shape> aCuttedEdgesAndFaces;
279 if(!anEdgesAndFaces.empty()) {
280 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(anEdgesAndFaces, anOriginalShapes, GeomAlgoAPI_Boolean::BOOL_CUT));
281 if(aCutAlgo->isDone()) {
282 aCuttedEdgesAndFaces = aCutAlgo->shape();
283 aMakeShapeList.appendAlgo(aCutAlgo);
284 aMapOfShapes.merge(aCutAlgo->mapOfSubShapes());
287 anOriginalShapes.insert(anOriginalShapes.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
289 // If we have compsolids then cut with not used solids all others.
290 if(!aShapesToAdd.empty()) {
291 aSolidsToFuse.clear();
292 for(ListOfShape::iterator anIt = anOriginalShapes.begin(); anIt != anOriginalShapes.end(); anIt++) {
293 ListOfShape aOneObjectList;
294 aOneObjectList.push_back(*anIt);
295 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(aOneObjectList, aShapesToAdd, GeomAlgoAPI_Boolean::BOOL_CUT));
297 if(GeomAlgoAPI_ShapeTools::volume(aCutAlgo->shape()) > 1.e-7) {
298 aSolidsToFuse.push_back(aCutAlgo->shape());
299 aMakeShapeList.appendAlgo(aCutAlgo);
300 aMapOfShapes.merge(aCutAlgo->mapOfSubShapes());
305 if(!aSolidsToFuse.empty()) {
307 anObjects.push_back(aSolidsToFuse.back());
308 aSolidsToFuse.pop_back();
309 aTools = aSolidsToFuse;
312 // Fuse all objects and all tools.
313 std::shared_ptr<GeomAPI_Shape> aShape;
314 if(anObjects.size() == 1 && aTools.empty()) {
315 aShape = anObjects.front();
316 } else if(anObjects.empty() && aTools.size() == 1) {
317 aShape = aTools.front();
318 } else if((anObjects.size() + aTools.size()) > 1){
319 std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects, aTools, aType));
321 // Checking that the algorithm worked properly.
322 if(!aFuseAlgo->isDone()) {
323 static const std::string aFeatureError = "Boolean algorithm failed";
324 setError(aFeatureError);
327 if(aFuseAlgo->shape()->isNull()) {
328 static const std::string aShapeError = "Resulting shape is Null";
329 setError(aShapeError);
332 if(!aFuseAlgo->isValid()) {
333 std::string aFeatureError = "Warning: resulting shape is not valid";
334 setError(aFeatureError);
338 aShape = aFuseAlgo->shape();
339 aMakeShapeList.appendAlgo(aFuseAlgo);
340 aMapOfShapes.merge(aFuseAlgo->mapOfSubShapes());
343 // Combine result with not used solids from compsolid and edges and faces (if we have any).
344 if(aCuttedEdgesAndFaces.get() && !aCuttedEdgesAndFaces->isNull()) {
345 aShapesToAdd.push_back(aCuttedEdgesAndFaces);
347 aShapesToAdd.insert(aShapesToAdd.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
349 if(!aShapesToAdd.empty()) {
351 aShapesToAdd.push_back(aShape);
353 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
354 if(!aFillerAlgo->isDone()) {
355 std::string aFeatureError = "PaveFiller algorithm failed";
356 setError(aFeatureError);
359 if(aFillerAlgo->shape()->isNull()) {
360 static const std::string aShapeError = "Resulting shape is Null";
361 setError(aShapeError);
364 if(!aFillerAlgo->isValid()) {
365 std::string aFeatureError = "Warning: resulting shape is not valid";
366 setError(aFeatureError);
370 aShape = aFillerAlgo->shape();
371 aMakeShapeList.appendAlgo(aFillerAlgo);
372 aMapOfShapes.merge(aFillerAlgo->mapOfSubShapes());
375 std::shared_ptr<GeomAPI_Shape> aBackShape = anOriginalShapes.back();
376 anOriginalShapes.pop_back();
377 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
378 loadNamingDS(aResultBody, aBackShape, anOriginalShapes, aShape, aMakeShapeList, aMapOfShapes);
379 setResult(aResultBody, aResultIndex);
384 std::string anOperationError = "Error: wrong type of operation";
385 setError(anOperationError);
389 // remove the rest results if there were produced in the previous pass
390 removeResults(aResultIndex);
393 //=================================================================================================
394 void FeaturesPlugin_Boolean::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
395 const std::shared_ptr<GeomAPI_Shape> theBaseShape,
396 const ListOfShape& theTools,
397 const std::shared_ptr<GeomAPI_Shape> theResultShape,
398 GeomAlgoAPI_MakeShape& theMakeShape,
399 GeomAPI_DataMapOfShapeShape& theMapOfShapes)
402 if(theBaseShape->isEqual(theResultShape)) {
403 theResultBody->store(theResultShape);
405 const int aModifyTag = 1;
406 const int aDeletedTag = 2;
407 const int aSubsolidsTag = 3; /// sub solids will be placed at labels 3, 4, etc. if result is compound of solids
408 const int anEdgesAndFacesTag = 10000;
410 theResultBody->storeModified(theBaseShape, theResultShape, aSubsolidsTag);
412 const std::string aModName = "Modified";
413 const std::string aModEName = "Modified_Edge";
414 const std::string aModFName = "Modified_Face";
416 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE,
417 aModifyTag, aModName, theMapOfShapes);
418 theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE, aDeletedTag);
422 for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
423 if((*anIter)->shapeType() == GeomAPI_Shape::EDGE) {
424 aTag = anEdgesAndFacesTag;
427 else if((*anIter)->shapeType() == GeomAPI_Shape::FACE) {
428 aTag = anEdgesAndFacesTag;
434 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, aName == aModEName ? GeomAPI_Shape::EDGE : GeomAPI_Shape::FACE,
435 aTag, aName, theMapOfShapes);
436 theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE, aDeletedTag);