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 AttributePtr aVerAttr = data()->addAttribute(VERSION_ID(), ModelAPI_AttributeInteger::typeId());
66 aVerAttr->setIsArgument(false);
67 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), VERSION_ID());
68 if (!integer(VERSION_ID())->isInitialized() &&
69 !selectionList(OBJECT_LIST_ID())->isInitialized() &&
70 !selectionList(TOOL_LIST_ID())->isInitialized()) {
71 // this is a newly created feature (not read from file),
72 // so, initialize the latest version
73 integer(VERSION_ID())->setValue(THE_FUSE_VERSION_1);
77 //==================================================================================================
78 void FeaturesPlugin_BooleanFuse::execute()
81 ObjectHierarchy anObjectsHierarchy, aToolsHierarchy;
84 bool isSimpleCreation = false;
86 AttributeStringPtr aCreationMethodAttr = string(CREATION_METHOD());
87 if (aCreationMethodAttr.get()
88 && aCreationMethodAttr->value() == CREATION_METHOD_SIMPLE())
90 isSimpleCreation = true;
94 if (!processAttribute(OBJECT_LIST_ID(), anObjectsHierarchy, aPlanes))
98 if (!isSimpleCreation &&
99 !processAttribute(TOOL_LIST_ID(), aToolsHierarchy, aPlanes))
102 ListOfShape anObjects, aTools, anEdgesAndFaces;
103 // all objects except edges and faces
104 anObjectsHierarchy.ObjectsByType(anEdgesAndFaces, anObjects,
105 GeomAPI_Shape::FACE, GeomAPI_Shape::EDGE);
106 aToolsHierarchy.ObjectsByType(anEdgesAndFaces, aTools,
107 GeomAPI_Shape::FACE, GeomAPI_Shape::EDGE);
109 if ((anObjects.size() + aTools.size() + anEdgesAndFaces.size()) < 2) {
110 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
111 setError(aFeatureError);
115 // version of FUSE feature
116 int aFuseVersion = version();
118 // Collecting all solids which will be fused.
119 ListOfShape aSolidsToFuse;
120 aSolidsToFuse.insert(aSolidsToFuse.end(), anObjects.begin(), anObjects.end());
121 aSolidsToFuse.insert(aSolidsToFuse.end(), aTools.begin(), aTools.end());
123 // Collecting solids from compsolids which will not be modified
124 // in boolean operation and will be added to result.
125 ListOfShape aShapesToAdd;
126 for (ObjectHierarchy::Iterator anObjectsIt = anObjectsHierarchy.Begin();
127 !isSimpleCreation && anObjectsIt != anObjectsHierarchy.End();
129 GeomShapePtr anObject = *anObjectsIt;
130 GeomShapePtr aParent = anObjectsHierarchy.Parent(anObject, false);
132 if (aParent && aParent->shapeType() == GeomAPI_Shape::COMPSOLID) {
133 // mark all subs of this parent as precessed to avoid handling twice
134 aParent = anObjectsHierarchy.Parent(anObject);
136 ListOfShape aUsed, aNotUsed;
137 anObjectsHierarchy.SplitCompound(aParent, aUsed, aNotUsed);
138 aShapesToAdd.insert(aShapesToAdd.end(), aNotUsed.begin(), aNotUsed.end());
142 ListOfShape anOriginalShapes = aSolidsToFuse;
143 anOriginalShapes.insert(anOriginalShapes.end(), aShapesToAdd.begin(), aShapesToAdd.end());
145 // Cut edges and faces(if we have any) with solids.
146 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
147 GeomShapePtr aCuttedEdgesAndFaces;
148 if (!anEdgesAndFaces.empty()) {
149 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(anEdgesAndFaces,
150 anOriginalShapes, GeomAlgoAPI_Tools::BOOL_CUT));
151 if (aCutAlgo->isDone()) {
152 aCuttedEdgesAndFaces = aCutAlgo->shape();
153 aMakeShapeList->appendAlgo(aCutAlgo);
156 anOriginalShapes.insert(anOriginalShapes.end(), anEdgesAndFaces.begin(),
157 anEdgesAndFaces.end());
159 // If we have compsolids then cut with not used solids all others.
160 if (!aShapesToAdd.empty()) {
161 aSolidsToFuse.clear();
162 for (ListOfShape::iterator
163 anIt = anOriginalShapes.begin(); anIt != anOriginalShapes.end(); anIt++) {
164 ListOfShape aOneObjectList;
165 aOneObjectList.push_back(*anIt);
166 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
167 new GeomAlgoAPI_Boolean(aOneObjectList, aShapesToAdd, GeomAlgoAPI_Tools::BOOL_CUT));
169 if (GeomAlgoAPI_ShapeTools::volume(aCutAlgo->shape()) > 1.e-27) {
170 aSolidsToFuse.push_back(aCutAlgo->shape());
171 aMakeShapeList->appendAlgo(aCutAlgo);
176 if (!aSolidsToFuse.empty()) {
178 anObjects.push_back(aSolidsToFuse.back());
179 aSolidsToFuse.pop_back();
180 aTools = aSolidsToFuse;
183 // Fuse all objects and all tools.
185 if (anObjects.size() == 1 && aTools.empty()) {
186 aShape = anObjects.front();
187 } else if (anObjects.empty() && aTools.size() == 1) {
188 aShape = aTools.front();
189 } else if ((anObjects.size() + aTools.size()) > 1) {
190 std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
192 GeomAlgoAPI_Tools::BOOL_FUSE));
194 // Checking that the algorithm worked properly.
195 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFuseAlgo, getKind(), anError)) {
200 aShape = aFuseAlgo->shape();
201 aMakeShapeList->appendAlgo(aFuseAlgo);
204 // Combine result with not used solids from compsolid and edges and faces (if we have any).
205 if (aCuttedEdgesAndFaces.get() && !aCuttedEdgesAndFaces->isNull()) {
206 aShapesToAdd.push_back(aCuttedEdgesAndFaces);
208 aShapesToAdd.insert(aShapesToAdd.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
210 if (!aShapesToAdd.empty()) {
212 aShapesToAdd.push_back(aShape);
214 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
215 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
216 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFillerAlgo, getKind(), anError)) {
221 aShape = aFillerAlgo->shape();
222 aMakeShapeList->appendAlgo(aFillerAlgo);
225 bool isRemoveEdges = false;
226 AttributeBooleanPtr removeEdgesAttr = boolean(REMOVE_INTERSECTION_EDGES_ID());
227 if (removeEdgesAttr.get()) {
228 isRemoveEdges = removeEdgesAttr->value();
232 std::shared_ptr<GeomAlgoAPI_UnifySameDomain> aUnifyAlgo(
233 new GeomAlgoAPI_UnifySameDomain(aShape));
235 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aUnifyAlgo, getKind(), anError)) {
240 aShape = aUnifyAlgo->shape();
241 aMakeShapeList->appendAlgo(aUnifyAlgo);
244 if (aFuseVersion == THE_FUSE_VERSION_1) {
245 // merge hierarchies of compounds containing objects and tools
246 // and append the result of the FUSE operation
247 aShape = keepUnusedSubsOfCompound(aShape, anObjectsHierarchy, aToolsHierarchy, aMakeShapeList);
250 int aResultIndex = 0;
252 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
254 ListOfShape anEmptyTools;
255 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
260 setResult(aResultBody, aResultIndex);
263 FeaturesPlugin_Tools::loadDeletedShapes(aResultBody,
269 // remove the rest results if there were produced in the previous pass
270 removeResults(aResultIndex);