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 AttributeSelectionListPtr aSelection =
33 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
34 LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
36 data()->addAttribute(SKETCH_OBJECT_ID(), ModelAPI_AttributeReference::typeId());
37 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
40 // Boolean works with solids always.
41 data()->addAttribute(BOOLEAN_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
42 aSelection = data()->selectionList(BOOLEAN_OBJECTS_ID());
43 aSelection->setSelectionType("SOLID");
45 initMakeSolidsAttributes();
47 data()->addAttribute(SKETCH_SELECTION_ID(), ModelAPI_AttributeSelection::typeId());
48 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SKETCH_SELECTION_ID());
51 //=================================================================================================
52 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeBoolean::addFeature(std::string theID)
54 std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
56 data()->reference(SKETCH_OBJECT_ID())->setValue(aNew);
58 // set as current also after it becomes sub to set correctly enabled for other sketch subs
59 document()->setCurrentFeature(aNew, false);
63 //=================================================================================================
64 int FeaturesPlugin_CompositeBoolean::numberOfSubs(bool forTree) const
66 ObjectPtr aObj = data()->reference(SKETCH_OBJECT_ID())->value();
67 return aObj.get()? 1 : 0;
70 //=================================================================================================
71 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeBoolean::subFeature(const int theIndex, bool forTree)
74 return std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_OBJECT_ID())->value());
75 return std::shared_ptr<ModelAPI_Feature>();
78 //=================================================================================================
79 int FeaturesPlugin_CompositeBoolean::subFeatureId(const int theIndex) const
83 std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_OBJECT_ID())->value());
85 return aFeature->data()->featureId();
90 //=================================================================================================
91 bool FeaturesPlugin_CompositeBoolean::isSub(ObjectPtr theObject) const
93 // check is this feature of result
94 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
98 ObjectPtr aSub = data()->reference(SKETCH_OBJECT_ID())->value();
99 return aSub == theObject;
102 //=================================================================================================
103 void FeaturesPlugin_CompositeBoolean::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
105 AttributeSelectionListPtr aFacesSelectionList = selectionList(LIST_ID());
106 if (aFacesSelectionList.get() && aFacesSelectionList->size() > 0)
107 aFacesSelectionList->clear();
110 //=================================================================================================
111 void FeaturesPlugin_CompositeBoolean::erase()
113 if (data().get() && data()->isValid()) { // on abort of sketch of this composite it may be invalid
115 std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_OBJECT_ID())->value());
116 if (aSketch.get() && aSketch->data()->isValid()) {
117 document()->removeFeature(aSketch);
120 ModelAPI_CompositeFeature::erase();
124 //=================================================================================================
125 void FeaturesPlugin_CompositeBoolean::execute()
127 /// feature extrusion does not have the next attribute
128 AttributeSelectionListPtr aFacesSelectionList = selectionList(LIST_ID());
129 if (aFacesSelectionList.get() && !aFacesSelectionList->isInitialized()) {
130 AttributeReferencePtr aSketchAttr = reference(SKETCH_OBJECT_ID());
131 if (aSketchAttr.get() && aSketchAttr->isInitialized())
132 setSketchObjectToList();
135 // Getting faces to create solids.
136 std::shared_ptr<ModelAPI_Feature> aSketchFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(
137 reference(SKETCH_OBJECT_ID())->value());
138 if(!aSketchFeature || aSketchFeature->results().empty()) {
141 ResultPtr aSketchRes = aSketchFeature->results().front();
142 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
143 if(!aConstruction.get()) {
146 selection(SKETCH_SELECTION_ID())->setValue(aSketchRes, std::shared_ptr<GeomAPI_Shape>());
147 int aSketchFacesNum = aConstruction->facesNum();
148 if(aSketchFacesNum == 0) {
151 ListOfShape aFacesList;
152 for(int aFaceIndex = 0; aFaceIndex < aSketchFacesNum; aFaceIndex++) {
153 std::shared_ptr<GeomAPI_Shape> aFace = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
154 aFacesList.push_back(aFace);
157 // Searching faces with common edges.
159 ListOfShape aFreeFaces;
160 std::shared_ptr<GeomAPI_Shape> aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aFacesList);
161 GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL, aShells, aFreeFaces);
162 aShells.insert(aShells.end(), aFreeFaces.begin(), aFreeFaces.end());
164 // Pass shells/faces to solids creation function.
166 ListOfMakeShape aSolidsAlgos;
167 makeSolids(aShells, aTools, aSolidsAlgos);
172 // Getting objects for boolean operation.
173 ListOfShape anObjects;
174 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
175 AttributeSelectionListPtr anObjectsSelList = selectionList(BOOLEAN_OBJECTS_ID());
176 if(anObjectsSelList->size() == 0) {
179 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
180 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
181 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
182 if(!anObject.get()) {
185 ResultPtr aContext = anObjectAttr->context();
186 ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(aContext);
187 if(aResCompSolidPtr.get()) {
188 std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
189 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
190 for(; anIt != aCompSolidsObjects.end(); anIt++) {
191 if(anIt->first->isEqual(aContextShape)) {
192 aCompSolidsObjects[anIt->first].push_back(anObject);
196 if(anIt == aCompSolidsObjects.end()) {
197 aCompSolidsObjects[aContextShape].push_back(anObject);
200 anObjects.push_back(anObject);
204 // Cut from each object solids.
205 int aResultIndex = 0;
207 switch(myBooleanOperationType) {
208 case GeomAlgoAPI_Boolean::BOOL_CUT:
209 case GeomAlgoAPI_Boolean::BOOL_COMMON:{
210 // Cut each object with all tools
211 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
212 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
213 ListOfShape aListWithObject;
214 aListWithObject.push_back(anObject);
215 GeomAlgoAPI_Boolean aBoolAlgo(aListWithObject, aTools, myBooleanOperationType);
217 // Checking that the algorithm worked properly.
218 if(!aBoolAlgo.isDone() || aBoolAlgo.shape()->isNull() || !aBoolAlgo.isValid()) {
219 setError("Error: Boolean algorithm failed.");
223 if(GeomAlgoAPI_ShapeTools::volume(aBoolAlgo.shape()) > 1.e-7) {
224 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
225 loadNamingDS(aResultBody, aShells, aSolidsAlgos, anObject, aTools, aBoolAlgo.shape(),
226 aBoolAlgo, *aBoolAlgo.mapOfSubShapes().get());
227 setResult(aResultBody, aResultIndex);
232 // Compsolids handling
233 for(std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
234 anIt != aCompSolidsObjects.end(); anIt++) {
235 std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
236 ListOfShape& aUsedInOperationSolids = anIt->second;
238 // Collecting solids from compsolids which will not be modified in boolean operation.
239 ListOfShape aNotUsedSolids;
240 for(GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
241 std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
242 ListOfShape::iterator anIt = aUsedInOperationSolids.begin();
243 for(; anIt != aUsedInOperationSolids.end(); anIt++) {
244 if(aSolidInCompSolid->isEqual(*anIt)) {
248 if(anIt == aUsedInOperationSolids.end()) {
249 aNotUsedSolids.push_back(aSolidInCompSolid);
253 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(new GeomAlgoAPI_Boolean(aUsedInOperationSolids, aTools, myBooleanOperationType));
255 // Checking that the algorithm worked properly.
256 if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
257 setError("Error: Boolean algorithm failed.");
261 GeomAlgoAPI_MakeShapeList aMakeShapeList;
262 aMakeShapeList.appendAlgo(aBoolAlgo);
263 GeomAPI_DataMapOfShapeShape aMapOfShapes;
264 aMapOfShapes.merge(aBoolAlgo->mapOfSubShapes());
266 // Add result to not used solids from compsolid.
267 ListOfShape aShapesToAdd = aNotUsedSolids;
268 aShapesToAdd.push_back(aBoolAlgo->shape());
269 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
270 if(!aFillerAlgo->isDone()) {
271 std::string aFeatureError = "Error: PaveFiller algorithm failed.";
272 setError(aFeatureError);
276 aMakeShapeList.appendAlgo(aFillerAlgo);
277 aMapOfShapes.merge(aFillerAlgo->mapOfSubShapes());
279 if(GeomAlgoAPI_ShapeTools::volume(aFillerAlgo->shape()) > 1.e-7) {
280 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
281 loadNamingDS(aResultBody, aShells, aSolidsAlgos, aCompSolid, aTools, aFillerAlgo->shape(), aMakeShapeList, aMapOfShapes);
282 setResult(aResultBody, aResultIndex);
288 case GeomAlgoAPI_Boolean::BOOL_FUSE: {
289 // Collecting all solids which will be fused.
290 ListOfShape aSolidsToFuse;
291 aSolidsToFuse.insert(aSolidsToFuse.end(), anObjects.begin(), anObjects.end());
292 aSolidsToFuse.insert(aSolidsToFuse.end(), aTools.begin(), aTools.end());
294 // Collecting solids from compsolids which will not be modified in boolean operation.
295 ListOfShape aNotUsedSolids;
296 for(std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
297 anIt != aCompSolidsObjects.end(); anIt++) {
298 std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
299 ListOfShape& aUsedInOperationSolids = anIt->second;
300 aSolidsToFuse.insert(aSolidsToFuse.end(), aUsedInOperationSolids.begin(), aUsedInOperationSolids.end());
302 // Collect solids from compsolid which will not be modified in boolean operation.
303 for(GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
304 std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
305 ListOfShape::iterator anIt = aUsedInOperationSolids.begin();
306 for(; anIt != aUsedInOperationSolids.end(); anIt++) {
307 if(aSolidInCompSolid->isEqual(*anIt)) {
311 if(anIt == aUsedInOperationSolids.end()) {
312 aNotUsedSolids.push_back(aSolidInCompSolid);
317 ListOfShape anOriginalSolids = aSolidsToFuse;
318 anOriginalSolids.insert(anOriginalSolids.end(), aNotUsedSolids.begin(), aNotUsedSolids.end());
319 GeomAlgoAPI_MakeShapeList aMakeShapeList;
320 GeomAPI_DataMapOfShapeShape aMapOfShapes;
322 // If we have compsolids then cut with not used solids all others.
323 if(!aNotUsedSolids.empty()) {
324 aSolidsToFuse.clear();
325 for(ListOfShape::iterator anIt = anOriginalSolids.begin(); anIt != anOriginalSolids.end(); anIt++) {
326 ListOfShape aOneObjectList;
327 aOneObjectList.push_back(*anIt);
328 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(aOneObjectList, aNotUsedSolids, GeomAlgoAPI_Boolean::BOOL_CUT));
330 if(GeomAlgoAPI_ShapeTools::volume(aCutAlgo->shape()) > 1.e-7) {
331 aSolidsToFuse.push_back(aCutAlgo->shape());
332 aMakeShapeList.appendAlgo(aCutAlgo);
333 aMapOfShapes.merge(aCutAlgo->mapOfSubShapes());
339 anObjects.push_back(aSolidsToFuse.back());
340 aSolidsToFuse.pop_back();
341 aTools = aSolidsToFuse;
343 // Fuse all objects and all tools.
344 std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects, aTools, myBooleanOperationType));
346 // Checking that the algorithm worked properly.
347 if(!aFuseAlgo->isDone() || aFuseAlgo->shape()->isNull() || !aFuseAlgo->isValid()) {
348 static const std::string aFeatureError = "Error: Boolean algorithm failed.";
349 setError(aFeatureError);
353 std::shared_ptr<GeomAPI_Shape> aShape = aFuseAlgo->shape();
354 aMakeShapeList.appendAlgo(aFuseAlgo);
355 aMapOfShapes.merge(aFuseAlgo->mapOfSubShapes());
357 // Add result to not used solids from compsolid (if we have any).
358 if(!aNotUsedSolids.empty()) {
359 aNotUsedSolids.push_back(aShape);
360 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(new GeomAlgoAPI_PaveFiller(aNotUsedSolids, true));
361 if(!aFillerAlgo->isDone()) {
362 std::string aFeatureError = "Error: PaveFiller algorithm failed.";
363 setError(aFeatureError);
366 if(aFillerAlgo->shape()->isNull()) {
367 static const std::string aShapeError = "Error: Resulting shape is Null.";
368 setError(aShapeError);
371 if(!aFillerAlgo->isValid()) {
372 std::string aFeatureError = "Error: Resulting shape is not valid.";
373 setError(aFeatureError);
377 aShape = aFillerAlgo->shape();
378 aMakeShapeList.appendAlgo(aFillerAlgo);
379 aMapOfShapes.merge(aFillerAlgo->mapOfSubShapes());
382 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
383 loadNamingDS(aResultBody, aShells, aSolidsAlgos, anOriginalSolids.front(), anOriginalSolids, aShape, aMakeShapeList, aMapOfShapes);
384 setResult(aResultBody, aResultIndex);
389 setError("Error: Wrong type of boolean operation.");
394 // Remove the rest results if there were produced in the previous pass.
395 removeResults(aResultIndex);
398 //=================================================================================================
399 void FeaturesPlugin_CompositeBoolean::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
400 const ListOfShape& theShells,
401 ListOfMakeShape& theSolidsAlgos,
402 const std::shared_ptr<GeomAPI_Shape> theBaseShape,
403 const ListOfShape& theTools,
404 const std::shared_ptr<GeomAPI_Shape> theResultShape,
405 GeomAlgoAPI_MakeShape& theMakeShape,
406 GeomAPI_DataMapOfShapeShape& theMapOfShapes)
409 if(theBaseShape->isEqual(theResultShape)) {
410 theResultBody->store(theResultShape);
412 const int aGenTag = 1;
413 const int aModTag = 2;
414 const int aDelTag = 3;
415 const int aSubsolidsTag=4; /// sub solids will be placed at labels 6, 7, etc. if result is compound of solids
416 int aToTag = 5000; // may be many labels, starting from this index
417 int aFromTag = 10000; // may be many labels, starting from this index or last aToTag index
418 const std::string aGenName = "Generated";
419 const std::string aModName = "Modified";
420 const std::string aLatName = "LateralFace";
421 const std::string aFromName = "FromFace";
422 const std::string aToName = "ToFace";
424 theResultBody->storeModified(theBaseShape, theResultShape, aSubsolidsTag);
426 ListOfShape::const_iterator aShellsIter = theShells.begin();
427 ListOfMakeShape::const_iterator aSolidsAlgosIter = theSolidsAlgos.begin();
428 for(; aShellsIter != theShells.end() && aSolidsAlgosIter != theSolidsAlgos.end(); aShellsIter++, aSolidsAlgosIter++) {
429 //Insert lateral face : Face from Edge
430 std::shared_ptr<GeomAlgoAPI_MakeShape> aSolidAlgo = std::dynamic_pointer_cast<GeomAlgoAPI_MakeShape>(*aSolidsAlgosIter);
431 if(aSolidAlgo.get()) {
432 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = aSolidAlgo->mapOfSubShapes();
433 theResultBody->loadAndOrientGeneratedShapes(aSolidAlgo.get(), *aShellsIter, GeomAPI_Shape::EDGE, aGenTag,
434 aLatName, *aSubShapes.get());
437 std::shared_ptr<GeomAlgoAPI_MakeSweep> aSweepAlgo = std::dynamic_pointer_cast<GeomAlgoAPI_MakeSweep>(aSolidAlgo);
438 if(aSweepAlgo.get()) {
440 int aToFaceIndex = 1;
441 const ListOfShape& aToFaces = aSweepAlgo->toShapes();
442 for(ListOfShape::const_iterator anIt = aToFaces.cbegin(); anIt != aToFaces.cend(); anIt++) {
443 std::shared_ptr<GeomAPI_Shape> aToFace = *anIt;
444 if(aSubShapes->isBound(aToFace)) {
445 aToFace = aSubShapes->find(aToFace);
447 std::ostringstream aStr;
448 aStr << aToName << "_" << aToFaceIndex++;
449 theResultBody->generated(aToFace, aStr.str(), aToTag++);
453 int aFromFaceIndex = 1;
454 const ListOfShape& aFromFaces = aSweepAlgo->fromShapes();
455 if (aFromTag < aToTag) aFromTag = aToTag;
456 for(ListOfShape::const_iterator anIt = aFromFaces.cbegin(); anIt != aFromFaces.cend(); anIt++) {
457 std::shared_ptr<GeomAPI_Shape> aFromFace = *anIt;
458 if(aSubShapes->isBound(aFromFace)) {
459 aFromFace = aSubShapes->find(aFromFace);
461 std::ostringstream aStr;
462 aStr << aFromName << "_" << aFromFaceIndex++;
463 theResultBody->generated(aFromFace, aStr.str(), aFromTag++);
469 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE,
470 aModTag, aModName, theMapOfShapes);
471 theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE, aDelTag);
473 for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
474 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE,
475 aModTag, aModName, theMapOfShapes);
476 theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE, aDelTag);
481 //=================================================================================================
482 void FeaturesPlugin_CompositeBoolean::setSketchObjectToList()
484 std::shared_ptr<ModelAPI_Feature> aSketchFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(
485 reference(SKETCH_OBJECT_ID())->value());
487 if(aSketchFeature.get() && !aSketchFeature->results().empty()) {
488 ResultPtr aSketchRes = aSketchFeature->results().front();
489 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
490 if(aConstruction.get()) {
491 AttributeSelectionListPtr aFacesSelectionList = selectionList(LIST_ID());
492 if (aFacesSelectionList.get() && aFacesSelectionList->size() == 0)
493 aFacesSelectionList->append(aSketchRes, std::shared_ptr<GeomAPI_Shape>());