Salome HOME
Create goups for stream.
[modules/hydro.git] / src / HYDROData / HYDROData_PolylineXY.cxx
index 0d79dd238678eeb86abac756dc34ea566189eda2..5129d6e8568574edd4844e7e860d59fcf8014475 100755 (executable)
 #include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
 #include <TDataStd_RealList.hxx>
 
+#include <TopoDS_Iterator.hxx>
 #include <TopTools_ListIteratorOfListOfShape.hxx>
-
+#include <TopTools_HSequenceOfShape.hxx>
+#include <TopExp_Explorer.hxx>
+#include <ShapeAnalysis_FreeBounds.hxx>
+#include <TopoDS.hxx>
+#include <QColor>
 #include <QPainterPath>
 #include <QVariant>
 
 #define PYTHON_POLYLINEXY_ID "KIND_POLYLINEXY"
 
+const double LOCAL_SELECTION_TOLERANCE = 0.0001;
+
 IMPLEMENT_STANDARD_HANDLE(HYDROData_PolylineXY, HYDROData_IPolyline)
 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_PolylineXY, HYDROData_IPolyline)
 
@@ -105,6 +112,11 @@ QVariant HYDROData_PolylineXY::GetDataVariant()
   return aVarData;
 }
 
+QColor HYDROData_PolylineXY::DefaultWireColor()
+{
+  return QColor( Qt::red );
+}
+
 TopoDS_Shape HYDROData_PolylineXY::GetShape()
 {
   return getPolylineShape();
@@ -138,13 +150,16 @@ TopoDS_Wire HYDROData_PolylineXY::BuildWire( const SectionType&
       gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), aFirstPoint.Z() );
       gp_Pnt aPnt2( aLastPoint.X(), aLastPoint.Y(), aLastPoint.Z() );
 
+      if ( aPnt1.IsEqual( aPnt2, LOCAL_SELECTION_TOLERANCE ) )
+        continue;
+
       TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aPnt1, aPnt2 ).Edge();
       aMakeWire.Add( anEdge );
     }
   }
   else //if( theType == PolylineSection::SECTION_SPLINE )
   {
-    HYDROData_BSplineOperation aBSpline( thePoints, theIsClosed );
+    HYDROData_BSplineOperation aBSpline( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
 
     TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSpline.Curve() ).Edge();
     aMakeWire.Add( anEdge );
@@ -158,39 +173,40 @@ TopoDS_Wire HYDROData_PolylineXY::BuildWire( const SectionType&
   return aWire;
 }
 
-QPainterPath HYDROData_PolylineXY::BuildPainterPath( const SectionType&                  theType,
-                                                     const bool&                         theIsClosed,
-                                                     const NCollection_Sequence<gp_XYZ>& thePoints )
+void HYDROData_PolylineXY::BuildPainterPath( QPainterPath&                       thePath,
+                                             const SectionType&                  theType,
+                                             const bool&                         theIsClosed,
+                                             const NCollection_Sequence<gp_XYZ>& thePoints )
 {
-  QPainterPath aPath;
   if ( thePoints.IsEmpty() )
-    return aPath;
+    return;
 
   if ( theType == SECTION_POLYLINE )
   {
-    aPath.moveTo( thePoints.Value( 1 ).X(), thePoints.Value( 1 ).Y() );
+    const gp_XYZ& aFirstPoint = thePoints.Value( 1 );
+    thePath.moveTo( aFirstPoint.X(), aFirstPoint.Y() );
 
     for( int i = 2, n = thePoints.Size(); i <= n; ++i )
     {
       const gp_XYZ& aSectPoint = thePoints.Value( i );
 
-      aPath.lineTo( aSectPoint.X(), aSectPoint.Y() );
+      thePath.lineTo( aSectPoint.X(), aSectPoint.Y() );
     }
 
     if( theIsClosed )
-      aPath.closeSubpath();
+      thePath.closeSubpath();
   }
   else
   {
-    HYDROData_BSplineOperation aBSpline( thePoints, theIsClosed );
-    aPath = aBSpline.ComputePath();
+    HYDROData_BSplineOperation aBSpline( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
+    aBSpline.ComputePath( thePath );
   }
-
-  return aPath;
 }
 
 void HYDROData_PolylineXY::Update()
 {
+  HYDROData_IPolyline::Update();
+
   NCollection_Sequence<TCollection_AsciiString>           aSectNames;
   NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
   NCollection_Sequence<bool>                              aSectClosures;
@@ -220,34 +236,49 @@ void HYDROData_PolylineXY::Update()
     }
 
     TopoDS_Wire aSectionWire = BuildWire( aSectionType, anIsSectionClosed, aPoints );
-    aSectionWiresList.Append( aSectionWire );
-    aMakeWire.Add( aSectionWire );
-  }
-
-  TopoDS_Shape aShape;
-
-  if ( aMakeWire.IsDone() )
-  {
-    aShape = aMakeWire.Shape();
+    if ( !aSectionWire.IsNull() ) {
+      aSectionWiresList.Append( aSectionWire );
+      aMakeWire.Add( aSectionWire );
+    }
   }
-  else if ( !aSectionWiresList.IsEmpty() )
+// all input wires in the <aSectionWiresList>
+  Handle(TopTools_HSequenceOfShape) aSeqWires = new TopTools_HSequenceOfShape;
+  Handle(TopTools_HSequenceOfShape) aSeqEdges = new TopTools_HSequenceOfShape;
+  TopTools_ListIteratorOfListOfShape it(aSectionWiresList);
+  for(;it.More();it.Next())
   {
-    // build compound
-    TopoDS_Compound aCompound;
-    
-    BRep_Builder aBuilder;
-    aBuilder.MakeCompound( aCompound );
-    
-    TopTools_ListIteratorOfListOfShape anIter( aSectionWiresList );
-    for ( ; anIter.More(); anIter.Next() )
-    {
-      aBuilder.Add( aCompound, anIter.Value() );
-    }
-    
-    aShape = aCompound;
+         TopExp_Explorer it2(it.Value(), TopAbs_EDGE);
+         for(;it2.More();it2.Next()) 
+           aSeqEdges->Append(it2.Current());
   }
-
-  setPolylineShape( aShape );
+   BRep_Builder aBB;
+   TopoDS_Compound aCmp;
+   TopoDS_Shape aResult;
+   aBB.MakeCompound(aCmp);
+   if(aSeqEdges->Length() >1)
+   {
+     ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdges,1E-5,Standard_False,aSeqWires);
+
+     if( aSeqWires->Length()==1 )
+       aResult = aSeqWires->Value( 1 );
+     else
+     {
+       for (Standard_Integer i = 1; i <= aSeqWires->Length();i++)
+       {
+         const TopoDS_Shape& aS1 = aSeqWires->Value(i);
+         aBB.Add(aCmp, aS1);
+       }
+       aResult = aCmp;
+     }
+        }
+   else if (aSeqEdges->Length() == 1)
+   {
+          BRepBuilderAPI_MakeWire mkWire (TopoDS::Edge(aSeqEdges->Value(1)));
+          if (mkWire.IsDone())
+       aResult = mkWire.Wire();
+   }
+
+  setPolylineShape( aResult );
 }
 
 /**
@@ -295,6 +326,9 @@ double HYDROData_PolylineXY::GetDistance( const int theSectionIndex,
       const Point& aSectPoint = aSectPointsList.Value( i );
       aResDistance += gp_Pnt2d( aPrevPoint ).Distance( aSectPoint );
       aPrevPoint = aSectPoint;
+
+      if ( thePointIndex == i - 1 )
+        break;
     }
   }
   else
@@ -314,7 +348,7 @@ double HYDROData_PolylineXY::GetDistance( const int theSectionIndex,
         aPointToTest = aPoint;
     }
 
-    HYDROData_BSplineOperation aBSpline( aPoints, anIsSectionClosed );
+    HYDROData_BSplineOperation aBSpline( aPoints, anIsSectionClosed, LOCAL_SELECTION_TOLERANCE );
 
     Quantity_Parameter aFirstParam = aBSpline.Curve()->FirstParameter();
     Quantity_Parameter aSecondParam = aBSpline.Curve()->LastParameter();
@@ -380,6 +414,8 @@ void HYDROData_PolylineXY::AddSection( const TCollection_AsciiString& theSectNam
   aNamesList->Append( aSectName );
   aTypesList->Append( theSectionType );
   aClosuresList->Append( theIsClosed );
+
+  SetToUpdate( true );
 }
 
 TCollection_AsciiString HYDROData_PolylineXY::GetSectionName( const int theSectionIndex ) const
@@ -423,6 +459,8 @@ void HYDROData_PolylineXY::SetSectionName( const int                      theSec
   TDataStd_ListIteratorOfListOfExtendedString aNamesIter( anOldNamesList );
   for ( int i = 0; aNamesIter.More(); aNamesIter.Next(), ++i )
     aNamesList->Append( i == theSectionIndex ? aNewSectName : aNamesIter.Value() );
+
+  SetToUpdate( true );
 }
 
 HYDROData_PolylineXY::SectionType HYDROData_PolylineXY::GetSectionType( const int theSectionIndex ) const
@@ -459,6 +497,8 @@ void HYDROData_PolylineXY::SetSectionType( const int         theSectionIndex,
   TColStd_ListIteratorOfListOfInteger aTypesIter( anOldTypesList );
   for ( int i = 0; aTypesIter.More(); aTypesIter.Next(), ++i )
     aTypesList->Append( i == theSectionIndex ? theSectionType : aTypesIter.Value() );
+
+  SetToUpdate( true );
 }
 
 bool HYDROData_PolylineXY::IsClosedSection( const int theSectionIndex ) const
@@ -495,6 +535,8 @@ void HYDROData_PolylineXY::SetSectionClosed( const int  theSectionIndex,
   TDataStd_ListIteratorOfListOfByte aClosuresIter( anOldClosuresList );
   for ( int i = 0; aClosuresIter.More(); aClosuresIter.Next(), ++i )
     aClosuresList->Append( i == theSectionIndex ? theIsClosed : (bool)aClosuresIter.Value() );
+
+  SetToUpdate( true );
 }
 
 void HYDROData_PolylineXY::GetSections( NCollection_Sequence<TCollection_AsciiString>& theSectNames,
@@ -575,12 +617,15 @@ void HYDROData_PolylineXY::RemoveSection( const int theSectionIndex )
     // Remove points that belongs to removed section
     removePointsLists( theSectionIndex );
   }
+
+  SetToUpdate( true );
 }
 
 void HYDROData_PolylineXY::RemoveSections()
 {
   removeSectionsLists();
   removePointsLists();
+  SetToUpdate( true );
 }
 
 void HYDROData_PolylineXY::AddPoint( const int    theSectionIndex,
@@ -625,6 +670,8 @@ void HYDROData_PolylineXY::AddPoint( const int    theSectionIndex,
       aListY->Append( aCoordY );
     }
   }
+
+  SetToUpdate( true );
 }
 
 void HYDROData_PolylineXY::SetPoint( const int    theSectionIndex,
@@ -674,6 +721,8 @@ void HYDROData_PolylineXY::SetPoint( const int    theSectionIndex,
       aListY->Append( aCoordY );
     }
   }
+
+  SetToUpdate( true );
 }
 
 void HYDROData_PolylineXY::RemovePoint( const int theSectionIndex,
@@ -711,6 +760,8 @@ void HYDROData_PolylineXY::RemovePoint( const int theSectionIndex,
       aListY->Append( anIterY.Value() );
     }
   }
+
+  SetToUpdate( true );
 }
 
 HYDROData_PolylineXY::PointsList HYDROData_PolylineXY::GetPoints( const int theSectionIndex ) const
@@ -741,29 +792,30 @@ QPainterPath HYDROData_PolylineXY::GetPainterPath() const
   NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
   NCollection_Sequence<bool>                              aSectClosures;
   GetSections( aSectNames, aSectTypes, aSectClosures );
-  if( aSectNames.IsEmpty() )
-    return aPath;
 
-  PointsList aSectPointsList = GetPoints( 0 );
-  if( aSectPointsList.IsEmpty() )
-    return aPath;
+  for ( int aSectionId = 1, aNbSects = aSectNames.Size(); aSectionId <= aNbSects; aSectionId++ )
+  {
+    TCollection_AsciiString aSectName = aSectNames.Value( aSectionId );
+    SectionType aSectionType = aSectTypes.Value( aSectionId );
+    bool anIsSectionClosed = aSectClosures.Value( aSectionId );
 
-  SectionType aSectionType = aSectTypes.Value( 1 );
-  bool anIsSectionClosed = aSectClosures.Value( 1 );
+    PointsList aSectPointsList = GetPoints( aSectionId - 1 );
+    if ( aSectPointsList.IsEmpty() )
+      continue;
 
-  NCollection_Sequence<gp_XYZ> aPoints;
-  for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
-  {
-    const Point& aSectPoint = aSectPointsList.Value( i );
+    NCollection_Sequence<gp_XYZ> aPoints;
+    for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
+    {
+      const Point& aSectPoint = aSectPointsList.Value( i );
 
-    gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
-    aPoints.Append( aPoint );
-  }
+      gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
+      aPoints.Append( aPoint );
+    }
 
-  aPath = BuildPainterPath( aSectionType, anIsSectionClosed, aPoints );
+    BuildPainterPath( aPath, aSectionType, anIsSectionClosed, aPoints );
+  }
 
   return aPath;
-
 }