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