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