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