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_BodyBuilder.h>
12 #include <ModelAPI_ResultBody.h>
13 #include <ModelAPI_ResultConstruction.h>
15 #include <GeomAlgoAPI_CompoundBuilder.h>
16 #include <GeomAlgoAPI_Prism.h>
17 #include <GeomAlgoAPI_Revolution.h>
18 #include <GeomAlgoAPI_ShapeTools.h>
20 //=================================================================================================
21 void FeaturesPlugin_CompositeBoolean::initAttributes()
23 data()->addAttribute(SKETCH_OBJECT_ID(), ModelAPI_AttributeReference::typeId());
25 // Boolean works with solids always.
26 data()->addAttribute(BOOLEAN_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
27 AttributeSelectionListPtr aSelection = data()->selectionList(BOOLEAN_OBJECTS_ID());
28 aSelection->setSelectionType("SOLID");
30 initMakeSolidsAttributes();
33 //=================================================================================================
34 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeBoolean::addFeature(std::string theID)
36 std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
38 data()->reference(SKETCH_OBJECT_ID())->setValue(aNew);
40 // set as current also after it becomes sub to set correctly enabled for other sketch subs
41 document()->setCurrentFeature(aNew, false);
45 //=================================================================================================
46 int FeaturesPlugin_CompositeBoolean::numberOfSubs(bool forTree) const
48 ObjectPtr aObj = data()->reference(SKETCH_OBJECT_ID())->value();
49 return aObj.get()? 1 : 0;
52 //=================================================================================================
53 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeBoolean::subFeature(const int theIndex, bool forTree) const
56 return std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_OBJECT_ID())->value());
57 return std::shared_ptr<ModelAPI_Feature>();
60 //=================================================================================================
61 int FeaturesPlugin_CompositeBoolean::subFeatureId(const int theIndex) const
63 std::shared_ptr<ModelAPI_Feature> aFeature = subFeature(theIndex);
65 return aFeature->data()->featureId();
69 //=================================================================================================
70 bool FeaturesPlugin_CompositeBoolean::isSub(ObjectPtr theObject) const
72 // check is this feature of result
73 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
77 ObjectPtr aSub = data()->reference(SKETCH_OBJECT_ID())->value();
78 return aSub == theObject;
81 //=================================================================================================
82 void FeaturesPlugin_CompositeBoolean::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
86 //=================================================================================================
87 void FeaturesPlugin_CompositeBoolean::erase()
89 if (data().get() && data()->isValid()) { // on abort of sketch of this composite it may be invalid
91 std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_OBJECT_ID())->value());
92 if (aSketch.get() && aSketch->data()->isValid()) {
93 document()->removeFeature(aSketch);
96 ModelAPI_CompositeFeature::erase();
100 //=================================================================================================
101 void FeaturesPlugin_CompositeBoolean::execute()
103 // Getting faces to create solids.
104 std::shared_ptr<ModelAPI_Feature> aSketchFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(
105 reference(SKETCH_OBJECT_ID())->value());
106 if(!aSketchFeature) {
109 ResultPtr aSketchRes = aSketchFeature->results().front();
110 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
111 if(!aConstruction.get()) {
114 int aSketchFacesNum = aConstruction->facesNum();
115 if(aSketchFacesNum == 0) {
118 ListOfShape aFacesList;
119 for(int aFaceIndex = 0; aFaceIndex < aSketchFacesNum; aFaceIndex++) {
120 std::shared_ptr<GeomAPI_Shape> aFace = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
121 aFacesList.push_back(aFace);
124 // Searching faces with common edges.
126 ListOfShape aFreeFaces;
127 std::shared_ptr<GeomAPI_Shape> aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aFacesList);
128 GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL, aShells, aFreeFaces);
129 aShells.insert(aShells.end(), aFreeFaces.begin(), aFreeFaces.end());
131 // Pass shells/faces to solids creation function.
132 ListOfShape aBooleanTools;
133 std::list<std::shared_ptr<GeomAPI_Interface>> aSolidsAlgos;
134 makeSolids(aShells, aBooleanTools, aSolidsAlgos);
135 if(aBooleanTools.empty()) {
139 // Getting objects for boolean operation.
140 ListOfShape aBooleanObjects;
141 AttributeSelectionListPtr anObjectsSelList = selectionList(BOOLEAN_OBJECTS_ID());
142 if (anObjectsSelList->size() == 0) {
145 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
146 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
147 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
148 if(!anObject.get()) {
151 aBooleanObjects.push_back(anObject);
154 // Cut from each object solids.
155 int aResultIndex = 0;
157 switch(myBooleanOperationType) {
158 case GeomAlgoAPI_Boolean::BOOL_CUT:
159 case GeomAlgoAPI_Boolean::BOOL_COMMON:{
160 // Cut each object with all tools
161 for(ListOfShape::iterator anObjectsIt = aBooleanObjects.begin(); anObjectsIt != aBooleanObjects.end(); anObjectsIt++) {
162 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
163 ListOfShape aListWithObject;
164 aListWithObject.push_back(anObject);
165 GeomAlgoAPI_Boolean aBoolAlgo(aListWithObject, aBooleanTools, myBooleanOperationType);
167 // Checking that the algorithm worked properly.
168 if(!aBoolAlgo.isDone() || aBoolAlgo.shape()->isNull() || !aBoolAlgo.isValid()) {
169 setError("Boolean algorithm failed");
173 if(GeomAlgoAPI_ShapeTools::volume(aBoolAlgo.shape()) > 1.e-7) {
174 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
175 loadNamingDS(aResultBody, anObject, aShells, aSolidsAlgos, aBooleanTools, aBoolAlgo);
176 setResult(aResultBody, aResultIndex);
182 case GeomAlgoAPI_Boolean::BOOL_FUSE: {
183 // Fuse all objects and all tools.
184 GeomAlgoAPI_Boolean aBoolAlgo(aBooleanObjects, aBooleanTools, myBooleanOperationType);
186 // Checking that the algorithm worked properly.
187 if(!aBoolAlgo.isDone() || aBoolAlgo.shape()->isNull() || !aBoolAlgo.isValid()) {
188 setError("Boolean algorithm failed");
192 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
193 loadNamingDS(aResultBody, aBooleanObjects.front(), aShells, aSolidsAlgos, aBooleanTools, aBoolAlgo);
194 setResult(aResultBody, aResultIndex);
199 setError("Error: wrong type of boolean operation");
204 // Remove the rest results if there were produced in the previous pass.
205 removeResults(aResultIndex);
208 //=================================================================================================
209 void FeaturesPlugin_CompositeBoolean::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
210 const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
211 const ListOfShape& theShells,
212 const std::list<std::shared_ptr<GeomAPI_Interface>>& theSolidsAlgos,
213 const ListOfShape& theTools,
214 const GeomAlgoAPI_Boolean& theAlgo)
217 if(theBaseShape->isEqual(theAlgo.shape())) {
218 theResultBody->store(theAlgo.shape());
220 const int aGenTag = 1;
221 const int aFromTag = 2;
222 const int aToTag = 3;
223 const int aModTag = 4;
224 const int aDelTag = 5;
225 const int aSubsolidsTag=6; /// sub solids will be placed at labels 6, 7, etc. if result is compound of solids
226 const std::string aGenName = "Generated";
227 const std::string aModName = "Modified";
228 const std::string aLatName = "LateralFace";
229 const std::string aFromName = "FromFace";
230 const std::string aToName = "ToFace";
232 theResultBody->storeModified(theBaseShape, theAlgo.shape(), aSubsolidsTag);
234 ListOfShape::const_iterator aShellsIter = theShells.begin();
235 std::list<std::shared_ptr<GeomAPI_Interface>>::const_iterator aSolidsAlgosIter = theSolidsAlgos.begin();
236 for(; aShellsIter != theShells.end() && aSolidsAlgosIter != theSolidsAlgos.end(); aShellsIter++, aSolidsAlgosIter++) {
237 ListOfShape aFromFaces;
238 ListOfShape aToFaces;
239 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes;
241 //Insert lateral face : Face from Edge
242 if(std::dynamic_pointer_cast<GeomAlgoAPI_Prism>(*aSolidsAlgosIter)) {
243 std::shared_ptr<GeomAlgoAPI_Prism> aPrismAlgo = std::dynamic_pointer_cast<GeomAlgoAPI_Prism>(*aSolidsAlgosIter);
244 aSubShapes = aPrismAlgo->mapOfShapes();
245 theResultBody->loadAndOrientGeneratedShapes(aPrismAlgo->makeShape().get(), *aShellsIter, GeomAPI_Shape::EDGE, aGenTag,
246 aLatName, *aSubShapes.get());
248 aFromFaces = aPrismAlgo->fromFaces();
249 aToFaces = aPrismAlgo->toFaces();
250 } else if(std::dynamic_pointer_cast<GeomAlgoAPI_Revolution>(*aSolidsAlgosIter)) {
251 std::shared_ptr<GeomAlgoAPI_Revolution> aRevolAlgo = std::dynamic_pointer_cast<GeomAlgoAPI_Revolution>(*aSolidsAlgosIter);
252 aSubShapes = aRevolAlgo->mapOfShapes();
253 theResultBody->loadAndOrientGeneratedShapes(aRevolAlgo->makeShape().get(), *aShellsIter, GeomAPI_Shape::EDGE, aGenTag,
254 aLatName, *aSubShapes.get());
255 aFromFaces = aRevolAlgo->fromFaces();
256 aToFaces = aRevolAlgo->toFaces();
260 for(ListOfShape::const_iterator anIt = aToFaces.cbegin(); anIt != aToFaces.cend(); anIt++) {
261 std::shared_ptr<GeomAPI_Shape> aToFace = *anIt;
262 if(aSubShapes->isBound(aToFace)) {
263 aToFace = aSubShapes->find(aToFace);
265 theResultBody->generated(aToFace, aToName, aToTag);
269 for(ListOfShape::const_iterator anIt = aFromFaces.cbegin(); anIt != aFromFaces.cend(); anIt++) {
270 std::shared_ptr<GeomAPI_Shape> aFromFace = *anIt;
271 if(aSubShapes->isBound(aFromFace)) {
272 aFromFace = aSubShapes->find(aFromFace);
274 theResultBody->generated(aFromFace, aFromName, aFromTag);
278 theResultBody->loadAndOrientModifiedShapes(theAlgo.makeShape().get(), theBaseShape, GeomAPI_Shape::FACE,
279 aModTag, aModName, *theAlgo.mapOfShapes().get());
280 theResultBody->loadDeletedShapes(theAlgo.makeShape().get(), theBaseShape, GeomAPI_Shape::FACE, aDelTag);
282 for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
283 theResultBody->loadAndOrientModifiedShapes(theAlgo.makeShape().get(), *anIter, GeomAPI_Shape::FACE,
284 aModTag, aModName, *theAlgo.mapOfShapes().get());
285 theResultBody->loadDeletedShapes(theAlgo.makeShape().get(), *anIter, GeomAPI_Shape::FACE, aDelTag);