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>
33 #include <BRepTools_History.hxx>
36 //=================================================================================================
37 static bool isSubShape(const TopoDS_Shape& theShape, const TopoDS_Shape& theSubShape)
39 for(TopExp_Explorer anExp(theShape, theSubShape.ShapeType()); anExp.More(); anExp.Next()) {
40 if(theSubShape.IsSame(anExp.Current())) {
48 //=================================================================================================
49 void getHistorySupportedType(const TopoDS_Shape& theShape, TopTools_ListOfShape& theResult) {
50 if (BRepTools_History::IsSupportedType(theShape)) {
51 theResult.Append(theShape);
53 for (TopoDS_Iterator aSubIter(theShape); aSubIter.More(); aSubIter.Next()) {
54 getHistorySupportedType(aSubIter.Value(), theResult);
59 //=================================================================================================
60 // Operation is used for production of ordered sorting: generated/modified from the first argument
61 // must be located in hte result first, etc. THis is for the issue #2517
62 static void sortCompound(TopoDS_Shape& theCompound, GEOMAlgo_Splitter* theOperation)
64 TopoDS_Compound aResCompound;
65 TopoDS_Builder aBuilder;
66 aBuilder.MakeCompound(aResCompound);
68 TopTools_MapOfShape anAlreadyThere; // to avoid duplications if it was produced by two arguments
70 // flag to add to result also results which were not produced by any argument
71 bool aNotProduced = true;
72 TopTools_ListOfShape::Iterator anArgs(theOperation->Arguments());
73 while(aNotProduced || anArgs.More()) {
74 // collect shapes that were produced from the current argument
75 TopTools_MapOfShape aProducedByArg;
77 TopTools_ListOfShape allArgs;
78 getHistorySupportedType(anArgs.Value(), allArgs);
79 for (TopTools_ListOfShape::Iterator argsIter(allArgs); argsIter.More(); argsIter.Next()) {
80 // if argument was not modified, it is fully in the result
81 aProducedByArg.Add(argsIter.Value());
82 const TopTools_ListOfShape& aModified = theOperation->Modified(argsIter.Value());
83 for (TopTools_ListOfShape::Iterator aModIter(aModified); aModIter.More(); aModIter.Next()) {
84 aProducedByArg.Add(aModIter.Value());
86 const TopTools_ListOfShape& aGenerated = theOperation->Generated(argsIter.Value());
87 for (TopTools_ListOfShape::Iterator aGenIter(aGenerated); aGenIter.More(); aGenIter.Next())
89 aProducedByArg.Add(aGenIter.Value());
98 ListOfShape aCombiningShapes;
99 for (TopoDS_Iterator anIt(theCompound); anIt.More(); anIt.Next()) {
100 bool aProducedContains = false;
101 if (aNotProduced) { // collect all supported type-shapes of result
102 TopTools_ListOfShape allRes;
103 getHistorySupportedType(anIt.Value(), allRes);
104 for (TopTools_ListOfShape::Iterator aResIter(allRes); aResIter.More(); aResIter.Next()) {
105 if (aProducedByArg.Contains(aResIter.Value())) {
106 aProducedContains = true;
111 if ((!aNotProduced || aProducedContains) && anAlreadyThere.Add(anIt.Value())) {
112 GeomShapePtr aSub(new GeomAPI_Shape);
113 aSub->setImpl(new TopoDS_Shape(anIt.Value()));
114 aCombiningShapes.push_back(aSub);
118 // sort sub-shapes of compound to stabilize the sequence of the Partition's results
119 GeomAlgoAPI_SortListOfShapes::sort(aCombiningShapes);
121 for (ListOfShape::iterator anIt = aCombiningShapes.begin();
122 anIt != aCombiningShapes.end(); ++anIt)
123 aBuilder.Add(aResCompound, (*anIt)->impl<TopoDS_Shape>());
126 theCompound = aResCompound;
129 //=================================================================================================
130 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Partition::make(const ListOfShape& theObjects,
131 const ListOfShape& theTools)
133 GeomAlgoAPI_Partition aPartitionAlgo(theObjects, theTools);
134 if(aPartitionAlgo.isDone() && !aPartitionAlgo.shape()->isNull() && aPartitionAlgo.isValid()) {
135 return aPartitionAlgo.shape();
137 return std::shared_ptr<GeomAPI_Shape>();
140 //=================================================================================================
141 GeomAlgoAPI_Partition::GeomAlgoAPI_Partition(const ListOfShape& theObjects,
142 const ListOfShape& theTools)
144 build(theObjects, theTools);
147 static void prepareShapes(const TopoDS_Shape& theShape,
148 TopTools_ListOfShape& theSimpleList)
150 if (theShape.ShapeType() != TopAbs_COMPOUND) {
151 theSimpleList.Append(theShape);
155 // explode compound on simple shapes to allow their intersections
156 TopoDS_Iterator It (theShape, Standard_True, Standard_True);
157 for (; It.More(); It.Next()) {
158 TopoDS_Shape curSh = It.Value();
159 prepareShapes(curSh, theSimpleList);
163 //=================================================================================================
164 void GeomAlgoAPI_Partition::build(const ListOfShape& theObjects,
165 const ListOfShape& theTools)
167 if (theObjects.empty()) {
171 // Creating partition operation.
172 GEOMAlgo_Splitter* anOperation = new GEOMAlgo_Splitter;
173 this->setImpl(anOperation);
174 this->setBuilderType(OCCT_BOPAlgo_Builder);
176 TopTools_MapOfShape ShapesMap;
178 for(ListOfShape::const_iterator anObjectsIt = theObjects.begin();
179 anObjectsIt != theObjects.end();
182 const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
183 // #2240: decompose compounds to get the valid result
184 TopTools_ListOfShape aSimpleShapes;
185 prepareShapes(aShape, aSimpleShapes);
186 TopTools_ListIteratorOfListOfShape aSimpleIter(aSimpleShapes);
187 for (; aSimpleIter.More(); aSimpleIter.Next()) {
188 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
189 if (ShapesMap.Add(aSimpleSh)) {
190 anOperation->AddArgument(aSimpleSh);
196 for (ListOfShape::const_iterator
197 aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++) {
198 const TopoDS_Shape& aShape = (*aToolsIt)->impl<TopoDS_Shape>();
199 // #2419: decompose compounds to get the valid result
200 TopTools_ListOfShape aSimpleShapes;
201 prepareShapes(aShape, aSimpleShapes);
202 TopTools_ListIteratorOfListOfShape aSimpleIter(aSimpleShapes);
203 for (; aSimpleIter.More(); aSimpleIter.Next()) {
204 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
205 if (ShapesMap.Add(aSimpleSh)) {
206 anOperation->AddTool(aSimpleSh);
211 // Building and getting result.
212 anOperation->Perform();
213 if (anOperation->HasErrors())
215 TopoDS_Shape aResult = anOperation->Shape();
217 if(aResult.ShapeType() == TopAbs_COMPOUND) {
218 // Exclude faces and edges which are shared as another sub-shape.
219 NCollection_Vector<TopoDS_Shape> aFaces;
220 NCollection_Vector<TopoDS_Shape> anEdges;
221 TopoDS_Compound aTempCompound;
222 TopoDS_Builder aBuilder;
223 aBuilder.MakeCompound(aTempCompound);
224 for(TopoDS_Iterator anIt(aResult);
227 const TopoDS_Shape& aSubShape = anIt.Value();
228 if (aSubShape.ShapeType() == TopAbs_FACE) {
229 aFaces.Append(aSubShape);
230 } else if (aSubShape.ShapeType() == TopAbs_EDGE) {
231 anEdges.Append(aSubShape);
233 aBuilder.Add(aTempCompound, aSubShape);
237 for (NCollection_Vector<TopoDS_Shape>::Iterator anIt(aFaces);
241 const TopoDS_Shape& aSubShape = anIt.Value();
242 if (!isSubShape(aTempCompound, aSubShape))
244 aBuilder.Add(aTempCompound, aSubShape);
248 for (NCollection_Vector<TopoDS_Shape>::Iterator anIt(anEdges);
252 const TopoDS_Shape& aSubShape = anIt.Value();
253 if (!isSubShape(aTempCompound, aSubShape))
255 aBuilder.Add(aTempCompound, aSubShape);
259 aResult = aTempCompound;
262 if(aResult.ShapeType() == TopAbs_COMPOUND) {
263 // sort sub-shapes of compound before creation of a compsolid
264 sortCompound(aResult, anOperation);
266 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
267 aGeomShape->setImpl(new TopoDS_Shape(aResult));
268 aResult = GeomAlgoAPI_ShapeTools::groupSharedTopology(aGeomShape)->impl<TopoDS_Shape>();
272 if(aResult.IsNull()) {
275 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
276 aShape->setImpl(new TopoDS_Shape(aResult));
277 this->setShape(aShape);