2 #include "HYDROData_SplitToZonesTool.h"
4 #include "HYDROData_PolylineXY.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,
39 const Handle(HYDROData_PolylineXY)& thePolyline )
41 SplitDataList anOutputSplitDataList;
43 // Preparation. Collect the object shapes to split.
44 SplitDataList anInputSplitDataList;
45 for( int anIndex = 1, aLength = theObjectList.Length(); anIndex <= aLength; anIndex++ )
47 Handle(HYDROData_Object) aGeomObj =
48 Handle(HYDROData_Object)::DownCast( theObjectList.Value( anIndex ) );
49 if( aGeomObj.IsNull() )
52 TopoDS_Shape aShape = aGeomObj->GetTopShape();
53 if ( aShape.IsNull() )
56 if ( aShape.ShapeType() == TopAbs_COMPOUND ) {
57 // Create split data for each face contained in the compound
58 TopExp_Explorer anExp( aShape, TopAbs_FACE );
59 for ( ; anExp.More(); anExp.Next() ) {
60 TopoDS_Face aFace = TopoDS::Face( anExp.Current() );
61 if ( !aFace.IsNull() ) {
62 SplitData aSplitData( aFace, aGeomObj->GetName() );
63 anInputSplitDataList.append( aSplitData );
67 SplitData aSplitData( aShape, aGeomObj->GetName() );
68 anInputSplitDataList.append( aSplitData );
72 // Step 1. Split the paths.
73 SplitDataListIterator anInputIter( anInputSplitDataList );
74 while( anInputIter.hasNext() )
76 const SplitData& anInputSplitData = anInputIter.next();
77 if( anOutputSplitDataList.isEmpty() )
78 anOutputSplitDataList.append( anInputSplitData );
81 SplitDataList aSplitDataList;
83 SplitDataList aSrcSplitDataList;
84 aSrcSplitDataList.append( anInputSplitData );
86 SplitDataList aDestSplitDataList = anOutputSplitDataList;
87 anOutputSplitDataList.clear();
89 while( !aDestSplitDataList.isEmpty() )
91 SplitData aSrcSplitData = aSrcSplitDataList.last();
93 SplitData aDestSplitData = aDestSplitDataList.first();
94 aDestSplitDataList.pop_front();
96 SplitData aData1Subtracted, aData2Subtracted, aDataIntersected;
97 if( SplitTwoData( aSrcSplitData, aDestSplitData,
98 aData1Subtracted, aData2Subtracted, aDataIntersected ) )
99 anOutputSplitDataList.append( aDataIntersected );
100 anOutputSplitDataList.append( aData2Subtracted );
101 aSrcSplitDataList.append( aData1Subtracted );
104 if( !aSrcSplitDataList.isEmpty() )
105 anOutputSplitDataList.append( aSrcSplitDataList.last() );
109 // Step 2. Take into account the boundary polyline.
110 if( !thePolyline.IsNull() )
112 TopoDS_Wire aWire = TopoDS::Wire( thePolyline->GetShape() );
113 if( !aWire.IsNull() )
115 BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
117 if( aMakeFace.IsDone() )
119 SplitData aBoundarySplitData( aMakeFace.Face(), "" );
121 SplitDataList aCutSplitDataList;
122 SplitDataListIterator anOutputIter( anOutputSplitDataList );
123 while( anOutputIter.hasNext() )
125 const SplitData& anOutputSplitData = anOutputIter.next();
127 SplitData aData1Subtracted, aData2Subtracted, aDataIntersected;
128 if( SplitTwoData( anOutputSplitData, aBoundarySplitData,
129 aData1Subtracted, aData2Subtracted, aDataIntersected ) )
130 aCutSplitDataList.append( aDataIntersected );
132 anOutputSplitDataList = aCutSplitDataList;
137 // Step 3. Extract the separate regions.
138 SplitDataList anExtractedSplitDataList;
139 SplitDataListIterator anOutputIter( anOutputSplitDataList );
140 while( anOutputIter.hasNext() )
142 const SplitData& anOutputSplitData = anOutputIter.next();
143 anExtractedSplitDataList.append( ExtractSeparateData( anOutputSplitData ) );
146 return anExtractedSplitDataList;
149 bool HYDROData_SplitToZonesTool::SplitTwoData( const SplitData& theData1,
150 const SplitData& theData2,
151 SplitData& theData1Subtracted,
152 SplitData& theData2Subtracted,
153 SplitData& theDataIntersected )
155 const TopoDS_Shape& aShape1 = theData1.Shape;
156 const TopoDS_Shape& aShape2 = theData2.Shape;
158 const QStringList& anObjectNames1 = theData1.ObjectNames;
159 const QStringList& anObjectNames2 = theData2.ObjectNames;
161 BRepAlgoAPI_Common aCommon( aShape1, aShape2 );
162 TopoDS_Shape aCommonShape = aCommon.Shape();
163 if( aCommonShape.IsNull() )
165 theData1Subtracted = theData1;
166 theData2Subtracted = theData2;
170 BRepAlgoAPI_Cut aCut1( aShape1, aShape2 );
171 TopoDS_Shape aCut1Shape = aCut1.Shape();
173 BRepAlgoAPI_Cut aCut2( aShape2, aShape1 );
174 TopoDS_Shape aCut2Shape = aCut2.Shape();
176 theData1Subtracted = SplitData( aCut1Shape, anObjectNames1 );
177 theData2Subtracted = SplitData( aCut2Shape, anObjectNames2 );
178 theDataIntersected = SplitData( aCommonShape, anObjectNames1 + anObjectNames2 );
183 HYDROData_SplitToZonesTool::SplitDataList
184 HYDROData_SplitToZonesTool::ExtractSeparateData( const SplitData& theData )
186 SplitDataList aSplitDataList;
187 TopExp_Explorer anExp( theData.Shape, TopAbs_FACE );
188 for( ; anExp.More(); anExp.Next() )
190 TopoDS_Shape aShape = anExp.Current();
191 if( aShape.ShapeType() == TopAbs_FACE )
193 TopoDS_Face aFace = TopoDS::Face( aShape );
194 if( !aFace.IsNull() )
196 SplitData aSplitData( aFace, theData.ObjectNames );
197 aSplitDataList.append( aSplitData );
201 return aSplitDataList;