]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROData/HYDROData_SplitToZonesTool.cxx
Salome HOME
split zones (draft)
[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 #include <BOPAlgo_BOP.hxx>
44 #include <BOPAlgo_Builder.hxx>
45
46 //#define DEB_SPLIT_TO_ZONES 1
47 //#define DEB_SPLIT_TO_ZONES_CHECK_PARTITION 1
48 #if (defined (DEB_SPLIT_TO_ZONES) || defined(DEB_SPLIT_TO_ZONES_CHECK_PARTITION))
49 #include <BRepTools.hxx>
50 static TCollection_AsciiString fileNameBefore("BeforeTranslation");
51 #endif
52
53 #define _DEVDEBUG_
54 #include "HYDRO_trace.hxx"
55
56 TopoDS_Face HYDROData_SplitToZonesTool::SplitData::Face() const
57 {
58   TopoDS_Face aResFace;
59
60   if( !Shape.IsNull() )
61   {
62     if ( Shape.ShapeType() == TopAbs_FACE )
63     {
64       aResFace = TopoDS::Face( Shape );
65     }
66     else if ( Shape.ShapeType() == TopAbs_WIRE )
67     {
68       BRepBuilderAPI_MakeFace aMakeFace( TopoDS::Wire( Shape ), Standard_True );
69       aMakeFace.Build();
70       if( aMakeFace.IsDone() )
71         aResFace = aMakeFace.Face();
72     }
73   }
74
75   return aResFace;
76 }
77
78 Standard_Boolean buildLimFace(const TopoDS_Wire& theBndWire, TopoDS_Face& outFace) 
79 {
80   GProp_GProps G;
81   BRepGProp::LinearProperties(theBndWire,G);
82   const gp_Pnt& aCPnt = G.CentreOfMass();
83   gp_Pln aPln;
84   BRepBuilderAPI_FindPlane fndPlane (theBndWire, Precision::Confusion());  
85   if(fndPlane.Found())
86     aPln = fndPlane.Plane()->Pln();
87   else
88     aPln = gp_Pln(aCPnt, gp::OZ().Direction());
89   BRepBuilderAPI_MakeFace  aMkFace(aPln, theBndWire);
90   if(aMkFace.IsDone()) {
91     outFace = aMkFace.Face();      
92     if(!outFace.IsNull()) {
93 #ifdef DEB_SPLIT_TO_ZONES
94 //      BRepTools::Write(limFace,"FL.brep");
95 #endif
96       return Standard_True;
97     }
98   }
99   return Standard_False;
100 }
101 //======================================================================================
102
103 void HYDROData_SplitToZonesTool::SetFileNames(const QString& theNameBefore, const QString& theNameAfter)
104 {
105 #ifdef DEB_SPLIT_TO_ZONES_CHECK_PARTITION
106   if(!theNameBefore.isNull() && !theNameBefore.isEmpty())
107   {
108     TCollection_AsciiString aName (theNameBefore.toStdString().data());
109     fileNameBefore = aName;
110   }
111   if(!theNameAfter.isNull() && !theNameAfter.isEmpty())
112   {
113     TCollection_AsciiString aName (theNameAfter.toStdString().data());
114     HYDROData_Transform::SetFileName (aName);
115   }
116 #endif
117 }
118 //======================================================================================
119 Standard_Integer HYDROData_SplitToZonesTool::SplitFaces(const TopoDS_Compound& theComp, 
120                                                         HYDROData_Transform& theTool)
121 {
122   theTool.Clear();
123   theTool.SetArgument(theComp);
124   theTool.Detect();
125   const Standard_Boolean bToTransform = theTool.ToTransform();  
126   theTool.SetToTransform(bToTransform);  
127   theTool.Perform();
128   Standard_Integer anErr = theTool.ErrorStatus();
129   return anErr;
130 }
131
132 //======================================================================================
133 HYDROData_SplitToZonesTool::SplitDataList
134   HYDROData_SplitToZonesTool::Split( const HYDROData_SequenceOfObjects&  theObjectList,
135                                      const HYDROData_SequenceOfObjects&  theGroupsList,
136                                      const Handle(HYDROData_PolylineXY)& thePolyline,
137                                      const HYDROData_SequenceOfObjects& InterPolys)
138 {
139   DEBTRACE("Split");
140   SplitDataList anOutputSplitDataList;
141   if(theObjectList.IsEmpty()) return anOutputSplitDataList;
142   // Preparation. 
143   // Collect the object shapes to split. InputDataList will contain elements which will hold shape & name_of_shape.
144   SplitDataList anInputSplitDataList;
145 #ifdef DEB_SPLIT_TO_ZONES
146   TCollection_AsciiString aNam("Faces_");
147 #endif
148   for( int anIndex = 1, aLength = theObjectList.Length(); anIndex <= aLength; anIndex++ )
149   {
150     Handle(HYDROData_Entity) anObj = theObjectList.Value( anIndex );
151     TopoDS_Shape aShape;
152
153     Handle(HYDROData_Object) aGeomObj = Handle(HYDROData_Object)::DownCast( anObj );
154     if( !aGeomObj.IsNull() ) {
155       aShape = aGeomObj->GetTopShape();
156     }
157
158     if ( aShape.IsNull() )
159       continue;  
160 #ifdef DEB_SPLIT_TO_ZONES
161    TCollection_AsciiString aName = aNam + anIndex + "_a.brep";
162    BRepTools::Write(aShape, aName.ToCString());
163 #endif
164     if ( aShape.ShapeType() == TopAbs_COMPOUND ) {
165       // Create split data for each face contained in the compound
166       TopExp_Explorer anExp( aShape, TopAbs_FACE );
167       for ( ; anExp.More(); anExp.Next() ) {
168         const TopoDS_Face& aFace = TopoDS::Face( anExp.Current() );
169         if ( !aFace.IsNull() ) {
170           SplitData aSplitData( SplitData::Data_Zone, aFace, anObj->GetName() );
171           anInputSplitDataList.append( aSplitData );
172         }
173       }
174     } else {
175       SplitData aSplitData( SplitData::Data_Zone, aShape, anObj->GetName() );
176       anInputSplitDataList.append( aSplitData );
177     }
178   }
179   //
180   SplitDataList anInputGroupList;
181   for( int anIndex = 1, aLength = theGroupsList.Length(); anIndex <=aLength; anIndex++ )
182   {
183     Handle(HYDROData_ShapesGroup) aGeomGroup = 
184       Handle(HYDROData_ShapesGroup)::DownCast( theGroupsList.Value( anIndex ) );
185     if( aGeomGroup.IsNull() )
186       continue;
187       
188     TopTools_SequenceOfShape aGroupShapes;
189     aGeomGroup->GetShapes( aGroupShapes );
190     for( int i = 1, aNbShapes = aGroupShapes.Length(); i <= aNbShapes; i++ ) {
191       const TopoDS_Shape& aGroupShape = aGroupShapes.Value( i );
192       if ( aGroupShape.IsNull() )
193         continue;  
194
195       if ( aGroupShape.ShapeType() == TopAbs_COMPOUND ) {    
196         TopExp_Explorer anExp( aGroupShape, TopAbs_EDGE );
197         for ( ; anExp.More(); anExp.Next() ) {
198           const TopoDS_Edge& anEdge = TopoDS::Edge( anExp.Current() );
199           if ( !anEdge.IsNull() ) {
200             SplitData aSplitData( SplitData::Data_Edge, anEdge, aGeomGroup->GetName() );
201             anInputGroupList.append( aSplitData );
202           }
203         }
204       } else {
205         SplitData aSplitData( SplitData::Data_Edge, aGroupShape, aGeomGroup->GetName() );
206         anInputGroupList.append( aSplitData );
207 #ifdef DEB_SPLIT_TO_ZONES
208         QString aStr = aSplitData.ObjectNames.join(" "); 
209         DEBTRACE("Group# = " <<anIndex <<" Nmb in grp = " << i<< " Names = "<<aStr.toStdString() << " size = " <<aSplitData.ObjectNames.size());
210 #endif
211       }
212     }
213   }
214   // If only one shape is given we don't split it 
215   // algorithm just returns the unpacked input data
216   bool limplus1Object(false);
217   if(theObjectList.Size() == 1 )  {
218     if(thePolyline.IsNull()) {
219       anOutputSplitDataList.append(anInputSplitDataList); 
220       if(!theGroupsList.IsEmpty() ) 
221         anOutputSplitDataList.append(anInputGroupList);
222       return anOutputSplitDataList;
223     } else
224         limplus1Object = true;// size =1 && hasLimits
225   }
226   HYDROData_DataMapOfShapeListOfString aDM3;
227   if(!anInputGroupList.isEmpty()) {// Old edge ==> List_Of_Names
228     QStringList aListOfNames;
229     for (int i=0;i < anInputGroupList.size() ;i++) {
230       const TopoDS_Shape& aSh = anInputGroupList.at(i).Shape;
231       aDM3.Add(aSh, anInputGroupList.at(i).ObjectNames);
232     }
233   }
234
235   // Step 1. Prepare Partition structures. 
236   TopoDS_Shape aResult;
237   BOPCol_ListOfShape aLS;  
238   QStringList aListOfNames;
239   TopoDS_Compound aCmp;
240   BRep_Builder aBB;
241   aBB.MakeCompound(aCmp);
242   for (int i=0;i < anInputSplitDataList.size() ;i++) {
243     const TopoDS_Shape& aSh = anInputSplitDataList.at(i).Shape;
244     aDM3.Add(aSh, anInputSplitDataList.at(i).ObjectNames);
245     aLS.Append(aSh);
246     aBB.Add(aCmp,aSh);
247 #ifdef DEB_SPLIT_TO_ZONES
248     TCollection_AsciiString aName = aNam + i + "_b.brep";
249     BRepTools::Write(aSh, aName.ToCString());
250 #endif
251   }
252 #ifdef DEB_SPLIT_TO_ZONES_CHECK_PARTITION
253   TCollection_AsciiString aNameBefore = fileNameBefore + "_c.brep";
254   BRepTools::Write(aCmp, aNameBefore.ToCString());
255 #endif
256
257   HYDROData_DataMapOfShapeListOfShape aDM1;
258   if(anInputSplitDataList.size() > 1) {
259     HYDROData_Transform splitTool; 
260     const Standard_Integer anErr = SplitFaces(aCmp, splitTool);    
261     if(anErr)
262       return anOutputSplitDataList;
263     aResult = splitTool.Shape();
264     if (aResult.IsNull()) 
265       return anOutputSplitDataList;
266     BRepCheck_Analyzer aCheck (aResult);
267     if(!aCheck.IsValid()) {
268   #ifdef DEB_SPLIT_TO_ZONES
269       DEBTRACE("result is not valid");
270       BRepTools::Write(aResult, "SplitFacesNV.brep");  
271   #endif
272       return anOutputSplitDataList;
273     }
274   #ifdef DEB_SPLIT_TO_ZONES
275     BRepTools::Write(aResult, "SplitFacesV.brep");
276   #endif
277     
278     // Step 3. Collect history  
279     //HYDROData_DataMapOfShapeListOfShape aDM1;
280     BOPCol_ListIteratorOfListOfShape anIt(aLS); 
281   #ifdef DEB_SPLIT_TO_ZONES
282     TCollection_AsciiString aNamM ("EdgM_");
283     TCollection_AsciiString aNamG ("EdgG_");
284   #endif
285     for (int i =1;anIt.More();anIt.Next(),i++) {
286       Standard_Boolean foundF(Standard_False);
287       const TopTools_ListOfShape& aListOfNew = splitTool.Modified(anIt.Value());
288       if(!aListOfNew.IsEmpty())
289         foundF = Standard_True;
290
291       TopTools_ListOfShape aList;
292
293       TopTools_ListIteratorOfListOfShape it(aListOfNew);
294       for(;it.More();it.Next())       
295         aList.Append(it.Value());
296           /* *********************************************************************
297       // Bug in History: partition should give only modified entities! => temporary solution is used
298       //const TopTools_ListOfShape& aListOfGen = splitTool.Generated(anIt.Value());
299       //if(!aListOfGen.IsEmpty())
300         //foundF = Standard_True;
301       //it.Initialize(aListOfGen);    
302       //for(;it.More();it.Next())     
303        // aList.Append(it.Value());  
304            ********************************************************************* */
305       if(!foundF) // face is not modified
306         aList.Append (anIt.Value());
307       aDM1.Add(anIt.Value(), aList);
308   #ifdef DEB_SPLIT_TO_ZONES
309       TCollection_AsciiString aName;
310   #endif
311       if(!anInputGroupList.isEmpty() ) { // 1
312         TopExp_Explorer exp (anIt.Value(), TopAbs_EDGE);
313         for (int j =1;exp.More();exp.Next(),j++) {
314           aList.Clear();
315           Standard_Boolean foundE(Standard_False);
316           const TopTools_ListOfShape& aListM = splitTool.Modified(exp.Current());    
317           DEBTRACE("NB_EDGE_M = " << aListM.Extent());
318           if(aListM.Extent()) foundE = Standard_True;
319           it.Initialize(aListM);    
320           for(int k=1;it.More();it.Next(),k++) {    
321             aList.Append(it.Value());
322   #ifdef DEB_SPLIT_TO_ZONES
323             aName = aNamM + i + j +k +"_d.brep";
324             BRepTools::Write(it.Value(),aName.ToCString());
325   #endif
326           }
327           /* *********************************************************************
328                   //const TopTools_ListOfShape& aListG = splitTool.Generated(exp.Current());
329           //if(aListG.Extent()) foundE = Standard_True;
330           //it.Initialize(aListG);    
331           //for(int k=1;it.More();it.Next(),k++)
332             //aList.Append(it.Value());
333           //cout << "NB_EDGE = " << aList.Extent() <<endl;
334                   ************************************************************************** */
335           if(!foundE) {
336             aList.Append (exp.Current());
337   #ifdef DEB_SPLIT_TO_ZONES
338             aName = aNamG + i + j +"_e.brep";
339             BRepTools::Write(exp.Current(),aName.ToCString());
340             DEBTRACE(aName.ToCString()<< " = " << exp.Current().TShape());
341   #endif
342           }
343           aDM1.Add(exp.Current(), aList);
344         }
345       }
346     }
347   } else {
348       aResult = anInputSplitDataList.at(0).Shape; // get single input shape
349   }
350
351   // aDM2: NewShape ==> ListOfOldShapes
352   HYDROData_DataMapOfShapeListOfShape aDM2;
353   // make limiting face
354   HYDROData_DataMapOfShapeListOfShape aDM4;
355   Standard_Boolean hasLimits(Standard_False);
356   QString aBndName;
357   HYDROData_MapOfShape aBndView;
358   if (! thePolyline.IsNull()) {
359     Handle(TopTools_HSequenceOfShape) aConnectedWires = new TopTools_HSequenceOfShape;
360     int nbWires = thePolyline->GetNbConnectedWires(aConnectedWires);
361     const TopoDS_Wire aBndWire = TopoDS::Wire(aConnectedWires->Value(1));
362     if(!aBndWire.IsNull()) {
363       TopoDS_Face limFace;       
364       if(buildLimFace(aBndWire, limFace)) {
365         TopoDS_Shape aComResult;
366         BRepAlgoAPI_Common mkCom(aResult, limFace);
367         if(mkCom.IsDone()) {
368           aComResult = mkCom.Shape();
369           BRepCheck_Analyzer aCheck (aComResult);
370           if(aCheck.IsValid()) {
371 #ifdef DEB_SPLIT_TO_ZONES
372             BRepTools::Write(aComResult,"CommonV.brep");
373             BRepTools::Write(limFace,"limFace.brep");
374 #endif    
375             aBndName = thePolyline->GetName();
376             hasLimits = Standard_True; // DM2 should be filled here
377             TopExp_Explorer exp (limFace, TopAbs_EDGE);
378             for (int i =1;exp.More();exp.Next(),i++) {
379               const TopoDS_Shape& anEdge = exp.Current();
380               if(anEdge.IsNull()) continue;
381               aBndView.Add(anEdge);
382               QStringList aListOfNames;
383               aListOfNames.append(aBndName);
384               aDM3.Add(anEdge, aListOfNames);
385               TopTools_ListOfShape aList;
386               aList.Append(anEdge);
387               aDM1.Add(anEdge,aList);
388             }
389             HYDROData_MapOfShape aView;
390             exp.Init (aResult, TopAbs_FACE);
391             for (int i =1;exp.More();exp.Next(),i++) {
392               const TopoDS_Shape& aFace = exp.Current();
393               if(!aFace.IsNull()) {
394                 const TopTools_ListOfShape& aListOfNew = mkCom.Modified(aFace);
395                 DEBTRACE("Modified: " << aListOfNew.Extent());
396                 if(!aListOfNew.IsEmpty()) {
397                   aDM4.Add(aFace, aListOfNew);
398 #ifdef DEB_SPLIT_TO_ZONES
399                   TCollection_AsciiString aName = aNam + i + "_f.brep";
400                   BRepTools::Write(aListOfNew.Last(), aName.ToCString());
401 #endif
402                 }
403                 else {
404                     if(!mkCom.IsDeleted(aFace)) {
405                       const TopTools_ListOfShape& aListOfGen = mkCom.Generated(aFace);    
406                       if(!aListOfGen.IsEmpty()) {
407                         /* aDM4.Bind(aFace, aListOfGen); ???   */
408 #ifdef DEB_SPLIT_TO_ZONES
409                         TCollection_AsciiString aName = aNam + i + "_g.brep";
410                         BRepTools::Write(aListOfGen.Last(), aName.ToCString());
411 #endif
412                       }
413                        else {
414                         TopTools_ListOfShape aList; 
415                         aList.Append(aFace);
416                         aDM4.Add(aFace, aList); //the same face - not modified
417                        }
418                     }
419              }
420              TopExp_Explorer exp2 (aFace, TopAbs_EDGE);
421              for (int j =1;exp2.More();exp2.Next(),j++) {
422                const TopoDS_Shape& anEdge = exp2.Current();
423                if(!anEdge.IsNull()) {
424                  if(aView.Contains(anEdge)) continue;
425                  aView.Add(anEdge);
426                  const TopTools_ListOfShape& aListOfNewEd = mkCom.Modified(anEdge);                 
427                  if(!aListOfNewEd.IsEmpty())
428                    aDM4.Add(anEdge, aListOfNewEd);
429                  else {
430                    if(!mkCom.IsDeleted(anEdge)) {
431                      const TopTools_ListOfShape& aListOfGenEd = mkCom.Generated(anEdge);
432                      if(!aListOfGenEd.IsEmpty()) {
433                        /* aDM4.Bind(anEdge, aListOfGenEd); ???*/
434                      } else {
435                        TopTools_ListOfShape aList; 
436                        aList.Append(anEdge);
437                        aDM4.Add(anEdge, aList);//the same edge - not modified
438                      }
439                    }
440                  }
441                }
442              }
443             }
444             } //end DM4 filling (phase 1)
445             //cout << "DM4 Ext = " <<aDM4.Extent() <<endl;
446          // phase 2 (from tool)
447 #ifdef DEB_SPLIT_TO_ZONES
448          TCollection_AsciiString aNam("BndEd_");
449 #endif
450          TopExp_Explorer expt (limFace, TopAbs_EDGE);
451          for(int i =1;expt.More();expt.Next(),i++) {
452            const TopoDS_Shape& anEdge = expt.Current();
453            if(!anEdge.IsNull()) {
454              const TopTools_ListOfShape& aListOfNewEd = mkCom.Modified(anEdge);    
455 #ifdef DEB_SPLIT_TO_ZONES
456              TopTools_ListIteratorOfListOfShape itl(aListOfNewEd);
457              for(int j=1;itl.More();itl.Next(),j++) {
458                TCollection_AsciiString aName = aNam + i + "_" + j + "_j.brep";
459                BRepTools::Write(itl.Value(), aName.ToCString());
460                DEBTRACE(aName.ToCString()<<" = "<< itl.Value().TShape());
461              }
462 #endif
463              if(!aListOfNewEd.IsEmpty())
464                aDM4.Add(anEdge, aListOfNewEd);
465              else {
466                if(!mkCom.IsDeleted(anEdge)) {
467                  const TopTools_ListOfShape& aListOfGenEd = mkCom.Generated(anEdge);
468                  if(!aListOfGenEd.IsEmpty()) {
469                    /* aDM4.Bind(anEdge, aListOfGenEd); ??? */
470                  } else {
471                    TopTools_ListOfShape aList; 
472                    aList.Append(anEdge);
473                    aDM4.Add(anEdge, aList);//the same edge - not modified
474                  }
475                }
476              }
477            }
478          }
479          //cout << "DM4 Ext = " <<aDM4.Extent() <<endl;
480          if(limplus1Object) {
481            // fill DM1 (old - new) and DM2 (new - old)
482            HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape mIt(aDM4);
483            for(;mIt.More();mIt.Next()) {
484              const TopoDS_Shape& aKey = mIt.Key();//old
485              TopTools_ListOfShape aList;
486              aList.Append(aKey);
487              const TopTools_ListOfShape& aListOfNew = mIt.Value();
488              aDM1.Add(aKey, aListOfNew);
489              TopTools_ListIteratorOfListOfShape it(aListOfNew);
490              for(;it.More();it.Next()) {
491                if(!aDM2.Contains(it.Value()))
492                  aDM2.Add(it.Value(), aList);
493                else {
494                  TopTools_ListOfShape& aList = aDM2.ChangeFromKey(it.Value());
495                  aList.Prepend(aKey);
496                }
497              }
498            }
499          } else {
500            HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape aMIt(aDM1); 
501            //DM4 contains Old - New after common op. DM1: old - new after Split op.
502            for(;aMIt.More();aMIt.Next()) {
503              const TopoDS_Shape& aKey = aMIt.Key();
504              TopTools_ListOfShape aList;
505              aList.Append(aKey);
506              const TopTools_ListOfShape& aListOfNew = aMIt.Value();
507              TopTools_ListIteratorOfListOfShape it(aListOfNew);
508              for(;it.More();it.Next()) {// iterate new: b1, b2, b3...
509                if(!aDM4.Contains(it.Value())) // bi - is deleted
510                  continue; // go to the next bi
511                else {
512                  const TopTools_ListOfShape& aListOfNew4 = aDM4.FindFromKey(it.Value());
513                   TopTools_ListIteratorOfListOfShape it4(aListOfNew4); // {c1, c2, c3,...}
514                   for(;it4.More();it4.Next()) {
515                     if(!aDM2.Contains(it4.Value()))
516                       aDM2.Add(it4.Value(), aList);
517                     else {
518                       TopTools_ListOfShape& aList = aDM2.ChangeFromKey(it4.Value());
519                       aList.Prepend(aKey);
520                     }
521                   }
522                }
523              }
524            }
525          }
526          } else {
527              hasLimits = Standard_False;
528 #ifdef DEB_SPLIT_TO_ZONES
529              BRepTools::Write(aComResult,"CommonNV.brep");
530 #endif    
531           }
532        }
533       }
534     }
535   }// end limits processing
536   if(!hasLimits) {
537     HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape aMIt(aDM1);
538     for(;aMIt.More();aMIt.Next()) {
539       const TopoDS_Shape& aKey = aMIt.Key();
540       TopTools_ListOfShape aList;
541       aList.Append(aKey);
542       const TopTools_ListOfShape& aListOfNew = aMIt.Value();
543       TopTools_ListIteratorOfListOfShape it(aListOfNew);
544       for(;it.More();it.Next()) {
545         if(!aDM2.Contains(it.Value()))
546           aDM2.Add(it.Value(), aList);
547         else {
548           TopTools_ListOfShape& aList = aDM2.ChangeFromKey(it.Value());
549           aList.Prepend(aKey);
550         }
551       }
552     }
553   }
554
555   AddInternalEdges(aDM2, InterPolys);
556
557   // Step 4. Fill output structure.
558 #ifdef DEB_SPLIT_TO_ZONES
559   TCollection_AsciiString aNam4 ("SC_");
560 #endif  
561   HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape aMIt(aDM2);
562   for(int i =1;aMIt.More();aMIt.Next(),i++) {
563     SplitData aDestSplitData;
564     const TopoDS_Shape& aKey = aMIt.Key(); //new
565     aDestSplitData.Shape = aKey;
566     if(aKey.ShapeType() == TopAbs_FACE)
567       aDestSplitData.Type = SplitData::Data_Zone;
568     else {
569       aDestSplitData.Type = SplitData::Data_Edge;
570 #ifdef DEB_SPLIT_TO_ZONES
571       TCollection_AsciiString aName = aNam4 + i + "_k.brep";
572       BRepTools::Write(aKey,aName.ToCString());
573 #endif    
574     }
575
576     QStringList aListOfNames; // names processing
577     const TopTools_ListOfShape& aListOfOld = aMIt.Value();
578     TopTools_ListIteratorOfListOfShape it(aListOfOld);
579     for(int j =1;it.More();it.Next(),j++) {    
580       const TopoDS_Shape& aSh = it.Value(); //old
581       if(aDM3.Contains(aSh)) {
582         const QStringList& ObjectNames = aDM3.FindFromKey(aSh);    
583         aListOfNames.append(ObjectNames);
584 #ifdef DEB_SPLIT_TO_ZONES
585         TCollection_AsciiString aName = aNam4 + i +"_" + j + "_l.brep";
586         BRepTools::Write(aSh ,aName.ToCString());        
587 #endif    
588       }    else {
589 #ifdef DEB_SPLIT_TO_ZONES
590         TCollection_AsciiString aName = aNam4 +"__" + i +"_" + j + "_m.brep";
591         BRepTools::Write(aSh ,aName.ToCString());        
592         DEBTRACE(aName.ToCString()<<" = "<< aSh.TShape());
593 #endif
594           if(aBndView.Contains(aSh) && hasLimits) {            
595             aListOfNames.append(aBndName);
596 #ifdef DEB_SPLIT_TO_ZONES
597             DEBTRACE(" BndName = "<<aBndName.toStdString());
598 #endif
599           }
600       }
601     }
602
603     aDestSplitData.ObjectNames = aListOfNames;  
604     anOutputSplitDataList.append(aDestSplitData);    
605 #ifdef DEB_SPLIT_TO_ZONES
606     QString aStr = aDestSplitData.ObjectNames.join(" "); 
607     DEBTRACE("New# = " << i<< " Names = "<<aStr.toStdString() << " size = " <<aDestSplitData.ObjectNames.size());
608 #endif
609   }
610
611   return anOutputSplitDataList;
612 }
613
614 HYDROData_SplitToZonesTool::SplitDataList
615   HYDROData_SplitToZonesTool::Split( const HYDROData_SequenceOfObjects& theObjectList )
616 {
617   HYDROData_SequenceOfObjects aGeomGroups;
618   Handle(HYDROData_PolylineXY) aPolyline;
619   HYDROData_SequenceOfObjects InterPolys;
620
621   return Split( theObjectList, aGeomGroups, aPolyline, InterPolys );
622 }
623
624 void HYDROData_SplitToZonesTool::AddInternalEdges(HYDROData_DataMapOfShapeListOfShape& DM,
625    const HYDROData_SequenceOfObjects& thePolylines)
626 {
627
628   HYDROData_SequenceOfObjects::Iterator it(thePolylines);
629   TopTools_ListOfShape Wires;
630   for (;it.More();it.Next())
631   {
632     Handle(HYDROData_PolylineXY) P = Handle(HYDROData_PolylineXY)::DownCast(it.Value());
633     Wires.Append(P->GetShape());
634     //Handle(TopTools_HSequenceOfShape) aConnectedWires = new TopTools_HSequenceOfShape;
635     //if (P->GetNbConnectedWires(aConnectedWires) > 0)
636     //  Wires.Append(aConnectedWires->Value(1));
637   }
638
639   HYDROData_DataMapOfShapeListOfShape newDM;
640
641   for (int i = 1; i <= DM.Extent();i++)
642   {
643     const TopoDS_Shape& K = DM.FindKey(i);
644     const TopTools_ListOfShape& V = DM.FindFromIndex(i);
645     TopTools_ListOfShape out;
646     if (K.ShapeType() == TopAbs_FACE)
647     {
648       CutByEdges(K, Wires, out);
649       TopTools_ListIteratorOfListOfShape it(out);
650       for (;it.More(); it.Next())
651       {
652         const TopoDS_Shape& NF = it.Value();
653         if (!NF.IsNull())
654           newDM.Add(NF, V);
655       }
656     }
657     else
658       newDM.Add(K, V); // ignore edges, wires...
659   }
660
661   DM = newDM;
662 }
663
664
665 int HYDROData_SplitToZonesTool::CutByEdges(const TopoDS_Shape& InSh, const TopTools_ListOfShape& InEdges,
666   TopTools_ListOfShape& outShs)
667 {
668   int anError;
669   if (InSh.IsNull())
670     return -1;
671   if (InEdges.IsEmpty())
672   {
673     outShs.Clear();
674     outShs.Append(InSh);
675     return 0;
676   }
677   TopTools_ListIteratorOfListOfShape anIt;
678   BOPAlgo_Builder anAlgo;
679   anAlgo.AddArgument(InSh);
680   anIt.Initialize( InEdges );
681   for( ; anIt.More(); anIt.Next() )
682     anAlgo.AddArgument( anIt.Value() );
683
684   anAlgo.Perform(); 
685   anError = anAlgo.ErrorStatus();
686   if (anError)
687     return anError;
688
689   outShs = anAlgo.Modified( InSh );
690   if (outShs.IsEmpty())
691     outShs.Append(InSh);
692   return 0;
693 }