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 // If is only one shape is given we do not process the intersection
126 // algorithm but just return the unpacked input data
127 if ( theObjectList.Length() == 1 )
129 anOutputSplitDataList << anInputSplitDataList << anInputGroupList;
130 return anOutputSplitDataList;
133 HYDROData_DataMapOfShapeListOfString aDM3;
134 if(!anInputGroupList.isEmpty()) {// Old edge ==> List_Of_Names
135 QStringList aListOfNames;
136 for (int i=0;i < anInputGroupList.size() ;i++) {
137 const TopoDS_Shape& aSh = anInputGroupList.at(i).Shape;
138 aDM3.Bind(aSh, anInputGroupList.at(i).ObjectNames);
141 // Step 1. Prepare Partition structures.
142 Handle(NCollection_BaseAllocator) pA1 = new NCollection_IncAllocator, pA2 = new NCollection_IncAllocator;
143 BOPAlgo_PaveFiller* aPaveFiller = new BOPAlgo_PaveFiller(pA1);
144 BOPAlgo_Builder* aBuilder = new BOPAlgo_Builder(pA2);
145 BOPCol_ListOfShape aLS;
146 QStringList aListOfNames;
147 for (int i=0;i < anInputSplitDataList.size() ;i++) {
148 const TopoDS_Shape& aSh = anInputSplitDataList.at(i).Shape;
149 aDM3.Bind(aSh, anInputSplitDataList.at(i).ObjectNames);
152 aPaveFiller->SetArguments(aLS);
153 aPaveFiller->Perform();
154 Standard_Integer anErr = aPaveFiller->ErrorStatus();
156 return anOutputSplitDataList;
157 BOPDS_PDS pDS = aPaveFiller->PDS();
159 return anOutputSplitDataList;
162 // Step 2. Split faces
163 BOPCol_ListIteratorOfListOfShape anIt(aPaveFiller->Arguments());
164 for (; anIt.More(); anIt.Next()) {
165 const TopoDS_Shape& aS = anIt.Value();
166 aBuilder->AddArgument(aS);
168 aBuilder->PerformWithFiller(*aPaveFiller);
169 anErr = aBuilder->ErrorStatus();
171 return anOutputSplitDataList;
172 const TopoDS_Shape& aResult = aBuilder->Shape();
173 if (aResult.IsNull())
174 return anOutputSplitDataList;
175 BRepCheck_Analyzer aCheck (aResult);
176 if(!aCheck.IsValid()) {
177 #ifdef DEB_SPLIT_TO_ZONES
178 cout << "result is not valid" <<endl;
179 BRepTools::Write(aResult, "SplitFacesNV.brep");
181 return anOutputSplitDataList;
183 #ifdef DEB_SPLIT_TO_ZONES
184 //BRepTools::Write(aResult, "SplitFacesV.brep");
187 // Step 3. Collect history
188 HYDROData_DataMapOfShapeListOfShape aDM1;
190 //TCollection_AsciiString aNamM ("EdgM_");
191 //TCollection_AsciiString aNamG ("EdgG_");
192 for (int i =1;anIt.More();anIt.Next(),i++) {
193 const TopTools_ListOfShape& aListOfNew = aBuilder->Modified(anIt.Value());
194 TopTools_ListOfShape aList;
195 TopTools_ListIteratorOfListOfShape it(aListOfNew);
196 for(;it.More();it.Next())
197 aList.Append(it.Value());
198 // Bug in History: partition should give only modified entities! => temporary solution is used
199 const TopTools_ListOfShape& aListOfGen = aBuilder->Generated(anIt.Value());
200 it.Initialize(aListOfGen);
201 for(;it.More();it.Next())
202 aList.Append(it.Value());
204 aDM1.Bind(anIt.Value(), aList);
205 //TCollection_AsciiString aName;
206 if(!anInputGroupList.isEmpty()) { /* 1 */
208 TopExp_Explorer exp (anIt.Value(), TopAbs_EDGE);
209 for (int j =1;exp.More();exp.Next(),j++) {
210 const TopTools_ListOfShape& aListM = aBuilder->Modified(exp.Current());
211 //cout << "NB_EDGE_M = " << aListM.Extent() <<endl;
212 it.Initialize(aListM);
213 for(int k=1;it.More();it.Next(),k++) {
214 aList.Append(it.Value());
215 //aName = aNamM + i + j +k +".brep";
216 //BRepTools::Write(it.Value(),aName.ToCString());
218 const TopTools_ListOfShape& aListG = aBuilder->Generated(exp.Current());
219 it.Initialize(aListG);
220 for(int k=1;it.More();it.Next(),k++) {
221 aList.Append(it.Value());
222 //aName = aNamG + i + j +k +".brep";
223 //BRepTools::Write(it.Value(),aName.ToCString());
225 //cout << "NB_EDGE = " << aList.Extent() <<endl;
226 aDM1.Bind(exp.Current(), aList);
231 // aDM2: NewShape ==> ListOfOldShapes
232 HYDROData_DataMapOfShapeListOfShape aDM2;
233 HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape aMIt(aDM1);
234 for(;aMIt.More();aMIt.Next()) {
235 const TopoDS_Shape& aKey = aMIt.Key();
236 TopTools_ListOfShape aList;
238 const TopTools_ListOfShape& aListOfNew = aMIt.Value();
239 TopTools_ListIteratorOfListOfShape it(aListOfNew);
240 for(;it.More();it.Next()) {
241 if(!aDM2.IsBound(it.Value()))
242 aDM2.Bind(it.Value(), aList);
244 TopTools_ListOfShape& aList = aDM2.ChangeFind(it.Value());
249 //cout << "DM2 Ext = " <<aDM2.Extent() <<endl;
250 // Step 4. Fill output structure.
251 aMIt.Initialize(aDM2);
252 for(int i =1;aMIt.More();aMIt.Next(),i++) {
253 SplitData aDestSplitData;
254 const TopoDS_Shape& aKey = aMIt.Key(); //new
255 aDestSplitData.Shape = aKey;
256 if(aKey.ShapeType() == TopAbs_FACE)
257 aDestSplitData.Type = SplitData::Data_Zone;
259 aDestSplitData.Type = SplitData::Data_Edge;
261 QStringList aListOfNames; // names processing
262 const TopTools_ListOfShape& aListOfOld = aMIt.Value();
263 TopTools_ListIteratorOfListOfShape it(aListOfOld);
264 for(;it.More();it.Next()) {
265 const TopoDS_Shape& aSh = it.Value(); //old
266 if(aDM3.IsBound(aSh)) {
267 const QStringList& ObjectNames = aDM3.Find(aSh);
268 aListOfNames.append(ObjectNames);
272 if(aKey.ShapeType() == TopAbs_EDGE) {
273 QString aStr("EDGE_Of_Face_");
274 aStr = aStr.append(TCollection_AsciiString(i).ToCString());
275 const QStringList& ObjectNames = QStringList( aStr);
276 cout << "EDGE ==> " << ObjectNames.size() <<" " << aStr.toStdString()<<endl;
277 aListOfNames.append(ObjectNames);
279 aDestSplitData.ObjectNames = aListOfNames;
280 anOutputSplitDataList.append(aDestSplitData);
282 //cout << "anOutputSplitDataList = " <<anOutputSplitDataList.size() <<endl;
283 return anOutputSplitDataList;
286 SplitDataListIterator anInputIter( anInputSplitDataList );
287 while( anInputIter.hasNext() )
289 const SplitData& anInputSplitData = anInputIter.next();
290 if( anOutputSplitDataList.isEmpty() )
291 anOutputSplitDataList.append( anInputSplitData );
294 SplitDataList aSplitDataList;
296 SplitDataList aSrcSplitDataList;
297 aSrcSplitDataList.append( anInputSplitData );
299 SplitDataList aDestSplitDataList = anOutputSplitDataList;
300 anOutputSplitDataList.clear();
302 while( !aDestSplitDataList.isEmpty() )
304 SplitData aSrcSplitData = aSrcSplitDataList.last();
306 SplitData aDestSplitData = aDestSplitDataList.first();
307 aDestSplitDataList.pop_front();
309 SplitData aData1Subtracted, aData2Subtracted, aDataIntersected;
310 if( SplitTwoData( aSrcSplitData, aDestSplitData,
311 aData1Subtracted, aData2Subtracted, aDataIntersected ) )
312 anOutputSplitDataList.append( aDataIntersected );
313 anOutputSplitDataList.append( aData2Subtracted );
314 aSrcSplitDataList.append( aData1Subtracted );
317 if( !aSrcSplitDataList.isEmpty() )
318 anOutputSplitDataList.append( aSrcSplitDataList.last() );
322 // Step 2. Take into account the boundary polyline.
323 if( !thePolyline.IsNull() )
325 TopoDS_Wire aWire = TopoDS::Wire( thePolyline->GetShape() );
326 if( !aWire.IsNull() )
328 BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
330 if( aMakeFace.IsDone() )
332 SplitData aBoundarySplitData( SplitData::Data_Zone, aMakeFace.Face(), "" );
334 SplitDataList aCutSplitDataList;
335 SplitDataListIterator anOutputIter( anOutputSplitDataList );
336 while( anOutputIter.hasNext() )
338 const SplitData& anOutputSplitData = anOutputIter.next();
340 SplitData aData1Subtracted, aData2Subtracted, aDataIntersected;
341 if( SplitTwoData( anOutputSplitData, aBoundarySplitData,
342 aData1Subtracted, aData2Subtracted, aDataIntersected ) )
343 aCutSplitDataList.append( aDataIntersected );
345 anOutputSplitDataList = aCutSplitDataList;
350 // Step 3. Extract the separate regions.
351 SplitDataList anExtractedSplitDataList;
352 SplitDataListIterator anOutputIter( anOutputSplitDataList );
353 while( anOutputIter.hasNext() )
355 const SplitData& anOutputSplitData = anOutputIter.next();
356 anExtractedSplitDataList.append( ExtractSeparateData( anOutputSplitData ) );
359 // return anExtractedSplitDataList;
362 bool HYDROData_SplitToZonesTool::SplitTwoData( const SplitData& theData1,
363 const SplitData& theData2,
364 SplitData& theData1Subtracted,
365 SplitData& theData2Subtracted,
366 SplitData& theDataIntersected )
368 const TopoDS_Shape& aShape1 = theData1.Shape;
369 const TopoDS_Shape& aShape2 = theData2.Shape;
371 const QStringList& anObjectNames1 = theData1.ObjectNames;
372 const QStringList& anObjectNames2 = theData2.ObjectNames;
374 BRepAlgoAPI_Common aCommon( aShape1, aShape2 );
375 TopoDS_Shape aCommonShape = aCommon.Shape();
376 if( aCommonShape.IsNull() )
378 theData1Subtracted = theData1;
379 theData2Subtracted = theData2;
383 BRepAlgoAPI_Cut aCut1( aShape1, aShape2 );
384 TopoDS_Shape aCut1Shape = aCut1.Shape();
386 BRepAlgoAPI_Cut aCut2( aShape2, aShape1 );
387 TopoDS_Shape aCut2Shape = aCut2.Shape();
389 theData1Subtracted = SplitData( SplitData::Data_Zone, aCut1Shape, anObjectNames1 );
390 theData2Subtracted = SplitData( SplitData::Data_Zone, aCut2Shape, anObjectNames2 );
391 theDataIntersected = SplitData( SplitData::Data_Zone, aCommonShape, anObjectNames1 + anObjectNames2 );
396 HYDROData_SplitToZonesTool::SplitDataList
397 HYDROData_SplitToZonesTool::ExtractSeparateData( const SplitData& theData )
399 SplitDataList aSplitDataList;
400 TopExp_Explorer anExp( theData.Shape, TopAbs_FACE );
401 for( ; anExp.More(); anExp.Next() )
403 TopoDS_Shape aShape = anExp.Current();
404 if( aShape.ShapeType() == TopAbs_FACE )
406 TopoDS_Face aFace = TopoDS::Face( aShape );
407 if( !aFace.IsNull() )
409 SplitData aSplitData( SplitData::Data_Zone, aFace, theData.ObjectNames );
410 aSplitDataList.append( aSplitData );
414 return aSplitDataList;