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