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