1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "GeomAlgoAPI_Boolean.h"
23 #include <GeomAlgoAPI_DFLoader.h>
24 #include <GeomAlgoAPI_ShapeTools.h>
26 #include <BOPAlgo_BOP.hxx>
27 #include <TopTools_ListOfShape.hxx>
28 #include <TopoDS_Iterator.hxx>
29 #include <TopExp_Explorer.hxx>
31 //=================================================================================================
32 GeomAlgoAPI_Boolean::GeomAlgoAPI_Boolean(const GeomShapePtr theObject,
33 const GeomShapePtr theTool,
34 const OperationType theOperationType)
36 ListOfShape aListWithObject, aListWithTool;
37 aListWithObject.push_back(theObject);
38 aListWithTool.push_back(theTool);
39 build(aListWithObject, aListWithTool, theOperationType);
42 //=================================================================================================
43 GeomAlgoAPI_Boolean::GeomAlgoAPI_Boolean(const GeomShapePtr theObject,
44 const ListOfShape& theTools,
45 const OperationType theOperationType)
47 ListOfShape aListWithObject;
48 aListWithObject.push_back(theObject);
49 build(aListWithObject, theTools, theOperationType);
52 //=================================================================================================
53 GeomAlgoAPI_Boolean::GeomAlgoAPI_Boolean(const ListOfShape& theObjects,
54 const ListOfShape& theTools,
55 const OperationType theOperationType)
57 build(theObjects, theTools, theOperationType);
61 //=================================================================================================
62 void GeomAlgoAPI_Boolean::build(const ListOfShape& theObjects,
63 const ListOfShape& theTools,
64 const OperationType theOperationType)
66 if(theObjects.empty() || theTools.empty()) {
71 TopTools_ListOfShape anObjects;
72 for(ListOfShape::const_iterator
73 anObjectsIt = theObjects.begin(); anObjectsIt != theObjects.end(); anObjectsIt++)
75 anObjects.Append((*anObjectsIt)->impl<TopoDS_Shape>());
79 TopTools_ListOfShape aTools;
80 for(ListOfShape::const_iterator
81 aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++)
83 aTools.Append((*aToolsIt)->impl<TopoDS_Shape>());
86 // Creating boolean operation.
87 BOPAlgo_BOP* aBuilder = new BOPAlgo_BOP();
88 switch (theOperationType) {
90 aBuilder->SetOperation(BOPAlgo_CUT);
94 aBuilder->SetOperation(BOPAlgo_FUSE);
98 aBuilder->SetOperation(BOPAlgo_COMMON);
105 this->setImpl(aBuilder);
106 this->setBuilderType(OCCT_BOPAlgo_Builder);
107 aBuilder->SetArguments(anObjects);
108 aBuilder->SetTools(aTools);
110 // Building and getting result.
112 if (aBuilder->HasErrors())
114 TopoDS_Shape aResult = aBuilder->Shape();
116 if(aResult.ShapeType() == TopAbs_COMPOUND) {
117 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
119 if(aResult.ShapeType() == TopAbs_COMPOUND) {
120 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
121 aGeomShape->setImpl(new TopoDS_Shape(aResult));
122 ListOfShape aCompSolids, aFreeSolids;
123 aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
124 GeomAPI_Shape::COMPSOLID,
127 aResult = aGeomShape->impl<TopoDS_Shape>();
130 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
131 aShape->setImpl(new TopoDS_Shape(aResult));
132 this->setShape(aShape);
136 static bool isHistoryType(TopAbs_ShapeEnum theType) {
137 return theType == TopAbs_VERTEX || theType == TopAbs_EDGE ||
138 theType == TopAbs_FACE || theType == TopAbs_SOLID;
141 /// searches the corresponding result for theOld
142 static void searchResult(const TopoDS_Shape& theOld, const TopoDS_Shape& theResult,
143 BOPAlgo_BOP* theBuilder, TopTools_MapOfShape& theNews)
145 if (theResult.ShapeType() == theOld.ShapeType()) { // check some sub-shape produces a sub-result
146 if (theOld.IsSame(theResult)) {
147 theNews.Add(theResult);
150 // searching for new result by sub-shapes of aSubType type
151 TopAbs_ShapeEnum aSubType = TopAbs_ShapeEnum(int(theOld.ShapeType()) + 1);
152 while(aSubType < TopAbs_VERTEX && !isHistoryType(aSubType))
153 aSubType = TopAbs_ShapeEnum(int(aSubType) + 1);
154 if (aSubType == TopAbs_SHAPE)
156 TopTools_MapOfShape aResSubs;
157 for(TopExp_Explorer aResExp(theResult, aSubType); aResExp.More(); aResExp.Next())
158 aResSubs.Add(aResExp.Current());
159 for(TopExp_Explorer anExp(theOld, aSubType); anExp.More(); anExp.Next()) {
160 const TopTools_ListOfShape& aNewSubs = theBuilder->Modified(anExp.Current());
161 // searching for this new sub in theResult
162 for(TopTools_ListIteratorOfListOfShape aNewSub(aNewSubs); aNewSub.More(); aNewSub.Next()) {
163 if (aResSubs.Contains(aNewSub.Value())) {
164 theNews.Add(theResult);
169 } else if (theResult.ShapeType() < theOld.ShapeType()) { // recursive search among sub-shapes
170 for(TopoDS_Iterator aSubResults(theResult); aSubResults.More(); aSubResults.Next()) {
171 searchResult(theOld, aSubResults.Value(), theBuilder, theNews);
176 // check the shape is on the higher level of compound or compsolid
177 bool isInComp(const TopoDS_Shape& theComp, const TopoDS_Shape& theShape) {
178 if (theComp.ShapeType() == TopAbs_COMPOUND || theComp.ShapeType() == TopAbs_COMPSOLID) {
179 for(TopoDS_Iterator anIter(theComp); anIter.More(); anIter.Next()) {
180 if (isInComp(anIter.Value(), theShape))
183 } else return theShape.IsSame(theComp);
187 //=================================================================================================
188 /// make arguments of Fuse produce result shapes with "modified" evolution
189 void GeomAlgoAPI_Boolean::modified(const GeomShapePtr theOldShape, ListOfShape& theNewShapes)
191 BOPAlgo_BOP* aBuilder = this->implPtr<BOPAlgo_BOP>();
192 if (aBuilder->Operation() == BOPAlgo_FUSE) { // only for fuse and when old is and argument
193 TopoDS_Shape anOld = theOldShape->impl<TopoDS_Shape>();
194 bool isOldComp = anOld.ShapeType() == TopAbs_COMPOUND || anOld.ShapeType() == TopAbs_COMPSOLID;
196 TopTools_ListIteratorOfListOfShape anIter(aBuilder->Arguments());
197 for(; !aFound && anIter.More(); anIter.Next())
198 aFound = anOld.IsSame(anIter.Value()) || (!isOldComp && isInComp(anIter.Value(), anOld));
199 for(anIter.Initialize(aBuilder->Tools()); !aFound && anIter.More(); anIter.Next())
200 aFound = anOld.IsSame(anIter.Value()) || (!isOldComp && isInComp(anIter.Value(), anOld));
202 TopoDS_Shape aResult = aBuilder->Shape();
203 TopTools_MapOfShape aNewsMap;
204 searchResult(anOld, aResult, aBuilder, aNewsMap);
205 if (!aNewsMap.IsEmpty()) {
206 for(TopTools_MapIteratorOfMapOfShape aNewsIter(aNewsMap); aNewsIter.More(); aNewsIter.Next())
208 GeomShapePtr aShape(new GeomAPI_Shape);
209 aShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aNewsIter.Value()));
210 theNewShapes.push_back(aShape);
216 GeomAlgoAPI_MakeShape::modified(theOldShape, theNewShapes); // default behavior