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_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 static const int THE_FUSE_VERSION_1 = 20190506;
46 //==================================================================================================
47 FeaturesPlugin_BooleanFuse::FeaturesPlugin_BooleanFuse()
48 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_FUSE)
52 //==================================================================================================
53 void FeaturesPlugin_BooleanFuse::initAttributes()
55 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
57 data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
58 data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
60 data()->addAttribute(REMOVE_INTERSECTION_EDGES_ID(), ModelAPI_AttributeBoolean::typeId());
62 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), OBJECT_LIST_ID());
63 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TOOL_LIST_ID());
65 initVersion(THE_FUSE_VERSION_1, selectionList(OBJECT_LIST_ID()), selectionList(TOOL_LIST_ID()));
68 //==================================================================================================
69 void FeaturesPlugin_BooleanFuse::execute()
72 ObjectHierarchy anObjectsHierarchy, aToolsHierarchy;
75 bool isSimpleCreation = false;
77 AttributeStringPtr aCreationMethodAttr = string(CREATION_METHOD());
78 if (aCreationMethodAttr.get()
79 && aCreationMethodAttr->value() == CREATION_METHOD_SIMPLE())
81 isSimpleCreation = true;
85 if (!processAttribute(OBJECT_LIST_ID(), anObjectsHierarchy, aPlanes))
89 if (!isSimpleCreation &&
90 !processAttribute(TOOL_LIST_ID(), aToolsHierarchy, aPlanes))
93 ListOfShape anObjects, aTools, anEdgesAndFaces;
94 // all objects except edges and faces
95 anObjectsHierarchy.ObjectsByType(anEdgesAndFaces, anObjects,
96 GeomAPI_Shape::FACE, GeomAPI_Shape::EDGE);
97 aToolsHierarchy.ObjectsByType(anEdgesAndFaces, aTools,
98 GeomAPI_Shape::FACE, GeomAPI_Shape::EDGE);
100 if ((anObjects.size() + aTools.size() + anEdgesAndFaces.size()) < 2) {
101 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
102 setError(aFeatureError);
106 // version of FUSE feature
107 int aFuseVersion = version();
109 // Collecting all solids which will be fused.
110 ListOfShape aSolidsToFuse;
111 aSolidsToFuse.insert(aSolidsToFuse.end(), anObjects.begin(), anObjects.end());
112 aSolidsToFuse.insert(aSolidsToFuse.end(), aTools.begin(), aTools.end());
114 // Collecting solids from compsolids which will not be modified
115 // in boolean operation and will be added to result.
116 ListOfShape aShapesToAdd;
117 for (ObjectHierarchy::Iterator anObjectsIt = anObjectsHierarchy.Begin();
118 anObjectsIt != anObjectsHierarchy.End(); ++anObjectsIt) {
119 GeomShapePtr anObject = *anObjectsIt;
120 GeomShapePtr aParent = anObjectsHierarchy.Parent(anObject, false);
122 if (aParent && aParent->shapeType() == GeomAPI_Shape::COMPSOLID) {
123 // mark all subs of this parent as precessed to avoid handling twice
124 aParent = anObjectsHierarchy.Parent(anObject);
126 ListOfShape aUsed, aNotUsed;
127 anObjectsHierarchy.SplitCompound(aParent, aUsed, aNotUsed);
128 aShapesToAdd.insert(aShapesToAdd.end(), aNotUsed.begin(), aNotUsed.end());
132 ListOfShape anOriginalShapes = aSolidsToFuse;
133 anOriginalShapes.insert(anOriginalShapes.end(), aShapesToAdd.begin(), aShapesToAdd.end());
135 // Cut edges and faces(if we have any) with solids.
136 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
137 GeomShapePtr aCuttedEdgesAndFaces;
138 if (!anEdgesAndFaces.empty()) {
139 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(anEdgesAndFaces,
140 anOriginalShapes, GeomAlgoAPI_Tools::BOOL_CUT));
141 if (aCutAlgo->isDone()) {
142 aCuttedEdgesAndFaces = aCutAlgo->shape();
143 aMakeShapeList->appendAlgo(aCutAlgo);
146 anOriginalShapes.insert(anOriginalShapes.end(), anEdgesAndFaces.begin(),
147 anEdgesAndFaces.end());
149 // If we have compsolids then cut with not used solids all others.
150 if (!aShapesToAdd.empty()) {
151 aSolidsToFuse.clear();
152 for (ListOfShape::iterator
153 anIt = anOriginalShapes.begin(); anIt != anOriginalShapes.end(); anIt++) {
154 ListOfShape aOneObjectList;
155 aOneObjectList.push_back(*anIt);
156 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
157 new GeomAlgoAPI_Boolean(aOneObjectList, aShapesToAdd, GeomAlgoAPI_Tools::BOOL_CUT));
159 if (GeomAlgoAPI_ShapeTools::volume(aCutAlgo->shape()) > 1.e-27) {
160 aSolidsToFuse.push_back(aCutAlgo->shape());
161 aMakeShapeList->appendAlgo(aCutAlgo);
166 if (!aSolidsToFuse.empty()) {
168 anObjects.push_back(aSolidsToFuse.back());
169 aSolidsToFuse.pop_back();
170 aTools = aSolidsToFuse;
173 // Fuse all objects and all tools.
175 if (anObjects.size() == 1 && aTools.empty()) {
176 aShape = anObjects.front();
177 } else if (anObjects.empty() && aTools.size() == 1) {
178 aShape = aTools.front();
179 } else if ((anObjects.size() + aTools.size()) > 1) {
180 std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
182 GeomAlgoAPI_Tools::BOOL_FUSE));
184 // Checking that the algorithm worked properly.
185 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFuseAlgo, getKind(), anError)) {
190 aShape = aFuseAlgo->shape();
191 aMakeShapeList->appendAlgo(aFuseAlgo);
194 // Combine result with not used solids from compsolid and edges and faces (if we have any).
195 if (aCuttedEdgesAndFaces.get() && !aCuttedEdgesAndFaces->isNull()) {
196 aShapesToAdd.push_back(aCuttedEdgesAndFaces);
198 aShapesToAdd.insert(aShapesToAdd.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
200 if (!aShapesToAdd.empty()) {
202 aShapesToAdd.push_back(aShape);
204 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
205 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
206 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFillerAlgo, getKind(), anError)) {
211 aShape = aFillerAlgo->shape();
212 aMakeShapeList->appendAlgo(aFillerAlgo);
215 bool isRemoveEdges = false;
216 AttributeBooleanPtr removeEdgesAttr = boolean(REMOVE_INTERSECTION_EDGES_ID());
217 if (removeEdgesAttr.get()) {
218 isRemoveEdges = removeEdgesAttr->value();
222 std::shared_ptr<GeomAlgoAPI_UnifySameDomain> aUnifyAlgo(
223 new GeomAlgoAPI_UnifySameDomain(aShape));
225 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aUnifyAlgo, getKind(), anError)) {
230 aShape = aUnifyAlgo->shape();
231 aMakeShapeList->appendAlgo(aUnifyAlgo);
234 if (aFuseVersion == THE_FUSE_VERSION_1) {
235 // merge hierarchies of compounds containing objects and tools
236 // and append the result of the FUSE operation
237 aShape = keepUnusedSubsOfCompound(aShape, anObjectsHierarchy, aToolsHierarchy, aMakeShapeList);
240 int aResultIndex = 0;
242 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
244 ListOfShape anEmptyTools;
245 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
250 setResult(aResultBody, aResultIndex);
253 FeaturesPlugin_Tools::loadDeletedShapes(aResultBody,
259 // remove the rest results if there were produced in the previous pass
260 removeResults(aResultIndex);