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;
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))
81 if (!isSimpleCreation &&
82 !processAttribute(TOOL_LIST_ID(), aToolsHierarchy, aPlanes))
85 ListOfShape anObjects, aTools, anEdgesAndFaces;
86 // all objects except edges and faces
87 anObjectsHierarchy.ObjectsByType(anEdgesAndFaces, anObjects,
88 GeomAPI_Shape::FACE, GeomAPI_Shape::EDGE);
89 aToolsHierarchy.ObjectsByType(anEdgesAndFaces, aTools,
90 GeomAPI_Shape::FACE, GeomAPI_Shape::EDGE);
92 if ((anObjects.size() + aTools.size() + anEdgesAndFaces.size()) < 2) {
93 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
94 setError(aFeatureError);
98 // Collecting all solids which will be fused.
99 ListOfShape aSolidsToFuse;
100 aSolidsToFuse.insert(aSolidsToFuse.end(), anObjects.begin(), anObjects.end());
101 aSolidsToFuse.insert(aSolidsToFuse.end(), aTools.begin(), aTools.end());
103 // Collecting solids from compsolids which will not be modified
104 // in boolean operation and will be added to result.
105 ListOfShape aShapesToAdd;
106 for (ObjectHierarchy::Iterator anObjectsIt = anObjectsHierarchy.Begin();
107 !isSimpleCreation && anObjectsIt != anObjectsHierarchy.End();
109 GeomShapePtr anObject = *anObjectsIt;
110 GeomShapePtr aParent = anObjectsHierarchy.Parent(anObject, false);
112 if (aParent && aParent->shapeType() == GeomAPI_Shape::COMPSOLID) {
113 // mark all subs of this parent as precessed to avoid handling twice
114 aParent = anObjectsHierarchy.Parent(anObject);
116 ListOfShape aUsed, aNotUsed;
117 anObjectsHierarchy.SplitCompound(aParent, aUsed, aNotUsed);
118 aShapesToAdd.insert(aShapesToAdd.end(), aNotUsed.begin(), aNotUsed.end());
122 ListOfShape anOriginalShapes = aSolidsToFuse;
123 anOriginalShapes.insert(anOriginalShapes.end(), aShapesToAdd.begin(), aShapesToAdd.end());
125 // Cut edges and faces(if we have any) with solids.
126 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
127 GeomShapePtr aCuttedEdgesAndFaces;
128 if (!anEdgesAndFaces.empty()) {
129 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(anEdgesAndFaces,
130 anOriginalShapes, GeomAlgoAPI_Tools::BOOL_CUT));
131 if (aCutAlgo->isDone()) {
132 aCuttedEdgesAndFaces = aCutAlgo->shape();
133 aMakeShapeList->appendAlgo(aCutAlgo);
136 anOriginalShapes.insert(anOriginalShapes.end(), anEdgesAndFaces.begin(),
137 anEdgesAndFaces.end());
139 // If we have compsolids then cut with not used solids all others.
140 if (!aShapesToAdd.empty()) {
141 aSolidsToFuse.clear();
142 for (ListOfShape::iterator
143 anIt = anOriginalShapes.begin(); anIt != anOriginalShapes.end(); anIt++) {
144 ListOfShape aOneObjectList;
145 aOneObjectList.push_back(*anIt);
146 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
147 new GeomAlgoAPI_Boolean(aOneObjectList, aShapesToAdd, GeomAlgoAPI_Tools::BOOL_CUT));
149 if (GeomAlgoAPI_ShapeTools::volume(aCutAlgo->shape()) > 1.e-27) {
150 aSolidsToFuse.push_back(aCutAlgo->shape());
151 aMakeShapeList->appendAlgo(aCutAlgo);
156 if (!aSolidsToFuse.empty()) {
158 anObjects.push_back(aSolidsToFuse.back());
159 aSolidsToFuse.pop_back();
160 aTools = aSolidsToFuse;
163 // Fuse all objects and all tools.
165 if (anObjects.size() == 1 && aTools.empty()) {
166 aShape = anObjects.front();
167 } else if (anObjects.empty() && aTools.size() == 1) {
168 aShape = aTools.front();
169 } else if ((anObjects.size() + aTools.size()) > 1) {
170 std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
172 GeomAlgoAPI_Tools::BOOL_FUSE));
174 // Checking that the algorithm worked properly.
175 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFuseAlgo, getKind(), anError)) {
180 aShape = aFuseAlgo->shape();
181 aMakeShapeList->appendAlgo(aFuseAlgo);
184 // Combine result with not used solids from compsolid and edges and faces (if we have any).
185 if (aCuttedEdgesAndFaces.get() && !aCuttedEdgesAndFaces->isNull()) {
186 aShapesToAdd.push_back(aCuttedEdgesAndFaces);
188 aShapesToAdd.insert(aShapesToAdd.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
190 if (!aShapesToAdd.empty()) {
192 aShapesToAdd.push_back(aShape);
194 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
195 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
196 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFillerAlgo, getKind(), anError)) {
201 aShape = aFillerAlgo->shape();
202 aMakeShapeList->appendAlgo(aFillerAlgo);
205 bool isRemoveEdges = false;
206 AttributeBooleanPtr removeEdgesAttr = boolean(REMOVE_INTERSECTION_EDGES_ID());
207 if (removeEdgesAttr.get()) {
208 isRemoveEdges = removeEdgesAttr->value();
212 std::shared_ptr<GeomAlgoAPI_UnifySameDomain> aUnifyAlgo(
213 new GeomAlgoAPI_UnifySameDomain(aShape));
215 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aUnifyAlgo, getKind(), anError)) {
220 aShape = aUnifyAlgo->shape();
221 aMakeShapeList->appendAlgo(aUnifyAlgo);
224 int aResultIndex = 0;
226 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
228 ListOfShape anEmptyTools;
229 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
234 setResult(aResultBody, aResultIndex);
237 FeaturesPlugin_Tools::loadDeletedShapes(aResultBody,
243 // remove the rest results if there were produced in the previous pass
244 removeResults(aResultIndex);