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 for(ListOfShape::const_iterator anIter = aFreeFaces.cbegin(); anIter != aFreeFaces.cend(); anIter++) {
130 aShells.push_back(*anIter);
133 // Pass shells/faces to solids creation function.
134 ListOfShape aBooleanTools;
135 std::list<std::shared_ptr<GeomAPI_Interface>> aSolidsAlgos;
136 makeSolids(aShells, aBooleanTools, aSolidsAlgos);
137 if(aBooleanTools.empty()) {
141 // Getting objects for boolean operation.
142 ListOfShape aBooleanObjects;
143 AttributeSelectionListPtr anObjectsSelList = selectionList(BOOLEAN_OBJECTS_ID());
144 if (anObjectsSelList->size() == 0) {
147 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
148 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
149 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
150 if(!anObject.get()) {
153 aBooleanObjects.push_back(anObject);
156 // Cut from each object solids.
157 int aResultIndex = 0;
159 switch(myBooleanOperationType) {
160 case GeomAlgoAPI_Boolean::BOOL_CUT:
161 case GeomAlgoAPI_Boolean::BOOL_COMMON:{
162 // Cut each object with all tools
163 for(ListOfShape::iterator anObjectsIt = aBooleanObjects.begin(); anObjectsIt != aBooleanObjects.end(); anObjectsIt++) {
164 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
165 ListOfShape aListWithObject;
166 aListWithObject.push_back(anObject);
167 GeomAlgoAPI_Boolean aBoolAlgo(aListWithObject, aBooleanTools, myBooleanOperationType);
169 // Checking that the algorithm worked properly.
170 if(!aBoolAlgo.isDone() || aBoolAlgo.shape()->isNull() || !aBoolAlgo.isValid()) {
171 setError("Boolean algorithm failed");
175 if(GeomAlgoAPI_ShapeTools::volume(aBoolAlgo.shape()) > 1.e-7) {
176 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
177 loadNamingDS(aResultBody, anObject, aShells, aSolidsAlgos, aBooleanTools, aBoolAlgo);
178 setResult(aResultBody, aResultIndex);
184 case GeomAlgoAPI_Boolean::BOOL_FUSE: {
185 // Fuse all objects and all tools.
186 GeomAlgoAPI_Boolean aBoolAlgo(aBooleanObjects, aBooleanTools, myBooleanOperationType);
188 // Checking that the algorithm worked properly.
189 if(!aBoolAlgo.isDone() || aBoolAlgo.shape()->isNull() || !aBoolAlgo.isValid()) {
190 setError("Boolean algorithm failed");
194 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
195 loadNamingDS(aResultBody, aBooleanObjects.front(), aShells, aSolidsAlgos, aBooleanTools, aBoolAlgo);
196 setResult(aResultBody, aResultIndex);
201 setError("Error: wrong type of boolean operation");
206 // Remove the rest results if there were produced in the previous pass.
207 removeResults(aResultIndex);
210 //=================================================================================================
211 void FeaturesPlugin_CompositeBoolean::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
212 const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
213 const ListOfShape& theShells,
214 const std::list<std::shared_ptr<GeomAPI_Interface>>& theSolidsAlgos,
215 const ListOfShape& theTools,
216 const GeomAlgoAPI_Boolean& theAlgo)
219 if(theBaseShape->isEqual(theAlgo.shape())) {
220 theResultBody->store(theAlgo.shape());
222 const int aGenTag = 1;
223 const int aFromTag = 2;
224 const int aToTag = 3;
225 const int aModTag = 4;
226 const int aDelTag = 5;
227 const int aSubsolidsTag=6; /// sub solids will be placed at labels 6, 7, etc. if result is compound of solids
228 const std::string aGenName = "Generated";
229 const std::string aModName = "Modified";
230 const std::string aLatName = "LateralFace";
231 const std::string aFromName = "FromFace";
232 const std::string aToName = "ToFace";
234 theResultBody->storeModified(theBaseShape, theAlgo.shape(), aSubsolidsTag);
236 ListOfShape::const_iterator aShellsIter = theShells.begin();
237 std::list<std::shared_ptr<GeomAPI_Interface>>::const_iterator aSolidsAlgosIter = theSolidsAlgos.begin();
238 for(; aShellsIter != theShells.end() && aSolidsAlgosIter != theSolidsAlgos.end(); aShellsIter++, aSolidsAlgosIter++) {
239 ListOfShape aFromFaces;
240 ListOfShape aToFaces;
241 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes;
243 //Insert lateral face : Face from Edge
244 if(std::dynamic_pointer_cast<GeomAlgoAPI_Prism>(*aSolidsAlgosIter)) {
245 std::shared_ptr<GeomAlgoAPI_Prism> aPrismAlgo = std::dynamic_pointer_cast<GeomAlgoAPI_Prism>(*aSolidsAlgosIter);
246 aSubShapes = aPrismAlgo->mapOfShapes();
247 theResultBody->loadAndOrientGeneratedShapes(aPrismAlgo->makeShape().get(), *aShellsIter, GeomAPI_Shape::EDGE, aGenTag,
248 aLatName, *aSubShapes.get());
250 aFromFaces = aPrismAlgo->fromFaces();
251 aToFaces = aPrismAlgo->toFaces();
252 } else if(std::dynamic_pointer_cast<GeomAlgoAPI_Revolution>(*aSolidsAlgosIter)) {
253 std::shared_ptr<GeomAlgoAPI_Revolution> aRevolAlgo = std::dynamic_pointer_cast<GeomAlgoAPI_Revolution>(*aSolidsAlgosIter);
254 aSubShapes = aRevolAlgo->mapOfShapes();
255 theResultBody->loadAndOrientGeneratedShapes(aRevolAlgo->makeShape().get(), *aShellsIter, GeomAPI_Shape::EDGE, aGenTag,
256 aLatName, *aSubShapes.get());
257 aFromFaces = aRevolAlgo->fromFaces();
258 aToFaces = aRevolAlgo->toFaces();
262 for(ListOfShape::const_iterator anIt = aToFaces.cbegin(); anIt != aToFaces.cend(); anIt++) {
263 std::shared_ptr<GeomAPI_Shape> aToFace = *anIt;
264 if(aSubShapes->isBound(aToFace)) {
265 aToFace = aSubShapes->find(aToFace);
267 theResultBody->generated(aToFace, aToName, aToTag);
271 for(ListOfShape::const_iterator anIt = aFromFaces.cbegin(); anIt != aFromFaces.cend(); anIt++) {
272 std::shared_ptr<GeomAPI_Shape> aFromFace = *anIt;
273 if(aSubShapes->isBound(aFromFace)) {
274 aFromFace = aSubShapes->find(aFromFace);
276 theResultBody->generated(aFromFace, aFromName, aFromTag);
280 theResultBody->loadAndOrientModifiedShapes(theAlgo.makeShape().get(), theBaseShape, GeomAPI_Shape::FACE,
281 aModTag, aModName, *theAlgo.mapOfShapes().get());
282 theResultBody->loadDeletedShapes(theAlgo.makeShape().get(), theBaseShape, GeomAPI_Shape::FACE, aDelTag);
284 for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
285 theResultBody->loadAndOrientModifiedShapes(theAlgo.makeShape().get(), *anIter, GeomAPI_Shape::FACE,
286 aModTag, aModName, *theAlgo.mapOfShapes().get());
287 theResultBody->loadDeletedShapes(theAlgo.makeShape().get(), *anIter, GeomAPI_Shape::FACE, aDelTag);