]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROData/HYDROData_SplitToZonesTool.cxx
Salome HOME
Merge branch 'BR_H2018_2' of https://codev-tuleap.cea.fr/plugins/git/salome/hydro...
[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
648 #ifdef DEB_SPLIT_TO_ZONES
649   BRep_Builder B;
650   TopoDS_Compound cmp2;
651   B.MakeCompound(cmp2);
652   HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape dIt(aDM2);
653   for(int i =1;dIt.More();dIt.Next(),i++)
654   {
655     const TopoDS_Shape& aKey = dIt.Key();
656     B.Add(cmp2, aKey);
657   }
658 #endif
659
660   AddInternalEdges(aDM2, InterPolys, &OutNE);
661   NCollection_DataMap<TopoDS_Shape, Handle(HYDROData_PolylineXY), TopTools_ShapeMapHasher>::Iterator spit(OutNE);
662   for (;spit.More();spit.Next())
663   {
664     const TopoDS_Edge& E = TopoDS::Edge(spit.Key());
665     if (E.IsNull())
666       continue;
667     SplitData SDI(SplitData::Data_IntEdge, E, spit.Value()->GetName());   
668     anOutputSplitDataList.append(SDI);
669   }
670
671   // Step 4. Fill output structure.
672 #ifdef DEB_SPLIT_TO_ZONES
673   TCollection_AsciiString aNam4 ("SC_");
674 #endif  
675   HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape aMIt(aDM2);
676   for(int i =1;aMIt.More();aMIt.Next(),i++) {
677     SplitData aDestSplitData;
678     const TopoDS_Shape& aKey = aMIt.Key(); //new
679     aDestSplitData.Shape = aKey;
680     if(aKey.ShapeType() == TopAbs_FACE)
681       aDestSplitData.Type = SplitData::Data_Zone;
682     else {
683       aDestSplitData.Type = SplitData::Data_Edge;
684 #ifdef DEB_SPLIT_TO_ZONES
685       TCollection_AsciiString aName = aNam4 + i + "_k.brep";
686       BRepTools::Write(aKey,aName.ToCString());
687 #endif    
688     }
689
690     QStringList aListOfNames; // names processing
691     const TopTools_ListOfShape& aListOfOld = aMIt.Value();
692     TopTools_ListIteratorOfListOfShape it(aListOfOld);
693     for(int j =1;it.More();it.Next(),j++) {    
694       const TopoDS_Shape& aSh = it.Value(); //old
695       if(aDM3.Contains(aSh)) {
696         const QStringList& ObjectNames = aDM3.FindFromKey(aSh);    
697         aListOfNames.append(ObjectNames);
698 #ifdef DEB_SPLIT_TO_ZONES
699         TCollection_AsciiString aName = aNam4 + i +"_" + j + "_l.brep";
700         BRepTools::Write(aSh ,aName.ToCString());        
701 #endif    
702       }    else {
703 #ifdef DEB_SPLIT_TO_ZONES
704         TCollection_AsciiString aName = aNam4 +"__" + i +"_" + j + "_m.brep";
705         BRepTools::Write(aSh ,aName.ToCString());        
706         DEBTRACE(aName.ToCString()<<" = "<< aSh.TShape());
707 #endif
708           if(aBndView.Contains(aSh) && hasLimits) {            
709             aListOfNames.append(aBndName);
710 #ifdef DEB_SPLIT_TO_ZONES
711             DEBTRACE(" BndName = "<<aBndName.toStdString());
712 #endif
713           }
714       }
715     }
716
717     aDestSplitData.ObjectNames = aListOfNames;  
718     anOutputSplitDataList.append(aDestSplitData);    
719 #ifdef DEB_SPLIT_TO_ZONES
720     QString aStr = aDestSplitData.ObjectNames.join(" "); 
721     DEBTRACE("New# = " << i<< " Names = "<<aStr.toStdString() << " size = " <<aDestSplitData.ObjectNames.size());
722 #endif
723   }
724
725   return anOutputSplitDataList;
726 }
727
728 HYDROData_SplitToZonesTool::SplitDataList
729   HYDROData_SplitToZonesTool::Split( const HYDROData_SequenceOfObjects& theObjectList )
730 {
731   HYDROData_SequenceOfObjects aGeomGroups;
732   Handle(HYDROData_PolylineXY) aPolyline;
733   HYDROData_SequenceOfObjects InterPolys;
734
735   return Split( theObjectList, aGeomGroups, aPolyline, InterPolys );
736 }
737
738 void HYDROData_SplitToZonesTool::AddInternalEdges(HYDROData_DataMapOfShapeListOfShape& DM,
739    const HYDROData_SequenceOfObjects& thePolylines,
740    NCollection_DataMap<TopoDS_Shape, Handle(HYDROData_PolylineXY), TopTools_ShapeMapHasher>* OutNE)
741 {
742
743   HYDROData_SequenceOfObjects::Iterator it(thePolylines);
744   TopTools_ListOfShape Wires;
745   NCollection_DataMap<TopoDS_Shape, Handle(HYDROData_PolylineXY), TopTools_ShapeMapHasher> W2P;
746   for (;it.More();it.Next())
747   {
748     Handle(HYDROData_PolylineXY) P = Handle(HYDROData_PolylineXY)::DownCast(it.Value());
749     const TopoDS_Shape& CW = P->GetShape();
750     Wires.Append(CW);
751     W2P.Bind(CW, P);
752   }
753
754   HYDROData_DataMapOfShapeListOfShape newDM;
755   TopTools_IndexedDataMapOfShapeShape OutNE1;
756
757   BRep_Builder BB;
758   TopoDS_Compound cmpDM;
759   BB.MakeCompound(cmpDM);
760   TopTools_ListOfShape out;
761
762   for (int i = 1; i <= DM.Extent();i++)
763   {
764     const TopoDS_Shape& K = DM.FindKey(i);
765     BB.Add(cmpDM, K);
766   }
767
768   TopTools_IndexedDataMapOfShapeListOfShape OInSH2MSH;
769   CutByEdges(cmpDM, Wires, out, true, &OutNE1, &OInSH2MSH);
770
771   for (int i = 1; i <= DM.Extent();i++)
772   {
773     const TopoDS_Shape& K = DM.FindKey(i);
774     const TopTools_ListOfShape& V = DM.FindFromIndex(i);  
775     TopTools_ListOfShape modifSh = OInSH2MSH.FindFromKey(K);
776     if (!modifSh.IsEmpty())
777     {
778       TopTools_ListIteratorOfListOfShape it(modifSh);
779       for (;it.More(); it.Next())
780       {
781         const TopoDS_Shape& NF = it.Value();
782         if (!NF.IsNull())
783           newDM.Add(NF, V);
784       }
785     }
786     else
787       newDM.Add(K, V); //was not modified by bop (CutByEdges)
788   }
789
790
791
792   for (int i = 1; i <= OutNE1.Extent(); i++)
793     OutNE->Bind(OutNE1.FindKey(i), W2P(OutNE1.FindFromIndex(i)));
794
795   DM = newDM;
796 }
797
798 void HYDROData_SplitToZonesTool::CutFaceByEdges(const TopoDS_Face& in, 
799                                                 TopTools_ListOfShape& out, 
800                                                 const HYDROData_SequenceOfObjects& thePolylines,
801                                                 NCollection_DataMap<TopoDS_Shape, Handle(HYDROData_PolylineXY), TopTools_ShapeMapHasher>* OutNE,                                                
802                                                 TopTools_IndexedDataMapOfShapeListOfShape* OutOrSh2M)
803 {
804
805   HYDROData_SequenceOfObjects::Iterator it(thePolylines);
806   TopTools_ListOfShape Wires;
807   NCollection_DataMap<TopoDS_Shape, Handle(HYDROData_PolylineXY), TopTools_ShapeMapHasher> W2P;
808   for (;it.More();it.Next())
809   {
810     Handle(HYDROData_PolylineXY) P = Handle(HYDROData_PolylineXY)::DownCast(it.Value());
811     const TopoDS_Shape& CW = P->GetShape();
812     Wires.Append(CW);
813     W2P.Bind(CW, P);
814   }
815
816   TopTools_IndexedDataMapOfShapeShape OutNE1;
817   CutByEdges(in, Wires, out, false, &OutNE1, OutOrSh2M);
818
819   for (int i = 1; i <= OutNE1.Extent(); i++)
820     OutNE->Bind(OutNE1.FindKey(i), W2P(OutNE1.FindFromIndex(i)));
821
822 }
823
824 int HYDROData_SplitToZonesTool::CutByEdges(const TopoDS_Shape& InSh, const TopTools_ListOfShape& InW,
825                                            TopTools_ListOfShape& outShs, 
826                                            bool OutShapeAsRes,
827                                            TopTools_IndexedDataMapOfShapeShape* OutNE, 
828                                            TopTools_IndexedDataMapOfShapeListOfShape* OInSH2MSH)
829 {
830   int anError;
831   if (InSh.IsNull())
832     return -1;
833   if (InW.IsEmpty())
834   {
835     outShs.Clear();
836     outShs.Append(InSh);
837     return 0;
838   }
839   TopTools_ListIteratorOfListOfShape anIt;
840   BOPAlgo_Builder anAlgo;
841   anAlgo.AddArgument(InSh);
842
843   anIt.Initialize( InW );
844   for( ; anIt.More(); anIt.Next() )
845     anAlgo.AddArgument( anIt.Value() );
846
847   anAlgo.Perform(); 
848   if (anAlgo.HasErrors())
849     return -1;
850
851   if (!OutShapeAsRes)
852   {
853     outShs = anAlgo.Modified( InSh );
854     if (outShs.IsEmpty())
855       outShs.Append(InSh);
856   }
857   else
858   {
859     TopoDS_Shape Res = anAlgo.Shape();
860     outShs.Append(Res);
861   }
862
863   if (OutNE)
864   {
865     TopTools_IndexedDataMapOfShapeShape NonFOutNE;
866     anIt.Initialize( InW );
867     for( ; anIt.More(); anIt.Next() )
868     {
869       const TopoDS_Shape& OSH = anIt.Value(); 
870       TopExp_Explorer exp(OSH, TopAbs_EDGE);
871       for (;exp.More();exp.Next())
872       {
873         const TopoDS_Edge& OE = TopoDS::Edge(exp.Current());
874         const TopTools_ListOfShape& ls = anAlgo.Modified(OE);
875         TopTools_ListIteratorOfListOfShape itls(ls);
876         for (;itls.More();itls.Next())
877           NonFOutNE.Add(itls.Value(), OSH);
878       }
879     }
880
881     //filter edges; if face contain an edge => keep it
882     TopTools_IndexedMapOfShape AllEdges; //edges from output shape
883     for (anIt.Initialize(outShs); anIt.More();anIt.Next())
884       TopExp::MapShapes(anIt.Value(), TopAbs_EDGE, AllEdges);
885
886     for (int i = 1; i <= NonFOutNE.Extent(); i++)
887     {
888       const TopoDS_Shape& CE = NonFOutNE.FindKey(i);
889       const TopoDS_Shape& V = NonFOutNE.FindFromIndex(i);
890       if (AllEdges.Contains(CE))
891         OutNE->Add(CE, V);
892     }
893   }
894
895   //map all sub-shapes from the original InSh to modified shapes  
896   if (OInSH2MSH)
897   {
898     TopTools_IndexedMapOfShape allSh;
899     TopExp::MapShapes(InSh, allSh);
900     for(int i = 1; i <= allSh.Extent(); i++ )
901     {
902       const TopoDS_Shape& or_sh = allSh(i); 
903       const TopTools_ListOfShape& ls = anAlgo.Modified(or_sh);
904       OInSH2MSH->Add(or_sh, ls);
905     }
906   }
907
908   return 0;
909 }