Salome HOME
lots 3,8
[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 #include <assert.h>
48
49 //#define DEB_SPLIT_TO_ZONES 1
50 //#define DEB_SPLIT_TO_ZONES_CHECK_PARTITION 1
51 #if (defined (DEB_SPLIT_TO_ZONES) || defined(DEB_SPLIT_TO_ZONES_CHECK_PARTITION))
52 #include <BRepTools.hxx>
53 static TCollection_AsciiString fileNameBefore("BeforeTranslation");
54 #endif
55
56 //#define _DEVDEBUG_
57 #include "HYDRO_trace.hxx"
58
59 TopoDS_Face HYDROData_SplitToZonesTool::SplitData::Face() const
60 {
61   TopoDS_Face aResFace;
62
63   if( !Shape.IsNull() )
64   {
65     if ( Shape.ShapeType() == TopAbs_FACE )
66     {
67       aResFace = TopoDS::Face( Shape );
68     }
69     else if ( Shape.ShapeType() == TopAbs_WIRE )
70     {
71       BRepBuilderAPI_MakeFace aMakeFace( TopoDS::Wire( Shape ), Standard_True );
72       aMakeFace.Build();
73       if( aMakeFace.IsDone() )
74         aResFace = aMakeFace.Face();
75     }
76   }
77
78   return aResFace;
79 }
80
81 Standard_Boolean buildLimFace(const TopoDS_Wire& theBndWire, TopoDS_Face& outFace) 
82 {
83   GProp_GProps G;
84   BRepGProp::LinearProperties(theBndWire,G);
85   const gp_Pnt& aCPnt = G.CentreOfMass();
86   gp_Pln aPln;
87   BRepBuilderAPI_FindPlane fndPlane (theBndWire, Precision::Confusion());  
88   if(fndPlane.Found())
89     aPln = fndPlane.Plane()->Pln();
90   else
91     aPln = gp_Pln(aCPnt, gp::OZ().Direction());
92   BRepBuilderAPI_MakeFace  aMkFace(aPln, theBndWire);
93   if(aMkFace.IsDone()) {
94     outFace = aMkFace.Face();      
95     if(!outFace.IsNull()) {
96 #ifdef DEB_SPLIT_TO_ZONES
97 //      BRepTools::Write(limFace,"FL.brep");
98 #endif
99       return Standard_True;
100     }
101   }
102   return Standard_False;
103 }
104 //======================================================================================
105
106 void HYDROData_SplitToZonesTool::SetFileNames(const QString& theNameBefore, const QString& theNameAfter)
107 {
108 #ifdef DEB_SPLIT_TO_ZONES_CHECK_PARTITION
109   if(!theNameBefore.isNull() && !theNameBefore.isEmpty())
110   {
111     TCollection_AsciiString aName (theNameBefore.toStdString().data());
112     fileNameBefore = aName;
113   }
114   if(!theNameAfter.isNull() && !theNameAfter.isEmpty())
115   {
116     TCollection_AsciiString aName (theNameAfter.toStdString().data());
117     HYDROData_Transform::SetFileName (aName);
118   }
119 #endif
120 }
121 //======================================================================================
122 bool HYDROData_SplitToZonesTool::SplitFaces(const TopoDS_Compound& theComp, 
123                                                         HYDROData_Transform& theTool)
124 {
125   theTool.Clear();
126   theTool.SetArgument(theComp);
127   theTool.Detect();
128   const Standard_Boolean bToTransform = theTool.ToTransform();  
129   theTool.SetToTransform(bToTransform);  
130   theTool.Perform();
131   return theTool.HasErrors();
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 #ifndef NDEBUG
250       //check CutFaceByEdges method: ensure that SD.Face() edges fully covered by
251       //history map (OutOrSh2M)
252       if (!OutOrSh2M.IsEmpty())
253       {
254         TopTools_IndexedMapOfShape EE;
255         TopExp::MapShapes(SD.Face(), TopAbs_EDGE, EE);
256         int noncontNb = 0;      
257         for (int i = 1; i <= EE.Extent(); i++)
258         {
259           const TopoDS_Shape& E = EE(i);
260           noncontNb += !OutOrSh2M.Contains(E);
261         }
262         //noncontNb > 0 => some problem with edge history
263         assert(noncontNb == 0);
264       }
265 #endif
266
267       if(!theGroupsList.IsEmpty() ) 
268       {
269         SplitDataList ModifInpGroupList;
270         SplitDataListIterator it(anInputGroupList);
271         while( it.hasNext() )
272         {
273           const SplitData& SData = it.next();
274           if (SData.Type != SplitData::Data_Edge)
275             ModifInpGroupList.append(SData); //add as is
276           const TopoDS_Shape& SData_sh = SData.Shape;
277           TopTools_ListOfShape modif_ls;
278           if (!InterPolys.IsEmpty())
279             if (OutOrSh2M.Contains(SData_sh))
280               modif_ls = OutOrSh2M.FindFromKey(SData_sh);
281             //else TODO -  show message that one of the object should be updated
282
283 #ifndef NDEBUG
284           if (!InterPolys.IsEmpty() && OutOrSh2M.IsEmpty())
285             assert (true);
286 #endif
287           if (modif_ls.IsEmpty())
288             ModifInpGroupList.append(SData); //non modified
289           else
290           {
291             TopTools_ListIteratorOfListOfShape itl_modif(modif_ls);
292             for (;itl_modif.More();itl_modif.Next())
293             {
294               const TopoDS_Shape& CSH = itl_modif.Value();
295               if (CSH.ShapeType() == TopAbs_EDGE)
296               {
297                 SplitData NewSData;
298                 NewSData.ObjectNames = SData.ObjectNames;
299                 NewSData.Type = SData.Type;
300                 NewSData.Shape = CSH;
301                 ModifInpGroupList.append(NewSData);
302               }
303             }
304           }
305
306         }
307         anOutputSplitDataList.append(ModifInpGroupList);
308       }
309       return anOutputSplitDataList;
310     }
311     else
312       limplus1Object = true;// size =1 && hasLimits
313   }
314   HYDROData_DataMapOfShapeListOfString aDM3;
315   if(!anInputGroupList.isEmpty()) {// Old edge ==> List_Of_Names
316     QStringList aListOfNames;
317     for (int i=0;i < anInputGroupList.size() ;i++) {
318       const TopoDS_Shape& aSh = anInputGroupList.at(i).Shape;
319       aDM3.Add(aSh, anInputGroupList.at(i).ObjectNames);
320     }
321   }
322
323   // Step 1. Prepare Partition structures. 
324   TopoDS_Shape aResult;
325   TopTools_ListOfShape aLS;  
326   QStringList aListOfNames;
327   TopoDS_Compound aCmp;
328   BRep_Builder aBB;
329   aBB.MakeCompound(aCmp);
330   for (int i=0;i < anInputSplitDataList.size() ;i++) {
331     const TopoDS_Shape& aSh = anInputSplitDataList.at(i).Shape;
332     aDM3.Add(aSh, anInputSplitDataList.at(i).ObjectNames);
333     aLS.Append(aSh);
334     aBB.Add(aCmp,aSh);
335 #ifdef DEB_SPLIT_TO_ZONES
336     TCollection_AsciiString aName = aNam + i + "_b.brep";
337     BRepTools::Write(aSh, aName.ToCString());
338 #endif
339   }
340 #ifdef DEB_SPLIT_TO_ZONES_CHECK_PARTITION
341   TCollection_AsciiString aNameBefore = fileNameBefore + "_c.brep";
342   BRepTools::Write(aCmp, aNameBefore.ToCString());
343 #endif
344
345   HYDROData_DataMapOfShapeListOfShape aDM1;
346   if(anInputSplitDataList.size() > 1) {
347     HYDROData_Transform splitTool; 
348     bool anErr = SplitFaces(aCmp, splitTool);    
349     if(anErr)
350       return anOutputSplitDataList;
351     aResult = splitTool.Shape();
352     if (aResult.IsNull()) 
353       return anOutputSplitDataList;
354     BRepCheck_Analyzer aCheck (aResult);
355     if(!aCheck.IsValid()) {
356   #ifdef DEB_SPLIT_TO_ZONES
357       DEBTRACE("result is not valid");
358       BRepTools::Write(aResult, "SplitFacesNV.brep");  
359   #endif
360       return anOutputSplitDataList;
361     }
362   #ifdef DEB_SPLIT_TO_ZONES
363     BRepTools::Write(aResult, "SplitFacesV.brep");
364   #endif
365     
366     // Step 3. Collect history  
367     //HYDROData_DataMapOfShapeListOfShape aDM1;
368     TopTools_ListIteratorOfListOfShape anIt(aLS); 
369   #ifdef DEB_SPLIT_TO_ZONES
370     TCollection_AsciiString aNamM ("EdgM_");
371     TCollection_AsciiString aNamG ("EdgG_");
372   #endif
373     for (int i =1;anIt.More();anIt.Next(),i++) {
374       Standard_Boolean foundF(Standard_False);
375       const TopTools_ListOfShape& aListOfNew = splitTool.Modified(anIt.Value());
376       if(!aListOfNew.IsEmpty())
377         foundF = Standard_True;
378
379       TopTools_ListOfShape aList;
380
381       TopTools_ListIteratorOfListOfShape it(aListOfNew);
382       for(;it.More();it.Next())       
383         aList.Append(it.Value());
384           /* *********************************************************************
385       // Bug in History: partition should give only modified entities! => temporary solution is used
386       //const TopTools_ListOfShape& aListOfGen = splitTool.Generated(anIt.Value());
387       //if(!aListOfGen.IsEmpty())
388         //foundF = Standard_True;
389       //it.Initialize(aListOfGen);    
390       //for(;it.More();it.Next())     
391        // aList.Append(it.Value());  
392            ********************************************************************* */
393       if(!foundF) // face is not modified
394         aList.Append (anIt.Value());
395       aDM1.Add(anIt.Value(), aList);
396   #ifdef DEB_SPLIT_TO_ZONES
397       TCollection_AsciiString aName;
398   #endif
399       if(!anInputGroupList.isEmpty() ) { // 1
400         TopExp_Explorer exp (anIt.Value(), TopAbs_EDGE);
401         for (int j =1;exp.More();exp.Next(),j++) {
402           aList.Clear();
403           Standard_Boolean foundE(Standard_False);
404           const TopTools_ListOfShape& aListM = splitTool.Modified(exp.Current()); 
405 #ifndef NDEBUG
406           DEBTRACE("NB_EDGE_M = " << aListM.Extent());
407 #endif
408           if(aListM.Extent()) foundE = Standard_True;
409           it.Initialize(aListM);    
410           for(int k=1;it.More();it.Next(),k++) {    
411             aList.Append(it.Value());
412   #ifdef DEB_SPLIT_TO_ZONES
413             aName = aNamM + i + j +k +"_d.brep";
414             BRepTools::Write(it.Value(),aName.ToCString());
415   #endif
416           }
417           /* *********************************************************************
418                   //const TopTools_ListOfShape& aListG = splitTool.Generated(exp.Current());
419           //if(aListG.Extent()) foundE = Standard_True;
420           //it.Initialize(aListG);    
421           //for(int k=1;it.More();it.Next(),k++)
422             //aList.Append(it.Value());
423           //cout << "NB_EDGE = " << aList.Extent() <<endl;
424                   ************************************************************************** */
425           if(!foundE) {
426             aList.Append (exp.Current());
427   #ifdef DEB_SPLIT_TO_ZONES
428             aName = aNamG + i + j +"_e.brep";
429             BRepTools::Write(exp.Current(),aName.ToCString());
430             DEBTRACE(aName.ToCString()<< " = " << exp.Current().TShape());
431   #endif
432           }
433           aDM1.Add(exp.Current(), aList);
434         }
435       }
436     }
437   } else {
438       aResult = anInputSplitDataList.at(0).Shape; // get single input shape
439   }
440
441   // aDM2: NewShape ==> ListOfOldShapes
442   HYDROData_DataMapOfShapeListOfShape aDM2;
443   // make limiting face
444   HYDROData_DataMapOfShapeListOfShape aDM4;
445   Standard_Boolean hasLimits(Standard_False);
446   QString aBndName;
447   HYDROData_MapOfShape aBndView;
448   if (! thePolyline.IsNull()) {
449     Handle(TopTools_HSequenceOfShape) aConnectedWires = new TopTools_HSequenceOfShape;
450     int nbWires = thePolyline->GetNbConnectedWires(aConnectedWires);
451     const TopoDS_Wire aBndWire = TopoDS::Wire(aConnectedWires->Value(1));
452     if(!aBndWire.IsNull()) {
453       TopoDS_Face limFace;       
454       if(buildLimFace(aBndWire, limFace)) {
455         TopoDS_Shape aComResult;
456         BRepAlgoAPI_Common mkCom(aResult, limFace);
457         if(mkCom.IsDone()) {
458           aComResult = mkCom.Shape();
459           BRepCheck_Analyzer aCheck (aComResult);
460           if(aCheck.IsValid()) {
461 #ifdef DEB_SPLIT_TO_ZONES
462             BRepTools::Write(aComResult,"CommonV.brep");
463             BRepTools::Write(limFace,"limFace.brep");
464 #endif    
465             aBndName = thePolyline->GetName();
466             hasLimits = Standard_True; // DM2 should be filled here
467             TopExp_Explorer exp (limFace, TopAbs_EDGE);
468             for (int i =1;exp.More();exp.Next(),i++) {
469               const TopoDS_Shape& anEdge = exp.Current();
470               if(anEdge.IsNull()) continue;
471               aBndView.Add(anEdge);
472               QStringList aListOfNames;
473               aListOfNames.append(aBndName);
474               aDM3.Add(anEdge, aListOfNames);
475               TopTools_ListOfShape aList;
476               aList.Append(anEdge);
477               aDM1.Add(anEdge,aList);
478             }
479             HYDROData_MapOfShape aView;
480             exp.Init (aResult, TopAbs_FACE);
481             for (int i =1;exp.More();exp.Next(),i++) {
482               const TopoDS_Shape& aFace = exp.Current();
483               if(!aFace.IsNull()) {
484                 const TopTools_ListOfShape& aListOfNew = mkCom.Modified(aFace);
485 #ifndef NDEBUG
486                 DEBTRACE("Modified: " << aListOfNew.Extent());
487 #endif
488                 if(!aListOfNew.IsEmpty()) {
489                   aDM4.Add(aFace, aListOfNew);
490 #ifdef DEB_SPLIT_TO_ZONES
491                   TCollection_AsciiString aName = aNam + i + "_f.brep";
492                   BRepTools::Write(aListOfNew.Last(), aName.ToCString());
493 #endif
494                 }
495                 else {
496                     if(!mkCom.IsDeleted(aFace)) {
497                       const TopTools_ListOfShape& aListOfGen = mkCom.Generated(aFace);    
498                       if(!aListOfGen.IsEmpty()) {
499                         /* aDM4.Bind(aFace, aListOfGen); ???   */
500 #ifdef DEB_SPLIT_TO_ZONES
501                         TCollection_AsciiString aName = aNam + i + "_g.brep";
502                         BRepTools::Write(aListOfGen.Last(), aName.ToCString());
503 #endif
504                       }
505                        else {
506                         TopTools_ListOfShape aList; 
507                         aList.Append(aFace);
508                         aDM4.Add(aFace, aList); //the same face - not modified
509                        }
510                     }
511              }
512              TopExp_Explorer exp2 (aFace, TopAbs_EDGE);
513              for (int j =1;exp2.More();exp2.Next(),j++) {
514                const TopoDS_Shape& anEdge = exp2.Current();
515                if(!anEdge.IsNull()) {
516                  if(aView.Contains(anEdge)) continue;
517                  aView.Add(anEdge);
518                  const TopTools_ListOfShape& aListOfNewEd = mkCom.Modified(anEdge);                 
519                  if(!aListOfNewEd.IsEmpty())
520                    aDM4.Add(anEdge, aListOfNewEd);
521                  else {
522                    if(!mkCom.IsDeleted(anEdge)) {
523                      const TopTools_ListOfShape& aListOfGenEd = mkCom.Generated(anEdge);
524                      if(!aListOfGenEd.IsEmpty()) {
525                        /* aDM4.Bind(anEdge, aListOfGenEd); ???*/
526                      } else {
527                        TopTools_ListOfShape aList; 
528                        aList.Append(anEdge);
529                        aDM4.Add(anEdge, aList);//the same edge - not modified
530                      }
531                    }
532                  }
533                }
534              }
535             }
536             } //end DM4 filling (phase 1)
537             //cout << "DM4 Ext = " <<aDM4.Extent() <<endl;
538          // phase 2 (from tool)
539 #ifdef DEB_SPLIT_TO_ZONES
540          TCollection_AsciiString aNam("BndEd_");
541 #endif
542          TopExp_Explorer expt (limFace, TopAbs_EDGE);
543          for(int i =1;expt.More();expt.Next(),i++) {
544            const TopoDS_Shape& anEdge = expt.Current();
545            if(!anEdge.IsNull()) {
546              const TopTools_ListOfShape& aListOfNewEd = mkCom.Modified(anEdge);    
547 #ifdef DEB_SPLIT_TO_ZONES
548              TopTools_ListIteratorOfListOfShape itl(aListOfNewEd);
549              for(int j=1;itl.More();itl.Next(),j++) {
550                TCollection_AsciiString aName = aNam + i + "_" + j + "_j.brep";
551                BRepTools::Write(itl.Value(), aName.ToCString());
552                DEBTRACE(aName.ToCString()<<" = "<< itl.Value().TShape());
553              }
554 #endif
555              if(!aListOfNewEd.IsEmpty())
556                aDM4.Add(anEdge, aListOfNewEd);
557              else {
558                if(!mkCom.IsDeleted(anEdge)) {
559                  const TopTools_ListOfShape& aListOfGenEd = mkCom.Generated(anEdge);
560                  if(!aListOfGenEd.IsEmpty()) {
561                    /* aDM4.Bind(anEdge, aListOfGenEd); ??? */
562                  } else {
563                    TopTools_ListOfShape aList; 
564                    aList.Append(anEdge);
565                    aDM4.Add(anEdge, aList);//the same edge - not modified
566                  }
567                }
568              }
569            }
570          }
571          //cout << "DM4 Ext = " <<aDM4.Extent() <<endl;
572          if(limplus1Object) {
573            // fill DM1 (old - new) and DM2 (new - old)
574            HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape mIt(aDM4);
575            for(;mIt.More();mIt.Next()) {
576              const TopoDS_Shape& aKey = mIt.Key();//old
577              TopTools_ListOfShape aList;
578              aList.Append(aKey);
579              const TopTools_ListOfShape& aListOfNew = mIt.Value();
580              aDM1.Add(aKey, aListOfNew);
581              TopTools_ListIteratorOfListOfShape it(aListOfNew);
582              for(;it.More();it.Next()) {
583                if(!aDM2.Contains(it.Value()))
584                  aDM2.Add(it.Value(), aList);
585                else {
586                  TopTools_ListOfShape& aList = aDM2.ChangeFromKey(it.Value());
587                  aList.Prepend(aKey);
588                }
589              }
590            }
591          } else {
592            HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape aMIt(aDM1); 
593            //DM4 contains Old - New after common op. DM1: old - new after Split op.
594            for(;aMIt.More();aMIt.Next()) {
595              const TopoDS_Shape& aKey = aMIt.Key();
596              TopTools_ListOfShape aList;
597              aList.Append(aKey);
598              const TopTools_ListOfShape& aListOfNew = aMIt.Value();
599              TopTools_ListIteratorOfListOfShape it(aListOfNew);
600              for(;it.More();it.Next()) {// iterate new: b1, b2, b3...
601                if(!aDM4.Contains(it.Value())) // bi - is deleted
602                  continue; // go to the next bi
603                else {
604                  const TopTools_ListOfShape& aListOfNew4 = aDM4.FindFromKey(it.Value());
605                   TopTools_ListIteratorOfListOfShape it4(aListOfNew4); // {c1, c2, c3,...}
606                   for(;it4.More();it4.Next()) {
607                     if(!aDM2.Contains(it4.Value()))
608                       aDM2.Add(it4.Value(), aList);
609                     else {
610                       TopTools_ListOfShape& aList = aDM2.ChangeFromKey(it4.Value());
611                       aList.Prepend(aKey);
612                     }
613                   }
614                }
615              }
616            }
617          }
618          } else {
619              hasLimits = Standard_False;
620 #ifdef DEB_SPLIT_TO_ZONES
621              BRepTools::Write(aComResult,"CommonNV.brep");
622 #endif    
623           }
624        }
625       }
626     }
627   }// end limits processing
628   if(!hasLimits) {
629     HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape aMIt(aDM1);
630     for(;aMIt.More();aMIt.Next()) {
631       const TopoDS_Shape& aKey = aMIt.Key();
632       TopTools_ListOfShape aList;
633       aList.Append(aKey);
634       const TopTools_ListOfShape& aListOfNew = aMIt.Value();
635       TopTools_ListIteratorOfListOfShape it(aListOfNew);
636       for(;it.More();it.Next()) {
637         if(!aDM2.Contains(it.Value()))
638           aDM2.Add(it.Value(), aList);
639         else {
640           TopTools_ListOfShape& aList = aDM2.ChangeFromKey(it.Value());
641           aList.Prepend(aKey);
642         }
643       }
644     }
645   }
646
647   AddInternalEdges(aDM2, InterPolys, &OutNE);
648   NCollection_DataMap<TopoDS_Shape, Handle(HYDROData_PolylineXY), TopTools_ShapeMapHasher>::Iterator spit(OutNE);
649   for (;spit.More();spit.Next())
650   {
651     const TopoDS_Edge& E = TopoDS::Edge(spit.Key());
652     if (E.IsNull())
653       continue;
654     SplitData SDI(SplitData::Data_IntEdge, E, spit.Value()->GetName());   
655     anOutputSplitDataList.append(SDI);
656   }
657
658   // Step 4. Fill output structure.
659 #ifdef DEB_SPLIT_TO_ZONES
660   TCollection_AsciiString aNam4 ("SC_");
661 #endif  
662   HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape aMIt(aDM2);
663   for(int i =1;aMIt.More();aMIt.Next(),i++) {
664     SplitData aDestSplitData;
665     const TopoDS_Shape& aKey = aMIt.Key(); //new
666     aDestSplitData.Shape = aKey;
667     if(aKey.ShapeType() == TopAbs_FACE)
668       aDestSplitData.Type = SplitData::Data_Zone;
669     else {
670       aDestSplitData.Type = SplitData::Data_Edge;
671 #ifdef DEB_SPLIT_TO_ZONES
672       TCollection_AsciiString aName = aNam4 + i + "_k.brep";
673       BRepTools::Write(aKey,aName.ToCString());
674 #endif    
675     }
676
677     QStringList aListOfNames; // names processing
678     const TopTools_ListOfShape& aListOfOld = aMIt.Value();
679     TopTools_ListIteratorOfListOfShape it(aListOfOld);
680     for(int j =1;it.More();it.Next(),j++) {    
681       const TopoDS_Shape& aSh = it.Value(); //old
682       if(aDM3.Contains(aSh)) {
683         const QStringList& ObjectNames = aDM3.FindFromKey(aSh);    
684         aListOfNames.append(ObjectNames);
685 #ifdef DEB_SPLIT_TO_ZONES
686         TCollection_AsciiString aName = aNam4 + i +"_" + j + "_l.brep";
687         BRepTools::Write(aSh ,aName.ToCString());        
688 #endif    
689       }    else {
690 #ifdef DEB_SPLIT_TO_ZONES
691         TCollection_AsciiString aName = aNam4 +"__" + i +"_" + j + "_m.brep";
692         BRepTools::Write(aSh ,aName.ToCString());        
693         DEBTRACE(aName.ToCString()<<" = "<< aSh.TShape());
694 #endif
695           if(aBndView.Contains(aSh) && hasLimits) {            
696             aListOfNames.append(aBndName);
697 #ifdef DEB_SPLIT_TO_ZONES
698             DEBTRACE(" BndName = "<<aBndName.toStdString());
699 #endif
700           }
701       }
702     }
703
704     aDestSplitData.ObjectNames = aListOfNames;  
705     anOutputSplitDataList.append(aDestSplitData);    
706 #ifdef DEB_SPLIT_TO_ZONES
707     QString aStr = aDestSplitData.ObjectNames.join(" "); 
708     DEBTRACE("New# = " << i<< " Names = "<<aStr.toStdString() << " size = " <<aDestSplitData.ObjectNames.size());
709 #endif
710   }
711
712   return anOutputSplitDataList;
713 }
714
715 HYDROData_SplitToZonesTool::SplitDataList
716   HYDROData_SplitToZonesTool::Split( const HYDROData_SequenceOfObjects& theObjectList )
717 {
718   HYDROData_SequenceOfObjects aGeomGroups;
719   Handle(HYDROData_PolylineXY) aPolyline;
720   HYDROData_SequenceOfObjects InterPolys;
721
722   return Split( theObjectList, aGeomGroups, aPolyline, InterPolys );
723 }
724
725 void HYDROData_SplitToZonesTool::AddInternalEdges(HYDROData_DataMapOfShapeListOfShape& DM,
726    const HYDROData_SequenceOfObjects& thePolylines,
727    NCollection_DataMap<TopoDS_Shape, Handle(HYDROData_PolylineXY), TopTools_ShapeMapHasher>* OutNE)
728 {
729
730   HYDROData_SequenceOfObjects::Iterator it(thePolylines);
731   TopTools_ListOfShape Wires;
732   NCollection_DataMap<TopoDS_Shape, Handle(HYDROData_PolylineXY), TopTools_ShapeMapHasher> W2P;
733   for (;it.More();it.Next())
734   {
735     Handle(HYDROData_PolylineXY) P = Handle(HYDROData_PolylineXY)::DownCast(it.Value());
736     const TopoDS_Shape& CW = P->GetShape();
737     Wires.Append(CW);
738     W2P.Bind(CW, P);
739   }
740
741   HYDROData_DataMapOfShapeListOfShape newDM;
742   TopTools_IndexedDataMapOfShapeShape OutNE1;
743   for (int i = 1; i <= DM.Extent();i++)
744   {
745     const TopoDS_Shape& K = DM.FindKey(i);
746     const TopTools_ListOfShape& V = DM.FindFromIndex(i);
747     TopTools_ListOfShape out;
748     if (K.ShapeType() == TopAbs_FACE)
749     {
750       CutByEdges(K, Wires, out, &OutNE1, NULL);
751       TopTools_ListIteratorOfListOfShape it(out);
752       for (;it.More(); it.Next())
753       {
754         const TopoDS_Shape& NF = it.Value();
755         if (!NF.IsNull())
756           newDM.Add(NF, V);
757       }
758     }
759     else
760       newDM.Add(K, V); // ignore edges, wires...
761   }
762
763   for (int i = 1; i <= OutNE1.Extent(); i++)
764     OutNE->Bind(OutNE1.FindKey(i), W2P(OutNE1.FindFromIndex(i)));
765
766   DM = newDM;
767 }
768
769 void HYDROData_SplitToZonesTool::CutFaceByEdges(const TopoDS_Face& in, 
770                                                 TopTools_ListOfShape& out, 
771                                                 const HYDROData_SequenceOfObjects& thePolylines,
772                                                 NCollection_DataMap<TopoDS_Shape, Handle(HYDROData_PolylineXY), TopTools_ShapeMapHasher>* OutNE,                                                
773                                                 TopTools_IndexedDataMapOfShapeListOfShape* OutOrSh2M)
774 {
775
776   HYDROData_SequenceOfObjects::Iterator it(thePolylines);
777   TopTools_ListOfShape Wires;
778   NCollection_DataMap<TopoDS_Shape, Handle(HYDROData_PolylineXY), TopTools_ShapeMapHasher> W2P;
779   for (;it.More();it.Next())
780   {
781     Handle(HYDROData_PolylineXY) P = Handle(HYDROData_PolylineXY)::DownCast(it.Value());
782     const TopoDS_Shape& CW = P->GetShape();
783     Wires.Append(CW);
784     W2P.Bind(CW, P);
785   }
786
787   TopTools_IndexedDataMapOfShapeShape OutNE1;
788   CutByEdges(in, Wires, out, &OutNE1, OutOrSh2M);
789
790   for (int i = 1; i <= OutNE1.Extent(); i++)
791     OutNE->Bind(OutNE1.FindKey(i), W2P(OutNE1.FindFromIndex(i)));
792
793 }
794
795 int HYDROData_SplitToZonesTool::CutByEdges(const TopoDS_Shape& InSh, const TopTools_ListOfShape& InW,
796                                            TopTools_ListOfShape& outShs, 
797                                            TopTools_IndexedDataMapOfShapeShape* OutNE, 
798                                            TopTools_IndexedDataMapOfShapeListOfShape* OInSH2MSH)
799 {
800   int anError;
801   if (InSh.IsNull())
802     return -1;
803   if (InW.IsEmpty())
804   {
805     outShs.Clear();
806     outShs.Append(InSh);
807     return 0;
808   }
809   TopTools_ListIteratorOfListOfShape anIt;
810   BOPAlgo_Builder anAlgo;
811   anAlgo.AddArgument(InSh);
812
813   anIt.Initialize( InW );
814   for( ; anIt.More(); anIt.Next() )
815     anAlgo.AddArgument( anIt.Value() );
816
817   anAlgo.Perform(); 
818   if (anAlgo.HasErrors())
819     return -1;
820
821   outShs = anAlgo.Modified( InSh );
822   if (outShs.IsEmpty())
823     outShs.Append(InSh);
824
825   if (OutNE)
826   {
827     TopTools_IndexedDataMapOfShapeShape NonFOutNE;
828     anIt.Initialize( InW );
829     for( ; anIt.More(); anIt.Next() )
830     {
831       const TopoDS_Shape& OSH = anIt.Value(); 
832       TopExp_Explorer exp(OSH, TopAbs_EDGE);
833       for (;exp.More();exp.Next())
834       {
835         const TopoDS_Edge& OE = TopoDS::Edge(exp.Current());
836         const TopTools_ListOfShape& ls = anAlgo.Modified(OE);
837         TopTools_ListIteratorOfListOfShape itls(ls);
838         for (;itls.More();itls.Next())
839           NonFOutNE.Add(itls.Value(), OSH);
840       }
841     }
842
843     //filter edges; if face contain an edge => keep it
844     TopTools_IndexedMapOfShape AllEdges; //edges from output shape
845     for (anIt.Initialize(outShs); anIt.More();anIt.Next())
846       TopExp::MapShapes(anIt.Value(), TopAbs_EDGE, AllEdges);
847
848     for (int i = 1; i <= NonFOutNE.Extent(); i++)
849     {
850       const TopoDS_Shape& CE = NonFOutNE.FindKey(i);
851       const TopoDS_Shape& V = NonFOutNE.FindFromIndex(i);
852       if (AllEdges.Contains(CE))
853         OutNE->Add(CE, V);
854     }
855   }
856
857   //map all sub-shapes from the original InSh to modified shapes  
858   if (OInSH2MSH)
859   {
860     TopTools_IndexedMapOfShape allSh;
861     TopExp::MapShapes(InSh, allSh);
862     for(int i = 1; i <= allSh.Extent(); i++ )
863     {
864       const TopoDS_Shape& or_sh = allSh(i); 
865       const TopTools_ListOfShape& ls = anAlgo.Modified(or_sh);
866       OInSH2MSH->Add(or_sh, ls);
867     }
868   }
869
870   return 0;
871 }