2 #include "HYDROData_PolylineXY.h"
4 #include "HYDROData_BSplineOperation.h"
5 #include "HYDROData_Document.h"
6 #include "HYDROData_Tool.h"
8 #include <BRep_Builder.hxx>
9 #include <BRepBuilderAPI_MakeEdge.hxx>
10 #include <BRepBuilderAPI_MakeWire.hxx>
12 #include <ImageComposer_MetaTypes.h>
17 #include <NCollection_Map.hxx>
19 #include <TCollection_ExtendedString.hxx>
21 #include <TDataStd_ListIteratorOfListOfByte.hxx>
22 #include <TColStd_ListIteratorOfListOfInteger.hxx>
23 #include <TColStd_ListIteratorOfListOfReal.hxx>
25 #include <TDataStd_BooleanList.hxx>
26 #include <TDataStd_ExtStringList.hxx>
27 #include <TDataStd_IntegerList.hxx>
28 #include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
29 #include <TDataStd_RealList.hxx>
31 #include <TopTools_ListIteratorOfListOfShape.hxx>
33 #include <QPainterPath>
36 #define PYTHON_POLYLINEXY_ID "KIND_POLYLINEXY"
38 IMPLEMENT_STANDARD_HANDLE(HYDROData_PolylineXY, HYDROData_IPolyline)
39 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_PolylineXY, HYDROData_IPolyline)
41 HYDROData_PolylineXY::HYDROData_PolylineXY()
42 : HYDROData_IPolyline()
46 HYDROData_PolylineXY::~HYDROData_PolylineXY()
50 QStringList HYDROData_PolylineXY::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
54 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
55 if ( aDocument.IsNull() )
58 QString aDocName = aDocument->GetDocPyName();
59 QString aPolylineName = GetName();
61 aResList << QString( "%1 = %2.CreateObject( %3 );" )
62 .arg( aPolylineName ).arg( aDocName ).arg( PYTHON_POLYLINEXY_ID );
63 aResList << QString( "%1.SetName( \"%1\" );" ).arg( aPolylineName );
66 NCollection_Sequence<TCollection_AsciiString> aSectNames;
67 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
68 NCollection_Sequence<bool> aSectClosures;
69 GetSections( aSectNames, aSectTypes, aSectClosures );
71 for ( int i = 1, n = aSectNames.Size(); i <= n; ++i )
73 const TCollection_AsciiString& aSectName = aSectNames.Value( i );
74 const SectionType& aSectType = aSectTypes.Value( i );
75 bool aSectClosure = aSectClosures.Value( i );
77 aResList << QString( "%1.AddSection( \"%2\", %3, %4 );" ).arg( aPolylineName )
78 .arg( aSectName.ToCString() ).arg( aSectType ).arg( aSectClosure );
80 HYDROData_PolylineXY::PointsList aSectPointsList = GetPoints( i );
81 for ( int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k )
83 const HYDROData_PolylineXY::Point& aSectPoint = aSectPointsList.Value( k );
85 aResList << QString( "%1.AddPoint( %2, QPointF( %3, %4 ) );" ).arg( aPolylineName )
86 .arg( i - 1 ).arg( aSectPoint.X() ).arg( aSectPoint.Y() );
93 QVariant HYDROData_PolylineXY::GetDataVariant()
95 QPainterPath aPath = GetPainterPath();
98 aVarData.setValue<QPainterPath>( aPath );
103 TopoDS_Shape HYDROData_PolylineXY::GetShape()
105 return getPolylineShape();
108 void HYDROData_PolylineXY::Update()
110 NCollection_Sequence<TCollection_AsciiString> aSectNames;
111 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
112 NCollection_Sequence<bool> aSectClosures;
113 GetSections( aSectNames, aSectTypes, aSectClosures );
115 BRepBuilderAPI_MakeWire aMakeWire;
117 TopTools_ListOfShape aSectionWiresList;
119 for ( int aSectionId = 1, aNbSects = aSectNames.Size(); aSectionId <= aNbSects; aSectionId++ )
121 TCollection_AsciiString aSectName = aSectNames.Value( aSectionId );
122 SectionType aSectionType = aSectTypes.Value( aSectionId );
123 bool anIsSectionClosed = aSectClosures.Value( aSectionId );
125 PointsList aSectPointsList = GetPoints( aSectionId - 1 );
126 if ( aSectPointsList.IsEmpty() )
129 BRepBuilderAPI_MakeWire aMakeSectionWire;
130 if( aSectionType == SECTION_POLYLINE )
132 for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
134 const Point& aFirstPoint = aSectPointsList.Value( i );
139 if( anIsSectionClosed )
140 aLastPoint = aSectPointsList.Value( 1 );
146 aLastPoint = aSectPointsList.Value( i + 1 );
149 gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), 0.0 );
150 gp_Pnt aPnt2( aLastPoint.X(), aLastPoint.Y(), 0.0 );
152 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aPnt1, aPnt2 ).Edge();
153 aMakeSectionWire.Add( anEdge );
156 else //if( aSectionType == PolylineSection::SECTION_SPLINE )
158 QList<double> aPoints;
159 for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
161 const Point& aSectPoint = aSectPointsList.Value( i );
162 aPoints << aSectPoint.X() << aSectPoint.Y();
165 HYDROData_BSplineOperation aBSpline( aPoints, 0.0, anIsSectionClosed );
167 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSpline.Curve() ).Edge();
168 aMakeSectionWire.Add( anEdge );
171 TopoDS_Wire aSectionWire = aMakeSectionWire.Wire();
172 aSectionWiresList.Append( aSectionWire );
173 aMakeWire.Add( aSectionWire );
178 if ( aMakeWire.IsDone() )
180 aShape = aMakeWire.Shape();
182 else if ( !aSectionWiresList.IsEmpty() )
185 TopoDS_Compound aCompound;
187 BRep_Builder aBuilder;
188 aBuilder.MakeCompound( aCompound );
190 TopTools_ListIteratorOfListOfShape anIter( aSectionWiresList );
191 for ( ; anIter.More(); anIter.Next() )
193 aBuilder.Add( aCompound, anIter.Value() );
199 setPolylineShape( aShape );
203 * Returns true if polyline is closed
205 bool HYDROData_PolylineXY::IsClosed() const
207 NCollection_Sequence<TCollection_AsciiString> aSectNames;
208 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
209 NCollection_Sequence<bool> aSectClosures;
210 GetSections( aSectNames, aSectTypes, aSectClosures );
211 if( aSectNames.IsEmpty() )
214 bool anIsClosed = true;
215 for( int i = 1, n = aSectClosures.Size(); i <= n && anIsClosed; ++i )
216 anIsClosed = anIsClosed && aSectClosures.Value( i );
221 int HYDROData_PolylineXY::NbSections() const
223 Handle(TDataStd_ExtStringList) aNamesList;
224 Handle(TDataStd_IntegerList) aTypesList;
225 Handle(TDataStd_BooleanList) aClosuresList;
226 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
228 return !aClosuresList.IsNull() ? aClosuresList->Extent() : 0;
231 TCollection_ExtendedString getUniqueSectionName( const Handle(TDataStd_ExtStringList)& theNamesList )
233 NCollection_Map<TCollection_ExtendedString> aNamesMap;
235 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( theNamesList->List() );
236 for ( ; aNamesIter.More(); aNamesIter.Next() )
237 aNamesMap.Add( aNamesIter.Value() );
239 TCollection_ExtendedString aResName;
244 aResName = "Section_" + aPrefIdx;
247 while ( aNamesMap.Contains( aResName ) );
252 void HYDROData_PolylineXY::AddSection( const TCollection_AsciiString& theSectName,
253 const SectionType theSectionType,
254 const bool theIsClosed )
256 Handle(TDataStd_ExtStringList) aNamesList;
257 Handle(TDataStd_IntegerList) aTypesList;
258 Handle(TDataStd_BooleanList) aClosuresList;
259 getSectionsLists( aNamesList, aTypesList, aClosuresList );
261 TCollection_ExtendedString aSectName( theSectName );
262 if ( aSectName.Length() <= 0 )
263 aSectName = getUniqueSectionName( aNamesList );
265 aNamesList->Append( aSectName );
266 aTypesList->Append( theSectionType );
267 aClosuresList->Append( theIsClosed );
270 TCollection_AsciiString HYDROData_PolylineXY::GetSectionName( const int theSectionIndex ) const
272 TCollection_AsciiString aResName;
274 Handle(TDataStd_ExtStringList) aNamesList;
275 Handle(TDataStd_IntegerList) aTypesList;
276 Handle(TDataStd_BooleanList) aClosuresList;
277 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
278 if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
281 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( aNamesList->List() );
282 for ( int i = 0; aNamesIter.More() && i != theSectionIndex; aNamesIter.Next(), ++i );
284 if ( aNamesIter.More() )
285 aResName = aNamesIter.Value();
290 void HYDROData_PolylineXY::SetSectionName( const int theSectionIndex,
291 const TCollection_AsciiString& theSectionName )
293 Handle(TDataStd_ExtStringList) aNamesList;
294 Handle(TDataStd_IntegerList) aTypesList;
295 Handle(TDataStd_BooleanList) aClosuresList;
296 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
297 if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
300 TDataStd_ListOfExtendedString anOldNamesList;
301 anOldNamesList = aNamesList->List();
303 // Refill the existing list
306 TCollection_ExtendedString aNewSectName = theSectionName;
308 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( anOldNamesList );
309 for ( int i = 0; aNamesIter.More(); aNamesIter.Next(), ++i )
310 aNamesList->Append( i == theSectionIndex ? aNewSectName : aNamesIter.Value() );
313 HYDROData_PolylineXY::SectionType HYDROData_PolylineXY::GetSectionType( const int theSectionIndex ) const
315 Handle(TDataStd_ExtStringList) aNamesList;
316 Handle(TDataStd_IntegerList) aTypesList;
317 Handle(TDataStd_BooleanList) aClosuresList;
318 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
319 if ( aTypesList.IsNull() || theSectionIndex >= aTypesList->Extent() )
320 return SECTION_POLYLINE;
322 TColStd_ListIteratorOfListOfInteger aTypesIter( aTypesList->List() );
323 for ( int i = 0; aTypesIter.More() && i != theSectionIndex; aTypesIter.Next(), ++i );
325 return aTypesIter.More() ? (SectionType)aTypesIter.Value() : SECTION_POLYLINE;
328 void HYDROData_PolylineXY::SetSectionType( const int theSectionIndex,
329 const SectionType theSectionType )
331 Handle(TDataStd_ExtStringList) aNamesList;
332 Handle(TDataStd_IntegerList) aTypesList;
333 Handle(TDataStd_BooleanList) aClosuresList;
334 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
335 if ( aTypesList.IsNull() || theSectionIndex >= aTypesList->Extent() )
338 TColStd_ListOfInteger anOldTypesList;
339 anOldTypesList = aTypesList->List();
341 // Refill the existing list
344 TColStd_ListIteratorOfListOfInteger aTypesIter( anOldTypesList );
345 for ( int i = 0; aTypesIter.More(); aTypesIter.Next(), ++i )
346 aTypesList->Append( i == theSectionIndex ? theSectionType : aTypesIter.Value() );
349 bool HYDROData_PolylineXY::IsClosedSection( const int theSectionIndex ) const
351 Handle(TDataStd_ExtStringList) aNamesList;
352 Handle(TDataStd_IntegerList) aTypesList;
353 Handle(TDataStd_BooleanList) aClosuresList;
354 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
355 if ( aClosuresList.IsNull() || theSectionIndex >= aClosuresList->Extent() )
358 TDataStd_ListIteratorOfListOfByte aClosuresIter( aClosuresList->List() );
359 for ( int i = 0; aClosuresIter.More() && i != theSectionIndex; aClosuresIter.Next(), ++i );
361 return aClosuresIter.More() ? (bool)aClosuresIter.Value() : false;
364 void HYDROData_PolylineXY::SetSectionClosed( const int theSectionIndex,
365 const bool theIsClosed )
367 Handle(TDataStd_ExtStringList) aNamesList;
368 Handle(TDataStd_IntegerList) aTypesList;
369 Handle(TDataStd_BooleanList) aClosuresList;
370 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
371 if ( aClosuresList.IsNull() || theSectionIndex >= aClosuresList->Extent() )
374 TDataStd_ListOfByte anOldClosuresList;
375 anOldClosuresList = aClosuresList->List();
377 // Refill the existing list
378 aClosuresList->Clear();
380 TDataStd_ListIteratorOfListOfByte aClosuresIter( anOldClosuresList );
381 for ( int i = 0; aClosuresIter.More(); aClosuresIter.Next(), ++i )
382 aClosuresList->Append( i == theSectionIndex ? theIsClosed : (bool)aClosuresIter.Value() );
385 void HYDROData_PolylineXY::GetSections( NCollection_Sequence<TCollection_AsciiString>& theSectNames,
386 NCollection_Sequence<SectionType>& theSectTypes,
387 NCollection_Sequence<bool>& theSectClosures ) const
389 theSectNames.Clear();
390 theSectTypes.Clear();
391 theSectClosures.Clear();
393 Handle(TDataStd_ExtStringList) aNamesList;
394 Handle(TDataStd_IntegerList) aTypesList;
395 Handle(TDataStd_BooleanList) aClosuresList;
396 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
397 if ( aNamesList.IsNull() || aTypesList.IsNull() || aClosuresList.IsNull() )
400 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( aNamesList->List() );
401 TColStd_ListIteratorOfListOfInteger aTypesIter( aTypesList->List() );
402 TDataStd_ListIteratorOfListOfByte aClosuresIter( aClosuresList->List() );
403 for ( ; aNamesIter.More() && aTypesIter.More() && aClosuresIter.More();
404 aNamesIter.Next(), aTypesIter.Next(), aClosuresIter.Next() )
406 const TCollection_ExtendedString& aSectName = aNamesIter.Value();
407 SectionType aSectType = (SectionType)aTypesIter.Value();
408 bool aSectClosures = aClosuresIter.Value();
410 theSectNames.Append( aSectName );
411 theSectTypes.Append( aSectType );
412 theSectClosures.Append( aSectClosures );
416 void HYDROData_PolylineXY::RemoveSection( const int theSectionIndex )
418 Handle(TDataStd_ExtStringList) aNamesList;
419 Handle(TDataStd_IntegerList) aTypesList;
420 Handle(TDataStd_BooleanList) aClosuresList;
421 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
422 if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
425 if ( aNamesList->Extent() == 1 )
427 removeSectionsLists();
432 TDataStd_ListOfExtendedString anOldNamesList;
433 anOldNamesList = aNamesList->List();
435 TColStd_ListOfInteger anOldTypesList;
436 anOldTypesList = aTypesList->List();
438 TDataStd_ListOfByte anOldClosuresList;
439 anOldClosuresList = aClosuresList->List();
441 // Refill the existing lists
444 aClosuresList->Clear();
446 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( anOldNamesList );
447 TColStd_ListIteratorOfListOfInteger aTypesIter( anOldTypesList );
448 TDataStd_ListIteratorOfListOfByte aClosuresIter( anOldClosuresList );
449 for ( int i = 0; aNamesIter.More() && aTypesIter.More() && aClosuresIter.More();
450 aNamesIter.Next(), aTypesIter.Next(), aClosuresIter.Next(), ++i )
452 if ( i == theSectionIndex )
453 continue; // skip index to remove
455 aNamesList->Append( aNamesIter.Value() );
456 aTypesList->Append( aTypesIter.Value() );
457 aClosuresList->Append( (bool)aClosuresIter.Value() );
460 // Remove points that belongs to removed section
461 removePointsLists( theSectionIndex );
465 void HYDROData_PolylineXY::RemoveSections()
467 removeSectionsLists();
471 void HYDROData_PolylineXY::AddPoint( const int theSectionIndex,
472 const Point& thePoint,
473 const int thePointIndex )
475 Handle(TDataStd_RealList) aListX, aListY;
476 getPointsLists( theSectionIndex, aListX, aListY );
478 if ( thePointIndex < 0 || thePointIndex >= aListX->Extent() )
480 aListX->Append( thePoint.X() );
481 aListY->Append( thePoint.Y() );
485 TColStd_ListOfReal anOldListX;
486 anOldListX = aListX->List();
488 TColStd_ListOfReal anOldListY;
489 anOldListY = aListY->List();
491 // Refill the existing lists
495 TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
496 TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
497 for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
499 double aCoordX = anIterX.Value();
500 double aCoordY = anIterY.Value();
502 if ( i == thePointIndex )
504 // Insert our new point
505 aListX->Append( thePoint.X() );
506 aListY->Append( thePoint.Y() );
509 aListX->Append( aCoordX );
510 aListY->Append( aCoordY );
515 void HYDROData_PolylineXY::SetPoint( const int theSectionIndex,
516 const Point& thePoint,
517 const int thePointIndex )
519 Handle(TDataStd_RealList) aListX, aListY;
520 getPointsLists( theSectionIndex, aListX, aListY );
522 if ( thePointIndex < 0 )
524 aListX->Prepend( thePoint.X() );
525 aListY->Prepend( thePoint.Y() );
527 else if ( thePointIndex >= aListX->Extent() )
529 aListX->Append( thePoint.X() );
530 aListY->Append( thePoint.Y() );
534 TColStd_ListOfReal anOldListX;
535 anOldListX = aListX->List();
537 TColStd_ListOfReal anOldListY;
538 anOldListY = aListY->List();
540 // Refill the existing lists
544 TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
545 TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
546 for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
548 double aCoordX = anIterX.Value();
549 double aCoordY = anIterY.Value();
551 if ( i == thePointIndex )
553 // Insert our new point instead of old one
554 aCoordX = thePoint.X();
555 aCoordY = thePoint.Y();
558 aListX->Append( aCoordX );
559 aListY->Append( aCoordY );
564 void HYDROData_PolylineXY::RemovePoint( const int theSectionIndex,
565 const int thePointIndex )
567 Handle(TDataStd_RealList) aListX, aListY;
568 getPointsLists( theSectionIndex, aListX, aListY, false );
569 if ( aListX.IsNull() || aListY.IsNull() || aListX->IsEmpty() )
572 if ( aListX->Extent() == 1 )
574 removePointsLists( theSectionIndex );
578 TColStd_ListOfReal anOldListX;
579 anOldListX = aListX->List();
581 TColStd_ListOfReal anOldListY;
582 anOldListY = aListY->List();
584 // Refill the existing lists
588 TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
589 TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
590 for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
592 if ( i == thePointIndex )
593 continue; // skip index to remove
595 aListX->Append( anIterX.Value() );
596 aListY->Append( anIterY.Value() );
601 HYDROData_PolylineXY::PointsList HYDROData_PolylineXY::GetPoints( const int theSectionIndex ) const
605 Handle(TDataStd_RealList) aListX, aListY;
606 getPointsLists( theSectionIndex, aListX, aListY, false );
607 if ( aListX.IsNull() || aListY.IsNull() || aListX->IsEmpty() )
610 TColStd_ListIteratorOfListOfReal anIterX( aListX->List() );
611 TColStd_ListIteratorOfListOfReal anIterY( aListY->List() );
612 for ( ; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next() )
614 Point aPoint( anIterX.Value(), anIterY.Value() );
615 aResList.Append( aPoint );
621 QPainterPath HYDROData_PolylineXY::GetPainterPath() const
625 NCollection_Sequence<TCollection_AsciiString> aSectNames;
626 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
627 NCollection_Sequence<bool> aSectClosures;
628 GetSections( aSectNames, aSectTypes, aSectClosures );
629 if( aSectNames.IsEmpty() )
632 PointsList aSectPointsList = GetPoints( 0 );
633 if( aSectPointsList.IsEmpty() )
636 SectionType aSectionType = aSectTypes.Value( 1 );
637 bool anIsSectionClosed = aSectClosures.Value( 1 );
639 if ( aSectionType == SECTION_POLYLINE )
641 aPath.moveTo( aSectPointsList.Value( 1 ).X(), aSectPointsList.Value( 1 ).Y() );
643 for( int i = 2, n = aSectPointsList.Size(); i <= n; ++i )
645 const Point& aSectPoint = aSectPointsList.Value( i );
647 aPath.lineTo( aSectPoint.X(), aSectPoint.Y() );
650 if( anIsSectionClosed )
651 aPath.closeSubpath();
655 QList<double> aPoints;
656 for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
658 const Point& aSectPoint = aSectPointsList.Value( i );
659 aPoints << aSectPoint.X() << aSectPoint.Y();
662 HYDROData_BSplineOperation aBSpline( aPoints, 0, anIsSectionClosed );
663 aPath = aBSpline.ComputePath();