Salome HOME
d076706fadf98920b6266bf9f770ddc7d9932e80
[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     //TCollection_AsciiString aName = aNam + i + ".brep";
217     //BRepTools::Write(aSh, aName.ToCString());
218   }
219 #ifdef DEB_SPLIT_TO_ZONES_CHECK_PARTITION
220   TCollection_AsciiString aNameBefore = fileNameBefore + ".brep";
221   BRepTools::Write(aCmp, aNameBefore.ToCString());
222 #endif
223
224   HYDROData_DataMapOfShapeListOfShape aDM1;
225   if(anInputSplitDataList.size() > 1) {
226   HYDROData_Transform splitTool; 
227   const Standard_Integer anErr = SplitFaces(aCmp, splitTool);    
228   if(anErr)
229     return anOutputSplitDataList;
230   aResult = splitTool.Shape();
231   if (aResult.IsNull()) 
232     return anOutputSplitDataList;
233   BRepCheck_Analyzer aCheck (aResult);
234   if(!aCheck.IsValid()) {
235 #ifdef DEB_SPLIT_TO_ZONES
236     cout << "result is not valid" <<endl;
237     BRepTools::Write(aResult, "SplitFacesNV.brep");  
238 #endif
239     return anOutputSplitDataList;
240   }
241 #ifdef DEB_SPLIT_TO_ZONES
242   BRepTools::Write(aResult, "SplitFacesV.brep");
243 #endif
244   
245   // Step 3. Collect history  
246   //HYDROData_DataMapOfShapeListOfShape aDM1;
247   BOPCol_ListIteratorOfListOfShape anIt(aLS); 
248   //TCollection_AsciiString aNamM ("EdgM_");
249   //TCollection_AsciiString aNamG ("EdgG_");
250   for (int i =1;anIt.More();anIt.Next(),i++) {
251         Standard_Boolean foundF(Standard_False);
252     const TopTools_ListOfShape& aListOfNew = splitTool.Modified(anIt.Value());
253         if(!aListOfNew.IsEmpty())
254           foundF = Standard_True;
255     TopTools_ListOfShape aList;
256     TopTools_ListIteratorOfListOfShape it(aListOfNew);
257     for(;it.More();it.Next())       
258       aList.Append(it.Value());
259     // Bug in History: partition should give only modified entities! => temporary solution is used
260     const TopTools_ListOfShape& aListOfGen = splitTool.Generated(anIt.Value());
261         if(!aListOfGen.IsEmpty())
262           foundF = Standard_True;
263         it.Initialize(aListOfGen);    
264         for(;it.More();it.Next())     
265           aList.Append(it.Value());
266     if(!foundF) // face is not modified
267           aList.Append (anIt.Value());
268         aDM1.Bind(anIt.Value(), aList);
269 #ifdef DEB_SPLIT_TO_ZONES
270         TCollection_AsciiString aName;
271 #endif
272         if(!anInputGroupList.isEmpty() ) { // 1
273           TopExp_Explorer exp (anIt.Value(), TopAbs_EDGE);
274           for (int j =1;exp.More();exp.Next(),j++) {
275         aList.Clear();
276             Standard_Boolean foundE(Standard_False);
277             const TopTools_ListOfShape& aListM = splitTool.Modified(exp.Current());     
278                 //cout << "NB_EDGE_M = " << aListM.Extent() <<endl;
279                 if(aListM.Extent()) foundE = Standard_True;
280                 it.Initialize(aListM);    
281                 for(int k=1;it.More();it.Next(),k++) {    
282               aList.Append(it.Value());
283                   //aName = aNamM + i + j +k +".brep";
284                   //BRepTools::Write(it.Value(),aName.ToCString());
285                 }
286                 const TopTools_ListOfShape& aListG = splitTool.Generated(exp.Current());
287                 if(aListG.Extent()) foundE = Standard_True;
288                 it.Initialize(aListG);    
289                 for(int k=1;it.More();it.Next(),k++)   {  
290               aList.Append(it.Value());
291                   //aName = aNamG + i + j +k +".brep";
292                   //BRepTools::Write(it.Value(),aName.ToCString());
293                 }
294                 //cout << "NB_EDGE = " << aList.Extent() <<endl;
295                 if(!foundE) {
296                   aList.Append (exp.Current());
297 #ifdef DEB_SPLIT_TO_ZONES
298                   aName = aNamG + i + j +".brep";
299                   BRepTools::Write(exp.Current(),aName.ToCString());
300                   cout << aName.ToCString()<< " = " << exp.Current().TShape() <<endl;
301 #endif
302                 }
303                 aDM1.Bind(exp.Current(), aList);
304           }
305         }
306   }
307   } else 
308           aResult = anInputSplitDataList.at(0).Shape; // get single input shape
309   // aDM2: NewShape ==> ListOfOldShapes
310   HYDROData_DataMapOfShapeListOfShape aDM2;
311   // make limiting face
312   HYDROData_DataMapOfShapeListOfShape aDM4;
313   Standard_Boolean hasLimits(Standard_False);
314   QString aBndName;
315   HYDROData_MapOfShape aBndView;
316   if (! thePolyline.IsNull()) {    
317     const TopoDS_Wire aBndWire = TopoDS::Wire(thePolyline->GetShape());
318     if(!aBndWire.IsNull()) {
319       TopoDS_Face limFace;        
320           if(buildLimFace(aBndWire, limFace)) {
321                 TopoDS_Shape aComResult;
322             BRepAlgoAPI_Common mkCom(aResult, limFace);
323                 if(mkCom.IsDone()) {
324               aComResult = mkCom.Shape();
325                   BRepCheck_Analyzer aCheck (aComResult);
326           if(aCheck.IsValid()) {
327 #ifdef DEB_SPLIT_TO_ZONES
328                     BRepTools::Write(aComResult,"CommonV.brep");
329 #endif  
330                         aBndName = thePolyline->GetName();
331                     hasLimits = Standard_True; // DM2 should be filled here
332                         TopExp_Explorer exp (limFace, TopAbs_EDGE);
333                         for (int i =1;exp.More();exp.Next(),i++) {
334               const TopoDS_Shape& anEdge = exp.Current();
335                           if(anEdge.IsNull()) continue;
336                           aBndView.Add(anEdge);
337                         }
338                     HYDROData_MapOfShape aView;
339                         exp.Init (aResult, TopAbs_FACE);
340                         for (int i =1;exp.More();exp.Next(),i++) {
341               const TopoDS_Shape& aFace = exp.Current();
342                       if(!aFace.IsNull()) {
343                         const TopTools_ListOfShape& aListOfNew = mkCom.Modified(aFace);
344                             //cout << "Modified: " << aListOfNew.Extent() <<endl;                       
345                 const TopTools_ListOfShape& aListOfGen = mkCom.Generated(aFace);                        
346                             if(!aListOfNew.IsEmpty()) {
347                       aDM4.Bind(aFace, aListOfNew);
348                               //TCollection_AsciiString aName = aNam + i + ".brep";
349                                   //BRepTools::Write(aListOfNew.Last(), aName.ToCString());                             
350                                 }
351                                 else {
352                               if(!mkCom.IsDeleted(aFace)) {
353                                     if(!aListOfGen.IsEmpty()) {
354                           aDM4.Bind(aFace, aListOfGen);                        
355                                       //TCollection_AsciiString aName = aNam + i + "g.brep";
356                                       //BRepTools::Write(aListOfGen.Last(), aName.ToCString());
357                                         }
358                                          else {
359                                     TopTools_ListOfShape aList; 
360                                         aList.Append(aFace);
361                                         aDM4.Bind(aFace, aList); //the same face - not modified
362                                          }
363                                   }
364                          }
365                          TopExp_Explorer exp2 (aFace, TopAbs_EDGE);
366                          for (int j =1;exp2.More();exp2.Next(),j++) {
367                            const TopoDS_Shape& anEdge = exp2.Current();
368                            if(!anEdge.IsNull()) {
369                                  if(aView.Contains(anEdge)) continue;
370                                  aView.Add(anEdge);
371                                  const TopTools_ListOfShape& aListOfNewEd = mkCom.Modified(anEdge);
372                                  const TopTools_ListOfShape& aListOfGenEd = mkCom.Generated(anEdge);
373                  if(!aListOfNewEd.IsEmpty())
374                        aDM4.Bind(anEdge, aListOfNewEd);
375                                  else {
376                                    if(!mkCom.IsDeleted(anEdge)) {
377                                          if(!aListOfGenEd.IsEmpty()) {
378                                            aDM4.Bind(anEdge, aListOfGenEd);
379                                          } else {
380                                            TopTools_ListOfShape aList; 
381                                        aList.Append(anEdge);
382                                        aDM4.Bind(anEdge, aList);//the same edge - not modified
383                                          }
384                                    }
385                                  }
386                            }
387                          }
388                         }
389                   } //end DM4 filling (phase 1)
390                     //cout << "DM4 Ext = " <<aDM4.Extent() <<endl;
391                  // phase 2 (from tool)
392          TopExp_Explorer expt (limFace, TopAbs_EDGE);
393                  for(;expt.More();expt.Next()) {
394                    const TopoDS_Shape& anEdge = expt.Current();
395                    if(!anEdge.IsNull()) {
396              const TopTools_ListOfShape& aListOfNewEd = mkCom.Modified(anEdge);
397                          const TopTools_ListOfShape& aListOfGenEd = mkCom.Generated(anEdge);
398              if(!aListOfNewEd.IsEmpty())
399                    aDM4.Bind(anEdge, aListOfNewEd);
400                          else {
401                            if(!mkCom.IsDeleted(anEdge)) {
402                                  if(!aListOfGenEd.IsEmpty()) {
403                                    aDM4.Bind(anEdge, aListOfGenEd);
404                                  } else {
405                                    TopTools_ListOfShape aList; 
406                                aList.Append(anEdge);
407                                aDM4.Bind(anEdge, aList);//the same edge - not modified
408                                  }
409                            }
410                          }
411                    }
412                  }
413                  //cout << "DM4 Ext = " <<aDM4.Extent() <<endl;
414                  if(limplus1Object) {
415                    // fill DM1 (old - new) and DM2 (new - old)
416                    HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape mIt(aDM4);
417            for(;mIt.More();mIt.Next()) {
418                          const TopoDS_Shape& aKey = mIt.Key();//old
419                          TopTools_ListOfShape aList;
420                          aList.Append(aKey);
421                          const TopTools_ListOfShape& aListOfNew = mIt.Value();
422                          aDM1.Bind(aKey, aListOfNew);
423                          TopTools_ListIteratorOfListOfShape it(aListOfNew);
424                          for(;it.More();it.Next()) {
425                            if(!aDM2.IsBound(it.Value()))
426                                  aDM2.Bind(it.Value(), aList);
427                            else {
428                                  TopTools_ListOfShape& aList = aDM2.ChangeFind(it.Value());
429                                  aList.Prepend(aKey);
430                            }
431                          }
432                    }
433                  } else {
434          HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape aMIt(aDM1); 
435                  //DM4 contains Old - New after common op. DM1: old - new after Split op.
436                  for(;aMIt.More();aMIt.Next()) {
437            const TopoDS_Shape& aKey = aMIt.Key();
438            TopTools_ListOfShape aList;
439            aList.Append(aKey);
440            const TopTools_ListOfShape& aListOfNew = aMIt.Value();
441            TopTools_ListIteratorOfListOfShape it(aListOfNew);
442            for(;it.More();it.Next()) {// iterate new: b1, b2, b3...
443                          if(!aDM4.IsBound(it.Value())) // bi - is deleted
444                continue; // go to the next bi
445                          else {
446                                 const TopTools_ListOfShape& aListOfNew4 = aDM4.Find(it.Value());
447                             TopTools_ListIteratorOfListOfShape it4(aListOfNew4); // {c1, c2, c3,...}
448                 for(;it4.More();it4.Next()) {
449                                   if(!aDM2.IsBound(it4.Value()))
450                     aDM2.Bind(it4.Value(), aList);
451                               else {
452                     TopTools_ListOfShape& aList = aDM2.ChangeFind(it4.Value());
453                     aList.Prepend(aKey);
454                                   }
455                                 }
456                          }
457                    }
458                  }
459                  }
460                  } else {
461                          hasLimits = Standard_False;
462 #ifdef DEB_SPLIT_TO_ZONES
463                      BRepTools::Write(aComResult,"CommonNV.brep");
464 #endif  
465                   }
466            }
467           }
468         }
469   }// end limits processing
470   if(!hasLimits) {
471     HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape aMIt(aDM1);
472     for(;aMIt.More();aMIt.Next()) {
473       const TopoDS_Shape& aKey = aMIt.Key();
474       TopTools_ListOfShape aList;
475       aList.Append(aKey);
476       const TopTools_ListOfShape& aListOfNew = aMIt.Value();
477       TopTools_ListIteratorOfListOfShape it(aListOfNew);
478       for(;it.More();it.Next()) {
479         if(!aDM2.IsBound(it.Value()))
480           aDM2.Bind(it.Value(), aList);
481         else {
482           TopTools_ListOfShape& aList = aDM2.ChangeFind(it.Value());
483           aList.Prepend(aKey);
484                 }
485           }
486         }
487   }
488   // Step 4. Fill output structure.
489 #ifdef DEB_SPLIT_TO_ZONES
490   TCollection_AsciiString aNam4 ("SC_");
491 #endif  
492   HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape aMIt(aDM2);
493   for(int i =1;aMIt.More();aMIt.Next(),i++) {
494     SplitData aDestSplitData;
495     const TopoDS_Shape& aKey = aMIt.Key(); //new
496     aDestSplitData.Shape = aKey;
497     if(aKey.ShapeType() == TopAbs_FACE)
498       aDestSplitData.Type = SplitData::Data_Zone;
499         else {
500       aDestSplitData.Type = SplitData::Data_Edge;
501 #ifdef DEB_SPLIT_TO_ZONES
502           TCollection_AsciiString aName = aNam4 + i + ".brep";
503           BRepTools::Write(aKey,aName.ToCString());
504 #endif  
505         }
506
507     QStringList aListOfNames; // names processing
508     const TopTools_ListOfShape& aListOfOld = aMIt.Value();
509     TopTools_ListIteratorOfListOfShape it(aListOfOld);
510         for(int j =1;it.More();it.Next(),j++) { 
511           const TopoDS_Shape& aSh = it.Value(); //old
512           if(aDM3.IsBound(aSh)) {
513             const QStringList& ObjectNames = aDM3.Find(aSh);    
514                 aListOfNames.append(ObjectNames);
515 #ifdef DEB_SPLIT_TO_ZONES
516                 TCollection_AsciiString aName = aNam4 + i +"_" + j + ".brep";
517             BRepTools::Write(aSh ,aName.ToCString());           
518 #endif  
519           }     else {
520                   if(aBndView.Contains(aSh) && hasLimits) {                     
521                         aListOfNames.append(aBndName);
522 #ifdef DEB_SPLIT_TO_ZONES
523                         cout << " BndName = "<<aBndName.toStdString() <<endl; 
524 #endif
525                   }
526           }
527         }
528
529         aDestSplitData.ObjectNames = aListOfNames;  
530         anOutputSplitDataList.append(aDestSplitData);   
531 #ifdef DEB_SPLIT_TO_ZONES
532         QString aStr = aDestSplitData.ObjectNames.join(" "); 
533         cout << "New# = " << i<< " Names = "<<aStr.toStdString() << " size = " <<aDestSplitData.ObjectNames.size() <<endl; 
534 #endif
535   }
536  
537   return anOutputSplitDataList;
538 }
539
540 bool HYDROData_SplitToZonesTool::SplitTwoData( const SplitData& theData1,
541                                                const SplitData& theData2,
542                                                SplitData& theData1Subtracted,
543                                                SplitData& theData2Subtracted,
544                                                SplitData& theDataIntersected )
545 {
546   const TopoDS_Shape& aShape1 = theData1.Shape;
547   const TopoDS_Shape& aShape2 = theData2.Shape;
548
549   const QStringList& anObjectNames1 = theData1.ObjectNames;
550   const QStringList& anObjectNames2 = theData2.ObjectNames;
551
552   BRepAlgoAPI_Common aCommon( aShape1, aShape2 );
553   TopoDS_Shape aCommonShape = aCommon.Shape();
554   if( aCommonShape.IsNull() )
555   {
556     theData1Subtracted = theData1;
557     theData2Subtracted = theData2;
558     return false;
559   }
560
561   BRepAlgoAPI_Cut aCut1( aShape1, aShape2 );
562   TopoDS_Shape aCut1Shape = aCut1.Shape();
563
564   BRepAlgoAPI_Cut aCut2( aShape2, aShape1 );
565   TopoDS_Shape aCut2Shape = aCut2.Shape();
566
567   theData1Subtracted = SplitData( SplitData::Data_Zone, aCut1Shape, anObjectNames1 );
568   theData2Subtracted = SplitData( SplitData::Data_Zone, aCut2Shape, anObjectNames2 );
569   theDataIntersected = SplitData( SplitData::Data_Zone, aCommonShape, anObjectNames1 + anObjectNames2 );
570
571   return true;
572 }
573
574 HYDROData_SplitToZonesTool::SplitDataList
575 HYDROData_SplitToZonesTool::ExtractSeparateData( const SplitData& theData )
576 {
577   SplitDataList aSplitDataList;
578   TopExp_Explorer anExp( theData.Shape, TopAbs_FACE );
579   for( ; anExp.More(); anExp.Next() )
580   {
581     TopoDS_Shape aShape = anExp.Current();
582     if( aShape.ShapeType() == TopAbs_FACE )
583     {
584       TopoDS_Face aFace = TopoDS::Face( aShape );
585       if( !aFace.IsNull() )
586       {
587         SplitData aSplitData( SplitData::Data_Zone, aFace, theData.ObjectNames );
588         aSplitDataList.append( aSplitData );
589       }
590     }
591   }
592   return aSplitDataList;
593 }