2 #include "HYDROData_SplitToZonesTool.h"
4 #include "HYDROData_Object.h"
6 #include <BRepAlgoAPI_Common.hxx>
7 #include <BRepAlgoAPI_Cut.hxx>
8 #include <BRepBuilderAPI_MakeFace.hxx>
10 #include <TopExp_Explorer.hxx>
12 #include <TopoDS_Iterator.hxx>
13 #include <TopoDS_Wire.hxx>
15 TopoDS_Face HYDROData_SplitToZonesTool::SplitData::Face() const
21 if ( Shape.ShapeType() == TopAbs_FACE )
23 aResFace = TopoDS::Face( Shape );
25 else if ( Shape.ShapeType() == TopAbs_WIRE )
27 BRepBuilderAPI_MakeFace aMakeFace( TopoDS::Wire( Shape ), Standard_True );
29 if( aMakeFace.IsDone() )
30 aResFace = aMakeFace.Face();
37 HYDROData_SplitToZonesTool::SplitDataList
38 HYDROData_SplitToZonesTool::SplitToZones( const HYDROData_SequenceOfObjects& theObjectList )
40 SplitDataList anOutputSplitDataList;
42 // Preparation. Collect the object shapes to split.
43 SplitDataList anInputSplitDataList;
44 for( int anIndex = 1, aLength = theObjectList.Length(); anIndex <= aLength; anIndex++ )
46 Handle(HYDROData_Object) aGeomObj =
47 Handle(HYDROData_Object)::DownCast( theObjectList.Value( anIndex ) );
48 if( !aGeomObj.IsNull() )
50 SplitData aSplitData( aGeomObj->GetTopShape(), aGeomObj->GetName() );
51 anInputSplitDataList.append( aSplitData );
55 // Step 1. Split the paths.
56 SplitDataListIterator anInputIter( anInputSplitDataList );
57 while( anInputIter.hasNext() )
59 const SplitData& anInputSplitData = anInputIter.next();
60 if( anOutputSplitDataList.isEmpty() )
61 anOutputSplitDataList.append( anInputSplitData );
64 SplitDataList aSplitDataList;
66 SplitDataList aSrcSplitDataList;
67 aSrcSplitDataList.append( anInputSplitData );
69 SplitDataList aDestSplitDataList = anOutputSplitDataList;
70 anOutputSplitDataList.clear();
72 while( !aDestSplitDataList.isEmpty() )
74 SplitData aSrcSplitData = aSrcSplitDataList.last();
76 SplitData aDestSplitData = aDestSplitDataList.first();
77 aDestSplitDataList.pop_front();
79 SplitData aData1Subtracted, aData2Subtracted, aDataIntersected;
80 if( SplitTwoData( aSrcSplitData, aDestSplitData,
81 aData1Subtracted, aData2Subtracted, aDataIntersected ) )
82 anOutputSplitDataList.append( aDataIntersected );
83 anOutputSplitDataList.append( aData2Subtracted );
84 aSrcSplitDataList.append( aData1Subtracted );
87 if( !aSrcSplitDataList.isEmpty() )
88 anOutputSplitDataList.append( aSrcSplitDataList.last() );
92 // Step 2. Take into account the boundary polyline.
94 if( !thePolyline.IsNull() )
96 TopoDS_Wire aWire = TopoDS::Wire( thePolyline->GetTopShape() );
99 BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
101 if( aMakeFace.IsDone() )
103 SplitData aBoundarySplitData( aMakeFace.Face(), "" );
105 SplitDataList aCutSplitDataList;
106 SplitDataListIterator anOutputIter( anOutputSplitDataList );
107 while( anOutputIter.hasNext() )
109 const SplitData& anOutputSplitData = anOutputIter.next();
111 SplitData aData1Subtracted, aData2Subtracted, aDataIntersected;
112 if( SplitTwoData( anOutputSplitData, aBoundarySplitData,
113 aData1Subtracted, aData2Subtracted, aDataIntersected ) )
114 aCutSplitDataList.append( aDataIntersected );
116 anOutputSplitDataList = aCutSplitDataList;
122 // Step 3. Extract the separate regions.
123 SplitDataList anExtractedSplitDataList;
124 SplitDataListIterator anOutputIter( anOutputSplitDataList );
125 while( anOutputIter.hasNext() )
127 const SplitData& anOutputSplitData = anOutputIter.next();
128 anExtractedSplitDataList.append( ExtractSeparateData( anOutputSplitData ) );
131 return anExtractedSplitDataList;
134 bool HYDROData_SplitToZonesTool::SplitTwoData( const SplitData& theData1,
135 const SplitData& theData2,
136 SplitData& theData1Subtracted,
137 SplitData& theData2Subtracted,
138 SplitData& theDataIntersected )
140 const TopoDS_Shape& aShape1 = theData1.Shape;
141 const TopoDS_Shape& aShape2 = theData2.Shape;
143 const QStringList& anObjectNames1 = theData1.ObjectNames;
144 const QStringList& anObjectNames2 = theData2.ObjectNames;
146 BRepAlgoAPI_Common aCommon( aShape1, aShape2 );
147 TopoDS_Shape aCommonShape = aCommon.Shape();
148 if( aCommonShape.IsNull() )
150 theData1Subtracted = theData1;
151 theData2Subtracted = theData2;
155 BRepAlgoAPI_Cut aCut1( aShape1, aShape2 );
156 TopoDS_Shape aCut1Shape = aCut1.Shape();
158 BRepAlgoAPI_Cut aCut2( aShape2, aShape1 );
159 TopoDS_Shape aCut2Shape = aCut2.Shape();
161 theData1Subtracted = SplitData( aCut1Shape, anObjectNames1 );
162 theData2Subtracted = SplitData( aCut2Shape, anObjectNames2 );
163 theDataIntersected = SplitData( aCommonShape, anObjectNames1 + anObjectNames2 );
168 HYDROData_SplitToZonesTool::SplitDataList
169 HYDROData_SplitToZonesTool::ExtractSeparateData( const SplitData& theData )
171 SplitDataList aSplitDataList;
172 TopExp_Explorer anExp( theData.Shape, TopAbs_FACE );
173 for( ; anExp.More(); anExp.Next() )
175 TopoDS_Shape aShape = anExp.Current();
176 if( aShape.ShapeType() == TopAbs_FACE )
178 TopoDS_Face aFace = TopoDS::Face( aShape );
179 if( !aFace.IsNull() )
181 SplitData aSplitData( aFace, theData.ObjectNames );
182 aSplitDataList.append( aSplitData );
186 return aSplitDataList;