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