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_Partition.h"
23 #include <GeomAlgoAPI_DFLoader.h>
24 #include <GeomAlgoAPI_ShapeTools.h>
25 #include <GeomAlgoAPI_SortListOfShapes.h>
27 #include <GEOMAlgo_Splitter.hxx>
29 #include <NCollection_Vector.hxx>
30 #include <TopExp_Explorer.hxx>
31 #include <TopoDS_Builder.hxx>
32 #include <TopTools_MapOfShape.hxx>
35 //=================================================================================================
36 static bool isSubShape(const TopoDS_Shape& theShape, const TopoDS_Shape& theSubShape)
38 for(TopExp_Explorer anExp(theShape, theSubShape.ShapeType()); anExp.More(); anExp.Next()) {
39 if(theSubShape.IsSame(anExp.Current())) {
47 //=================================================================================================
48 static void sortCompound(TopoDS_Shape& theCompound)
50 ListOfShape aCombiningShapes;
51 for (TopoDS_Iterator anIt(theCompound); anIt.More(); anIt.Next()) {
52 GeomShapePtr aSub(new GeomAPI_Shape);
53 aSub->setImpl(new TopoDS_Shape(anIt.Value()));
54 aCombiningShapes.push_back(aSub);
57 // sort sub-shapes of compound to stabilize the sequence of the Partition's results
58 GeomAlgoAPI_SortListOfShapes::sort(aCombiningShapes);
60 TopoDS_Compound aTempCompound;
61 TopoDS_Builder aBuilder;
62 aBuilder.MakeCompound(aTempCompound);
63 for (ListOfShape::iterator anIt = aCombiningShapes.begin();
64 anIt != aCombiningShapes.end(); ++anIt)
65 aBuilder.Add(aTempCompound, (*anIt)->impl<TopoDS_Shape>());
66 theCompound = aTempCompound;
69 //=================================================================================================
70 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Partition::make(const ListOfShape& theObjects,
71 const ListOfShape& theTools)
73 GeomAlgoAPI_Partition aPartitionAlgo(theObjects, theTools);
74 if(aPartitionAlgo.isDone() && !aPartitionAlgo.shape()->isNull() && aPartitionAlgo.isValid()) {
75 return aPartitionAlgo.shape();
77 return std::shared_ptr<GeomAPI_Shape>();
80 //=================================================================================================
81 GeomAlgoAPI_Partition::GeomAlgoAPI_Partition(const ListOfShape& theObjects,
82 const ListOfShape& theTools)
84 build(theObjects, theTools);
87 static void prepareShapes(const TopoDS_Shape& theShape,
88 TopTools_ListOfShape& theSimpleList)
90 if (theShape.ShapeType() != TopAbs_COMPOUND) {
91 theSimpleList.Append(theShape);
95 // explode compound on simple shapes to allow their intersections
96 TopoDS_Iterator It (theShape, Standard_True, Standard_True);
97 for (; It.More(); It.Next()) {
98 TopoDS_Shape curSh = It.Value();
99 prepareShapes(curSh, theSimpleList);
103 //=================================================================================================
104 void GeomAlgoAPI_Partition::build(const ListOfShape& theObjects,
105 const ListOfShape& theTools)
107 if (theObjects.empty()) {
111 // Creating partition operation.
112 GEOMAlgo_Splitter* anOperation = new GEOMAlgo_Splitter;
113 this->setImpl(anOperation);
114 this->setBuilderType(OCCT_BOPAlgo_Builder);
116 TopTools_MapOfShape ShapesMap;
118 for(ListOfShape::const_iterator anObjectsIt = theObjects.begin();
119 anObjectsIt != theObjects.end();
122 const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
123 // #2240: decompose compounds to get the valid result
124 TopTools_ListOfShape aSimpleShapes;
125 prepareShapes(aShape, aSimpleShapes);
126 TopTools_ListIteratorOfListOfShape aSimpleIter(aSimpleShapes);
127 for (; aSimpleIter.More(); aSimpleIter.Next()) {
128 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
129 if (ShapesMap.Add(aSimpleSh)) {
130 anOperation->AddArgument(aSimpleSh);
136 for (ListOfShape::const_iterator
137 aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++) {
138 const TopoDS_Shape& aShape = (*aToolsIt)->impl<TopoDS_Shape>();
139 anOperation->AddTool(aShape);
142 // Building and getting result.
143 anOperation->Perform();
145 if (anOperation->HasErrors())
148 if(anOperation->ErrorStatus() != 0) {
152 TopoDS_Shape aResult = anOperation->Shape();
154 if(aResult.ShapeType() == TopAbs_COMPOUND) {
155 // Exclude faces and edges which are shared as another sub-shape.
156 NCollection_Vector<TopoDS_Shape> aFaces;
157 NCollection_Vector<TopoDS_Shape> anEdges;
158 TopoDS_Compound aTempCompound;
159 TopoDS_Builder aBuilder;
160 aBuilder.MakeCompound(aTempCompound);
161 for(TopoDS_Iterator anIt(aResult);
164 const TopoDS_Shape& aSubShape = anIt.Value();
165 if (aSubShape.ShapeType() == TopAbs_FACE) {
166 aFaces.Append(aSubShape);
167 } else if (aSubShape.ShapeType() == TopAbs_EDGE) {
168 anEdges.Append(aSubShape);
170 aBuilder.Add(aTempCompound, aSubShape);
174 for (NCollection_Vector<TopoDS_Shape>::Iterator anIt(aFaces);
178 const TopoDS_Shape& aSubShape = anIt.Value();
179 if (!isSubShape(aTempCompound, aSubShape))
181 aBuilder.Add(aTempCompound, aSubShape);
185 for (NCollection_Vector<TopoDS_Shape>::Iterator anIt(anEdges);
189 const TopoDS_Shape& aSubShape = anIt.Value();
190 if (!isSubShape(aTempCompound, aSubShape))
192 aBuilder.Add(aTempCompound, aSubShape);
196 aResult = aTempCompound;
199 if(aResult.ShapeType() == TopAbs_COMPOUND) {
200 // sort sub-shapes of compound before creation of a compsolid
201 sortCompound(aResult);
203 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
204 aGeomShape->setImpl(new TopoDS_Shape(aResult));
205 aResult = GeomAlgoAPI_ShapeTools::groupSharedTopology(aGeomShape)->impl<TopoDS_Shape>();
209 if(aResult.IsNull()) {
212 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
213 aShape->setImpl(new TopoDS_Shape(aResult));
214 this->setShape(aShape);