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_Prism.h>
16 #include <GeomAlgoAPI_Revolution.h>
17 #include <GeomAlgoAPI_ShapeTools.h>
19 //=================================================================================================
20 void FeaturesPlugin_CompositeBoolean::initAttributes()
22 data()->addAttribute(SKETCH_OBJECT_ID(), ModelAPI_AttributeReference::typeId());
24 // Boolean works with solids always.
25 data()->addAttribute(BOOLEAN_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
26 AttributeSelectionListPtr aSelection = data()->selectionList(BOOLEAN_OBJECTS_ID());
27 aSelection->setSelectionType("SOLID");
29 initMakeSolidsAttributes();
32 //=================================================================================================
33 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeBoolean::addFeature(std::string theID)
35 std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
37 data()->reference(SKETCH_OBJECT_ID())->setValue(aNew);
39 // set as current also after it becomes sub to set correctly enabled for other sketch subs
40 document()->setCurrentFeature(aNew, false);
44 //=================================================================================================
45 int FeaturesPlugin_CompositeBoolean::numberOfSubs(bool forTree) const
47 ObjectPtr aObj = data()->reference(SKETCH_OBJECT_ID())->value();
48 return aObj.get()? 1 : 0;
51 //=================================================================================================
52 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeBoolean::subFeature(const int theIndex, bool forTree) const
55 return std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_OBJECT_ID())->value());
56 return std::shared_ptr<ModelAPI_Feature>();
59 //=================================================================================================
60 int FeaturesPlugin_CompositeBoolean::subFeatureId(const int theIndex) const
62 std::shared_ptr<ModelAPI_Feature> aFeature = subFeature(theIndex);
64 return aFeature->data()->featureId();
68 //=================================================================================================
69 bool FeaturesPlugin_CompositeBoolean::isSub(ObjectPtr theObject) const
71 // check is this feature of result
72 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
76 ObjectPtr aSub = data()->reference(SKETCH_OBJECT_ID())->value();
77 return aSub == theObject;
80 //=================================================================================================
81 void FeaturesPlugin_CompositeBoolean::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
85 //=================================================================================================
86 void FeaturesPlugin_CompositeBoolean::erase()
88 if (data().get() && data()->isValid()) { // on abort of sketch of this composite it may be invalid
90 std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_OBJECT_ID())->value());
91 if (aSketch.get() && aSketch->data()->isValid()) {
92 document()->removeFeature(aSketch);
95 ModelAPI_CompositeFeature::erase();
99 //=================================================================================================
100 void FeaturesPlugin_CompositeBoolean::execute()
102 // Getting faces to create solids.
103 std::shared_ptr<ModelAPI_Feature> aSketchFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(
104 reference(SKETCH_OBJECT_ID())->value());
105 if(!aSketchFeature) {
108 ResultPtr aSketchRes = aSketchFeature->results().front();
109 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
110 if(!aConstruction.get()) {
113 int aSketchFacesNum = aConstruction->facesNum();
114 if(aSketchFacesNum == 0) {
115 return; //TODO: set error message
117 ListOfShape aSketchFacesList;
118 for(int aFaceIndex = 0; aFaceIndex < aSketchFacesNum; aFaceIndex++) {
119 std::shared_ptr<GeomAPI_Shape> aFace = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
120 aSketchFacesList.push_back(aFace);
123 // Pass faces to soldis creation function.
124 ListOfShape aBooleanTools;
125 std::list<std::shared_ptr<GeomAPI_Interface>> theSolidsAlgos;
126 makeSolids(aSketchFacesList, aBooleanTools, theSolidsAlgos);
127 if(aBooleanTools.empty()) {
131 // Getting objects for boolean operation.
132 ListOfShape aBooleanObjects;
133 AttributeSelectionListPtr anObjectsSelList = selectionList(BOOLEAN_OBJECTS_ID());
134 if (anObjectsSelList->size() == 0) {
137 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
138 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
139 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
140 if(!anObject.get()) {
143 aBooleanObjects.push_back(anObject);
146 // Cut from each object solids.
147 int aResultIndex = 0;
149 switch(myBooleanOperationType) {
150 case GeomAlgoAPI_Boolean::BOOL_CUT:
151 case GeomAlgoAPI_Boolean::BOOL_COMMON:{
152 // Cut each object with all tools
153 for(ListOfShape::iterator anObjectsIt = aBooleanObjects.begin(); anObjectsIt != aBooleanObjects.end(); anObjectsIt++) {
154 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
155 ListOfShape aListWithObject;
156 aListWithObject.push_back(anObject);
157 GeomAlgoAPI_Boolean aBoolAlgo(aListWithObject, aBooleanTools, myBooleanOperationType);
159 // Checking that the algorithm worked properly.
160 if(!aBoolAlgo.isDone() || aBoolAlgo.shape()->isNull() || !aBoolAlgo.isValid()) {
161 setError("Boolean algorithm failed");
165 if(GeomAlgoAPI_ShapeTools::volume(aBoolAlgo.shape()) > 1.e-7) {
166 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
167 loadNamingDS(aResultBody, anObject, aSketchFacesList, theSolidsAlgos, aBooleanTools, aBoolAlgo);
168 setResult(aResultBody, aResultIndex);
174 case GeomAlgoAPI_Boolean::BOOL_FUSE: {
175 // Fuse all objects and all tools.
176 GeomAlgoAPI_Boolean aBoolAlgo(aBooleanObjects, aBooleanTools, myBooleanOperationType);
178 // Checking that the algorithm worked properly.
179 if(!aBoolAlgo.isDone() || aBoolAlgo.shape()->isNull() || !aBoolAlgo.isValid()) {
180 setError("Boolean algorithm failed");
184 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
185 loadNamingDS(aResultBody, aBooleanObjects.front(), aSketchFacesList, theSolidsAlgos, aBooleanTools, aBoolAlgo);
186 setResult(aResultBody, aResultIndex);
191 setError("Error: wrong type of boolean operation");
196 // Remove the rest results if there were produced in the previous pass.
197 removeResults(aResultIndex);
200 //=================================================================================================
201 void FeaturesPlugin_CompositeBoolean::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
202 const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
203 const ListOfShape& theFaces,
204 const std::list<std::shared_ptr<GeomAPI_Interface>>& theSolidsAlgos,
205 const ListOfShape& theTools,
206 const GeomAlgoAPI_Boolean& theAlgo)
209 if(theBaseShape->isEqual(theAlgo.shape())) {
210 theResultBody->store(theAlgo.shape());
212 const int aGenTag = 1;
213 const int aFrTag = 2;
214 const int aToTag = 3;
215 const int aModTag = 4;
216 const int aDelTag = 5;
217 const int aSubsolidsTag=6; /// sub solids will be placed at labels 6, 7, etc. if result is compound of solids
218 const std::string aGenName = "Generated";
219 const std::string aModName = "Modified";
220 const std::string aLatName = "LateralFace";
221 const std::string aFrName = "FromFace";
222 const std::string aToName = "ToFace";
224 theResultBody->storeModified(theBaseShape, theAlgo.shape(), aSubsolidsTag);
226 ListOfShape::const_iterator aFaceIter = theFaces.begin();
227 std::list<std::shared_ptr<GeomAPI_Interface>>::const_iterator aSolidsAlgosIter = theSolidsAlgos.begin();
228 for(; aFaceIter != theFaces.end() && aSolidsAlgosIter != theSolidsAlgos.end(); aFaceIter++, aSolidsAlgosIter++) {
229 std::shared_ptr<GeomAPI_Shape> aFromFace;
230 std::shared_ptr<GeomAPI_Shape> aToFace;
231 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes;
233 //Insert lateral face : Face from Edge
234 if(std::dynamic_pointer_cast<GeomAlgoAPI_Prism>(*aSolidsAlgosIter)) {
235 std::shared_ptr<GeomAlgoAPI_Prism> aPrismAlgo = std::dynamic_pointer_cast<GeomAlgoAPI_Prism>(*aSolidsAlgosIter);
236 aSubShapes = aPrismAlgo->mapOfShapes();
237 theResultBody->loadAndOrientGeneratedShapes(aPrismAlgo->makeShape().get(), *aFaceIter, GeomAPI_Shape::EDGE, aGenTag,
238 aLatName, *aSubShapes.get());
240 //aFromFace = aPrismAlgo->firstShape();
241 //aToFace = aPrismAlgo->lastShape();
242 } else if(std::dynamic_pointer_cast<GeomAlgoAPI_Revolution>(*aSolidsAlgosIter)) {
243 std::shared_ptr<GeomAlgoAPI_Revolution> aRevolAlgo = std::dynamic_pointer_cast<GeomAlgoAPI_Revolution>(*aSolidsAlgosIter);
244 aSubShapes = aRevolAlgo->mapOfShapes();
245 theResultBody->loadAndOrientGeneratedShapes(aRevolAlgo->makeShape().get(), *aFaceIter, GeomAPI_Shape::EDGE, aGenTag,
246 aLatName, *aSubShapes.get());
248 //aFromFace = aRevolAlgo->firstShape();
249 //aToFace = aRevolAlgo->lastShape();
254 if(!aFromFace->isNull()) {
255 if(aSubShapes->isBound(aFromFace)) {
256 aFromFace = aSubShapes->find(aFromFace);
258 theResultBody->generated(aFromFace, aFrName, aFrTag);
262 if (!aToFace->isNull()) {
263 if (aSubShapes->isBound(aToFace)) {
264 aToFace = aSubShapes->find(aToFace);
266 theResultBody->generated(aToFace, aToName, aToTag);
271 theResultBody->loadAndOrientModifiedShapes(theAlgo.makeShape().get(), theBaseShape, GeomAPI_Shape::FACE,
272 aModTag, aModName, *theAlgo.mapOfShapes().get());
273 theResultBody->loadDeletedShapes(theAlgo.makeShape().get(), theBaseShape, GeomAPI_Shape::FACE, aDelTag);
275 for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
276 theResultBody->loadAndOrientModifiedShapes(theAlgo.makeShape().get(), *anIter, GeomAPI_Shape::FACE,
277 aModTag, aModName, *theAlgo.mapOfShapes().get());
278 theResultBody->loadDeletedShapes(theAlgo.makeShape().get(), *anIter, GeomAPI_Shape::FACE, aDelTag);