Salome HOME
Fix for the bug #255: VTK viewer is not updated after modification of objects.
[modules/hydro.git] / src / HYDROData / HYDROData_Stream.cxx
1
2 #include "HYDROData_Stream.h"
3
4 #include "HYDROData_Document.h"
5 #include "HYDROData_PolylineXY.h"
6 #include "HYDROData_Profile.h"
7 #include "HYDROData_ShapesGroup.h"
8 #include "HYDROData_ShapesTool.h"
9
10 #include <TDataStd_RealArray.hxx>
11
12 #include <Precision.hxx>
13
14 #include <NCollection_DataMap.hxx>
15
16 #include <TColStd_Array1OfReal.hxx>
17 #include <TColStd_ListOfReal.hxx>
18 #include <TColStd_ListIteratorOfListOfReal.hxx>
19 #include <TCollection_CompareOfReal.hxx>
20 #include <TColgp_Array1OfPnt.hxx>
21 #include <TColgp_HArray1OfPnt.hxx>
22
23 #include <TopoDS.hxx>
24 #include <TopoDS_Wire.hxx>
25 #include <TopoDS_Shell.hxx>
26 #include <TopoDS_Face.hxx>
27 #include <TopoDS_Edge.hxx>
28 #include <TopoDS_Vertex.hxx>
29 #include <TopExp.hxx>
30 #include <TopExp_Explorer.hxx>
31
32 #include <Bnd_Box.hxx>
33
34 #include <BRep_Builder.hxx>
35 #include <BRepBuilderAPI_MakeEdge.hxx>
36 #include <BRepBuilderAPI_MakeWire.hxx>
37 #include <BRepBuilderAPI_MakeFace.hxx>
38
39 #include <BRepBndLib.hxx>
40 #include <BRepProj_Projection.hxx>
41 #include <BRepExtrema_ExtCC.hxx>
42 #include <BRepCheck_Analyzer.hxx>
43
44 #include <gp.hxx>
45 #include <gp_Ax1.hxx>
46 #include <gp_Ax2.hxx>
47 #include <gp_Ax3.hxx>
48 #include <gp_Vec.hxx>
49 #include <gp_Pnt.hxx>
50 #include <gp_Pln.hxx>
51
52 #include <GeomAPI_Interpolate.hxx>
53 #include <Geom_BSplineCurve.hxx>
54
55 #include <TopTools_Array1OfShape.hxx>
56
57 #include <SortTools_QuickSortOfReal.hxx>
58
59 #include <QColor>
60 #include <QStringList>
61
62 //#define DEB_STREAM 1
63 #ifdef DEB_STREAM
64 //#define DEB_HASINT 1
65 //#define DEB_UPDATE 1
66 #include <BRepTools.hxx>
67 #include <TCollection_AsciiString.hxx>
68 #endif
69
70 #define PYTHON_STREAM_ID "KIND_STREAM"
71
72 typedef NCollection_DataMap<Standard_Real, Handle(HYDROData_Profile)> HYDROData_DataMapOfRealOfHDProfile;
73
74 IMPLEMENT_STANDARD_HANDLE(HYDROData_Stream,HYDROData_NaturalObject)
75 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Stream,HYDROData_NaturalObject)
76
77
78 HYDROData_Stream::HYDROData_Stream()
79 : HYDROData_NaturalObject()
80 {
81 }
82
83 HYDROData_Stream::~HYDROData_Stream()
84 {
85 }
86
87 QStringList HYDROData_Stream::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
88 {
89   QStringList aResList;
90
91   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
92   if ( aDocument.IsNull() )
93     return aResList;
94
95   QString aDocName = aDocument->GetDocPyName();
96   QString aStreamName = GetName();
97
98   aResList << QString( "%1 = %2.CreateObject( %3 );" )
99               .arg( aStreamName ).arg( aDocName ).arg( PYTHON_STREAM_ID );
100   aResList << QString( "%1.SetName( \"%2\" );" )
101               .arg( aStreamName ).arg( aStreamName );
102   aResList << QString( "" );
103
104   // TODO
105
106   return aResList;
107 }
108
109 HYDROData_SequenceOfObjects HYDROData_Stream::GetAllReferenceObjects() const
110 {
111   HYDROData_SequenceOfObjects aResSeq = HYDROData_Object::GetAllReferenceObjects();
112
113   Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
114   if ( !aHydAxis.IsNull() )
115     aResSeq.Append( aHydAxis );
116
117   HYDROData_SequenceOfObjects aSeqOfProfiles = GetProfiles();
118   aResSeq.Append( aSeqOfProfiles );
119
120   return aResSeq;
121 }
122
123 TopoDS_Shape HYDROData_Stream::GetTopShape() const
124 {
125   return getTopShape();
126 }
127
128 TopoDS_Shape HYDROData_Stream::GetShape3D() const
129 {
130   return getShape3D();
131 }
132
133 Handle(Geom_BSplineCurve) HYDROData_Stream::buildInterpolationCurve( 
134   const Handle(TColgp_HArray1OfPnt)& theArrayOfPnt )
135 {
136   Handle(Geom_BSplineCurve) aBSpline;
137   GeomAPI_Interpolate anInterpolator (theArrayOfPnt, Standard_False, 1.0e-5); 
138   anInterpolator.Perform() ;
139   if (anInterpolator.IsDone()) 
140     aBSpline = anInterpolator.Curve();
141   return aBSpline; 
142 }
143
144 void HYDROData_Stream::Update()
145 {
146   HYDROData_NaturalObject::Update();
147
148   Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
149   HYDROData_SequenceOfObjects aRefProfiles = GetProfiles();
150   if ( aHydAxis.IsNull() || aRefProfiles.Length() < 2 )
151     return; 
152  
153   bool anIsFirst = true;
154   gp_Pnt aPrevFirstPoint, aPrevLastPoint;
155   Handle(TColgp_HArray1OfPnt) anArrayOfFPnt    = new TColgp_HArray1OfPnt(1, aRefProfiles.Length());
156   Handle(TColgp_HArray1OfPnt) anArrayOfLPnt    = new TColgp_HArray1OfPnt(1, aRefProfiles.Length());  
157   TopTools_Array1OfShape anArrOfProfiles(1, aRefProfiles.Length());
158   TopTools_Array1OfShape anArrOf2DProfiles(1, aRefProfiles.Length());
159
160   // Pre-processing
161   HYDROData_SequenceOfObjects::Iterator anIter( aRefProfiles );
162   for (int i=1 ; anIter.More(); anIter.Next(),i++ )
163   {
164     Handle(HYDROData_Profile) aProfile =
165       Handle(HYDROData_Profile)::DownCast( anIter.Value() );
166     if ( aProfile.IsNull() )
167       continue;    
168     const TopoDS_Shape& aProf3d = aProfile->GetShape3D();
169     gp_XY aPnt1, aPnt2;
170     if ( !aProfile->GetLeftPoint( aPnt1 ) || !aProfile->GetRightPoint( aPnt2 ) )
171       continue;
172     anArrOfProfiles.SetValue(i,aProfile->GetShape3D());//aProfile->GetTopShape();
173     anArrOf2DProfiles.SetValue(i,aProfile->GetTopShape());
174
175     gp_Pnt aCurFirstPoint( aPnt1.X(), aPnt1.Y(), 0 ), aCurFP;
176     gp_Pnt aCurLastPoint(  aPnt2.X(), aPnt2.Y(), 0 ), aCurLP;
177     TopoDS_Vertex aV1, aV2;
178     TopExp::Vertices(TopoDS::Wire(aProf3d), aV1, aV2);
179        gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
180     if(aP1.X() == aPnt1.X() && aP1.Y() == aPnt1.Y())
181       aCurFP = aP1;
182     else
183       aCurLP = aP1;
184     aP1 = BRep_Tool::Pnt(aV2);
185     if(aP1.X() == aPnt2.X() && aP1.Y() == aPnt2.Y())
186       aCurLP = aP1;
187     else
188       aCurFP = aP1;
189     anArrayOfFPnt->SetValue(i,aCurFP);
190     anArrayOfLPnt->SetValue(i,aCurLP);
191   }
192
193   // Construct of the 3D presentation
194   Handle(Geom_BSplineCurve) aBSpline = buildInterpolationCurve (anArrayOfFPnt);
195   if(aBSpline.IsNull())
196     return;
197   TopoDS_Edge anEdgLeft, anEdgRight;
198   BRepBuilderAPI_MakeEdge aMakeEdge(aBSpline);
199   if(aMakeEdge.IsDone()) 
200     anEdgLeft = aMakeEdge.Edge();
201   if(anEdgLeft.IsNull())
202     return;
203   aBSpline.Nullify();
204   aBSpline = buildInterpolationCurve (anArrayOfLPnt);  
205   if(aBSpline.IsNull())
206     return; 
207   aMakeEdge.Init(aBSpline);
208   if(aMakeEdge.IsDone()) 
209     anEdgRight = aMakeEdge.Edge();
210   if(anEdgRight.IsNull())
211     return;
212   BRep_Builder aBB;
213   TopoDS_Compound aCmp;
214   aBB.MakeCompound(aCmp);
215   anIter.Init( aRefProfiles );
216   for (int i=1 ; i < anArrOfProfiles.Length() +1; i++ )  
217     aBB.Add(aCmp, anArrOfProfiles.Value(i));
218   aBB.Add(aCmp,anEdgLeft);
219   aBB.Add(aCmp,anEdgRight);
220   BRepCheck_Analyzer aCh(aCmp);
221   if(aCh.IsValid())
222     SetShape3D(aCmp);
223 #ifdef DEB_UPDATE
224   else {
225     BRepTools::Write(aCmp, "str3d.brep");
226     SetShape3D(aCmp);
227   }
228 #endif
229
230   // Construct the top presentation
231   for(int i=1;i<= anArrayOfLPnt->Length();i++) {
232       gp_Pnt aPnt = anArrayOfFPnt->Value(i);
233       aPnt.SetZ(.0); // make 2d
234       anArrayOfFPnt->SetValue(i, aPnt);
235       aPnt = anArrayOfLPnt->Value(i);
236       aPnt.SetZ(.0);
237       anArrayOfLPnt->SetValue(i, aPnt);
238   }
239   aBSpline.Nullify();
240   aBSpline = buildInterpolationCurve (anArrayOfFPnt);  
241   if(aBSpline.IsNull())
242     return; 
243   aMakeEdge.Init(aBSpline);
244   if(aMakeEdge.IsDone()) 
245       anEdgLeft = aMakeEdge.Edge();
246   aBSpline.Nullify();
247   aBSpline = buildInterpolationCurve (anArrayOfLPnt);  
248   if(aBSpline.IsNull())
249     return; 
250   aMakeEdge.Init(aBSpline);
251   if(aMakeEdge.IsDone()) 
252     anEdgRight = aMakeEdge.Edge();
253   if(anEdgRight.IsNull())
254     return;
255   BRepBuilderAPI_MakeEdge aMakeEdge2(anArrayOfFPnt->Value(1),anArrayOfLPnt->Value(1));
256   TopoDS_Edge aBotEdge, aTopEdge;
257   if(aMakeEdge2.IsDone()) 
258     aBotEdge = aMakeEdge2.Edge();
259   BRepBuilderAPI_MakeEdge aMakeEdge3(anArrayOfFPnt->Value(anArrayOfFPnt->Length()),anArrayOfLPnt->Value(anArrayOfLPnt->Length()));
260   if(aMakeEdge3.IsDone()) 
261     aTopEdge = aMakeEdge3.Edge();
262
263   BRepBuilderAPI_MakeWire aMakeWire( aBotEdge, anEdgLeft, aTopEdge,anEdgRight);
264   TopoDS_Wire aSectProfileWire;
265   if(aMakeWire.IsDone())
266     aSectProfileWire = aMakeWire.Wire();
267   BRepBuilderAPI_MakeFace aMakeFace( aSectProfileWire, Standard_True );
268   TopoDS_Face aFace;
269   aMakeFace.Build();
270   if( aMakeFace.IsDone() )
271     aFace = aMakeFace.Face();
272   aCmp.Nullify();
273   aBB.MakeCompound(aCmp);
274   aBB.Add(aCmp,aFace);
275   for(int i=1;i <= anArrOf2DProfiles.Length(); i++)
276     aBB.Add(aCmp,anArrOf2DProfiles.Value(i));
277   aCh.Init(aCmp);
278   if(aCh.IsValid())
279     SetTopShape(aCmp);
280 #ifdef DEB_UPDATE
281   else {
282     BRepTools::Write(aCmp, "str2d.brep");
283     SetTopShape(aCmp);
284   }
285 #endif 
286
287   // Create the stream groups
288   createGroupObjects();
289 }
290
291 QColor HYDROData_Stream::DefaultFillingColor()
292 {
293   return QColor( Qt::green );
294 }
295
296 QColor HYDROData_Stream::DefaultBorderColor()
297 {
298   return QColor( Qt::transparent );
299 }
300
301 bool HYDROData_Stream::IsValidAsAxis( const Handle(HYDROData_PolylineXY)& theHydAxis )
302 {
303   if ( theHydAxis.IsNull() )
304     return false;
305
306   TopoDS_Shape aHydraulicShape = theHydAxis->GetShape();
307   if ( aHydraulicShape.IsNull() || 
308        aHydraulicShape.ShapeType() != TopAbs_WIRE ||
309        BRep_Tool::IsClosed( aHydraulicShape ) )
310     return false; // The polyline must be a single not closed wire
311
312   return true;
313 }
314
315 QColor HYDROData_Stream::getDefaultFillingColor() const
316 {
317   return DefaultFillingColor();
318 }
319
320 QColor HYDROData_Stream::getDefaultBorderColor() const
321 {
322   return DefaultBorderColor();
323 }
324
325 bool HYDROData_Stream::SetHydraulicAxis( const Handle(HYDROData_PolylineXY)& theAxis )
326 {
327   if ( !IsValidAsAxis( theAxis ) )
328     return false;
329
330   Handle(HYDROData_PolylineXY) aPrevAxis = GetHydraulicAxis();
331   if ( IsEqual( aPrevAxis, theAxis ) )
332     return true;
333
334   SetReferenceObject( theAxis, DataTag_HydraulicAxis );
335
336   // Update the order of profiles
337   updateProfilesOrder();
338
339   // Indicate model of the need to update the stream presentation
340   SetToUpdate( true );
341
342   return true;
343 }
344
345 Handle(HYDROData_PolylineXY) HYDROData_Stream::GetHydraulicAxis() const
346 {
347   return Handle(HYDROData_PolylineXY)::DownCast( 
348            GetReferenceObject( DataTag_HydraulicAxis ) );
349 }
350
351 void HYDROData_Stream::RemoveHydraulicAxis()
352 {
353   Handle(HYDROData_PolylineXY) aPrevAxis = GetHydraulicAxis();
354   if ( aPrevAxis.IsNull() )
355     return;
356
357   ClearReferenceObjects( DataTag_HydraulicAxis );
358
359   // We remove the reference profiles
360   RemoveProfiles();
361
362   // Indicate model of the need to update the stream presentation
363   SetToUpdate( true );
364 }
365
366 bool HYDROData_Stream::HasIntersection( const Handle(HYDROData_PolylineXY)& theHydAxis, 
367                                         const Handle(HYDROData_Profile)& theProfile, 
368                                         const TopoDS_Face& thePlane,
369                                                               Standard_Real& outPar)
370 {
371   if ( theProfile.IsNull() || !IsValidAsAxis( theHydAxis ) )
372     return false; 
373
374   TopoDS_Wire aHydraulicWire = TopoDS::Wire( theHydAxis->GetShape() ); //guide line
375   TopoDS_Wire aProfileWire = TopoDS::Wire( theProfile->GetTopShape() );
376   if ( aHydraulicWire.IsNull() || aProfileWire.IsNull() )
377     return false;
378
379   BRepProj_Projection aProjector (aProfileWire, thePlane, gp::OZ().Direction());
380   if(!aProjector.IsDone())
381     return false;
382   TopoDS_Shape aPrjProfile = aProjector.Shape();
383   if(aPrjProfile.IsNull())
384     return false;
385   TopoDS_Vertex aV1, aV2;
386   if(aPrjProfile.ShapeType() == TopAbs_EDGE)
387     TopExp::Vertices(TopoDS::Edge(aPrjProfile), aV1, aV2);
388   else if(aPrjProfile.ShapeType() == TopAbs_WIRE)  
389     TopExp::Vertices(TopoDS::Wire(aPrjProfile), aV1, aV2);
390   else if(aPrjProfile.ShapeType() == TopAbs_COMPOUND){
391     TopExp_Explorer anExp(aPrjProfile, TopAbs_WIRE);
392     if(anExp.More()) {
393       TopExp::Vertices(TopoDS::Wire(anExp.Current()), aV1, aV2);
394     } else {
395       anExp.Init(aPrjProfile, TopAbs_EDGE);
396       if(anExp.More()) {
397         TopExp::Vertices(TopoDS::Edge(anExp.Current()), aV1, aV2);
398       }
399     }
400   }
401   if(aV1.IsNull() || aV2.IsNull())
402     return false;
403   gp_Pnt aPnt1 = BRep_Tool::Pnt(aV1);
404   gp_Pnt aPnt2 = BRep_Tool::Pnt(aV2);
405   aPnt1.SetZ(0.0);
406   aPnt2.SetZ(0.0);
407   BRepBuilderAPI_MakeEdge aMk(aPnt1, aPnt2); 
408   if(!aMk.IsDone())
409     return false;
410   const TopoDS_Edge& anEdg2 = aMk.Edge();//Section edge
411   Standard_Integer aNum(0);
412   
413   TopExp_Explorer anExplo(aHydraulicWire, TopAbs_EDGE);
414   for(;anExplo.More();anExplo.Next()) aNum++;
415   // check for self-intersection
416   const Standard_Real SquareTolerance = Precision::Confusion()*Precision::Confusion();
417   Standard_Boolean hasInt(false);
418   Standard_Real aSqDist(DBL_MAX);
419   Standard_Integer anIndx(0);
420   BRepExtrema_ExtCC aCC;
421   aCC.Initialize(anEdg2);
422   outPar = 0.0;
423   anExplo.Init(aHydraulicWire, TopAbs_EDGE);
424   for(Standard_Integer j=1;anExplo.More();anExplo.Next(),j++) {
425     const TopoDS_Edge& anEdg1 = TopoDS::Edge(anExplo.Current());
426     if(anEdg1.IsNull())
427       continue;
428     Standard_Boolean hasSol(false);
429     aCC.Perform(anEdg1);
430     if(aCC.IsDone()) {
431     // find minimal dist
432     for(Standard_Integer i=1; i<= aCC.NbExt();i++)
433       if(aCC.SquareDistance(i) < aSqDist) {
434         aSqDist = aCC.SquareDistance(i);
435         anIndx = i;
436         hasSol = true;
437       }  
438     }
439     if(hasSol) {
440       if(aSqDist <= SquareTolerance) { // hasInt
441         const gp_Pnt& aPnt = aCC.PointOnE1(anIndx);
442         if(aNum > 1) {
443           TopExp::Vertices(anEdg1, aV1, aV2, Standard_True);
444           outPar += BRep_Tool::Pnt(aV1).Distance(aPnt);
445         } else {
446           Standard_Real aPar = aCC.ParameterOnE1(anIndx);
447           outPar = aPar;
448         }
449         hasInt = true;
450         break;
451       } else {
452           // no ints-n
453         if(aNum > 1) {
454           TopExp::Vertices(anEdg1, aV1, aV2);
455           outPar += BRep_Tool::Pnt(aV1).Distance(BRep_Tool::Pnt(aV2));
456         }
457       }
458     } else if(aNum > 1) {
459       TopExp::Vertices(anEdg1, aV1, aV2);
460       outPar += BRep_Tool::Pnt(aV1).Distance(BRep_Tool::Pnt(aV2));
461     }
462   }
463   if(hasInt)
464     return true;
465   return false;
466 }
467
468 bool HYDROData_Stream::HasIntersection( const Handle(HYDROData_Profile)& theProfile, const TopoDS_Face& thePlane,
469                                        Standard_Real& outPar ) const
470 {
471   Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
472
473   return HasIntersection( aHydAxis, theProfile, thePlane, outPar );
474 }
475
476
477 bool HYDROData_Stream::AddProfile( const Handle(HYDROData_Profile)& theProfile )
478 {
479   if ( theProfile.IsNull() )
480     return false;
481
482   Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
483   if ( aHydAxis.IsNull() )
484     return false;
485
486   TopoDS_Face aPlane;
487   if(!BuildFace(aHydAxis, aPlane))
488     return false;
489
490   Standard_Real aPar(.0);
491   if ( HasReference( theProfile, DataTag_Profile ) || !HasIntersection( theProfile, aPlane, aPar ) )
492     return false; // Object is already in reference list or it has no intersection
493   
494   int aProfileIndex = insertParameter( aPar );
495   insertProfileInToOrder( theProfile, aProfileIndex );
496   
497   // Indicate model of the need to update the stream presentation
498   SetToUpdate( true );
499
500   return true;
501 }
502
503 HYDROData_SequenceOfObjects HYDROData_Stream::GetProfiles() const
504 {
505   return GetReferenceObjects( DataTag_Profile );
506 }
507
508 bool HYDROData_Stream::RemoveProfile( const Handle(HYDROData_Profile)& theProfile )
509 {
510   if ( theProfile.IsNull() )
511     return false;
512
513   int aProfileIndex = -1;
514
515   HYDROData_SequenceOfObjects aRefProfiles = GetProfiles();
516   HYDROData_SequenceOfObjects::Iterator anIter( aRefProfiles );
517   for ( int i = 0 ; anIter.More(); anIter.Next(), ++i )
518   {
519     Handle(HYDROData_Profile) aProfile =
520       Handle(HYDROData_Profile)::DownCast( anIter.Value() );
521     if ( aProfile.IsNull() )
522       continue;
523
524     if ( IsEqual( theProfile, aProfile ) )
525     {
526       aProfileIndex = i;
527       break;
528     }
529   }
530
531   if ( aProfileIndex == -1 )
532     return false;
533
534   RemoveReferenceObject( theProfile->Label(), DataTag_Profile );
535
536   // Remove parameter for removed profile
537   removeParameter( aProfileIndex );
538
539   // Indicate model of the need to update the stream presentation
540   SetToUpdate( true );
541
542   return true;
543 }
544
545 void HYDROData_Stream::RemoveProfiles()
546 {
547   bool anIsToUpdate = IsMustBeUpdated() || NbReferenceObjects( DataTag_Profile ) > 0;
548
549   ClearReferenceObjects( DataTag_Profile );
550
551   // Remove the parameters array
552   removeParametersArray();
553
554   // Indicate model of the need to update the stream presentation
555   SetToUpdate( anIsToUpdate );
556 }
557
558 void HYDROData_Stream::insertProfileInToOrder( const Handle(HYDROData_Profile)& theProfile,
559                                                const int                        theBeforeIndex )
560 {
561   Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
562   if ( theProfile.IsNull() || aHydAxis.IsNull() )
563     return; 
564
565   TopoDS_Wire aHydraulicWire = TopoDS::Wire( aHydAxis->GetShape() );
566   TopoDS_Wire aProfileWire = TopoDS::Wire( theProfile->GetTopShape() );
567   if ( aHydraulicWire.IsNull() || aProfileWire.IsNull() )
568     return;
569
570   if ( theBeforeIndex == -1 )
571     AddReferenceObject( theProfile, DataTag_Profile );
572   else
573     InsertReferenceObject( theProfile, DataTag_Profile, theBeforeIndex );
574 }
575
576 bool HYDROData_Stream::BuildFace( const Handle(HYDROData_PolylineXY)& theHydAxis,
577                                   TopoDS_Face&                        thePlane ) const
578 {
579   if ( !IsValidAsAxis( theHydAxis ) )
580     return false;
581
582   TopoDS_Wire aHydraulicWire = TopoDS::Wire( theHydAxis->GetShape() );
583
584   gp_Ax2 aX2(gp::XOY());
585   gp_Ax3 aX3(aX2);
586   gp_Pln aPln(aX3);   
587   Bnd_Box B;
588   BRepBndLib::Add(aHydraulicWire,B);
589   Standard_Real axmin,aymin,azmin,axmax,aymax,azmax;
590   B.Get(axmin,aymin,azmin,axmax,aymax,azmax);
591   BRepBuilderAPI_MakeFace aMkr(aPln, axmin-500., axmax+500., aymin-500., aymax+500.); // to be tuned later according max/ Profile deviation
592   if(!aMkr.IsDone() || aMkr.Shape().IsNull()) return false;
593   thePlane = TopoDS::Face(aMkr.Shape());
594   return true;
595 }
596
597 void HYDROData_Stream::updateProfilesOrder()
598 {
599   HYDROData_SequenceOfObjects aRefProfiles = GetProfiles();
600   if ( aRefProfiles.IsEmpty() )
601     return;
602
603   // At first we remove all profiles from order
604   RemoveProfiles();
605
606   Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
607   if ( aHydAxis.IsNull() )
608     return; 
609
610   TopoDS_Face aPlane;
611   if ( !BuildFace( aHydAxis, aPlane ) )
612     return;
613
614   Standard_Real aPar( .0 );
615
616 #ifdef DEB_HASINT
617   BRep_Builder aBB;
618   TopoDS_Compound aCmp;
619   aBB.MakeCompound(aCmp);
620 #endif
621
622   HYDROData_DataMapOfRealOfHDProfile aDM;  
623   TColStd_ListOfReal aList;
624   HYDROData_SequenceOfObjects::Iterator anIter( aRefProfiles );
625   for (int i = 1 ; anIter.More(); anIter.Next(), i++ )
626   {
627     Handle(HYDROData_Profile) aProfile =
628       Handle(HYDROData_Profile)::DownCast( anIter.Value() );
629 #ifdef DEB_HASINT
630   TopoDS_Wire aProfileWire = TopoDS::Wire( aProfile->GetTopShape() );
631   aBB.Add( aCmp, aProfileWire ); 
632 #endif
633     if ( aProfile.IsNull() || !HasIntersection( aProfile, aPlane, aPar ) )
634       continue;
635     
636     aDM.Bind( aPar, aProfile );
637       aList.Append( aPar );
638   }
639   
640   if ( aList.IsEmpty() )
641     return;
642
643   TColStd_Array1OfReal anArr( 1, aList.Extent() );
644
645   TColStd_ListIteratorOfListOfReal it( aList );
646   for ( int j = 1; it.More(); it.Next(), j++ )
647     anArr( j ) = it.Value();
648
649   // sorting
650   if ( aList.Extent() > 1 )
651   {
652     TCollection_CompareOfReal Compar;
653     SortTools_QuickSortOfReal::Sort( anArr, Compar );
654
655     for (int j = 1; j <= anArr.Length(); j++) {
656       const Standard_Real aKey =  anArr(j);
657       const Handle(HYDROData_Profile)& aProfile = aDM.Find(aKey);
658       insertProfileInToOrder( aProfile );
659     }
660   } else if ( aList.Extent() == 1 ) {
661      const Standard_Real aKey = aList.Last();
662      const Handle(HYDROData_Profile)& aProfile = aDM.Find(aKey);
663      insertProfileInToOrder( aProfile );
664   } 
665
666   setParametersArray( anArr );
667
668 #ifdef DEB_HASINT
669   TopoDS_Wire aHydraulicWire = TopoDS::Wire( aHydAxis->GetShape() );
670   BRepTools::Write(aHydraulicWire, "Path.brep");
671   BRepTools::Write(aCmp, "Prof.brep");
672 #endif
673 }
674
675 void HYDROData_Stream::createGroupObjects()
676 {
677   TopoDS_Shape aStreamShape = GetTopShape();
678   if ( aStreamShape.IsNull() )
679     return;
680
681   HYDROData_SequenceOfObjects aRefProfiles = GetProfiles();
682   if ( aRefProfiles.Length() < 2 )
683     return;
684
685   Handle(HYDROData_Profile) aFirstProfile =
686     Handle(HYDROData_Profile)::DownCast( aRefProfiles.First() );
687   Handle(HYDROData_Profile) aLastProfile =
688     Handle(HYDROData_Profile)::DownCast( aRefProfiles.Last() );
689   if ( aFirstProfile.IsNull() || aLastProfile.IsNull() )
690     return;
691
692   TopoDS_Shape aBotEdgeShape = aFirstProfile->GetTopShape();
693   TopoDS_Shape aTopEdgeShape = aLastProfile->GetTopShape();
694   if ( aBotEdgeShape.IsNull() || aBotEdgeShape.ShapeType() != TopAbs_WIRE ||
695        aTopEdgeShape.IsNull() || aTopEdgeShape.ShapeType() != TopAbs_WIRE )
696     return;
697
698   TopExp_Explorer aBotEdgeShapeExp( aBotEdgeShape, TopAbs_EDGE );
699   TopExp_Explorer aTopEdgeShapeExp( aTopEdgeShape, TopAbs_EDGE );
700   if ( !aBotEdgeShapeExp.More() || !aTopEdgeShapeExp.More() )
701     return;
702
703   TopoDS_Edge aBotEdge = TopoDS::Edge( aBotEdgeShapeExp.Current() );
704   TopoDS_Edge aTopEdge = TopoDS::Edge( aTopEdgeShapeExp.Current() );
705
706   TopoDS_Vertex aBotFirstVert, aBotLastVert, aTopFirstVert, aTopLastVert;
707   TopExp::Vertices( aBotEdge, aBotFirstVert, aBotLastVert, true );
708   TopExp::Vertices( aTopEdge, aTopFirstVert, aTopLastVert, true );
709
710   TopExp_Explorer aStreamFaceExp( aStreamShape, TopAbs_FACE );
711   if ( !aStreamFaceExp.More() )
712     return;
713
714   // Get only face because of 2d profile wires is in compound
715   TopoDS_Face aStreamFace = TopoDS::Face( aStreamFaceExp.Current() );
716   
717   TopTools_SequenceOfShape aStreamWires;
718   HYDROData_ShapesTool::ExploreShapeToShapes( aStreamFace, TopAbs_EDGE, aStreamWires );
719
720   bool anIsLeft = false;
721   bool anIsRight = false;
722
723   TopTools_SequenceOfShape aLeftEdges, aRightEdges;
724   for ( int i = 1, n = aStreamWires.Length(); i <= n; ++i )
725   {
726     TopoDS_Edge anEdge = TopoDS::Edge( aStreamWires.Value( i ) );
727     if ( HYDROData_ShapesTool::IsEdgesEquals( anEdge, aBotEdge ) || 
728          HYDROData_ShapesTool::IsEdgesEquals( anEdge, aTopEdge ) )
729     {
730       anIsLeft = false;
731       anIsRight = false;
732       continue;
733     }
734
735     if ( !anIsLeft && !anIsRight )
736     {
737       TopoDS_Vertex anEdgeFirstVert, anEdgeLastVert;
738       TopExp::Vertices( anEdge, anEdgeFirstVert, anEdgeLastVert, true );
739
740       anIsLeft = HYDROData_ShapesTool::IsVerticesEquals( anEdgeFirstVert, aBotFirstVert ) ||
741                  HYDROData_ShapesTool::IsVerticesEquals( anEdgeLastVert, aTopFirstVert );
742       
743       anIsRight = HYDROData_ShapesTool::IsVerticesEquals( anEdgeFirstVert, aTopLastVert ) ||
744                   HYDROData_ShapesTool::IsVerticesEquals( anEdgeLastVert, aBotLastVert );
745     }
746
747     if ( anIsLeft )
748     {
749       aLeftEdges.Append( anEdge );
750     }
751     else if ( anIsRight )
752     {
753       aRightEdges.Append( anEdge );
754     }
755   }
756
757   // Create edges groups
758   QString aLeftGroupName = GetName() + "_Left_Bank";
759
760   Handle(HYDROData_ShapesGroup) aLeftGroup = createGroupObject();
761   aLeftGroup->SetName( aLeftGroupName );
762   aLeftGroup->SetShapes( aLeftEdges );
763
764   QString aRightGroupName = GetName() + "_Right_Bank";
765
766   Handle(HYDROData_ShapesGroup) aRightGroup = createGroupObject();
767   aRightGroup->SetName( aRightGroupName );
768   aRightGroup->SetShapes( aRightEdges );
769
770   QString anInGroupName = GetName() + "_Inlet";
771
772   Handle(HYDROData_ShapesGroup) anInGroup = createGroupObject();
773   anInGroup->SetName( anInGroupName );
774   anInGroup->AddShape( aBotEdge );
775
776   QString anOutGroupName = GetName() + "_Outlet";
777
778   Handle(HYDROData_ShapesGroup) anOutGroup = createGroupObject();
779   anOutGroup->SetName( anOutGroupName );
780   anOutGroup->AddShape( aTopEdge );
781 }
782
783 ObjectKind HYDROData_Stream::getAltitudeObjectType() const
784 {
785   return KIND_STREAM_ALTITUDE;
786 }
787
788 void HYDROData_Stream::setParametersArray( const TColStd_Array1OfReal& theArray )
789 {
790   if ( theArray.Length() == 0 )
791   {
792     removeParametersArray();
793     return;
794   }
795
796   TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray );
797   
798   Handle(TDataStd_RealArray) aParamsArray = 
799     TDataStd_RealArray::Set( aLabel, theArray.Lower(), theArray.Upper() );
800
801   for ( int i = theArray.Lower(), n = theArray.Upper(); i <= n; ++i )
802   {
803     const Standard_Real& aParam = theArray( i );
804     aParamsArray->SetValue( i, aParam );
805   }
806 }
807
808 TColStd_Array1OfReal* HYDROData_Stream::getParametersArray() const
809 {
810   TColStd_Array1OfReal* anArray = NULL;
811
812   TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray, false );
813   if ( !aLabel.IsNull() )
814   {
815     Handle(TDataStd_RealArray) aParamsArray;
816     if ( aLabel.FindAttribute( TDataStd_RealArray::GetID(), aParamsArray ) )
817     {
818       anArray = new TColStd_Array1OfReal( aParamsArray->Lower(), aParamsArray->Upper() );
819       for ( int i = aParamsArray->Lower(), n = aParamsArray->Upper(); i <= n; ++i )
820       {
821         const Standard_Real& aParam = aParamsArray->Value( i );
822         anArray->SetValue( i, aParam );
823       }
824     }
825   }
826
827   return anArray;
828 }
829
830 void HYDROData_Stream::removeParametersArray()
831 {
832   TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray, false );
833   if ( !aLabel.IsNull() )
834     aLabel.ForgetAllAttributes();
835 }
836
837 int HYDROData_Stream::insertParameter( const Standard_Real& theParam )
838 {
839   int aResIndex = -1;
840
841   TColStd_Array1OfReal* anArr = getParametersArray();
842   if ( anArr )
843   {
844     aResIndex = 0;
845
846     TColStd_Array1OfReal aNewArr( anArr->Lower(), anArr->Upper() + 1 );
847
848     bool isInserted = false;
849     for ( int i = anArr->Lower(), j = i, n = anArr->Upper(); i <= n; ++i, ++j )
850     {
851       const Standard_Real& aStoredParam = anArr->Value( i );
852       if ( !isInserted )
853       {
854         if ( theParam > aStoredParam )
855         {
856           aResIndex++;
857         }
858         else
859         {
860           aNewArr( j ) = theParam;
861           isInserted = true;
862           ++j;
863         }
864       }
865
866       aNewArr( j ) = aStoredParam;
867     }
868
869     if ( !isInserted )
870     {
871       aResIndex = -1;
872       aNewArr( aNewArr.Upper() ) = theParam;
873     }
874     
875     setParametersArray( aNewArr );
876     delete anArr;
877   }
878   else
879   {
880     TColStd_Array1OfReal aNewArr( 1, 1 );
881     aNewArr.SetValue( 1, theParam );
882     setParametersArray( aNewArr );
883   }
884
885   return aResIndex;
886 }
887
888 void HYDROData_Stream::removeParameter( const int& theIndex )
889 {
890   TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray, false );
891   if ( aLabel.IsNull() )
892     return;
893
894   Handle(TDataStd_RealArray) aParamsArray;
895   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), aParamsArray ) )
896     return;
897
898   if ( aParamsArray->Length() == 1 )
899   {
900     removeParametersArray();
901     return;
902   }
903
904   TColStd_Array1OfReal aNewArr( aParamsArray->Lower(), aParamsArray->Upper() - 1 );
905
906   for ( int i = aParamsArray->Lower(), j = i, k = 0, n = aParamsArray->Upper(); i <= n; ++i, ++k )
907   {
908     const Standard_Real& aStoredParam = aParamsArray->Value( i );
909     if ( k == theIndex )
910       continue;
911
912     aNewArr.SetValue( j, aStoredParam );
913     ++j;
914   }
915
916   setParametersArray( aNewArr );
917 }