Salome HOME
95c514deac9b400f8e009767f5f2ae8f788db556
[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 #ifdef DEB_SPLIT_TO_ZONES
121   TCollection_AsciiString aNam("Faces_");
122 #endif
123   for( int anIndex = 1, aLength = theObjectList.Length(); anIndex <= aLength; anIndex++ )
124   {
125     Handle(HYDROData_Object) aGeomObj = 
126       Handle(HYDROData_Object)::DownCast( theObjectList.Value( anIndex ) );
127     if( aGeomObj.IsNull() )
128       continue;
129
130     TopoDS_Shape aShape = aGeomObj->GetTopShape();
131     if ( aShape.IsNull() )
132       continue;  
133 #ifdef DEB_SPLIT_TO_ZONES
134    TCollection_AsciiString aName = aNam + anIndex + ".brep";
135    BRepTools::Write(aShape, aName.ToCString());
136 #endif
137     if ( aShape.ShapeType() == TopAbs_COMPOUND ) {
138       // Create split data for each face contained in the compound
139       TopExp_Explorer anExp( aShape, TopAbs_FACE );
140       for ( ; anExp.More(); anExp.Next() ) {
141         const TopoDS_Face& aFace = TopoDS::Face( anExp.Current() );
142         if ( !aFace.IsNull() ) {
143           SplitData aSplitData( SplitData::Data_Zone, aFace, aGeomObj->GetName() );
144           anInputSplitDataList.append( aSplitData );
145         }
146       }
147     } else {
148       SplitData aSplitData( SplitData::Data_Zone, aShape, aGeomObj->GetName() );
149       anInputSplitDataList.append( aSplitData );
150     }
151   }
152   //
153   SplitDataList anInputGroupList;
154   for( int anIndex = 1, aLength = theGroupsList.Length(); anIndex <=aLength; anIndex++ )
155   {
156     Handle(HYDROData_ShapesGroup) aGeomGroup = 
157       Handle(HYDROData_ShapesGroup)::DownCast( theGroupsList.Value( anIndex ) );
158     if( aGeomGroup.IsNull() )
159       continue;
160       
161     TopTools_SequenceOfShape aGroupShapes;
162     aGeomGroup->GetShapes( aGroupShapes );
163     for( int i = 1, aNbShapes = aGroupShapes.Length(); i <= aNbShapes; i++ ) {
164       const TopoDS_Shape& aGroupShape = aGroupShapes.Value( i );
165       if ( aGroupShape.IsNull() )
166         continue;  
167
168       if ( aGroupShape.ShapeType() == TopAbs_COMPOUND ) {    
169         TopExp_Explorer anExp( aGroupShape, TopAbs_EDGE );
170         for ( ; anExp.More(); anExp.Next() ) {
171           const TopoDS_Edge& anEdge = TopoDS::Edge( anExp.Current() );
172           if ( !anEdge.IsNull() ) {
173             SplitData aSplitData( SplitData::Data_Edge, anEdge, aGeomGroup->GetName() );
174             anInputGroupList.append( aSplitData );
175           }
176                 }
177       } else {
178         SplitData aSplitData( SplitData::Data_Edge, aGroupShape, aGeomGroup->GetName() );
179         anInputGroupList.append( aSplitData );
180 #ifdef DEB_SPLIT_TO_ZONES
181                 QString aStr = aSplitData.ObjectNames.join(" "); 
182                 cout << "Group# = " <<anIndex <<" Nmb in grp = " << i<< " Names = "<<aStr.toStdString() << " size = " <<aSplitData.ObjectNames.size() <<endl;   
183 #endif
184       }
185         }
186   }
187   // If only one shape is given we don't split it 
188   // algorithm just returns the unpacked input data
189   bool limplus1Object(false);
190   if(theObjectList.Size() == 1 )  {
191         if(thePolyline.IsNull()) {
192           anOutputSplitDataList.append(anInputSplitDataList); 
193           if(!theGroupsList.IsEmpty() ) 
194             anOutputSplitDataList.append(anInputGroupList);     
195       return anOutputSplitDataList;
196         } else
197                 limplus1Object = true;// size =1 && hasLimits
198   }
199   HYDROData_DataMapOfShapeListOfString aDM3;
200   if(!anInputGroupList.isEmpty()) {// Old edge ==> List_Of_Names
201     QStringList aListOfNames;
202     for (int i=0;i < anInputGroupList.size() ;i++) {
203       const TopoDS_Shape& aSh = anInputGroupList.at(i).Shape;
204       aDM3.Bind(aSh, anInputGroupList.at(i).ObjectNames);
205     }
206   }
207
208   // Step 1. Prepare Partition structures. 
209   TopoDS_Shape aResult;
210   BOPCol_ListOfShape aLS;  
211   QStringList aListOfNames;
212   TopoDS_Compound aCmp;
213   BRep_Builder aBB;
214   aBB.MakeCompound(aCmp);
215   for (int i=0;i < anInputSplitDataList.size() ;i++) {
216     const TopoDS_Shape& aSh = anInputSplitDataList.at(i).Shape;
217     aDM3.Bind(aSh, anInputSplitDataList.at(i).ObjectNames);
218     aLS.Append(aSh);
219         aBB.Add(aCmp,aSh);
220 #ifdef DEB_SPLIT_TO_ZONES
221     //TCollection_AsciiString aName = aNam + i + ".brep";
222     //BRepTools::Write(aSh, aName.ToCString());
223 #endif
224   }
225 #ifdef DEB_SPLIT_TO_ZONES_CHECK_PARTITION
226   TCollection_AsciiString aNameBefore = fileNameBefore + ".brep";
227   BRepTools::Write(aCmp, aNameBefore.ToCString());
228 #endif
229
230   HYDROData_DataMapOfShapeListOfShape aDM1;
231   if(anInputSplitDataList.size() > 1) {
232   HYDROData_Transform splitTool; 
233   const Standard_Integer anErr = SplitFaces(aCmp, splitTool);    
234   if(anErr)
235     return anOutputSplitDataList;
236   aResult = splitTool.Shape();
237   if (aResult.IsNull()) 
238     return anOutputSplitDataList;
239   BRepCheck_Analyzer aCheck (aResult);
240   if(!aCheck.IsValid()) {
241 #ifdef DEB_SPLIT_TO_ZONES
242     cout << "result is not valid" <<endl;
243     BRepTools::Write(aResult, "SplitFacesNV.brep");  
244 #endif
245     return anOutputSplitDataList;
246   }
247 #ifdef DEB_SPLIT_TO_ZONES
248   BRepTools::Write(aResult, "SplitFacesV.brep");
249 #endif
250   
251   // Step 3. Collect history  
252   //HYDROData_DataMapOfShapeListOfShape aDM1;
253   BOPCol_ListIteratorOfListOfShape anIt(aLS); 
254 #ifdef DEB_SPLIT_TO_ZONES
255   TCollection_AsciiString aNamM ("EdgM_");
256   TCollection_AsciiString aNamG ("EdgG_");
257 #endif
258   for (int i =1;anIt.More();anIt.Next(),i++) {
259         Standard_Boolean foundF(Standard_False);
260     const TopTools_ListOfShape& aListOfNew = splitTool.Modified(anIt.Value());
261         if(!aListOfNew.IsEmpty())
262           foundF = Standard_True;
263     TopTools_ListOfShape aList;
264     TopTools_ListIteratorOfListOfShape it(aListOfNew);
265     for(;it.More();it.Next())       
266       aList.Append(it.Value());    
267     if(!foundF) // face is not modified
268           aList.Append (anIt.Value());
269         aDM1.Bind(anIt.Value(), aList);
270 #ifdef DEB_SPLIT_TO_ZONES
271         TCollection_AsciiString aName;
272 #endif
273         if(!anInputGroupList.isEmpty() ) { // 1
274           TopExp_Explorer exp (anIt.Value(), TopAbs_EDGE);
275           for (int j =1;exp.More();exp.Next(),j++) {
276         aList.Clear();
277             Standard_Boolean foundE(Standard_False);
278             const TopTools_ListOfShape& aListM = splitTool.Modified(exp.Current());     
279                 //cout << "NB_EDGE_M = " << aListM.Extent() <<endl;
280                 if(aListM.Extent()) foundE = Standard_True;
281                 it.Initialize(aListM);    
282                 for(int k=1;it.More();it.Next(),k++) {    
283               aList.Append(it.Value());
284 #ifdef DEB_SPLIT_TO_ZONES
285                   //aName = aNamM + i + j +k +".brep";
286                   //BRepTools::Write(it.Value(),aName.ToCString());
287 #endif
288                 }
289                 //cout << "NB_EDGE = " << aList.Extent() <<endl;
290                 if(!foundE) {
291                   aList.Append (exp.Current());
292 #ifdef DEB_SPLIT_TO_ZONES
293                   aName = aNamG + i + j +".brep";
294                   BRepTools::Write(exp.Current(),aName.ToCString());
295                   cout << aName.ToCString()<< " = " << exp.Current().TShape() <<endl;
296 #endif
297                 }
298                 aDM1.Bind(exp.Current(), aList);
299           }
300         }
301   }
302   } else 
303           aResult = anInputSplitDataList.at(0).Shape; // get single input shape
304   // aDM2: NewShape ==> ListOfOldShapes
305   HYDROData_DataMapOfShapeListOfShape aDM2;
306   // make limiting face
307   HYDROData_DataMapOfShapeListOfShape aDM4;
308   Standard_Boolean hasLimits(Standard_False);
309   QString aBndName;
310   HYDROData_MapOfShape aBndView;
311   if (! thePolyline.IsNull()) {    
312     const TopoDS_Wire aBndWire = TopoDS::Wire(thePolyline->GetShape());
313     if(!aBndWire.IsNull()) {
314       TopoDS_Face limFace;        
315           if(buildLimFace(aBndWire, limFace)) {
316                 TopoDS_Shape aComResult;
317             BRepAlgoAPI_Common mkCom(aResult, limFace);
318                 if(mkCom.IsDone()) {
319               aComResult = mkCom.Shape();
320                   BRepCheck_Analyzer aCheck (aComResult);
321           if(aCheck.IsValid()) {
322 #ifdef DEB_SPLIT_TO_ZONES
323                     BRepTools::Write(aComResult,"CommonV.brep");
324                         BRepTools::Write(limFace,"limFace.brep");
325 #endif  
326                         aBndName = thePolyline->GetName();
327                     hasLimits = Standard_True; // DM2 should be filled here
328                         TopExp_Explorer exp (limFace, TopAbs_EDGE);
329                         for (int i =1;exp.More();exp.Next(),i++) {
330               const TopoDS_Shape& anEdge = exp.Current();
331                           if(anEdge.IsNull()) continue;
332                           aBndView.Add(anEdge);
333                   QStringList aListOfNames;
334                       aListOfNames.append(aBndName);
335                           aDM3.Bind(anEdge, aListOfNames);
336                           TopTools_ListOfShape aList;
337                           aList.Append(anEdge);
338                           aDM1.Bind(anEdge,aList);
339                         }
340                     HYDROData_MapOfShape aView;
341                         exp.Init (aResult, TopAbs_FACE);
342                         for (int i =1;exp.More();exp.Next(),i++) {
343               const TopoDS_Shape& aFace = exp.Current();
344                       if(!aFace.IsNull()) {
345                         const TopTools_ListOfShape& aListOfNew = mkCom.Modified(aFace);
346                             //cout << "Modified: " << aListOfNew.Extent() <<endl;                                                       
347                             if(!aListOfNew.IsEmpty()) {
348                       aDM4.Bind(aFace, aListOfNew);
349 #ifdef DEB_SPLIT_TO_ZONES
350                               //TCollection_AsciiString aName = aNam + i + ".brep";
351                                   //BRepTools::Write(aListOfNew.Last(), aName.ToCString());                             
352 #endif
353                                 }
354                                 else {
355                                         if(!mkCom.IsDeleted(aFace)) {
356                                           const TopTools_ListOfShape& aListOfGen = mkCom.Generated(aFace);      
357                                       if(!aListOfGen.IsEmpty()) {
358                             aDM4.Bind(aFace, aListOfGen);       
359 #ifdef DEB_SPLIT_TO_ZONES
360                                         //TCollection_AsciiString aName = aNam + i + "g.brep";
361                                         //BRepTools::Write(aListOfGen.Last(), aName.ToCString());
362 #endif
363                                           }
364                                            else {
365                                     TopTools_ListOfShape aList; 
366                                         aList.Append(aFace);
367                                         aDM4.Bind(aFace, aList); //the same face - not modified
368                                            }
369                                         }
370                          }
371                          TopExp_Explorer exp2 (aFace, TopAbs_EDGE);
372                          for (int j =1;exp2.More();exp2.Next(),j++) {
373                            const TopoDS_Shape& anEdge = exp2.Current();
374                            if(!anEdge.IsNull()) {
375                                  if(aView.Contains(anEdge)) continue;
376                                  aView.Add(anEdge);
377                                  const TopTools_ListOfShape& aListOfNewEd = mkCom.Modified(anEdge);                              
378                  if(!aListOfNewEd.IsEmpty())
379                        aDM4.Bind(anEdge, aListOfNewEd);
380                                  else {
381                                    if(!mkCom.IsDeleted(anEdge)) {
382                                          const TopTools_ListOfShape& aListOfGenEd = mkCom.Generated(anEdge);
383                                          if(!aListOfGenEd.IsEmpty()) {
384                                            aDM4.Bind(anEdge, aListOfGenEd);
385                                          } else {
386                                            TopTools_ListOfShape aList; 
387                                        aList.Append(anEdge);
388                                        aDM4.Bind(anEdge, aList);//the same edge - not modified
389                                          }
390                                    }
391                                  }
392                            }
393                          }
394                         }
395                         } //end DM4 filling (phase 1)
396                     //cout << "DM4 Ext = " <<aDM4.Extent() <<endl;
397                  // phase 2 (from tool)
398 #ifdef DEB_SPLIT_TO_ZONES
399                  TCollection_AsciiString aNam("BndEd_");
400 #endif
401          TopExp_Explorer expt (limFace, TopAbs_EDGE);
402                  for(int i =1;expt.More();expt.Next(),i++) {
403                    const TopoDS_Shape& anEdge = expt.Current();
404                    if(!anEdge.IsNull()) {
405              const TopTools_ListOfShape& aListOfNewEd = mkCom.Modified(anEdge); 
406 #ifdef DEB_SPLIT_TO_ZONES
407                          TopTools_ListIteratorOfListOfShape itl(aListOfNewEd);
408                          for(int j=1;itl.More();itl.Next(),j++) {
409                            TCollection_AsciiString aName = aNam + i + "_" + j + ".brep";
410                            BRepTools::Write(itl.Value(), aName.ToCString());
411                            cout <<aName.ToCString()<<" = "<< itl.Value().TShape() <<endl;
412                          }
413 #endif
414              if(!aListOfNewEd.IsEmpty())
415                    aDM4.Bind(anEdge, aListOfNewEd);
416                          else {
417                            if(!mkCom.IsDeleted(anEdge)) {
418                  const TopTools_ListOfShape& aListOfGenEd = mkCom.Generated(anEdge);
419                                  if(!aListOfGenEd.IsEmpty()) {
420                                    aDM4.Bind(anEdge, aListOfGenEd);
421                                  } else {
422                                    TopTools_ListOfShape aList; 
423                                aList.Append(anEdge);
424                                aDM4.Bind(anEdge, aList);//the same edge - not modified
425                                  }
426                            }
427                          }
428                    }
429                  }
430                  //cout << "DM4 Ext = " <<aDM4.Extent() <<endl;
431                  if(limplus1Object) {
432                    // fill DM1 (old - new) and DM2 (new - old)
433                    HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape mIt(aDM4);
434            for(;mIt.More();mIt.Next()) {
435                          const TopoDS_Shape& aKey = mIt.Key();//old
436                          TopTools_ListOfShape aList;
437                          aList.Append(aKey);
438                          const TopTools_ListOfShape& aListOfNew = mIt.Value();
439                          aDM1.Bind(aKey, aListOfNew);
440                          TopTools_ListIteratorOfListOfShape it(aListOfNew);
441                          for(;it.More();it.Next()) {
442                            if(!aDM2.IsBound(it.Value()))
443                                  aDM2.Bind(it.Value(), aList);
444                            else {
445                                  TopTools_ListOfShape& aList = aDM2.ChangeFind(it.Value());
446                                  aList.Prepend(aKey);
447                            }
448                          }
449                    }
450                  } else {
451            HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape aMIt(aDM1); 
452                    //DM4 contains Old - New after common op. DM1: old - new after Split op.
453                    for(;aMIt.More();aMIt.Next()) {
454              const TopoDS_Shape& aKey = aMIt.Key();
455              TopTools_ListOfShape aList;
456              aList.Append(aKey);
457              const TopTools_ListOfShape& aListOfNew = aMIt.Value();
458              TopTools_ListIteratorOfListOfShape it(aListOfNew);
459              for(;it.More();it.Next()) {// iterate new: b1, b2, b3...
460                            if(!aDM4.IsBound(it.Value())) // bi - is deleted
461                  continue; // go to the next bi
462                            else {
463                                   const TopTools_ListOfShape& aListOfNew4 = aDM4.Find(it.Value());
464                               TopTools_ListIteratorOfListOfShape it4(aListOfNew4); // {c1, c2, c3,...}
465                   for(;it4.More();it4.Next()) {
466                                     if(!aDM2.IsBound(it4.Value()))
467                       aDM2.Bind(it4.Value(), aList);
468                                 else {
469                       TopTools_ListOfShape& aList = aDM2.ChangeFind(it4.Value());
470                       aList.Prepend(aKey);
471                                         }
472                                   }
473                            }
474                          }
475                    }
476                  }
477                  } else {
478                          hasLimits = Standard_False;
479 #ifdef DEB_SPLIT_TO_ZONES
480                      BRepTools::Write(aComResult,"CommonNV.brep");
481 #endif  
482                   }
483            }
484           }
485         }
486   }// end limits processing
487   if(!hasLimits) {
488     HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape aMIt(aDM1);
489     for(;aMIt.More();aMIt.Next()) {
490       const TopoDS_Shape& aKey = aMIt.Key();
491       TopTools_ListOfShape aList;
492       aList.Append(aKey);
493       const TopTools_ListOfShape& aListOfNew = aMIt.Value();
494       TopTools_ListIteratorOfListOfShape it(aListOfNew);
495       for(;it.More();it.Next()) {
496         if(!aDM2.IsBound(it.Value()))
497           aDM2.Bind(it.Value(), aList);
498         else {
499           TopTools_ListOfShape& aList = aDM2.ChangeFind(it.Value());
500           aList.Prepend(aKey);
501                 }
502           }
503         }
504   }
505   // Step 4. Fill output structure.
506 #ifdef DEB_SPLIT_TO_ZONES
507   TCollection_AsciiString aNam4 ("SC_");
508 #endif  
509   HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape aMIt(aDM2);
510   for(int i =1;aMIt.More();aMIt.Next(),i++) {
511     SplitData aDestSplitData;
512     const TopoDS_Shape& aKey = aMIt.Key(); //new
513     aDestSplitData.Shape = aKey;
514     if(aKey.ShapeType() == TopAbs_FACE)
515       aDestSplitData.Type = SplitData::Data_Zone;
516         else {
517       aDestSplitData.Type = SplitData::Data_Edge;
518 #ifdef DEB_SPLIT_TO_ZONES
519           TCollection_AsciiString aName = aNam4 + i + ".brep";
520           BRepTools::Write(aKey,aName.ToCString());
521 #endif  
522         }
523
524     QStringList aListOfNames; // names processing
525     const TopTools_ListOfShape& aListOfOld = aMIt.Value();
526     TopTools_ListIteratorOfListOfShape it(aListOfOld);
527         for(int j =1;it.More();it.Next(),j++) { 
528           const TopoDS_Shape& aSh = it.Value(); //old
529           if(aDM3.IsBound(aSh)) {
530             const QStringList& ObjectNames = aDM3.Find(aSh);    
531                 aListOfNames.append(ObjectNames);
532 #ifdef DEB_SPLIT_TO_ZONES
533                 TCollection_AsciiString aName = aNam4 + i +"_" + j + ".brep";
534             BRepTools::Write(aSh ,aName.ToCString());           
535 #endif  
536           }     else {
537 #ifdef DEB_SPLIT_TO_ZONES
538                 TCollection_AsciiString aName = aNam4 +"__" + i +"_" + j + ".brep";
539             BRepTools::Write(aSh ,aName.ToCString());           
540                 cout <<aName.ToCString()<<" = "<< aSh.TShape() <<endl;
541 #endif
542                   if(aBndView.Contains(aSh) && hasLimits) {                     
543                         aListOfNames.append(aBndName);
544 #ifdef DEB_SPLIT_TO_ZONES
545                         cout << " BndName = "<<aBndName.toStdString() <<endl; 
546 #endif
547                   }
548           }
549         }
550
551         aDestSplitData.ObjectNames = aListOfNames;  
552         anOutputSplitDataList.append(aDestSplitData);   
553 #ifdef DEB_SPLIT_TO_ZONES
554         QString aStr = aDestSplitData.ObjectNames.join(" "); 
555         cout << "New# = " << i<< " Names = "<<aStr.toStdString() << " size = " <<aDestSplitData.ObjectNames.size() <<endl; 
556 #endif
557   }
558
559   return anOutputSplitDataList;
560 }
561
562 bool HYDROData_SplitToZonesTool::SplitTwoData( const SplitData& theData1,
563                                                const SplitData& theData2,
564                                                SplitData& theData1Subtracted,
565                                                SplitData& theData2Subtracted,
566                                                SplitData& theDataIntersected )
567 {
568   const TopoDS_Shape& aShape1 = theData1.Shape;
569   const TopoDS_Shape& aShape2 = theData2.Shape;
570
571   const QStringList& anObjectNames1 = theData1.ObjectNames;
572   const QStringList& anObjectNames2 = theData2.ObjectNames;
573
574   BRepAlgoAPI_Common aCommon( aShape1, aShape2 );
575   TopoDS_Shape aCommonShape = aCommon.Shape();
576   if( aCommonShape.IsNull() )
577   {
578     theData1Subtracted = theData1;
579     theData2Subtracted = theData2;
580     return false;
581   }
582
583   BRepAlgoAPI_Cut aCut1( aShape1, aShape2 );
584   TopoDS_Shape aCut1Shape = aCut1.Shape();
585
586   BRepAlgoAPI_Cut aCut2( aShape2, aShape1 );
587   TopoDS_Shape aCut2Shape = aCut2.Shape();
588
589   theData1Subtracted = SplitData( SplitData::Data_Zone, aCut1Shape, anObjectNames1 );
590   theData2Subtracted = SplitData( SplitData::Data_Zone, aCut2Shape, anObjectNames2 );
591   theDataIntersected = SplitData( SplitData::Data_Zone, aCommonShape, anObjectNames1 + anObjectNames2 );
592
593   return true;
594 }
595
596 HYDROData_SplitToZonesTool::SplitDataList
597 HYDROData_SplitToZonesTool::ExtractSeparateData( const SplitData& theData )
598 {
599   SplitDataList aSplitDataList;
600   TopExp_Explorer anExp( theData.Shape, TopAbs_FACE );
601   for( ; anExp.More(); anExp.Next() )
602   {
603     TopoDS_Shape aShape = anExp.Current();
604     if( aShape.ShapeType() == TopAbs_FACE )
605     {
606       TopoDS_Face aFace = TopoDS::Face( aShape );
607       if( !aFace.IsNull() )
608       {
609         SplitData aSplitData( SplitData::Data_Zone, aFace, theData.ObjectNames );
610         aSplitDataList.append( aSplitData );
611       }
612     }
613   }
614   return aSplitDataList;
615 }