1 // Copyright (C) 2014-2020 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_AttributeInteger.h>
27 #include <ModelAPI_AttributeSelectionList.h>
28 #include <ModelAPI_AttributeString.h>
29 #include <ModelAPI_Session.h>
30 #include <ModelAPI_Tools.h>
31 #include <ModelAPI_Validator.h>
33 #include <GeomAlgoAPI_Boolean.h>
34 #include <GeomAlgoAPI_MakeShapeList.h>
35 #include <GeomAlgoAPI_PaveFiller.h>
36 #include <GeomAlgoAPI_ShapeBuilder.h>
37 #include <GeomAlgoAPI_ShapeTools.h>
38 #include <GeomAlgoAPI_Tools.h>
39 #include <GeomAlgoAPI_UnifySameDomain.h>
41 #include <GeomAPI_ShapeExplorer.h>
42 #include <GeomAPI_ShapeIterator.h>
44 //==================================================================================================
45 FeaturesPlugin_BooleanFuse::FeaturesPlugin_BooleanFuse()
46 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_FUSE)
50 //==================================================================================================
51 void FeaturesPlugin_BooleanFuse::initAttributes()
53 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
55 data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
56 data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
58 data()->addAttribute(REMOVE_INTERSECTION_EDGES_ID(), ModelAPI_AttributeBoolean::typeId());
60 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), OBJECT_LIST_ID());
61 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TOOL_LIST_ID());
63 initVersion(BOP_VERSION_9_4(), selectionList(OBJECT_LIST_ID()), selectionList(TOOL_LIST_ID()));
66 //==================================================================================================
67 void FeaturesPlugin_BooleanFuse::execute()
70 GeomAPI_ShapeHierarchy anObjectsHierarchy, aToolsHierarchy;
73 bool isSimpleCreation = false;
75 AttributeStringPtr aCreationMethodAttr = string(CREATION_METHOD());
76 if (aCreationMethodAttr.get()
77 && aCreationMethodAttr->value() == CREATION_METHOD_SIMPLE())
79 isSimpleCreation = true;
83 if (!processAttribute(OBJECT_LIST_ID(), anObjectsHierarchy, aPlanes))
87 if (!isSimpleCreation &&
88 !processAttribute(TOOL_LIST_ID(), aToolsHierarchy, aPlanes))
91 ListOfShape anObjects, aTools, anEdgesAndFaces;
92 // all objects except edges and faces
93 anObjectsHierarchy.objectsByType(anEdgesAndFaces, anObjects,
94 GeomAPI_Shape::FACE, GeomAPI_Shape::EDGE);
95 aToolsHierarchy.objectsByType(anEdgesAndFaces, aTools,
96 GeomAPI_Shape::FACE, GeomAPI_Shape::EDGE);
98 if ((anObjects.size() + aTools.size() + anEdgesAndFaces.size()) < 2) {
99 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
100 setError(aFeatureError);
104 // version of FUSE feature
105 const std::string aFuseVersion = data()->version();
107 // Collecting all solids which will be fused.
108 ListOfShape aSolidsToFuse;
109 aSolidsToFuse.insert(aSolidsToFuse.end(), anObjects.begin(), anObjects.end());
110 aSolidsToFuse.insert(aSolidsToFuse.end(), aTools.begin(), aTools.end());
112 // Collecting solids from compsolids which will not be modified
113 // in boolean operation and will be added to result.
114 bool isProcessCompsolid = !isSimpleCreation || !aFuseVersion.empty();
115 ListOfShape aShapesToAdd;
116 int aNbCompsolids = 0; // number of compsolids, which subs is taken into operation
117 bool hasSeparateSolids = false; // are solids or full results exist
118 for (GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjectsHierarchy.begin();
119 isProcessCompsolid && anObjectsIt != anObjectsHierarchy.end();
121 GeomShapePtr anObject = *anObjectsIt;
122 GeomShapePtr aParent = anObjectsHierarchy.parent(anObject, false);
124 if (aParent && aParent->shapeType() == GeomAPI_Shape::COMPSOLID) {
126 // mark all subs of this parent as precessed to avoid handling twice
127 aParent = anObjectsHierarchy.parent(anObject);
129 ListOfShape aUsed, aNotUsed;
130 anObjectsHierarchy.splitCompound(aParent, aUsed, aNotUsed);
131 aShapesToAdd.insert(aShapesToAdd.end(), aNotUsed.begin(), aNotUsed.end());
134 hasSeparateSolids = true;
136 bool isSingleCompsolid = aNbCompsolids == 1 && !hasSeparateSolids;
138 ListOfShape anOriginalShapes = aSolidsToFuse;
139 anOriginalShapes.insert(anOriginalShapes.end(), aShapesToAdd.begin(), aShapesToAdd.end());
141 // Cut edges and faces(if we have any) with solids.
142 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
143 GeomShapePtr aCuttedEdgesAndFaces;
144 if (!anEdgesAndFaces.empty()) {
145 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(anEdgesAndFaces,
146 anOriginalShapes, GeomAlgoAPI_Tools::BOOL_CUT));
147 if (aCutAlgo->isDone()) {
148 aCuttedEdgesAndFaces = aCutAlgo->shape();
149 aMakeShapeList->appendAlgo(aCutAlgo);
153 if (aShapesToAdd.empty() || !aCuttedEdgesAndFaces) {
154 anOriginalShapes.insert(anOriginalShapes.end(), anEdgesAndFaces.begin(),
155 anEdgesAndFaces.end());
158 // If we have compsolids then cut with not used solids all others.
159 if (!aShapesToAdd.empty() && !isSingleCompsolid) {
160 aSolidsToFuse.clear();
161 for (ListOfShape::iterator
162 anIt = anOriginalShapes.begin(); anIt != anOriginalShapes.end(); anIt++) {
163 ListOfShape aOneObjectList;
164 aOneObjectList.push_back(*anIt);
165 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
166 new GeomAlgoAPI_Boolean(aOneObjectList, aShapesToAdd, GeomAlgoAPI_Tools::BOOL_CUT));
168 if (GeomAlgoAPI_ShapeTools::volume(aCutAlgo->shape()) > 1.e-27) {
169 aSolidsToFuse.push_back(aCutAlgo->shape());
170 aMakeShapeList->appendAlgo(aCutAlgo);
175 if (!aSolidsToFuse.empty()) {
177 anObjects.push_back(aSolidsToFuse.back());
178 aSolidsToFuse.pop_back();
179 aTools = aSolidsToFuse;
182 // Fuse all objects and all tools.
184 if (anObjects.size() == 1 && aTools.empty()) {
185 aShape = anObjects.front();
186 } else if (anObjects.empty() && aTools.size() == 1) {
187 aShape = aTools.front();
188 } else if ((anObjects.size() + aTools.size()) > 1) {
189 std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
191 GeomAlgoAPI_Tools::BOOL_FUSE));
193 // Checking that the algorithm worked properly.
194 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFuseAlgo, getKind(), anError)) {
199 aShape = aFuseAlgo->shape();
200 aMakeShapeList->appendAlgo(aFuseAlgo);
203 // Combine result with not used solids from compsolid and edges and faces (if we have any).
204 if (aCuttedEdgesAndFaces.get() && !aCuttedEdgesAndFaces->isNull()) {
205 aShapesToAdd.push_back(aCuttedEdgesAndFaces);
207 aShapesToAdd.insert(aShapesToAdd.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
209 if (!aShapesToAdd.empty()) {
211 aShapesToAdd.push_back(aShape);
213 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
214 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
215 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFillerAlgo, getKind(), anError)) {
220 aShape = aFillerAlgo->shape();
221 aMakeShapeList->appendAlgo(aFillerAlgo);
224 bool isRemoveEdges = false;
225 AttributeBooleanPtr removeEdgesAttr = boolean(REMOVE_INTERSECTION_EDGES_ID());
226 if (removeEdgesAttr.get()) {
227 isRemoveEdges = removeEdgesAttr->value();
231 std::shared_ptr<GeomAlgoAPI_UnifySameDomain> aUnifyAlgo(
232 new GeomAlgoAPI_UnifySameDomain(aShape));
234 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aUnifyAlgo, getKind(), anError)) {
239 aShape = aUnifyAlgo->shape();
240 aMakeShapeList->appendAlgo(aUnifyAlgo);
243 if (aFuseVersion == BOP_VERSION_9_4()) {
244 // merge hierarchies of compounds containing objects and tools
245 // and append the result of the FUSE operation
246 aShape = keepUnusedSubsOfCompound(aShape, anObjectsHierarchy, aToolsHierarchy, aMakeShapeList);
249 int aResultIndex = 0;
251 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
253 ListOfShape anEmptyTools;
254 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
259 setResult(aResultBody, aResultIndex);
262 FeaturesPlugin_Tools::loadDeletedShapes(aResultBody,
268 // remove the rest results if there were produced in the previous pass
269 removeResults(aResultIndex);