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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROData_PolylineXY.h"
21 #include "HYDROData_BSplineOperation.h"
22 #include "HYDROData_Document.h"
23 #include "HYDROData_ShapesTool.h"
24 #include "HYDROData_Tool.h"
26 #include <BRep_Builder.hxx>
27 #include <BRepBuilderAPI_MakeEdge.hxx>
28 #include <BRepBuilderAPI_MakeWire.hxx>
29 #include <BRepBuilderAPI_MakePolygon.hxx>
30 #include <BRepBuilderAPI_MakeFace.hxx>
31 #include <BRepOffsetAPI_NormalProjection.hxx>
32 #include <BRepAdaptor_Curve.hxx>
38 #include <GeomAPI_ProjectPointOnCurve.hxx>
39 #include <GeomAdaptor_Curve.hxx>
40 #include <Geom_Line.hxx>
41 #include <Geom_BSplineCurve.hxx>
43 #include <GCPnts_AbscissaPoint.hxx>
44 #include <GCPnts_QuasiUniformDeflection.hxx>
46 #include <ImageComposer_MetaTypes.h>
52 #include <NCollection_Map.hxx>
54 #include <TCollection_ExtendedString.hxx>
56 #include <TDataStd_ListIteratorOfListOfByte.hxx>
57 #include <TColStd_ListIteratorOfListOfInteger.hxx>
58 #include <TColStd_ListIteratorOfListOfReal.hxx>
60 #include <TColStd_Array1OfReal.hxx>
61 #include <TColgp_Array1OfPnt.hxx>
63 #include <TDataStd_AsciiString.hxx>
64 #include <TDataStd_BooleanList.hxx>
65 #include <TDataStd_ExtStringList.hxx>
66 #include <TDataStd_IntegerList.hxx>
67 #include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
68 #include <TDataStd_RealList.hxx>
69 #include <TDataStd_UAttribute.hxx>
71 #include <TopoDS_Iterator.hxx>
72 #include <TopTools_ListIteratorOfListOfShape.hxx>
73 #include <TopTools_HSequenceOfShape.hxx>
74 #include <TopExp_Explorer.hxx>
75 #include <ShapeAnalysis_FreeBounds.hxx>
79 #include <QPainterPath>
82 static const Standard_GUID GUID_IS_UNEDITABLE("e5799736-9030-4051-91a4-2e58321fa153");
84 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
86 TCollection_AsciiString getUniqueSectionName( const NCollection_Sequence<TCollection_AsciiString>& theNamesSeq )
88 NCollection_Map<TCollection_AsciiString> aNamesMap;
90 for ( int i = 1, n = theNamesSeq.Size(); i <= n; ++i )
92 const TCollection_AsciiString& aSectName = theNamesSeq.Value( i );
93 aNamesMap.Add( aSectName );
96 TCollection_AsciiString aResName;
101 aResName = TCollection_AsciiString( "Section_" ) + aPrefIdx;
104 while ( aNamesMap.Contains( aResName ) );
109 TCollection_AsciiString getUniqueSectionName( const Handle(TDataStd_ExtStringList)& theNamesList )
111 NCollection_Sequence<TCollection_AsciiString> aNamesSeq;
113 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( theNamesList->List() );
114 for ( ; aNamesIter.More(); aNamesIter.Next() )
115 aNamesSeq.Append( aNamesIter.Value() );
117 return getUniqueSectionName( aNamesSeq );
120 IMPLEMENT_STANDARD_HANDLE(HYDROData_PolylineXY, HYDROData_IPolyline)
121 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_PolylineXY, HYDROData_IPolyline)
123 HYDROData_PolylineXY::HYDROData_PolylineXY()
124 : HYDROData_IPolyline(),
125 myIsInCustomFlag( false )
129 HYDROData_PolylineXY::~HYDROData_PolylineXY()
133 QStringList HYDROData_PolylineXY::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
135 QStringList aResList = dumpObjectCreation( theTreatedObjects );
136 QString aPolylineName = GetObjPyName();
138 // Set the wire color
139 QStringList aWireColorDef;
141 QColor aWireColor = GetWireColor();
142 setPythonObjectColor( aWireColorDef, aWireColor, DefaultWireColor(), "SetWireColor" );
144 if ( !aWireColorDef.isEmpty() )
146 aResList << aWireColorDef;
147 aResList << QString( "" );
150 bool anIsEditable = IsEditable();
153 // If polyline is not editable we try to import the shape from geom
154 TCollection_AsciiString aGeomObjectEntry = GetGeomObjectEntry();
155 if ( !aGeomObjectEntry.IsEmpty() )
157 QString aSalomeObjName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "polyline_sobj" );
158 aResList << QString( "%1 = salome.myStudy.FindObjectID( \"%2\" );" )
159 .arg( aSalomeObjName ).arg( aGeomObjectEntry.ToCString() );
161 aResList << QString( "%1.ImportFromGeomIOR( %2.GetIOR() );" )
162 .arg( aPolylineName ).arg( aSalomeObjName );
164 aResList << QString( "%1.SetGeomObjectEntry( \"%2\" );" )
165 .arg( aPolylineName ).arg( aGeomObjectEntry.ToCString() );
171 NCollection_Sequence<TCollection_AsciiString> aSectNames;
172 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
173 NCollection_Sequence<bool> aSectClosures;
174 GetSections( aSectNames, aSectTypes, aSectClosures );
176 for ( int i = 1, n = aSectNames.Size(); i <= n; ++i )
178 const TCollection_AsciiString& aSectName = aSectNames.Value( i );
179 const SectionType& aSectType = aSectTypes.Value( i );
180 bool aSectClosure = aSectClosures.Value( i );
182 aResList << QString( "%1.AddSection( \"%2\", %3, %4 );" ).arg( aPolylineName )
183 .arg( aSectName.ToCString() ).arg( aSectType ).arg( aSectClosure );
185 HYDROData_IPolyline::PointsList aSectPointsList = GetPoints( i - 1 );
186 for ( int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k )
188 const Point& aSectPoint = aSectPointsList.Value( k );
190 QString anXStr = QString::number( aSectPoint.X(), 'f', 2 );
191 QString anYStr = QString::number( aSectPoint.Y(), 'f', 2 );
192 aResList << QString( "%1.AddPoint( %2, gp_XY( %3, %4 ) );" ).arg( aPolylineName )
193 .arg( i - 1 ).arg( anXStr ).arg( anYStr );
197 aResList << QString( "" );
198 aResList << QString( "%1.Update();" ).arg( aPolylineName );
199 aResList << QString( "" );
204 QVariant HYDROData_PolylineXY::GetDataVariant()
206 QPainterPath aPath = GetPainterPath();
209 aVarData.setValue<QPainterPath>( aPath );
214 QColor HYDROData_PolylineXY::DefaultWireColor()
216 return QColor( Qt::red );
219 bool HYDROData_PolylineXY::ImportFromGeomIOR( const TCollection_AsciiString& theIOR )
224 if ( theIOR.IsEmpty() )
227 TopoDS_Shape aShape = GEOMBase::GetShapeFromIOR( theIOR.ToCString() );
228 if ( aShape.IsNull() )
231 return ImportShape( aShape );
235 void HYDROData_PolylineXY::SetGeomObjectEntry( const TCollection_AsciiString& theEntry )
237 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_GeomObjectEntry ), theEntry );
240 TCollection_AsciiString HYDROData_PolylineXY::GetGeomObjectEntry() const
242 TCollection_AsciiString aRes;
244 TDF_Label aLabel = myLab.FindChild( DataTag_GeomObjectEntry, false );
245 if ( !aLabel.IsNull() )
247 Handle(TDataStd_AsciiString) anAsciiStr;
248 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
249 aRes = anAsciiStr->Get();
255 bool convertEdgeToSection( const TopoDS_Edge& theEdge,
256 NCollection_Sequence<TCollection_AsciiString>& theSectNames,
257 NCollection_Sequence<HYDROData_PolylineXY::SectionType>& theSectTypes,
258 NCollection_Sequence<bool>& theSectClosures,
259 NCollection_Sequence<HYDROData_PolylineXY::PointsList>& theSectPoints,
261 bool IsInterpolationAllowed,
262 double theDeflection )
264 Standard_Real aFirst = 0.0, aLast = 0.0;
265 Handle(Geom_Curve) anEdgeGeomCurve = BRep_Tool::Curve( theEdge, aFirst, aLast );
266 if ( anEdgeGeomCurve.IsNull() )
269 TCollection_AsciiString aSectName = getUniqueSectionName( theSectNames );
270 bool anIsEdgeClosed = anEdgeGeomCurve->IsClosed();
272 HYDROData_PolylineXY::SectionType aSectionType = HYDROData_PolylineXY::SECTION_POLYLINE;
273 HYDROData_PolylineXY::PointsList aPointsList;
275 if( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_Line) ) )
277 Handle(Geom_Line) aGeomLine = Handle(Geom_Line)::DownCast( anEdgeGeomCurve );
279 gp_Pnt aFirstPoint, aLastPoint;
280 aGeomLine->D0( aFirst, aFirstPoint );
281 aGeomLine->D0( aLast, aLastPoint );
283 HYDROData_PolylineXY::Point aSectFirstPoint( aFirstPoint.X(), aFirstPoint.Y() );
284 aPointsList.Append( aSectFirstPoint );
286 HYDROData_PolylineXY::Point aSectLastPoint( aLastPoint.X(), aLastPoint.Y() );
287 aPointsList.Append( aSectLastPoint );
289 else if ( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) || IsInterpolationAllowed )
291 aSectionType = HYDROData_PolylineXY::SECTION_SPLINE;
293 BRepAdaptor_Curve anAdaptorCurve( theEdge );
294 GCPnts_QuasiUniformDeflection aDiscrete( anAdaptorCurve, theDeflection );
296 int aNbPoints = aDiscrete.NbPoints();
298 // Decrease the number of imported poles because of last one
299 // pole is the closing point which are the start point
300 if ( anIsEdgeClosed ) aNbPoints--;
302 for ( int i = 1; i <= aNbPoints; ++i )
304 const gp_Pnt& aPoint = aDiscrete.Value( i );
306 HYDROData_PolylineXY::Point aSectPoint( aPoint.X(), aPoint.Y() );
307 aPointsList.Append( aSectPoint );
312 // Other curve types are not supported
316 if ( aPointsList.IsEmpty() )
319 theSectNames.Append( aSectName );
320 theSectTypes.Append( aSectionType );
321 theSectClosures.Append( anIsEdgeClosed );
322 theSectPoints.Append( aPointsList );
327 bool HYDROData_PolylineXY::ImportShape( const TopoDS_Shape& theShape,
328 bool IsInterpolationAllowed,
329 double theDeviation )
331 if ( theShape.IsNull() )
336 bool anIsCanBeImported = false;
338 NCollection_Sequence<TCollection_AsciiString> aSectNames;
339 NCollection_Sequence<SectionType> aSectTypes;
340 NCollection_Sequence<bool> aSectClosures;
341 NCollection_Sequence<PointsList> aSectPoints;
343 if ( theShape.ShapeType() == TopAbs_EDGE )
345 TopoDS_Edge anEdge = TopoDS::Edge( theShape );
346 anIsCanBeImported = convertEdgeToSection( anEdge, aSectNames, aSectTypes,
347 aSectClosures, aSectPoints, true, IsInterpolationAllowed, theDeviation );
349 else if ( theShape.ShapeType() == TopAbs_WIRE )
351 TopTools_SequenceOfShape anEdges;
352 HYDROData_ShapesTool::ExploreShapeToShapes( theShape, TopAbs_EDGE, anEdges );
354 anIsCanBeImported = !anEdges.IsEmpty();
355 for ( int i = 1, n = anEdges.Length(); i <= n && anIsCanBeImported; ++i )
357 TopoDS_Edge aWireEdge = TopoDS::Edge( anEdges.Value( i ) );
358 anIsCanBeImported = convertEdgeToSection( aWireEdge, aSectNames, aSectTypes,
359 aSectClosures, aSectPoints, false, IsInterpolationAllowed, theDeviation );
363 if ( anIsCanBeImported )
365 for ( int i = 1, n = aSectNames.Length(); i <= n; ++i )
367 const TCollection_AsciiString& aSectName = aSectNames.Value( i );
368 const SectionType& aSectType = aSectTypes.Value( i );
369 bool anIsSectionClosed = aSectClosures.Value( i );
370 const PointsList& aSectPointsList = aSectPoints( i );
372 AddSection( aSectName, aSectType, anIsSectionClosed );
373 SetPoints( i - 1, aSectPointsList );
378 TopoDS_Shape aShape = theShape;
380 if ( theShape.ShapeType() == TopAbs_EDGE )
382 // We make the wire from incoming edge because of other algorithms
383 // are waiting at least the wire from polyline
384 TopoDS_Edge anEdge = TopoDS::Edge( theShape );
385 BRepBuilderAPI_MakeWire aMakeWire( anEdge );
387 if ( aMakeWire.IsDone() )
388 aShape = aMakeWire.Wire();
391 gp_Pln aPlane( gp_Pnt( 0, 0, 0 ), gp_Dir( 0, 0, 1 ) );
392 BRepBuilderAPI_MakeFace aMakeFace( aPlane );
394 BRepOffsetAPI_NormalProjection aProj( aMakeFace.Face() );
397 TopoDS_Shape aResult;
399 aResult = aProj.Shape();
404 setEditable( anIsCanBeImported );
409 TopoDS_Wire HYDROData_PolylineXY::BuildWire( const SectionType& theType,
410 const bool& theIsClosed,
411 const NCollection_Sequence<gp_XYZ>& thePoints )
414 if( theType == SECTION_POLYLINE )
416 int aNbPoints = thePoints.Length();
417 BRepBuilderAPI_MakePolygon aMakeWire;
418 for ( int i = 1, n = aNbPoints; i <= n ; ++i )
420 gp_XYZ aPoint = thePoints.Value( i );
421 gp_Pnt aPnt( aPoint.X(), aPoint.Y(), aPoint.Z() );
422 aMakeWire.Add( aPnt );
424 if( theIsClosed && ( aNbPoints > 2 ) )
427 if ( aMakeWire.IsDone() )
428 aWire = aMakeWire.Wire();
430 else //if( theType == PolylineSection::SECTION_SPLINE )
432 BRepBuilderAPI_MakeWire aMakeWire;
434 if ( thePoints.Size() > 1 )
436 Handle(Geom_BSplineCurve) aCurve =
437 HYDROData_BSplineOperation::ComputeCurve( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
439 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aCurve ).Edge();
440 aMakeWire.Add( anEdge );
443 if ( aMakeWire.IsDone() )
450 void HYDROData_PolylineXY::BuildPainterPath( QPainterPath& thePath,
451 const SectionType& theType,
452 const bool& theIsClosed,
453 const NCollection_Sequence<gp_XYZ>& thePoints )
455 if ( thePoints.IsEmpty() )
458 if ( theType == SECTION_POLYLINE )
460 const gp_XYZ& aFirstPoint = thePoints.Value( 1 );
461 thePath.moveTo( aFirstPoint.X(), aFirstPoint.Y() );
463 for( int i = 2, n = thePoints.Size(); i <= n; ++i )
465 const gp_XYZ& aSectPoint = thePoints.Value( i );
467 thePath.lineTo( aSectPoint.X(), aSectPoint.Y() );
471 thePath.closeSubpath();
475 Handle(Geom_BSplineCurve) aCurve =
476 HYDROData_BSplineOperation::ComputeCurve( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
477 HYDROData_BSplineOperation::ComputePath( aCurve, thePath );
481 void HYDROData_PolylineXY::Update()
485 // If polyline is not editable we no need to update it wire
490 HYDROData_IPolyline::Update();
492 NCollection_Sequence<TCollection_AsciiString> aSectNames;
493 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
494 NCollection_Sequence<bool> aSectClosures;
495 GetSections( aSectNames, aSectTypes, aSectClosures );
497 //BRepBuilderAPI_MakeWire aMakeWire;
499 TopTools_ListOfShape aSectionWiresList;
501 for ( int aSectionId = 1, aNbSects = aSectNames.Size(); aSectionId <= aNbSects; aSectionId++ )
503 TCollection_AsciiString aSectName = aSectNames.Value( aSectionId );
504 SectionType aSectionType = aSectTypes.Value( aSectionId );
505 bool anIsSectionClosed = aSectClosures.Value( aSectionId );
507 PointsList aSectPointsList = GetPoints( aSectionId - 1 );
508 if ( aSectPointsList.IsEmpty() )
511 NCollection_Sequence<gp_XYZ> aPoints;
512 for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
514 const Point& aSectPoint = aSectPointsList.Value( i );
516 gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
517 aPoints.Append( aPoint );
520 TopoDS_Wire aSectionWire = BuildWire( aSectionType, anIsSectionClosed, aPoints );
521 if ( !aSectionWire.IsNull() ) {
522 aSectionWiresList.Append( aSectionWire );
523 //aMakeWire.Add( aSectionWire );
526 // all input wires in the <aSectionWiresList>
527 Handle(TopTools_HSequenceOfShape) aSeqWires = new TopTools_HSequenceOfShape;
528 Handle(TopTools_HSequenceOfShape) aSeqEdges = new TopTools_HSequenceOfShape;
529 TopTools_ListIteratorOfListOfShape it(aSectionWiresList);
530 for(;it.More();it.Next())
532 TopExp_Explorer it2(it.Value(), TopAbs_EDGE);
533 for(;it2.More();it2.Next())
534 aSeqEdges->Append(it2.Current());
538 TopoDS_Compound aCmp;
539 TopoDS_Shape aResult;
540 aBB.MakeCompound(aCmp);
541 if(aSeqEdges->Length() >1)
543 ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdges,1E-5,Standard_False,aSeqWires);
545 if( aSeqWires->Length()==1 )
546 aResult = aSeqWires->Value( 1 );
549 for (Standard_Integer i = 1; i <= aSeqWires->Length();i++)
551 const TopoDS_Shape& aS1 = aSeqWires->Value(i);
557 else if (aSeqEdges->Length() == 1)
559 BRepBuilderAPI_MakeWire mkWire (TopoDS::Edge(aSeqEdges->Value(1)));
561 aResult = mkWire.Wire();
567 bool HYDROData_PolylineXY::IsHas2dPrs() const
572 bool HYDROData_PolylineXY::IsEditable() const
574 return !myLab.IsAttribute( GUID_IS_UNEDITABLE );
577 void HYDROData_PolylineXY::setEditable( const bool theIsEditable )
579 if ( !theIsEditable )
580 TDataStd_UAttribute::Set( myLab, GUID_IS_UNEDITABLE );
582 myLab.ForgetAttribute( GUID_IS_UNEDITABLE );
586 * Returns true if polyline is closed
588 bool HYDROData_PolylineXY::IsClosed( const bool theIsSimpleCheck ) const
590 bool anIsClosed = false;
592 TopoDS_Shape aShape = GetShape();
593 if ( aShape.IsNull() )
596 TopTools_SequenceOfShape aWires;
597 HYDROData_ShapesTool::ExploreShapeToShapes( aShape, TopAbs_WIRE, aWires );
599 int aNbWires = aWires.Length();
600 if ( theIsSimpleCheck )
602 anIsClosed = aNbWires > 0;
603 for ( int i = 1; i <= aNbWires && anIsClosed; ++i )
605 const TopoDS_Shape& aWire = aWires.Value( i );
606 anIsClosed = BRep_Tool::IsClosed( aWire );
611 anIsClosed = aNbWires == 1 && BRep_Tool::IsClosed( aWires.First() );
617 double HYDROData_PolylineXY::GetDistance( const int theSectionIndex,
618 const int thePointIndex ) const
620 double aResDistance = -1;
621 if ( theSectionIndex < 0 || theSectionIndex >= NbSections() )
624 if ( thePointIndex == 0 )
627 SectionType aSectionType = GetSectionType( theSectionIndex );
628 bool anIsSectionClosed = IsClosedSection( theSectionIndex );
629 PointsList aSectPointsList = GetPoints( theSectionIndex );
630 if ( thePointIndex < 0 || thePointIndex >= aSectPointsList.Size() )
633 if ( aSectionType == SECTION_POLYLINE )
637 Point aPrevPoint = aSectPointsList.Value( 1 );
638 for ( int i = 2, aNbPoints = aSectPointsList.Size(); i <= aNbPoints; ++i )
640 const Point& aSectPoint = aSectPointsList.Value( i );
641 aResDistance += gp_Pnt2d( aPrevPoint ).Distance( aSectPoint );
642 aPrevPoint = aSectPoint;
644 if ( thePointIndex == i - 1 )
652 int aSectNbPoints = aSectPointsList.Size();
653 NCollection_Sequence<gp_XYZ> aPoints;
654 for( int i = 1 ; i <= aSectNbPoints; ++i )
656 const Point& aSectPoint = aSectPointsList.Value( i );
658 gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
659 aPoints.Append( aPoint );
661 if ( thePointIndex == i - 1 )
662 aPointToTest = aPoint;
665 Handle(Geom_BSplineCurve) aCurve =
666 HYDROData_BSplineOperation::ComputeCurve( aPoints, anIsSectionClosed, LOCAL_SELECTION_TOLERANCE );
668 Quantity_Parameter aFirstParam = aCurve->FirstParameter();
669 Quantity_Parameter aSecondParam = aCurve->LastParameter();
671 if ( thePointIndex != aSectNbPoints - 1 )
673 GeomAPI_ProjectPointOnCurve aProject( aPointToTest, aCurve );
674 aSecondParam = aProject.LowerDistanceParameter();
677 GeomAdaptor_Curve anAdap( aCurve );
679 aResDistance = GCPnts_AbscissaPoint::Length( anAdap, aFirstParam, aSecondParam );
685 int HYDROData_PolylineXY::NbSections() const
687 Handle(TDataStd_ExtStringList) aNamesList;
688 Handle(TDataStd_IntegerList) aTypesList;
689 Handle(TDataStd_BooleanList) aClosuresList;
690 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
692 return !aClosuresList.IsNull() ? aClosuresList->Extent() : 0;
695 void HYDROData_PolylineXY::AddSection( const TCollection_AsciiString& theSectName,
696 const SectionType theSectionType,
697 const bool theIsClosed )
699 Handle(TDataStd_ExtStringList) aNamesList;
700 Handle(TDataStd_IntegerList) aTypesList;
701 Handle(TDataStd_BooleanList) aClosuresList;
702 getSectionsLists( aNamesList, aTypesList, aClosuresList );
704 TCollection_ExtendedString aSectName( theSectName );
705 if ( aSectName.Length() <= 0 )
706 aSectName = getUniqueSectionName( aNamesList );
708 aNamesList->Append( aSectName );
709 aTypesList->Append( theSectionType );
710 aClosuresList->Append( theIsClosed );
715 TCollection_AsciiString HYDROData_PolylineXY::GetSectionName( const int theSectionIndex ) const
717 TCollection_AsciiString aResName;
719 Handle(TDataStd_ExtStringList) aNamesList;
720 Handle(TDataStd_IntegerList) aTypesList;
721 Handle(TDataStd_BooleanList) aClosuresList;
722 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
723 if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
726 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( aNamesList->List() );
727 for ( int i = 0; aNamesIter.More() && i != theSectionIndex; aNamesIter.Next(), ++i );
729 if ( aNamesIter.More() )
730 aResName = aNamesIter.Value();
735 void HYDROData_PolylineXY::SetSectionName( const int theSectionIndex,
736 const TCollection_AsciiString& theSectionName )
738 Handle(TDataStd_ExtStringList) aNamesList;
739 Handle(TDataStd_IntegerList) aTypesList;
740 Handle(TDataStd_BooleanList) aClosuresList;
741 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
742 if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
745 TDataStd_ListOfExtendedString anOldNamesList;
746 anOldNamesList = aNamesList->List();
748 // Refill the existing list
751 TCollection_ExtendedString aNewSectName = theSectionName;
753 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( anOldNamesList );
754 for ( int i = 0; aNamesIter.More(); aNamesIter.Next(), ++i )
755 aNamesList->Append( i == theSectionIndex ? aNewSectName : aNamesIter.Value() );
758 HYDROData_PolylineXY::SectionType HYDROData_PolylineXY::GetSectionType( const int theSectionIndex ) const
760 Handle(TDataStd_ExtStringList) aNamesList;
761 Handle(TDataStd_IntegerList) aTypesList;
762 Handle(TDataStd_BooleanList) aClosuresList;
763 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
764 if ( aTypesList.IsNull() || theSectionIndex >= aTypesList->Extent() )
765 return SECTION_POLYLINE;
767 TColStd_ListIteratorOfListOfInteger aTypesIter( aTypesList->List() );
768 for ( int i = 0; aTypesIter.More() && i != theSectionIndex; aTypesIter.Next(), ++i );
770 return aTypesIter.More() ? (SectionType)aTypesIter.Value() : SECTION_POLYLINE;
773 void HYDROData_PolylineXY::SetSectionType( const int theSectionIndex,
774 const SectionType theSectionType )
776 Handle(TDataStd_ExtStringList) aNamesList;
777 Handle(TDataStd_IntegerList) aTypesList;
778 Handle(TDataStd_BooleanList) aClosuresList;
779 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
780 if ( aTypesList.IsNull() || theSectionIndex >= aTypesList->Extent() )
783 TColStd_ListOfInteger anOldTypesList;
784 anOldTypesList = aTypesList->List();
786 // Refill the existing list
789 TColStd_ListIteratorOfListOfInteger aTypesIter( anOldTypesList );
790 for ( int i = 0; aTypesIter.More(); aTypesIter.Next(), ++i )
791 aTypesList->Append( i == theSectionIndex ? theSectionType : aTypesIter.Value() );
796 bool HYDROData_PolylineXY::IsClosedSection( const int theSectionIndex ) const
798 Handle(TDataStd_ExtStringList) aNamesList;
799 Handle(TDataStd_IntegerList) aTypesList;
800 Handle(TDataStd_BooleanList) aClosuresList;
801 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
802 if ( aClosuresList.IsNull() || theSectionIndex >= aClosuresList->Extent() )
805 TDataStd_ListIteratorOfListOfByte aClosuresIter( aClosuresList->List() );
806 for ( int i = 0; aClosuresIter.More() && i != theSectionIndex; aClosuresIter.Next(), ++i );
808 return aClosuresIter.More() ? (bool)aClosuresIter.Value() : false;
811 void HYDROData_PolylineXY::SetSectionClosed( const int theSectionIndex,
812 const bool theIsClosed )
814 Handle(TDataStd_ExtStringList) aNamesList;
815 Handle(TDataStd_IntegerList) aTypesList;
816 Handle(TDataStd_BooleanList) aClosuresList;
817 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
818 if ( aClosuresList.IsNull() || theSectionIndex >= aClosuresList->Extent() )
821 TDataStd_ListOfByte anOldClosuresList;
822 anOldClosuresList = aClosuresList->List();
824 // Refill the existing list
825 aClosuresList->Clear();
827 TDataStd_ListIteratorOfListOfByte aClosuresIter( anOldClosuresList );
828 for ( int i = 0; aClosuresIter.More(); aClosuresIter.Next(), ++i )
829 aClosuresList->Append( i == theSectionIndex ? theIsClosed : (bool)aClosuresIter.Value() );
834 void HYDROData_PolylineXY::GetSections( NCollection_Sequence<TCollection_AsciiString>& theSectNames,
835 NCollection_Sequence<SectionType>& theSectTypes,
836 NCollection_Sequence<bool>& theSectClosures ) const
838 theSectNames.Clear();
839 theSectTypes.Clear();
840 theSectClosures.Clear();
844 const_cast<HYDROData_PolylineXY*>( this )->Interpolate();
847 Handle(TDataStd_ExtStringList) aNamesList;
848 Handle(TDataStd_IntegerList) aTypesList;
849 Handle(TDataStd_BooleanList) aClosuresList;
850 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
851 if ( aNamesList.IsNull() || aTypesList.IsNull() || aClosuresList.IsNull() )
854 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( aNamesList->List() );
855 TColStd_ListIteratorOfListOfInteger aTypesIter( aTypesList->List() );
856 TDataStd_ListIteratorOfListOfByte aClosuresIter( aClosuresList->List() );
857 for ( ; aNamesIter.More() && aTypesIter.More() && aClosuresIter.More();
858 aNamesIter.Next(), aTypesIter.Next(), aClosuresIter.Next() )
860 const TCollection_ExtendedString& aSectName = aNamesIter.Value();
861 SectionType aSectType = (SectionType)aTypesIter.Value();
862 bool aSectClosures = aClosuresIter.Value();
864 theSectNames.Append( aSectName );
865 theSectTypes.Append( aSectType );
866 theSectClosures.Append( aSectClosures );
870 void HYDROData_PolylineXY::RemoveSection( const int theSectionIndex )
872 Handle(TDataStd_ExtStringList) aNamesList;
873 Handle(TDataStd_IntegerList) aTypesList;
874 Handle(TDataStd_BooleanList) aClosuresList;
875 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
876 if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
879 if ( aNamesList->Extent() == 1 )
881 removeSectionsLists();
886 TDataStd_ListOfExtendedString anOldNamesList;
887 anOldNamesList = aNamesList->List();
889 TColStd_ListOfInteger anOldTypesList;
890 anOldTypesList = aTypesList->List();
892 TDataStd_ListOfByte anOldClosuresList;
893 anOldClosuresList = aClosuresList->List();
895 // Refill the existing lists
898 aClosuresList->Clear();
900 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( anOldNamesList );
901 TColStd_ListIteratorOfListOfInteger aTypesIter( anOldTypesList );
902 TDataStd_ListIteratorOfListOfByte aClosuresIter( anOldClosuresList );
903 for ( int i = 0; aNamesIter.More() && aTypesIter.More() && aClosuresIter.More();
904 aNamesIter.Next(), aTypesIter.Next(), aClosuresIter.Next(), ++i )
906 if ( i == theSectionIndex )
907 continue; // skip index to remove
909 aNamesList->Append( aNamesIter.Value() );
910 aTypesList->Append( aTypesIter.Value() );
911 aClosuresList->Append( (bool)aClosuresIter.Value() );
914 // Remove points that belongs to removed section
915 removePointsLists( theSectionIndex );
921 void HYDROData_PolylineXY::RemoveSections()
923 removeSectionsLists();
928 void HYDROData_PolylineXY::AddPoint( const int theSectionIndex,
929 const Point& thePoint,
930 const int thePointIndex )
932 Handle(TDataStd_RealList) aListX, aListY;
933 getPointsLists( theSectionIndex, aListX, aListY );
935 if ( thePointIndex < 0 || thePointIndex >= aListX->Extent() )
937 aListX->Append( thePoint.X() );
938 aListY->Append( thePoint.Y() );
942 TColStd_ListOfReal anOldListX;
943 anOldListX = aListX->List();
945 TColStd_ListOfReal anOldListY;
946 anOldListY = aListY->List();
948 // Refill the existing lists
952 TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
953 TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
954 for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
956 double aCoordX = anIterX.Value();
957 double aCoordY = anIterY.Value();
959 if ( i == thePointIndex )
961 // Insert our new point
962 aListX->Append( thePoint.X() );
963 aListY->Append( thePoint.Y() );
966 aListX->Append( aCoordX );
967 aListY->Append( aCoordY );
974 void HYDROData_PolylineXY::SetPoint( const int theSectionIndex,
975 const Point& thePoint,
976 const int thePointIndex )
978 Handle(TDataStd_RealList) aListX, aListY;
979 getPointsLists( theSectionIndex, aListX, aListY );
981 if ( thePointIndex < 0 )
983 aListX->Prepend( thePoint.X() );
984 aListY->Prepend( thePoint.Y() );
986 else if ( thePointIndex >= aListX->Extent() )
988 aListX->Append( thePoint.X() );
989 aListY->Append( thePoint.Y() );
993 TColStd_ListOfReal anOldListX;
994 anOldListX = aListX->List();
996 TColStd_ListOfReal anOldListY;
997 anOldListY = aListY->List();
999 // Refill the existing lists
1003 TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
1004 TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
1005 for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
1007 double aCoordX = anIterX.Value();
1008 double aCoordY = anIterY.Value();
1010 if ( i == thePointIndex )
1012 // Insert our new point instead of old one
1013 aCoordX = thePoint.X();
1014 aCoordY = thePoint.Y();
1017 aListX->Append( aCoordX );
1018 aListY->Append( aCoordY );
1025 void HYDROData_PolylineXY::SetPoints( const int theSectionIndex,
1026 const PointsList& thePoints )
1028 Handle(TDataStd_RealList) aListX, aListY;
1029 getPointsLists( theSectionIndex, aListX, aListY );
1034 for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
1036 const Point& aPoint = thePoints.Value( i );
1037 aListX->Append( aPoint.X() );
1038 aListY->Append( aPoint.Y() );
1042 void HYDROData_PolylineXY::RemovePoint( const int theSectionIndex,
1043 const int thePointIndex )
1045 Handle(TDataStd_RealList) aListX, aListY;
1046 getPointsLists( theSectionIndex, aListX, aListY, false );
1047 if ( aListX.IsNull() || aListY.IsNull() || aListX->IsEmpty() )
1050 if ( aListX->Extent() == 1 )
1052 removePointsLists( theSectionIndex );
1056 TColStd_ListOfReal anOldListX;
1057 anOldListX = aListX->List();
1059 TColStd_ListOfReal anOldListY;
1060 anOldListY = aListY->List();
1062 // Refill the existing lists
1066 TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
1067 TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
1068 for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
1070 if ( i == thePointIndex )
1071 continue; // skip index to remove
1073 aListX->Append( anIterX.Value() );
1074 aListY->Append( anIterY.Value() );
1081 HYDROData_PolylineXY::PointsList HYDROData_PolylineXY::GetPoints( const int theSectionIndex, bool IsConvertToGlobal ) const
1083 PointsList aResList;
1087 const_cast<HYDROData_PolylineXY*>( this )->Interpolate();
1090 Handle(TDataStd_RealList) aListX, aListY;
1091 getPointsLists( theSectionIndex, aListX, aListY, false );
1092 if ( aListX.IsNull() || aListY.IsNull() || aListX->IsEmpty() )
1095 TColStd_ListIteratorOfListOfReal anIterX( aListX->List() );
1096 TColStd_ListIteratorOfListOfReal anIterY( aListY->List() );
1097 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( myLab );
1098 for ( ; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next() )
1100 Point aPoint( anIterX.Value(), anIterY.Value() );
1101 if (IsConvertToGlobal)
1102 aDoc->Transform( aPoint, false );
1103 aResList.Append( aPoint );
1109 QPainterPath HYDROData_PolylineXY::GetPainterPath() const
1113 NCollection_Sequence<TCollection_AsciiString> aSectNames;
1114 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1115 NCollection_Sequence<bool> aSectClosures;
1116 GetSections( aSectNames, aSectTypes, aSectClosures );
1118 for ( int aSectionId = 1, aNbSects = aSectNames.Size(); aSectionId <= aNbSects; aSectionId++ )
1120 TCollection_AsciiString aSectName = aSectNames.Value( aSectionId );
1121 SectionType aSectionType = aSectTypes.Value( aSectionId );
1122 bool anIsSectionClosed = aSectClosures.Value( aSectionId );
1124 PointsList aSectPointsList = GetPoints( aSectionId - 1 );
1125 if ( aSectPointsList.IsEmpty() )
1128 NCollection_Sequence<gp_XYZ> aPoints;
1129 for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
1131 const Point& aSectPoint = aSectPointsList.Value( i );
1133 gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
1134 aPoints.Append( aPoint );
1137 BuildPainterPath( aPath, aSectionType, anIsSectionClosed, aPoints );
1143 void HYDROData_PolylineXY::UpdateLocalCS( double theDx, double theDy )
1145 NCollection_Sequence<TCollection_AsciiString> aSectNames;
1146 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1147 NCollection_Sequence<bool> aSectClosures;
1148 GetSections( aSectNames, aSectTypes, aSectClosures );
1150 gp_XY aDelta( theDx, theDy );
1151 for ( int i = 0, aNbSects = aSectNames.Size(); i < aNbSects; i++ )
1153 PointsList aPoints = GetPoints( i );
1154 for( int j = 1, n = aPoints.Size(); j <= n; ++j )
1156 Point& aPoint = aPoints.ChangeValue( j );
1159 SetPoints( i, aPoints );
1164 void HYDROData_PolylineXY::Transform( const QTransform& theTrsf )
1166 NCollection_Sequence<TCollection_AsciiString> aSectNames;
1167 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1168 NCollection_Sequence<bool> aSectClosures;
1169 GetSections( aSectNames, aSectTypes, aSectClosures );
1171 for ( int i = 0, aNbSects = aSectNames.Size(); i < aNbSects; i++ ) {
1172 PointsList aPoints = GetPoints( i );
1173 for( int j = 1, n = aPoints.Size(); j <= n; ++j ) {
1174 Point& aPoint = aPoints.ChangeValue( j );
1176 QPointF aTrsfPoint = theTrsf.map( QPointF( aPoint.X(), aPoint.Y() ) );
1178 aPoint.SetX( aTrsfPoint.x() );
1179 aPoint.SetY( aTrsfPoint.y() );
1181 SetPoints( i, aPoints );
1187 bool HYDROData_PolylineXY::IsCustom() const
1189 if( myIsInCustomFlag )
1192 bool isNull = GetShape().IsNull();
1195 HYDROData_PolylineXY* aThat = const_cast<HYDROData_PolylineXY*>( this );
1196 aThat->myIsInCustomFlag = true;
1197 for( int i=0, n=NbSections(); i<n; i++ )
1198 aNbPoints += NbPoints( i );
1199 aThat->myIsInCustomFlag = false;
1201 return !isNull && aNbPoints == 0;
1204 void HYDROData_PolylineXY::Interpolate()
1206 ImportShape( GetShape(), true );