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>
26 #include <GEOMAlgo_Splitter.hxx>
28 #include <NCollection_Vector.hxx>
29 #include <TopExp_Explorer.hxx>
30 #include <TopoDS_Builder.hxx>
31 #include <TopTools_MapOfShape.hxx>
33 //=================================================================================================
34 bool isSubShape(const TopoDS_Shape& theShape, const TopoDS_Shape& theSubShape)
36 for(TopExp_Explorer anExp(theShape, theSubShape.ShapeType()); anExp.More(); anExp.Next()) {
37 if(theSubShape.IsSame(anExp.Current())) {
45 //=================================================================================================
46 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Partition::make(const ListOfShape& theObjects,
47 const ListOfShape& theTools)
49 GeomAlgoAPI_Partition aPartitionAlgo(theObjects, theTools);
50 if(aPartitionAlgo.isDone() && !aPartitionAlgo.shape()->isNull() && aPartitionAlgo.isValid()) {
51 return aPartitionAlgo.shape();
53 return std::shared_ptr<GeomAPI_Shape>();
56 //=================================================================================================
57 GeomAlgoAPI_Partition::GeomAlgoAPI_Partition(const ListOfShape& theObjects,
58 const ListOfShape& theTools)
60 build(theObjects, theTools);
63 static void prepareShapes(const TopoDS_Shape& theShape,
64 TopTools_ListOfShape& theSimpleList)
66 if (theShape.ShapeType() != TopAbs_COMPOUND) {
67 theSimpleList.Append(theShape);
71 // explode compound on simple shapes to allow their intersections
72 TopoDS_Iterator It (theShape, Standard_True, Standard_True);
73 for (; It.More(); It.Next()) {
74 TopoDS_Shape curSh = It.Value();
75 prepareShapes(curSh, theSimpleList);
79 //=================================================================================================
80 void GeomAlgoAPI_Partition::build(const ListOfShape& theObjects,
81 const ListOfShape& theTools)
83 if (theObjects.empty()) {
87 // Creating partition operation.
88 GEOMAlgo_Splitter* anOperation = new GEOMAlgo_Splitter;
89 this->setImpl(anOperation);
90 this->setBuilderType(OCCT_BOPAlgo_Builder);
92 TopTools_MapOfShape ShapesMap;
94 for(ListOfShape::const_iterator anObjectsIt = theObjects.begin();
95 anObjectsIt != theObjects.end();
98 const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
99 // #2240: decompose compounds to get the valid result
100 TopTools_ListOfShape aSimpleShapes;
101 prepareShapes(aShape, aSimpleShapes);
102 TopTools_ListIteratorOfListOfShape aSimpleIter(aSimpleShapes);
103 for (; aSimpleIter.More(); aSimpleIter.Next()) {
104 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
105 if (ShapesMap.Add(aSimpleSh)) {
106 anOperation->AddArgument(aSimpleSh);
112 for (ListOfShape::const_iterator
113 aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++) {
114 const TopoDS_Shape& aShape = (*aToolsIt)->impl<TopoDS_Shape>();
115 anOperation->AddTool(aShape);
118 // Building and getting result.
119 anOperation->Perform();
121 if (anOperation->HasErrors())
124 if(anOperation->ErrorStatus() != 0) {
128 TopoDS_Shape aResult = anOperation->Shape();
130 if(aResult.ShapeType() == TopAbs_COMPOUND) {
131 // Exclude faces and edges which are shared as another sub-shape.
132 NCollection_Vector<TopoDS_Shape> aFacesAndEdges;
133 TopoDS_Compound aTempCompound;
134 TopoDS_Builder aBuilder;
135 aBuilder.MakeCompound(aTempCompound);
136 for(TopoDS_Iterator anIt(aResult);
139 const TopoDS_Shape& aSubShape = anIt.Value();
140 if (aSubShape.ShapeType() == TopAbs_FACE || aSubShape.ShapeType() == TopAbs_EDGE) {
141 aFacesAndEdges.Append(aSubShape);
143 aBuilder.Add(aTempCompound, aSubShape);
147 for (NCollection_Vector<TopoDS_Shape>::Iterator anIt(aFacesAndEdges);
151 const TopoDS_Shape& aSubShape = anIt.Value();
152 if (!isSubShape(aTempCompound, aSubShape))
154 aBuilder.Add(aTempCompound, aSubShape);
158 aResult = aTempCompound;
161 if(aResult.ShapeType() == TopAbs_COMPOUND) {
162 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
163 aGeomShape->setImpl(new TopoDS_Shape(aResult));
164 aResult = GeomAlgoAPI_ShapeTools::groupSharedTopology(aGeomShape)->impl<TopoDS_Shape>();
168 if(aResult.IsNull()) {
171 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
172 aShape->setImpl(new TopoDS_Shape(aResult));
173 this->setShape(aShape);