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)
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
70 std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_OBJECT_ID())->value());
72 return aFeature->data()->featureId();
77 //=================================================================================================
78 bool FeaturesPlugin_CompositeBoolean::isSub(ObjectPtr theObject) const
80 // check is this feature of result
81 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
85 ObjectPtr aSub = data()->reference(SKETCH_OBJECT_ID())->value();
86 return aSub == theObject;
89 //=================================================================================================
90 void FeaturesPlugin_CompositeBoolean::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
94 //=================================================================================================
95 void FeaturesPlugin_CompositeBoolean::erase()
97 if (data().get() && data()->isValid()) { // on abort of sketch of this composite it may be invalid
99 std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_OBJECT_ID())->value());
100 if (aSketch.get() && aSketch->data()->isValid()) {
101 document()->removeFeature(aSketch);
104 ModelAPI_CompositeFeature::erase();
108 //=================================================================================================
109 void FeaturesPlugin_CompositeBoolean::execute()
111 // Getting faces to create solids.
112 std::shared_ptr<ModelAPI_Feature> aSketchFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(
113 reference(SKETCH_OBJECT_ID())->value());
114 if(!aSketchFeature || aSketchFeature->results().empty()) {
117 ResultPtr aSketchRes = aSketchFeature->results().front();
118 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
119 if(!aConstruction.get()) {
122 selection(SKETCH_SELECTION_ID())->setValue(aSketchRes, std::shared_ptr<GeomAPI_Shape>());
123 int aSketchFacesNum = aConstruction->facesNum();
124 if(aSketchFacesNum == 0) {
127 ListOfShape aFacesList;
128 for(int aFaceIndex = 0; aFaceIndex < aSketchFacesNum; aFaceIndex++) {
129 std::shared_ptr<GeomAPI_Shape> aFace = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
130 aFacesList.push_back(aFace);
133 // Searching faces with common edges.
135 ListOfShape aFreeFaces;
136 std::shared_ptr<GeomAPI_Shape> aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aFacesList);
137 GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL, aShells, aFreeFaces);
138 aShells.insert(aShells.end(), aFreeFaces.begin(), aFreeFaces.end());
140 // Pass shells/faces to solids creation function.
141 ListOfShape aBooleanTools;
142 std::list<std::shared_ptr<GeomAPI_Interface>> aSolidsAlgos;
143 makeSolids(aShells, aBooleanTools, aSolidsAlgos);
144 if(aBooleanTools.empty()) {
148 // Getting objects for boolean operation.
149 ListOfShape aBooleanObjects;
150 AttributeSelectionListPtr anObjectsSelList = selectionList(BOOLEAN_OBJECTS_ID());
151 if (anObjectsSelList->size() == 0) {
154 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
155 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
156 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
157 if(!anObject.get()) {
160 aBooleanObjects.push_back(anObject);
163 // Cut from each object solids.
164 int aResultIndex = 0;
166 switch(myBooleanOperationType) {
167 case GeomAlgoAPI_Boolean::BOOL_CUT:
168 case GeomAlgoAPI_Boolean::BOOL_COMMON:{
169 // Cut each object with all tools
170 for(ListOfShape::iterator anObjectsIt = aBooleanObjects.begin(); anObjectsIt != aBooleanObjects.end(); anObjectsIt++) {
171 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
172 ListOfShape aListWithObject;
173 aListWithObject.push_back(anObject);
174 GeomAlgoAPI_Boolean aBoolAlgo(aListWithObject, aBooleanTools, myBooleanOperationType);
176 // Checking that the algorithm worked properly.
177 if(!aBoolAlgo.isDone() || aBoolAlgo.shape()->isNull() || !aBoolAlgo.isValid()) {
178 setError("Boolean algorithm failed");
182 if(GeomAlgoAPI_ShapeTools::volume(aBoolAlgo.shape()) > 1.e-7) {
183 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
184 loadNamingDS(aResultBody, anObject, aShells, aSolidsAlgos, aBooleanTools, aBoolAlgo);
185 setResult(aResultBody, aResultIndex);
191 case GeomAlgoAPI_Boolean::BOOL_FUSE: {
192 // Fuse all objects and all tools.
193 GeomAlgoAPI_Boolean aBoolAlgo(aBooleanObjects, aBooleanTools, myBooleanOperationType);
195 // Checking that the algorithm worked properly.
196 if(!aBoolAlgo.isDone() || aBoolAlgo.shape()->isNull() || !aBoolAlgo.isValid()) {
197 setError("Boolean algorithm failed");
201 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
202 loadNamingDS(aResultBody, aBooleanObjects.front(), aShells, aSolidsAlgos, aBooleanTools, aBoolAlgo);
203 setResult(aResultBody, aResultIndex);
208 setError("Error: wrong type of boolean operation");
213 // Remove the rest results if there were produced in the previous pass.
214 removeResults(aResultIndex);
217 //=================================================================================================
218 void FeaturesPlugin_CompositeBoolean::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
219 const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
220 const ListOfShape& theShells,
221 const std::list<std::shared_ptr<GeomAPI_Interface>>& theSolidsAlgos,
222 const ListOfShape& theTools,
223 const GeomAlgoAPI_Boolean& theAlgo)
226 if(theBaseShape->isEqual(theAlgo.shape())) {
227 theResultBody->store(theAlgo.shape());
229 const int aGenTag = 1;
230 const int aModTag = 2;
231 const int aDelTag = 3;
232 const int aSubsolidsTag=4; /// sub solids will be placed at labels 6, 7, etc. if result is compound of solids
233 int aToTag = 5; // may be many labels, starting from this index
234 int aFromTag = 10000; // may be many labels, starting from this index or last aToTag index
235 const std::string aGenName = "Generated";
236 const std::string aModName = "Modified";
237 const std::string aLatName = "LateralFace";
238 const std::string aFromName = "FromFace";
239 const std::string aToName = "ToFace";
241 theResultBody->storeModified(theBaseShape, theAlgo.shape(), aSubsolidsTag);
243 ListOfShape::const_iterator aShellsIter = theShells.begin();
244 std::list<std::shared_ptr<GeomAPI_Interface>>::const_iterator aSolidsAlgosIter = theSolidsAlgos.begin();
245 for(; aShellsIter != theShells.end() && aSolidsAlgosIter != theSolidsAlgos.end(); aShellsIter++, aSolidsAlgosIter++) {
246 ListOfShape aFromFaces;
247 ListOfShape aToFaces;
248 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes;
250 //Insert lateral face : Face from Edge
251 if(std::dynamic_pointer_cast<GeomAlgoAPI_Prism>(*aSolidsAlgosIter)) {
252 std::shared_ptr<GeomAlgoAPI_Prism> aPrismAlgo = std::dynamic_pointer_cast<GeomAlgoAPI_Prism>(*aSolidsAlgosIter);
253 aSubShapes = aPrismAlgo->mapOfShapes();
254 theResultBody->loadAndOrientGeneratedShapes(aPrismAlgo->makeShape().get(), *aShellsIter, GeomAPI_Shape::EDGE, aGenTag,
255 aLatName, *aSubShapes.get());
257 aFromFaces = aPrismAlgo->fromFaces();
258 aToFaces = aPrismAlgo->toFaces();
259 } else if(std::dynamic_pointer_cast<GeomAlgoAPI_Revolution>(*aSolidsAlgosIter)) {
260 std::shared_ptr<GeomAlgoAPI_Revolution> aRevolAlgo = std::dynamic_pointer_cast<GeomAlgoAPI_Revolution>(*aSolidsAlgosIter);
261 aSubShapes = aRevolAlgo->mapOfShapes();
262 theResultBody->loadAndOrientGeneratedShapes(aRevolAlgo->makeShape().get(), *aShellsIter, GeomAPI_Shape::EDGE, aGenTag,
263 aLatName, *aSubShapes.get());
264 aFromFaces = aRevolAlgo->fromFaces();
265 aToFaces = aRevolAlgo->toFaces();
269 for(ListOfShape::const_iterator anIt = aToFaces.cbegin(); anIt != aToFaces.cend(); anIt++) {
270 std::shared_ptr<GeomAPI_Shape> aToFace = *anIt;
271 if(aSubShapes->isBound(aToFace)) {
272 aToFace = aSubShapes->find(aToFace);
274 theResultBody->generated(aToFace, aToName, aToTag++);
278 if (aFromTag < aToTag) aFromTag = aToTag;
279 for(ListOfShape::const_iterator anIt = aFromFaces.cbegin(); anIt != aFromFaces.cend(); anIt++) {
280 std::shared_ptr<GeomAPI_Shape> aFromFace = *anIt;
281 if(aSubShapes->isBound(aFromFace)) {
282 aFromFace = aSubShapes->find(aFromFace);
284 theResultBody->generated(aFromFace, aFromName, aFromTag++);
288 theResultBody->loadAndOrientModifiedShapes(theAlgo.makeShape().get(), theBaseShape, GeomAPI_Shape::FACE,
289 aModTag, aModName, *theAlgo.mapOfShapes().get());
290 theResultBody->loadDeletedShapes(theAlgo.makeShape().get(), theBaseShape, GeomAPI_Shape::FACE, aDelTag);
292 for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
293 theResultBody->loadAndOrientModifiedShapes(theAlgo.makeShape().get(), *anIter, GeomAPI_Shape::FACE,
294 aModTag, aModName, *theAlgo.mapOfShapes().get());
295 theResultBody->loadDeletedShapes(theAlgo.makeShape().get(), *anIter, GeomAPI_Shape::FACE, aDelTag);