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