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 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( aNamesList->List() );
307 for ( int i = 0; aNamesIter.More(); aNamesIter.Next(), ++i )
308 aNamesList->Append( i == theSectionIndex ? theSectionName : aNamesIter.Value() );
311 HYDROData_PolylineXY::SectionType HYDROData_PolylineXY::GetSectionType( const int theSectionIndex ) const
313 Handle(TDataStd_ExtStringList) aNamesList;
314 Handle(TDataStd_IntegerList) aTypesList;
315 Handle(TDataStd_BooleanList) aClosuresList;
316 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
317 if ( aTypesList.IsNull() || theSectionIndex >= aTypesList->Extent() )
318 return SECTION_POLYLINE;
320 TColStd_ListIteratorOfListOfInteger aTypesIter( aTypesList->List() );
321 for ( int i = 0; aTypesIter.More() && i != theSectionIndex; aTypesIter.Next(), ++i );
323 return aTypesIter.More() ? (SectionType)aTypesIter.Value() : SECTION_POLYLINE;
326 void HYDROData_PolylineXY::SetSectionType( const int theSectionIndex,
327 const SectionType theSectionType )
329 Handle(TDataStd_ExtStringList) aNamesList;
330 Handle(TDataStd_IntegerList) aTypesList;
331 Handle(TDataStd_BooleanList) aClosuresList;
332 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
333 if ( aTypesList.IsNull() || theSectionIndex >= aTypesList->Extent() )
336 TColStd_ListOfInteger anOldTypesList;
337 anOldTypesList = aTypesList->List();
339 // Refill the existing list
342 TColStd_ListIteratorOfListOfInteger aTypesIter( anOldTypesList );
343 for ( int i = 0; aTypesIter.More(); aTypesIter.Next(), ++i )
344 aTypesList->Append( i == theSectionIndex ? theSectionType : aTypesIter.Value() );
347 bool HYDROData_PolylineXY::IsClosedSection( const int theSectionIndex ) const
349 Handle(TDataStd_ExtStringList) aNamesList;
350 Handle(TDataStd_IntegerList) aTypesList;
351 Handle(TDataStd_BooleanList) aClosuresList;
352 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
353 if ( aClosuresList.IsNull() || theSectionIndex >= aClosuresList->Extent() )
356 TDataStd_ListIteratorOfListOfByte aClosuresIter( aClosuresList->List() );
357 for ( int i = 0; aClosuresIter.More() && i != theSectionIndex; aClosuresIter.Next(), ++i );
359 return aClosuresIter.More() ? (bool)aClosuresIter.Value() : false;
362 void HYDROData_PolylineXY::SetSectionClosed( const int theSectionIndex,
363 const bool theIsClosed )
365 Handle(TDataStd_ExtStringList) aNamesList;
366 Handle(TDataStd_IntegerList) aTypesList;
367 Handle(TDataStd_BooleanList) aClosuresList;
368 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
369 if ( aClosuresList.IsNull() || theSectionIndex >= aClosuresList->Extent() )
372 TDataStd_ListOfByte anOldClosuresList;
373 anOldClosuresList = aClosuresList->List();
375 // Refill the existing list
376 aClosuresList->Clear();
378 TDataStd_ListIteratorOfListOfByte aClosuresIter( anOldClosuresList );
379 for ( int i = 0; aClosuresIter.More(); aClosuresIter.Next(), ++i )
380 aClosuresList->Append( i == theSectionIndex ? theIsClosed : (bool)aClosuresIter.Value() );
383 void HYDROData_PolylineXY::GetSections( NCollection_Sequence<TCollection_AsciiString>& theSectNames,
384 NCollection_Sequence<SectionType>& theSectTypes,
385 NCollection_Sequence<bool>& theSectClosures ) const
387 theSectNames.Clear();
388 theSectTypes.Clear();
389 theSectClosures.Clear();
391 Handle(TDataStd_ExtStringList) aNamesList;
392 Handle(TDataStd_IntegerList) aTypesList;
393 Handle(TDataStd_BooleanList) aClosuresList;
394 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
395 if ( aNamesList.IsNull() || aTypesList.IsNull() || aClosuresList.IsNull() )
398 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( aNamesList->List() );
399 TColStd_ListIteratorOfListOfInteger aTypesIter( aTypesList->List() );
400 TDataStd_ListIteratorOfListOfByte aClosuresIter( aClosuresList->List() );
401 for ( ; aNamesIter.More() && aTypesIter.More() && aClosuresIter.More();
402 aNamesIter.Next(), aTypesIter.Next(), aClosuresIter.Next() )
404 const TCollection_ExtendedString& aSectName = aNamesIter.Value();
405 SectionType aSectType = (SectionType)aTypesIter.Value();
406 bool aSectClosures = aClosuresIter.Value();
408 theSectNames.Append( aSectName );
409 theSectTypes.Append( aSectType );
410 theSectClosures.Append( aSectClosures );
414 void HYDROData_PolylineXY::RemoveSection( const int theSectionIndex )
416 Handle(TDataStd_ExtStringList) aNamesList;
417 Handle(TDataStd_IntegerList) aTypesList;
418 Handle(TDataStd_BooleanList) aClosuresList;
419 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
420 if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
423 if ( aNamesList->Extent() == 1 )
425 removeSectionsLists();
430 TDataStd_ListOfExtendedString anOldNamesList;
431 anOldNamesList = aNamesList->List();
433 TColStd_ListOfInteger anOldTypesList;
434 anOldTypesList = aTypesList->List();
436 TDataStd_ListOfByte anOldClosuresList;
437 anOldClosuresList = aClosuresList->List();
439 // Refill the existing lists
442 aClosuresList->Clear();
444 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( anOldNamesList );
445 TColStd_ListIteratorOfListOfInteger aTypesIter( anOldTypesList );
446 TDataStd_ListIteratorOfListOfByte aClosuresIter( anOldClosuresList );
447 for ( int i = 0; aNamesIter.More() && aTypesIter.More() && aClosuresIter.More();
448 aNamesIter.Next(), aTypesIter.Next(), aClosuresIter.Next(), ++i )
450 if ( i == theSectionIndex )
451 continue; // skip index to remove
453 aNamesList->Append( aNamesIter.Value() );
454 aTypesList->Append( aTypesIter.Value() );
455 aClosuresList->Append( (bool)aClosuresIter.Value() );
458 // Remove points that belongs to removed section
459 removePointsLists( theSectionIndex );
463 void HYDROData_PolylineXY::RemoveSections()
465 removeSectionsLists();
469 void HYDROData_PolylineXY::AddPoint( const int theSectionIndex,
470 const Point& thePoint,
471 const int thePointIndex )
473 Handle(TDataStd_RealList) aListX, aListY;
474 getPointsLists( theSectionIndex, aListX, aListY );
476 if ( thePointIndex < 0 || thePointIndex >= aListX->Extent() )
478 aListX->Append( thePoint.X() );
479 aListY->Append( thePoint.Y() );
483 TColStd_ListOfReal anOldListX;
484 anOldListX = aListX->List();
486 TColStd_ListOfReal anOldListY;
487 anOldListY = aListY->List();
489 // Refill the existing lists
493 TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
494 TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
495 for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
497 double aCoordX = anIterX.Value();
498 double aCoordY = anIterY.Value();
500 if ( i == thePointIndex )
502 // Insert our new point
503 aListX->Append( thePoint.X() );
504 aListY->Append( thePoint.Y() );
507 aListX->Append( aCoordX );
508 aListY->Append( aCoordY );
513 void HYDROData_PolylineXY::SetPoint( const int theSectionIndex,
514 const Point& thePoint,
515 const int thePointIndex )
517 Handle(TDataStd_RealList) aListX, aListY;
518 getPointsLists( theSectionIndex, aListX, aListY );
520 if ( thePointIndex < 0 )
522 aListX->Prepend( thePoint.X() );
523 aListY->Prepend( thePoint.Y() );
525 else if ( thePointIndex >= aListX->Extent() )
527 aListX->Append( thePoint.X() );
528 aListY->Append( thePoint.Y() );
532 TColStd_ListOfReal anOldListX;
533 anOldListX = aListX->List();
535 TColStd_ListOfReal anOldListY;
536 anOldListY = aListY->List();
538 // Refill the existing lists
542 TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
543 TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
544 for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
546 double aCoordX = anIterX.Value();
547 double aCoordY = anIterY.Value();
549 if ( i == thePointIndex )
551 // Insert our new point instead of old one
552 aCoordX = thePoint.X();
553 aCoordY = thePoint.Y();
556 aListX->Append( aCoordX );
557 aListY->Append( aCoordY );
562 void HYDROData_PolylineXY::RemovePoint( const int theSectionIndex,
563 const int thePointIndex )
565 Handle(TDataStd_RealList) aListX, aListY;
566 getPointsLists( theSectionIndex, aListX, aListY, false );
567 if ( aListX.IsNull() || aListY.IsNull() || aListX->IsEmpty() )
570 if ( aListX->Extent() == 1 )
572 removePointsLists( theSectionIndex );
576 TColStd_ListOfReal anOldListX;
577 anOldListX = aListX->List();
579 TColStd_ListOfReal anOldListY;
580 anOldListY = aListY->List();
582 // Refill the existing lists
586 TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
587 TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
588 for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
590 if ( i == thePointIndex )
591 continue; // skip index to remove
593 aListX->Append( anIterX.Value() );
594 aListY->Append( anIterY.Value() );
599 HYDROData_PolylineXY::PointsList HYDROData_PolylineXY::GetPoints( const int theSectionIndex ) const
603 Handle(TDataStd_RealList) aListX, aListY;
604 getPointsLists( theSectionIndex, aListX, aListY, false );
605 if ( aListX.IsNull() || aListY.IsNull() || aListX->IsEmpty() )
608 TColStd_ListIteratorOfListOfReal anIterX( aListX->List() );
609 TColStd_ListIteratorOfListOfReal anIterY( aListY->List() );
610 for ( ; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next() )
612 Point aPoint( anIterX.Value(), anIterY.Value() );
613 aResList.Append( aPoint );
619 QPainterPath HYDROData_PolylineXY::GetPainterPath() const
623 NCollection_Sequence<TCollection_AsciiString> aSectNames;
624 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
625 NCollection_Sequence<bool> aSectClosures;
626 GetSections( aSectNames, aSectTypes, aSectClosures );
627 if( aSectNames.IsEmpty() )
630 PointsList aSectPointsList = GetPoints( 0 );
631 if( aSectPointsList.IsEmpty() )
634 SectionType aSectionType = aSectTypes.Value( 1 );
635 bool anIsSectionClosed = aSectClosures.Value( 1 );
637 if ( aSectionType == SECTION_POLYLINE )
639 aPath.moveTo( aSectPointsList.Value( 1 ).X(), aSectPointsList.Value( 1 ).Y() );
641 for( int i = 2, n = aSectPointsList.Size(); i <= n; ++i )
643 const Point& aSectPoint = aSectPointsList.Value( i );
645 aPath.lineTo( aSectPoint.X(), aSectPoint.Y() );
648 if( anIsSectionClosed )
649 aPath.closeSubpath();
653 QList<double> aPoints;
654 for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
656 const Point& aSectPoint = aSectPointsList.Value( i );
657 aPoints << aSectPoint.X() << aSectPoint.Y();
660 HYDROData_BSplineOperation aBSpline( aPoints, 0, anIsSectionClosed );
661 aPath = aBSpline.ComputePath();