Salome HOME
compilation on Linux
[modules/hydro.git] / src / HYDROData / HYDROData_PolylineXY.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROData_PolylineXY.h"
24
25 #include "HYDROData_BSplineOperation.h"
26 #include "HYDROData_Document.h"
27 #include "HYDROData_ShapesTool.h"
28 #include "HYDROData_Tool.h"
29
30 #include <BRep_Builder.hxx>
31 #include <BRepBuilderAPI_MakeEdge.hxx>
32 #include <BRepBuilderAPI_MakeWire.hxx>
33 #include <BRepBuilderAPI_MakePolygon.hxx>
34 #include <BRepBuilderAPI_MakeFace.hxx>
35 #include <BRepOffsetAPI_NormalProjection.hxx>
36
37 #include <GEOMBase.h>
38
39 #include <GeomAPI_ProjectPointOnCurve.hxx>
40 #include <GeomAdaptor_Curve.hxx>
41 #include <Geom_Line.hxx>
42 #include <Geom_BSplineCurve.hxx>
43
44 #include <GCPnts_AbscissaPoint.hxx>
45
46 #include <ImageComposer_MetaTypes.h>
47
48 #include <gp_Pnt.hxx>
49 #include <gp_XY.hxx>
50 #include <gp_Pln.hxx>
51
52 #include <NCollection_Map.hxx>
53
54 #include <TCollection_ExtendedString.hxx>
55
56 #include <TDataStd_ListIteratorOfListOfByte.hxx>
57 #include <TColStd_ListIteratorOfListOfInteger.hxx>
58 #include <TColStd_ListIteratorOfListOfReal.hxx>
59
60 #include <TColStd_Array1OfReal.hxx>
61
62 #include <TDataStd_AsciiString.hxx>
63 #include <TDataStd_BooleanList.hxx>
64 #include <TDataStd_ExtStringList.hxx>
65 #include <TDataStd_IntegerList.hxx>
66 #include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
67 #include <TDataStd_RealList.hxx>
68 #include <TDataStd_UAttribute.hxx>
69
70 #include <TopoDS_Iterator.hxx>
71 #include <TopTools_ListIteratorOfListOfShape.hxx>
72 #include <TopTools_HSequenceOfShape.hxx>
73 #include <TopExp_Explorer.hxx>
74 #include <ShapeAnalysis_FreeBounds.hxx>
75 #include <TopoDS.hxx>
76
77 #include <QColor>
78 #include <QPainterPath>
79 #include <QVariant>
80
81 static const Standard_GUID GUID_IS_UNEDITABLE("e5799736-9030-4051-91a4-2e58321fa153");
82
83 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
84
85 TCollection_AsciiString getUniqueSectionName( const NCollection_Sequence<TCollection_AsciiString>& theNamesSeq )
86 {
87   NCollection_Map<TCollection_AsciiString> aNamesMap;
88
89   for ( int i = 1, n = theNamesSeq.Size(); i <= n; ++i )
90   {
91     const TCollection_AsciiString& aSectName = theNamesSeq.Value( i );
92     aNamesMap.Add( aSectName );
93   }
94
95   TCollection_AsciiString aResName;
96
97   int aPrefIdx = 1;
98   do
99   {
100     aResName = TCollection_AsciiString( "Section_" ) + aPrefIdx;
101     ++aPrefIdx;
102   }
103   while ( aNamesMap.Contains( aResName ) );
104
105   return aResName;
106 }
107
108 TCollection_AsciiString getUniqueSectionName( const Handle(TDataStd_ExtStringList)& theNamesList )
109 {
110   NCollection_Sequence<TCollection_AsciiString> aNamesSeq;
111
112   TDataStd_ListIteratorOfListOfExtendedString aNamesIter( theNamesList->List() );
113   for ( ; aNamesIter.More(); aNamesIter.Next() )
114     aNamesSeq.Append( aNamesIter.Value() );
115
116   return getUniqueSectionName( aNamesSeq );
117 }
118
119 IMPLEMENT_STANDARD_HANDLE(HYDROData_PolylineXY, HYDROData_IPolyline)
120 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_PolylineXY, HYDROData_IPolyline)
121
122 HYDROData_PolylineXY::HYDROData_PolylineXY()
123 : HYDROData_IPolyline()
124 {
125 }
126
127 HYDROData_PolylineXY::~HYDROData_PolylineXY()
128 {
129 }
130
131 QStringList HYDROData_PolylineXY::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
132 {
133   QStringList aResList = dumpObjectCreation( theTreatedObjects );
134   QString aPolylineName = GetObjPyName();
135
136   // Set the wire color
137   QStringList aWireColorDef;
138
139   QColor aWireColor = GetWireColor();
140   setPythonObjectColor( aWireColorDef, aWireColor, DefaultWireColor(), "SetWireColor" );
141   
142   if ( !aWireColorDef.isEmpty() )
143   {
144     aResList << aWireColorDef;
145     aResList << QString( "" );
146   }
147
148   bool anIsEditable = IsEditable();
149   if ( !anIsEditable )
150   {
151     // If polyline is not editable we try to import the shape from geom
152     TCollection_AsciiString aGeomObjectEntry = GetGeomObjectEntry();
153     if ( !aGeomObjectEntry.IsEmpty() )
154     {
155       QString aSalomeObjName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "polyline_sobj" );
156       aResList << QString( "%1 = salome.myStudy.FindObjectID( \"%2\" );" )
157                   .arg( aSalomeObjName ).arg( aGeomObjectEntry.ToCString() );
158
159       aResList << QString( "%1.ImportFromGeomIOR( %2.GetIOR() );" )
160                   .arg( aPolylineName ).arg( aSalomeObjName );
161
162       aResList << QString( "%1.SetGeomObjectEntry( \"%2\" );" )
163                   .arg( aPolylineName ).arg( aGeomObjectEntry.ToCString() );
164     }
165   }
166   else
167   {
168     // Set polilyne data
169     NCollection_Sequence<TCollection_AsciiString>           aSectNames;
170     NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
171     NCollection_Sequence<bool>                              aSectClosures;
172     GetSections( aSectNames, aSectTypes, aSectClosures );
173
174     for ( int i = 1, n = aSectNames.Size(); i <= n; ++i )
175     {
176       const TCollection_AsciiString& aSectName = aSectNames.Value( i );
177       const SectionType& aSectType = aSectTypes.Value( i );
178       bool aSectClosure = aSectClosures.Value( i );
179
180       aResList << QString( "%1.AddSection( \"%2\", %3, %4 );" ).arg( aPolylineName )
181                   .arg( aSectName.ToCString() ).arg( aSectType ).arg( aSectClosure );
182
183       HYDROData_IPolyline::PointsList aSectPointsList = GetPoints( i - 1 );
184       for ( int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k )
185       {
186         const Point& aSectPoint = aSectPointsList.Value( k );
187
188         QString anXStr = QString::number( aSectPoint.X(), 'f', 2 );
189         QString anYStr = QString::number( aSectPoint.Y(), 'f', 2 );
190         aResList << QString( "%1.AddPoint( %2, gp_XY( %3, %4 ) );" ).arg( aPolylineName )
191           .arg( i - 1 ).arg( anXStr ).arg( anYStr );
192       }
193     }
194   }
195   aResList << QString( "" );
196   aResList << QString( "%1.Update();" ).arg( aPolylineName );
197   aResList << QString( "" );
198
199   return aResList;
200 }
201
202 QVariant HYDROData_PolylineXY::GetDataVariant()
203 {
204   QPainterPath aPath = GetPainterPath();
205
206   QVariant aVarData;
207   aVarData.setValue<QPainterPath>( aPath );
208   
209   return aVarData;
210 }
211
212 QColor HYDROData_PolylineXY::DefaultWireColor()
213 {
214   return QColor( Qt::red );
215 }
216
217 bool HYDROData_PolylineXY::ImportFromGeomIOR( const TCollection_AsciiString& theIOR )
218 {
219   if ( theIOR.IsEmpty() )
220     return false;
221
222   TopoDS_Shape aShape = GEOMBase::GetShapeFromIOR( theIOR.ToCString() );
223   if ( aShape.IsNull() )
224     return false;
225
226   return ImportShape( aShape );
227 }
228
229 void HYDROData_PolylineXY::SetGeomObjectEntry( const TCollection_AsciiString& theEntry )
230 {
231   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_GeomObjectEntry ), theEntry );
232 }
233
234 TCollection_AsciiString HYDROData_PolylineXY::GetGeomObjectEntry() const
235 {
236   TCollection_AsciiString aRes;
237
238   TDF_Label aLabel = myLab.FindChild( DataTag_GeomObjectEntry, false );
239   if ( !aLabel.IsNull() )
240   {
241     Handle(TDataStd_AsciiString) anAsciiStr;
242     if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
243       aRes = anAsciiStr->Get();
244   }
245
246   return aRes;
247 }
248
249 TopoDS_Shape HYDROData_PolylineXY::GetShape() const
250 {
251   return getPolylineShape();
252 }
253
254 bool convertEdgeToSection( const TopoDS_Edge&                                       theEdge,
255                            NCollection_Sequence<TCollection_AsciiString>&           theSectNames,
256                            NCollection_Sequence<HYDROData_PolylineXY::SectionType>& theSectTypes,
257                            NCollection_Sequence<bool>&                              theSectClosures,
258                            NCollection_Sequence<HYDROData_PolylineXY::PointsList>&  theSectPoints,
259                            const bool                                               theIsCanBeClosed )
260 {
261   Standard_Real aFirst = 0.0, aLast = 0.0;
262   Handle(Geom_Curve) anEdgeGeomCurve = BRep_Tool::Curve( theEdge, aFirst, aLast );
263   if ( anEdgeGeomCurve.IsNull() )
264     return false;
265
266   TCollection_AsciiString aSectName = getUniqueSectionName( theSectNames );
267   bool anIsEdgeClosed = anEdgeGeomCurve->IsClosed();
268
269   HYDROData_PolylineXY::SectionType aSectionType = HYDROData_PolylineXY::SECTION_POLYLINE;
270   HYDROData_PolylineXY::PointsList aPointsList;
271
272   if ( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_Line) ) )
273   {
274     Handle(Geom_Line) aGeomLine = Handle(Geom_Line)::DownCast( anEdgeGeomCurve );
275
276     gp_Pnt aFirstPoint, aLastPoint;
277     aGeomLine->D0( aFirst, aFirstPoint );
278     aGeomLine->D0( aLast, aLastPoint );
279
280     HYDROData_PolylineXY::Point aSectFirstPoint( aFirstPoint.X(), aFirstPoint.Y() );
281     aPointsList.Append( aSectFirstPoint );
282
283     HYDROData_PolylineXY::Point aSectLastPoint( aLastPoint.X(), aLastPoint.Y() );
284     aPointsList.Append( aSectLastPoint );
285   }
286   else if ( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) )
287   {
288     aSectionType = HYDROData_PolylineXY::SECTION_SPLINE;
289
290     Handle(Geom_BSplineCurve) aGeomSpline = 
291       Handle(Geom_BSplineCurve)::DownCast( anEdgeGeomCurve );
292
293     int aNbKnots = aGeomSpline->NbKnots();
294
295     TColStd_Array1OfReal aSplineKnots( 1, aNbKnots );
296     aGeomSpline->Knots( aSplineKnots );
297
298     // Decrease the number of imported knots because of last one 
299     // knot is the closing point which are the start point
300     if ( anIsEdgeClosed ) aNbKnots--;
301
302     for ( int i = 1; i <= aNbKnots; ++i )
303     {
304       const Standard_Real& aKnot = aSplineKnots.Value( i );
305
306       gp_Pnt aPoint;
307       aGeomSpline->D0( aKnot, aPoint );
308
309       HYDROData_PolylineXY::Point aSectPoint( aPoint.X(), aPoint.Y() );
310       aPointsList.Append( aSectPoint );
311     }
312   }
313   else
314   {
315     // Other curve types are not supported
316     return false;
317   }
318
319   if ( aPointsList.IsEmpty() )
320     return false;
321
322   theSectNames.Append( aSectName );
323   theSectTypes.Append( aSectionType );
324   theSectClosures.Append( anIsEdgeClosed );
325   theSectPoints.Append( aPointsList );
326
327   return true;
328 }
329
330 bool HYDROData_PolylineXY::ImportShape( const TopoDS_Shape& theShape )
331 {
332   if ( theShape.IsNull() )
333     return false;
334
335   RemoveSections();
336
337   bool anIsCanBeImported = false;
338
339   NCollection_Sequence<TCollection_AsciiString> aSectNames;
340   NCollection_Sequence<SectionType>             aSectTypes;
341   NCollection_Sequence<bool>                    aSectClosures;
342   NCollection_Sequence<PointsList>              aSectPoints;
343
344   if ( theShape.ShapeType() == TopAbs_EDGE )
345   {
346     TopoDS_Edge anEdge = TopoDS::Edge( theShape );
347     anIsCanBeImported = convertEdgeToSection( anEdge, aSectNames, aSectTypes, aSectClosures, aSectPoints, true );
348   }
349   else if ( theShape.ShapeType() == TopAbs_WIRE )
350   {
351     TopTools_SequenceOfShape anEdges;
352     HYDROData_ShapesTool::ExploreShapeToShapes( theShape, TopAbs_EDGE, anEdges );
353
354     anIsCanBeImported = !anEdges.IsEmpty();
355     for ( int i = 1, n = anEdges.Length(); i <= n && anIsCanBeImported; ++i )
356     {
357       TopoDS_Edge aWireEdge = TopoDS::Edge( anEdges.Value( i ) );
358       anIsCanBeImported = convertEdgeToSection( aWireEdge, aSectNames, aSectTypes, aSectClosures, aSectPoints, false );
359     }
360   }
361
362   if ( anIsCanBeImported )
363   {
364     for ( int i = 1, n = aSectNames.Length(); i <= n; ++i )
365     {
366       const TCollection_AsciiString& aSectName = aSectNames.Value( i );
367       const SectionType& aSectType = aSectTypes.Value( i );
368       bool anIsSectionClosed = aSectClosures.Value( i );
369       const PointsList& aSectPointsList = aSectPoints( i );
370
371       AddSection( aSectName, aSectType, anIsSectionClosed );
372       SetPoints( i - 1, aSectPointsList );
373     }
374   }
375   else
376   {
377     TopoDS_Shape aShape = theShape;
378
379     if ( theShape.ShapeType() == TopAbs_EDGE )
380     {
381       // We make the wire from incoming edge because of other algorithms
382       // are waiting at least the wire from polyline
383       TopoDS_Edge anEdge = TopoDS::Edge( theShape );
384       BRepBuilderAPI_MakeWire aMakeWire( anEdge );
385       aMakeWire.Build();
386       if ( aMakeWire.IsDone() )
387         aShape = aMakeWire.Wire();
388     }
389
390     gp_Pln aPlane( gp_Pnt( 0, 0, 0 ), gp_Dir( 0, 0, 1 ) );
391     BRepBuilderAPI_MakeFace aMakeFace( aPlane );
392     aMakeFace.Build();
393     BRepOffsetAPI_NormalProjection aProj( aMakeFace.Face() );
394     aProj.Add( aShape );
395     aProj.Build();
396     TopoDS_Shape aResult;
397     if( aProj.IsDone() )
398       aResult = aProj.Shape();
399
400     setPolylineShape( aResult );
401   }
402
403   setEditable( anIsCanBeImported );
404
405   return true;
406 }
407
408 TopoDS_Wire HYDROData_PolylineXY::BuildWire( const SectionType&                  theType,
409                                              const bool&                         theIsClosed,
410                                              const NCollection_Sequence<gp_XYZ>& thePoints )
411 {
412   TopoDS_Wire aWire;
413   if( theType == SECTION_POLYLINE )
414   {
415     int aNbPoints = thePoints.Length();
416     BRepBuilderAPI_MakePolygon aMakeWire;
417     for ( int i = 1, n = aNbPoints; i <= n ; ++i )
418     {
419       gp_XYZ aPoint = thePoints.Value( i );
420       gp_Pnt aPnt( aPoint.X(), aPoint.Y(), aPoint.Z() );
421       aMakeWire.Add( aPnt );
422     }
423     if( theIsClosed && ( aNbPoints > 2 ) )
424       aMakeWire.Close();
425
426     if ( aMakeWire.IsDone() )
427       aWire = aMakeWire.Wire();
428   }
429   else //if( theType == PolylineSection::SECTION_SPLINE )
430   {
431     BRepBuilderAPI_MakeWire aMakeWire;
432
433     if ( thePoints.Size() > 1 )
434     {
435       HYDROData_BSplineOperation aBSpline( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
436
437       TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSpline.Curve() ).Edge();
438       aMakeWire.Add( anEdge );
439     }
440     aMakeWire.Build();
441     if ( aMakeWire.IsDone() )
442       aWire = aMakeWire;
443   }
444
445   return aWire;
446 }
447
448 void HYDROData_PolylineXY::BuildPainterPath( QPainterPath&                       thePath,
449                                              const SectionType&                  theType,
450                                              const bool&                         theIsClosed,
451                                              const NCollection_Sequence<gp_XYZ>& thePoints )
452 {
453   if ( thePoints.IsEmpty() )
454     return;
455
456   if ( theType == SECTION_POLYLINE )
457   {
458     const gp_XYZ& aFirstPoint = thePoints.Value( 1 );
459     thePath.moveTo( aFirstPoint.X(), aFirstPoint.Y() );
460
461     for( int i = 2, n = thePoints.Size(); i <= n; ++i )
462     {
463       const gp_XYZ& aSectPoint = thePoints.Value( i );
464
465       thePath.lineTo( aSectPoint.X(), aSectPoint.Y() );
466     }
467
468     if( theIsClosed )
469       thePath.closeSubpath();
470   }
471   else
472   {
473     HYDROData_BSplineOperation aBSpline( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
474     aBSpline.ComputePath( thePath );
475   }
476 }
477
478 void HYDROData_PolylineXY::Update()
479 {
480   if ( !IsEditable() )
481   {
482     // If polyline is not editable we no need to update it wire
483     SetToUpdate( false );
484     return;
485   }
486
487   HYDROData_IPolyline::Update();
488
489   NCollection_Sequence<TCollection_AsciiString>           aSectNames;
490   NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
491   NCollection_Sequence<bool>                              aSectClosures;
492   GetSections( aSectNames, aSectTypes, aSectClosures );
493
494   BRepBuilderAPI_MakeWire aMakeWire;
495
496   TopTools_ListOfShape aSectionWiresList;
497
498   for ( int aSectionId = 1, aNbSects = aSectNames.Size(); aSectionId <= aNbSects; aSectionId++ )
499   {
500     TCollection_AsciiString aSectName = aSectNames.Value( aSectionId );
501     SectionType aSectionType = aSectTypes.Value( aSectionId );
502     bool anIsSectionClosed = aSectClosures.Value( aSectionId );
503
504     PointsList aSectPointsList = GetPoints( aSectionId - 1 );
505     if ( aSectPointsList.IsEmpty() )
506       continue;
507     
508     NCollection_Sequence<gp_XYZ> aPoints;
509     for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
510     {
511       const Point& aSectPoint = aSectPointsList.Value( i );
512
513       gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
514       aPoints.Append( aPoint );
515     }
516
517     TopoDS_Wire aSectionWire = BuildWire( aSectionType, anIsSectionClosed, aPoints );
518     if ( !aSectionWire.IsNull() ) {
519       aSectionWiresList.Append( aSectionWire );
520       aMakeWire.Add( aSectionWire );
521     }
522   }
523 // all input wires in the <aSectionWiresList>
524   Handle(TopTools_HSequenceOfShape) aSeqWires = new TopTools_HSequenceOfShape;
525   Handle(TopTools_HSequenceOfShape) aSeqEdges = new TopTools_HSequenceOfShape;
526   TopTools_ListIteratorOfListOfShape it(aSectionWiresList);
527   for(;it.More();it.Next())
528   {
529     TopExp_Explorer it2(it.Value(), TopAbs_EDGE);
530     for(;it2.More();it2.Next()) 
531       aSeqEdges->Append(it2.Current());
532   }
533
534   BRep_Builder aBB;
535   TopoDS_Compound aCmp;
536   TopoDS_Shape aResult;
537   aBB.MakeCompound(aCmp);
538   if(aSeqEdges->Length() >1)
539   {
540     ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdges,1E-5,Standard_False,aSeqWires);
541
542     if( aSeqWires->Length()==1 )
543       aResult = aSeqWires->Value( 1 );
544     else
545     {
546       for (Standard_Integer i = 1; i <= aSeqWires->Length();i++)
547       {
548         const TopoDS_Shape& aS1 = aSeqWires->Value(i);
549         aBB.Add(aCmp, aS1);
550       }
551       aResult = aCmp;
552     }
553   }
554   else if (aSeqEdges->Length() == 1)
555   {
556     BRepBuilderAPI_MakeWire mkWire (TopoDS::Edge(aSeqEdges->Value(1)));
557     if (mkWire.IsDone())
558       aResult = mkWire.Wire();
559   }
560
561   setPolylineShape( aResult );
562 }
563
564 bool HYDROData_PolylineXY::IsHas2dPrs() const
565 {
566   return true;
567 }
568
569 bool HYDROData_PolylineXY::IsEditable() const
570 {
571   return !myLab.IsAttribute( GUID_IS_UNEDITABLE );
572 }
573
574 void HYDROData_PolylineXY::setEditable( const bool theIsEditable )
575 {
576   if ( !theIsEditable )
577     TDataStd_UAttribute::Set( myLab, GUID_IS_UNEDITABLE );
578   else
579     myLab.ForgetAttribute( GUID_IS_UNEDITABLE );
580 }
581
582 /**
583  * Returns true if polyline is closed
584  */
585 bool HYDROData_PolylineXY::IsClosed( const bool theIsSimpleCheck ) const
586 {
587   bool anIsClosed = false;
588
589   TopoDS_Shape aShape = GetShape();
590   if ( aShape.IsNull() )
591     return anIsClosed;
592
593   TopTools_SequenceOfShape aWires;
594   HYDROData_ShapesTool::ExploreShapeToShapes( aShape, TopAbs_WIRE, aWires );
595
596   int aNbWires = aWires.Length();
597   if ( theIsSimpleCheck )
598   {
599     anIsClosed = aNbWires > 0;
600     for ( int i = 1; i <= aNbWires && anIsClosed; ++i )
601     {
602       const TopoDS_Shape& aWire = aWires.Value( i );
603       anIsClosed = BRep_Tool::IsClosed( aWire );
604     }
605   }
606   else
607   {
608     anIsClosed = aNbWires == 1 && BRep_Tool::IsClosed( aWires.First() );
609   }
610
611   return anIsClosed;
612 }
613
614 double HYDROData_PolylineXY::GetDistance( const int theSectionIndex,
615                                           const int thePointIndex ) const
616 {
617   double aResDistance = -1;
618   if ( theSectionIndex < 0 || theSectionIndex >= NbSections() )
619     return aResDistance;
620
621   if ( thePointIndex == 0 )
622     return 0.0;
623
624   SectionType aSectionType = GetSectionType( theSectionIndex );
625   bool anIsSectionClosed = IsClosedSection( theSectionIndex );
626   PointsList aSectPointsList = GetPoints( theSectionIndex );
627   if ( thePointIndex < 0 || thePointIndex >= aSectPointsList.Size()  )
628     return aResDistance;
629
630   if ( aSectionType == SECTION_POLYLINE )
631   {
632     aResDistance = 0.0;
633   
634     Point aPrevPoint = aSectPointsList.Value( 1 );
635     for ( int i = 2, aNbPoints = aSectPointsList.Size(); i <= aNbPoints; ++i )
636     {
637       const Point& aSectPoint = aSectPointsList.Value( i );
638       aResDistance += gp_Pnt2d( aPrevPoint ).Distance( aSectPoint );
639       aPrevPoint = aSectPoint;
640
641       if ( thePointIndex == i - 1 )
642         break;
643     }
644   }
645   else
646   {
647     gp_XYZ aPointToTest;
648
649     int aSectNbPoints = aSectPointsList.Size();
650     NCollection_Sequence<gp_XYZ> aPoints;
651     for( int i = 1 ; i <= aSectNbPoints; ++i )
652     {
653       const Point& aSectPoint = aSectPointsList.Value( i );
654
655       gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
656       aPoints.Append( aPoint );
657
658       if ( thePointIndex == i - 1 )
659         aPointToTest = aPoint;
660     }
661
662     HYDROData_BSplineOperation aBSpline( aPoints, anIsSectionClosed, LOCAL_SELECTION_TOLERANCE );
663
664     Quantity_Parameter aFirstParam = aBSpline.Curve()->FirstParameter();
665     Quantity_Parameter aSecondParam = aBSpline.Curve()->LastParameter();
666
667     if ( thePointIndex != aSectNbPoints - 1 )
668     {
669       GeomAPI_ProjectPointOnCurve aProject( aPointToTest, aBSpline.Curve() );
670       aSecondParam = aProject.LowerDistanceParameter();
671     }
672
673     GeomAdaptor_Curve anAdap( aBSpline.Curve() );
674     
675     aResDistance = GCPnts_AbscissaPoint::Length( anAdap, aFirstParam, aSecondParam );
676   }
677
678   return aResDistance;
679 }
680
681 int HYDROData_PolylineXY::NbSections() const
682 {
683   Handle(TDataStd_ExtStringList) aNamesList;
684   Handle(TDataStd_IntegerList)   aTypesList;
685   Handle(TDataStd_BooleanList)   aClosuresList;
686   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
687
688   return !aClosuresList.IsNull() ? aClosuresList->Extent() : 0;
689 }
690
691 void HYDROData_PolylineXY::AddSection( const TCollection_AsciiString& theSectName,
692                                        const SectionType              theSectionType,
693                                        const bool                     theIsClosed )
694 {
695   Handle(TDataStd_ExtStringList) aNamesList;
696   Handle(TDataStd_IntegerList)   aTypesList;
697   Handle(TDataStd_BooleanList)   aClosuresList;
698   getSectionsLists( aNamesList, aTypesList, aClosuresList );
699
700   TCollection_ExtendedString aSectName( theSectName );
701   if ( aSectName.Length() <= 0 )
702     aSectName = getUniqueSectionName( aNamesList );
703
704   aNamesList->Append( aSectName );
705   aTypesList->Append( theSectionType );
706   aClosuresList->Append( theIsClosed );
707
708   SetToUpdate( true );
709 }
710
711 TCollection_AsciiString HYDROData_PolylineXY::GetSectionName( const int theSectionIndex ) const
712 {
713   TCollection_AsciiString aResName;
714
715   Handle(TDataStd_ExtStringList) aNamesList;
716   Handle(TDataStd_IntegerList) aTypesList;
717   Handle(TDataStd_BooleanList) aClosuresList;
718   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
719   if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
720     return aResName;
721
722   TDataStd_ListIteratorOfListOfExtendedString aNamesIter( aNamesList->List() );
723   for ( int i = 0; aNamesIter.More() && i != theSectionIndex; aNamesIter.Next(), ++i );
724
725   if ( aNamesIter.More() )
726     aResName = aNamesIter.Value();
727
728   return aResName;
729 }
730
731 void HYDROData_PolylineXY::SetSectionName( const int                      theSectionIndex, 
732                                            const TCollection_AsciiString& theSectionName )
733 {
734   Handle(TDataStd_ExtStringList) aNamesList;
735   Handle(TDataStd_IntegerList) aTypesList;
736   Handle(TDataStd_BooleanList) aClosuresList;
737   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
738   if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
739     return;
740
741   TDataStd_ListOfExtendedString anOldNamesList;
742   anOldNamesList = aNamesList->List();
743
744   // Refill the existing list
745   aNamesList->Clear();
746
747   TCollection_ExtendedString aNewSectName = theSectionName;
748
749   TDataStd_ListIteratorOfListOfExtendedString aNamesIter( anOldNamesList );
750   for ( int i = 0; aNamesIter.More(); aNamesIter.Next(), ++i )
751     aNamesList->Append( i == theSectionIndex ? aNewSectName : aNamesIter.Value() );
752
753   SetToUpdate( true );
754 }
755
756 HYDROData_PolylineXY::SectionType HYDROData_PolylineXY::GetSectionType( const int theSectionIndex ) const
757 {
758   Handle(TDataStd_ExtStringList) aNamesList;
759   Handle(TDataStd_IntegerList) aTypesList;
760   Handle(TDataStd_BooleanList) aClosuresList;
761   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
762   if ( aTypesList.IsNull() || theSectionIndex >= aTypesList->Extent() )
763     return SECTION_POLYLINE;
764
765   TColStd_ListIteratorOfListOfInteger aTypesIter( aTypesList->List() );
766   for ( int i = 0; aTypesIter.More() && i != theSectionIndex; aTypesIter.Next(), ++i );
767
768   return aTypesIter.More() ? (SectionType)aTypesIter.Value() : SECTION_POLYLINE;
769 }
770
771 void HYDROData_PolylineXY::SetSectionType( const int         theSectionIndex, 
772                                            const SectionType theSectionType )
773 {
774   Handle(TDataStd_ExtStringList) aNamesList;
775   Handle(TDataStd_IntegerList) aTypesList;
776   Handle(TDataStd_BooleanList) aClosuresList;
777   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
778   if ( aTypesList.IsNull() || theSectionIndex >= aTypesList->Extent() )
779     return;
780
781   TColStd_ListOfInteger anOldTypesList;
782   anOldTypesList = aTypesList->List();
783
784   // Refill the existing list
785   aTypesList->Clear();
786
787   TColStd_ListIteratorOfListOfInteger aTypesIter( anOldTypesList );
788   for ( int i = 0; aTypesIter.More(); aTypesIter.Next(), ++i )
789     aTypesList->Append( i == theSectionIndex ? theSectionType : aTypesIter.Value() );
790
791   SetToUpdate( true );
792 }
793
794 bool HYDROData_PolylineXY::IsClosedSection( const int theSectionIndex ) const
795 {
796   Handle(TDataStd_ExtStringList) aNamesList;
797   Handle(TDataStd_IntegerList) aTypesList;
798   Handle(TDataStd_BooleanList) aClosuresList;
799   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
800   if ( aClosuresList.IsNull() || theSectionIndex >= aClosuresList->Extent() )
801     return false;
802
803   TDataStd_ListIteratorOfListOfByte aClosuresIter( aClosuresList->List() );
804   for ( int i = 0; aClosuresIter.More() && i != theSectionIndex; aClosuresIter.Next(), ++i );
805
806   return aClosuresIter.More() ? (bool)aClosuresIter.Value() : false;
807 }
808
809 void HYDROData_PolylineXY::SetSectionClosed( const int  theSectionIndex, 
810                                              const bool theIsClosed )
811 {
812   Handle(TDataStd_ExtStringList) aNamesList;
813   Handle(TDataStd_IntegerList) aTypesList;
814   Handle(TDataStd_BooleanList) aClosuresList;
815   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
816   if ( aClosuresList.IsNull() || theSectionIndex >= aClosuresList->Extent() )
817     return;
818
819   TDataStd_ListOfByte anOldClosuresList;
820   anOldClosuresList = aClosuresList->List();
821
822   // Refill the existing list
823   aClosuresList->Clear();
824
825   TDataStd_ListIteratorOfListOfByte aClosuresIter( anOldClosuresList );
826   for ( int i = 0; aClosuresIter.More(); aClosuresIter.Next(), ++i )
827     aClosuresList->Append( i == theSectionIndex ? theIsClosed : (bool)aClosuresIter.Value() );
828
829   SetToUpdate( true );
830 }
831
832 void HYDROData_PolylineXY::GetSections( NCollection_Sequence<TCollection_AsciiString>& theSectNames,
833                                         NCollection_Sequence<SectionType>&             theSectTypes,
834                                         NCollection_Sequence<bool>&                    theSectClosures ) const
835 {
836   theSectNames.Clear();
837   theSectTypes.Clear();
838   theSectClosures.Clear();
839
840   Handle(TDataStd_ExtStringList) aNamesList;
841   Handle(TDataStd_IntegerList) aTypesList;
842   Handle(TDataStd_BooleanList) aClosuresList;
843   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
844   if ( aNamesList.IsNull() || aTypesList.IsNull() || aClosuresList.IsNull() )
845     return;
846
847   TDataStd_ListIteratorOfListOfExtendedString aNamesIter( aNamesList->List() );
848   TColStd_ListIteratorOfListOfInteger aTypesIter( aTypesList->List() );
849   TDataStd_ListIteratorOfListOfByte aClosuresIter( aClosuresList->List() );
850   for ( ; aNamesIter.More() && aTypesIter.More() && aClosuresIter.More();
851           aNamesIter.Next(), aTypesIter.Next(), aClosuresIter.Next() )
852   {
853     const TCollection_ExtendedString& aSectName = aNamesIter.Value();
854     SectionType aSectType = (SectionType)aTypesIter.Value();
855     bool aSectClosures = aClosuresIter.Value();
856
857     theSectNames.Append( aSectName );
858     theSectTypes.Append( aSectType );
859     theSectClosures.Append( aSectClosures );
860   }
861 }
862
863 void HYDROData_PolylineXY::RemoveSection( const int theSectionIndex )
864 {
865   Handle(TDataStd_ExtStringList) aNamesList;
866   Handle(TDataStd_IntegerList)   aTypesList;
867   Handle(TDataStd_BooleanList)   aClosuresList;
868   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
869   if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
870     return;
871
872   if ( aNamesList->Extent() == 1 )
873   {
874     removeSectionsLists();
875     removePointsLists();
876   }
877   else
878   {
879     TDataStd_ListOfExtendedString anOldNamesList;
880     anOldNamesList = aNamesList->List();
881
882     TColStd_ListOfInteger anOldTypesList;
883     anOldTypesList = aTypesList->List();
884
885     TDataStd_ListOfByte anOldClosuresList;
886     anOldClosuresList = aClosuresList->List();
887
888     // Refill the existing lists
889     aNamesList->Clear();
890     aTypesList->Clear();
891     aClosuresList->Clear();
892
893     TDataStd_ListIteratorOfListOfExtendedString aNamesIter( anOldNamesList );
894     TColStd_ListIteratorOfListOfInteger aTypesIter( anOldTypesList );
895     TDataStd_ListIteratorOfListOfByte aClosuresIter( anOldClosuresList );
896     for ( int i = 0; aNamesIter.More() && aTypesIter.More() && aClosuresIter.More();
897                      aNamesIter.Next(), aTypesIter.Next(), aClosuresIter.Next(), ++i )
898     {
899       if ( i == theSectionIndex )
900         continue; // skip index to remove
901
902       aNamesList->Append( aNamesIter.Value() );
903       aTypesList->Append( aTypesIter.Value() );
904       aClosuresList->Append( (bool)aClosuresIter.Value() );
905     }
906
907     // Remove points that belongs to removed section
908     removePointsLists( theSectionIndex );
909   }
910
911   SetToUpdate( true );
912 }
913
914 void HYDROData_PolylineXY::RemoveSections()
915 {
916   removeSectionsLists();
917   removePointsLists();
918   SetToUpdate( true );
919 }
920
921 void HYDROData_PolylineXY::AddPoint( const int    theSectionIndex,
922                                      const Point& thePoint,
923                                      const int    thePointIndex )
924 {
925   Handle(TDataStd_RealList) aListX, aListY;
926   getPointsLists( theSectionIndex, aListX, aListY );
927
928   if ( thePointIndex < 0 || thePointIndex >= aListX->Extent() )
929   {
930     aListX->Append( thePoint.X() );
931     aListY->Append( thePoint.Y() );
932   }
933   else
934   {
935     TColStd_ListOfReal anOldListX;
936     anOldListX = aListX->List();
937
938     TColStd_ListOfReal anOldListY;
939     anOldListY = aListY->List();
940
941     // Refill the existing lists
942     aListX->Clear();
943     aListY->Clear();
944
945     TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
946     TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
947     for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
948     {
949       double aCoordX = anIterX.Value();
950       double aCoordY = anIterY.Value();
951
952       if ( i == thePointIndex )
953       {
954         // Insert our new point
955         aListX->Append( thePoint.X() );
956         aListY->Append( thePoint.Y() );
957       }
958
959       aListX->Append( aCoordX );
960       aListY->Append( aCoordY );
961     }
962   }
963
964   SetToUpdate( true );
965 }
966
967 void HYDROData_PolylineXY::SetPoint( const int    theSectionIndex,
968                                      const Point& thePoint,
969                                      const int    thePointIndex )
970 {
971   Handle(TDataStd_RealList) aListX, aListY;
972   getPointsLists( theSectionIndex, aListX, aListY );
973
974   if ( thePointIndex < 0 )
975   {
976     aListX->Prepend( thePoint.X() );
977     aListY->Prepend( thePoint.Y() );
978   }
979   else if ( thePointIndex >= aListX->Extent() )
980   {
981     aListX->Append( thePoint.X() );
982     aListY->Append( thePoint.Y() );
983   }
984   else
985   {
986     TColStd_ListOfReal anOldListX;
987     anOldListX = aListX->List();
988
989     TColStd_ListOfReal anOldListY;
990     anOldListY = aListY->List();
991
992     // Refill the existing lists
993     aListX->Clear();
994     aListY->Clear();
995
996     TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
997     TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
998     for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
999     {
1000       double aCoordX = anIterX.Value();
1001       double aCoordY = anIterY.Value();
1002
1003       if ( i == thePointIndex )
1004       {
1005         // Insert our new point instead of old one
1006         aCoordX = thePoint.X();
1007         aCoordY = thePoint.Y();
1008       }
1009
1010       aListX->Append( aCoordX );
1011       aListY->Append( aCoordY );
1012     }
1013   }
1014
1015   SetToUpdate( true );
1016 }
1017
1018 void HYDROData_PolylineXY::SetPoints( const int         theSectionIndex,
1019                                       const PointsList& thePoints )
1020 {
1021   Handle(TDataStd_RealList) aListX, aListY;
1022   getPointsLists( theSectionIndex, aListX, aListY );
1023
1024   aListX->Clear();
1025   aListY->Clear();
1026
1027   for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
1028   {
1029     const Point& aPoint = thePoints.Value( i );
1030     aListX->Append( aPoint.X() );
1031     aListY->Append( aPoint.Y() );
1032   }
1033 }
1034
1035 void HYDROData_PolylineXY::RemovePoint( const int theSectionIndex,
1036                                         const int thePointIndex )
1037 {
1038   Handle(TDataStd_RealList) aListX, aListY;
1039   getPointsLists( theSectionIndex, aListX, aListY, false );
1040   if ( aListX.IsNull() || aListY.IsNull() || aListX->IsEmpty() )
1041     return;
1042
1043   if ( aListX->Extent() == 1 )
1044   {
1045     removePointsLists( theSectionIndex );
1046   }
1047   else
1048   {
1049     TColStd_ListOfReal anOldListX;
1050     anOldListX = aListX->List();
1051
1052     TColStd_ListOfReal anOldListY;
1053     anOldListY = aListY->List();
1054
1055     // Refill the existing lists
1056     aListX->Clear();
1057     aListY->Clear();
1058
1059     TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
1060     TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
1061     for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
1062     {
1063       if ( i == thePointIndex )
1064         continue; // skip index to remove
1065
1066       aListX->Append( anIterX.Value() );
1067       aListY->Append( anIterY.Value() );
1068     }
1069   }
1070
1071   SetToUpdate( true );
1072 }
1073
1074 HYDROData_PolylineXY::PointsList HYDROData_PolylineXY::GetPoints( const int theSectionIndex ) const
1075 {
1076   PointsList aResList;
1077
1078   Handle(TDataStd_RealList) aListX, aListY;
1079   getPointsLists( theSectionIndex, aListX, aListY, false );
1080   if ( aListX.IsNull() || aListY.IsNull() || aListX->IsEmpty() )
1081     return aResList;
1082
1083   TColStd_ListIteratorOfListOfReal anIterX( aListX->List() );
1084   TColStd_ListIteratorOfListOfReal anIterY( aListY->List() );
1085   for ( ; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next() )
1086   {
1087     Point aPoint( anIterX.Value(), anIterY.Value() );
1088     aResList.Append( aPoint );
1089   }
1090
1091   return aResList;
1092 }
1093
1094 QPainterPath HYDROData_PolylineXY::GetPainterPath() const
1095 {
1096   QPainterPath aPath;
1097
1098   NCollection_Sequence<TCollection_AsciiString>           aSectNames;
1099   NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1100   NCollection_Sequence<bool>                              aSectClosures;
1101   GetSections( aSectNames, aSectTypes, aSectClosures );
1102
1103   for ( int aSectionId = 1, aNbSects = aSectNames.Size(); aSectionId <= aNbSects; aSectionId++ )
1104   {
1105     TCollection_AsciiString aSectName = aSectNames.Value( aSectionId );
1106     SectionType aSectionType = aSectTypes.Value( aSectionId );
1107     bool anIsSectionClosed = aSectClosures.Value( aSectionId );
1108
1109     PointsList aSectPointsList = GetPoints( aSectionId - 1 );
1110     if ( aSectPointsList.IsEmpty() )
1111       continue;
1112
1113     NCollection_Sequence<gp_XYZ> aPoints;
1114     for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
1115     {
1116       const Point& aSectPoint = aSectPointsList.Value( i );
1117
1118       gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
1119       aPoints.Append( aPoint );
1120     }
1121
1122     BuildPainterPath( aPath, aSectionType, anIsSectionClosed, aPoints );
1123   }
1124
1125   return aPath;
1126 }
1127
1128 void HYDROData_PolylineXY::UpdateLocalCS( double theDx, double theDy )
1129 {
1130   NCollection_Sequence<TCollection_AsciiString>           aSectNames;
1131   NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1132   NCollection_Sequence<bool>                              aSectClosures;
1133   GetSections( aSectNames, aSectTypes, aSectClosures );
1134
1135   gp_XY aDelta( theDx, theDy );
1136   for ( int i = 0, aNbSects = aSectNames.Size(); i < aNbSects; i++ )
1137   {
1138     PointsList aPoints = GetPoints( i );
1139     for( int j = 1, n = aPoints.Size(); j <= n; ++j )
1140     {
1141       Point& aPoint = aPoints.ChangeValue( j );
1142       aPoint += aDelta;
1143     }
1144     SetPoints( i, aPoints );
1145   }
1146   SetToUpdate( true );
1147 }
1148
1149
1150