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