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>
37 #include <GeomAPI_ProjectPointOnCurve.hxx>
38 #include <GeomAdaptor_Curve.hxx>
39 #include <Geom_Line.hxx>
40 #include <Geom_BSplineCurve.hxx>
42 #include <GCPnts_AbscissaPoint.hxx>
44 #include <ImageComposer_MetaTypes.h>
50 #include <NCollection_Map.hxx>
52 #include <TCollection_ExtendedString.hxx>
54 #include <TDataStd_ListIteratorOfListOfByte.hxx>
55 #include <TColStd_ListIteratorOfListOfInteger.hxx>
56 #include <TColStd_ListIteratorOfListOfReal.hxx>
58 #include <TColStd_Array1OfReal.hxx>
60 #include <TDataStd_AsciiString.hxx>
61 #include <TDataStd_BooleanList.hxx>
62 #include <TDataStd_ExtStringList.hxx>
63 #include <TDataStd_IntegerList.hxx>
64 #include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
65 #include <TDataStd_RealList.hxx>
66 #include <TDataStd_UAttribute.hxx>
68 #include <TopoDS_Iterator.hxx>
69 #include <TopTools_ListIteratorOfListOfShape.hxx>
70 #include <TopTools_HSequenceOfShape.hxx>
71 #include <TopExp_Explorer.hxx>
72 #include <ShapeAnalysis_FreeBounds.hxx>
76 #include <QPainterPath>
79 static const Standard_GUID GUID_IS_UNEDITABLE("e5799736-9030-4051-91a4-2e58321fa153");
81 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
83 TCollection_AsciiString getUniqueSectionName( const NCollection_Sequence<TCollection_AsciiString>& theNamesSeq )
85 NCollection_Map<TCollection_AsciiString> aNamesMap;
87 for ( int i = 1, n = theNamesSeq.Size(); i <= n; ++i )
89 const TCollection_AsciiString& aSectName = theNamesSeq.Value( i );
90 aNamesMap.Add( aSectName );
93 TCollection_AsciiString aResName;
98 aResName = TCollection_AsciiString( "Section_" ) + aPrefIdx;
101 while ( aNamesMap.Contains( aResName ) );
106 TCollection_AsciiString getUniqueSectionName( const Handle(TDataStd_ExtStringList)& theNamesList )
108 NCollection_Sequence<TCollection_AsciiString> aNamesSeq;
110 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( theNamesList->List() );
111 for ( ; aNamesIter.More(); aNamesIter.Next() )
112 aNamesSeq.Append( aNamesIter.Value() );
114 return getUniqueSectionName( aNamesSeq );
117 IMPLEMENT_STANDARD_HANDLE(HYDROData_PolylineXY, HYDROData_IPolyline)
118 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_PolylineXY, HYDROData_IPolyline)
120 HYDROData_PolylineXY::HYDROData_PolylineXY()
121 : HYDROData_IPolyline()
125 HYDROData_PolylineXY::~HYDROData_PolylineXY()
129 QStringList HYDROData_PolylineXY::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
131 QStringList aResList = dumpObjectCreation( theTreatedObjects );
132 QString aPolylineName = GetObjPyName();
134 // Set the wire color
135 QStringList aWireColorDef;
137 QColor aWireColor = GetWireColor();
138 setPythonObjectColor( aWireColorDef, aWireColor, DefaultWireColor(), "SetWireColor" );
140 if ( !aWireColorDef.isEmpty() )
142 aResList << aWireColorDef;
143 aResList << QString( "" );
146 bool anIsEditable = IsEditable();
149 // If polyline is not editable we try to import the shape from geom
150 TCollection_AsciiString aGeomObjectEntry = GetGeomObjectEntry();
151 if ( !aGeomObjectEntry.IsEmpty() )
153 QString aSalomeObjName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "polyline_sobj" );
154 aResList << QString( "%1 = salome.myStudy.FindObjectID( \"%2\" );" )
155 .arg( aSalomeObjName ).arg( aGeomObjectEntry.ToCString() );
157 aResList << QString( "%1.ImportFromGeomIOR( %2.GetIOR() );" )
158 .arg( aPolylineName ).arg( aSalomeObjName );
160 aResList << QString( "%1.SetGeomObjectEntry( \"%2\" );" )
161 .arg( aPolylineName ).arg( aGeomObjectEntry.ToCString() );
167 NCollection_Sequence<TCollection_AsciiString> aSectNames;
168 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
169 NCollection_Sequence<bool> aSectClosures;
170 GetSections( aSectNames, aSectTypes, aSectClosures );
172 for ( int i = 1, n = aSectNames.Size(); i <= n; ++i )
174 const TCollection_AsciiString& aSectName = aSectNames.Value( i );
175 const SectionType& aSectType = aSectTypes.Value( i );
176 bool aSectClosure = aSectClosures.Value( i );
178 aResList << QString( "%1.AddSection( \"%2\", %3, %4 );" ).arg( aPolylineName )
179 .arg( aSectName.ToCString() ).arg( aSectType ).arg( aSectClosure );
181 HYDROData_IPolyline::PointsList aSectPointsList = GetPoints( i - 1 );
182 for ( int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k )
184 const Point& aSectPoint = aSectPointsList.Value( k );
186 QString anXStr = QString::number( aSectPoint.X(), 'f', 2 );
187 QString anYStr = QString::number( aSectPoint.Y(), 'f', 2 );
188 aResList << QString( "%1.AddPoint( %2, gp_XY( %3, %4 ) );" ).arg( aPolylineName )
189 .arg( i - 1 ).arg( anXStr ).arg( anYStr );
193 aResList << QString( "" );
194 aResList << QString( "%1.Update();" ).arg( aPolylineName );
195 aResList << QString( "" );
200 QVariant HYDROData_PolylineXY::GetDataVariant()
202 QPainterPath aPath = GetPainterPath();
205 aVarData.setValue<QPainterPath>( aPath );
210 QColor HYDROData_PolylineXY::DefaultWireColor()
212 return QColor( Qt::red );
215 bool HYDROData_PolylineXY::ImportFromGeomIOR( const TCollection_AsciiString& theIOR )
220 if ( theIOR.IsEmpty() )
223 TopoDS_Shape aShape = GEOMBase::GetShapeFromIOR( theIOR.ToCString() );
224 if ( aShape.IsNull() )
227 return ImportShape( aShape );
231 void HYDROData_PolylineXY::SetGeomObjectEntry( const TCollection_AsciiString& theEntry )
233 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_GeomObjectEntry ), theEntry );
236 TCollection_AsciiString HYDROData_PolylineXY::GetGeomObjectEntry() const
238 TCollection_AsciiString aRes;
240 TDF_Label aLabel = myLab.FindChild( DataTag_GeomObjectEntry, false );
241 if ( !aLabel.IsNull() )
243 Handle(TDataStd_AsciiString) anAsciiStr;
244 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
245 aRes = anAsciiStr->Get();
251 bool convertEdgeToSection( const TopoDS_Edge& theEdge,
252 NCollection_Sequence<TCollection_AsciiString>& theSectNames,
253 NCollection_Sequence<HYDROData_PolylineXY::SectionType>& theSectTypes,
254 NCollection_Sequence<bool>& theSectClosures,
255 NCollection_Sequence<HYDROData_PolylineXY::PointsList>& theSectPoints,
256 const bool theIsCanBeClosed )
258 Standard_Real aFirst = 0.0, aLast = 0.0;
259 Handle(Geom_Curve) anEdgeGeomCurve = BRep_Tool::Curve( theEdge, aFirst, aLast );
260 if ( anEdgeGeomCurve.IsNull() )
263 TCollection_AsciiString aSectName = getUniqueSectionName( theSectNames );
264 bool anIsEdgeClosed = anEdgeGeomCurve->IsClosed();
266 HYDROData_PolylineXY::SectionType aSectionType = HYDROData_PolylineXY::SECTION_POLYLINE;
267 HYDROData_PolylineXY::PointsList aPointsList;
269 if ( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_Line) ) )
271 Handle(Geom_Line) aGeomLine = Handle(Geom_Line)::DownCast( anEdgeGeomCurve );
273 gp_Pnt aFirstPoint, aLastPoint;
274 aGeomLine->D0( aFirst, aFirstPoint );
275 aGeomLine->D0( aLast, aLastPoint );
277 HYDROData_PolylineXY::Point aSectFirstPoint( aFirstPoint.X(), aFirstPoint.Y() );
278 aPointsList.Append( aSectFirstPoint );
280 HYDROData_PolylineXY::Point aSectLastPoint( aLastPoint.X(), aLastPoint.Y() );
281 aPointsList.Append( aSectLastPoint );
283 else if ( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) )
285 aSectionType = HYDROData_PolylineXY::SECTION_SPLINE;
287 Handle(Geom_BSplineCurve) aGeomSpline =
288 Handle(Geom_BSplineCurve)::DownCast( anEdgeGeomCurve );
290 int aNbKnots = aGeomSpline->NbKnots();
292 TColStd_Array1OfReal aSplineKnots( 1, aNbKnots );
293 aGeomSpline->Knots( aSplineKnots );
295 // Decrease the number of imported knots because of last one
296 // knot is the closing point which are the start point
297 if ( anIsEdgeClosed ) aNbKnots--;
299 for ( int i = 1; i <= aNbKnots; ++i )
301 const Standard_Real& aKnot = aSplineKnots.Value( i );
304 aGeomSpline->D0( aKnot, aPoint );
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 )
329 if ( theShape.IsNull() )
334 bool anIsCanBeImported = false;
336 NCollection_Sequence<TCollection_AsciiString> aSectNames;
337 NCollection_Sequence<SectionType> aSectTypes;
338 NCollection_Sequence<bool> aSectClosures;
339 NCollection_Sequence<PointsList> aSectPoints;
341 if ( theShape.ShapeType() == TopAbs_EDGE )
343 TopoDS_Edge anEdge = TopoDS::Edge( theShape );
344 anIsCanBeImported = convertEdgeToSection( anEdge, aSectNames, aSectTypes, aSectClosures, aSectPoints, true );
346 else if ( theShape.ShapeType() == TopAbs_WIRE )
348 TopTools_SequenceOfShape anEdges;
349 HYDROData_ShapesTool::ExploreShapeToShapes( theShape, TopAbs_EDGE, anEdges );
351 anIsCanBeImported = !anEdges.IsEmpty();
352 for ( int i = 1, n = anEdges.Length(); i <= n && anIsCanBeImported; ++i )
354 TopoDS_Edge aWireEdge = TopoDS::Edge( anEdges.Value( i ) );
355 anIsCanBeImported = convertEdgeToSection( aWireEdge, aSectNames, aSectTypes, aSectClosures, aSectPoints, false );
359 if ( anIsCanBeImported )
361 for ( int i = 1, n = aSectNames.Length(); i <= n; ++i )
363 const TCollection_AsciiString& aSectName = aSectNames.Value( i );
364 const SectionType& aSectType = aSectTypes.Value( i );
365 bool anIsSectionClosed = aSectClosures.Value( i );
366 const PointsList& aSectPointsList = aSectPoints( i );
368 AddSection( aSectName, aSectType, anIsSectionClosed );
369 SetPoints( i - 1, aSectPointsList );
374 TopoDS_Shape aShape = theShape;
376 if ( theShape.ShapeType() == TopAbs_EDGE )
378 // We make the wire from incoming edge because of other algorithms
379 // are waiting at least the wire from polyline
380 TopoDS_Edge anEdge = TopoDS::Edge( theShape );
381 BRepBuilderAPI_MakeWire aMakeWire( anEdge );
383 if ( aMakeWire.IsDone() )
384 aShape = aMakeWire.Wire();
387 gp_Pln aPlane( gp_Pnt( 0, 0, 0 ), gp_Dir( 0, 0, 1 ) );
388 BRepBuilderAPI_MakeFace aMakeFace( aPlane );
390 BRepOffsetAPI_NormalProjection aProj( aMakeFace.Face() );
393 TopoDS_Shape aResult;
395 aResult = aProj.Shape();
400 setEditable( anIsCanBeImported );
405 TopoDS_Wire HYDROData_PolylineXY::BuildWire( const SectionType& theType,
406 const bool& theIsClosed,
407 const NCollection_Sequence<gp_XYZ>& thePoints )
410 if( theType == SECTION_POLYLINE )
412 int aNbPoints = thePoints.Length();
413 BRepBuilderAPI_MakePolygon aMakeWire;
414 for ( int i = 1, n = aNbPoints; i <= n ; ++i )
416 gp_XYZ aPoint = thePoints.Value( i );
417 gp_Pnt aPnt( aPoint.X(), aPoint.Y(), aPoint.Z() );
418 aMakeWire.Add( aPnt );
420 if( theIsClosed && ( aNbPoints > 2 ) )
423 if ( aMakeWire.IsDone() )
424 aWire = aMakeWire.Wire();
426 else //if( theType == PolylineSection::SECTION_SPLINE )
428 BRepBuilderAPI_MakeWire aMakeWire;
430 if ( thePoints.Size() > 1 )
432 Handle(Geom_BSplineCurve) aCurve =
433 HYDROData_BSplineOperation::ComputeCurve( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
435 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aCurve ).Edge();
436 aMakeWire.Add( anEdge );
439 if ( aMakeWire.IsDone() )
446 void HYDROData_PolylineXY::BuildPainterPath( QPainterPath& thePath,
447 const SectionType& theType,
448 const bool& theIsClosed,
449 const NCollection_Sequence<gp_XYZ>& thePoints )
451 if ( thePoints.IsEmpty() )
454 if ( theType == SECTION_POLYLINE )
456 const gp_XYZ& aFirstPoint = thePoints.Value( 1 );
457 thePath.moveTo( aFirstPoint.X(), aFirstPoint.Y() );
459 for( int i = 2, n = thePoints.Size(); i <= n; ++i )
461 const gp_XYZ& aSectPoint = thePoints.Value( i );
463 thePath.lineTo( aSectPoint.X(), aSectPoint.Y() );
467 thePath.closeSubpath();
471 Handle(Geom_BSplineCurve) aCurve =
472 HYDROData_BSplineOperation::ComputeCurve( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
473 HYDROData_BSplineOperation::ComputePath( aCurve, thePath );
477 void HYDROData_PolylineXY::Update()
481 // If polyline is not editable we no need to update it wire
486 HYDROData_IPolyline::Update();
488 NCollection_Sequence<TCollection_AsciiString> aSectNames;
489 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
490 NCollection_Sequence<bool> aSectClosures;
491 GetSections( aSectNames, aSectTypes, aSectClosures );
493 //BRepBuilderAPI_MakeWire aMakeWire;
495 TopTools_ListOfShape aSectionWiresList;
497 for ( int aSectionId = 1, aNbSects = aSectNames.Size(); aSectionId <= aNbSects; aSectionId++ )
499 TCollection_AsciiString aSectName = aSectNames.Value( aSectionId );
500 SectionType aSectionType = aSectTypes.Value( aSectionId );
501 bool anIsSectionClosed = aSectClosures.Value( aSectionId );
503 PointsList aSectPointsList = GetPoints( aSectionId - 1 );
504 if ( aSectPointsList.IsEmpty() )
507 NCollection_Sequence<gp_XYZ> aPoints;
508 for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
510 const Point& aSectPoint = aSectPointsList.Value( i );
512 gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
513 aPoints.Append( aPoint );
516 TopoDS_Wire aSectionWire = BuildWire( aSectionType, anIsSectionClosed, aPoints );
517 if ( !aSectionWire.IsNull() ) {
518 aSectionWiresList.Append( aSectionWire );
519 //aMakeWire.Add( aSectionWire );
522 // all input wires in the <aSectionWiresList>
523 Handle(TopTools_HSequenceOfShape) aSeqWires = new TopTools_HSequenceOfShape;
524 Handle(TopTools_HSequenceOfShape) aSeqEdges = new TopTools_HSequenceOfShape;
525 TopTools_ListIteratorOfListOfShape it(aSectionWiresList);
526 for(;it.More();it.Next())
528 TopExp_Explorer it2(it.Value(), TopAbs_EDGE);
529 for(;it2.More();it2.Next())
530 aSeqEdges->Append(it2.Current());
534 TopoDS_Compound aCmp;
535 TopoDS_Shape aResult;
536 aBB.MakeCompound(aCmp);
537 if(aSeqEdges->Length() >1)
539 ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdges,1E-5,Standard_False,aSeqWires);
541 if( aSeqWires->Length()==1 )
542 aResult = aSeqWires->Value( 1 );
545 for (Standard_Integer i = 1; i <= aSeqWires->Length();i++)
547 const TopoDS_Shape& aS1 = aSeqWires->Value(i);
553 else if (aSeqEdges->Length() == 1)
555 BRepBuilderAPI_MakeWire mkWire (TopoDS::Edge(aSeqEdges->Value(1)));
557 aResult = mkWire.Wire();
563 bool HYDROData_PolylineXY::IsHas2dPrs() const
568 bool HYDROData_PolylineXY::IsEditable() const
570 return !myLab.IsAttribute( GUID_IS_UNEDITABLE );
573 void HYDROData_PolylineXY::setEditable( const bool theIsEditable )
575 if ( !theIsEditable )
576 TDataStd_UAttribute::Set( myLab, GUID_IS_UNEDITABLE );
578 myLab.ForgetAttribute( GUID_IS_UNEDITABLE );
582 * Returns true if polyline is closed
584 bool HYDROData_PolylineXY::IsClosed( const bool theIsSimpleCheck ) const
586 bool anIsClosed = false;
588 TopoDS_Shape aShape = GetShape();
589 if ( aShape.IsNull() )
592 TopTools_SequenceOfShape aWires;
593 HYDROData_ShapesTool::ExploreShapeToShapes( aShape, TopAbs_WIRE, aWires );
595 int aNbWires = aWires.Length();
596 if ( theIsSimpleCheck )
598 anIsClosed = aNbWires > 0;
599 for ( int i = 1; i <= aNbWires && anIsClosed; ++i )
601 const TopoDS_Shape& aWire = aWires.Value( i );
602 anIsClosed = BRep_Tool::IsClosed( aWire );
607 anIsClosed = aNbWires == 1 && BRep_Tool::IsClosed( aWires.First() );
613 double HYDROData_PolylineXY::GetDistance( const int theSectionIndex,
614 const int thePointIndex ) const
616 double aResDistance = -1;
617 if ( theSectionIndex < 0 || theSectionIndex >= NbSections() )
620 if ( thePointIndex == 0 )
623 SectionType aSectionType = GetSectionType( theSectionIndex );
624 bool anIsSectionClosed = IsClosedSection( theSectionIndex );
625 PointsList aSectPointsList = GetPoints( theSectionIndex );
626 if ( thePointIndex < 0 || thePointIndex >= aSectPointsList.Size() )
629 if ( aSectionType == SECTION_POLYLINE )
633 Point aPrevPoint = aSectPointsList.Value( 1 );
634 for ( int i = 2, aNbPoints = aSectPointsList.Size(); i <= aNbPoints; ++i )
636 const Point& aSectPoint = aSectPointsList.Value( i );
637 aResDistance += gp_Pnt2d( aPrevPoint ).Distance( aSectPoint );
638 aPrevPoint = aSectPoint;
640 if ( thePointIndex == i - 1 )
648 int aSectNbPoints = aSectPointsList.Size();
649 NCollection_Sequence<gp_XYZ> aPoints;
650 for( int i = 1 ; i <= aSectNbPoints; ++i )
652 const Point& aSectPoint = aSectPointsList.Value( i );
654 gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
655 aPoints.Append( aPoint );
657 if ( thePointIndex == i - 1 )
658 aPointToTest = aPoint;
661 Handle(Geom_BSplineCurve) aCurve =
662 HYDROData_BSplineOperation::ComputeCurve( aPoints, anIsSectionClosed, LOCAL_SELECTION_TOLERANCE );
664 Quantity_Parameter aFirstParam = aCurve->FirstParameter();
665 Quantity_Parameter aSecondParam = aCurve->LastParameter();
667 if ( thePointIndex != aSectNbPoints - 1 )
669 GeomAPI_ProjectPointOnCurve aProject( aPointToTest, aCurve );
670 aSecondParam = aProject.LowerDistanceParameter();
673 GeomAdaptor_Curve anAdap( aCurve );
675 aResDistance = GCPnts_AbscissaPoint::Length( anAdap, aFirstParam, aSecondParam );
681 int HYDROData_PolylineXY::NbSections() const
683 Handle(TDataStd_ExtStringList) aNamesList;
684 Handle(TDataStd_IntegerList) aTypesList;
685 Handle(TDataStd_BooleanList) aClosuresList;
686 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
688 return !aClosuresList.IsNull() ? aClosuresList->Extent() : 0;
691 void HYDROData_PolylineXY::AddSection( const TCollection_AsciiString& theSectName,
692 const SectionType theSectionType,
693 const bool theIsClosed )
695 Handle(TDataStd_ExtStringList) aNamesList;
696 Handle(TDataStd_IntegerList) aTypesList;
697 Handle(TDataStd_BooleanList) aClosuresList;
698 getSectionsLists( aNamesList, aTypesList, aClosuresList );
700 TCollection_ExtendedString aSectName( theSectName );
701 if ( aSectName.Length() <= 0 )
702 aSectName = getUniqueSectionName( aNamesList );
704 aNamesList->Append( aSectName );
705 aTypesList->Append( theSectionType );
706 aClosuresList->Append( theIsClosed );
711 TCollection_AsciiString HYDROData_PolylineXY::GetSectionName( const int theSectionIndex ) const
713 TCollection_AsciiString aResName;
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() )
722 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( aNamesList->List() );
723 for ( int i = 0; aNamesIter.More() && i != theSectionIndex; aNamesIter.Next(), ++i );
725 if ( aNamesIter.More() )
726 aResName = aNamesIter.Value();
731 void HYDROData_PolylineXY::SetSectionName( const int theSectionIndex,
732 const TCollection_AsciiString& theSectionName )
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() )
741 TDataStd_ListOfExtendedString anOldNamesList;
742 anOldNamesList = aNamesList->List();
744 // Refill the existing list
747 TCollection_ExtendedString aNewSectName = theSectionName;
749 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( anOldNamesList );
750 for ( int i = 0; aNamesIter.More(); aNamesIter.Next(), ++i )
751 aNamesList->Append( i == theSectionIndex ? aNewSectName : aNamesIter.Value() );
754 HYDROData_PolylineXY::SectionType HYDROData_PolylineXY::GetSectionType( const int theSectionIndex ) const
756 Handle(TDataStd_ExtStringList) aNamesList;
757 Handle(TDataStd_IntegerList) aTypesList;
758 Handle(TDataStd_BooleanList) aClosuresList;
759 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
760 if ( aTypesList.IsNull() || theSectionIndex >= aTypesList->Extent() )
761 return SECTION_POLYLINE;
763 TColStd_ListIteratorOfListOfInteger aTypesIter( aTypesList->List() );
764 for ( int i = 0; aTypesIter.More() && i != theSectionIndex; aTypesIter.Next(), ++i );
766 return aTypesIter.More() ? (SectionType)aTypesIter.Value() : SECTION_POLYLINE;
769 void HYDROData_PolylineXY::SetSectionType( const int theSectionIndex,
770 const SectionType theSectionType )
772 Handle(TDataStd_ExtStringList) aNamesList;
773 Handle(TDataStd_IntegerList) aTypesList;
774 Handle(TDataStd_BooleanList) aClosuresList;
775 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
776 if ( aTypesList.IsNull() || theSectionIndex >= aTypesList->Extent() )
779 TColStd_ListOfInteger anOldTypesList;
780 anOldTypesList = aTypesList->List();
782 // Refill the existing list
785 TColStd_ListIteratorOfListOfInteger aTypesIter( anOldTypesList );
786 for ( int i = 0; aTypesIter.More(); aTypesIter.Next(), ++i )
787 aTypesList->Append( i == theSectionIndex ? theSectionType : aTypesIter.Value() );
792 bool HYDROData_PolylineXY::IsClosedSection( const int theSectionIndex ) const
794 Handle(TDataStd_ExtStringList) aNamesList;
795 Handle(TDataStd_IntegerList) aTypesList;
796 Handle(TDataStd_BooleanList) aClosuresList;
797 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
798 if ( aClosuresList.IsNull() || theSectionIndex >= aClosuresList->Extent() )
801 TDataStd_ListIteratorOfListOfByte aClosuresIter( aClosuresList->List() );
802 for ( int i = 0; aClosuresIter.More() && i != theSectionIndex; aClosuresIter.Next(), ++i );
804 return aClosuresIter.More() ? (bool)aClosuresIter.Value() : false;
807 void HYDROData_PolylineXY::SetSectionClosed( const int theSectionIndex,
808 const bool theIsClosed )
810 Handle(TDataStd_ExtStringList) aNamesList;
811 Handle(TDataStd_IntegerList) aTypesList;
812 Handle(TDataStd_BooleanList) aClosuresList;
813 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
814 if ( aClosuresList.IsNull() || theSectionIndex >= aClosuresList->Extent() )
817 TDataStd_ListOfByte anOldClosuresList;
818 anOldClosuresList = aClosuresList->List();
820 // Refill the existing list
821 aClosuresList->Clear();
823 TDataStd_ListIteratorOfListOfByte aClosuresIter( anOldClosuresList );
824 for ( int i = 0; aClosuresIter.More(); aClosuresIter.Next(), ++i )
825 aClosuresList->Append( i == theSectionIndex ? theIsClosed : (bool)aClosuresIter.Value() );
830 void HYDROData_PolylineXY::GetSections( NCollection_Sequence<TCollection_AsciiString>& theSectNames,
831 NCollection_Sequence<SectionType>& theSectTypes,
832 NCollection_Sequence<bool>& theSectClosures ) const
834 theSectNames.Clear();
835 theSectTypes.Clear();
836 theSectClosures.Clear();
838 Handle(TDataStd_ExtStringList) aNamesList;
839 Handle(TDataStd_IntegerList) aTypesList;
840 Handle(TDataStd_BooleanList) aClosuresList;
841 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
842 if ( aNamesList.IsNull() || aTypesList.IsNull() || aClosuresList.IsNull() )
845 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( aNamesList->List() );
846 TColStd_ListIteratorOfListOfInteger aTypesIter( aTypesList->List() );
847 TDataStd_ListIteratorOfListOfByte aClosuresIter( aClosuresList->List() );
848 for ( ; aNamesIter.More() && aTypesIter.More() && aClosuresIter.More();
849 aNamesIter.Next(), aTypesIter.Next(), aClosuresIter.Next() )
851 const TCollection_ExtendedString& aSectName = aNamesIter.Value();
852 SectionType aSectType = (SectionType)aTypesIter.Value();
853 bool aSectClosures = aClosuresIter.Value();
855 theSectNames.Append( aSectName );
856 theSectTypes.Append( aSectType );
857 theSectClosures.Append( aSectClosures );
861 void HYDROData_PolylineXY::RemoveSection( const int theSectionIndex )
863 Handle(TDataStd_ExtStringList) aNamesList;
864 Handle(TDataStd_IntegerList) aTypesList;
865 Handle(TDataStd_BooleanList) aClosuresList;
866 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
867 if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
870 if ( aNamesList->Extent() == 1 )
872 removeSectionsLists();
877 TDataStd_ListOfExtendedString anOldNamesList;
878 anOldNamesList = aNamesList->List();
880 TColStd_ListOfInteger anOldTypesList;
881 anOldTypesList = aTypesList->List();
883 TDataStd_ListOfByte anOldClosuresList;
884 anOldClosuresList = aClosuresList->List();
886 // Refill the existing lists
889 aClosuresList->Clear();
891 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( anOldNamesList );
892 TColStd_ListIteratorOfListOfInteger aTypesIter( anOldTypesList );
893 TDataStd_ListIteratorOfListOfByte aClosuresIter( anOldClosuresList );
894 for ( int i = 0; aNamesIter.More() && aTypesIter.More() && aClosuresIter.More();
895 aNamesIter.Next(), aTypesIter.Next(), aClosuresIter.Next(), ++i )
897 if ( i == theSectionIndex )
898 continue; // skip index to remove
900 aNamesList->Append( aNamesIter.Value() );
901 aTypesList->Append( aTypesIter.Value() );
902 aClosuresList->Append( (bool)aClosuresIter.Value() );
905 // Remove points that belongs to removed section
906 removePointsLists( theSectionIndex );
912 void HYDROData_PolylineXY::RemoveSections()
914 removeSectionsLists();
919 void HYDROData_PolylineXY::AddPoint( const int theSectionIndex,
920 const Point& thePoint,
921 const int thePointIndex )
923 Handle(TDataStd_RealList) aListX, aListY;
924 getPointsLists( theSectionIndex, aListX, aListY );
926 if ( thePointIndex < 0 || thePointIndex >= aListX->Extent() )
928 aListX->Append( thePoint.X() );
929 aListY->Append( thePoint.Y() );
933 TColStd_ListOfReal anOldListX;
934 anOldListX = aListX->List();
936 TColStd_ListOfReal anOldListY;
937 anOldListY = aListY->List();
939 // Refill the existing lists
943 TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
944 TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
945 for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
947 double aCoordX = anIterX.Value();
948 double aCoordY = anIterY.Value();
950 if ( i == thePointIndex )
952 // Insert our new point
953 aListX->Append( thePoint.X() );
954 aListY->Append( thePoint.Y() );
957 aListX->Append( aCoordX );
958 aListY->Append( aCoordY );
965 void HYDROData_PolylineXY::SetPoint( const int theSectionIndex,
966 const Point& thePoint,
967 const int thePointIndex )
969 Handle(TDataStd_RealList) aListX, aListY;
970 getPointsLists( theSectionIndex, aListX, aListY );
972 if ( thePointIndex < 0 )
974 aListX->Prepend( thePoint.X() );
975 aListY->Prepend( thePoint.Y() );
977 else if ( thePointIndex >= aListX->Extent() )
979 aListX->Append( thePoint.X() );
980 aListY->Append( thePoint.Y() );
984 TColStd_ListOfReal anOldListX;
985 anOldListX = aListX->List();
987 TColStd_ListOfReal anOldListY;
988 anOldListY = aListY->List();
990 // Refill the existing lists
994 TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
995 TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
996 for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
998 double aCoordX = anIterX.Value();
999 double aCoordY = anIterY.Value();
1001 if ( i == thePointIndex )
1003 // Insert our new point instead of old one
1004 aCoordX = thePoint.X();
1005 aCoordY = thePoint.Y();
1008 aListX->Append( aCoordX );
1009 aListY->Append( aCoordY );
1016 void HYDROData_PolylineXY::SetPoints( const int theSectionIndex,
1017 const PointsList& thePoints )
1019 Handle(TDataStd_RealList) aListX, aListY;
1020 getPointsLists( theSectionIndex, aListX, aListY );
1025 for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
1027 const Point& aPoint = thePoints.Value( i );
1028 aListX->Append( aPoint.X() );
1029 aListY->Append( aPoint.Y() );
1033 void HYDROData_PolylineXY::RemovePoint( const int theSectionIndex,
1034 const int thePointIndex )
1036 Handle(TDataStd_RealList) aListX, aListY;
1037 getPointsLists( theSectionIndex, aListX, aListY, false );
1038 if ( aListX.IsNull() || aListY.IsNull() || aListX->IsEmpty() )
1041 if ( aListX->Extent() == 1 )
1043 removePointsLists( theSectionIndex );
1047 TColStd_ListOfReal anOldListX;
1048 anOldListX = aListX->List();
1050 TColStd_ListOfReal anOldListY;
1051 anOldListY = aListY->List();
1053 // Refill the existing lists
1057 TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
1058 TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
1059 for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
1061 if ( i == thePointIndex )
1062 continue; // skip index to remove
1064 aListX->Append( anIterX.Value() );
1065 aListY->Append( anIterY.Value() );
1072 HYDROData_PolylineXY::PointsList HYDROData_PolylineXY::GetPoints( const int theSectionIndex, bool IsConvertToGlobal ) const
1074 PointsList aResList;
1078 //TODO: make interpolation to fill the list
1081 Handle(TDataStd_RealList) aListX, aListY;
1082 getPointsLists( theSectionIndex, aListX, aListY, false );
1083 if ( aListX.IsNull() || aListY.IsNull() || aListX->IsEmpty() )
1086 TColStd_ListIteratorOfListOfReal anIterX( aListX->List() );
1087 TColStd_ListIteratorOfListOfReal anIterY( aListY->List() );
1088 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( myLab );
1089 for ( ; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next() )
1091 Point aPoint( anIterX.Value(), anIterY.Value() );
1092 if (IsConvertToGlobal)
1093 aDoc->Transform( aPoint, false );
1094 aResList.Append( aPoint );
1100 QPainterPath HYDROData_PolylineXY::GetPainterPath() const
1104 NCollection_Sequence<TCollection_AsciiString> aSectNames;
1105 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1106 NCollection_Sequence<bool> aSectClosures;
1107 GetSections( aSectNames, aSectTypes, aSectClosures );
1109 for ( int aSectionId = 1, aNbSects = aSectNames.Size(); aSectionId <= aNbSects; aSectionId++ )
1111 TCollection_AsciiString aSectName = aSectNames.Value( aSectionId );
1112 SectionType aSectionType = aSectTypes.Value( aSectionId );
1113 bool anIsSectionClosed = aSectClosures.Value( aSectionId );
1115 PointsList aSectPointsList = GetPoints( aSectionId - 1 );
1116 if ( aSectPointsList.IsEmpty() )
1119 NCollection_Sequence<gp_XYZ> aPoints;
1120 for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
1122 const Point& aSectPoint = aSectPointsList.Value( i );
1124 gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
1125 aPoints.Append( aPoint );
1128 BuildPainterPath( aPath, aSectionType, anIsSectionClosed, aPoints );
1134 void HYDROData_PolylineXY::UpdateLocalCS( double theDx, double theDy )
1136 NCollection_Sequence<TCollection_AsciiString> aSectNames;
1137 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1138 NCollection_Sequence<bool> aSectClosures;
1139 GetSections( aSectNames, aSectTypes, aSectClosures );
1141 gp_XY aDelta( theDx, theDy );
1142 for ( int i = 0, aNbSects = aSectNames.Size(); i < aNbSects; i++ )
1144 PointsList aPoints = GetPoints( i );
1145 for( int j = 1, n = aPoints.Size(); j <= n; ++j )
1147 Point& aPoint = aPoints.ChangeValue( j );
1150 SetPoints( i, aPoints );
1155 void HYDROData_PolylineXY::Transform( const QTransform& theTrsf )
1157 NCollection_Sequence<TCollection_AsciiString> aSectNames;
1158 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1159 NCollection_Sequence<bool> aSectClosures;
1160 GetSections( aSectNames, aSectTypes, aSectClosures );
1162 for ( int i = 0, aNbSects = aSectNames.Size(); i < aNbSects; i++ ) {
1163 PointsList aPoints = GetPoints( i );
1164 for( int j = 1, n = aPoints.Size(); j <= n; ++j ) {
1165 Point& aPoint = aPoints.ChangeValue( j );
1167 QPointF aTrsfPoint = theTrsf.map( QPointF( aPoint.X(), aPoint.Y() ) );
1169 aPoint.SetX( aTrsfPoint.x() );
1170 aPoint.SetY( aTrsfPoint.y() );
1172 SetPoints( i, aPoints );
1178 bool HYDROData_PolylineXY::IsCustom() const
1180 bool isNull = GetShape().IsNull();
1183 //TODO: to check if there is no points
1184 //for( int i=0, n=NbSections(); i<n; i++ )
1185 // aNbPoints += NbPoints( i );
1187 return !isNull && aNbPoints == 0;