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)
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
65 std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_OBJECT_ID())->value());
67 return aFeature->data()->featureId();
72 //=================================================================================================
73 bool FeaturesPlugin_CompositeBoolean::isSub(ObjectPtr theObject) const
75 // check is this feature of result
76 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
80 ObjectPtr aSub = data()->reference(SKETCH_OBJECT_ID())->value();
81 return aSub == theObject;
84 //=================================================================================================
85 void FeaturesPlugin_CompositeBoolean::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
89 //=================================================================================================
90 void FeaturesPlugin_CompositeBoolean::erase()
92 if (data().get() && data()->isValid()) { // on abort of sketch of this composite it may be invalid
94 std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_OBJECT_ID())->value());
95 if (aSketch.get() && aSketch->data()->isValid()) {
96 document()->removeFeature(aSketch);
99 ModelAPI_CompositeFeature::erase();
103 //=================================================================================================
104 void FeaturesPlugin_CompositeBoolean::execute()
106 // Getting faces to create solids.
107 std::shared_ptr<ModelAPI_Feature> aSketchFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(
108 reference(SKETCH_OBJECT_ID())->value());
109 if(!aSketchFeature || aSketchFeature->results().empty()) {
112 ResultPtr aSketchRes = aSketchFeature->results().front();
113 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
114 if(!aConstruction.get()) {
117 int aSketchFacesNum = aConstruction->facesNum();
118 if(aSketchFacesNum == 0) {
121 ListOfShape aFacesList;
122 for(int aFaceIndex = 0; aFaceIndex < aSketchFacesNum; aFaceIndex++) {
123 std::shared_ptr<GeomAPI_Shape> aFace = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
124 aFacesList.push_back(aFace);
127 // Searching faces with common edges.
129 ListOfShape aFreeFaces;
130 std::shared_ptr<GeomAPI_Shape> aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aFacesList);
131 GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL, aShells, aFreeFaces);
132 aShells.insert(aShells.end(), aFreeFaces.begin(), aFreeFaces.end());
134 // Pass shells/faces to solids creation function.
135 ListOfShape aBooleanTools;
136 std::list<std::shared_ptr<GeomAPI_Interface>> aSolidsAlgos;
137 makeSolids(aShells, aBooleanTools, aSolidsAlgos);
138 if(aBooleanTools.empty()) {
142 // Getting objects for boolean operation.
143 ListOfShape aBooleanObjects;
144 AttributeSelectionListPtr anObjectsSelList = selectionList(BOOLEAN_OBJECTS_ID());
145 if (anObjectsSelList->size() == 0) {
148 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
149 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
150 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
151 if(!anObject.get()) {
154 aBooleanObjects.push_back(anObject);
157 // Cut from each object solids.
158 int aResultIndex = 0;
160 switch(myBooleanOperationType) {
161 case GeomAlgoAPI_Boolean::BOOL_CUT:
162 case GeomAlgoAPI_Boolean::BOOL_COMMON:{
163 // Cut each object with all tools
164 for(ListOfShape::iterator anObjectsIt = aBooleanObjects.begin(); anObjectsIt != aBooleanObjects.end(); anObjectsIt++) {
165 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
166 ListOfShape aListWithObject;
167 aListWithObject.push_back(anObject);
168 GeomAlgoAPI_Boolean aBoolAlgo(aListWithObject, aBooleanTools, myBooleanOperationType);
170 // Checking that the algorithm worked properly.
171 if(!aBoolAlgo.isDone() || aBoolAlgo.shape()->isNull() || !aBoolAlgo.isValid()) {
172 setError("Boolean algorithm failed");
176 if(GeomAlgoAPI_ShapeTools::volume(aBoolAlgo.shape()) > 1.e-7) {
177 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
178 loadNamingDS(aResultBody, anObject, aShells, aSolidsAlgos, aBooleanTools, aBoolAlgo);
179 setResult(aResultBody, aResultIndex);
185 case GeomAlgoAPI_Boolean::BOOL_FUSE: {
186 // Fuse all objects and all tools.
187 GeomAlgoAPI_Boolean aBoolAlgo(aBooleanObjects, aBooleanTools, myBooleanOperationType);
189 // Checking that the algorithm worked properly.
190 if(!aBoolAlgo.isDone() || aBoolAlgo.shape()->isNull() || !aBoolAlgo.isValid()) {
191 setError("Boolean algorithm failed");
195 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
196 loadNamingDS(aResultBody, aBooleanObjects.front(), aShells, aSolidsAlgos, aBooleanTools, aBoolAlgo);
197 setResult(aResultBody, aResultIndex);
202 setError("Error: wrong type of boolean operation");
207 // Remove the rest results if there were produced in the previous pass.
208 removeResults(aResultIndex);
211 //=================================================================================================
212 void FeaturesPlugin_CompositeBoolean::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
213 const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
214 const ListOfShape& theShells,
215 const std::list<std::shared_ptr<GeomAPI_Interface>>& theSolidsAlgos,
216 const ListOfShape& theTools,
217 const GeomAlgoAPI_Boolean& theAlgo)
220 if(theBaseShape->isEqual(theAlgo.shape())) {
221 theResultBody->store(theAlgo.shape());
223 const int aGenTag = 1;
224 const int aModTag = 2;
225 const int aDelTag = 3;
226 const int aSubsolidsTag=4; /// sub solids will be placed at labels 6, 7, etc. if result is compound of solids
227 int aToTag = 5; // may be many labels, starting from this index
228 int aFromTag = 10000; // may be many labels, starting from this index or last aToTag index
229 const std::string aGenName = "Generated";
230 const std::string aModName = "Modified";
231 const std::string aLatName = "LateralFace";
232 const std::string aFromName = "FromFace";
233 const std::string aToName = "ToFace";
235 theResultBody->storeModified(theBaseShape, theAlgo.shape(), aSubsolidsTag);
237 ListOfShape::const_iterator aShellsIter = theShells.begin();
238 std::list<std::shared_ptr<GeomAPI_Interface>>::const_iterator aSolidsAlgosIter = theSolidsAlgos.begin();
239 for(; aShellsIter != theShells.end() && aSolidsAlgosIter != theSolidsAlgos.end(); aShellsIter++, aSolidsAlgosIter++) {
240 ListOfShape aFromFaces;
241 ListOfShape aToFaces;
242 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes;
244 //Insert lateral face : Face from Edge
245 if(std::dynamic_pointer_cast<GeomAlgoAPI_Prism>(*aSolidsAlgosIter)) {
246 std::shared_ptr<GeomAlgoAPI_Prism> aPrismAlgo = std::dynamic_pointer_cast<GeomAlgoAPI_Prism>(*aSolidsAlgosIter);
247 aSubShapes = aPrismAlgo->mapOfShapes();
248 theResultBody->loadAndOrientGeneratedShapes(aPrismAlgo->makeShape().get(), *aShellsIter, GeomAPI_Shape::EDGE, aGenTag,
249 aLatName, *aSubShapes.get());
251 aFromFaces = aPrismAlgo->fromFaces();
252 aToFaces = aPrismAlgo->toFaces();
253 } else if(std::dynamic_pointer_cast<GeomAlgoAPI_Revolution>(*aSolidsAlgosIter)) {
254 std::shared_ptr<GeomAlgoAPI_Revolution> aRevolAlgo = std::dynamic_pointer_cast<GeomAlgoAPI_Revolution>(*aSolidsAlgosIter);
255 aSubShapes = aRevolAlgo->mapOfShapes();
256 theResultBody->loadAndOrientGeneratedShapes(aRevolAlgo->makeShape().get(), *aShellsIter, GeomAPI_Shape::EDGE, aGenTag,
257 aLatName, *aSubShapes.get());
258 aFromFaces = aRevolAlgo->fromFaces();
259 aToFaces = aRevolAlgo->toFaces();
263 for(ListOfShape::const_iterator anIt = aToFaces.cbegin(); anIt != aToFaces.cend(); anIt++) {
264 std::shared_ptr<GeomAPI_Shape> aToFace = *anIt;
265 if(aSubShapes->isBound(aToFace)) {
266 aToFace = aSubShapes->find(aToFace);
268 theResultBody->generated(aToFace, aToName, aToTag++);
272 if (aFromTag < aToTag) aFromTag = aToTag;
273 for(ListOfShape::const_iterator anIt = aFromFaces.cbegin(); anIt != aFromFaces.cend(); anIt++) {
274 std::shared_ptr<GeomAPI_Shape> aFromFace = *anIt;
275 if(aSubShapes->isBound(aFromFace)) {
276 aFromFace = aSubShapes->find(aFromFace);
278 theResultBody->generated(aFromFace, aFromName, aFromTag++);
282 theResultBody->loadAndOrientModifiedShapes(theAlgo.makeShape().get(), theBaseShape, GeomAPI_Shape::FACE,
283 aModTag, aModName, *theAlgo.mapOfShapes().get());
284 theResultBody->loadDeletedShapes(theAlgo.makeShape().get(), theBaseShape, GeomAPI_Shape::FACE, aDelTag);
286 for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
287 theResultBody->loadAndOrientModifiedShapes(theAlgo.makeShape().get(), *anIter, GeomAPI_Shape::FACE,
288 aModTag, aModName, *theAlgo.mapOfShapes().get());
289 theResultBody->loadDeletedShapes(theAlgo.makeShape().get(), *anIter, GeomAPI_Shape::FACE, aDelTag);