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