Salome HOME
06a85fefdcd4533903a4631ca287adeeb4c6357b
[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 #include "HYDROData_DTM.h"
31 #include <HYDROData_Bathymetry.h>
32
33 #include <TDataStd_RealArray.hxx>
34
35 #include <Precision.hxx>
36
37 #include <NCollection_DataMap.hxx>
38
39 #include <TColStd_Array1OfReal.hxx>
40 #include <TColStd_ListOfReal.hxx>
41 #include <TColStd_ListIteratorOfListOfReal.hxx>
42 #include <TCollection_CompareOfReal.hxx>
43 #include <TColgp_Array1OfPnt.hxx>
44 #include <TColgp_HArray1OfPnt.hxx>
45
46 #include <TopoDS.hxx>
47 #include <TopoDS_Wire.hxx>
48 #include <TopoDS_Shell.hxx>
49 #include <TopoDS_Face.hxx>
50 #include <TopoDS_Edge.hxx>
51 #include <TopoDS_Vertex.hxx>
52 #include <TopExp.hxx>
53 #include <TopExp_Explorer.hxx>
54
55 #include <Bnd_Box.hxx>
56
57 #include <BRep_Builder.hxx>
58 #include <BRepBuilderAPI_MakeEdge.hxx>
59 #include <BRepBuilderAPI_MakeWire.hxx>
60 #include <BRepBuilderAPI_MakeFace.hxx>
61
62 #include <BRepBndLib.hxx>
63 #include <BRepProj_Projection.hxx>
64 #include <BRepExtrema_ExtCC.hxx>
65 #include <BRepCheck_Analyzer.hxx>
66
67 #include <BRepLib_MakeEdge.hxx>
68 #include <BRepLib_MakeWire.hxx>
69 #include <BRep_Builder.hxx>
70 #include <GeomProjLib.hxx>
71 #include <Geom_TrimmedCurve.hxx>
72 #include <Geom_Plane.hxx>
73 #include <BRepTools_WireExplorer.hxx>
74 #include <TopTools_IndexedMapOfShape.hxx>
75 #include <BRepBuilderAPI_MakeFace.hxx>
76
77 #include <gp.hxx>
78 #include <gp_Ax1.hxx>
79 #include <gp_Ax2.hxx>
80 #include <gp_Ax3.hxx>
81 #include <gp_Vec.hxx>
82 #include <gp_Pnt.hxx>
83 #include <gp_Pln.hxx>
84
85 #include <GeomAPI_Interpolate.hxx>
86 #include <Geom_BSplineCurve.hxx>
87
88 #include <TopTools_HArray1OfShape.hxx>
89
90 #include <SortTools_QuickSortOfReal.hxx>
91
92 #include <QColor>
93 #include <QStringList>
94
95 //#define DEB_STREAM 1
96 #ifdef DEB_STREAM
97 //#define DEB_HASINT 1
98 //#define DEB_UPDATE 1
99 #include <BRepTools.hxx>
100 #include <TCollection_AsciiString.hxx>
101 #endif
102
103 typedef NCollection_DataMap<Standard_Real, Handle(HYDROData_Profile)> HYDROData_DataMapOfRealOfHDProfile;
104
105 IMPLEMENT_STANDARD_HANDLE(HYDROData_Stream,HYDROData_NaturalObject)
106 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Stream,HYDROData_NaturalObject)
107
108
109 HYDROData_Stream::HYDROData_Stream()
110 : HYDROData_NaturalObject( Geom_3d )
111 {
112 }
113
114 HYDROData_Stream::~HYDROData_Stream()
115 {
116 }
117
118 QStringList HYDROData_Stream::DumpToPython( const QString&       thePyScriptPath,
119                                             MapOfTreatedObjects& theTreatedObjects ) const
120 {
121   QStringList aResList = dumpObjectCreation( theTreatedObjects );
122   QString aName = GetObjPyName();
123
124   Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
125   setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aHydAxis, "SetHydraulicAxis" );
126
127   HYDROData_SequenceOfObjects aSeqOfProfiles = GetProfiles();
128   for ( int i = 1, aNb = aSeqOfProfiles.Size(); i <= aNb; ++i )
129   {
130     const Handle(HYDROData_Entity) aProfile = aSeqOfProfiles.Value( i );
131     setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aProfile, "AddProfile" );
132   }
133
134   // Set bottom polyline if exists
135   const Handle(HYDROData_Polyline3D) aBottomPolyline = GetBottomPolyline();
136   if ( !aBottomPolyline.IsNull() ) {
137     setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aBottomPolyline, "SetBottomPolyline" );
138   }
139
140   QString aDDZs = QString::number( GetDDZ(), 'f', 3 );
141   QString aSSteps = QString::number( GetSpatialStep(), 'f', 3 );
142   aResList << QString( "%1.SetDDZ( %2 )" ).arg( aName ).arg( aDDZs );
143   aResList << QString( "%1.SetSpatialStep( %2 )" ).arg( aName ).arg( aSSteps );
144
145   aResList << QString( "" );
146   aResList << QString( "%1.Update()" ).arg( aName );
147   aResList << QString( "" );
148
149   return aResList;
150 }
151
152 HYDROData_SequenceOfObjects HYDROData_Stream::GetAllReferenceObjects() const
153 {
154   HYDROData_SequenceOfObjects aResSeq = HYDROData_Object::GetAllReferenceObjects();
155
156   Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
157   if ( !aHydAxis.IsNull() )
158     aResSeq.Append( aHydAxis );
159
160   HYDROData_SequenceOfObjects aSeqOfProfiles = GetProfiles();
161   aResSeq.Append( aSeqOfProfiles );
162
163   return aResSeq;
164 }
165
166 Handle(Geom_BSplineCurve) HYDROData_Stream::buildInterpolationCurve( 
167   const Handle(TColgp_HArray1OfPnt)& theArrayOfPnt )
168 {
169   Handle(Geom_BSplineCurve) aBSpline;
170   GeomAPI_Interpolate anInterpolator (theArrayOfPnt, Standard_False, 1.0e-5); 
171   anInterpolator.Perform() ;
172   if (anInterpolator.IsDone()) 
173     aBSpline = anInterpolator.Curve();
174   return aBSpline; 
175 }
176
177 void HYDROData_Stream::Update()
178 {
179   updateProfilesOrder();
180
181   // Update bottom polyline if exists
182   const Handle(HYDROData_Polyline3D) aBottomPolyline = GetBottomPolyline();
183   if ( !aBottomPolyline.IsNull() ) {
184     if ( GenerateBottomPolyline() ) {
185       Handle(HYDROData_PolylineXY) aPolylineXY = aBottomPolyline->GetPolylineXY();
186       if ( !aPolylineXY.IsNull() ) {
187         aPolylineXY->Update();
188       }
189       aBottomPolyline->Update();
190     }
191   }
192
193   Handle_HYDROData_DTM dtm = DTM();
194   dtm->Update();
195   UpdatePrs( dtm );
196 }
197
198 bool HYDROData_Stream::IsHas2dPrs() const
199 {
200   return true;
201 }
202
203 bool HYDROData_Stream::CreatePresentations( const Handle_HYDROData_DTM& theDTM,
204                                             PrsDefinition&              thePrs )
205 {
206   if ( theDTM.IsNull() )
207     return false;
208
209   HYDROData_SequenceOfObjects profiles = theDTM->GetProfiles();
210   if( profiles.Length() < 2 )
211     return false;
212
213   /*std::vector<TopoDS_Wire> profiles3d;
214   profiles3d.reserve(profiles.Length());
215
216   // Pre-processing
217   HYDROData_SequenceOfObjects::Iterator anIter( profiles );
218   for (int i=1 ; anIter.More(); anIter.Next(),i++ )
219   {
220     Handle(HYDROData_Profile) aProfile =
221       Handle(HYDROData_Profile)::DownCast( anIter.Value() );
222
223     if ( aProfile.IsNull() )
224       continue;
225
226     const TopoDS_Shape& aProfileShape = aProfile->GetShape3D();
227     //TopExp_Explorer exp(aProfileShape, TopAbs_EDGE);
228     profiles3d.push_back( TopoDS::Wire(aProfileShape) );
229   }*/
230
231   CreatePresentationsIntern( theDTM, thePrs );
232   return true;
233 }
234
235 void HYDROData_Stream::UpdatePrs( const Handle_HYDROData_DTM& theDTM )
236 {
237   HYDROData_NaturalObject::Update();
238   
239   PrsDefinition aResultPrs;
240   if ( !CreatePresentations( theDTM, 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 Handle_HYDROData_DTM HYDROData_Stream::DTM() const
321 {
322   const_cast<HYDROData_Stream*>( this )->checkAndSetAltitudeObject();
323   return Handle(HYDROData_DTM)::DownCast( GetAltitudeObject() );
324 }
325
326 double HYDROData_Stream::GetDDZ() const
327 {
328   return DTM()->GetDDZ();
329 }
330
331 void HYDROData_Stream::SetDDZ( double theDDZ )
332 {
333   DTM()->SetDDZ( theDDZ );
334 }
335   
336 double HYDROData_Stream::GetSpatialStep() const
337 {
338   return DTM()->GetSpatialStep();
339 }
340
341 void HYDROData_Stream::SetSpatialStep( double theSpatialStep )
342 {
343   DTM()->SetSpatialStep( theSpatialStep );
344 }
345
346 bool HYDROData_Stream::SetHydraulicAxis( const Handle(HYDROData_PolylineXY)& theAxis )
347 {
348   if ( !IsValidAsAxis( theAxis ) )
349     return false;
350
351   Handle(HYDROData_PolylineXY) aPrevAxis = GetHydraulicAxis();
352   if ( IsEqual( aPrevAxis, theAxis ) )
353     return true;
354
355   SetReferenceObject( theAxis, DataTag_HydraulicAxis );
356
357   // Update the order of profiles
358   updateProfilesOrder();
359
360   // Indicate model of the need to update the stream presentation
361   Changed( Geom_3d );
362
363   return true;
364 }
365
366 Handle(HYDROData_PolylineXY) HYDROData_Stream::GetHydraulicAxis() const
367 {
368   return Handle(HYDROData_PolylineXY)::DownCast( 
369            GetReferenceObject( DataTag_HydraulicAxis ) );
370 }
371
372 void HYDROData_Stream::RemoveHydraulicAxis()
373 {
374   Handle(HYDROData_PolylineXY) aPrevAxis = GetHydraulicAxis();
375   if ( aPrevAxis.IsNull() )
376     return;
377
378   ClearReferenceObjects( DataTag_HydraulicAxis );
379
380   // We remove the reference profiles
381   RemoveProfiles();
382
383   // Indicate model of the need to update the stream presentation
384   Changed( Geom_3d );
385 }
386
387 bool HYDROData_Stream::HasIntersection( const Handle(HYDROData_Profile)& theProfile,
388                                         const TopoDS_Face&               thePlane,
389                                         Standard_Real&                   theOutPar ) const
390 {
391   Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
392   return HasIntersection( aHydAxis, theProfile, thePlane, theOutPar );
393 }
394
395 bool HYDROData_Stream::HasIntersection( const Handle(HYDROData_PolylineXY)& theHydAxis, 
396                                         const Handle(HYDROData_Profile)&    theProfile, 
397                                         const TopoDS_Face&                  thePlane,
398                                         Standard_Real&                      theOutPar )
399 {
400   if ( theProfile.IsNull() || !IsValidAsAxis( theHydAxis ) )
401     return false; 
402
403   TopoDS_Wire aHydraulicWire = TopoDS::Wire( theHydAxis->GetShape() ); //guide line
404   TopoDS_Wire aProfileWire = TopoDS::Wire( theProfile->GetTopShape() );
405   if ( aHydraulicWire.IsNull() || aProfileWire.IsNull() )
406     return false;
407
408   BRepProj_Projection aProjector (aProfileWire, thePlane, gp::OZ().Direction());
409   if(!aProjector.IsDone())
410     return false;
411   TopoDS_Shape aPrjProfile = aProjector.Shape();
412   if(aPrjProfile.IsNull())
413     return false;
414   TopoDS_Vertex aV1, aV2;
415   if(aPrjProfile.ShapeType() == TopAbs_EDGE)
416     TopExp::Vertices(TopoDS::Edge(aPrjProfile), aV1, aV2);
417   else if(aPrjProfile.ShapeType() == TopAbs_WIRE)  
418     TopExp::Vertices(TopoDS::Wire(aPrjProfile), aV1, aV2);
419   else if(aPrjProfile.ShapeType() == TopAbs_COMPOUND){
420     TopExp_Explorer anExp(aPrjProfile, TopAbs_WIRE);
421     if(anExp.More()) {
422       TopExp::Vertices(TopoDS::Wire(anExp.Current()), aV1, aV2);
423     } else {
424       anExp.Init(aPrjProfile, TopAbs_EDGE);
425       if(anExp.More()) {
426         TopExp::Vertices(TopoDS::Edge(anExp.Current()), aV1, aV2);
427       }
428     }
429   }
430   if(aV1.IsNull() || aV2.IsNull())
431     return false;
432   gp_Pnt aPnt1 = BRep_Tool::Pnt(aV1);
433   gp_Pnt aPnt2 = BRep_Tool::Pnt(aV2);
434   aPnt1.SetZ(0.0);
435   aPnt2.SetZ(0.0);
436   BRepBuilderAPI_MakeEdge aMk(aPnt1, aPnt2); 
437   if(!aMk.IsDone())
438     return false;
439   const TopoDS_Edge& anEdg2 = aMk.Edge();//Section edge
440   Standard_Integer aNum(0);
441   
442   TopExp_Explorer anExplo(aHydraulicWire, TopAbs_EDGE);
443   for(;anExplo.More();anExplo.Next()) aNum++;
444   // check for self-intersection
445   const Standard_Real SquareTolerance = Precision::Confusion()*Precision::Confusion();
446   Standard_Boolean hasInt(false);
447   Standard_Real aSqDist(DBL_MAX);
448   Standard_Integer anIndx(0);
449   BRepExtrema_ExtCC aCC;
450   aCC.Initialize(anEdg2);
451   theOutPar = 0.0;
452   anExplo.Init(aHydraulicWire, TopAbs_EDGE);
453   for(Standard_Integer j=1;anExplo.More();anExplo.Next(),j++) {
454     const TopoDS_Edge& anEdg1 = TopoDS::Edge(anExplo.Current());
455     if(anEdg1.IsNull())
456       continue;
457     Standard_Boolean hasSol(false);
458     aCC.Perform(anEdg1);
459     if(aCC.IsDone()) {
460     // find minimal dist
461     for(Standard_Integer i=1; i<= aCC.NbExt();i++)
462       if(aCC.SquareDistance(i) < aSqDist) {
463         aSqDist = aCC.SquareDistance(i);
464         anIndx = i;
465         hasSol = true;
466       }  
467     }
468     if(hasSol) {
469       if(aSqDist <= SquareTolerance) { // hasInt
470         const gp_Pnt& aPnt = aCC.PointOnE1(anIndx);
471         if(aNum > 1) {
472           TopExp::Vertices(anEdg1, aV1, aV2, Standard_True);
473           theOutPar += BRep_Tool::Pnt(aV1).Distance(aPnt);
474         } else {
475           Standard_Real aPar = aCC.ParameterOnE1(anIndx);
476           theOutPar = aPar;
477         }
478         hasInt = true;
479         break;
480       } else {
481           // no ints-n
482         if(aNum > 1) {
483           TopExp::Vertices(anEdg1, aV1, aV2);
484           theOutPar += BRep_Tool::Pnt(aV1).Distance(BRep_Tool::Pnt(aV2));
485         }
486       }
487     } else if(aNum > 1) {
488       TopExp::Vertices(anEdg1, aV1, aV2);
489       theOutPar += BRep_Tool::Pnt(aV1).Distance(BRep_Tool::Pnt(aV2));
490     }
491   }
492   if(hasInt)
493     return true;
494   return false;
495 }
496
497 bool HYDROData_Stream::AddProfile( const Handle(HYDROData_Profile)& theProfile )
498 {
499   if ( theProfile.IsNull() )
500     return false;
501
502   Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
503   if ( aHydAxis.IsNull() )
504     return false;
505
506   TopoDS_Face aPlane;
507   if(!BuildFace(aHydAxis, aPlane))
508     return false;
509
510   Standard_Real aPar(.0);
511   if ( HasReference( theProfile, DataTag_Profile ) || !HasIntersection( theProfile, aPlane, aPar ) )
512     return false; // Object is already in reference list or it has no intersection
513   
514   int aProfileIndex = insertParameter( aPar );
515   insertProfileInToOrder( theProfile, aProfileIndex );
516   
517   // Indicate model of the need to update the stream presentation
518   Changed( Geom_3d );
519
520   return true;
521 }
522
523 bool HYDROData_Stream::SetProfiles( const HYDROData_SequenceOfObjects& theProfiles,
524                                     const bool&                        theIsToOrder )
525 {
526   if ( theIsToOrder )
527   {
528     for ( int i = 1; i <= theProfiles.Length(); ++i )
529     {
530       Handle(HYDROData_Profile) aProfile = 
531         Handle(HYDROData_Profile)::DownCast( theProfiles.Value( i ) );
532       if ( aProfile.IsNull() )
533         continue;
534
535       if ( !AddProfile( aProfile ) )
536       {
537         DTM()->SetProfiles( HYDROData_SequenceOfObjects() );
538         return false;
539       }
540     }
541   }
542   else // Just store the sequence of objects as is
543   {
544     bool anIsToUpdate = true;
545
546     HYDROData_SequenceOfObjects anOldProfiles = GetProfiles();
547     if ( anOldProfiles.Length() == theProfiles.Length() )
548     {
549       anIsToUpdate = false;
550
551       for ( int i = 1; i <= theProfiles.Length(); ++i )
552       {
553         Handle(HYDROData_Entity) anOldProfile = anOldProfiles.Value( i );
554         Handle(HYDROData_Entity) aNewProfile = theProfiles.Value( i );
555         if ( !IsEqual( anOldProfile, aNewProfile ) )
556         {
557           anIsToUpdate = true;
558           break;
559         }
560       }
561     }
562     
563     SetReferenceObjects( theProfiles, DataTag_Profile );
564
565     if ( anIsToUpdate )
566       Changed( Geom_3d );
567   }
568
569   DTM()->SetProfiles( GetProfiles() );
570   return true;
571 }
572
573 HYDROData_SequenceOfObjects HYDROData_Stream::GetProfiles() const
574 {
575   return GetReferenceObjects( DataTag_Profile );
576 }
577
578 bool HYDROData_Stream::RemoveProfile( const Handle(HYDROData_Profile)& theProfile )
579 {
580   if ( theProfile.IsNull() )
581     return false;
582
583   int aProfileIndex = -1;
584
585   HYDROData_SequenceOfObjects aRefProfiles = GetProfiles();
586   HYDROData_SequenceOfObjects::Iterator anIter( aRefProfiles );
587   for ( int i = 0 ; anIter.More(); anIter.Next(), ++i )
588   {
589     Handle(HYDROData_Profile) aProfile =
590       Handle(HYDROData_Profile)::DownCast( anIter.Value() );
591     if ( aProfile.IsNull() )
592       continue;
593
594     if ( IsEqual( theProfile, aProfile ) )
595     {
596       aProfileIndex = i;
597       break;
598     }
599   }
600
601   if ( aProfileIndex == -1 )
602     return false;
603
604   RemoveReferenceObject( theProfile->Label(), DataTag_Profile );
605
606   // Remove parameter for removed profile
607   removeParameter( aProfileIndex );
608
609   // Indicate model of the need to update the stream presentation
610   Changed( Geom_3d );
611
612   return true;
613 }
614
615 void HYDROData_Stream::RemoveProfiles()
616 {
617   ClearReferenceObjects( DataTag_Profile );
618
619   // Remove the parameters array
620   removeParametersArray();
621
622   // Indicate model of the need to update the stream presentation
623   Changed( Geom_3d );
624 }
625
626 void HYDROData_Stream::insertProfileInToOrder( const Handle(HYDROData_Profile)& theProfile,
627                                                const int                        theBeforeIndex )
628 {
629   Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
630   if ( theProfile.IsNull() || aHydAxis.IsNull() )
631     return; 
632
633   TopoDS_Wire aHydraulicWire = TopoDS::Wire( aHydAxis->GetShape() );
634   TopoDS_Wire aProfileWire = TopoDS::Wire( theProfile->GetTopShape() );
635   if ( aHydraulicWire.IsNull() || aProfileWire.IsNull() )
636     return;
637
638   if ( theBeforeIndex == -1 )
639     AddReferenceObject( theProfile, DataTag_Profile );
640   else
641     InsertReferenceObject( theProfile, DataTag_Profile, theBeforeIndex );
642 }
643
644 bool HYDROData_Stream::BuildFace( const Handle(HYDROData_PolylineXY)& theHydAxis,
645                                   TopoDS_Face&                        thePlane )
646 {
647   if ( !IsValidAsAxis( theHydAxis ) )
648     return false;
649
650   TopoDS_Wire aHydraulicWire = TopoDS::Wire( theHydAxis->GetShape() );
651
652   gp_Ax2 aX2(gp::XOY());
653   gp_Ax3 aX3(aX2);
654   gp_Pln aPln(aX3);   
655   Bnd_Box B;
656   BRepBndLib::Add(aHydraulicWire,B);
657   Standard_Real axmin,aymin,azmin,axmax,aymax,azmax;
658   B.Get(axmin,aymin,azmin,axmax,aymax,azmax);
659   BRepBuilderAPI_MakeFace aMkr(aPln, axmin-500., axmax+500., aymin-500., aymax+500.); // to be tuned later according max/ Profile deviation
660   if(!aMkr.IsDone() || aMkr.Shape().IsNull()) return false;
661   thePlane = TopoDS::Face(aMkr.Shape());
662   return true;
663 }
664
665 void HYDROData_Stream::updateProfilesOrder()
666 {
667   HYDROData_SequenceOfObjects aRefProfiles = GetProfiles();
668   if ( aRefProfiles.IsEmpty() )
669     return;
670
671   // At first we remove all profiles from order
672   RemoveProfiles();
673
674   Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
675   if ( aHydAxis.IsNull() )
676     return; 
677
678   TopoDS_Face aPlane;
679   if ( !BuildFace( aHydAxis, aPlane ) )
680     return;
681
682   Standard_Real aPar( .0 );
683
684 #ifdef DEB_HASINT
685   BRep_Builder aBB;
686   TopoDS_Compound aCmp;
687   aBB.MakeCompound(aCmp);
688 #endif
689
690   HYDROData_DataMapOfRealOfHDProfile aDM;  
691   TColStd_ListOfReal aList;
692   HYDROData_SequenceOfObjects::Iterator anIter( aRefProfiles );
693   for (int i = 1 ; anIter.More(); anIter.Next(), i++ )
694   {
695     Handle(HYDROData_Profile) aProfile =
696       Handle(HYDROData_Profile)::DownCast( anIter.Value() );
697 #ifdef DEB_HASINT
698   TopoDS_Wire aProfileWire = TopoDS::Wire( aProfile->GetTopShape() );
699   aBB.Add( aCmp, aProfileWire ); 
700 #endif
701     if ( aProfile.IsNull() || !HasIntersection( aProfile, aPlane, aPar ) )
702       continue;
703     
704     aDM.Bind( aPar, aProfile );
705     aList.Append( aPar );
706   }
707   
708   if ( aList.IsEmpty() )
709     return;
710
711   TColStd_Array1OfReal anArr( 1, aList.Extent() );
712
713   TColStd_ListIteratorOfListOfReal it( aList );
714   for ( int j = 1; it.More(); it.Next(), j++ )
715     anArr( j ) = it.Value();
716
717   // sorting
718   if ( aList.Extent() > 1 )
719   {
720     TCollection_CompareOfReal Compar;
721     SortTools_QuickSortOfReal::Sort( anArr, Compar );
722
723     for (int j = 1; j <= anArr.Length(); j++) {
724       const Standard_Real aKey =  anArr(j);
725       const Handle(HYDROData_Profile)& aProfile = aDM.Find(aKey);
726       insertProfileInToOrder( aProfile );
727     }
728   } else if ( aList.Extent() == 1 ) {
729      const Standard_Real aKey = aList.Last();
730      const Handle(HYDROData_Profile)& aProfile = aDM.Find(aKey);
731      insertProfileInToOrder( aProfile );
732   } 
733
734   setParametersArray( anArr );
735
736 #ifdef DEB_HASINT
737   TopoDS_Wire aHydraulicWire = TopoDS::Wire( aHydAxis->GetShape() );
738   BRepTools::Write(aHydraulicWire, "Path.brep");
739   BRepTools::Write(aCmp, "Prof.brep");
740 #endif
741 }
742
743 ObjectKind HYDROData_Stream::getAltitudeObjectType() const
744 {
745   return KIND_DTM;
746 }
747
748 void HYDROData_Stream::setParametersArray( const TColStd_Array1OfReal& theArray )
749 {
750   if ( theArray.Length() == 0 )
751   {
752     removeParametersArray();
753     return;
754   }
755
756   TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray );
757   
758   Handle(TDataStd_RealArray) aParamsArray = 
759     TDataStd_RealArray::Set( aLabel, theArray.Lower(), theArray.Upper() );
760
761   for ( int i = theArray.Lower(), n = theArray.Upper(); i <= n; ++i )
762   {
763     const Standard_Real& aParam = theArray( i );
764     aParamsArray->SetValue( i, aParam );
765   }
766 }
767
768 TColStd_Array1OfReal* HYDROData_Stream::getParametersArray() const
769 {
770   TColStd_Array1OfReal* anArray = NULL;
771
772   TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray, false );
773   if ( !aLabel.IsNull() )
774   {
775     Handle(TDataStd_RealArray) aParamsArray;
776     if ( aLabel.FindAttribute( TDataStd_RealArray::GetID(), aParamsArray ) )
777     {
778       anArray = new TColStd_Array1OfReal( aParamsArray->Lower(), aParamsArray->Upper() );
779       for ( int i = aParamsArray->Lower(), n = aParamsArray->Upper(); i <= n; ++i )
780       {
781         const Standard_Real& aParam = aParamsArray->Value( i );
782         anArray->SetValue( i, aParam );
783       }
784     }
785   }
786
787   return anArray;
788 }
789
790 void HYDROData_Stream::removeParametersArray()
791 {
792   TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray, false );
793   if ( !aLabel.IsNull() )
794     aLabel.ForgetAllAttributes();
795 }
796
797 int HYDROData_Stream::insertParameter( const Standard_Real& theParam )
798 {
799   int aResIndex = -1;
800
801   TColStd_Array1OfReal* anArr = getParametersArray();
802   if ( anArr )
803   {
804     aResIndex = 0;
805
806     TColStd_Array1OfReal aNewArr( anArr->Lower(), anArr->Upper() + 1 );
807
808     bool isInserted = false;
809     for ( int i = anArr->Lower(), j = i, n = anArr->Upper(); i <= n; ++i, ++j )
810     {
811       const Standard_Real& aStoredParam = anArr->Value( i );
812       if ( !isInserted )
813       {
814         if ( theParam > aStoredParam )
815         {
816           aResIndex++;
817         }
818         else
819         {
820           aNewArr( j ) = theParam;
821           isInserted = true;
822           ++j;
823         }
824       }
825
826       aNewArr( j ) = aStoredParam;
827     }
828
829     if ( !isInserted )
830     {
831       aResIndex = -1;
832       aNewArr( aNewArr.Upper() ) = theParam;
833     }
834     
835     setParametersArray( aNewArr );
836     delete anArr;
837   }
838   else
839   {
840     TColStd_Array1OfReal aNewArr( 1, 1 );
841     aNewArr.SetValue( 1, theParam );
842     setParametersArray( aNewArr );
843   }
844
845   return aResIndex;
846 }
847
848 void HYDROData_Stream::removeParameter( const int& theIndex )
849 {
850   TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray, false );
851   if ( aLabel.IsNull() )
852     return;
853
854   Handle(TDataStd_RealArray) aParamsArray;
855   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), aParamsArray ) )
856     return;
857
858   if ( aParamsArray->Length() == 1 )
859   {
860     removeParametersArray();
861     return;
862   }
863
864   TColStd_Array1OfReal aNewArr( aParamsArray->Lower(), aParamsArray->Upper() - 1 );
865
866   for ( int i = aParamsArray->Lower(), j = i, k = 0, n = aParamsArray->Upper(); i <= n; ++i, ++k )
867   {
868     const Standard_Real& aStoredParam = aParamsArray->Value( i );
869     if ( k == theIndex )
870       continue;
871
872     aNewArr.SetValue( j, aStoredParam );
873     ++j;
874   }
875
876   setParametersArray( aNewArr );
877 }
878
879 bool HYDROData_Stream::GenerateBottomPolyline()
880 {
881   // Get the document
882   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
883   if ( aDocument.IsNull() ) {
884     return false;
885   }
886
887   // Collect bottom points ( one bottom point from each profile of the stream )
888   HYDROData_Profile::ProfilePoints aBottomPoints;
889   
890   HYDROData_SequenceOfObjects aSeqOfProfiles = GetProfiles();
891   for ( int i = 1, aNb = aSeqOfProfiles.Size(); i <= aNb; i++ ) {
892     const Handle(HYDROData_Profile) aProfile = 
893       Handle(HYDROData_Profile)::DownCast( aSeqOfProfiles.Value( i ) );
894     if ( aProfile.IsNull() ) {
895       continue;
896     }
897     
898     aBottomPoints.Append( aProfile->GetBottomPoint() );
899   }
900
901   int aNbBottomPoints = aBottomPoints.Size();
902
903   if ( aNbBottomPoints < 2 ) {
904     return false;
905   }
906
907   // Create bottom polyline object if the stream doesn't contain it yet
908   Handle(HYDROData_Polyline3D) aBottom = GetBottomPolyline();
909   if ( aBottom.IsNull() ) {
910     aBottom = Handle(HYDROData_Polyline3D)::DownCast( aDocument->CreateObject( KIND_POLYLINE ) );  
911     QString aBaseName = GetName() + "_bottom";
912     QString aName = HYDROData_Tool::GenerateObjectName( aDocument, aBaseName, QStringList(), true );
913     aBottom->SetName( aName );
914
915     SetReferenceObject( aBottom, DataTag_BottomPolyline );
916   }
917   
918   // Create 2D polyline if the bottom polyline doesn't contain it yet
919   Handle(HYDROData_PolylineXY) aPolylineXY = aBottom->GetPolylineXY();
920   if ( aPolylineXY.IsNull() ) {
921     aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( aDocument->CreateObject( KIND_POLYLINEXY ) );
922     QString aBaseName = GetName() + "_bottom_2d";
923     QString aName = HYDROData_Tool::GenerateObjectName( aDocument, aBaseName, QStringList(), true );
924     aPolylineXY->SetName( aName );
925     aBottom->SetPolylineXY( aPolylineXY, false );
926   }
927
928   aPolylineXY->RemoveSections();
929   aPolylineXY->AddSection( "", HYDROData_PolylineXY::SECTION_SPLINE, false );
930   
931   // Create profile if the bottom polyline doesn't contain it yet
932   Handle(HYDROData_ProfileUZ) aProfileUZ = aBottom->GetProfileUZ();
933   if ( aProfileUZ.IsNull() ) {
934     Handle(HYDROData_Profile) aProfile = 
935       Handle(HYDROData_Profile)::DownCast( aDocument->CreateObject( KIND_PROFILE ) );
936     QString aBaseName = GetName() + "_bottom_profile";
937     QString aName = HYDROData_Tool::GenerateObjectName( aDocument, aBaseName, QStringList(), true );
938     aProfile->SetName( aName );
939     aProfileUZ = aProfile->GetProfileUZ( true );
940     aBottom->SetProfileUZ( aProfileUZ );
941   }
942   
943   aProfileUZ->RemoveSection( 0 );
944
945   aProfileUZ->CalculateAndAddPoints(aBottomPoints, aPolylineXY);
946   
947   return true;
948 }
949
950 Handle(HYDROData_Polyline3D) HYDROData_Stream::GetBottomPolyline() const
951 {
952   return Handle(HYDROData_Polyline3D)::DownCast( 
953            GetReferenceObject( DataTag_BottomPolyline ) );
954 }
955
956 bool HYDROData_Stream::SetBottomPolyline( const Handle(HYDROData_Polyline3D)& theBottom )
957 {
958   if ( theBottom.IsNull() ) {
959     return false;
960   }
961
962   SetReferenceObject( theBottom, DataTag_BottomPolyline );
963
964   return true;
965 }
966
967 bool HYDROData_Stream::Interpolate( HYDROData_IProfilesInterpolator* theInterpolator )
968 {
969   // Get the document
970   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
971   if ( aDocument.IsNull() ) {
972     return false;
973   }
974   
975   if ( theInterpolator->GetCalculatedProfilesNumber() < 1 ) {
976     theInterpolator->Calculate();
977   }
978
979   if ( theInterpolator->GetErrorCode() != OK ) {
980     return false;
981   }
982
983   bool isOK = true;
984
985   for ( int aProfileInd = 0; aProfileInd < theInterpolator->GetCalculatedProfilesNumber(); aProfileInd++ ) {
986     // Get calculated point coordinates
987     HYDROData_Profile::ProfilePoints aResultPoints = theInterpolator->GetResultProfilePoints( aProfileInd );
988     if ( aResultPoints.IsEmpty() ) {
989       isOK = false;
990       continue;
991     }
992         
993     // Create profile object
994     Handle(HYDROData_Profile) aProfile = 
995       Handle(HYDROData_Profile)::DownCast( aDocument->CreateObject( KIND_PROFILE ) );
996     QString aBaseName = GetName() + "_interp_profile";
997     QString aName = HYDROData_Tool::GenerateObjectName( aDocument, aBaseName );
998     aProfile->SetName( aName );
999
1000     // Fill the profile with points
1001     aProfile->SetProfilePoints( aResultPoints );
1002
1003     // Add profile to the stream
1004     bool isAdded = AddProfile( aProfile );
1005     if ( !isAdded ) {
1006       aProfile->Remove();
1007     }
1008     else
1009       aProfile->Update();
1010   }
1011
1012   if ( isOK )
1013     Update();
1014
1015   return isOK;
1016 }
1017
1018 void HYDROData_Stream::CopyTo( const Handle(HYDROData_Entity)& theDestination,
1019                                bool isGenerateNewName ) const
1020 {
1021   // Get the document
1022   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
1023   if ( aDocument.IsNull() ) {
1024     return;
1025   }
1026
1027   // Call base method
1028   HYDROData_Entity::CopyTo( theDestination, isGenerateNewName );
1029
1030   Handle(HYDROData_Stream) aStreamCopy = 
1031     Handle(HYDROData_Stream)::DownCast( theDestination );
1032
1033   // Copy bottom polyline if exists
1034   if ( !aStreamCopy.IsNull() ) {
1035     const Handle(HYDROData_Polyline3D) aBottom = GetBottomPolyline();
1036     if ( !aBottom.IsNull() ) {
1037       aStreamCopy->ClearReferenceObjects( DataTag_BottomPolyline );
1038       aStreamCopy->GenerateBottomPolyline();
1039       const Handle(HYDROData_Polyline3D) aBottomCopy = aStreamCopy->GetBottomPolyline();
1040       if ( !aBottomCopy.IsNull() && !aBottomCopy->GetPolylineXY().IsNull() ) {
1041         aBottomCopy->GetPolylineXY()->Update();
1042         aBottomCopy->Update();
1043       }
1044     }
1045   }
1046 }
1047
1048 static void ProjWireOnPlane(const TopoDS_Wire& inpWire, const Handle_Geom_Plane& RefPlane, TopoDS_Wire& outWire)
1049 {
1050   BRepTools_WireExplorer ex(TopoDS::Wire(inpWire.Oriented(TopAbs_FORWARD)));
1051   BRepLib_MakeWire WM;
1052   for (;ex.More();ex.Next())
1053   {
1054     const TopoDS_Edge& CE = ex.Current(); 
1055     double f, l;
1056     Handle(Geom_Curve) C3d = BRep_Tool::Curve(CE, f, l);
1057     Handle(Geom_Curve) ProjectedCurve = GeomProjLib::ProjectOnPlane(new Geom_TrimmedCurve(C3d, f, l), RefPlane, RefPlane->Position().Direction(), Standard_True);
1058     TopoDS_Edge ProjEdge = BRepLib_MakeEdge(ProjectedCurve);
1059     WM.Add(ProjEdge); //auto sharing between edges if vertex is coincident
1060   }
1061   outWire = WM.Wire();
1062   outWire.Orientation(inpWire.Orientation()); //take from the original wire
1063 }
1064
1065
1066 static void Get2dFaceFrom3dPres(const TopoDS_Compound& cmp, TopoDS_Face& outF )
1067 {
1068   Handle_Geom_Plane refpl = new Geom_Plane(gp_Pnt(0,0,0), gp_Dir(0,0,1));
1069   BRepLib_MakeWire WM;
1070   TopoDS_Iterator it(cmp);
1071   //TopTools_IndexedMapOfShape IntW;
1072   for (;it.More(); it.Next())
1073   {
1074     const TopoDS_Wire& W = TopoDS::Wire(it.Value());
1075     if (W.Orientation() != TopAbs_INTERNAL)
1076     {
1077       //use list of edges to protect againts non-manifold cases.
1078       //auto sharing between edges will be added automatically
1079       TopTools_IndexedMapOfShape ME;
1080       TopTools_ListOfShape LE;
1081       TopExp::MapShapes(W, TopAbs_EDGE, ME);
1082       for (int i = 1; i <= ME.Extent(); i++)
1083         LE.Append(ME(i));
1084       WM.Add(LE);
1085     }
1086     //else
1087     //  IntW.Add(W);
1088   }
1089
1090   TopoDS_Wire outW;
1091   ProjWireOnPlane(WM.Wire(), refpl, outW);
1092   BRepBuilderAPI_MakeFace mf(refpl, outW); //check inside is true by def
1093   outF = mf.Face();
1094
1095   ///!!! the internal wires cant be added with 'internal' ori.
1096   // it's possible to do with brep builder yet the result will not be correct!
1097   // more proper way is to use BOP operation here.
1098   /*for (int i = 1; i <= IntW.Extent(); i++)
1099   {
1100   TopoDS_Wire outIW;
1101   const TopoDS_Wire& W = TopoDS::Wire(IntW(i));
1102   ProjWireOnPlane(W, refpl, outIW);
1103   BB.Add(outF, outIW);
1104   }*/
1105 }
1106
1107 void HYDROData_Stream::CreatePresentationsIntern( const Handle_HYDROData_DTM& theDTM,
1108                                                   PrsDefinition& thePrs )
1109 {
1110   TopoDS_Compound cmp = TopoDS::Compound(theDTM->GetShape(HYDROData_DTM::DataTag_DTM_Shape));
1111   thePrs.myPrs3D = cmp;
1112   NCollection_Sequence<TopoDS_Wire> WW;
1113   TopoDS_Iterator it(cmp);
1114   for (;it.More(); it.Next())
1115     WW.Append(TopoDS::Wire(it.Value()));
1116
1117   //same order as in HYDROData_DTM::Update()
1118   thePrs.myLeftBank = WW.First();
1119   thePrs.myRightBank = WW.Last();
1120   thePrs.myInlet = WW(1); //TODO check this!!
1121   thePrs.myOutlet = WW(WW.Length() - 1);
1122
1123   TopoDS_Face outF;
1124   Get2dFaceFrom3dPres(cmp, outF);
1125   thePrs.myPrs2D = outF;
1126
1127   /*if ( theArrayOfFPnt.IsNull() || theArrayOfLPnt.IsNull() || theArrOfProfiles.IsNull() ) {
1128     return false;
1129   }
1130
1131   if ( theArrayOfFPnt->Length() != theArrayOfLPnt->Length() ) {
1132     return false;
1133   }
1134   
1135   // Construct of the 3D presentation
1136   Handle(Geom_BSplineCurve) aBSpline = buildInterpolationCurve (theArrayOfFPnt);
1137   if(aBSpline.IsNull())
1138     return false;
1139
1140   TopoDS_Edge anEdgLeft, anEdgRight;
1141   
1142   BRepBuilderAPI_MakeEdge aMakeEdge(aBSpline);
1143   if(aMakeEdge.IsDone()) 
1144     anEdgLeft = aMakeEdge.Edge();
1145
1146   if(anEdgLeft.IsNull())
1147     return false;
1148
1149   aBSpline.Nullify();
1150   aBSpline = buildInterpolationCurve (theArrayOfLPnt);  
1151   if(aBSpline.IsNull())
1152     return false;
1153
1154   aMakeEdge.Init(aBSpline);
1155   if(aMakeEdge.IsDone()) 
1156     anEdgRight = aMakeEdge.Edge();
1157
1158   if(anEdgRight.IsNull())
1159     return false;
1160
1161   BRep_Builder aBB;
1162   TopoDS_Compound aCmp;
1163   aBB.MakeCompound(aCmp);
1164   for (int i=1 ; i < theArrOfProfiles->Length() +1; i++ )  
1165     aBB.Add(aCmp, theArrOfProfiles->Value(i));
1166
1167   aBB.Add(aCmp,anEdgLeft);
1168   aBB.Add(aCmp,anEdgRight);
1169   BRepCheck_Analyzer aCh(aCmp);
1170   if(aCh.IsValid())
1171     thePrs.myPrs3D = aCmp;
1172 #ifdef DEB_UPDATE
1173   else {
1174     BRepTools::Write(aCmp, "str3d.brep");
1175     thePrs.myPrs3D = aCmp;
1176   }
1177 #endif
1178
1179   // Construct the top presentation
1180   int aNbPoints = theArrayOfFPnt->Length();
1181   Handle(TColgp_HArray1OfPnt) anArrayOfFPnt = new TColgp_HArray1OfPnt(1, aNbPoints);
1182   Handle(TColgp_HArray1OfPnt) anArrayOfLPnt = new TColgp_HArray1OfPnt(1, aNbPoints);  
1183   for( int i=1; i <= aNbPoints; i++ ) {
1184       gp_Pnt aPnt = theArrayOfFPnt->Value(i);
1185       aPnt.SetZ(.0); // make 2d
1186       anArrayOfFPnt->SetValue(i, aPnt);
1187       aPnt = theArrayOfLPnt->Value(i);
1188       aPnt.SetZ(.0);
1189       anArrayOfLPnt->SetValue(i, aPnt);
1190   }
1191
1192   aBSpline.Nullify();
1193   aBSpline = buildInterpolationCurve (anArrayOfFPnt);  
1194   if(aBSpline.IsNull())
1195     return false; 
1196
1197   aMakeEdge.Init(aBSpline);
1198   if(aMakeEdge.IsDone()) 
1199       anEdgLeft = aMakeEdge.Edge();
1200
1201   aBSpline.Nullify();
1202   aBSpline = buildInterpolationCurve (anArrayOfLPnt);  
1203   if(aBSpline.IsNull())
1204     return false; 
1205
1206   aMakeEdge.Init(aBSpline);
1207   if(aMakeEdge.IsDone()) 
1208     anEdgRight = aMakeEdge.Edge();
1209   if(anEdgRight.IsNull())
1210     return false;
1211
1212   BRepBuilderAPI_MakeEdge aMakeEdge2(anArrayOfFPnt->Value(1),anArrayOfLPnt->Value(1));
1213   TopoDS_Edge aBotEdge, aTopEdge;
1214   if(aMakeEdge2.IsDone()) 
1215     aBotEdge = aMakeEdge2.Edge();
1216
1217   BRepBuilderAPI_MakeEdge aMakeEdge3(anArrayOfFPnt->Value(anArrayOfFPnt->Length()),anArrayOfLPnt->Value(anArrayOfLPnt->Length()));
1218   if(aMakeEdge3.IsDone()) 
1219     aTopEdge = aMakeEdge3.Edge();
1220
1221   // Make wire for 2D presentation with updating of corresponding edges
1222   BRepBuilderAPI_MakeWire aMakeWire;
1223   
1224   aMakeWire.Add( aBotEdge );
1225   thePrs.myInlet = aMakeWire.Edge();
1226
1227   aMakeWire.Add( anEdgLeft );
1228   thePrs.myLeftBank = aMakeWire.Edge();
1229
1230   aMakeWire.Add( aTopEdge );
1231   thePrs.myOutlet = aMakeWire.Edge();
1232
1233   aMakeWire.Add( anEdgRight );
1234   thePrs.myRightBank = aMakeWire.Edge();
1235
1236   TopoDS_Wire aSectProfileWire;
1237   if(aMakeWire.IsDone())
1238     aSectProfileWire = aMakeWire.Wire();
1239
1240   BRepBuilderAPI_MakeFace aMakeFace( aSectProfileWire, Standard_True );
1241   TopoDS_Face aFace;
1242   aMakeFace.Build();
1243   if( aMakeFace.IsDone() )
1244     aFace = aMakeFace.Face();
1245
1246   TopoDS_Shape aPrs2D;
1247
1248   if ( !theArrOf2DProfiles.IsNull() ) {
1249     aCmp.Nullify();
1250     aBB.MakeCompound(aCmp);
1251     aBB.Add(aCmp,aFace);
1252     for(int i=1;i <= theArrOf2DProfiles->Length(); i++)
1253       aBB.Add(aCmp, theArrOf2DProfiles->Value(i));
1254
1255     aPrs2D = aCmp;
1256   } else {
1257     aPrs2D = aFace;
1258   }
1259
1260   aCh.Init(aPrs2D);
1261   if(aCh.IsValid())
1262    thePrs.myPrs2D = aPrs2D;
1263 #ifdef DEB_UPDATE
1264   else {
1265     BRepTools::Write(aPrs2D, "str2d.brep");
1266     thePrs.myPrs2D = aPrs2D;
1267   }
1268 #endif
1269
1270   return true;*/
1271 }
1272
1273 void HYDROData_Stream::CreatePresentations( const Handle(TColgp_HArray1OfPnt)     theArrayOfFPnt,
1274                                             const Handle(TColgp_HArray1OfPnt)     theArrayOfLPnt,
1275                                             const Handle(TopTools_HArray1OfShape) theArrOfProfiles,
1276                                             PrsDefinition&                        thePrs )
1277 {
1278
1279   HYDROData_Bathymetry::AltitudePoints left;
1280   for (int i = theArrayOfLPnt->Lower(); i <= theArrayOfLPnt->Upper(); i++)
1281   {
1282     left.push_back(HYDROData_Bathymetry::AltitudePoint(theArrayOfLPnt->Value(i).X(), 
1283       theArrayOfLPnt->Value(i).Y(),
1284       theArrayOfLPnt->Value(i).Z()));
1285   }
1286
1287   HYDROData_Bathymetry::AltitudePoints right;
1288   for (int i = theArrayOfFPnt->Lower(); i <= theArrayOfFPnt->Upper(); i++)
1289   {
1290     right.push_back(HYDROData_Bathymetry::AltitudePoint(theArrayOfFPnt->Value(i).X(), 
1291       theArrayOfFPnt->Value(i).Y(),
1292       theArrayOfFPnt->Value(i).Z()));
1293   }
1294
1295   std::vector<HYDROData_Bathymetry::AltitudePoints> dummy;
1296   TopoDS_Compound cmp = HYDROData_DTM::Create3DShape(left, right, dummy);
1297   TopoDS_Iterator it(cmp);
1298   thePrs.myLeftBank = TopoDS::Wire(it.Value());
1299   it.Next();
1300   thePrs.myRightBank = TopoDS::Wire(it.Value());
1301
1302   thePrs.myInlet = TopoDS::Wire(theArrOfProfiles->Value(theArrOfProfiles->Lower())); //TODO check that
1303   thePrs.myOutlet = TopoDS::Wire(theArrOfProfiles->Value(theArrOfProfiles->Upper()));
1304
1305   //make new compound so it's shapes will be in known order to build correct projection
1306   BRep_Builder BB;
1307   TopoDS_Compound newCmp;
1308   BB.MakeCompound(newCmp);
1309   BB.Add(newCmp, thePrs.myLeftBank);
1310   BB.Add(newCmp, thePrs.myInlet);
1311   BB.Add(newCmp, thePrs.myOutlet);
1312   BB.Add(newCmp, thePrs.myRightBank);
1313
1314   thePrs.myPrs3D = newCmp;
1315
1316   Get2dFaceFrom3dPres( newCmp, TopoDS::Face(thePrs.myPrs2D) );
1317
1318 }