1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROData_PolylineXY.h"
21 #include "HYDROData_BSplineOperation.h"
22 #include "HYDROData_Document.h"
23 #include "HYDROData_ShapesTool.h"
24 #include "HYDROData_Tool.h"
26 #include <BRep_Builder.hxx>
27 #include <BRepBuilderAPI_MakeEdge.hxx>
28 #include <BRepBuilderAPI_MakeWire.hxx>
29 #include <BRepBuilderAPI_MakePolygon.hxx>
30 #include <BRepBuilderAPI_MakeFace.hxx>
31 #include <BRepOffsetAPI_NormalProjection.hxx>
32 #include <BRepAdaptor_Curve.hxx>
38 #include <GeomAPI_ProjectPointOnCurve.hxx>
39 #include <GeomAdaptor_Curve.hxx>
40 #include <Geom_Line.hxx>
41 #include <Geom_BSplineCurve.hxx>
43 #include <GCPnts_AbscissaPoint.hxx>
44 #include <GCPnts_QuasiUniformDeflection.hxx>
46 #include <ImageComposer_MetaTypes.h>
52 #include <NCollection_Map.hxx>
54 #include <TCollection_ExtendedString.hxx>
56 #include <TDataStd_ListIteratorOfListOfByte.hxx>
57 #include <TColStd_ListIteratorOfListOfInteger.hxx>
58 #include <TColStd_ListIteratorOfListOfReal.hxx>
60 #include <TColStd_Array1OfReal.hxx>
61 #include <TColgp_Array1OfPnt.hxx>
63 #include <TDataStd_AsciiString.hxx>
64 #include <TDataStd_BooleanList.hxx>
65 #include <TDataStd_ExtStringList.hxx>
66 #include <TDataStd_IntegerList.hxx>
67 #include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
68 #include <TDataStd_RealList.hxx>
69 #include <TDataStd_UAttribute.hxx>
71 #include <TopoDS_Iterator.hxx>
72 #include <TopTools_ListIteratorOfListOfShape.hxx>
73 #include <TopTools_HSequenceOfShape.hxx>
74 #include <TopExp_Explorer.hxx>
75 #include <ShapeAnalysis_FreeBounds.hxx>
79 #include <QPainterPath>
82 static const Standard_GUID GUID_IS_UNEDITABLE("e5799736-9030-4051-91a4-2e58321fa153");
84 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
86 TCollection_AsciiString getUniqueSectionName( const NCollection_Sequence<TCollection_AsciiString>& theNamesSeq )
88 NCollection_Map<TCollection_AsciiString> aNamesMap;
90 for ( int i = 1, n = theNamesSeq.Size(); i <= n; ++i )
92 const TCollection_AsciiString& aSectName = theNamesSeq.Value( i );
93 aNamesMap.Add( aSectName );
96 TCollection_AsciiString aResName;
101 aResName = TCollection_AsciiString( "Section_" ) + aPrefIdx;
104 while ( aNamesMap.Contains( aResName ) );
109 TCollection_AsciiString getUniqueSectionName( const Handle(TDataStd_ExtStringList)& theNamesList )
111 NCollection_Sequence<TCollection_AsciiString> aNamesSeq;
113 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( theNamesList->List() );
114 for ( ; aNamesIter.More(); aNamesIter.Next() )
115 aNamesSeq.Append( aNamesIter.Value() );
117 return getUniqueSectionName( aNamesSeq );
120 IMPLEMENT_STANDARD_HANDLE(HYDROData_PolylineXY, HYDROData_IPolyline)
121 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_PolylineXY, HYDROData_IPolyline)
123 HYDROData_PolylineXY::HYDROData_PolylineXY()
124 : HYDROData_IPolyline(),
125 myIsInCustomFlag( false )
129 HYDROData_PolylineXY::~HYDROData_PolylineXY()
133 QStringList HYDROData_PolylineXY::DumpToPython( const QString& thePyScriptPath,
134 MapOfTreatedObjects& theTreatedObjects ) const
136 QStringList aResList = dumpObjectCreation( theTreatedObjects );
137 QString aPolylineName = GetObjPyName();
139 // Set the wire color
140 QStringList aWireColorDef;
142 QColor aWireColor = GetWireColor();
143 setPythonObjectColor( aWireColorDef, aWireColor, DefaultWireColor(), "SetWireColor" );
145 if ( !aWireColorDef.isEmpty() )
147 aResList << aWireColorDef;
148 aResList << QString( "" );
151 bool anIsEditable = IsEditable();
154 // If polyline is not editable we try to import the shape from geom
155 TCollection_AsciiString aGeomObjectEntry = GetGeomObjectEntry();
156 if ( !aGeomObjectEntry.IsEmpty() )
158 QString aSalomeObjName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "polyline_sobj" );
159 aResList << QString( "%1 = salome.myStudy.FindObjectID( \"%2\" );" )
160 .arg( aSalomeObjName ).arg( aGeomObjectEntry.ToCString() );
162 aResList << QString( "%1.ImportFromGeomIOR( %2.GetIOR() );" )
163 .arg( aPolylineName ).arg( aSalomeObjName );
165 aResList << QString( "%1.SetGeomObjectEntry( \"%2\" );" )
166 .arg( aPolylineName ).arg( aGeomObjectEntry.ToCString() );
172 NCollection_Sequence<TCollection_AsciiString> aSectNames;
173 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
174 NCollection_Sequence<bool> aSectClosures;
175 GetSections( aSectNames, aSectTypes, aSectClosures );
177 for ( int i = 1, n = aSectNames.Size(); i <= n; ++i )
179 const TCollection_AsciiString& aSectName = aSectNames.Value( i );
180 const SectionType& aSectType = aSectTypes.Value( i );
181 bool aSectClosure = aSectClosures.Value( i );
183 aResList << QString( "%1.AddSection( \"%2\", %3, %4 );" ).arg( aPolylineName )
184 .arg( aSectName.ToCString() ).arg( aSectType ).arg( aSectClosure );
186 HYDROData_IPolyline::PointsList aSectPointsList = GetPoints( i - 1 );
187 for ( int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k )
189 const Point& aSectPoint = aSectPointsList.Value( k );
191 QString anXStr = QString::number( aSectPoint.X(), 'f', 2 );
192 QString anYStr = QString::number( aSectPoint.Y(), 'f', 2 );
193 aResList << QString( "%1.AddPoint( %2, gp_XY( %3, %4 ) );" ).arg( aPolylineName )
194 .arg( i - 1 ).arg( anXStr ).arg( anYStr );
198 aResList << QString( "" );
199 aResList << QString( "%1.Update();" ).arg( aPolylineName );
200 aResList << QString( "" );
205 QVariant HYDROData_PolylineXY::GetDataVariant()
207 QPainterPath aPath = GetPainterPath();
210 aVarData.setValue<QPainterPath>( aPath );
215 QColor HYDROData_PolylineXY::DefaultWireColor()
217 return QColor( Qt::red );
220 bool HYDROData_PolylineXY::ImportFromGeomIOR( const TCollection_AsciiString& theIOR )
225 if ( theIOR.IsEmpty() )
228 TopoDS_Shape aShape = GEOMBase::GetShapeFromIOR( theIOR.ToCString() );
229 if ( aShape.IsNull() )
232 return ImportShape( aShape );
236 void HYDROData_PolylineXY::SetGeomObjectEntry( const TCollection_AsciiString& theEntry )
238 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_GeomObjectEntry ), theEntry );
241 TCollection_AsciiString HYDROData_PolylineXY::GetGeomObjectEntry() const
243 TCollection_AsciiString aRes;
245 TDF_Label aLabel = myLab.FindChild( DataTag_GeomObjectEntry, false );
246 if ( !aLabel.IsNull() )
248 Handle(TDataStd_AsciiString) anAsciiStr;
249 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
250 aRes = anAsciiStr->Get();
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,
262 bool IsInterpolationAllowed,
263 double theDeflection )
265 Standard_Real aFirst = 0.0, aLast = 0.0;
266 Handle(Geom_Curve) anEdgeGeomCurve = BRep_Tool::Curve( theEdge, aFirst, aLast );
267 if ( anEdgeGeomCurve.IsNull() )
270 TCollection_AsciiString aSectName = getUniqueSectionName( theSectNames );
271 bool anIsEdgeClosed = anEdgeGeomCurve->IsClosed();
273 HYDROData_PolylineXY::SectionType aSectionType = HYDROData_PolylineXY::SECTION_POLYLINE;
274 HYDROData_PolylineXY::PointsList aPointsList;
276 if( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_Line) ) )
278 Handle(Geom_Line) aGeomLine = Handle(Geom_Line)::DownCast( anEdgeGeomCurve );
280 gp_Pnt aFirstPoint, aLastPoint;
281 aGeomLine->D0( aFirst, aFirstPoint );
282 aGeomLine->D0( aLast, aLastPoint );
284 HYDROData_PolylineXY::Point aSectFirstPoint( aFirstPoint.X(), aFirstPoint.Y() );
285 aPointsList.Append( aSectFirstPoint );
287 HYDROData_PolylineXY::Point aSectLastPoint( aLastPoint.X(), aLastPoint.Y() );
288 aPointsList.Append( aSectLastPoint );
290 else if ( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) || IsInterpolationAllowed )
292 aSectionType = HYDROData_PolylineXY::SECTION_SPLINE;
294 BRepAdaptor_Curve anAdaptorCurve( theEdge );
295 GCPnts_QuasiUniformDeflection aDiscrete( anAdaptorCurve, theDeflection );
297 int aNbPoints = aDiscrete.NbPoints();
299 // Decrease the number of imported poles because of last one
300 // pole is the closing point which are the start point
301 if ( anIsEdgeClosed ) aNbPoints--;
303 for ( int i = 1; i <= aNbPoints; ++i )
305 const gp_Pnt& aPoint = aDiscrete.Value( i );
307 HYDROData_PolylineXY::Point aSectPoint( aPoint.X(), aPoint.Y() );
308 aPointsList.Append( aSectPoint );
313 // Other curve types are not supported
317 if ( aPointsList.IsEmpty() )
320 theSectNames.Append( aSectName );
321 theSectTypes.Append( aSectionType );
322 theSectClosures.Append( anIsEdgeClosed );
323 theSectPoints.Append( aPointsList );
328 bool HYDROData_PolylineXY::ImportShape( const TopoDS_Shape& theShape,
329 bool IsInterpolationAllowed,
330 double theDeviation )
332 if ( theShape.IsNull() )
337 bool anIsCanBeImported = false;
339 NCollection_Sequence<TCollection_AsciiString> aSectNames;
340 NCollection_Sequence<SectionType> aSectTypes;
341 NCollection_Sequence<bool> aSectClosures;
342 NCollection_Sequence<PointsList> aSectPoints;
344 if ( theShape.ShapeType() == TopAbs_EDGE )
346 TopoDS_Edge anEdge = TopoDS::Edge( theShape );
347 anIsCanBeImported = convertEdgeToSection( anEdge, aSectNames, aSectTypes,
348 aSectClosures, aSectPoints, true, IsInterpolationAllowed, theDeviation );
350 else if ( theShape.ShapeType() == TopAbs_WIRE )
352 TopTools_SequenceOfShape anEdges;
353 HYDROData_ShapesTool::ExploreShapeToShapes( theShape, TopAbs_EDGE, anEdges );
355 anIsCanBeImported = !anEdges.IsEmpty();
356 for ( int i = 1, n = anEdges.Length(); i <= n && anIsCanBeImported; ++i )
358 TopoDS_Edge aWireEdge = TopoDS::Edge( anEdges.Value( i ) );
359 anIsCanBeImported = convertEdgeToSection( aWireEdge, aSectNames, aSectTypes,
360 aSectClosures, aSectPoints, false, IsInterpolationAllowed, theDeviation );
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();
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
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();
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() );
759 HYDROData_PolylineXY::SectionType HYDROData_PolylineXY::GetSectionType( const int theSectionIndex ) const
761 Handle(TDataStd_ExtStringList) aNamesList;
762 Handle(TDataStd_IntegerList) aTypesList;
763 Handle(TDataStd_BooleanList) aClosuresList;
764 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
765 if ( aTypesList.IsNull() || theSectionIndex >= aTypesList->Extent() )
766 return SECTION_POLYLINE;
768 TColStd_ListIteratorOfListOfInteger aTypesIter( aTypesList->List() );
769 for ( int i = 0; aTypesIter.More() && i != theSectionIndex; aTypesIter.Next(), ++i );
771 return aTypesIter.More() ? (SectionType)aTypesIter.Value() : SECTION_POLYLINE;
774 void HYDROData_PolylineXY::SetSectionType( const int theSectionIndex,
775 const SectionType theSectionType )
777 Handle(TDataStd_ExtStringList) aNamesList;
778 Handle(TDataStd_IntegerList) aTypesList;
779 Handle(TDataStd_BooleanList) aClosuresList;
780 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
781 if ( aTypesList.IsNull() || theSectionIndex >= aTypesList->Extent() )
784 TColStd_ListOfInteger anOldTypesList;
785 anOldTypesList = aTypesList->List();
787 // Refill the existing list
790 TColStd_ListIteratorOfListOfInteger aTypesIter( anOldTypesList );
791 for ( int i = 0; aTypesIter.More(); aTypesIter.Next(), ++i )
792 aTypesList->Append( i == theSectionIndex ? theSectionType : aTypesIter.Value() );
797 bool HYDROData_PolylineXY::IsClosedSection( const int theSectionIndex ) const
799 Handle(TDataStd_ExtStringList) aNamesList;
800 Handle(TDataStd_IntegerList) aTypesList;
801 Handle(TDataStd_BooleanList) aClosuresList;
802 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
803 if ( aClosuresList.IsNull() || theSectionIndex >= aClosuresList->Extent() )
806 TDataStd_ListIteratorOfListOfByte aClosuresIter( aClosuresList->List() );
807 for ( int i = 0; aClosuresIter.More() && i != theSectionIndex; aClosuresIter.Next(), ++i );
809 return aClosuresIter.More() ? (bool)aClosuresIter.Value() : false;
812 void HYDROData_PolylineXY::SetSectionClosed( const int theSectionIndex,
813 const bool theIsClosed )
815 Handle(TDataStd_ExtStringList) aNamesList;
816 Handle(TDataStd_IntegerList) aTypesList;
817 Handle(TDataStd_BooleanList) aClosuresList;
818 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
819 if ( aClosuresList.IsNull() || theSectionIndex >= aClosuresList->Extent() )
822 TDataStd_ListOfByte anOldClosuresList;
823 anOldClosuresList = aClosuresList->List();
825 // Refill the existing list
826 aClosuresList->Clear();
828 TDataStd_ListIteratorOfListOfByte aClosuresIter( anOldClosuresList );
829 for ( int i = 0; aClosuresIter.More(); aClosuresIter.Next(), ++i )
830 aClosuresList->Append( i == theSectionIndex ? theIsClosed : (bool)aClosuresIter.Value() );
835 void HYDROData_PolylineXY::GetSections( NCollection_Sequence<TCollection_AsciiString>& theSectNames,
836 NCollection_Sequence<SectionType>& theSectTypes,
837 NCollection_Sequence<bool>& theSectClosures ) const
839 theSectNames.Clear();
840 theSectTypes.Clear();
841 theSectClosures.Clear();
845 const_cast<HYDROData_PolylineXY*>( this )->Interpolate();
848 Handle(TDataStd_ExtStringList) aNamesList;
849 Handle(TDataStd_IntegerList) aTypesList;
850 Handle(TDataStd_BooleanList) aClosuresList;
851 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
852 if ( aNamesList.IsNull() || aTypesList.IsNull() || aClosuresList.IsNull() )
855 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( aNamesList->List() );
856 TColStd_ListIteratorOfListOfInteger aTypesIter( aTypesList->List() );
857 TDataStd_ListIteratorOfListOfByte aClosuresIter( aClosuresList->List() );
858 for ( ; aNamesIter.More() && aTypesIter.More() && aClosuresIter.More();
859 aNamesIter.Next(), aTypesIter.Next(), aClosuresIter.Next() )
861 const TCollection_ExtendedString& aSectName = aNamesIter.Value();
862 SectionType aSectType = (SectionType)aTypesIter.Value();
863 bool aSectClosures = aClosuresIter.Value();
865 theSectNames.Append( aSectName );
866 theSectTypes.Append( aSectType );
867 theSectClosures.Append( aSectClosures );
871 void HYDROData_PolylineXY::RemoveSection( const int theSectionIndex )
873 Handle(TDataStd_ExtStringList) aNamesList;
874 Handle(TDataStd_IntegerList) aTypesList;
875 Handle(TDataStd_BooleanList) aClosuresList;
876 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
877 if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
880 if ( aNamesList->Extent() == 1 )
882 removeSectionsLists();
887 TDataStd_ListOfExtendedString anOldNamesList;
888 anOldNamesList = aNamesList->List();
890 TColStd_ListOfInteger anOldTypesList;
891 anOldTypesList = aTypesList->List();
893 TDataStd_ListOfByte anOldClosuresList;
894 anOldClosuresList = aClosuresList->List();
896 // Refill the existing lists
899 aClosuresList->Clear();
901 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( anOldNamesList );
902 TColStd_ListIteratorOfListOfInteger aTypesIter( anOldTypesList );
903 TDataStd_ListIteratorOfListOfByte aClosuresIter( anOldClosuresList );
904 for ( int i = 0; aNamesIter.More() && aTypesIter.More() && aClosuresIter.More();
905 aNamesIter.Next(), aTypesIter.Next(), aClosuresIter.Next(), ++i )
907 if ( i == theSectionIndex )
908 continue; // skip index to remove
910 aNamesList->Append( aNamesIter.Value() );
911 aTypesList->Append( aTypesIter.Value() );
912 aClosuresList->Append( (bool)aClosuresIter.Value() );
915 // Remove points that belongs to removed section
916 removePointsLists( theSectionIndex );
922 void HYDROData_PolylineXY::RemoveSections()
924 removeSectionsLists();
929 void HYDROData_PolylineXY::AddPoint( const int theSectionIndex,
930 const Point& thePoint,
931 const int thePointIndex )
933 Handle(TDataStd_RealList) aListX, aListY;
934 getPointsLists( theSectionIndex, aListX, aListY );
936 if ( thePointIndex < 0 || thePointIndex >= aListX->Extent() )
938 aListX->Append( thePoint.X() );
939 aListY->Append( thePoint.Y() );
943 TColStd_ListOfReal anOldListX;
944 anOldListX = aListX->List();
946 TColStd_ListOfReal anOldListY;
947 anOldListY = aListY->List();
949 // Refill the existing lists
953 TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
954 TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
955 for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
957 double aCoordX = anIterX.Value();
958 double aCoordY = anIterY.Value();
960 if ( i == thePointIndex )
962 // Insert our new point
963 aListX->Append( thePoint.X() );
964 aListY->Append( thePoint.Y() );
967 aListX->Append( aCoordX );
968 aListY->Append( aCoordY );
975 void HYDROData_PolylineXY::SetPoint( const int theSectionIndex,
976 const Point& thePoint,
977 const int thePointIndex )
979 Handle(TDataStd_RealList) aListX, aListY;
980 getPointsLists( theSectionIndex, aListX, aListY );
982 if ( thePointIndex < 0 )
984 aListX->Prepend( thePoint.X() );
985 aListY->Prepend( thePoint.Y() );
987 else if ( thePointIndex >= aListX->Extent() )
989 aListX->Append( thePoint.X() );
990 aListY->Append( thePoint.Y() );
994 TColStd_ListOfReal anOldListX;
995 anOldListX = aListX->List();
997 TColStd_ListOfReal anOldListY;
998 anOldListY = aListY->List();
1000 // Refill the existing lists
1004 TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
1005 TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
1006 for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
1008 double aCoordX = anIterX.Value();
1009 double aCoordY = anIterY.Value();
1011 if ( i == thePointIndex )
1013 // Insert our new point instead of old one
1014 aCoordX = thePoint.X();
1015 aCoordY = thePoint.Y();
1018 aListX->Append( aCoordX );
1019 aListY->Append( aCoordY );
1026 void HYDROData_PolylineXY::SetPoints( const int theSectionIndex,
1027 const PointsList& thePoints )
1029 Handle(TDataStd_RealList) aListX, aListY;
1030 getPointsLists( theSectionIndex, aListX, aListY );
1035 for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
1037 const Point& aPoint = thePoints.Value( i );
1038 aListX->Append( aPoint.X() );
1039 aListY->Append( aPoint.Y() );
1043 void HYDROData_PolylineXY::RemovePoint( const int theSectionIndex,
1044 const int thePointIndex )
1046 Handle(TDataStd_RealList) aListX, aListY;
1047 getPointsLists( theSectionIndex, aListX, aListY, false );
1048 if ( aListX.IsNull() || aListY.IsNull() || aListX->IsEmpty() )
1051 if ( aListX->Extent() == 1 )
1053 removePointsLists( theSectionIndex );
1057 TColStd_ListOfReal anOldListX;
1058 anOldListX = aListX->List();
1060 TColStd_ListOfReal anOldListY;
1061 anOldListY = aListY->List();
1063 // Refill the existing lists
1067 TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
1068 TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
1069 for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
1071 if ( i == thePointIndex )
1072 continue; // skip index to remove
1074 aListX->Append( anIterX.Value() );
1075 aListY->Append( anIterY.Value() );
1082 HYDROData_PolylineXY::PointsList HYDROData_PolylineXY::GetPoints( const int theSectionIndex, bool IsConvertToGlobal ) const
1084 PointsList aResList;
1088 const_cast<HYDROData_PolylineXY*>( this )->Interpolate();
1091 Handle(TDataStd_RealList) aListX, aListY;
1092 getPointsLists( theSectionIndex, aListX, aListY, false );
1093 if ( aListX.IsNull() || aListY.IsNull() || aListX->IsEmpty() )
1096 TColStd_ListIteratorOfListOfReal anIterX( aListX->List() );
1097 TColStd_ListIteratorOfListOfReal anIterY( aListY->List() );
1098 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( myLab );
1099 for ( ; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next() )
1101 Point aPoint( anIterX.Value(), anIterY.Value() );
1102 if (IsConvertToGlobal)
1103 aDoc->Transform( aPoint, false );
1104 aResList.Append( aPoint );
1110 QPainterPath HYDROData_PolylineXY::GetPainterPath() const
1114 NCollection_Sequence<TCollection_AsciiString> aSectNames;
1115 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1116 NCollection_Sequence<bool> aSectClosures;
1117 GetSections( aSectNames, aSectTypes, aSectClosures );
1119 for ( int aSectionId = 1, aNbSects = aSectNames.Size(); aSectionId <= aNbSects; aSectionId++ )
1121 TCollection_AsciiString aSectName = aSectNames.Value( aSectionId );
1122 SectionType aSectionType = aSectTypes.Value( aSectionId );
1123 bool anIsSectionClosed = aSectClosures.Value( aSectionId );
1125 PointsList aSectPointsList = GetPoints( aSectionId - 1 );
1126 if ( aSectPointsList.IsEmpty() )
1129 NCollection_Sequence<gp_XYZ> aPoints;
1130 for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
1132 const Point& aSectPoint = aSectPointsList.Value( i );
1134 gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
1135 aPoints.Append( aPoint );
1138 BuildPainterPath( aPath, aSectionType, anIsSectionClosed, aPoints );
1144 void HYDROData_PolylineXY::UpdateLocalCS( double theDx, double theDy )
1146 NCollection_Sequence<TCollection_AsciiString> aSectNames;
1147 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1148 NCollection_Sequence<bool> aSectClosures;
1149 GetSections( aSectNames, aSectTypes, aSectClosures );
1151 gp_XY aDelta( theDx, theDy );
1152 for ( int i = 0, aNbSects = aSectNames.Size(); i < aNbSects; i++ )
1154 PointsList aPoints = GetPoints( i );
1155 for( int j = 1, n = aPoints.Size(); j <= n; ++j )
1157 Point& aPoint = aPoints.ChangeValue( j );
1160 SetPoints( i, aPoints );
1165 void HYDROData_PolylineXY::Transform( const QTransform& theTrsf )
1167 NCollection_Sequence<TCollection_AsciiString> aSectNames;
1168 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1169 NCollection_Sequence<bool> aSectClosures;
1170 GetSections( aSectNames, aSectTypes, aSectClosures );
1172 for ( int i = 0, aNbSects = aSectNames.Size(); i < aNbSects; i++ ) {
1173 PointsList aPoints = GetPoints( i );
1174 for( int j = 1, n = aPoints.Size(); j <= n; ++j ) {
1175 Point& aPoint = aPoints.ChangeValue( j );
1177 QPointF aTrsfPoint = theTrsf.map( QPointF( aPoint.X(), aPoint.Y() ) );
1179 aPoint.SetX( aTrsfPoint.x() );
1180 aPoint.SetY( aTrsfPoint.y() );
1182 SetPoints( i, aPoints );
1188 bool HYDROData_PolylineXY::IsCustom() const
1190 if( myIsInCustomFlag )
1193 bool isNull = GetShape().IsNull();
1196 HYDROData_PolylineXY* aThat = const_cast<HYDROData_PolylineXY*>( this );
1197 aThat->myIsInCustomFlag = true;
1198 for( int i=0, n=NbSections(); i<n; i++ )
1199 aNbPoints += NbPoints( i );
1200 aThat->myIsInCustomFlag = false;
1202 return !isNull && aNbPoints == 0;
1205 void HYDROData_PolylineXY::Interpolate()
1207 ImportShape( GetShape(), true );