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