1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: FeaturesPlugin_CompositeBoolean.cpp
4 // Created: 11 June 2015
5 // Author: Dmitry Bobylev
7 #include "FeaturesPlugin_CompositeBoolean.h"
9 #include <ModelAPI_AttributeSelectionList.h>
10 #include <ModelAPI_Tools.h>
12 #include <GeomAlgoAPI_Boolean.h>
13 #include <GeomAlgoAPI_MakeShapeList.h>
14 #include <GeomAlgoAPI_PaveFiller.h>
15 #include <GeomAlgoAPI_ShapeTools.h>
17 #include <GeomAPI_ShapeExplorer.h>
21 //=================================================================================================
22 void FeaturesPlugin_CompositeBoolean::initBooleanAttributes()
24 myFeature->data()->addAttribute(OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
27 //=================================================================================================
28 void FeaturesPlugin_CompositeBoolean::execute()
31 ListOfShape aGenBaseShapes;
32 ListOfMakeShape aGenMakeShapes;
33 if(!makeGeneration(aGenBaseShapes, aGenMakeShapes)) {
39 for(ListOfMakeShape::const_iterator anIt = aGenMakeShapes.cbegin(); anIt != aGenMakeShapes.cend(); ++anIt) {
40 aTools.push_back((*anIt)->shape());
44 ListOfShape aBooleanObjects;
45 ListOfMakeShape aBooleanMakeShapes;
46 if(!makeBoolean(aTools, aBooleanObjects, aBooleanMakeShapes)) {
50 if(myOperationType == BOOL_FUSE) {
51 aTools.splice(aTools.begin(), aBooleanObjects);
52 aBooleanObjects.splice(aBooleanObjects.begin(), aTools, aTools.begin());
57 ListOfShape::const_iterator aBoolObjIt = aBooleanObjects.cbegin();
58 ListOfMakeShape::const_iterator aBoolMSIt = aBooleanMakeShapes.cbegin();
59 for(; aBoolObjIt != aBooleanObjects.cend() && aBoolMSIt != aBooleanMakeShapes.cend();
60 ++aBoolObjIt, ++aBoolMSIt) {
64 ResultBodyPtr aResultBody = myFeature->document()->createBody(myFeature->data(), aResultIndex);
65 aResultBody->storeModified(*aBoolObjIt, (*aBoolMSIt)->shape(), aTag);
69 // Store generation history.
70 ListOfShape::const_iterator aGenBaseIt = aGenBaseShapes.cbegin();
71 ListOfMakeShape::const_iterator aGenMSIt = aGenMakeShapes.cbegin();
72 for(; aGenBaseIt != aGenBaseShapes.cend() && aGenMSIt != aGenMakeShapes.cend();
73 ++aGenBaseIt, ++aGenMSIt) {
74 storeGenerationHistory(aResultBody, *aGenBaseIt, *aGenMSIt, aTag);
78 storeModificationHistory(aResultBody, *aBoolObjIt, aTools, *aBoolMSIt, aModTag);
82 //=================================================================================================
83 bool FeaturesPlugin_CompositeBoolean::makeBoolean(const ListOfShape& theTools,
84 ListOfShape& theObjects,
85 ListOfMakeShape& theMakeShapes)
88 ListOfShape anObjects, anEdgesAndFaces, aCompSolids;
89 std::map<GeomShapePtr, ListOfShape> aCompSolidsObjects;
90 AttributeSelectionListPtr anObjectsSelList = myFeature->selectionList(OBJECTS_ID());
91 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
92 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
93 GeomShapePtr anObject = anObjectAttr->value();
95 myFeature->setError("Error: Could not get object.");
98 ResultPtr aContext = anObjectAttr->context();
99 ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(aContext);
100 if(aResCompSolidPtr.get()) {
101 GeomShapePtr aContextShape = aResCompSolidPtr->shape();
102 std::map<GeomShapePtr, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
103 for(; anIt != aCompSolidsObjects.end(); anIt++) {
104 if(anIt->first->isEqual(aContextShape)) {
105 aCompSolidsObjects[anIt->first].push_back(anObject);
109 if(anIt == aCompSolidsObjects.end()) {
110 aCompSolidsObjects[aContextShape].push_back(anObject);
111 aCompSolids.push_back(aContextShape);
114 if(anObject->shapeType() == GeomAPI_Shape::EDGE ||
115 anObject->shapeType() == GeomAPI_Shape::FACE) {
116 anEdgesAndFaces.push_back(anObject);
118 anObjects.push_back(anObject);
123 switch(myOperationType) {
125 if((anObjects.empty() && aCompSolidsObjects.empty()) || theTools.empty()) {
126 myFeature->setError("Error: Not enough objects for boolean operation.");
130 // For solids cut each object with all tools.
131 for(ListOfShape::const_iterator anIt = anObjects.cbegin(); anIt != anObjects.cend(); ++anIt) {
132 GeomShapePtr anObject = *anIt;
133 ListOfShape aListWithObject;
134 aListWithObject.push_back(anObject);
135 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(new GeomAlgoAPI_Boolean(aListWithObject,
137 GeomAlgoAPI_Boolean::BOOL_CUT));
139 // Checking that the algorithm worked properly.
140 if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
141 myFeature->setError("Error: Boolean algorithm failed.");
145 if(GeomAlgoAPI_ShapeTools::volume(aBoolAlgo->shape()) > 1.e-7) {
146 theObjects.push_back(anObject);
147 theMakeShapes.push_back(aBoolAlgo);
151 // Compsolids handling
152 for(std::map<GeomShapePtr, ListOfShape>::const_iterator anIt = aCompSolidsObjects.cbegin();
153 anIt != aCompSolidsObjects.cend(); ++anIt) {
154 GeomShapePtr aCompSolid = anIt->first;
155 const ListOfShape& aUsedShapes = anIt->second;
157 // Collecting solids from compsolids which will not be modified in boolean operation.
158 ListOfShape aShapesToAdd;
159 for(GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
160 GeomShapePtr aSolidInCompSolid = anExp.current();
161 ListOfShape::const_iterator aUsedShapesIt = aUsedShapes.cbegin();
162 for(; aUsedShapesIt != aUsedShapes.cend(); ++aUsedShapesIt) {
163 if(aSolidInCompSolid->isEqual(*aUsedShapesIt)) {
167 if(aUsedShapesIt == aUsedShapes.end()) {
168 aShapesToAdd.push_back(aSolidInCompSolid);
172 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(new GeomAlgoAPI_Boolean(aUsedShapes,
174 GeomAlgoAPI_Boolean::BOOL_CUT));
176 // Checking that the algorithm worked properly.
177 if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
178 myFeature->setError("Error: Boolean algorithm failed.");
182 GeomAlgoAPI_MakeShapeList aMakeShapeList;
183 aMakeShapeList.appendAlgo(aBoolAlgo);
185 // Add result to not used solids from compsolid.
186 aShapesToAdd.push_back(aBoolAlgo->shape());
187 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
188 if(!aFillerAlgo->isDone() || aFillerAlgo->shape()->isNull() || !aFillerAlgo->isValid()) {
189 myFeature->setError("Error: PaveFiller algorithm failed.");
193 aMakeShapeList.appendAlgo(aFillerAlgo);
195 if(GeomAlgoAPI_ShapeTools::volume(aFillerAlgo->shape()) > 1.e-7) {
196 theObjects.push_back(aCompSolid);
197 theMakeShapes.push_back(aBoolAlgo);
204 theObjects.insert(theObjects.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
205 theObjects.insert(theObjects.end(), anObjects.begin(), anObjects.end());
206 theObjects.insert(theObjects.end(), aCompSolids.begin(), aCompSolids.end());
208 // Filter edges and faces in tools.
210 for(ListOfShape::const_iterator anIt = aTools.cbegin(); anIt != aTools.cend(); ++anIt) {
211 if((*anIt)->shapeType() == GeomAPI_Shape::EDGE ||
212 (*anIt)->shapeType() == GeomAPI_Shape::FACE) {
213 anEdgesAndFaces.push_back(*anIt);
215 aTools.push_back(*anIt);
219 if((anObjects.size() + aTools.size() + aCompSolidsObjects.size() + anEdgesAndFaces.size()) < 2) {
220 myFeature->setError("Error: Not enough objects for boolean operation.");
224 // Collecting all solids which will be fused.
225 ListOfShape aSolidsToFuse;
226 aSolidsToFuse.insert(aSolidsToFuse.end(), anObjects.begin(), anObjects.end());
227 aSolidsToFuse.insert(aSolidsToFuse.end(), aTools.begin(), aTools.end());
229 // Collecting solids from compsolids which will not be modified in boolean operation and will be added to result.
230 ListOfShape aShapesToAdd;
231 for(std::map<GeomShapePtr, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
232 anIt != aCompSolidsObjects.end(); anIt++) {
233 GeomShapePtr aCompSolid = anIt->first;
234 ListOfShape& aUsedShapes = anIt->second;
235 aSolidsToFuse.insert(aSolidsToFuse.end(), aUsedShapes.begin(), aUsedShapes.end());
237 // Collect solids from compsolid which will not be modified in boolean operation.
238 for(GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
239 GeomShapePtr aSolidInCompSolid = anExp.current();
240 ListOfShape::iterator anIt = aUsedShapes.begin();
241 for(; anIt != aUsedShapes.end(); anIt++) {
242 if(aSolidInCompSolid->isEqual(*anIt)) {
246 if(anIt == aUsedShapes.end()) {
247 aShapesToAdd.push_back(aSolidInCompSolid);
252 // Cut edges and faces(if we have any) with solids.
253 ListOfShape aCutTools;
254 aCutTools.insert(aCutTools.end(), anObjects.begin(), anObjects.end());
255 aCutTools.insert(aCutTools.end(), aCompSolids.begin(), aCompSolids.end());
256 aCutTools.insert(aCutTools.end(), aTools.begin(), aTools.end());
258 GeomAlgoAPI_MakeShapeList aMakeShapeList;
259 if(!anEdgesAndFaces.empty() && !aCutTools.empty()) {
260 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(anEdgesAndFaces,
262 GeomAlgoAPI_Boolean::BOOL_CUT));
263 if(aCutAlgo->isDone() && !aCutAlgo->shape()->isNull() && aCutAlgo->isValid()) {
264 anEdgesAndFaces.clear();
265 anEdgesAndFaces.push_back(aCutAlgo->shape());
266 aMakeShapeList.appendAlgo(aCutAlgo);
270 // If we have compsolids then cut with not used solids all others.
271 if(!aShapesToAdd.empty()) {
272 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(aSolidsToFuse,
274 GeomAlgoAPI_Boolean::BOOL_CUT));
275 if(aCutAlgo->isDone() && GeomAlgoAPI_ShapeTools::volume(aCutAlgo->shape()) > 1.e-7) {
276 aSolidsToFuse.clear();
277 aSolidsToFuse.push_back(aCutAlgo->shape());
278 aMakeShapeList.appendAlgo(aCutAlgo);
282 // Fuse all objects and all tools.
283 GeomShapePtr aFusedShape;
284 if(aSolidsToFuse.size() == 1) {
285 aFusedShape = aSolidsToFuse.front();
286 } else if(aSolidsToFuse.size() > 1){
288 anObjects.push_back(aSolidsToFuse.front());
289 aSolidsToFuse.pop_front();
290 aTools = aSolidsToFuse;
292 std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
294 GeomAlgoAPI_Boolean::BOOL_FUSE));
296 // Checking that the algorithm worked properly.
297 if(!aFuseAlgo->isDone() || aFuseAlgo->shape()->isNull() || !aFuseAlgo->isValid()) {
298 myFeature->setError("Error: Boolean algorithm failed.");
302 aFusedShape = aFuseAlgo->shape();
303 aMakeShapeList.appendAlgo(aFuseAlgo);
306 // Combine result with not used solids from compsolid and edges and faces (if we have any).
307 aShapesToAdd.insert(aShapesToAdd.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
308 if(!aShapesToAdd.empty()) {
309 if(aFusedShape.get()) {
310 aShapesToAdd.push_back(aFusedShape);
313 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
314 if(!aFillerAlgo->isDone() || aFillerAlgo->shape()->isNull() || !aFillerAlgo->isValid()) {
315 myFeature->setError("Error: PaveFiller algorithm failed.");
319 aMakeShapeList.appendAlgo(aFillerAlgo);
328 //=================================================================================================
329 void FeaturesPlugin_CompositeBoolean::storeModificationHistory(ResultBodyPtr theResultBody,
330 const GeomShapePtr theObject,
331 const ListOfShape& theTools,
332 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
335 int aModTag = theTag;
336 int anEdgesAndFacesTag = ++aModTag;
337 int aDelTag = ++anEdgesAndFacesTag;
340 const std::string aModName = "Modfied";
342 ListOfShape aTools = theTools;
343 aTools.push_back(theObject);
345 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMap = theMakeShape->mapOfSubShapes();
349 for(ListOfShape::const_iterator anIt = aTools.begin(); anIt != aTools.end(); anIt++) {
350 if((*anIt)->shapeType() == GeomAPI_Shape::EDGE) {
351 aTag = anEdgesAndFacesTag;
352 aName = aModName + "_Edge";
354 else if((*anIt)->shapeType() == GeomAPI_Shape::FACE) {
355 aTag = anEdgesAndFacesTag;
356 aName = aModName + "_Face";
361 theResultBody->loadAndOrientModifiedShapes(theMakeShape.get(), *anIt, (*anIt)->shapeType() == GeomAPI_Shape::EDGE ?
362 GeomAPI_Shape::EDGE : GeomAPI_Shape::FACE, aTag, aName, *aMap.get());
363 theResultBody->loadDeletedShapes(theMakeShape.get(), *anIt, GeomAPI_Shape::FACE, aDelTag);