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>
35 #include <GeomAPI_ProjectPointOnCurve.hxx>
36 #include <GeomAdaptor_Curve.hxx>
37 #include <Geom_Line.hxx>
38 #include <Geom_BSplineCurve.hxx>
40 #include <GCPnts_AbscissaPoint.hxx>
42 #include <ImageComposer_MetaTypes.h>
48 #include <NCollection_Map.hxx>
50 #include <TCollection_ExtendedString.hxx>
52 #include <TDataStd_ListIteratorOfListOfByte.hxx>
53 #include <TColStd_ListIteratorOfListOfInteger.hxx>
54 #include <TColStd_ListIteratorOfListOfReal.hxx>
56 #include <TColStd_Array1OfReal.hxx>
58 #include <TDataStd_AsciiString.hxx>
59 #include <TDataStd_BooleanList.hxx>
60 #include <TDataStd_ExtStringList.hxx>
61 #include <TDataStd_IntegerList.hxx>
62 #include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
63 #include <TDataStd_RealList.hxx>
64 #include <TDataStd_UAttribute.hxx>
66 #include <TopoDS_Iterator.hxx>
67 #include <TopTools_ListIteratorOfListOfShape.hxx>
68 #include <TopTools_HSequenceOfShape.hxx>
69 #include <TopExp_Explorer.hxx>
70 #include <ShapeAnalysis_FreeBounds.hxx>
74 #include <QPainterPath>
77 static const Standard_GUID GUID_IS_UNEDITABLE("e5799736-9030-4051-91a4-2e58321fa153");
79 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
81 TCollection_AsciiString getUniqueSectionName( const NCollection_Sequence<TCollection_AsciiString>& theNamesSeq )
83 NCollection_Map<TCollection_AsciiString> aNamesMap;
85 for ( int i = 1, n = theNamesSeq.Size(); i <= n; ++i )
87 const TCollection_AsciiString& aSectName = theNamesSeq.Value( i );
88 aNamesMap.Add( aSectName );
91 TCollection_AsciiString aResName;
96 aResName = TCollection_AsciiString( "Section_" ) + aPrefIdx;
99 while ( aNamesMap.Contains( aResName ) );
104 TCollection_AsciiString getUniqueSectionName( const Handle(TDataStd_ExtStringList)& theNamesList )
106 NCollection_Sequence<TCollection_AsciiString> aNamesSeq;
108 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( theNamesList->List() );
109 for ( ; aNamesIter.More(); aNamesIter.Next() )
110 aNamesSeq.Append( aNamesIter.Value() );
112 return getUniqueSectionName( aNamesSeq );
115 IMPLEMENT_STANDARD_HANDLE(HYDROData_PolylineXY, HYDROData_IPolyline)
116 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_PolylineXY, HYDROData_IPolyline)
118 HYDROData_PolylineXY::HYDROData_PolylineXY()
119 : HYDROData_IPolyline()
123 HYDROData_PolylineXY::~HYDROData_PolylineXY()
127 QStringList HYDROData_PolylineXY::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
129 QStringList aResList = dumpObjectCreation( theTreatedObjects );
130 QString aPolylineName = GetObjPyName();
132 // Set the wire color
133 QStringList aWireColorDef;
135 QColor aWireColor = GetWireColor();
136 setPythonObjectColor( aWireColorDef, aWireColor, DefaultWireColor(), "SetWireColor" );
138 if ( !aWireColorDef.isEmpty() )
140 aResList << aWireColorDef;
141 aResList << QString( "" );
144 bool anIsEditable = IsEditable();
147 // If polyline is not editable we try to import the shape from geom
148 TCollection_AsciiString aGeomObjectEntry = GetGeomObjectEntry();
149 if ( !aGeomObjectEntry.IsEmpty() )
151 QString aSalomeObjName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "polyline_sobj" );
152 aResList << QString( "%1 = salome.myStudy.FindObjectID( \"%2\" );" )
153 .arg( aSalomeObjName ).arg( aGeomObjectEntry.ToCString() );
155 aResList << QString( "%1.ImportFromGeomIOR( %2.GetIOR() );" )
156 .arg( aPolylineName ).arg( aSalomeObjName );
158 aResList << QString( "%1.SetGeomObjectEntry( \"%2\" );" )
159 .arg( aPolylineName ).arg( aGeomObjectEntry.ToCString() );
165 NCollection_Sequence<TCollection_AsciiString> aSectNames;
166 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
167 NCollection_Sequence<bool> aSectClosures;
168 GetSections( aSectNames, aSectTypes, aSectClosures );
170 for ( int i = 1, n = aSectNames.Size(); i <= n; ++i )
172 const TCollection_AsciiString& aSectName = aSectNames.Value( i );
173 const SectionType& aSectType = aSectTypes.Value( i );
174 bool aSectClosure = aSectClosures.Value( i );
176 aResList << QString( "%1.AddSection( \"%2\", %3, %4 );" ).arg( aPolylineName )
177 .arg( aSectName.ToCString() ).arg( aSectType ).arg( aSectClosure );
179 HYDROData_IPolyline::PointsList aSectPointsList = GetPoints( i - 1 );
180 for ( int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k )
182 const Point& aSectPoint = aSectPointsList.Value( k );
184 QString anXStr = QString::number( aSectPoint.X(), 'f', 2 );
185 QString anYStr = QString::number( aSectPoint.Y(), 'f', 2 );
186 aResList << QString( "%1.AddPoint( %2, gp_XY( %3, %4 ) );" ).arg( aPolylineName )
187 .arg( i - 1 ).arg( anXStr ).arg( anYStr );
191 aResList << QString( "" );
192 aResList << QString( "%1.Update();" ).arg( aPolylineName );
193 aResList << QString( "" );
198 QVariant HYDROData_PolylineXY::GetDataVariant()
200 QPainterPath aPath = GetPainterPath();
203 aVarData.setValue<QPainterPath>( aPath );
208 QColor HYDROData_PolylineXY::DefaultWireColor()
210 return QColor( Qt::red );
213 bool HYDROData_PolylineXY::ImportFromGeomIOR( const TCollection_AsciiString& theIOR )
215 if ( theIOR.IsEmpty() )
218 TopoDS_Shape aShape = GEOMBase::GetShapeFromIOR( theIOR.ToCString() );
219 if ( aShape.IsNull() )
222 return ImportShape( aShape );
225 void HYDROData_PolylineXY::SetGeomObjectEntry( const TCollection_AsciiString& theEntry )
227 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_GeomObjectEntry ), theEntry );
230 TCollection_AsciiString HYDROData_PolylineXY::GetGeomObjectEntry() const
232 TCollection_AsciiString aRes;
234 TDF_Label aLabel = myLab.FindChild( DataTag_GeomObjectEntry, false );
235 if ( !aLabel.IsNull() )
237 Handle(TDataStd_AsciiString) anAsciiStr;
238 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
239 aRes = anAsciiStr->Get();
245 TopoDS_Shape HYDROData_PolylineXY::GetShape() const
247 return getPolylineShape();
250 bool HYDROData_PolylineXY::SetShape( const TopoDS_Shape& theShape )
252 setPolylineShape( theShape );
256 bool convertEdgeToSection( const TopoDS_Edge& theEdge,
257 NCollection_Sequence<TCollection_AsciiString>& theSectNames,
258 NCollection_Sequence<HYDROData_PolylineXY::SectionType>& theSectTypes,
259 NCollection_Sequence<bool>& theSectClosures,
260 NCollection_Sequence<HYDROData_PolylineXY::PointsList>& theSectPoints,
261 const bool theIsCanBeClosed )
263 Standard_Real aFirst = 0.0, aLast = 0.0;
264 Handle(Geom_Curve) anEdgeGeomCurve = BRep_Tool::Curve( theEdge, aFirst, aLast );
265 if ( anEdgeGeomCurve.IsNull() )
268 TCollection_AsciiString aSectName = getUniqueSectionName( theSectNames );
269 bool anIsEdgeClosed = anEdgeGeomCurve->IsClosed();
271 HYDROData_PolylineXY::SectionType aSectionType = HYDROData_PolylineXY::SECTION_POLYLINE;
272 HYDROData_PolylineXY::PointsList aPointsList;
274 if ( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_Line) ) )
276 Handle(Geom_Line) aGeomLine = Handle(Geom_Line)::DownCast( anEdgeGeomCurve );
278 gp_Pnt aFirstPoint, aLastPoint;
279 aGeomLine->D0( aFirst, aFirstPoint );
280 aGeomLine->D0( aLast, aLastPoint );
282 HYDROData_PolylineXY::Point aSectFirstPoint( aFirstPoint.X(), aFirstPoint.Y() );
283 aPointsList.Append( aSectFirstPoint );
285 HYDROData_PolylineXY::Point aSectLastPoint( aLastPoint.X(), aLastPoint.Y() );
286 aPointsList.Append( aSectLastPoint );
288 else if ( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) )
290 aSectionType = HYDROData_PolylineXY::SECTION_SPLINE;
292 Handle(Geom_BSplineCurve) aGeomSpline =
293 Handle(Geom_BSplineCurve)::DownCast( anEdgeGeomCurve );
295 int aNbKnots = aGeomSpline->NbKnots();
297 TColStd_Array1OfReal aSplineKnots( 1, aNbKnots );
298 aGeomSpline->Knots( aSplineKnots );
300 // Decrease the number of imported knots because of last one
301 // knot is the closing point which are the start point
302 if ( anIsEdgeClosed ) aNbKnots--;
304 for ( int i = 1; i <= aNbKnots; ++i )
306 const Standard_Real& aKnot = aSplineKnots.Value( i );
309 aGeomSpline->D0( aKnot, aPoint );
311 HYDROData_PolylineXY::Point aSectPoint( aPoint.X(), aPoint.Y() );
312 aPointsList.Append( aSectPoint );
317 // Other curve types are not supported
321 if ( aPointsList.IsEmpty() )
324 theSectNames.Append( aSectName );
325 theSectTypes.Append( aSectionType );
326 theSectClosures.Append( anIsEdgeClosed );
327 theSectPoints.Append( aPointsList );
332 bool HYDROData_PolylineXY::ImportShape( const TopoDS_Shape& theShape )
334 if ( theShape.IsNull() )
339 bool anIsCanBeImported = false;
341 NCollection_Sequence<TCollection_AsciiString> aSectNames;
342 NCollection_Sequence<SectionType> aSectTypes;
343 NCollection_Sequence<bool> aSectClosures;
344 NCollection_Sequence<PointsList> aSectPoints;
346 if ( theShape.ShapeType() == TopAbs_EDGE )
348 TopoDS_Edge anEdge = TopoDS::Edge( theShape );
349 anIsCanBeImported = convertEdgeToSection( anEdge, aSectNames, aSectTypes, aSectClosures, aSectPoints, true );
351 else if ( theShape.ShapeType() == TopAbs_WIRE )
353 TopTools_SequenceOfShape anEdges;
354 HYDROData_ShapesTool::ExploreShapeToShapes( theShape, TopAbs_EDGE, anEdges );
356 anIsCanBeImported = !anEdges.IsEmpty();
357 for ( int i = 1, n = anEdges.Length(); i <= n && anIsCanBeImported; ++i )
359 TopoDS_Edge aWireEdge = TopoDS::Edge( anEdges.Value( i ) );
360 anIsCanBeImported = convertEdgeToSection( aWireEdge, aSectNames, aSectTypes, aSectClosures, aSectPoints, false );
364 if ( anIsCanBeImported )
366 for ( int i = 1, n = aSectNames.Length(); i <= n; ++i )
368 const TCollection_AsciiString& aSectName = aSectNames.Value( i );
369 const SectionType& aSectType = aSectTypes.Value( i );
370 bool anIsSectionClosed = aSectClosures.Value( i );
371 const PointsList& aSectPointsList = aSectPoints( i );
373 AddSection( aSectName, aSectType, anIsSectionClosed );
374 SetPoints( i - 1, aSectPointsList );
379 TopoDS_Shape aShape = theShape;
381 if ( theShape.ShapeType() == TopAbs_EDGE )
383 // We make the wire from incoming edge because of other algorithms
384 // are waiting at least the wire from polyline
385 TopoDS_Edge anEdge = TopoDS::Edge( theShape );
386 BRepBuilderAPI_MakeWire aMakeWire( anEdge );
388 if ( aMakeWire.IsDone() )
389 aShape = aMakeWire.Wire();
392 gp_Pln aPlane( gp_Pnt( 0, 0, 0 ), gp_Dir( 0, 0, 1 ) );
393 BRepBuilderAPI_MakeFace aMakeFace( aPlane );
395 BRepOffsetAPI_NormalProjection aProj( aMakeFace.Face() );
398 TopoDS_Shape aResult;
400 aResult = aProj.Shape();
402 setPolylineShape( aResult );
405 setEditable( anIsCanBeImported );
410 TopoDS_Wire HYDROData_PolylineXY::BuildWire( const SectionType& theType,
411 const bool& theIsClosed,
412 const NCollection_Sequence<gp_XYZ>& thePoints )
415 if( theType == SECTION_POLYLINE )
417 int aNbPoints = thePoints.Length();
418 BRepBuilderAPI_MakePolygon aMakeWire;
419 for ( int i = 1, n = aNbPoints; i <= n ; ++i )
421 gp_XYZ aPoint = thePoints.Value( i );
422 gp_Pnt aPnt( aPoint.X(), aPoint.Y(), aPoint.Z() );
423 aMakeWire.Add( aPnt );
425 if( theIsClosed && ( aNbPoints > 2 ) )
428 if ( aMakeWire.IsDone() )
429 aWire = aMakeWire.Wire();
431 else //if( theType == PolylineSection::SECTION_SPLINE )
433 BRepBuilderAPI_MakeWire aMakeWire;
435 if ( thePoints.Size() > 1 )
437 Handle(Geom_BSplineCurve) aCurve =
438 HYDROData_BSplineOperation::ComputeCurve( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
440 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aCurve ).Edge();
441 aMakeWire.Add( anEdge );
444 if ( aMakeWire.IsDone() )
451 void HYDROData_PolylineXY::BuildPainterPath( QPainterPath& thePath,
452 const SectionType& theType,
453 const bool& theIsClosed,
454 const NCollection_Sequence<gp_XYZ>& thePoints )
456 if ( thePoints.IsEmpty() )
459 if ( theType == SECTION_POLYLINE )
461 const gp_XYZ& aFirstPoint = thePoints.Value( 1 );
462 thePath.moveTo( aFirstPoint.X(), aFirstPoint.Y() );
464 for( int i = 2, n = thePoints.Size(); i <= n; ++i )
466 const gp_XYZ& aSectPoint = thePoints.Value( i );
468 thePath.lineTo( aSectPoint.X(), aSectPoint.Y() );
472 thePath.closeSubpath();
476 Handle(Geom_BSplineCurve) aCurve =
477 HYDROData_BSplineOperation::ComputeCurve( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
478 HYDROData_BSplineOperation::ComputePath( aCurve, thePath );
482 void HYDROData_PolylineXY::Update()
486 // If polyline is not editable we no need to update it wire
487 SetToUpdate( false );
491 HYDROData_IPolyline::Update();
493 NCollection_Sequence<TCollection_AsciiString> aSectNames;
494 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
495 NCollection_Sequence<bool> aSectClosures;
496 GetSections( aSectNames, aSectTypes, aSectClosures );
498 //BRepBuilderAPI_MakeWire aMakeWire;
500 TopTools_ListOfShape aSectionWiresList;
502 for ( int aSectionId = 1, aNbSects = aSectNames.Size(); aSectionId <= aNbSects; aSectionId++ )
504 TCollection_AsciiString aSectName = aSectNames.Value( aSectionId );
505 SectionType aSectionType = aSectTypes.Value( aSectionId );
506 bool anIsSectionClosed = aSectClosures.Value( aSectionId );
508 PointsList aSectPointsList = GetPoints( aSectionId - 1 );
509 if ( aSectPointsList.IsEmpty() )
512 NCollection_Sequence<gp_XYZ> aPoints;
513 for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
515 const Point& aSectPoint = aSectPointsList.Value( i );
517 gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
518 aPoints.Append( aPoint );
521 TopoDS_Wire aSectionWire = BuildWire( aSectionType, anIsSectionClosed, aPoints );
522 if ( !aSectionWire.IsNull() ) {
523 aSectionWiresList.Append( aSectionWire );
524 //aMakeWire.Add( aSectionWire );
527 // all input wires in the <aSectionWiresList>
528 Handle(TopTools_HSequenceOfShape) aSeqWires = new TopTools_HSequenceOfShape;
529 Handle(TopTools_HSequenceOfShape) aSeqEdges = new TopTools_HSequenceOfShape;
530 TopTools_ListIteratorOfListOfShape it(aSectionWiresList);
531 for(;it.More();it.Next())
533 TopExp_Explorer it2(it.Value(), TopAbs_EDGE);
534 for(;it2.More();it2.Next())
535 aSeqEdges->Append(it2.Current());
539 TopoDS_Compound aCmp;
540 TopoDS_Shape aResult;
541 aBB.MakeCompound(aCmp);
542 if(aSeqEdges->Length() >1)
544 ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdges,1E-5,Standard_False,aSeqWires);
546 if( aSeqWires->Length()==1 )
547 aResult = aSeqWires->Value( 1 );
550 for (Standard_Integer i = 1; i <= aSeqWires->Length();i++)
552 const TopoDS_Shape& aS1 = aSeqWires->Value(i);
558 else if (aSeqEdges->Length() == 1)
560 BRepBuilderAPI_MakeWire mkWire (TopoDS::Edge(aSeqEdges->Value(1)));
562 aResult = mkWire.Wire();
565 setPolylineShape( aResult );
568 bool HYDROData_PolylineXY::IsHas2dPrs() const
573 bool HYDROData_PolylineXY::IsEditable() const
575 return !myLab.IsAttribute( GUID_IS_UNEDITABLE );
578 void HYDROData_PolylineXY::setEditable( const bool theIsEditable )
580 if ( !theIsEditable )
581 TDataStd_UAttribute::Set( myLab, GUID_IS_UNEDITABLE );
583 myLab.ForgetAttribute( GUID_IS_UNEDITABLE );
587 * Returns true if polyline is closed
589 bool HYDROData_PolylineXY::IsClosed( const bool theIsSimpleCheck ) const
591 bool anIsClosed = false;
593 TopoDS_Shape aShape = GetShape();
594 if ( aShape.IsNull() )
597 TopTools_SequenceOfShape aWires;
598 HYDROData_ShapesTool::ExploreShapeToShapes( aShape, TopAbs_WIRE, aWires );
600 int aNbWires = aWires.Length();
601 if ( theIsSimpleCheck )
603 anIsClosed = aNbWires > 0;
604 for ( int i = 1; i <= aNbWires && anIsClosed; ++i )
606 const TopoDS_Shape& aWire = aWires.Value( i );
607 anIsClosed = BRep_Tool::IsClosed( aWire );
612 anIsClosed = aNbWires == 1 && BRep_Tool::IsClosed( aWires.First() );
618 double HYDROData_PolylineXY::GetDistance( const int theSectionIndex,
619 const int thePointIndex ) const
621 double aResDistance = -1;
622 if ( theSectionIndex < 0 || theSectionIndex >= NbSections() )
625 if ( thePointIndex == 0 )
628 SectionType aSectionType = GetSectionType( theSectionIndex );
629 bool anIsSectionClosed = IsClosedSection( theSectionIndex );
630 PointsList aSectPointsList = GetPoints( theSectionIndex );
631 if ( thePointIndex < 0 || thePointIndex >= aSectPointsList.Size() )
634 if ( aSectionType == SECTION_POLYLINE )
638 Point aPrevPoint = aSectPointsList.Value( 1 );
639 for ( int i = 2, aNbPoints = aSectPointsList.Size(); i <= aNbPoints; ++i )
641 const Point& aSectPoint = aSectPointsList.Value( i );
642 aResDistance += gp_Pnt2d( aPrevPoint ).Distance( aSectPoint );
643 aPrevPoint = aSectPoint;
645 if ( thePointIndex == i - 1 )
653 int aSectNbPoints = aSectPointsList.Size();
654 NCollection_Sequence<gp_XYZ> aPoints;
655 for( int i = 1 ; i <= aSectNbPoints; ++i )
657 const Point& aSectPoint = aSectPointsList.Value( i );
659 gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
660 aPoints.Append( aPoint );
662 if ( thePointIndex == i - 1 )
663 aPointToTest = aPoint;
666 Handle(Geom_BSplineCurve) aCurve =
667 HYDROData_BSplineOperation::ComputeCurve( aPoints, anIsSectionClosed, LOCAL_SELECTION_TOLERANCE );
669 Quantity_Parameter aFirstParam = aCurve->FirstParameter();
670 Quantity_Parameter aSecondParam = aCurve->LastParameter();
672 if ( thePointIndex != aSectNbPoints - 1 )
674 GeomAPI_ProjectPointOnCurve aProject( aPointToTest, aCurve );
675 aSecondParam = aProject.LowerDistanceParameter();
678 GeomAdaptor_Curve anAdap( aCurve );
680 aResDistance = GCPnts_AbscissaPoint::Length( anAdap, aFirstParam, aSecondParam );
686 int HYDROData_PolylineXY::NbSections() const
688 Handle(TDataStd_ExtStringList) aNamesList;
689 Handle(TDataStd_IntegerList) aTypesList;
690 Handle(TDataStd_BooleanList) aClosuresList;
691 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
693 return !aClosuresList.IsNull() ? aClosuresList->Extent() : 0;
696 void HYDROData_PolylineXY::AddSection( const TCollection_AsciiString& theSectName,
697 const SectionType theSectionType,
698 const bool theIsClosed )
700 Handle(TDataStd_ExtStringList) aNamesList;
701 Handle(TDataStd_IntegerList) aTypesList;
702 Handle(TDataStd_BooleanList) aClosuresList;
703 getSectionsLists( aNamesList, aTypesList, aClosuresList );
705 TCollection_ExtendedString aSectName( theSectName );
706 if ( aSectName.Length() <= 0 )
707 aSectName = getUniqueSectionName( aNamesList );
709 aNamesList->Append( aSectName );
710 aTypesList->Append( theSectionType );
711 aClosuresList->Append( theIsClosed );
716 TCollection_AsciiString HYDROData_PolylineXY::GetSectionName( const int theSectionIndex ) const
718 TCollection_AsciiString aResName;
720 Handle(TDataStd_ExtStringList) aNamesList;
721 Handle(TDataStd_IntegerList) aTypesList;
722 Handle(TDataStd_BooleanList) aClosuresList;
723 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
724 if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
727 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( aNamesList->List() );
728 for ( int i = 0; aNamesIter.More() && i != theSectionIndex; aNamesIter.Next(), ++i );
730 if ( aNamesIter.More() )
731 aResName = aNamesIter.Value();
736 void HYDROData_PolylineXY::SetSectionName( const int theSectionIndex,
737 const TCollection_AsciiString& theSectionName )
739 Handle(TDataStd_ExtStringList) aNamesList;
740 Handle(TDataStd_IntegerList) aTypesList;
741 Handle(TDataStd_BooleanList) aClosuresList;
742 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
743 if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
746 TDataStd_ListOfExtendedString anOldNamesList;
747 anOldNamesList = aNamesList->List();
749 // Refill the existing list
752 TCollection_ExtendedString aNewSectName = theSectionName;
754 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( anOldNamesList );
755 for ( int i = 0; aNamesIter.More(); aNamesIter.Next(), ++i )
756 aNamesList->Append( i == theSectionIndex ? aNewSectName : aNamesIter.Value() );
761 HYDROData_PolylineXY::SectionType HYDROData_PolylineXY::GetSectionType( const int theSectionIndex ) const
763 Handle(TDataStd_ExtStringList) aNamesList;
764 Handle(TDataStd_IntegerList) aTypesList;
765 Handle(TDataStd_BooleanList) aClosuresList;
766 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
767 if ( aTypesList.IsNull() || theSectionIndex >= aTypesList->Extent() )
768 return SECTION_POLYLINE;
770 TColStd_ListIteratorOfListOfInteger aTypesIter( aTypesList->List() );
771 for ( int i = 0; aTypesIter.More() && i != theSectionIndex; aTypesIter.Next(), ++i );
773 return aTypesIter.More() ? (SectionType)aTypesIter.Value() : SECTION_POLYLINE;
776 void HYDROData_PolylineXY::SetSectionType( const int theSectionIndex,
777 const SectionType theSectionType )
779 Handle(TDataStd_ExtStringList) aNamesList;
780 Handle(TDataStd_IntegerList) aTypesList;
781 Handle(TDataStd_BooleanList) aClosuresList;
782 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
783 if ( aTypesList.IsNull() || theSectionIndex >= aTypesList->Extent() )
786 TColStd_ListOfInteger anOldTypesList;
787 anOldTypesList = aTypesList->List();
789 // Refill the existing list
792 TColStd_ListIteratorOfListOfInteger aTypesIter( anOldTypesList );
793 for ( int i = 0; aTypesIter.More(); aTypesIter.Next(), ++i )
794 aTypesList->Append( i == theSectionIndex ? theSectionType : aTypesIter.Value() );
799 bool HYDROData_PolylineXY::IsClosedSection( const int theSectionIndex ) const
801 Handle(TDataStd_ExtStringList) aNamesList;
802 Handle(TDataStd_IntegerList) aTypesList;
803 Handle(TDataStd_BooleanList) aClosuresList;
804 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
805 if ( aClosuresList.IsNull() || theSectionIndex >= aClosuresList->Extent() )
808 TDataStd_ListIteratorOfListOfByte aClosuresIter( aClosuresList->List() );
809 for ( int i = 0; aClosuresIter.More() && i != theSectionIndex; aClosuresIter.Next(), ++i );
811 return aClosuresIter.More() ? (bool)aClosuresIter.Value() : false;
814 void HYDROData_PolylineXY::SetSectionClosed( const int theSectionIndex,
815 const bool theIsClosed )
817 Handle(TDataStd_ExtStringList) aNamesList;
818 Handle(TDataStd_IntegerList) aTypesList;
819 Handle(TDataStd_BooleanList) aClosuresList;
820 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
821 if ( aClosuresList.IsNull() || theSectionIndex >= aClosuresList->Extent() )
824 TDataStd_ListOfByte anOldClosuresList;
825 anOldClosuresList = aClosuresList->List();
827 // Refill the existing list
828 aClosuresList->Clear();
830 TDataStd_ListIteratorOfListOfByte aClosuresIter( anOldClosuresList );
831 for ( int i = 0; aClosuresIter.More(); aClosuresIter.Next(), ++i )
832 aClosuresList->Append( i == theSectionIndex ? theIsClosed : (bool)aClosuresIter.Value() );
837 void HYDROData_PolylineXY::GetSections( NCollection_Sequence<TCollection_AsciiString>& theSectNames,
838 NCollection_Sequence<SectionType>& theSectTypes,
839 NCollection_Sequence<bool>& theSectClosures ) const
841 theSectNames.Clear();
842 theSectTypes.Clear();
843 theSectClosures.Clear();
845 Handle(TDataStd_ExtStringList) aNamesList;
846 Handle(TDataStd_IntegerList) aTypesList;
847 Handle(TDataStd_BooleanList) aClosuresList;
848 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
849 if ( aNamesList.IsNull() || aTypesList.IsNull() || aClosuresList.IsNull() )
852 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( aNamesList->List() );
853 TColStd_ListIteratorOfListOfInteger aTypesIter( aTypesList->List() );
854 TDataStd_ListIteratorOfListOfByte aClosuresIter( aClosuresList->List() );
855 for ( ; aNamesIter.More() && aTypesIter.More() && aClosuresIter.More();
856 aNamesIter.Next(), aTypesIter.Next(), aClosuresIter.Next() )
858 const TCollection_ExtendedString& aSectName = aNamesIter.Value();
859 SectionType aSectType = (SectionType)aTypesIter.Value();
860 bool aSectClosures = aClosuresIter.Value();
862 theSectNames.Append( aSectName );
863 theSectTypes.Append( aSectType );
864 theSectClosures.Append( aSectClosures );
868 void HYDROData_PolylineXY::RemoveSection( const int theSectionIndex )
870 Handle(TDataStd_ExtStringList) aNamesList;
871 Handle(TDataStd_IntegerList) aTypesList;
872 Handle(TDataStd_BooleanList) aClosuresList;
873 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
874 if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
877 if ( aNamesList->Extent() == 1 )
879 removeSectionsLists();
884 TDataStd_ListOfExtendedString anOldNamesList;
885 anOldNamesList = aNamesList->List();
887 TColStd_ListOfInteger anOldTypesList;
888 anOldTypesList = aTypesList->List();
890 TDataStd_ListOfByte anOldClosuresList;
891 anOldClosuresList = aClosuresList->List();
893 // Refill the existing lists
896 aClosuresList->Clear();
898 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( anOldNamesList );
899 TColStd_ListIteratorOfListOfInteger aTypesIter( anOldTypesList );
900 TDataStd_ListIteratorOfListOfByte aClosuresIter( anOldClosuresList );
901 for ( int i = 0; aNamesIter.More() && aTypesIter.More() && aClosuresIter.More();
902 aNamesIter.Next(), aTypesIter.Next(), aClosuresIter.Next(), ++i )
904 if ( i == theSectionIndex )
905 continue; // skip index to remove
907 aNamesList->Append( aNamesIter.Value() );
908 aTypesList->Append( aTypesIter.Value() );
909 aClosuresList->Append( (bool)aClosuresIter.Value() );
912 // Remove points that belongs to removed section
913 removePointsLists( theSectionIndex );
919 void HYDROData_PolylineXY::RemoveSections()
921 removeSectionsLists();
926 void HYDROData_PolylineXY::AddPoint( const int theSectionIndex,
927 const Point& thePoint,
928 const int thePointIndex )
930 Handle(TDataStd_RealList) aListX, aListY;
931 getPointsLists( theSectionIndex, aListX, aListY );
933 if ( thePointIndex < 0 || thePointIndex >= aListX->Extent() )
935 aListX->Append( thePoint.X() );
936 aListY->Append( thePoint.Y() );
940 TColStd_ListOfReal anOldListX;
941 anOldListX = aListX->List();
943 TColStd_ListOfReal anOldListY;
944 anOldListY = aListY->List();
946 // Refill the existing lists
950 TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
951 TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
952 for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
954 double aCoordX = anIterX.Value();
955 double aCoordY = anIterY.Value();
957 if ( i == thePointIndex )
959 // Insert our new point
960 aListX->Append( thePoint.X() );
961 aListY->Append( thePoint.Y() );
964 aListX->Append( aCoordX );
965 aListY->Append( aCoordY );
972 void HYDROData_PolylineXY::SetPoint( const int theSectionIndex,
973 const Point& thePoint,
974 const int thePointIndex )
976 Handle(TDataStd_RealList) aListX, aListY;
977 getPointsLists( theSectionIndex, aListX, aListY );
979 if ( thePointIndex < 0 )
981 aListX->Prepend( thePoint.X() );
982 aListY->Prepend( thePoint.Y() );
984 else if ( thePointIndex >= aListX->Extent() )
986 aListX->Append( thePoint.X() );
987 aListY->Append( thePoint.Y() );
991 TColStd_ListOfReal anOldListX;
992 anOldListX = aListX->List();
994 TColStd_ListOfReal anOldListY;
995 anOldListY = aListY->List();
997 // Refill the existing lists
1001 TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
1002 TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
1003 for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
1005 double aCoordX = anIterX.Value();
1006 double aCoordY = anIterY.Value();
1008 if ( i == thePointIndex )
1010 // Insert our new point instead of old one
1011 aCoordX = thePoint.X();
1012 aCoordY = thePoint.Y();
1015 aListX->Append( aCoordX );
1016 aListY->Append( aCoordY );
1020 SetToUpdate( true );
1023 void HYDROData_PolylineXY::SetPoints( const int theSectionIndex,
1024 const PointsList& thePoints )
1026 Handle(TDataStd_RealList) aListX, aListY;
1027 getPointsLists( theSectionIndex, aListX, aListY );
1032 for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
1034 const Point& aPoint = thePoints.Value( i );
1035 aListX->Append( aPoint.X() );
1036 aListY->Append( aPoint.Y() );
1040 void HYDROData_PolylineXY::RemovePoint( const int theSectionIndex,
1041 const int thePointIndex )
1043 Handle(TDataStd_RealList) aListX, aListY;
1044 getPointsLists( theSectionIndex, aListX, aListY, false );
1045 if ( aListX.IsNull() || aListY.IsNull() || aListX->IsEmpty() )
1048 if ( aListX->Extent() == 1 )
1050 removePointsLists( theSectionIndex );
1054 TColStd_ListOfReal anOldListX;
1055 anOldListX = aListX->List();
1057 TColStd_ListOfReal anOldListY;
1058 anOldListY = aListY->List();
1060 // Refill the existing lists
1064 TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
1065 TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
1066 for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
1068 if ( i == thePointIndex )
1069 continue; // skip index to remove
1071 aListX->Append( anIterX.Value() );
1072 aListY->Append( anIterY.Value() );
1076 SetToUpdate( true );
1079 HYDROData_PolylineXY::PointsList HYDROData_PolylineXY::GetPoints( const int theSectionIndex ) const
1081 PointsList aResList;
1085 //TODO: make interpolation to fill the list
1088 Handle(TDataStd_RealList) aListX, aListY;
1089 getPointsLists( theSectionIndex, aListX, aListY, false );
1090 if ( aListX.IsNull() || aListY.IsNull() || aListX->IsEmpty() )
1093 TColStd_ListIteratorOfListOfReal anIterX( aListX->List() );
1094 TColStd_ListIteratorOfListOfReal anIterY( aListY->List() );
1095 for ( ; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next() )
1097 Point aPoint( anIterX.Value(), anIterY.Value() );
1098 aResList.Append( aPoint );
1104 QPainterPath HYDROData_PolylineXY::GetPainterPath() const
1108 NCollection_Sequence<TCollection_AsciiString> aSectNames;
1109 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1110 NCollection_Sequence<bool> aSectClosures;
1111 GetSections( aSectNames, aSectTypes, aSectClosures );
1113 for ( int aSectionId = 1, aNbSects = aSectNames.Size(); aSectionId <= aNbSects; aSectionId++ )
1115 TCollection_AsciiString aSectName = aSectNames.Value( aSectionId );
1116 SectionType aSectionType = aSectTypes.Value( aSectionId );
1117 bool anIsSectionClosed = aSectClosures.Value( aSectionId );
1119 PointsList aSectPointsList = GetPoints( aSectionId - 1 );
1120 if ( aSectPointsList.IsEmpty() )
1123 NCollection_Sequence<gp_XYZ> aPoints;
1124 for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
1126 const Point& aSectPoint = aSectPointsList.Value( i );
1128 gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
1129 aPoints.Append( aPoint );
1132 BuildPainterPath( aPath, aSectionType, anIsSectionClosed, aPoints );
1138 void HYDROData_PolylineXY::UpdateLocalCS( double theDx, double theDy )
1140 NCollection_Sequence<TCollection_AsciiString> aSectNames;
1141 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1142 NCollection_Sequence<bool> aSectClosures;
1143 GetSections( aSectNames, aSectTypes, aSectClosures );
1145 gp_XY aDelta( theDx, theDy );
1146 for ( int i = 0, aNbSects = aSectNames.Size(); i < aNbSects; i++ )
1148 PointsList aPoints = GetPoints( i );
1149 for( int j = 1, n = aPoints.Size(); j <= n; ++j )
1151 Point& aPoint = aPoints.ChangeValue( j );
1154 SetPoints( i, aPoints );
1156 SetToUpdate( true );
1159 void HYDROData_PolylineXY::Transform( const QTransform& theTrsf )
1161 NCollection_Sequence<TCollection_AsciiString> aSectNames;
1162 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1163 NCollection_Sequence<bool> aSectClosures;
1164 GetSections( aSectNames, aSectTypes, aSectClosures );
1166 for ( int i = 0, aNbSects = aSectNames.Size(); i < aNbSects; i++ ) {
1167 PointsList aPoints = GetPoints( i );
1168 for( int j = 1, n = aPoints.Size(); j <= n; ++j ) {
1169 Point& aPoint = aPoints.ChangeValue( j );
1171 QPointF aTrsfPoint = theTrsf.map( QPointF( aPoint.X(), aPoint.Y() ) );
1173 aPoint.SetX( aTrsfPoint.x() );
1174 aPoint.SetY( aTrsfPoint.y() );
1176 SetPoints( i, aPoints );
1182 bool HYDROData_PolylineXY::IsCustom() const
1184 bool isNull = getPolylineShape().IsNull();
1187 //TODO: to check if there is no points
1188 //for( int i=0, n=NbSections(); i<n; i++ )
1189 // aNbPoints += NbPoints( i );
1191 return !isNull && aNbPoints == 0;