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>
14 #include <ModelAPI_Session.h>
15 #include <ModelAPI_Validator.h>
17 #include <GeomAlgoAPI_CompoundBuilder.h>
18 #include <GeomAlgoAPI_Prism.h>
19 #include <GeomAlgoAPI_Revolution.h>
20 #include <GeomAlgoAPI_ShapeTools.h>
22 //=================================================================================================
23 void FeaturesPlugin_CompositeBoolean::initAttributes()
25 data()->addAttribute(SKETCH_OBJECT_ID(), ModelAPI_AttributeReference::typeId());
27 // Boolean works with solids always.
28 data()->addAttribute(BOOLEAN_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
29 AttributeSelectionListPtr aSelection = data()->selectionList(BOOLEAN_OBJECTS_ID());
30 aSelection->setSelectionType("SOLID");
32 initMakeSolidsAttributes();
34 data()->addAttribute(SKETCH_SELECTION_ID(), ModelAPI_AttributeSelection::typeId());
35 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SKETCH_SELECTION_ID());
38 //=================================================================================================
39 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeBoolean::addFeature(std::string theID)
41 std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
43 data()->reference(SKETCH_OBJECT_ID())->setValue(aNew);
45 // set as current also after it becomes sub to set correctly enabled for other sketch subs
46 document()->setCurrentFeature(aNew, false);
50 //=================================================================================================
51 int FeaturesPlugin_CompositeBoolean::numberOfSubs(bool forTree) const
53 ObjectPtr aObj = data()->reference(SKETCH_OBJECT_ID())->value();
54 return aObj.get()? 1 : 0;
57 //=================================================================================================
58 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeBoolean::subFeature(const int theIndex, bool forTree) const
61 return std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_OBJECT_ID())->value());
62 return std::shared_ptr<ModelAPI_Feature>();
65 //=================================================================================================
66 int FeaturesPlugin_CompositeBoolean::subFeatureId(const int theIndex) const
68 std::shared_ptr<ModelAPI_Feature> aFeature = subFeature(theIndex);
70 return aFeature->data()->featureId();
74 //=================================================================================================
75 bool FeaturesPlugin_CompositeBoolean::isSub(ObjectPtr theObject) const
77 // check is this feature of result
78 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
82 ObjectPtr aSub = data()->reference(SKETCH_OBJECT_ID())->value();
83 return aSub == theObject;
86 //=================================================================================================
87 void FeaturesPlugin_CompositeBoolean::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
91 //=================================================================================================
92 void FeaturesPlugin_CompositeBoolean::erase()
94 if (data().get() && data()->isValid()) { // on abort of sketch of this composite it may be invalid
96 std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_OBJECT_ID())->value());
97 if (aSketch.get() && aSketch->data()->isValid()) {
98 document()->removeFeature(aSketch);
101 ModelAPI_CompositeFeature::erase();
105 //=================================================================================================
106 void FeaturesPlugin_CompositeBoolean::execute()
108 // Getting faces to create solids.
109 std::shared_ptr<ModelAPI_Feature> aSketchFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(
110 reference(SKETCH_OBJECT_ID())->value());
111 if(!aSketchFeature || aSketchFeature->results().empty()) {
114 ResultPtr aSketchRes = aSketchFeature->results().front();
115 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
116 if(!aConstruction.get()) {
119 selection(SKETCH_SELECTION_ID())->setValue(aSketchRes, std::shared_ptr<GeomAPI_Shape>());
120 int aSketchFacesNum = aConstruction->facesNum();
121 if(aSketchFacesNum == 0) {
124 ListOfShape aFacesList;
125 for(int aFaceIndex = 0; aFaceIndex < aSketchFacesNum; aFaceIndex++) {
126 std::shared_ptr<GeomAPI_Shape> aFace = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
127 aFacesList.push_back(aFace);
130 // Searching faces with common edges.
132 ListOfShape aFreeFaces;
133 std::shared_ptr<GeomAPI_Shape> aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aFacesList);
134 GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL, aShells, aFreeFaces);
135 aShells.insert(aShells.end(), aFreeFaces.begin(), aFreeFaces.end());
137 // Pass shells/faces to solids creation function.
138 ListOfShape aBooleanTools;
139 std::list<std::shared_ptr<GeomAPI_Interface>> aSolidsAlgos;
140 makeSolids(aShells, aBooleanTools, aSolidsAlgos);
141 if(aBooleanTools.empty()) {
145 // Getting objects for boolean operation.
146 ListOfShape aBooleanObjects;
147 AttributeSelectionListPtr anObjectsSelList = selectionList(BOOLEAN_OBJECTS_ID());
148 if (anObjectsSelList->size() == 0) {
151 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
152 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
153 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
154 if(!anObject.get()) {
157 aBooleanObjects.push_back(anObject);
160 // Cut from each object solids.
161 int aResultIndex = 0;
163 switch(myBooleanOperationType) {
164 case GeomAlgoAPI_Boolean::BOOL_CUT:
165 case GeomAlgoAPI_Boolean::BOOL_COMMON:{
166 // Cut each object with all tools
167 for(ListOfShape::iterator anObjectsIt = aBooleanObjects.begin(); anObjectsIt != aBooleanObjects.end(); anObjectsIt++) {
168 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
169 ListOfShape aListWithObject;
170 aListWithObject.push_back(anObject);
171 GeomAlgoAPI_Boolean aBoolAlgo(aListWithObject, aBooleanTools, myBooleanOperationType);
173 // Checking that the algorithm worked properly.
174 if(!aBoolAlgo.isDone() || aBoolAlgo.shape()->isNull() || !aBoolAlgo.isValid()) {
175 setError("Boolean algorithm failed");
179 if(GeomAlgoAPI_ShapeTools::volume(aBoolAlgo.shape()) > 1.e-7) {
180 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
181 loadNamingDS(aResultBody, anObject, aShells, aSolidsAlgos, aBooleanTools, aBoolAlgo);
182 setResult(aResultBody, aResultIndex);
188 case GeomAlgoAPI_Boolean::BOOL_FUSE: {
189 // Fuse all objects and all tools.
190 GeomAlgoAPI_Boolean aBoolAlgo(aBooleanObjects, aBooleanTools, myBooleanOperationType);
192 // Checking that the algorithm worked properly.
193 if(!aBoolAlgo.isDone() || aBoolAlgo.shape()->isNull() || !aBoolAlgo.isValid()) {
194 setError("Boolean algorithm failed");
198 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
199 loadNamingDS(aResultBody, aBooleanObjects.front(), aShells, aSolidsAlgos, aBooleanTools, aBoolAlgo);
200 setResult(aResultBody, aResultIndex);
205 setError("Error: wrong type of boolean operation");
210 // Remove the rest results if there were produced in the previous pass.
211 removeResults(aResultIndex);
214 //=================================================================================================
215 void FeaturesPlugin_CompositeBoolean::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
216 const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
217 const ListOfShape& theShells,
218 const std::list<std::shared_ptr<GeomAPI_Interface>>& theSolidsAlgos,
219 const ListOfShape& theTools,
220 const GeomAlgoAPI_Boolean& theAlgo)
223 if(theBaseShape->isEqual(theAlgo.shape())) {
224 theResultBody->store(theAlgo.shape());
226 const int aGenTag = 1;
227 const int aModTag = 2;
228 const int aDelTag = 3;
229 const int aSubsolidsTag=4; /// sub solids will be placed at labels 6, 7, etc. if result is compound of solids
230 int aToTag = 5; // may be many labels, starting from this index
231 int aFromTag = 10000; // may be many labels, starting from this index or last aToTag index
232 const std::string aGenName = "Generated";
233 const std::string aModName = "Modified";
234 const std::string aLatName = "LateralFace";
235 const std::string aFromName = "FromFace";
236 const std::string aToName = "ToFace";
238 theResultBody->storeModified(theBaseShape, theAlgo.shape(), aSubsolidsTag);
240 ListOfShape::const_iterator aShellsIter = theShells.begin();
241 std::list<std::shared_ptr<GeomAPI_Interface>>::const_iterator aSolidsAlgosIter = theSolidsAlgos.begin();
242 for(; aShellsIter != theShells.end() && aSolidsAlgosIter != theSolidsAlgos.end(); aShellsIter++, aSolidsAlgosIter++) {
243 ListOfShape aFromFaces;
244 ListOfShape aToFaces;
245 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes;
247 //Insert lateral face : Face from Edge
248 if(std::dynamic_pointer_cast<GeomAlgoAPI_Prism>(*aSolidsAlgosIter)) {
249 std::shared_ptr<GeomAlgoAPI_Prism> aPrismAlgo = std::dynamic_pointer_cast<GeomAlgoAPI_Prism>(*aSolidsAlgosIter);
250 aSubShapes = aPrismAlgo->mapOfShapes();
251 theResultBody->loadAndOrientGeneratedShapes(aPrismAlgo->makeShape().get(), *aShellsIter, GeomAPI_Shape::EDGE, aGenTag,
252 aLatName, *aSubShapes.get());
254 aFromFaces = aPrismAlgo->fromFaces();
255 aToFaces = aPrismAlgo->toFaces();
256 } else if(std::dynamic_pointer_cast<GeomAlgoAPI_Revolution>(*aSolidsAlgosIter)) {
257 std::shared_ptr<GeomAlgoAPI_Revolution> aRevolAlgo = std::dynamic_pointer_cast<GeomAlgoAPI_Revolution>(*aSolidsAlgosIter);
258 aSubShapes = aRevolAlgo->mapOfShapes();
259 theResultBody->loadAndOrientGeneratedShapes(aRevolAlgo->makeShape().get(), *aShellsIter, GeomAPI_Shape::EDGE, aGenTag,
260 aLatName, *aSubShapes.get());
261 aFromFaces = aRevolAlgo->fromFaces();
262 aToFaces = aRevolAlgo->toFaces();
266 for(ListOfShape::const_iterator anIt = aToFaces.cbegin(); anIt != aToFaces.cend(); anIt++) {
267 std::shared_ptr<GeomAPI_Shape> aToFace = *anIt;
268 if(aSubShapes->isBound(aToFace)) {
269 aToFace = aSubShapes->find(aToFace);
271 theResultBody->generated(aToFace, aToName, aToTag++);
275 if (aFromTag < aToTag) aFromTag = aToTag;
276 for(ListOfShape::const_iterator anIt = aFromFaces.cbegin(); anIt != aFromFaces.cend(); anIt++) {
277 std::shared_ptr<GeomAPI_Shape> aFromFace = *anIt;
278 if(aSubShapes->isBound(aFromFace)) {
279 aFromFace = aSubShapes->find(aFromFace);
281 theResultBody->generated(aFromFace, aFromName, aFromTag++);
285 theResultBody->loadAndOrientModifiedShapes(theAlgo.makeShape().get(), theBaseShape, GeomAPI_Shape::FACE,
286 aModTag, aModName, *theAlgo.mapOfShapes().get());
287 theResultBody->loadDeletedShapes(theAlgo.makeShape().get(), theBaseShape, GeomAPI_Shape::FACE, aDelTag);
289 for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
290 theResultBody->loadAndOrientModifiedShapes(theAlgo.makeShape().get(), *anIter, GeomAPI_Shape::FACE,
291 aModTag, aModName, *theAlgo.mapOfShapes().get());
292 theResultBody->loadDeletedShapes(theAlgo.makeShape().get(), *anIter, GeomAPI_Shape::FACE, aDelTag);