1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "FeaturesPlugin_BooleanFuse.h"
22 #include "FeaturesPlugin_Tools.h"
24 #include <ModelAPI_ResultBody.h>
25 #include <ModelAPI_AttributeBoolean.h>
26 #include <ModelAPI_AttributeSelectionList.h>
27 #include <ModelAPI_AttributeString.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_Tools.h>
30 #include <ModelAPI_Validator.h>
32 #include <GeomAlgoAPI_Boolean.h>
33 #include <GeomAlgoAPI_MakeShapeList.h>
34 #include <GeomAlgoAPI_PaveFiller.h>
35 #include <GeomAlgoAPI_ShapeTools.h>
36 #include <GeomAlgoAPI_Tools.h>
37 #include <GeomAlgoAPI_UnifySameDomain.h>
38 #include <GeomAPI_ShapeExplorer.h>
40 //==================================================================================================
41 FeaturesPlugin_BooleanFuse::FeaturesPlugin_BooleanFuse()
42 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_FUSE)
46 //==================================================================================================
47 void FeaturesPlugin_BooleanFuse::initAttributes()
49 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
51 data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
52 data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
54 data()->addAttribute(REMOVE_INTERSECTION_EDGES_ID(), ModelAPI_AttributeBoolean::typeId());
56 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), OBJECT_LIST_ID());
57 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TOOL_LIST_ID());
60 //==================================================================================================
61 void FeaturesPlugin_BooleanFuse::execute()
64 ObjectHierarchy anObjectsHierarchy, aToolsHierarchy;
65 ListOfShape aPlanes, anEdgesAndFaces;
67 bool isSimpleCreation = false;
69 AttributeStringPtr aCreationMethodAttr = string(CREATION_METHOD());
70 if (aCreationMethodAttr.get()
71 && aCreationMethodAttr->value() == CREATION_METHOD_SIMPLE())
73 isSimpleCreation = true;
77 if (!processAttribute(OBJECT_LIST_ID(), anObjectsHierarchy, aPlanes, anEdgesAndFaces))
81 if (!isSimpleCreation &&
82 !processAttribute(TOOL_LIST_ID(), aToolsHierarchy, aPlanes, anEdgesAndFaces))
85 ListOfShape anObjects = anObjectsHierarchy.Objects();
86 ListOfShape aTools = aToolsHierarchy.Objects();
88 if ((anObjects.size() + aTools.size() + anEdgesAndFaces.size()) < 2) {
89 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
90 setError(aFeatureError);
94 // Collecting all solids which will be fused.
95 ListOfShape aSolidsToFuse;
96 aSolidsToFuse.insert(aSolidsToFuse.end(), anObjects.begin(), anObjects.end());
97 aSolidsToFuse.insert(aSolidsToFuse.end(), aTools.begin(), aTools.end());
99 // Collecting solids from compsolids which will not be modified
100 // in boolean operation and will be added to result.
101 ListOfShape aShapesToAdd;
102 for (ObjectHierarchy::Iterator anObjectsIt = anObjectsHierarchy.Begin();
103 !isSimpleCreation && anObjectsIt != anObjectsHierarchy.End();
105 GeomShapePtr anObject = *anObjectsIt;
106 GeomShapePtr aParent = anObjectsHierarchy.Parent(anObject, false);
108 if (aParent && aParent->shapeType() == GeomAPI_Shape::COMPSOLID) {
109 ListOfShape aUsed, aNotUsed;
110 anObjectsHierarchy.SplitCompound(aParent, aUsed, aNotUsed);
111 aShapesToAdd.insert(aShapesToAdd.end(), aNotUsed.begin(), aNotUsed.end());
115 ListOfShape anOriginalShapes = aSolidsToFuse;
116 anOriginalShapes.insert(anOriginalShapes.end(), aShapesToAdd.begin(), aShapesToAdd.end());
118 // Cut edges and faces(if we have any) with solids.
119 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
120 GeomShapePtr aCuttedEdgesAndFaces;
121 if (!anEdgesAndFaces.empty()) {
122 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(anEdgesAndFaces,
123 anOriginalShapes, GeomAlgoAPI_Tools::BOOL_CUT));
124 if (aCutAlgo->isDone()) {
125 aCuttedEdgesAndFaces = aCutAlgo->shape();
126 aMakeShapeList->appendAlgo(aCutAlgo);
130 // If we have compsolids then cut with not used solids all others.
131 if (!aShapesToAdd.empty()) {
132 aSolidsToFuse.clear();
133 for (ListOfShape::iterator
134 anIt = anOriginalShapes.begin(); anIt != anOriginalShapes.end(); anIt++) {
135 ListOfShape aOneObjectList;
136 aOneObjectList.push_back(*anIt);
137 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
138 new GeomAlgoAPI_Boolean(aOneObjectList, aShapesToAdd, GeomAlgoAPI_Tools::BOOL_CUT));
140 if (GeomAlgoAPI_ShapeTools::volume(aCutAlgo->shape()) > 1.e-27) {
141 aSolidsToFuse.push_back(aCutAlgo->shape());
142 aMakeShapeList->appendAlgo(aCutAlgo);
147 anOriginalShapes.insert(anOriginalShapes.end(), anEdgesAndFaces.begin(),
148 anEdgesAndFaces.end());
150 if (!aSolidsToFuse.empty()) {
152 anObjects.push_back(aSolidsToFuse.back());
153 aSolidsToFuse.pop_back();
154 aTools = aSolidsToFuse;
157 // Fuse all objects and all tools.
159 if (anObjects.size() == 1 && aTools.empty()) {
160 aShape = anObjects.front();
161 } else if (anObjects.empty() && aTools.size() == 1) {
162 aShape = aTools.front();
163 } else if ((anObjects.size() + aTools.size()) > 1) {
164 std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
166 GeomAlgoAPI_Tools::BOOL_FUSE));
168 // Checking that the algorithm worked properly.
169 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFuseAlgo, getKind(), anError)) {
174 aShape = aFuseAlgo->shape();
175 aMakeShapeList->appendAlgo(aFuseAlgo);
178 // Combine result with not used solids from compsolid and edges and faces (if we have any).
179 if (aCuttedEdgesAndFaces.get() && !aCuttedEdgesAndFaces->isNull()) {
180 aShapesToAdd.push_back(aCuttedEdgesAndFaces);
182 aShapesToAdd.insert(aShapesToAdd.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
184 if (!aShapesToAdd.empty()) {
186 aShapesToAdd.push_back(aShape);
188 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
189 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
190 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFillerAlgo, getKind(), anError)) {
195 aShape = aFillerAlgo->shape();
196 aMakeShapeList->appendAlgo(aFillerAlgo);
199 bool isRemoveEdges = false;
200 AttributeBooleanPtr removeEdgesAttr = boolean(REMOVE_INTERSECTION_EDGES_ID());
201 if (removeEdgesAttr.get()) {
202 isRemoveEdges = removeEdgesAttr->value();
206 std::shared_ptr<GeomAlgoAPI_UnifySameDomain> aUnifyAlgo(
207 new GeomAlgoAPI_UnifySameDomain(aShape));
209 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aUnifyAlgo, getKind(), anError)) {
214 aShape = aUnifyAlgo->shape();
215 aMakeShapeList->appendAlgo(aUnifyAlgo);
218 int aResultIndex = 0;
220 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
222 ListOfShape anEmptyTools;
223 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
228 setResult(aResultBody, aResultIndex);
231 FeaturesPlugin_Tools::loadDeletedShapes(aResultBody,
237 // remove the rest results if there were produced in the previous pass
238 removeResults(aResultIndex);