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>
29 //=================================================================================================
30 void FeaturesPlugin_CompositeBoolean::initAttributes()
32 data()->addAttribute(SKETCH_OBJECT_ID(), ModelAPI_AttributeReference::typeId());
34 // Boolean works with solids always.
35 data()->addAttribute(BOOLEAN_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
36 AttributeSelectionListPtr aSelection = data()->selectionList(BOOLEAN_OBJECTS_ID());
37 aSelection->setSelectionType("SOLID");
39 initMakeSolidsAttributes();
41 data()->addAttribute(SKETCH_SELECTION_ID(), ModelAPI_AttributeSelection::typeId());
42 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SKETCH_SELECTION_ID());
45 //=================================================================================================
46 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeBoolean::addFeature(std::string theID)
48 std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
50 data()->reference(SKETCH_OBJECT_ID())->setValue(aNew);
52 // set as current also after it becomes sub to set correctly enabled for other sketch subs
53 document()->setCurrentFeature(aNew, false);
57 //=================================================================================================
58 int FeaturesPlugin_CompositeBoolean::numberOfSubs(bool forTree) const
60 ObjectPtr aObj = data()->reference(SKETCH_OBJECT_ID())->value();
61 return aObj.get()? 1 : 0;
64 //=================================================================================================
65 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeBoolean::subFeature(const int theIndex, bool forTree)
68 return std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_OBJECT_ID())->value());
69 return std::shared_ptr<ModelAPI_Feature>();
72 //=================================================================================================
73 int FeaturesPlugin_CompositeBoolean::subFeatureId(const int theIndex) const
77 std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_OBJECT_ID())->value());
79 return aFeature->data()->featureId();
84 //=================================================================================================
85 bool FeaturesPlugin_CompositeBoolean::isSub(ObjectPtr theObject) const
87 // check is this feature of result
88 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
92 ObjectPtr aSub = data()->reference(SKETCH_OBJECT_ID())->value();
93 return aSub == theObject;
96 //=================================================================================================
97 void FeaturesPlugin_CompositeBoolean::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
101 //=================================================================================================
102 void FeaturesPlugin_CompositeBoolean::erase()
104 if (data().get() && data()->isValid()) { // on abort of sketch of this composite it may be invalid
106 std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_OBJECT_ID())->value());
107 if (aSketch.get() && aSketch->data()->isValid()) {
108 document()->removeFeature(aSketch);
111 ModelAPI_CompositeFeature::erase();
115 //=================================================================================================
116 void FeaturesPlugin_CompositeBoolean::execute()
118 // Getting faces to create solids.
119 std::shared_ptr<ModelAPI_Feature> aSketchFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(
120 reference(SKETCH_OBJECT_ID())->value());
121 if(!aSketchFeature || aSketchFeature->results().empty()) {
124 ResultPtr aSketchRes = aSketchFeature->results().front();
125 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
126 if(!aConstruction.get()) {
129 selection(SKETCH_SELECTION_ID())->setValue(aSketchRes, std::shared_ptr<GeomAPI_Shape>());
130 int aSketchFacesNum = aConstruction->facesNum();
131 if(aSketchFacesNum == 0) {
134 ListOfShape aFacesList;
135 for(int aFaceIndex = 0; aFaceIndex < aSketchFacesNum; aFaceIndex++) {
136 std::shared_ptr<GeomAPI_Shape> aFace = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
137 aFacesList.push_back(aFace);
140 // Searching faces with common edges.
142 ListOfShape aFreeFaces;
143 std::shared_ptr<GeomAPI_Shape> aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aFacesList);
144 GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL, aShells, aFreeFaces);
145 aShells.insert(aShells.end(), aFreeFaces.begin(), aFreeFaces.end());
147 // Pass shells/faces to solids creation function.
149 ListOfMakeShape aSolidsAlgos;
150 makeSolids(aShells, aTools, aSolidsAlgos);
155 // Getting objects for boolean operation.
156 ListOfShape anObjects;
157 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
158 AttributeSelectionListPtr anObjectsSelList = selectionList(BOOLEAN_OBJECTS_ID());
159 if(anObjectsSelList->size() == 0) {
162 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
163 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
164 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
165 if(!anObject.get()) {
168 ResultPtr aContext = anObjectAttr->context();
169 ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(aContext);
170 if(aResCompSolidPtr.get()) {
171 std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
172 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
173 for(; anIt != aCompSolidsObjects.end(); anIt++) {
174 if(anIt->first->isEqual(aContextShape)) {
175 aCompSolidsObjects[anIt->first].push_back(anObject);
179 if(anIt == aCompSolidsObjects.end()) {
180 aCompSolidsObjects[aContextShape].push_back(anObject);
183 anObjects.push_back(anObject);
187 // Cut from each object solids.
188 int aResultIndex = 0;
190 switch(myBooleanOperationType) {
191 case GeomAlgoAPI_Boolean::BOOL_CUT:
192 case GeomAlgoAPI_Boolean::BOOL_COMMON:{
193 // Cut each object with all tools
194 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
195 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
196 ListOfShape aListWithObject;
197 aListWithObject.push_back(anObject);
198 GeomAlgoAPI_Boolean aBoolAlgo(aListWithObject, aTools, myBooleanOperationType);
200 // Checking that the algorithm worked properly.
201 if(!aBoolAlgo.isDone() || aBoolAlgo.shape()->isNull() || !aBoolAlgo.isValid()) {
202 setError("Error: Boolean algorithm failed.");
206 if(GeomAlgoAPI_ShapeTools::volume(aBoolAlgo.shape()) > 1.e-7) {
207 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
208 loadNamingDS(aResultBody, aShells, aSolidsAlgos, anObject, aTools, aBoolAlgo.shape(),
209 aBoolAlgo, *aBoolAlgo.mapOfSubShapes().get());
210 setResult(aResultBody, aResultIndex);
215 // Compsolids handling
216 for(std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
217 anIt != aCompSolidsObjects.end(); anIt++) {
218 std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
219 ListOfShape& aUsedInOperationSolids = anIt->second;
221 // Collecting solids from compsolids which will not be modified in boolean operation.
222 ListOfShape aNotUsedSolids;
223 for(GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
224 std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
225 ListOfShape::iterator anIt = aUsedInOperationSolids.begin();
226 for(; anIt != aUsedInOperationSolids.end(); anIt++) {
227 if(aSolidInCompSolid->isEqual(*anIt)) {
231 if(anIt == aUsedInOperationSolids.end()) {
232 aNotUsedSolids.push_back(aSolidInCompSolid);
236 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(new GeomAlgoAPI_Boolean(aUsedInOperationSolids, aTools, myBooleanOperationType));
238 // Checking that the algorithm worked properly.
239 if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
240 setError("Error: Boolean algorithm failed.");
244 GeomAlgoAPI_MakeShapeList aMakeShapeList;
245 aMakeShapeList.appendAlgo(aBoolAlgo);
246 GeomAPI_DataMapOfShapeShape aMapOfShapes;
247 aMapOfShapes.merge(aBoolAlgo->mapOfSubShapes());
249 // Add result to not used solids from compsolid.
250 ListOfShape aShapesToAdd = aNotUsedSolids;
251 aShapesToAdd.push_back(aBoolAlgo->shape());
252 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
253 if(!aFillerAlgo->isDone()) {
254 std::string aFeatureError = "Error: PaveFiller algorithm failed.";
255 setError(aFeatureError);
259 aMakeShapeList.appendAlgo(aFillerAlgo);
260 aMapOfShapes.merge(aFillerAlgo->mapOfSubShapes());
262 if(GeomAlgoAPI_ShapeTools::volume(aFillerAlgo->shape()) > 1.e-7) {
263 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
264 loadNamingDS(aResultBody, aShells, aSolidsAlgos, aCompSolid, aTools, aFillerAlgo->shape(), aMakeShapeList, aMapOfShapes);
265 setResult(aResultBody, aResultIndex);
271 case GeomAlgoAPI_Boolean::BOOL_FUSE: {
272 // Collecting all solids which will be fused.
273 ListOfShape aSolidsToFuse;
274 aSolidsToFuse.insert(aSolidsToFuse.end(), anObjects.begin(), anObjects.end());
275 aSolidsToFuse.insert(aSolidsToFuse.end(), aTools.begin(), aTools.end());
277 // Collecting solids from compsolids which will not be modified in boolean operation.
278 ListOfShape aNotUsedSolids;
279 for(std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
280 anIt != aCompSolidsObjects.end(); anIt++) {
281 std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
282 ListOfShape& aUsedInOperationSolids = anIt->second;
283 aSolidsToFuse.insert(aSolidsToFuse.end(), aUsedInOperationSolids.begin(), aUsedInOperationSolids.end());
285 // Collect solids from compsolid which will not be modified in boolean operation.
286 for(GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
287 std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
288 ListOfShape::iterator anIt = aUsedInOperationSolids.begin();
289 for(; anIt != aUsedInOperationSolids.end(); anIt++) {
290 if(aSolidInCompSolid->isEqual(*anIt)) {
294 if(anIt == aUsedInOperationSolids.end()) {
295 aNotUsedSolids.push_back(aSolidInCompSolid);
300 ListOfShape anOriginalSolids = aSolidsToFuse;
301 anOriginalSolids.insert(anOriginalSolids.end(), aNotUsedSolids.begin(), aNotUsedSolids.end());
302 GeomAlgoAPI_MakeShapeList aMakeShapeList;
303 GeomAPI_DataMapOfShapeShape aMapOfShapes;
305 // If we have compsolids then cut with not used solids all others.
306 if(!aNotUsedSolids.empty()) {
307 aSolidsToFuse.clear();
308 for(ListOfShape::iterator anIt = anOriginalSolids.begin(); anIt != anOriginalSolids.end(); anIt++) {
309 ListOfShape aOneObjectList;
310 aOneObjectList.push_back(*anIt);
311 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(aOneObjectList, aNotUsedSolids, GeomAlgoAPI_Boolean::BOOL_CUT));
313 if(GeomAlgoAPI_ShapeTools::volume(aCutAlgo->shape()) > 1.e-7) {
314 aSolidsToFuse.push_back(aCutAlgo->shape());
315 aMakeShapeList.appendAlgo(aCutAlgo);
316 aMapOfShapes.merge(aCutAlgo->mapOfSubShapes());
322 anObjects.push_back(aSolidsToFuse.back());
323 aSolidsToFuse.pop_back();
324 aTools = aSolidsToFuse;
326 // Fuse all objects and all tools.
327 std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects, aTools, myBooleanOperationType));
329 // Checking that the algorithm worked properly.
330 if(!aFuseAlgo->isDone() || aFuseAlgo->shape()->isNull() || !aFuseAlgo->isValid()) {
331 static const std::string aFeatureError = "Error: Boolean algorithm failed.";
332 setError(aFeatureError);
336 std::shared_ptr<GeomAPI_Shape> aShape = aFuseAlgo->shape();
337 aMakeShapeList.appendAlgo(aFuseAlgo);
338 aMapOfShapes.merge(aFuseAlgo->mapOfSubShapes());
340 // Add result to not used solids from compsolid (if we have any).
341 if(!aNotUsedSolids.empty()) {
342 aNotUsedSolids.push_back(aShape);
343 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(new GeomAlgoAPI_PaveFiller(aNotUsedSolids, true));
344 if(!aFillerAlgo->isDone()) {
345 std::string aFeatureError = "Error: PaveFiller algorithm failed.";
346 setError(aFeatureError);
349 if(aFillerAlgo->shape()->isNull()) {
350 static const std::string aShapeError = "Error: Resulting shape is Null.";
351 setError(aShapeError);
354 if(!aFillerAlgo->isValid()) {
355 std::string aFeatureError = "Error: Resulting shape is not valid.";
356 setError(aFeatureError);
360 aShape = aFillerAlgo->shape();
361 aMakeShapeList.appendAlgo(aFillerAlgo);
362 aMapOfShapes.merge(aFillerAlgo->mapOfSubShapes());
365 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
366 loadNamingDS(aResultBody, aShells, aSolidsAlgos, anOriginalSolids.front(), anOriginalSolids, aShape, aMakeShapeList, aMapOfShapes);
367 setResult(aResultBody, aResultIndex);
372 setError("Error: Wrong type of boolean operation.");
377 // Remove the rest results if there were produced in the previous pass.
378 removeResults(aResultIndex);
381 //=================================================================================================
382 void FeaturesPlugin_CompositeBoolean::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
383 const ListOfShape& theShells,
384 ListOfMakeShape& theSolidsAlgos,
385 const std::shared_ptr<GeomAPI_Shape> theBaseShape,
386 const ListOfShape& theTools,
387 const std::shared_ptr<GeomAPI_Shape> theResultShape,
388 GeomAlgoAPI_MakeShape& theMakeShape,
389 GeomAPI_DataMapOfShapeShape& theMapOfShapes)
392 if(theBaseShape->isEqual(theResultShape)) {
393 theResultBody->store(theResultShape);
395 const int aGenTag = 1;
396 const int aModTag = 2;
397 const int aDelTag = 3;
398 const int aSubsolidsTag=4; /// sub solids will be placed at labels 6, 7, etc. if result is compound of solids
399 int aToTag = 5000; // may be many labels, starting from this index
400 int aFromTag = 10000; // may be many labels, starting from this index or last aToTag index
401 const std::string aGenName = "Generated";
402 const std::string aModName = "Modified";
403 const std::string aLatName = "LateralFace";
404 const std::string aFromName = "FromFace";
405 const std::string aToName = "ToFace";
407 theResultBody->storeModified(theBaseShape, theResultShape, aSubsolidsTag);
409 ListOfShape::const_iterator aShellsIter = theShells.begin();
410 ListOfMakeShape::const_iterator aSolidsAlgosIter = theSolidsAlgos.begin();
411 for(; aShellsIter != theShells.end() && aSolidsAlgosIter != theSolidsAlgos.end(); aShellsIter++, aSolidsAlgosIter++) {
412 //Insert lateral face : Face from Edge
413 std::shared_ptr<GeomAlgoAPI_MakeShape> aSolidAlgo = std::dynamic_pointer_cast<GeomAlgoAPI_MakeShape>(*aSolidsAlgosIter);
414 if(aSolidAlgo.get()) {
415 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = aSolidAlgo->mapOfSubShapes();
416 theResultBody->loadAndOrientGeneratedShapes(aSolidAlgo.get(), *aShellsIter, GeomAPI_Shape::EDGE, aGenTag,
417 aLatName, *aSubShapes.get());
420 std::shared_ptr<GeomAlgoAPI_MakeSweep> aSweepAlgo = std::dynamic_pointer_cast<GeomAlgoAPI_MakeSweep>(aSolidAlgo);
421 if(aSweepAlgo.get()) {
423 int aToFaceIndex = 1;
424 const ListOfShape& aToFaces = aSweepAlgo->toShapes();
425 for(ListOfShape::const_iterator anIt = aToFaces.cbegin(); anIt != aToFaces.cend(); anIt++) {
426 std::shared_ptr<GeomAPI_Shape> aToFace = *anIt;
427 if(aSubShapes->isBound(aToFace)) {
428 aToFace = aSubShapes->find(aToFace);
430 std::ostringstream aStr;
431 aStr << aToName << "_" << aToFaceIndex++;
432 theResultBody->generated(aToFace, aStr.str(), aToTag++);
436 int aFromFaceIndex = 1;
437 const ListOfShape& aFromFaces = aSweepAlgo->fromShapes();
438 if (aFromTag < aToTag) aFromTag = aToTag;
439 for(ListOfShape::const_iterator anIt = aFromFaces.cbegin(); anIt != aFromFaces.cend(); anIt++) {
440 std::shared_ptr<GeomAPI_Shape> aFromFace = *anIt;
441 if(aSubShapes->isBound(aFromFace)) {
442 aFromFace = aSubShapes->find(aFromFace);
444 std::ostringstream aStr;
445 aStr << aFromName << "_" << aFromFaceIndex++;
446 theResultBody->generated(aFromFace, aStr.str(), aFromTag++);
452 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE,
453 aModTag, aModName, theMapOfShapes);
454 theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE, aDelTag);
456 for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
457 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE,
458 aModTag, aModName, theMapOfShapes);
459 theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE, aDelTag);