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 bool aNotProduced = true;// flag to add to result also results that were not produced by any argument
71 TopTools_ListOfShape::Iterator anArgs(theOperation->Arguments());
72 while(aNotProduced || anArgs.More()) {
73 // collect shapes that were produced from the current argument
74 TopTools_MapOfShape aProducedByArg;
76 TopTools_ListOfShape allArgs;
77 getHistorySupportedType(anArgs.Value(), allArgs);
78 for (TopTools_ListOfShape::Iterator argsIter(allArgs); argsIter.More(); argsIter.Next()) {
79 aProducedByArg.Add(argsIter.Value()); // if argument was not modified, it is fully in the result
80 const TopTools_ListOfShape& aModified = theOperation->Modified(argsIter.Value());
81 for (TopTools_ListOfShape::Iterator aModIter(aModified); aModIter.More(); aModIter.Next()) {
82 aProducedByArg.Add(aModIter.Value());
84 const TopTools_ListOfShape& aGenerated = theOperation->Generated(argsIter.Value());
85 for (TopTools_ListOfShape::Iterator aGenIter(aGenerated); aGenIter.More(); aGenIter.Next()) {
86 aProducedByArg.Add(aGenIter.Value());
95 ListOfShape aCombiningShapes;
96 for (TopoDS_Iterator anIt(theCompound); anIt.More(); anIt.Next()) {
97 bool aProducedContains = false;
98 if (aNotProduced) { // collect all supported type-shapes of result
99 TopTools_ListOfShape allRes;
100 getHistorySupportedType(anIt.Value(), allRes);
101 for (TopTools_ListOfShape::Iterator aResIter(allRes); aResIter.More(); aResIter.Next()) {
102 if (aProducedByArg.Contains(aResIter.Value())) {
103 aProducedContains = true;
108 if ((!aNotProduced || aProducedContains) && anAlreadyThere.Add(anIt.Value())) {
109 GeomShapePtr aSub(new GeomAPI_Shape);
110 aSub->setImpl(new TopoDS_Shape(anIt.Value()));
111 aCombiningShapes.push_back(aSub);
115 // sort sub-shapes of compound to stabilize the sequence of the Partition's results
116 GeomAlgoAPI_SortListOfShapes::sort(aCombiningShapes);
118 for (ListOfShape::iterator anIt = aCombiningShapes.begin();
119 anIt != aCombiningShapes.end(); ++anIt)
120 aBuilder.Add(aResCompound, (*anIt)->impl<TopoDS_Shape>());
123 theCompound = aResCompound;
126 //=================================================================================================
127 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Partition::make(const ListOfShape& theObjects,
128 const ListOfShape& theTools)
130 GeomAlgoAPI_Partition aPartitionAlgo(theObjects, theTools);
131 if(aPartitionAlgo.isDone() && !aPartitionAlgo.shape()->isNull() && aPartitionAlgo.isValid()) {
132 return aPartitionAlgo.shape();
134 return std::shared_ptr<GeomAPI_Shape>();
137 //=================================================================================================
138 GeomAlgoAPI_Partition::GeomAlgoAPI_Partition(const ListOfShape& theObjects,
139 const ListOfShape& theTools)
141 build(theObjects, theTools);
144 static void prepareShapes(const TopoDS_Shape& theShape,
145 TopTools_ListOfShape& theSimpleList)
147 if (theShape.ShapeType() != TopAbs_COMPOUND) {
148 theSimpleList.Append(theShape);
152 // explode compound on simple shapes to allow their intersections
153 TopoDS_Iterator It (theShape, Standard_True, Standard_True);
154 for (; It.More(); It.Next()) {
155 TopoDS_Shape curSh = It.Value();
156 prepareShapes(curSh, theSimpleList);
160 //=================================================================================================
161 void GeomAlgoAPI_Partition::build(const ListOfShape& theObjects,
162 const ListOfShape& theTools)
164 if (theObjects.empty()) {
168 // Creating partition operation.
169 GEOMAlgo_Splitter* anOperation = new GEOMAlgo_Splitter;
170 this->setImpl(anOperation);
171 this->setBuilderType(OCCT_BOPAlgo_Builder);
173 TopTools_MapOfShape ShapesMap;
175 for(ListOfShape::const_iterator anObjectsIt = theObjects.begin();
176 anObjectsIt != theObjects.end();
179 const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
180 // #2240: decompose compounds to get the valid result
181 TopTools_ListOfShape aSimpleShapes;
182 prepareShapes(aShape, aSimpleShapes);
183 TopTools_ListIteratorOfListOfShape aSimpleIter(aSimpleShapes);
184 for (; aSimpleIter.More(); aSimpleIter.Next()) {
185 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
186 if (ShapesMap.Add(aSimpleSh)) {
187 anOperation->AddArgument(aSimpleSh);
193 for (ListOfShape::const_iterator
194 aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++) {
195 const TopoDS_Shape& aShape = (*aToolsIt)->impl<TopoDS_Shape>();
196 // #2419: decompose compounds to get the valid result
197 TopTools_ListOfShape aSimpleShapes;
198 prepareShapes(aShape, aSimpleShapes);
199 TopTools_ListIteratorOfListOfShape aSimpleIter(aSimpleShapes);
200 for (; aSimpleIter.More(); aSimpleIter.Next()) {
201 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
202 if (ShapesMap.Add(aSimpleSh)) {
203 anOperation->AddTool(aSimpleSh);
208 // Building and getting result.
209 anOperation->Perform();
210 if (anOperation->HasErrors())
212 TopoDS_Shape aResult = anOperation->Shape();
214 if(aResult.ShapeType() == TopAbs_COMPOUND) {
215 // Exclude faces and edges which are shared as another sub-shape.
216 NCollection_Vector<TopoDS_Shape> aFaces;
217 NCollection_Vector<TopoDS_Shape> anEdges;
218 TopoDS_Compound aTempCompound;
219 TopoDS_Builder aBuilder;
220 aBuilder.MakeCompound(aTempCompound);
221 for(TopoDS_Iterator anIt(aResult);
224 const TopoDS_Shape& aSubShape = anIt.Value();
225 if (aSubShape.ShapeType() == TopAbs_FACE) {
226 aFaces.Append(aSubShape);
227 } else if (aSubShape.ShapeType() == TopAbs_EDGE) {
228 anEdges.Append(aSubShape);
230 aBuilder.Add(aTempCompound, aSubShape);
234 for (NCollection_Vector<TopoDS_Shape>::Iterator anIt(aFaces);
238 const TopoDS_Shape& aSubShape = anIt.Value();
239 if (!isSubShape(aTempCompound, aSubShape))
241 aBuilder.Add(aTempCompound, aSubShape);
245 for (NCollection_Vector<TopoDS_Shape>::Iterator anIt(anEdges);
249 const TopoDS_Shape& aSubShape = anIt.Value();
250 if (!isSubShape(aTempCompound, aSubShape))
252 aBuilder.Add(aTempCompound, aSubShape);
256 aResult = aTempCompound;
259 if(aResult.ShapeType() == TopAbs_COMPOUND) {
260 // sort sub-shapes of compound before creation of a compsolid
261 sortCompound(aResult, anOperation);
263 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
264 aGeomShape->setImpl(new TopoDS_Shape(aResult));
265 aResult = GeomAlgoAPI_ShapeTools::groupSharedTopology(aGeomShape)->impl<TopoDS_Shape>();
269 if(aResult.IsNull()) {
272 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
273 aShape->setImpl(new TopoDS_Shape(aResult));
274 this->setShape(aShape);