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