Salome HOME
Issue #2304: 1.1.2.1 To modify Partition
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Partition.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "GeomAlgoAPI_Partition.h"
22
23 #include <GeomAlgoAPI_DFLoader.h>
24 #include <GeomAlgoAPI_ShapeTools.h>
25
26 #include <GEOMAlgo_Splitter.hxx>
27
28 #include <NCollection_Vector.hxx>
29 #include <TopExp_Explorer.hxx>
30 #include <TopoDS_Builder.hxx>
31 #include <TopTools_MapOfShape.hxx>
32
33 //=================================================================================================
34 bool isSubShape(const TopoDS_Shape& theShape, const TopoDS_Shape& theSubShape)
35 {
36   for(TopExp_Explorer anExp(theShape, theSubShape.ShapeType()); anExp.More(); anExp.Next()) {
37     if(theSubShape.IsSame(anExp.Current())) {
38       return true;
39     }
40   }
41
42   return false;
43 }
44
45 //=================================================================================================
46 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Partition::make(const ListOfShape& theObjects,
47                                                            const ListOfShape& theTools)
48 {
49   GeomAlgoAPI_Partition aPartitionAlgo(theObjects, theTools);
50   if(aPartitionAlgo.isDone() && !aPartitionAlgo.shape()->isNull() && aPartitionAlgo.isValid()) {
51     return aPartitionAlgo.shape();
52   }
53   return std::shared_ptr<GeomAPI_Shape>();
54 }
55
56 //=================================================================================================
57 GeomAlgoAPI_Partition::GeomAlgoAPI_Partition(const ListOfShape& theObjects,
58                                              const ListOfShape& theTools)
59 {
60   build(theObjects, theTools);
61 }
62
63 static void prepareShapes(const TopoDS_Shape&   theShape,
64                            TopTools_ListOfShape& theSimpleList)
65 {
66   if (theShape.ShapeType() != TopAbs_COMPOUND) {
67       theSimpleList.Append(theShape);
68     return;
69   }
70
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);
76   }
77 }
78
79 //=================================================================================================
80 void GeomAlgoAPI_Partition::build(const ListOfShape& theObjects,
81                                   const ListOfShape& theTools)
82 {
83   if (theObjects.empty()) {
84     return;
85   }
86
87   // Creating partition operation.
88   GEOMAlgo_Splitter* anOperation = new GEOMAlgo_Splitter;
89   this->setImpl(anOperation);
90   this->setBuilderType(OCCT_BOPAlgo_Builder);
91
92   TopTools_MapOfShape ShapesMap;
93   // Getting objects.
94   for(ListOfShape::const_iterator anObjectsIt = theObjects.begin();
95       anObjectsIt != theObjects.end();
96       anObjectsIt++)
97   {
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);
107       }
108     }
109   }
110
111   // Getting tools.
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);
116   }
117
118   // Building and getting result.
119   anOperation->Perform();
120 #ifdef USE_OCCT_720
121   if (anOperation->HasErrors())
122     return;
123 #else
124   if(anOperation->ErrorStatus() != 0) {
125     return;
126   }
127 #endif
128   TopoDS_Shape aResult = anOperation->Shape();
129
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);
137         anIt.More();
138         anIt.Next()) {
139       const TopoDS_Shape& aSubShape = anIt.Value();
140       if (aSubShape.ShapeType() == TopAbs_FACE || aSubShape.ShapeType() == TopAbs_EDGE) {
141         aFacesAndEdges.Append(aSubShape);
142       } else {
143         aBuilder.Add(aTempCompound, aSubShape);
144       }
145     }
146
147     for (NCollection_Vector<TopoDS_Shape>::Iterator anIt(aFacesAndEdges);
148         anIt.More();
149         anIt.Next())
150     {
151       const TopoDS_Shape& aSubShape = anIt.Value();
152       if (!isSubShape(aTempCompound, aSubShape))
153       {
154         aBuilder.Add(aTempCompound, aSubShape);
155       }
156     }
157
158     aResult = aTempCompound;
159   }
160
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>();
165   }
166
167   // Setting result.
168   if(aResult.IsNull()) {
169     return;
170   }
171   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
172   aShape->setImpl(new TopoDS_Shape(aResult));
173   this->setShape(aShape);
174   this->setDone(true);
175 }