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