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 TopoDS_Shape HYDROData_PolylineXY::GetShape() const
253 return getPolylineShape();
256 bool HYDROData_PolylineXY::SetShape( const TopoDS_Shape& theShape )
258 setPolylineShape( theShape );
262 bool convertEdgeToSection( const TopoDS_Edge& theEdge,
263 NCollection_Sequence<TCollection_AsciiString>& theSectNames,
264 NCollection_Sequence<HYDROData_PolylineXY::SectionType>& theSectTypes,
265 NCollection_Sequence<bool>& theSectClosures,
266 NCollection_Sequence<HYDROData_PolylineXY::PointsList>& theSectPoints,
267 const bool theIsCanBeClosed )
269 Standard_Real aFirst = 0.0, aLast = 0.0;
270 Handle(Geom_Curve) anEdgeGeomCurve = BRep_Tool::Curve( theEdge, aFirst, aLast );
271 if ( anEdgeGeomCurve.IsNull() )
274 TCollection_AsciiString aSectName = getUniqueSectionName( theSectNames );
275 bool anIsEdgeClosed = anEdgeGeomCurve->IsClosed();
277 HYDROData_PolylineXY::SectionType aSectionType = HYDROData_PolylineXY::SECTION_POLYLINE;
278 HYDROData_PolylineXY::PointsList aPointsList;
280 if ( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_Line) ) )
282 Handle(Geom_Line) aGeomLine = Handle(Geom_Line)::DownCast( anEdgeGeomCurve );
284 gp_Pnt aFirstPoint, aLastPoint;
285 aGeomLine->D0( aFirst, aFirstPoint );
286 aGeomLine->D0( aLast, aLastPoint );
288 HYDROData_PolylineXY::Point aSectFirstPoint( aFirstPoint.X(), aFirstPoint.Y() );
289 aPointsList.Append( aSectFirstPoint );
291 HYDROData_PolylineXY::Point aSectLastPoint( aLastPoint.X(), aLastPoint.Y() );
292 aPointsList.Append( aSectLastPoint );
294 else if ( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) )
296 aSectionType = HYDROData_PolylineXY::SECTION_SPLINE;
298 Handle(Geom_BSplineCurve) aGeomSpline =
299 Handle(Geom_BSplineCurve)::DownCast( anEdgeGeomCurve );
301 int aNbKnots = aGeomSpline->NbKnots();
303 TColStd_Array1OfReal aSplineKnots( 1, aNbKnots );
304 aGeomSpline->Knots( aSplineKnots );
306 // Decrease the number of imported knots because of last one
307 // knot is the closing point which are the start point
308 if ( anIsEdgeClosed ) aNbKnots--;
310 for ( int i = 1; i <= aNbKnots; ++i )
312 const Standard_Real& aKnot = aSplineKnots.Value( i );
315 aGeomSpline->D0( aKnot, aPoint );
317 HYDROData_PolylineXY::Point aSectPoint( aPoint.X(), aPoint.Y() );
318 aPointsList.Append( aSectPoint );
323 // Other curve types are not supported
327 if ( aPointsList.IsEmpty() )
330 theSectNames.Append( aSectName );
331 theSectTypes.Append( aSectionType );
332 theSectClosures.Append( anIsEdgeClosed );
333 theSectPoints.Append( aPointsList );
338 bool HYDROData_PolylineXY::ImportShape( const TopoDS_Shape& theShape )
340 if ( theShape.IsNull() )
345 bool anIsCanBeImported = false;
347 NCollection_Sequence<TCollection_AsciiString> aSectNames;
348 NCollection_Sequence<SectionType> aSectTypes;
349 NCollection_Sequence<bool> aSectClosures;
350 NCollection_Sequence<PointsList> aSectPoints;
352 if ( theShape.ShapeType() == TopAbs_EDGE )
354 TopoDS_Edge anEdge = TopoDS::Edge( theShape );
355 anIsCanBeImported = convertEdgeToSection( anEdge, aSectNames, aSectTypes, aSectClosures, aSectPoints, true );
357 else if ( theShape.ShapeType() == TopAbs_WIRE )
359 TopTools_SequenceOfShape anEdges;
360 HYDROData_ShapesTool::ExploreShapeToShapes( theShape, TopAbs_EDGE, anEdges );
362 anIsCanBeImported = !anEdges.IsEmpty();
363 for ( int i = 1, n = anEdges.Length(); i <= n && anIsCanBeImported; ++i )
365 TopoDS_Edge aWireEdge = TopoDS::Edge( anEdges.Value( i ) );
366 anIsCanBeImported = convertEdgeToSection( aWireEdge, aSectNames, aSectTypes, aSectClosures, aSectPoints, false );
370 if ( anIsCanBeImported )
372 for ( int i = 1, n = aSectNames.Length(); i <= n; ++i )
374 const TCollection_AsciiString& aSectName = aSectNames.Value( i );
375 const SectionType& aSectType = aSectTypes.Value( i );
376 bool anIsSectionClosed = aSectClosures.Value( i );
377 const PointsList& aSectPointsList = aSectPoints( i );
379 AddSection( aSectName, aSectType, anIsSectionClosed );
380 SetPoints( i - 1, aSectPointsList );
385 TopoDS_Shape aShape = theShape;
387 if ( theShape.ShapeType() == TopAbs_EDGE )
389 // We make the wire from incoming edge because of other algorithms
390 // are waiting at least the wire from polyline
391 TopoDS_Edge anEdge = TopoDS::Edge( theShape );
392 BRepBuilderAPI_MakeWire aMakeWire( anEdge );
394 if ( aMakeWire.IsDone() )
395 aShape = aMakeWire.Wire();
398 gp_Pln aPlane( gp_Pnt( 0, 0, 0 ), gp_Dir( 0, 0, 1 ) );
399 BRepBuilderAPI_MakeFace aMakeFace( aPlane );
401 BRepOffsetAPI_NormalProjection aProj( aMakeFace.Face() );
404 TopoDS_Shape aResult;
406 aResult = aProj.Shape();
408 setPolylineShape( aResult );
411 setEditable( anIsCanBeImported );
416 TopoDS_Wire HYDROData_PolylineXY::BuildWire( const SectionType& theType,
417 const bool& theIsClosed,
418 const NCollection_Sequence<gp_XYZ>& thePoints )
421 if( theType == SECTION_POLYLINE )
423 int aNbPoints = thePoints.Length();
424 BRepBuilderAPI_MakePolygon aMakeWire;
425 for ( int i = 1, n = aNbPoints; i <= n ; ++i )
427 gp_XYZ aPoint = thePoints.Value( i );
428 gp_Pnt aPnt( aPoint.X(), aPoint.Y(), aPoint.Z() );
429 aMakeWire.Add( aPnt );
431 if( theIsClosed && ( aNbPoints > 2 ) )
434 if ( aMakeWire.IsDone() )
435 aWire = aMakeWire.Wire();
437 else //if( theType == PolylineSection::SECTION_SPLINE )
439 BRepBuilderAPI_MakeWire aMakeWire;
441 if ( thePoints.Size() > 1 )
443 Handle(Geom_BSplineCurve) aCurve =
444 HYDROData_BSplineOperation::ComputeCurve( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
446 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aCurve ).Edge();
447 aMakeWire.Add( anEdge );
450 if ( aMakeWire.IsDone() )
457 void HYDROData_PolylineXY::BuildPainterPath( QPainterPath& thePath,
458 const SectionType& theType,
459 const bool& theIsClosed,
460 const NCollection_Sequence<gp_XYZ>& thePoints )
462 if ( thePoints.IsEmpty() )
465 if ( theType == SECTION_POLYLINE )
467 const gp_XYZ& aFirstPoint = thePoints.Value( 1 );
468 thePath.moveTo( aFirstPoint.X(), aFirstPoint.Y() );
470 for( int i = 2, n = thePoints.Size(); i <= n; ++i )
472 const gp_XYZ& aSectPoint = thePoints.Value( i );
474 thePath.lineTo( aSectPoint.X(), aSectPoint.Y() );
478 thePath.closeSubpath();
482 Handle(Geom_BSplineCurve) aCurve =
483 HYDROData_BSplineOperation::ComputeCurve( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
484 HYDROData_BSplineOperation::ComputePath( aCurve, thePath );
488 void HYDROData_PolylineXY::Update()
492 // If polyline is not editable we no need to update it wire
493 SetToUpdate( false );
497 HYDROData_IPolyline::Update();
499 NCollection_Sequence<TCollection_AsciiString> aSectNames;
500 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
501 NCollection_Sequence<bool> aSectClosures;
502 GetSections( aSectNames, aSectTypes, aSectClosures );
504 //BRepBuilderAPI_MakeWire aMakeWire;
506 TopTools_ListOfShape aSectionWiresList;
508 for ( int aSectionId = 1, aNbSects = aSectNames.Size(); aSectionId <= aNbSects; aSectionId++ )
510 TCollection_AsciiString aSectName = aSectNames.Value( aSectionId );
511 SectionType aSectionType = aSectTypes.Value( aSectionId );
512 bool anIsSectionClosed = aSectClosures.Value( aSectionId );
514 PointsList aSectPointsList = GetPoints( aSectionId - 1 );
515 if ( aSectPointsList.IsEmpty() )
518 NCollection_Sequence<gp_XYZ> aPoints;
519 for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
521 const Point& aSectPoint = aSectPointsList.Value( i );
523 gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
524 aPoints.Append( aPoint );
527 TopoDS_Wire aSectionWire = BuildWire( aSectionType, anIsSectionClosed, aPoints );
528 if ( !aSectionWire.IsNull() ) {
529 aSectionWiresList.Append( aSectionWire );
530 //aMakeWire.Add( aSectionWire );
533 // all input wires in the <aSectionWiresList>
534 Handle(TopTools_HSequenceOfShape) aSeqWires = new TopTools_HSequenceOfShape;
535 Handle(TopTools_HSequenceOfShape) aSeqEdges = new TopTools_HSequenceOfShape;
536 TopTools_ListIteratorOfListOfShape it(aSectionWiresList);
537 for(;it.More();it.Next())
539 TopExp_Explorer it2(it.Value(), TopAbs_EDGE);
540 for(;it2.More();it2.Next())
541 aSeqEdges->Append(it2.Current());
545 TopoDS_Compound aCmp;
546 TopoDS_Shape aResult;
547 aBB.MakeCompound(aCmp);
548 if(aSeqEdges->Length() >1)
550 ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdges,1E-5,Standard_False,aSeqWires);
552 if( aSeqWires->Length()==1 )
553 aResult = aSeqWires->Value( 1 );
556 for (Standard_Integer i = 1; i <= aSeqWires->Length();i++)
558 const TopoDS_Shape& aS1 = aSeqWires->Value(i);
564 else if (aSeqEdges->Length() == 1)
566 BRepBuilderAPI_MakeWire mkWire (TopoDS::Edge(aSeqEdges->Value(1)));
568 aResult = mkWire.Wire();
571 setPolylineShape( aResult );
574 bool HYDROData_PolylineXY::IsHas2dPrs() const
579 bool HYDROData_PolylineXY::IsEditable() const
581 return !myLab.IsAttribute( GUID_IS_UNEDITABLE );
584 void HYDROData_PolylineXY::setEditable( const bool theIsEditable )
586 if ( !theIsEditable )
587 TDataStd_UAttribute::Set( myLab, GUID_IS_UNEDITABLE );
589 myLab.ForgetAttribute( GUID_IS_UNEDITABLE );
593 * Returns true if polyline is closed
595 bool HYDROData_PolylineXY::IsClosed( const bool theIsSimpleCheck ) const
597 bool anIsClosed = false;
599 TopoDS_Shape aShape = GetShape();
600 if ( aShape.IsNull() )
603 TopTools_SequenceOfShape aWires;
604 HYDROData_ShapesTool::ExploreShapeToShapes( aShape, TopAbs_WIRE, aWires );
606 int aNbWires = aWires.Length();
607 if ( theIsSimpleCheck )
609 anIsClosed = aNbWires > 0;
610 for ( int i = 1; i <= aNbWires && anIsClosed; ++i )
612 const TopoDS_Shape& aWire = aWires.Value( i );
613 anIsClosed = BRep_Tool::IsClosed( aWire );
618 anIsClosed = aNbWires == 1 && BRep_Tool::IsClosed( aWires.First() );
624 double HYDROData_PolylineXY::GetDistance( const int theSectionIndex,
625 const int thePointIndex ) const
627 double aResDistance = -1;
628 if ( theSectionIndex < 0 || theSectionIndex >= NbSections() )
631 if ( thePointIndex == 0 )
634 SectionType aSectionType = GetSectionType( theSectionIndex );
635 bool anIsSectionClosed = IsClosedSection( theSectionIndex );
636 PointsList aSectPointsList = GetPoints( theSectionIndex );
637 if ( thePointIndex < 0 || thePointIndex >= aSectPointsList.Size() )
640 if ( aSectionType == SECTION_POLYLINE )
644 Point aPrevPoint = aSectPointsList.Value( 1 );
645 for ( int i = 2, aNbPoints = aSectPointsList.Size(); i <= aNbPoints; ++i )
647 const Point& aSectPoint = aSectPointsList.Value( i );
648 aResDistance += gp_Pnt2d( aPrevPoint ).Distance( aSectPoint );
649 aPrevPoint = aSectPoint;
651 if ( thePointIndex == i - 1 )
659 int aSectNbPoints = aSectPointsList.Size();
660 NCollection_Sequence<gp_XYZ> aPoints;
661 for( int i = 1 ; i <= aSectNbPoints; ++i )
663 const Point& aSectPoint = aSectPointsList.Value( i );
665 gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
666 aPoints.Append( aPoint );
668 if ( thePointIndex == i - 1 )
669 aPointToTest = aPoint;
672 Handle(Geom_BSplineCurve) aCurve =
673 HYDROData_BSplineOperation::ComputeCurve( aPoints, anIsSectionClosed, LOCAL_SELECTION_TOLERANCE );
675 Quantity_Parameter aFirstParam = aCurve->FirstParameter();
676 Quantity_Parameter aSecondParam = aCurve->LastParameter();
678 if ( thePointIndex != aSectNbPoints - 1 )
680 GeomAPI_ProjectPointOnCurve aProject( aPointToTest, aCurve );
681 aSecondParam = aProject.LowerDistanceParameter();
684 GeomAdaptor_Curve anAdap( aCurve );
686 aResDistance = GCPnts_AbscissaPoint::Length( anAdap, aFirstParam, aSecondParam );
692 int HYDROData_PolylineXY::NbSections() const
694 Handle(TDataStd_ExtStringList) aNamesList;
695 Handle(TDataStd_IntegerList) aTypesList;
696 Handle(TDataStd_BooleanList) aClosuresList;
697 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
699 return !aClosuresList.IsNull() ? aClosuresList->Extent() : 0;
702 void HYDROData_PolylineXY::AddSection( const TCollection_AsciiString& theSectName,
703 const SectionType theSectionType,
704 const bool theIsClosed )
706 Handle(TDataStd_ExtStringList) aNamesList;
707 Handle(TDataStd_IntegerList) aTypesList;
708 Handle(TDataStd_BooleanList) aClosuresList;
709 getSectionsLists( aNamesList, aTypesList, aClosuresList );
711 TCollection_ExtendedString aSectName( theSectName );
712 if ( aSectName.Length() <= 0 )
713 aSectName = getUniqueSectionName( aNamesList );
715 aNamesList->Append( aSectName );
716 aTypesList->Append( theSectionType );
717 aClosuresList->Append( theIsClosed );
722 TCollection_AsciiString HYDROData_PolylineXY::GetSectionName( const int theSectionIndex ) const
724 TCollection_AsciiString aResName;
726 Handle(TDataStd_ExtStringList) aNamesList;
727 Handle(TDataStd_IntegerList) aTypesList;
728 Handle(TDataStd_BooleanList) aClosuresList;
729 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
730 if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
733 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( aNamesList->List() );
734 for ( int i = 0; aNamesIter.More() && i != theSectionIndex; aNamesIter.Next(), ++i );
736 if ( aNamesIter.More() )
737 aResName = aNamesIter.Value();
742 void HYDROData_PolylineXY::SetSectionName( const int theSectionIndex,
743 const TCollection_AsciiString& theSectionName )
745 Handle(TDataStd_ExtStringList) aNamesList;
746 Handle(TDataStd_IntegerList) aTypesList;
747 Handle(TDataStd_BooleanList) aClosuresList;
748 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
749 if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
752 TDataStd_ListOfExtendedString anOldNamesList;
753 anOldNamesList = aNamesList->List();
755 // Refill the existing list
758 TCollection_ExtendedString aNewSectName = theSectionName;
760 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( anOldNamesList );
761 for ( int i = 0; aNamesIter.More(); aNamesIter.Next(), ++i )
762 aNamesList->Append( i == theSectionIndex ? aNewSectName : aNamesIter.Value() );
767 HYDROData_PolylineXY::SectionType HYDROData_PolylineXY::GetSectionType( const int theSectionIndex ) const
769 Handle(TDataStd_ExtStringList) aNamesList;
770 Handle(TDataStd_IntegerList) aTypesList;
771 Handle(TDataStd_BooleanList) aClosuresList;
772 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
773 if ( aTypesList.IsNull() || theSectionIndex >= aTypesList->Extent() )
774 return SECTION_POLYLINE;
776 TColStd_ListIteratorOfListOfInteger aTypesIter( aTypesList->List() );
777 for ( int i = 0; aTypesIter.More() && i != theSectionIndex; aTypesIter.Next(), ++i );
779 return aTypesIter.More() ? (SectionType)aTypesIter.Value() : SECTION_POLYLINE;
782 void HYDROData_PolylineXY::SetSectionType( const int theSectionIndex,
783 const SectionType theSectionType )
785 Handle(TDataStd_ExtStringList) aNamesList;
786 Handle(TDataStd_IntegerList) aTypesList;
787 Handle(TDataStd_BooleanList) aClosuresList;
788 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
789 if ( aTypesList.IsNull() || theSectionIndex >= aTypesList->Extent() )
792 TColStd_ListOfInteger anOldTypesList;
793 anOldTypesList = aTypesList->List();
795 // Refill the existing list
798 TColStd_ListIteratorOfListOfInteger aTypesIter( anOldTypesList );
799 for ( int i = 0; aTypesIter.More(); aTypesIter.Next(), ++i )
800 aTypesList->Append( i == theSectionIndex ? theSectionType : aTypesIter.Value() );
805 bool HYDROData_PolylineXY::IsClosedSection( const int theSectionIndex ) const
807 Handle(TDataStd_ExtStringList) aNamesList;
808 Handle(TDataStd_IntegerList) aTypesList;
809 Handle(TDataStd_BooleanList) aClosuresList;
810 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
811 if ( aClosuresList.IsNull() || theSectionIndex >= aClosuresList->Extent() )
814 TDataStd_ListIteratorOfListOfByte aClosuresIter( aClosuresList->List() );
815 for ( int i = 0; aClosuresIter.More() && i != theSectionIndex; aClosuresIter.Next(), ++i );
817 return aClosuresIter.More() ? (bool)aClosuresIter.Value() : false;
820 void HYDROData_PolylineXY::SetSectionClosed( const int theSectionIndex,
821 const bool theIsClosed )
823 Handle(TDataStd_ExtStringList) aNamesList;
824 Handle(TDataStd_IntegerList) aTypesList;
825 Handle(TDataStd_BooleanList) aClosuresList;
826 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
827 if ( aClosuresList.IsNull() || theSectionIndex >= aClosuresList->Extent() )
830 TDataStd_ListOfByte anOldClosuresList;
831 anOldClosuresList = aClosuresList->List();
833 // Refill the existing list
834 aClosuresList->Clear();
836 TDataStd_ListIteratorOfListOfByte aClosuresIter( anOldClosuresList );
837 for ( int i = 0; aClosuresIter.More(); aClosuresIter.Next(), ++i )
838 aClosuresList->Append( i == theSectionIndex ? theIsClosed : (bool)aClosuresIter.Value() );
843 void HYDROData_PolylineXY::GetSections( NCollection_Sequence<TCollection_AsciiString>& theSectNames,
844 NCollection_Sequence<SectionType>& theSectTypes,
845 NCollection_Sequence<bool>& theSectClosures ) const
847 theSectNames.Clear();
848 theSectTypes.Clear();
849 theSectClosures.Clear();
851 Handle(TDataStd_ExtStringList) aNamesList;
852 Handle(TDataStd_IntegerList) aTypesList;
853 Handle(TDataStd_BooleanList) aClosuresList;
854 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
855 if ( aNamesList.IsNull() || aTypesList.IsNull() || aClosuresList.IsNull() )
858 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( aNamesList->List() );
859 TColStd_ListIteratorOfListOfInteger aTypesIter( aTypesList->List() );
860 TDataStd_ListIteratorOfListOfByte aClosuresIter( aClosuresList->List() );
861 for ( ; aNamesIter.More() && aTypesIter.More() && aClosuresIter.More();
862 aNamesIter.Next(), aTypesIter.Next(), aClosuresIter.Next() )
864 const TCollection_ExtendedString& aSectName = aNamesIter.Value();
865 SectionType aSectType = (SectionType)aTypesIter.Value();
866 bool aSectClosures = aClosuresIter.Value();
868 theSectNames.Append( aSectName );
869 theSectTypes.Append( aSectType );
870 theSectClosures.Append( aSectClosures );
874 void HYDROData_PolylineXY::RemoveSection( const int theSectionIndex )
876 Handle(TDataStd_ExtStringList) aNamesList;
877 Handle(TDataStd_IntegerList) aTypesList;
878 Handle(TDataStd_BooleanList) aClosuresList;
879 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
880 if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
883 if ( aNamesList->Extent() == 1 )
885 removeSectionsLists();
890 TDataStd_ListOfExtendedString anOldNamesList;
891 anOldNamesList = aNamesList->List();
893 TColStd_ListOfInteger anOldTypesList;
894 anOldTypesList = aTypesList->List();
896 TDataStd_ListOfByte anOldClosuresList;
897 anOldClosuresList = aClosuresList->List();
899 // Refill the existing lists
902 aClosuresList->Clear();
904 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( anOldNamesList );
905 TColStd_ListIteratorOfListOfInteger aTypesIter( anOldTypesList );
906 TDataStd_ListIteratorOfListOfByte aClosuresIter( anOldClosuresList );
907 for ( int i = 0; aNamesIter.More() && aTypesIter.More() && aClosuresIter.More();
908 aNamesIter.Next(), aTypesIter.Next(), aClosuresIter.Next(), ++i )
910 if ( i == theSectionIndex )
911 continue; // skip index to remove
913 aNamesList->Append( aNamesIter.Value() );
914 aTypesList->Append( aTypesIter.Value() );
915 aClosuresList->Append( (bool)aClosuresIter.Value() );
918 // Remove points that belongs to removed section
919 removePointsLists( theSectionIndex );
925 void HYDROData_PolylineXY::RemoveSections()
927 removeSectionsLists();
932 void HYDROData_PolylineXY::AddPoint( const int theSectionIndex,
933 const Point& thePoint,
934 const int thePointIndex )
936 Handle(TDataStd_RealList) aListX, aListY;
937 getPointsLists( theSectionIndex, aListX, aListY );
939 if ( thePointIndex < 0 || thePointIndex >= aListX->Extent() )
941 aListX->Append( thePoint.X() );
942 aListY->Append( thePoint.Y() );
946 TColStd_ListOfReal anOldListX;
947 anOldListX = aListX->List();
949 TColStd_ListOfReal anOldListY;
950 anOldListY = aListY->List();
952 // Refill the existing lists
956 TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
957 TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
958 for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
960 double aCoordX = anIterX.Value();
961 double aCoordY = anIterY.Value();
963 if ( i == thePointIndex )
965 // Insert our new point
966 aListX->Append( thePoint.X() );
967 aListY->Append( thePoint.Y() );
970 aListX->Append( aCoordX );
971 aListY->Append( aCoordY );
978 void HYDROData_PolylineXY::SetPoint( const int theSectionIndex,
979 const Point& thePoint,
980 const int thePointIndex )
982 Handle(TDataStd_RealList) aListX, aListY;
983 getPointsLists( theSectionIndex, aListX, aListY );
985 if ( thePointIndex < 0 )
987 aListX->Prepend( thePoint.X() );
988 aListY->Prepend( thePoint.Y() );
990 else if ( thePointIndex >= aListX->Extent() )
992 aListX->Append( thePoint.X() );
993 aListY->Append( thePoint.Y() );
997 TColStd_ListOfReal anOldListX;
998 anOldListX = aListX->List();
1000 TColStd_ListOfReal anOldListY;
1001 anOldListY = aListY->List();
1003 // Refill the existing lists
1007 TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
1008 TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
1009 for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
1011 double aCoordX = anIterX.Value();
1012 double aCoordY = anIterY.Value();
1014 if ( i == thePointIndex )
1016 // Insert our new point instead of old one
1017 aCoordX = thePoint.X();
1018 aCoordY = thePoint.Y();
1021 aListX->Append( aCoordX );
1022 aListY->Append( aCoordY );
1026 SetToUpdate( true );
1029 void HYDROData_PolylineXY::SetPoints( const int theSectionIndex,
1030 const PointsList& thePoints )
1032 Handle(TDataStd_RealList) aListX, aListY;
1033 getPointsLists( theSectionIndex, aListX, aListY );
1038 for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
1040 const Point& aPoint = thePoints.Value( i );
1041 aListX->Append( aPoint.X() );
1042 aListY->Append( aPoint.Y() );
1046 void HYDROData_PolylineXY::RemovePoint( const int theSectionIndex,
1047 const int thePointIndex )
1049 Handle(TDataStd_RealList) aListX, aListY;
1050 getPointsLists( theSectionIndex, aListX, aListY, false );
1051 if ( aListX.IsNull() || aListY.IsNull() || aListX->IsEmpty() )
1054 if ( aListX->Extent() == 1 )
1056 removePointsLists( theSectionIndex );
1060 TColStd_ListOfReal anOldListX;
1061 anOldListX = aListX->List();
1063 TColStd_ListOfReal anOldListY;
1064 anOldListY = aListY->List();
1066 // Refill the existing lists
1070 TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
1071 TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
1072 for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
1074 if ( i == thePointIndex )
1075 continue; // skip index to remove
1077 aListX->Append( anIterX.Value() );
1078 aListY->Append( anIterY.Value() );
1082 SetToUpdate( true );
1085 HYDROData_PolylineXY::PointsList HYDROData_PolylineXY::GetPoints( const int theSectionIndex, bool IsConvertToGlobal ) const
1087 PointsList aResList;
1091 //TODO: make interpolation to fill the list
1094 Handle(TDataStd_RealList) aListX, aListY;
1095 getPointsLists( theSectionIndex, aListX, aListY, false );
1096 if ( aListX.IsNull() || aListY.IsNull() || aListX->IsEmpty() )
1099 TColStd_ListIteratorOfListOfReal anIterX( aListX->List() );
1100 TColStd_ListIteratorOfListOfReal anIterY( aListY->List() );
1101 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( myLab );
1102 for ( ; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next() )
1104 Point aPoint( anIterX.Value(), anIterY.Value() );
1105 if (IsConvertToGlobal)
1106 aDoc->Transform( aPoint, false );
1107 aResList.Append( aPoint );
1113 QPainterPath HYDROData_PolylineXY::GetPainterPath() const
1117 NCollection_Sequence<TCollection_AsciiString> aSectNames;
1118 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1119 NCollection_Sequence<bool> aSectClosures;
1120 GetSections( aSectNames, aSectTypes, aSectClosures );
1122 for ( int aSectionId = 1, aNbSects = aSectNames.Size(); aSectionId <= aNbSects; aSectionId++ )
1124 TCollection_AsciiString aSectName = aSectNames.Value( aSectionId );
1125 SectionType aSectionType = aSectTypes.Value( aSectionId );
1126 bool anIsSectionClosed = aSectClosures.Value( aSectionId );
1128 PointsList aSectPointsList = GetPoints( aSectionId - 1 );
1129 if ( aSectPointsList.IsEmpty() )
1132 NCollection_Sequence<gp_XYZ> aPoints;
1133 for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
1135 const Point& aSectPoint = aSectPointsList.Value( i );
1137 gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
1138 aPoints.Append( aPoint );
1141 BuildPainterPath( aPath, aSectionType, anIsSectionClosed, aPoints );
1147 void HYDROData_PolylineXY::UpdateLocalCS( double theDx, double theDy )
1149 NCollection_Sequence<TCollection_AsciiString> aSectNames;
1150 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1151 NCollection_Sequence<bool> aSectClosures;
1152 GetSections( aSectNames, aSectTypes, aSectClosures );
1154 gp_XY aDelta( theDx, theDy );
1155 for ( int i = 0, aNbSects = aSectNames.Size(); i < aNbSects; i++ )
1157 PointsList aPoints = GetPoints( i );
1158 for( int j = 1, n = aPoints.Size(); j <= n; ++j )
1160 Point& aPoint = aPoints.ChangeValue( j );
1163 SetPoints( i, aPoints );
1165 SetToUpdate( true );
1168 void HYDROData_PolylineXY::Transform( const QTransform& theTrsf )
1170 NCollection_Sequence<TCollection_AsciiString> aSectNames;
1171 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1172 NCollection_Sequence<bool> aSectClosures;
1173 GetSections( aSectNames, aSectTypes, aSectClosures );
1175 for ( int i = 0, aNbSects = aSectNames.Size(); i < aNbSects; i++ ) {
1176 PointsList aPoints = GetPoints( i );
1177 for( int j = 1, n = aPoints.Size(); j <= n; ++j ) {
1178 Point& aPoint = aPoints.ChangeValue( j );
1180 QPointF aTrsfPoint = theTrsf.map( QPointF( aPoint.X(), aPoint.Y() ) );
1182 aPoint.SetX( aTrsfPoint.x() );
1183 aPoint.SetY( aTrsfPoint.y() );
1185 SetPoints( i, aPoints );
1191 bool HYDROData_PolylineXY::IsCustom() const
1193 bool isNull = getPolylineShape().IsNull();
1196 //TODO: to check if there is no points
1197 //for( int i=0, n=NbSections(); i<n; i++ )
1198 // aNbPoints += NbPoints( i );
1200 return !isNull && aNbPoints == 0;