Salome HOME
Temporary workaround for python.
[modules/hydro.git] / src / HYDROData / HYDROData_PolylineXY.cxx
index 46e5205e0f1c764b730123f4abeacb7d64b20401..d418144a2e1865ef4b2fa25d71411bc399ed1274 100755 (executable)
@@ -9,6 +9,9 @@
 #include <BRep_Builder.hxx>
 #include <BRepBuilderAPI_MakeEdge.hxx>
 #include <BRepBuilderAPI_MakeWire.hxx>
+#include <BRepBuilderAPI_MakePolygon.hxx>
+#include <BRepBuilderAPI_MakeFace.hxx>
+#include <BRepOffsetAPI_NormalProjection.hxx>
 
 #include <GeomAPI_ProjectPointOnCurve.hxx>
 #include <GeomAdaptor_Curve.hxx>
@@ -21,6 +24,7 @@
 
 #include <gp_Pnt.hxx>
 #include <gp_XY.hxx>
+#include <gp_Pln.hxx>
 
 #include <NCollection_Map.hxx>
 
 #include <TopExp_Explorer.hxx>
 #include <ShapeAnalysis_FreeBounds.hxx>
 #include <TopoDS.hxx>
+
 #include <QColor>
 #include <QPainterPath>
 #include <QVariant>
 
 static const Standard_GUID GUID_IS_UNEDITABLE("e5799736-9030-4051-91a4-2e58321fa153");
 
-#define PYTHON_POLYLINEXY_ID "KIND_POLYLINEXY"
-
 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
 
 TCollection_AsciiString getUniqueSectionName( const NCollection_Sequence<TCollection_AsciiString>& theNamesSeq )
@@ -103,18 +106,8 @@ HYDROData_PolylineXY::~HYDROData_PolylineXY()
 
 QStringList HYDROData_PolylineXY::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
 {
-  QStringList aResList;
-
-  Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
-  if ( aDocument.IsNull() )
-    return aResList;
-                             
-  QString aDocName = aDocument->GetDocPyName();
-  QString aPolylineName = GetName();
-
-  aResList << QString( "%1 = %2.CreateObject( %3 );" )
-              .arg( aPolylineName ).arg( aDocName ).arg( PYTHON_POLYLINEXY_ID );
-  aResList << QString( "%1.SetName( \"%1\" );" ).arg( aPolylineName );
+  QStringList aResList = dumpObjectCreation( theTreatedObjects );
+  QString aPolylineName = GetObjPyName();
 
   // Set polilyne data
   NCollection_Sequence<TCollection_AsciiString>           aSectNames;
@@ -131,16 +124,20 @@ QStringList HYDROData_PolylineXY::DumpToPython( MapOfTreatedObjects& theTreatedO
     aResList << QString( "%1.AddSection( \"%2\", %3, %4 );" ).arg( aPolylineName )
                 .arg( aSectName.ToCString() ).arg( aSectType ).arg( aSectClosure );
 
-    HYDROData_PolylineXY::PointsList aSectPointsList = GetPoints( i );
+    HYDROData_IPolyline::PointsList aSectPointsList = GetPoints( i - 1 );
     for ( int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k )
     {
       const Point& aSectPoint = aSectPointsList.Value( k );
 
-      aResList << QString( "%1.AddPoint( %2, QPointF( %3, %4 ) );" ).arg( aPolylineName )
+      aResList << QString( "%1.AddPoint( %2, gp_XY( %3, %4 ) );" ).arg( aPolylineName )
         .arg( i - 1 ).arg( aSectPoint.X() ).arg( aSectPoint.Y() );
     }
   }
 
+  aResList << QString( "" );
+  aResList << QString( "%1.Update();" ).arg( aPolylineName );
+  aResList << QString( "" );
+
   return aResList;
 }
 
@@ -300,7 +297,17 @@ bool HYDROData_PolylineXY::ImportShape( const TopoDS_Shape& theShape )
         aShape = aMakeWire.Wire();
     }
 
-    setPolylineShape( aShape );
+    gp_Pln aPlane( gp_Pnt( 0, 0, 0 ), gp_Dir( 0, 0, 1 ) );
+    BRepBuilderAPI_MakeFace aMakeFace( aPlane );
+    aMakeFace.Build();
+    BRepOffsetAPI_NormalProjection aProj( aMakeFace.Face() );
+    aProj.Add( aShape );
+    aProj.Build();
+    TopoDS_Shape aResult;
+    if( aProj.IsDone() )
+      aResult = aProj.Shape();
+
+    setPolylineShape( aResult );
   }
 
   setEditable( anIsCanBeImported );
@@ -312,49 +319,37 @@ TopoDS_Wire HYDROData_PolylineXY::BuildWire( const SectionType&
                                              const bool&                         theIsClosed,
                                              const NCollection_Sequence<gp_XYZ>& thePoints )
 {
-  BRepBuilderAPI_MakeWire aMakeWire;
-  
+  TopoDS_Wire aWire;
   if( theType == SECTION_POLYLINE )
   {
-    for( int i = 1, n = thePoints.Size(); i <= n; ++i )
+    BRepBuilderAPI_MakePolygon aMakeWire;
+    for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
     {
-      const gp_XYZ& aFirstPoint = thePoints.Value( i );
-
-      gp_XYZ aLastPoint;
-      if ( i == n )
-      {
-        if( theIsClosed )
-          aLastPoint = thePoints.Value( 1 );
-        else
-          break;
-      }
-      else
-      {
-        aLastPoint = thePoints.Value( i + 1 );
-      }
-
-      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 );
+      gp_XYZ aPoint = thePoints.Value( i );
+      gp_Pnt aPnt( aPoint.X(), aPoint.Y(), aPoint.Z() );
+      aMakeWire.Add( aPnt );
     }
+    if( theIsClosed )
+      aMakeWire.Close();
+
+    if ( aMakeWire.IsDone() )
+      aWire = aMakeWire.Wire();
   }
   else //if( theType == PolylineSection::SECTION_SPLINE )
   {
-    HYDROData_BSplineOperation aBSpline( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
+    BRepBuilderAPI_MakeWire aMakeWire;
 
-    TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSpline.Curve() ).Edge();
-    aMakeWire.Add( anEdge );
-  }
+    if ( thePoints.Size() > 1 )
+    {
+      HYDROData_BSplineOperation aBSpline( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
 
-  TopoDS_Wire aWire;
-  aMakeWire.Build();
-  if ( aMakeWire.IsDone() )
-    aWire = aMakeWire;
+      TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSpline.Curve() ).Edge();
+      aMakeWire.Add( anEdge );
+    }
+    aMakeWire.Build();
+    if ( aMakeWire.IsDone() )
+      aWire = aMakeWire;
+  }
 
   return aWire;
 }