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 // #2419: decompose compounds to get the valid result
140 TopTools_ListOfShape aSimpleShapes;
141 prepareShapes(aShape, aSimpleShapes);
142 TopTools_ListIteratorOfListOfShape aSimpleIter(aSimpleShapes);
143 for (; aSimpleIter.More(); aSimpleIter.Next()) {
144 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
145 if (ShapesMap.Add(aSimpleSh)) {
146 anOperation->AddTool(aSimpleSh);
151 // Building and getting result.
152 anOperation->Perform();
153 if (anOperation->HasErrors())
155 TopoDS_Shape aResult = anOperation->Shape();
157 if(aResult.ShapeType() == TopAbs_COMPOUND) {
158 // Exclude faces and edges which are shared as another sub-shape.
159 NCollection_Vector<TopoDS_Shape> aFaces;
160 NCollection_Vector<TopoDS_Shape> anEdges;
161 TopoDS_Compound aTempCompound;
162 TopoDS_Builder aBuilder;
163 aBuilder.MakeCompound(aTempCompound);
164 for(TopoDS_Iterator anIt(aResult);
167 const TopoDS_Shape& aSubShape = anIt.Value();
168 if (aSubShape.ShapeType() == TopAbs_FACE) {
169 aFaces.Append(aSubShape);
170 } else if (aSubShape.ShapeType() == TopAbs_EDGE) {
171 anEdges.Append(aSubShape);
173 aBuilder.Add(aTempCompound, aSubShape);
177 for (NCollection_Vector<TopoDS_Shape>::Iterator anIt(aFaces);
181 const TopoDS_Shape& aSubShape = anIt.Value();
182 if (!isSubShape(aTempCompound, aSubShape))
184 aBuilder.Add(aTempCompound, aSubShape);
188 for (NCollection_Vector<TopoDS_Shape>::Iterator anIt(anEdges);
192 const TopoDS_Shape& aSubShape = anIt.Value();
193 if (!isSubShape(aTempCompound, aSubShape))
195 aBuilder.Add(aTempCompound, aSubShape);
199 aResult = aTempCompound;
202 if(aResult.ShapeType() == TopAbs_COMPOUND) {
203 // sort sub-shapes of compound before creation of a compsolid
204 sortCompound(aResult);
206 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
207 aGeomShape->setImpl(new TopoDS_Shape(aResult));
208 aResult = GeomAlgoAPI_ShapeTools::groupSharedTopology(aGeomShape)->impl<TopoDS_Shape>();
212 if(aResult.IsNull()) {
215 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
216 aShape->setImpl(new TopoDS_Shape(aResult));
217 this->setShape(aShape);