1 // Copyright (C) 2014-2020 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 email : webmaster.salome@opencascade.com
20 #include "GeomAlgoAPI_Partition.h"
22 #include <GeomAlgoAPI_DFLoader.h>
23 #include <GeomAlgoAPI_ShapeTools.h>
24 #include <GeomAlgoAPI_SortListOfShapes.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>
32 #include <BRepTools_History.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 void getHistorySupportedType(const TopoDS_Shape& theShape, TopTools_ListOfShape& theResult) {
49 if (BRepTools_History::IsSupportedType(theShape)) {
50 theResult.Append(theShape);
52 for (TopoDS_Iterator aSubIter(theShape); aSubIter.More(); aSubIter.Next()) {
53 getHistorySupportedType(aSubIter.Value(), theResult);
58 //=================================================================================================
59 // Operation is used for production of ordered sorting: generated/modified from the first argument
60 // must be located in hte result first, etc. THis is for the issue #2517
61 static void sortCompound(TopoDS_Shape& theCompound, GEOMAlgo_Splitter* theOperation)
63 TopoDS_Compound aResCompound;
64 TopoDS_Builder aBuilder;
65 aBuilder.MakeCompound(aResCompound);
67 TopTools_MapOfShape anAlreadyThere; // to avoid duplications if it was produced by two arguments
69 // flag to add to result also results which were not produced by any argument
70 bool aNotProduced = true;
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 // if argument was not modified, it is fully in the result
80 aProducedByArg.Add(argsIter.Value());
81 const TopTools_ListOfShape& aModified = theOperation->Modified(argsIter.Value());
82 for (TopTools_ListOfShape::Iterator aModIter(aModified); aModIter.More(); aModIter.Next()) {
83 aProducedByArg.Add(aModIter.Value());
85 const TopTools_ListOfShape& aGenerated = theOperation->Generated(argsIter.Value());
86 for (TopTools_ListOfShape::Iterator aGenIter(aGenerated); aGenIter.More(); aGenIter.Next())
88 aProducedByArg.Add(aGenIter.Value());
97 ListOfShape aCombiningShapes;
98 for (TopoDS_Iterator anIt(theCompound); anIt.More(); anIt.Next()) {
99 bool aProducedContains = false;
100 if (aNotProduced) { // collect all supported type-shapes of result
101 TopTools_ListOfShape allRes;
102 getHistorySupportedType(anIt.Value(), allRes);
103 for (TopTools_ListOfShape::Iterator aResIter(allRes); aResIter.More(); aResIter.Next()) {
104 if (aProducedByArg.Contains(aResIter.Value())) {
105 aProducedContains = true;
110 if ((!aNotProduced || aProducedContains) && anAlreadyThere.Add(anIt.Value())) {
111 GeomShapePtr aSub(new GeomAPI_Shape);
112 aSub->setImpl(new TopoDS_Shape(anIt.Value()));
113 aCombiningShapes.push_back(aSub);
117 // sort sub-shapes of compound to stabilize the sequence of the Partition's results
118 GeomAlgoAPI_SortListOfShapes::sort(aCombiningShapes);
120 for (ListOfShape::iterator anIt = aCombiningShapes.begin();
121 anIt != aCombiningShapes.end(); ++anIt)
122 aBuilder.Add(aResCompound, (*anIt)->impl<TopoDS_Shape>());
125 theCompound = aResCompound;
128 //=================================================================================================
129 GeomAlgoAPI_Partition::GeomAlgoAPI_Partition(const ListOfShape& theObjects,
130 const ListOfShape& theTools)
132 build(theObjects, theTools);
135 static void prepareShapes(const TopoDS_Shape& theShape,
136 TopTools_ListOfShape& theSimpleList)
138 if (theShape.ShapeType() != TopAbs_COMPOUND) {
139 theSimpleList.Append(theShape);
143 // explode compound on simple shapes to allow their intersections
144 TopoDS_Iterator It (theShape, Standard_True, Standard_True);
145 for (; It.More(); It.Next()) {
146 TopoDS_Shape curSh = It.Value();
147 prepareShapes(curSh, theSimpleList);
151 //=================================================================================================
152 void GeomAlgoAPI_Partition::build(const ListOfShape& theObjects,
153 const ListOfShape& theTools)
155 if (theObjects.empty()) {
159 // Creating partition operation.
160 GEOMAlgo_Splitter* anOperation = new GEOMAlgo_Splitter;
161 this->setImpl(anOperation);
162 this->setBuilderType(OCCT_BOPAlgo_Builder);
164 TopTools_MapOfShape ShapesMap;
166 for(ListOfShape::const_iterator anObjectsIt = theObjects.begin();
167 anObjectsIt != theObjects.end();
170 const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
171 // #2240: decompose compounds to get the valid result
172 TopTools_ListOfShape aSimpleShapes;
173 prepareShapes(aShape, aSimpleShapes);
174 TopTools_ListIteratorOfListOfShape aSimpleIter(aSimpleShapes);
175 for (; aSimpleIter.More(); aSimpleIter.Next()) {
176 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
177 if (ShapesMap.Add(aSimpleSh)) {
178 anOperation->AddArgument(aSimpleSh);
184 for (ListOfShape::const_iterator
185 aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++) {
186 const TopoDS_Shape& aShape = (*aToolsIt)->impl<TopoDS_Shape>();
187 // #2419: decompose compounds to get the valid result
188 TopTools_ListOfShape aSimpleShapes;
189 prepareShapes(aShape, aSimpleShapes);
190 TopTools_ListIteratorOfListOfShape aSimpleIter(aSimpleShapes);
191 for (; aSimpleIter.More(); aSimpleIter.Next()) {
192 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
193 if (ShapesMap.Add(aSimpleSh)) {
194 anOperation->AddTool(aSimpleSh);
199 // Set parallel processing mode (default is false)
200 Standard_Boolean bRunParallel = Standard_True;
201 anOperation->SetRunParallel(bRunParallel);
203 // Building and getting result.
204 anOperation->Perform();
205 if (anOperation->HasErrors())
207 TopoDS_Shape aResult = anOperation->Shape();
209 if(aResult.ShapeType() == TopAbs_COMPOUND) {
210 // Exclude faces and edges which are shared as another sub-shape.
211 NCollection_Vector<TopoDS_Shape> aFaces;
212 NCollection_Vector<TopoDS_Shape> anEdges;
213 TopoDS_Compound aTempCompound;
214 TopoDS_Builder aBuilder;
215 aBuilder.MakeCompound(aTempCompound);
216 for(TopoDS_Iterator anIt(aResult);
219 const TopoDS_Shape& aSubShape = anIt.Value();
220 if (aSubShape.ShapeType() == TopAbs_FACE) {
221 aFaces.Append(aSubShape);
222 } else if (aSubShape.ShapeType() == TopAbs_EDGE) {
223 anEdges.Append(aSubShape);
225 aBuilder.Add(aTempCompound, aSubShape);
229 for (NCollection_Vector<TopoDS_Shape>::Iterator anIt(aFaces);
233 const TopoDS_Shape& aSubShape = anIt.Value();
234 if (!isSubShape(aTempCompound, aSubShape))
236 aBuilder.Add(aTempCompound, aSubShape);
240 for (NCollection_Vector<TopoDS_Shape>::Iterator anIt(anEdges);
244 const TopoDS_Shape& aSubShape = anIt.Value();
245 if (!isSubShape(aTempCompound, aSubShape))
247 aBuilder.Add(aTempCompound, aSubShape);
251 aResult = aTempCompound;
254 if(aResult.ShapeType() == TopAbs_COMPOUND) {
255 // sort sub-shapes of compound before creation of a compsolid
256 sortCompound(aResult, anOperation);
258 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
259 aGeomShape->setImpl(new TopoDS_Shape(aResult));
260 aResult = GeomAlgoAPI_ShapeTools::groupSharedTopology(aGeomShape)->impl<TopoDS_Shape>();
264 if(aResult.IsNull()) {
267 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
268 aShape->setImpl(new TopoDS_Shape(aResult));
269 this->setShape(aShape);