]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROData/HYDROData_SplitToZonesTool.cxx
Salome HOME
refs #768: local CS change for land covers
[modules/hydro.git] / src / HYDROData / HYDROData_SplitToZonesTool.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROData_SplitToZonesTool.h"
20
21 #include "HYDROData_PolylineXY.h"
22 #include "HYDROData_ShapesGroup.h"
23 #include <HYDROData_Transform.h>
24 #include <BRepAlgoAPI_Cut.hxx>
25 #include <BRepAlgoAPI_Common.hxx>
26 #include <BRepBuilderAPI_MakeFace.hxx>
27 #include <BRep_Builder.hxx>
28 #include <TopExp_Explorer.hxx>
29 #include <TopoDS.hxx>
30 #include <TopoDS_Shape.hxx>
31 #include <TopoDS_Compound.hxx>
32 #include <TopoDS_Wire.hxx>
33 #include <TopoDS_Edge.hxx>
34 #include <BRepCheck_Analyzer.hxx>
35 #include <TopTools_ListOfShape.hxx>
36 #include <TopTools_ListIteratorOfListOfShape.hxx>
37 #include <gp_Pln.hxx>
38 #include <BRepGProp.hxx>
39 #include <GProp_GProps.hxx>
40 #include <Geom_Plane.hxx>
41 #include <BRepBuilderAPI_FindPlane.hxx>
42
43 //#define DEB_SPLIT_TO_ZONES 1
44 //#define DEB_SPLIT_TO_ZONES_CHECK_PARTITION 1
45 #if (defined (DEB_SPLIT_TO_ZONES) || defined(DEB_SPLIT_TO_ZONES_CHECK_PARTITION))
46 #include <BRepTools.hxx>
47 static TCollection_AsciiString fileNameBefore("BeforeTranslation");
48 #endif
49
50 TopoDS_Face HYDROData_SplitToZonesTool::SplitData::Face() const
51 {
52   TopoDS_Face aResFace;
53
54   if( !Shape.IsNull() )
55   {
56     if ( Shape.ShapeType() == TopAbs_FACE )
57     {
58       aResFace = TopoDS::Face( Shape );
59     }
60     else if ( Shape.ShapeType() == TopAbs_WIRE )
61     {
62       BRepBuilderAPI_MakeFace aMakeFace( TopoDS::Wire( Shape ), Standard_True );
63       aMakeFace.Build();
64       if( aMakeFace.IsDone() )
65         aResFace = aMakeFace.Face();
66     }
67   }
68
69   return aResFace;
70 }
71
72 Standard_Boolean buildLimFace(const TopoDS_Wire& theBndWire, TopoDS_Face& outFace) 
73 {
74   GProp_GProps G;
75   BRepGProp::LinearProperties(theBndWire,G);
76   const gp_Pnt& aCPnt = G.CentreOfMass();
77   gp_Pln aPln;
78   BRepBuilderAPI_FindPlane fndPlane (theBndWire, Precision::Confusion());  
79   if(fndPlane.Found())
80     aPln = fndPlane.Plane()->Pln();
81   else
82     aPln = gp_Pln(aCPnt, gp::OZ().Direction());
83   BRepBuilderAPI_MakeFace  aMkFace(aPln, theBndWire);
84   if(aMkFace.IsDone()) {
85     outFace = aMkFace.Face();      
86     if(!outFace.IsNull()) {
87 #ifdef DEB_SPLIT_TO_ZONES
88 //      BRepTools::Write(limFace,"FL.brep");
89 #endif
90       return Standard_True;
91     }
92   }
93   return Standard_False;
94 }
95 //======================================================================================
96
97 void HYDROData_SplitToZonesTool::SetFileNames(const QString& theNameBefore, const QString& theNameAfter)
98 {
99 #ifdef DEB_SPLIT_TO_ZONES_CHECK_PARTITION
100   if(!theNameBefore.isNull() && !theNameBefore.isEmpty())
101   {
102     TCollection_AsciiString aName (theNameBefore.toStdString().data());
103     fileNameBefore = aName;
104   }
105   if(!theNameAfter.isNull() && !theNameAfter.isEmpty())
106   {
107     TCollection_AsciiString aName (theNameAfter.toStdString().data());
108     HYDROData_Transform::SetFileName (aName);
109   }
110 #endif
111 }
112 //======================================================================================
113 Standard_Integer HYDROData_SplitToZonesTool::SplitFaces(const TopoDS_Compound& theComp, 
114                                                         HYDROData_Transform& theTool)
115 {
116   theTool.Clear();
117   theTool.SetArgument(theComp);
118   theTool.Detect();
119   const Standard_Boolean bToTransform = theTool.ToTransform();  
120   theTool.SetToTransform(bToTransform);  
121   theTool.Perform();
122   Standard_Integer anErr = theTool.ErrorStatus();
123   return anErr;
124 }
125
126 //======================================================================================
127 HYDROData_SplitToZonesTool::SplitDataList
128   HYDROData_SplitToZonesTool::Split( const HYDROData_SequenceOfObjects&  theObjectList,
129                                      const HYDROData_SequenceOfObjects&  theGroupsList,
130                                      const Handle(HYDROData_PolylineXY)& thePolyline )
131 {
132   SplitDataList anOutputSplitDataList;
133   if(theObjectList.IsEmpty()) return anOutputSplitDataList;
134   // Preparation. 
135   // Collect the object shapes to split. InputDataList will contain elements which will hold shape & name_of_shape.
136   SplitDataList anInputSplitDataList;
137 #ifdef DEB_SPLIT_TO_ZONES
138   TCollection_AsciiString aNam("Faces_");
139 #endif
140   for( int anIndex = 1, aLength = theObjectList.Length(); anIndex <= aLength; anIndex++ )
141   {
142     Handle(HYDROData_Entity) anObj = theObjectList.Value( anIndex );
143     TopoDS_Shape aShape;
144
145     Handle(HYDROData_Object) aGeomObj = Handle(HYDROData_Object)::DownCast( anObj );
146     if( !aGeomObj.IsNull() ) {
147       aShape = aGeomObj->GetTopShape();
148     }
149
150     if ( aShape.IsNull() )
151       continue;  
152 #ifdef DEB_SPLIT_TO_ZONES
153    TCollection_AsciiString aName = aNam + anIndex + ".brep";
154    BRepTools::Write(aShape, aName.ToCString());
155 #endif
156     if ( aShape.ShapeType() == TopAbs_COMPOUND ) {
157       // Create split data for each face contained in the compound
158       TopExp_Explorer anExp( aShape, TopAbs_FACE );
159       for ( ; anExp.More(); anExp.Next() ) {
160         const TopoDS_Face& aFace = TopoDS::Face( anExp.Current() );
161         if ( !aFace.IsNull() ) {
162           SplitData aSplitData( SplitData::Data_Zone, aFace, anObj->GetName() );
163           anInputSplitDataList.append( aSplitData );
164         }
165       }
166     } else {
167       SplitData aSplitData( SplitData::Data_Zone, aShape, anObj->GetName() );
168       anInputSplitDataList.append( aSplitData );
169     }
170   }
171   //
172   SplitDataList anInputGroupList;
173   for( int anIndex = 1, aLength = theGroupsList.Length(); anIndex <=aLength; anIndex++ )
174   {
175     Handle(HYDROData_ShapesGroup) aGeomGroup = 
176       Handle(HYDROData_ShapesGroup)::DownCast( theGroupsList.Value( anIndex ) );
177     if( aGeomGroup.IsNull() )
178       continue;
179       
180     TopTools_SequenceOfShape aGroupShapes;
181     aGeomGroup->GetShapes( aGroupShapes );
182     for( int i = 1, aNbShapes = aGroupShapes.Length(); i <= aNbShapes; i++ ) {
183       const TopoDS_Shape& aGroupShape = aGroupShapes.Value( i );
184       if ( aGroupShape.IsNull() )
185         continue;  
186
187       if ( aGroupShape.ShapeType() == TopAbs_COMPOUND ) {    
188         TopExp_Explorer anExp( aGroupShape, TopAbs_EDGE );
189         for ( ; anExp.More(); anExp.Next() ) {
190           const TopoDS_Edge& anEdge = TopoDS::Edge( anExp.Current() );
191           if ( !anEdge.IsNull() ) {
192             SplitData aSplitData( SplitData::Data_Edge, anEdge, aGeomGroup->GetName() );
193             anInputGroupList.append( aSplitData );
194           }
195         }
196       } else {
197         SplitData aSplitData( SplitData::Data_Edge, aGroupShape, aGeomGroup->GetName() );
198         anInputGroupList.append( aSplitData );
199 #ifdef DEB_SPLIT_TO_ZONES
200         QString aStr = aSplitData.ObjectNames.join(" "); 
201         cout << "Group# = " <<anIndex <<" Nmb in grp = " << i<< " Names = "<<aStr.toStdString() << " size = " <<aSplitData.ObjectNames.size() <<endl;   
202 #endif
203       }
204     }
205   }
206   // If only one shape is given we don't split it 
207   // algorithm just returns the unpacked input data
208   bool limplus1Object(false);
209   if(theObjectList.Size() == 1 )  {
210     if(thePolyline.IsNull()) {
211       anOutputSplitDataList.append(anInputSplitDataList); 
212       if(!theGroupsList.IsEmpty() ) 
213         anOutputSplitDataList.append(anInputGroupList);
214       return anOutputSplitDataList;
215     } else
216         limplus1Object = true;// size =1 && hasLimits
217   }
218   HYDROData_DataMapOfShapeListOfString aDM3;
219   if(!anInputGroupList.isEmpty()) {// Old edge ==> List_Of_Names
220     QStringList aListOfNames;
221     for (int i=0;i < anInputGroupList.size() ;i++) {
222       const TopoDS_Shape& aSh = anInputGroupList.at(i).Shape;
223       aDM3.Add(aSh, anInputGroupList.at(i).ObjectNames);
224     }
225   }
226
227   // Step 1. Prepare Partition structures. 
228   TopoDS_Shape aResult;
229   BOPCol_ListOfShape aLS;  
230   QStringList aListOfNames;
231   TopoDS_Compound aCmp;
232   BRep_Builder aBB;
233   aBB.MakeCompound(aCmp);
234   for (int i=0;i < anInputSplitDataList.size() ;i++) {
235     const TopoDS_Shape& aSh = anInputSplitDataList.at(i).Shape;
236     aDM3.Add(aSh, anInputSplitDataList.at(i).ObjectNames);
237     aLS.Append(aSh);
238     aBB.Add(aCmp,aSh);
239 #ifdef DEB_SPLIT_TO_ZONES
240     //TCollection_AsciiString aName = aNam + i + ".brep";
241     //BRepTools::Write(aSh, aName.ToCString());
242 #endif
243   }
244 #ifdef DEB_SPLIT_TO_ZONES_CHECK_PARTITION
245   TCollection_AsciiString aNameBefore = fileNameBefore + ".brep";
246   BRepTools::Write(aCmp, aNameBefore.ToCString());
247 #endif
248
249   HYDROData_DataMapOfShapeListOfShape aDM1;
250   if(anInputSplitDataList.size() > 1) {
251     HYDROData_Transform splitTool; 
252     const Standard_Integer anErr = SplitFaces(aCmp, splitTool);    
253     if(anErr)
254       return anOutputSplitDataList;
255     aResult = splitTool.Shape();
256     if (aResult.IsNull()) 
257       return anOutputSplitDataList;
258     BRepCheck_Analyzer aCheck (aResult);
259     if(!aCheck.IsValid()) {
260   #ifdef DEB_SPLIT_TO_ZONES
261       cout << "result is not valid" <<endl;
262       BRepTools::Write(aResult, "SplitFacesNV.brep");  
263   #endif
264       return anOutputSplitDataList;
265     }
266   #ifdef DEB_SPLIT_TO_ZONES
267     BRepTools::Write(aResult, "SplitFacesV.brep");
268   #endif
269     
270     // Step 3. Collect history  
271     //HYDROData_DataMapOfShapeListOfShape aDM1;
272     BOPCol_ListIteratorOfListOfShape anIt(aLS); 
273   #ifdef DEB_SPLIT_TO_ZONES
274     TCollection_AsciiString aNamM ("EdgM_");
275     TCollection_AsciiString aNamG ("EdgG_");
276   #endif
277     for (int i =1;anIt.More();anIt.Next(),i++) {
278       Standard_Boolean foundF(Standard_False);
279       const TopTools_ListOfShape& aListOfNew = splitTool.Modified(anIt.Value());
280       if(!aListOfNew.IsEmpty())
281         foundF = Standard_True;
282
283       TopTools_ListOfShape aList;
284
285       TopTools_ListIteratorOfListOfShape it(aListOfNew);
286       for(;it.More();it.Next())       
287         aList.Append(it.Value());
288           /* *********************************************************************
289       // Bug in History: partition should give only modified entities! => temporary solution is used
290       //const TopTools_ListOfShape& aListOfGen = splitTool.Generated(anIt.Value());
291       //if(!aListOfGen.IsEmpty())
292         //foundF = Standard_True;
293       //it.Initialize(aListOfGen);    
294       //for(;it.More();it.Next())     
295        // aList.Append(it.Value());  
296            ********************************************************************* */
297       if(!foundF) // face is not modified
298         aList.Append (anIt.Value());
299       aDM1.Add(anIt.Value(), aList);
300   #ifdef DEB_SPLIT_TO_ZONES
301       TCollection_AsciiString aName;
302   #endif
303       if(!anInputGroupList.isEmpty() ) { // 1
304         TopExp_Explorer exp (anIt.Value(), TopAbs_EDGE);
305         for (int j =1;exp.More();exp.Next(),j++) {
306           aList.Clear();
307           Standard_Boolean foundE(Standard_False);
308           const TopTools_ListOfShape& aListM = splitTool.Modified(exp.Current());    
309           //cout << "NB_EDGE_M = " << aListM.Extent() <<endl;
310           if(aListM.Extent()) foundE = Standard_True;
311           it.Initialize(aListM);    
312           for(int k=1;it.More();it.Next(),k++) {    
313             aList.Append(it.Value());
314   #ifdef DEB_SPLIT_TO_ZONES
315             //aName = aNamM + i + j +k +".brep";
316             //BRepTools::Write(it.Value(),aName.ToCString());
317   #endif
318           }
319           /* *********************************************************************
320                   //const TopTools_ListOfShape& aListG = splitTool.Generated(exp.Current());
321           //if(aListG.Extent()) foundE = Standard_True;
322           //it.Initialize(aListG);    
323           //for(int k=1;it.More();it.Next(),k++)
324             //aList.Append(it.Value());
325           //cout << "NB_EDGE = " << aList.Extent() <<endl;
326                   ************************************************************************** */
327           if(!foundE) {
328             aList.Append (exp.Current());
329   #ifdef DEB_SPLIT_TO_ZONES
330             aName = aNamG + i + j +".brep";
331             BRepTools::Write(exp.Current(),aName.ToCString());
332             cout << aName.ToCString()<< " = " << exp.Current().TShape() <<endl;
333   #endif
334           }
335           aDM1.Add(exp.Current(), aList);
336         }
337       }
338     }
339   } else {
340       aResult = anInputSplitDataList.at(0).Shape; // get single input shape
341   }
342
343   // aDM2: NewShape ==> ListOfOldShapes
344   HYDROData_DataMapOfShapeListOfShape aDM2;
345   // make limiting face
346   HYDROData_DataMapOfShapeListOfShape aDM4;
347   Standard_Boolean hasLimits(Standard_False);
348   QString aBndName;
349   HYDROData_MapOfShape aBndView;
350   if (! thePolyline.IsNull()) {    
351     const TopoDS_Wire aBndWire = TopoDS::Wire(thePolyline->GetShape());
352     if(!aBndWire.IsNull()) {
353       TopoDS_Face limFace;       
354       if(buildLimFace(aBndWire, limFace)) {
355         TopoDS_Shape aComResult;
356         BRepAlgoAPI_Common mkCom(aResult, limFace);
357         if(mkCom.IsDone()) {
358           aComResult = mkCom.Shape();
359           BRepCheck_Analyzer aCheck (aComResult);
360           if(aCheck.IsValid()) {
361 #ifdef DEB_SPLIT_TO_ZONES
362             BRepTools::Write(aComResult,"CommonV.brep");
363             BRepTools::Write(limFace,"limFace.brep");
364 #endif    
365             aBndName = thePolyline->GetName();
366             hasLimits = Standard_True; // DM2 should be filled here
367             TopExp_Explorer exp (limFace, TopAbs_EDGE);
368             for (int i =1;exp.More();exp.Next(),i++) {
369               const TopoDS_Shape& anEdge = exp.Current();
370               if(anEdge.IsNull()) continue;
371               aBndView.Add(anEdge);
372               QStringList aListOfNames;
373               aListOfNames.append(aBndName);
374               aDM3.Add(anEdge, aListOfNames);
375               TopTools_ListOfShape aList;
376               aList.Append(anEdge);
377               aDM1.Add(anEdge,aList);
378             }
379             HYDROData_MapOfShape aView;
380             exp.Init (aResult, TopAbs_FACE);
381             for (int i =1;exp.More();exp.Next(),i++) {
382               const TopoDS_Shape& aFace = exp.Current();
383               if(!aFace.IsNull()) {
384                 const TopTools_ListOfShape& aListOfNew = mkCom.Modified(aFace);
385                 //cout << "Modified: " << aListOfNew.Extent() <<endl;                                    
386                 if(!aListOfNew.IsEmpty()) {
387                   aDM4.Add(aFace, aListOfNew);
388 #ifdef DEB_SPLIT_TO_ZONES
389                   //TCollection_AsciiString aName = aNam + i + ".brep";
390                   //BRepTools::Write(aListOfNew.Last(), aName.ToCString());                
391 #endif
392                 }
393                 else {
394                     if(!mkCom.IsDeleted(aFace)) {
395                       const TopTools_ListOfShape& aListOfGen = mkCom.Generated(aFace);    
396                       if(!aListOfGen.IsEmpty()) {
397                         /* aDM4.Bind(aFace, aListOfGen); ???   */
398 #ifdef DEB_SPLIT_TO_ZONES
399                         //TCollection_AsciiString aName = aNam + i + "g.brep";
400                         //BRepTools::Write(aListOfGen.Last(), aName.ToCString());
401 #endif
402                       }
403                        else {
404                         TopTools_ListOfShape aList; 
405                         aList.Append(aFace);
406                         aDM4.Add(aFace, aList); //the same face - not modified
407                        }
408                     }
409              }
410              TopExp_Explorer exp2 (aFace, TopAbs_EDGE);
411              for (int j =1;exp2.More();exp2.Next(),j++) {
412                const TopoDS_Shape& anEdge = exp2.Current();
413                if(!anEdge.IsNull()) {
414                  if(aView.Contains(anEdge)) continue;
415                  aView.Add(anEdge);
416                  const TopTools_ListOfShape& aListOfNewEd = mkCom.Modified(anEdge);                 
417                  if(!aListOfNewEd.IsEmpty())
418                    aDM4.Add(anEdge, aListOfNewEd);
419                  else {
420                    if(!mkCom.IsDeleted(anEdge)) {
421                      const TopTools_ListOfShape& aListOfGenEd = mkCom.Generated(anEdge);
422                      if(!aListOfGenEd.IsEmpty()) {
423                        /* aDM4.Bind(anEdge, aListOfGenEd); ???*/
424                      } else {
425                        TopTools_ListOfShape aList; 
426                        aList.Append(anEdge);
427                        aDM4.Add(anEdge, aList);//the same edge - not modified
428                      }
429                    }
430                  }
431                }
432              }
433             }
434             } //end DM4 filling (phase 1)
435             //cout << "DM4 Ext = " <<aDM4.Extent() <<endl;
436          // phase 2 (from tool)
437 #ifdef DEB_SPLIT_TO_ZONES
438          TCollection_AsciiString aNam("BndEd_");
439 #endif
440          TopExp_Explorer expt (limFace, TopAbs_EDGE);
441          for(int i =1;expt.More();expt.Next(),i++) {
442            const TopoDS_Shape& anEdge = expt.Current();
443            if(!anEdge.IsNull()) {
444              const TopTools_ListOfShape& aListOfNewEd = mkCom.Modified(anEdge);    
445 #ifdef DEB_SPLIT_TO_ZONES
446              TopTools_ListIteratorOfListOfShape itl(aListOfNewEd);
447              for(int j=1;itl.More();itl.Next(),j++) {
448                TCollection_AsciiString aName = aNam + i + "_" + j + ".brep";
449                BRepTools::Write(itl.Value(), aName.ToCString());
450                cout <<aName.ToCString()<<" = "<< itl.Value().TShape() <<endl;
451              }
452 #endif
453              if(!aListOfNewEd.IsEmpty())
454                aDM4.Add(anEdge, aListOfNewEd);
455              else {
456                if(!mkCom.IsDeleted(anEdge)) {
457                  const TopTools_ListOfShape& aListOfGenEd = mkCom.Generated(anEdge);
458                  if(!aListOfGenEd.IsEmpty()) {
459                    /* aDM4.Bind(anEdge, aListOfGenEd); ??? */
460                  } else {
461                    TopTools_ListOfShape aList; 
462                    aList.Append(anEdge);
463                    aDM4.Add(anEdge, aList);//the same edge - not modified
464                  }
465                }
466              }
467            }
468          }
469          //cout << "DM4 Ext = " <<aDM4.Extent() <<endl;
470          if(limplus1Object) {
471            // fill DM1 (old - new) and DM2 (new - old)
472            HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape mIt(aDM4);
473            for(;mIt.More();mIt.Next()) {
474              const TopoDS_Shape& aKey = mIt.Key();//old
475              TopTools_ListOfShape aList;
476              aList.Append(aKey);
477              const TopTools_ListOfShape& aListOfNew = mIt.Value();
478              aDM1.Add(aKey, aListOfNew);
479              TopTools_ListIteratorOfListOfShape it(aListOfNew);
480              for(;it.More();it.Next()) {
481                if(!aDM2.Contains(it.Value()))
482                  aDM2.Add(it.Value(), aList);
483                else {
484                  TopTools_ListOfShape& aList = aDM2.ChangeFromKey(it.Value());
485                  aList.Prepend(aKey);
486                }
487              }
488            }
489          } else {
490            HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape aMIt(aDM1); 
491            //DM4 contains Old - New after common op. DM1: old - new after Split op.
492            for(;aMIt.More();aMIt.Next()) {
493              const TopoDS_Shape& aKey = aMIt.Key();
494              TopTools_ListOfShape aList;
495              aList.Append(aKey);
496              const TopTools_ListOfShape& aListOfNew = aMIt.Value();
497              TopTools_ListIteratorOfListOfShape it(aListOfNew);
498              for(;it.More();it.Next()) {// iterate new: b1, b2, b3...
499                if(!aDM4.Contains(it.Value())) // bi - is deleted
500                  continue; // go to the next bi
501                else {
502                  const TopTools_ListOfShape& aListOfNew4 = aDM4.FindFromKey(it.Value());
503                   TopTools_ListIteratorOfListOfShape it4(aListOfNew4); // {c1, c2, c3,...}
504                   for(;it4.More();it4.Next()) {
505                     if(!aDM2.Contains(it4.Value()))
506                       aDM2.Add(it4.Value(), aList);
507                     else {
508                       TopTools_ListOfShape& aList = aDM2.ChangeFromKey(it4.Value());
509                       aList.Prepend(aKey);
510                     }
511                   }
512                }
513              }
514            }
515          }
516          } else {
517              hasLimits = Standard_False;
518 #ifdef DEB_SPLIT_TO_ZONES
519              BRepTools::Write(aComResult,"CommonNV.brep");
520 #endif    
521           }
522        }
523       }
524     }
525   }// end limits processing
526   if(!hasLimits) {
527     HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape aMIt(aDM1);
528     for(;aMIt.More();aMIt.Next()) {
529       const TopoDS_Shape& aKey = aMIt.Key();
530       TopTools_ListOfShape aList;
531       aList.Append(aKey);
532       const TopTools_ListOfShape& aListOfNew = aMIt.Value();
533       TopTools_ListIteratorOfListOfShape it(aListOfNew);
534       for(;it.More();it.Next()) {
535         if(!aDM2.Contains(it.Value()))
536           aDM2.Add(it.Value(), aList);
537         else {
538           TopTools_ListOfShape& aList = aDM2.ChangeFromKey(it.Value());
539           aList.Prepend(aKey);
540         }
541       }
542     }
543   }
544   // Step 4. Fill output structure.
545 #ifdef DEB_SPLIT_TO_ZONES
546   TCollection_AsciiString aNam4 ("SC_");
547 #endif  
548   HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape aMIt(aDM2);
549   for(int i =1;aMIt.More();aMIt.Next(),i++) {
550     SplitData aDestSplitData;
551     const TopoDS_Shape& aKey = aMIt.Key(); //new
552     aDestSplitData.Shape = aKey;
553     if(aKey.ShapeType() == TopAbs_FACE)
554       aDestSplitData.Type = SplitData::Data_Zone;
555     else {
556       aDestSplitData.Type = SplitData::Data_Edge;
557 #ifdef DEB_SPLIT_TO_ZONES
558       TCollection_AsciiString aName = aNam4 + i + ".brep";
559       BRepTools::Write(aKey,aName.ToCString());
560 #endif    
561     }
562
563     QStringList aListOfNames; // names processing
564     const TopTools_ListOfShape& aListOfOld = aMIt.Value();
565     TopTools_ListIteratorOfListOfShape it(aListOfOld);
566     for(int j =1;it.More();it.Next(),j++) {    
567       const TopoDS_Shape& aSh = it.Value(); //old
568       if(aDM3.Contains(aSh)) {
569         const QStringList& ObjectNames = aDM3.FindFromKey(aSh);    
570         aListOfNames.append(ObjectNames);
571 #ifdef DEB_SPLIT_TO_ZONES
572         TCollection_AsciiString aName = aNam4 + i +"_" + j + ".brep";
573         BRepTools::Write(aSh ,aName.ToCString());        
574 #endif    
575       }    else {
576 #ifdef DEB_SPLIT_TO_ZONES
577         TCollection_AsciiString aName = aNam4 +"__" + i +"_" + j + ".brep";
578         BRepTools::Write(aSh ,aName.ToCString());        
579         cout <<aName.ToCString()<<" = "<< aSh.TShape() <<endl;
580 #endif
581           if(aBndView.Contains(aSh) && hasLimits) {            
582             aListOfNames.append(aBndName);
583 #ifdef DEB_SPLIT_TO_ZONES
584             cout << " BndName = "<<aBndName.toStdString() <<endl; 
585 #endif
586           }
587       }
588     }
589
590     aDestSplitData.ObjectNames = aListOfNames;  
591     anOutputSplitDataList.append(aDestSplitData);    
592 #ifdef DEB_SPLIT_TO_ZONES
593     QString aStr = aDestSplitData.ObjectNames.join(" "); 
594     cout << "New# = " << i<< " Names = "<<aStr.toStdString() << " size = " <<aDestSplitData.ObjectNames.size() <<endl; 
595 #endif
596   }
597
598   return anOutputSplitDataList;
599 }
600
601 HYDROData_SplitToZonesTool::SplitDataList
602   HYDROData_SplitToZonesTool::Split( const HYDROData_SequenceOfObjects& theObjectList )
603 {
604   HYDROData_SequenceOfObjects aGeomGroups;
605   Handle(HYDROData_PolylineXY) aPolyline;
606
607   return Split( theObjectList, aGeomGroups, aPolyline );
608 }