2 #include "HYDROData_SplitToZonesTool.h"
4 #include "HYDROData_PolylineXY.h"
5 #include "HYDROData_ShapesGroup.h"
7 #include <BRepAlgoAPI_Common.hxx>
8 #include <BRepAlgoAPI_Cut.hxx>
9 #include <BRepBuilderAPI_MakeFace.hxx>
11 #include <TopExp_Explorer.hxx>
13 #include <TopoDS_Edge.hxx>
14 #include <TopoDS_Iterator.hxx>
15 #include <TopoDS_Wire.hxx>
16 #include <TopoDS_Iterator.hxx>
17 #include <BRepCheck_Analyzer.hxx>
18 #include <NCollection_IncAllocator.hxx>
19 #include <BOPAlgo_Builder.hxx>
20 #include <BOPAlgo_PaveFiller.hxx>
21 #include <NCollection_DataMap.hxx>
22 #include <TopTools_ListOfShape.hxx>
23 #include <TopTools_ListIteratorOfListOfShape.hxx>
24 #include <TopTools_ShapeMapHasher.hxx>
25 typedef NCollection_DataMap<TopoDS_Shape, TopTools_ListOfShape, TopTools_ShapeMapHasher> HYDROData_DataMapOfShapeListOfShape;
26 typedef HYDROData_DataMapOfShapeListOfShape::Iterator HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape;
27 typedef NCollection_DataMap<TopoDS_Shape, QStringList, TopTools_ShapeMapHasher> HYDROData_DataMapOfShapeListOfString;
28 typedef HYDROData_DataMapOfShapeListOfString::Iterator HYDROData_DataMapIteratorOfDataMapOfShapeListOfString;
29 #undef _NCollection_MapHasher
31 #define DEB_SPLIT_TO_ZONES 1
32 #ifdef DEB_SPLIT_TO_ZONES
33 #include <BRepTools.hxx>
35 TopoDS_Face HYDROData_SplitToZonesTool::SplitData::Face() const
41 if ( Shape.ShapeType() == TopAbs_FACE )
43 aResFace = TopoDS::Face( Shape );
45 else if ( Shape.ShapeType() == TopAbs_WIRE )
47 BRepBuilderAPI_MakeFace aMakeFace( TopoDS::Wire( Shape ), Standard_True );
49 if( aMakeFace.IsDone() )
50 aResFace = aMakeFace.Face();
57 HYDROData_SplitToZonesTool::SplitDataList
58 HYDROData_SplitToZonesTool::Split( const HYDROData_SequenceOfObjects& theObjectList,
59 const HYDROData_SequenceOfObjects& theGroupsList,
60 const Handle(HYDROData_PolylineXY)& thePolyline )
62 SplitDataList anOutputSplitDataList;
65 // Collect the object shapes to split. InputDataList will contain elements which will hold shape & name_of_shape.
66 SplitDataList anInputSplitDataList;
67 for( int anIndex = 1, aLength = theObjectList.Length(); anIndex <= aLength; anIndex++ )
69 Handle(HYDROData_Object) aGeomObj =
70 Handle(HYDROData_Object)::DownCast( theObjectList.Value( anIndex ) );
71 if( aGeomObj.IsNull() )
74 TopoDS_Shape aShape = aGeomObj->GetTopShape();
75 if ( aShape.IsNull() )
78 if ( aShape.ShapeType() == TopAbs_COMPOUND ) {
79 // Create split data for each face contained in the compound
80 TopExp_Explorer anExp( aShape, TopAbs_FACE );
81 for ( ; anExp.More(); anExp.Next() ) {
82 const TopoDS_Face& aFace = TopoDS::Face( anExp.Current() );
83 if ( !aFace.IsNull() ) {
84 SplitData aSplitData( SplitData::Data_Zone, aFace, aGeomObj->GetName() );
85 anInputSplitDataList.append( aSplitData );
89 SplitData aSplitData( SplitData::Data_Zone, aShape, aGeomObj->GetName() );
90 anInputSplitDataList.append( aSplitData );
94 SplitDataList anInputGroupList;
95 for( int anIndex = 1; anIndex <= theGroupsList.Length(); anIndex++ )
97 Handle(HYDROData_ShapesGroup) aGeomGroup =
98 Handle(HYDROData_ShapesGroup)::DownCast( theGroupsList.Value( anIndex ) );
99 if( aGeomGroup.IsNull() )
102 TopTools_SequenceOfShape aGroupShapes;
103 aGeomGroup->GetShapes( aGroupShapes );
104 for( int i = 1, aNbShapes = aGroupShapes.Length(); i <= aNbShapes; i++ ) {
105 const TopoDS_Shape& aGroupShape = aGroupShapes.Value( i );
106 if ( aGroupShape.IsNull() )
109 if ( aGroupShape.ShapeType() == TopAbs_COMPOUND ) {
110 TopExp_Explorer anExp( aGroupShape, TopAbs_EDGE );
111 for ( ; anExp.More(); anExp.Next() ) {
112 const TopoDS_Edge& anEdge = TopoDS::Edge( anExp.Current() );
113 if ( !anEdge.IsNull() ) {
114 SplitData aSplitData( SplitData::Data_Edge, anEdge, aGeomGroup->GetName() );
115 anInputGroupList.append( aSplitData );
119 SplitData aSplitData( SplitData::Data_Edge, aGroupShape, aGeomGroup->GetName() );
120 anInputGroupList.append( aSplitData );
125 HYDROData_DataMapOfShapeListOfString aDM3;
126 if(!anInputGroupList.isEmpty()) { // Old edge ==> List_Of_Names
127 QStringList aListOfNames;
128 for (int i=0;i < anInputGroupList.size() ;i++) {
129 const TopoDS_Shape& aSh = anInputGroupList.at(i).Shape;
130 aDM3.Bind(aSh, anInputGroupList.at(i).ObjectNames);
133 // Step 1. Prepare Partition structures.
134 Handle(NCollection_BaseAllocator) pA1 = new NCollection_IncAllocator, pA2 = new NCollection_IncAllocator;
135 BOPAlgo_PaveFiller* aPaveFiller = new BOPAlgo_PaveFiller(pA1);
136 BOPAlgo_Builder* aBuilder = new BOPAlgo_Builder(pA2);
137 BOPCol_ListOfShape aLS;
138 QStringList aListOfNames;
139 for (int i=0;i < anInputSplitDataList.size() ;i++) {
140 const TopoDS_Shape& aSh = anInputSplitDataList.at(i).Shape;
141 aDM3.Bind(aSh, anInputSplitDataList.at(i).ObjectNames);
144 aPaveFiller->SetArguments(aLS);
145 aPaveFiller->Perform();
146 Standard_Integer anErr = aPaveFiller->ErrorStatus();
148 return anOutputSplitDataList;
149 BOPDS_PDS pDS = aPaveFiller->PDS();
151 return anOutputSplitDataList;
154 // Step 2. Split faces
155 BOPCol_ListIteratorOfListOfShape anIt(aPaveFiller->Arguments());
156 for (; anIt.More(); anIt.Next()) {
157 const TopoDS_Shape& aS = anIt.Value();
158 aBuilder->AddArgument(aS);
160 aBuilder->PerformWithFiller(*aPaveFiller);
161 anErr = aBuilder->ErrorStatus();
163 return anOutputSplitDataList;
164 const TopoDS_Shape& aResult = aBuilder->Shape();
165 if (aResult.IsNull())
166 return anOutputSplitDataList;
167 BRepCheck_Analyzer aCheck (aResult);
168 if(!aCheck.IsValid()) {
169 #ifdef DEB_SPLIT_TO_ZONES
170 cout << "result is not valid" <<endl;
171 BRepTools::Write(aResult, "SplitFacesNV.brep");
173 return anOutputSplitDataList;
175 #ifdef DEB_SPLIT_TO_ZONES
176 //BRepTools::Write(aResult, "SplitFacesV.brep");
179 // Step 3. Collect history
180 HYDROData_DataMapOfShapeListOfShape aDM1;
182 //TCollection_AsciiString aNamM ("EdgM_");
183 //TCollection_AsciiString aNamG ("EdgG_");
184 for (int i =1;anIt.More();anIt.Next(),i++) {
185 const TopTools_ListOfShape& aListOfNew = aBuilder->Modified(anIt.Value());
186 TopTools_ListOfShape aList;
187 TopTools_ListIteratorOfListOfShape it(aListOfNew);
188 for(;it.More();it.Next())
189 aList.Append(it.Value());
190 // Bug in History: partition should give only modified entities! => temporary solution is used
191 const TopTools_ListOfShape& aListOfGen = aBuilder->Generated(anIt.Value());
192 it.Initialize(aListOfGen);
193 for(;it.More();it.Next())
194 aList.Append(it.Value());
196 aDM1.Bind(anIt.Value(), aList);
197 //TCollection_AsciiString aName;
198 if(!anInputGroupList.isEmpty()) { /* 1 */
200 TopExp_Explorer exp (anIt.Value(), TopAbs_EDGE);
201 for (int j =1;exp.More();exp.Next(),j++) {
202 const TopTools_ListOfShape& aListM = aBuilder->Modified(exp.Current());
203 //cout << "NB_EDGE_M = " << aListM.Extent() <<endl;
204 it.Initialize(aListM);
205 for(int k=1;it.More();it.Next(),k++) {
206 aList.Append(it.Value());
207 //aName = aNamM + i + j +k +".brep";
208 //BRepTools::Write(it.Value(),aName.ToCString());
210 const TopTools_ListOfShape& aListG = aBuilder->Generated(exp.Current());
211 it.Initialize(aListG);
212 for(int k=1;it.More();it.Next(),k++) {
213 aList.Append(it.Value());
214 //aName = aNamG + i + j +k +".brep";
215 //BRepTools::Write(it.Value(),aName.ToCString());
217 //cout << "NB_EDGE = " << aList.Extent() <<endl;
218 aDM1.Bind(exp.Current(), aList);
223 // aDM2: NewShape ==> ListOfOldShapes
224 HYDROData_DataMapOfShapeListOfShape aDM2;
225 HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape aMIt(aDM1);
226 for(;aMIt.More();aMIt.Next()) {
227 const TopoDS_Shape& aKey = aMIt.Key();
228 TopTools_ListOfShape aList;
230 const TopTools_ListOfShape& aListOfNew = aMIt.Value();
231 TopTools_ListIteratorOfListOfShape it(aListOfNew);
232 for(;it.More();it.Next()) {
233 if(!aDM2.IsBound(it.Value()))
234 aDM2.Bind(it.Value(), aList);
236 TopTools_ListOfShape& aList = aDM2.ChangeFind(it.Value());
241 //cout << "DM2 Ext = " <<aDM2.Extent() <<endl;
242 // Step 4. Fill output structure.
243 aMIt.Initialize(aDM2);
244 for(int i =1;aMIt.More();aMIt.Next(),i++) {
245 SplitData aDestSplitData;
246 const TopoDS_Shape& aKey = aMIt.Key(); //new
247 aDestSplitData.Shape = aKey;
248 if(aKey.ShapeType() == TopAbs_FACE)
249 aDestSplitData.Type = SplitData::Data_Zone;
251 aDestSplitData.Type = SplitData::Data_Edge;
253 QStringList aListOfNames; // names processing
254 const TopTools_ListOfShape& aListOfOld = aMIt.Value();
255 TopTools_ListIteratorOfListOfShape it(aListOfOld);
256 for(;it.More();it.Next()) {
257 const TopoDS_Shape& aSh = it.Value(); //old
258 if(aDM3.IsBound(aSh)) {
259 const QStringList& ObjectNames = aDM3.Find(aSh);
260 aListOfNames.append(ObjectNames);
264 if(aKey.ShapeType() == TopAbs_EDGE) {
265 QString aStr("EDGE_Of_Face_");
266 aStr = aStr.append(TCollection_AsciiString(i).ToCString());
267 const QStringList& ObjectNames = QStringList( aStr);
268 cout << "EDGE ==> " << ObjectNames.size() <<" " << aStr.toStdString()<<endl;
269 aListOfNames.append(ObjectNames);
271 aDestSplitData.ObjectNames = aListOfNames;
272 anOutputSplitDataList.append(aDestSplitData);
274 //cout << "anOutputSplitDataList = " <<anOutputSplitDataList.size() <<endl;
275 return anOutputSplitDataList;
278 SplitDataListIterator anInputIter( anInputSplitDataList );
279 while( anInputIter.hasNext() )
281 const SplitData& anInputSplitData = anInputIter.next();
282 if( anOutputSplitDataList.isEmpty() )
283 anOutputSplitDataList.append( anInputSplitData );
286 SplitDataList aSplitDataList;
288 SplitDataList aSrcSplitDataList;
289 aSrcSplitDataList.append( anInputSplitData );
291 SplitDataList aDestSplitDataList = anOutputSplitDataList;
292 anOutputSplitDataList.clear();
294 while( !aDestSplitDataList.isEmpty() )
296 SplitData aSrcSplitData = aSrcSplitDataList.last();
298 SplitData aDestSplitData = aDestSplitDataList.first();
299 aDestSplitDataList.pop_front();
301 SplitData aData1Subtracted, aData2Subtracted, aDataIntersected;
302 if( SplitTwoData( aSrcSplitData, aDestSplitData,
303 aData1Subtracted, aData2Subtracted, aDataIntersected ) )
304 anOutputSplitDataList.append( aDataIntersected );
305 anOutputSplitDataList.append( aData2Subtracted );
306 aSrcSplitDataList.append( aData1Subtracted );
309 if( !aSrcSplitDataList.isEmpty() )
310 anOutputSplitDataList.append( aSrcSplitDataList.last() );
314 // Step 2. Take into account the boundary polyline.
315 if( !thePolyline.IsNull() )
317 TopoDS_Wire aWire = TopoDS::Wire( thePolyline->GetShape() );
318 if( !aWire.IsNull() )
320 BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
322 if( aMakeFace.IsDone() )
324 SplitData aBoundarySplitData( SplitData::Data_Zone, aMakeFace.Face(), "" );
326 SplitDataList aCutSplitDataList;
327 SplitDataListIterator anOutputIter( anOutputSplitDataList );
328 while( anOutputIter.hasNext() )
330 const SplitData& anOutputSplitData = anOutputIter.next();
332 SplitData aData1Subtracted, aData2Subtracted, aDataIntersected;
333 if( SplitTwoData( anOutputSplitData, aBoundarySplitData,
334 aData1Subtracted, aData2Subtracted, aDataIntersected ) )
335 aCutSplitDataList.append( aDataIntersected );
337 anOutputSplitDataList = aCutSplitDataList;
342 // Step 3. Extract the separate regions.
343 SplitDataList anExtractedSplitDataList;
344 SplitDataListIterator anOutputIter( anOutputSplitDataList );
345 while( anOutputIter.hasNext() )
347 const SplitData& anOutputSplitData = anOutputIter.next();
348 anExtractedSplitDataList.append( ExtractSeparateData( anOutputSplitData ) );
351 // return anExtractedSplitDataList;
354 bool HYDROData_SplitToZonesTool::SplitTwoData( const SplitData& theData1,
355 const SplitData& theData2,
356 SplitData& theData1Subtracted,
357 SplitData& theData2Subtracted,
358 SplitData& theDataIntersected )
360 const TopoDS_Shape& aShape1 = theData1.Shape;
361 const TopoDS_Shape& aShape2 = theData2.Shape;
363 const QStringList& anObjectNames1 = theData1.ObjectNames;
364 const QStringList& anObjectNames2 = theData2.ObjectNames;
366 BRepAlgoAPI_Common aCommon( aShape1, aShape2 );
367 TopoDS_Shape aCommonShape = aCommon.Shape();
368 if( aCommonShape.IsNull() )
370 theData1Subtracted = theData1;
371 theData2Subtracted = theData2;
375 BRepAlgoAPI_Cut aCut1( aShape1, aShape2 );
376 TopoDS_Shape aCut1Shape = aCut1.Shape();
378 BRepAlgoAPI_Cut aCut2( aShape2, aShape1 );
379 TopoDS_Shape aCut2Shape = aCut2.Shape();
381 theData1Subtracted = SplitData( SplitData::Data_Zone, aCut1Shape, anObjectNames1 );
382 theData2Subtracted = SplitData( SplitData::Data_Zone, aCut2Shape, anObjectNames2 );
383 theDataIntersected = SplitData( SplitData::Data_Zone, aCommonShape, anObjectNames1 + anObjectNames2 );
388 HYDROData_SplitToZonesTool::SplitDataList
389 HYDROData_SplitToZonesTool::ExtractSeparateData( const SplitData& theData )
391 SplitDataList aSplitDataList;
392 TopExp_Explorer anExp( theData.Shape, TopAbs_FACE );
393 for( ; anExp.More(); anExp.Next() )
395 TopoDS_Shape aShape = anExp.Current();
396 if( aShape.ShapeType() == TopAbs_FACE )
398 TopoDS_Face aFace = TopoDS::Face( aShape );
399 if( !aFace.IsNull() )
401 SplitData aSplitData( SplitData::Data_Zone, aFace, theData.ObjectNames );
402 aSplitDataList.append( aSplitData );
406 return aSplitDataList;