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_AttributeReference.h>
11 #include <ModelAPI_ResultBody.h>
12 #include <ModelAPI_ResultConstruction.h>
13 #include <ModelAPI_Session.h>
14 #include <ModelAPI_Tools.h>
15 #include <ModelAPI_Validator.h>
17 #include <GeomAlgoAPI_CompoundBuilder.h>
18 #include <GeomAlgoAPI_MakeShapeList.h>
19 #include <GeomAlgoAPI_MakeSweep.h>
20 #include <GeomAlgoAPI_PaveFiller.h>
21 #include <GeomAlgoAPI_Prism.h>
22 #include <GeomAlgoAPI_Revolution.h>
23 #include <GeomAlgoAPI_ShapeTools.h>
24 #include <GeomAPI_ShapeExplorer.h>
28 //=================================================================================================
29 void FeaturesPlugin_CompositeBoolean::initAttributes()
31 data()->addAttribute(SKETCH_OBJECT_ID(), ModelAPI_AttributeReference::typeId());
33 // Boolean works with solids always.
34 data()->addAttribute(BOOLEAN_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
35 AttributeSelectionListPtr aSelection = data()->selectionList(BOOLEAN_OBJECTS_ID());
36 aSelection->setSelectionType("SOLID");
38 initMakeSolidsAttributes();
40 data()->addAttribute(SKETCH_SELECTION_ID(), ModelAPI_AttributeSelection::typeId());
41 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SKETCH_SELECTION_ID());
44 //=================================================================================================
45 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeBoolean::addFeature(std::string theID)
47 std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
49 data()->reference(SKETCH_OBJECT_ID())->setValue(aNew);
51 // set as current also after it becomes sub to set correctly enabled for other sketch subs
52 document()->setCurrentFeature(aNew, false);
56 //=================================================================================================
57 int FeaturesPlugin_CompositeBoolean::numberOfSubs(bool forTree) const
59 ObjectPtr aObj = data()->reference(SKETCH_OBJECT_ID())->value();
60 return aObj.get()? 1 : 0;
63 //=================================================================================================
64 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeBoolean::subFeature(const int theIndex, bool forTree)
67 return std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_OBJECT_ID())->value());
68 return std::shared_ptr<ModelAPI_Feature>();
71 //=================================================================================================
72 int FeaturesPlugin_CompositeBoolean::subFeatureId(const int theIndex) const
76 std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_OBJECT_ID())->value());
78 return aFeature->data()->featureId();
83 //=================================================================================================
84 bool FeaturesPlugin_CompositeBoolean::isSub(ObjectPtr theObject) const
86 // check is this feature of result
87 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
91 ObjectPtr aSub = data()->reference(SKETCH_OBJECT_ID())->value();
92 return aSub == theObject;
95 //=================================================================================================
96 void FeaturesPlugin_CompositeBoolean::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
100 //=================================================================================================
101 void FeaturesPlugin_CompositeBoolean::erase()
103 if (data().get() && data()->isValid()) { // on abort of sketch of this composite it may be invalid
105 std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_OBJECT_ID())->value());
106 if (aSketch.get() && aSketch->data()->isValid()) {
107 document()->removeFeature(aSketch);
110 ModelAPI_CompositeFeature::erase();
114 //=================================================================================================
115 void FeaturesPlugin_CompositeBoolean::execute()
117 // Getting faces to create solids.
118 std::shared_ptr<ModelAPI_Feature> aSketchFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(
119 reference(SKETCH_OBJECT_ID())->value());
120 if(!aSketchFeature || aSketchFeature->results().empty()) {
123 ResultPtr aSketchRes = aSketchFeature->results().front();
124 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
125 if(!aConstruction.get()) {
128 selection(SKETCH_SELECTION_ID())->setValue(aSketchRes, std::shared_ptr<GeomAPI_Shape>());
129 int aSketchFacesNum = aConstruction->facesNum();
130 if(aSketchFacesNum == 0) {
133 ListOfShape aFacesList;
134 for(int aFaceIndex = 0; aFaceIndex < aSketchFacesNum; aFaceIndex++) {
135 std::shared_ptr<GeomAPI_Shape> aFace = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
136 aFacesList.push_back(aFace);
139 // Searching faces with common edges.
141 ListOfShape aFreeFaces;
142 std::shared_ptr<GeomAPI_Shape> aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aFacesList);
143 GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL, aShells, aFreeFaces);
144 aShells.insert(aShells.end(), aFreeFaces.begin(), aFreeFaces.end());
146 // Pass shells/faces to solids creation function.
148 ListOfMakeShape aSolidsAlgos;
149 makeSolids(aShells, aTools, aSolidsAlgos);
154 // Getting objects for boolean operation.
155 ListOfShape anObjects;
156 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
157 AttributeSelectionListPtr anObjectsSelList = selectionList(BOOLEAN_OBJECTS_ID());
158 if(anObjectsSelList->size() == 0) {
161 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
162 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
163 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
164 if(!anObject.get()) {
167 ResultPtr aContext = anObjectAttr->context();
168 ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(aContext);
169 if(aResCompSolidPtr.get()) {
170 std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
171 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
172 for(; anIt != aCompSolidsObjects.end(); anIt++) {
173 if(anIt->first->isEqual(aContextShape)) {
174 aCompSolidsObjects[anIt->first].push_back(anObject);
178 if(anIt == aCompSolidsObjects.end()) {
179 aCompSolidsObjects[aContextShape].push_back(anObject);
182 anObjects.push_back(anObject);
186 // Cut from each object solids.
187 int aResultIndex = 0;
189 switch(myBooleanOperationType) {
190 case GeomAlgoAPI_Boolean::BOOL_CUT:
191 case GeomAlgoAPI_Boolean::BOOL_COMMON:{
192 // Cut each object with all tools
193 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
194 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
195 ListOfShape aListWithObject;
196 aListWithObject.push_back(anObject);
197 GeomAlgoAPI_Boolean aBoolAlgo(aListWithObject, aTools, myBooleanOperationType);
199 // Checking that the algorithm worked properly.
200 if(!aBoolAlgo.isDone() || aBoolAlgo.shape()->isNull() || !aBoolAlgo.isValid()) {
201 setError("Error: Boolean algorithm failed.");
205 if(GeomAlgoAPI_ShapeTools::volume(aBoolAlgo.shape()) > 1.e-7) {
206 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
207 loadNamingDS(aResultBody, aShells, aSolidsAlgos, anObject, aTools, aBoolAlgo.shape(),
208 aBoolAlgo, *aBoolAlgo.mapOfSubShapes().get());
209 setResult(aResultBody, aResultIndex);
214 // Compsolids handling
215 for(std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
216 anIt != aCompSolidsObjects.end(); anIt++) {
217 std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
218 ListOfShape& aUsedInOperationSolids = anIt->second;
220 // Collecting solids from compsolids which will not be modified in boolean operation.
221 ListOfShape aNotUsedSolids;
222 for(GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
223 std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
224 ListOfShape::iterator anIt = aUsedInOperationSolids.begin();
225 for(; anIt != aUsedInOperationSolids.end(); anIt++) {
226 if(aSolidInCompSolid->isEqual(*anIt)) {
230 if(anIt == aUsedInOperationSolids.end()) {
231 aNotUsedSolids.push_back(aSolidInCompSolid);
235 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(new GeomAlgoAPI_Boolean(aUsedInOperationSolids, aTools, myBooleanOperationType));
237 // Checking that the algorithm worked properly.
238 if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
239 setError("Error: Boolean algorithm failed.");
243 GeomAlgoAPI_MakeShapeList aMakeShapeList;
244 aMakeShapeList.appendAlgo(aBoolAlgo);
245 GeomAPI_DataMapOfShapeShape aMapOfShapes;
246 aMapOfShapes.merge(aBoolAlgo->mapOfSubShapes());
248 // Add result to not used solids from compsolid.
249 ListOfShape aShapesToAdd = aNotUsedSolids;
250 aShapesToAdd.push_back(aBoolAlgo->shape());
251 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
252 if(!aFillerAlgo->isDone()) {
253 std::string aFeatureError = "Error: PaveFiller algorithm failed.";
254 setError(aFeatureError);
258 aMakeShapeList.appendAlgo(aFillerAlgo);
259 aMapOfShapes.merge(aFillerAlgo->mapOfSubShapes());
261 if(GeomAlgoAPI_ShapeTools::volume(aFillerAlgo->shape()) > 1.e-7) {
262 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
263 loadNamingDS(aResultBody, aShells, aSolidsAlgos, aCompSolid, aTools, aFillerAlgo->shape(), aMakeShapeList, aMapOfShapes);
264 setResult(aResultBody, aResultIndex);
270 case GeomAlgoAPI_Boolean::BOOL_FUSE: {
271 // Collecting all solids which will be fused.
272 ListOfShape aSolidsToFuse;
273 aSolidsToFuse.insert(aSolidsToFuse.end(), anObjects.begin(), anObjects.end());
274 aSolidsToFuse.insert(aSolidsToFuse.end(), aTools.begin(), aTools.end());
276 // Collecting solids from compsolids which will not be modified in boolean operation.
277 ListOfShape aNotUsedSolids;
278 for(std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
279 anIt != aCompSolidsObjects.end(); anIt++) {
280 std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
281 ListOfShape& aUsedInOperationSolids = anIt->second;
282 aSolidsToFuse.insert(aSolidsToFuse.end(), aUsedInOperationSolids.begin(), aUsedInOperationSolids.end());
284 // Collect solids from compsolid which will not be modified in boolean operation.
285 for(GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
286 std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
287 ListOfShape::iterator anIt = aUsedInOperationSolids.begin();
288 for(; anIt != aUsedInOperationSolids.end(); anIt++) {
289 if(aSolidInCompSolid->isEqual(*anIt)) {
293 if(anIt == aUsedInOperationSolids.end()) {
294 aNotUsedSolids.push_back(aSolidInCompSolid);
299 ListOfShape anOriginalSolids = aSolidsToFuse;
300 anOriginalSolids.insert(anOriginalSolids.end(), aNotUsedSolids.begin(), aNotUsedSolids.end());
301 GeomAlgoAPI_MakeShapeList aMakeShapeList;
302 GeomAPI_DataMapOfShapeShape aMapOfShapes;
304 // If we have compsolids then cut with not used solids all others.
305 if(!aNotUsedSolids.empty()) {
306 aSolidsToFuse.clear();
307 for(ListOfShape::iterator anIt = anOriginalSolids.begin(); anIt != anOriginalSolids.end(); anIt++) {
308 ListOfShape aOneObjectList;
309 aOneObjectList.push_back(*anIt);
310 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(aOneObjectList, aNotUsedSolids, GeomAlgoAPI_Boolean::BOOL_CUT));
312 if(GeomAlgoAPI_ShapeTools::volume(aCutAlgo->shape()) > 1.e-7) {
313 aSolidsToFuse.push_back(aCutAlgo->shape());
314 aMakeShapeList.appendAlgo(aCutAlgo);
315 aMapOfShapes.merge(aCutAlgo->mapOfSubShapes());
321 anObjects.push_back(aSolidsToFuse.back());
322 aSolidsToFuse.pop_back();
323 aTools = aSolidsToFuse;
325 // Fuse all objects and all tools.
326 std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects, aTools, myBooleanOperationType));
328 // Checking that the algorithm worked properly.
329 if(!aFuseAlgo->isDone() || aFuseAlgo->shape()->isNull() || !aFuseAlgo->isValid()) {
330 static const std::string aFeatureError = "Error: Boolean algorithm failed.";
331 setError(aFeatureError);
335 std::shared_ptr<GeomAPI_Shape> aShape = aFuseAlgo->shape();
336 aMakeShapeList.appendAlgo(aFuseAlgo);
337 aMapOfShapes.merge(aFuseAlgo->mapOfSubShapes());
339 // Add result to not used solids from compsolid (if we have any).
340 if(!aNotUsedSolids.empty()) {
341 aNotUsedSolids.push_back(aShape);
342 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(new GeomAlgoAPI_PaveFiller(aNotUsedSolids, true));
343 if(!aFillerAlgo->isDone()) {
344 std::string aFeatureError = "Error: PaveFiller algorithm failed.";
345 setError(aFeatureError);
348 if(aFillerAlgo->shape()->isNull()) {
349 static const std::string aShapeError = "Error: Resulting shape is Null.";
350 setError(aShapeError);
353 if(!aFillerAlgo->isValid()) {
354 std::string aFeatureError = "Error: Resulting shape is not valid.";
355 setError(aFeatureError);
359 aShape = aFillerAlgo->shape();
360 aMakeShapeList.appendAlgo(aFillerAlgo);
361 aMapOfShapes.merge(aFillerAlgo->mapOfSubShapes());
364 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
365 loadNamingDS(aResultBody, aShells, aSolidsAlgos, anOriginalSolids.front(), anOriginalSolids, aShape, aMakeShapeList, aMapOfShapes);
366 setResult(aResultBody, aResultIndex);
371 setError("Error: Wrong type of boolean operation.");
376 // Remove the rest results if there were produced in the previous pass.
377 removeResults(aResultIndex);
380 //=================================================================================================
381 void FeaturesPlugin_CompositeBoolean::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
382 const ListOfShape& theShells,
383 ListOfMakeShape& theSolidsAlgos,
384 const std::shared_ptr<GeomAPI_Shape> theBaseShape,
385 const ListOfShape& theTools,
386 const std::shared_ptr<GeomAPI_Shape> theResultShape,
387 GeomAlgoAPI_MakeShape& theMakeShape,
388 GeomAPI_DataMapOfShapeShape& theMapOfShapes)
391 if(theBaseShape->isEqual(theResultShape)) {
392 theResultBody->store(theResultShape);
394 const int aGenTag = 1;
395 const int aModTag = 2;
396 const int aDelTag = 3;
397 const int aSubsolidsTag=4; /// sub solids will be placed at labels 6, 7, etc. if result is compound of solids
398 int aToTag = 5000; // may be many labels, starting from this index
399 int aFromTag = 10000; // may be many labels, starting from this index or last aToTag index
400 const std::string aGenName = "Generated";
401 const std::string aModName = "Modified";
402 const std::string aLatName = "LateralFace";
403 const std::string aFromName = "FromFace";
404 const std::string aToName = "ToFace";
406 theResultBody->storeModified(theBaseShape, theResultShape, aSubsolidsTag);
408 ListOfShape::const_iterator aShellsIter = theShells.begin();
409 ListOfMakeShape::const_iterator aSolidsAlgosIter = theSolidsAlgos.begin();
410 for(; aShellsIter != theShells.end() && aSolidsAlgosIter != theSolidsAlgos.end(); aShellsIter++, aSolidsAlgosIter++) {
411 //Insert lateral face : Face from Edge
412 std::shared_ptr<GeomAlgoAPI_MakeShape> aSolidAlgo = std::dynamic_pointer_cast<GeomAlgoAPI_MakeShape>(*aSolidsAlgosIter);
413 if(aSolidAlgo.get()) {
414 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = aSolidAlgo->mapOfSubShapes();
415 theResultBody->loadAndOrientGeneratedShapes(aSolidAlgo.get(), *aShellsIter, GeomAPI_Shape::EDGE, aGenTag,
416 aLatName, *aSubShapes.get());
419 std::shared_ptr<GeomAlgoAPI_MakeSweep> aSweepAlgo = std::dynamic_pointer_cast<GeomAlgoAPI_MakeSweep>(aSolidAlgo);
420 if(aSweepAlgo.get()) {
422 int aToFaceIndex = 1;
423 const ListOfShape& aToFaces = aSweepAlgo->toFaces();
424 for(ListOfShape::const_iterator anIt = aToFaces.cbegin(); anIt != aToFaces.cend(); anIt++) {
425 std::shared_ptr<GeomAPI_Shape> aToFace = *anIt;
426 if(aSubShapes->isBound(aToFace)) {
427 aToFace = aSubShapes->find(aToFace);
429 std::ostringstream aStr;
430 aStr << aToName << "_" << aToFaceIndex++;
431 theResultBody->generated(aToFace, aStr.str(), aToTag++);
435 int aFromFaceIndex = 1;
436 const ListOfShape& aFromFaces = aSweepAlgo->fromFaces();
437 if (aFromTag < aToTag) aFromTag = aToTag;
438 for(ListOfShape::const_iterator anIt = aFromFaces.cbegin(); anIt != aFromFaces.cend(); anIt++) {
439 std::shared_ptr<GeomAPI_Shape> aFromFace = *anIt;
440 if(aSubShapes->isBound(aFromFace)) {
441 aFromFace = aSubShapes->find(aFromFace);
443 std::ostringstream aStr;
444 aStr << aFromName << "_" << aFromFaceIndex++;
445 theResultBody->generated(aFromFace, aStr.str(), aFromTag++);
451 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE,
452 aModTag, aModName, theMapOfShapes);
453 theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE, aDelTag);
455 for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
456 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE,
457 aModTag, aModName, theMapOfShapes);
458 theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE, aDelTag);