2 #include "HYDROData_SplitToZonesTool.h"
4 #include "HYDROData_Polyline.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_Polyline)& 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() )
51 SplitData aSplitData( aGeomObj->GetTopShape(), aGeomObj->GetName() );
52 anInputSplitDataList.append( aSplitData );
56 // Step 1. Split the paths.
57 SplitDataListIterator anInputIter( anInputSplitDataList );
58 while( anInputIter.hasNext() )
60 const SplitData& anInputSplitData = anInputIter.next();
61 if( anOutputSplitDataList.isEmpty() )
62 anOutputSplitDataList.append( anInputSplitData );
65 SplitDataList aSplitDataList;
67 SplitDataList aSrcSplitDataList;
68 aSrcSplitDataList.append( anInputSplitData );
70 SplitDataList aDestSplitDataList = anOutputSplitDataList;
71 anOutputSplitDataList.clear();
73 while( !aDestSplitDataList.isEmpty() )
75 SplitData aSrcSplitData = aSrcSplitDataList.last();
77 SplitData aDestSplitData = aDestSplitDataList.first();
78 aDestSplitDataList.pop_front();
80 SplitData aData1Subtracted, aData2Subtracted, aDataIntersected;
81 if( SplitTwoData( aSrcSplitData, aDestSplitData,
82 aData1Subtracted, aData2Subtracted, aDataIntersected ) )
83 anOutputSplitDataList.append( aDataIntersected );
84 anOutputSplitDataList.append( aData2Subtracted );
85 aSrcSplitDataList.append( aData1Subtracted );
88 if( !aSrcSplitDataList.isEmpty() )
89 anOutputSplitDataList.append( aSrcSplitDataList.last() );
93 // 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;
121 // Step 3. Extract the separate regions.
122 SplitDataList anExtractedSplitDataList;
123 SplitDataListIterator anOutputIter( anOutputSplitDataList );
124 while( anOutputIter.hasNext() )
126 const SplitData& anOutputSplitData = anOutputIter.next();
127 anExtractedSplitDataList.append( ExtractSeparateData( anOutputSplitData ) );
130 return anExtractedSplitDataList;
133 bool HYDROData_SplitToZonesTool::SplitTwoData( const SplitData& theData1,
134 const SplitData& theData2,
135 SplitData& theData1Subtracted,
136 SplitData& theData2Subtracted,
137 SplitData& theDataIntersected )
139 const TopoDS_Shape& aShape1 = theData1.Shape;
140 const TopoDS_Shape& aShape2 = theData2.Shape;
142 const QStringList& anObjectNames1 = theData1.ObjectNames;
143 const QStringList& anObjectNames2 = theData2.ObjectNames;
145 BRepAlgoAPI_Common aCommon( aShape1, aShape2 );
146 TopoDS_Shape aCommonShape = aCommon.Shape();
147 if( aCommonShape.IsNull() )
149 theData1Subtracted = theData1;
150 theData2Subtracted = theData2;
154 BRepAlgoAPI_Cut aCut1( aShape1, aShape2 );
155 TopoDS_Shape aCut1Shape = aCut1.Shape();
157 BRepAlgoAPI_Cut aCut2( aShape2, aShape1 );
158 TopoDS_Shape aCut2Shape = aCut2.Shape();
160 theData1Subtracted = SplitData( aCut1Shape, anObjectNames1 );
161 theData2Subtracted = SplitData( aCut2Shape, anObjectNames2 );
162 theDataIntersected = SplitData( aCommonShape, anObjectNames1 + anObjectNames2 );
167 HYDROData_SplitToZonesTool::SplitDataList
168 HYDROData_SplitToZonesTool::ExtractSeparateData( const SplitData& theData )
170 SplitDataList aSplitDataList;
171 TopExp_Explorer anExp( theData.Shape, TopAbs_FACE );
172 for( ; anExp.More(); anExp.Next() )
174 TopoDS_Shape aShape = anExp.Current();
175 if( aShape.ShapeType() == TopAbs_FACE )
177 TopoDS_Face aFace = TopoDS::Face( aShape );
178 if( !aFace.IsNull() )
180 SplitData aSplitData( aFace, theData.ObjectNames );
181 aSplitDataList.append( aSplitData );
185 return aSplitDataList;