Salome HOME
Merge branch 'BR_LAND_COVER_MAP' of ssh://git.salome-platform.org/modules/hydro into...
authorisn <isn@opencascade.com>
Wed, 18 Nov 2015 13:18:40 +0000 (16:18 +0300)
committerisn <isn@opencascade.com>
Wed, 18 Nov 2015 13:18:40 +0000 (16:18 +0300)
src/HYDROData/HYDROData_ShapeFile.cxx

index 508ddec77747b1422227cde446e36cc74364e38b..42936173613dedc144156dc450b255dd39fbd97a 100644 (file)
@@ -56,6 +56,9 @@
 #include <BRep_Builder.hxx>
 #include <BRepAdaptor_Curve.hxx>
 #include <GCPnts_QuasiUniformDeflection.hxx>
+#include <TopLoc_Location.hxx>
+#include <Geom_Plane.hxx>
+#include <NCollection_Array1.hxx>
 
 #ifdef WIN32
   #pragma warning( disable: 4996 )
@@ -327,14 +330,14 @@ void HYDROData_ShapeFile::ProcessFace(const TopoDS_Face& theFace, SHPHandle theS
     //so just write all points "as-is"
     //External wire will be written in clockwise direction
     //any other wires (holes) - in anticlockwise direction
-    for (int j = 1; j <= aNPnts.Size(); j++)
+    for (int j = aNPnts.Size(); j >= 1; j--)
     { 
       x.push_back( aNPnts(j).X());
       y.push_back( aNPnts(j).Y()); 
     }
     //first point is same as the last one => closed polygon
-    x.push_back( aNPnts(1).X());
-    y.push_back( aNPnts(1).Y()); 
+    x.push_back( aNPnts.Last().X());
+    y.push_back( aNPnts.Last().Y()); 
 
   }
   
@@ -366,10 +369,12 @@ void HYDROData_ShapeFile::ReadSHPPolygon(SHPObject* anObj, int i, TopoDS_Face& F
 {
   if (!anObj)
     return;
-  TopoDS_Wire W;
   TopoDS_Edge E; 
   int nParts = anObj->nParts;
-  gp_Pln pln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
+  Handle_Geom_Plane aPlaneSur = new Geom_Plane(gp_Pnt(0,0,0), gp_Dir(0,0,1));
+
+  BRep_Builder BB;
+  BB.MakeFace(F);
 
   TopTools_SequenceOfShape aWires;
   for ( int i = 0 ; i < nParts ; i++ )
@@ -381,44 +386,47 @@ void HYDROData_ShapeFile::ReadSHPPolygon(SHPObject* anObj, int i, TopoDS_Face& F
       EndIndex = anObj->panPartStart[i + 1];
     else
       EndIndex = anObj->nVertices;
-
-    NCollection_List<TopoDS_Vertex> aVertices;
-    gp_Pnt FP (anObj->padfX[StartIndex], anObj->padfY[StartIndex], 0);
-    TopoDS_Vertex V = BRepLib_MakeVertex(FP).Vertex();
-    aVertices.Append(V);
+    
+    TopoDS_Wire W;
+    BB.MakeWire(W);
+
+    //First point is same as the last point 
+    int NbPnts = EndIndex - StartIndex - 1;
+    NCollection_Array1<TopoDS_Vertex> VPoints(0, NbPnts);
+    int j = NbPnts;
+    for ( int k = StartIndex; k < EndIndex; k++ )
+    {
+      gp_Pnt P (anObj->padfX[k], anObj->padfY[k], 0);
+      VPoints.ChangeValue(j) = BRepLib_MakeVertex(P).Vertex();
+      j--;
+    }
       
-    for ( int k = StartIndex + 1; k < EndIndex; k++ )
+    for ( int k = 0; k < VPoints.Size() - 1; k++ )
     {
-      gp_Pnt P1 (anObj->padfX[k - 1], anObj->padfY[k-1], 0); //prev point
-      gp_Pnt P2 (anObj->padfX[k], anObj->padfY[k], 0); //current point
+      gp_Pnt P1 = BRep_Tool::Pnt(VPoints(k));
+      gp_Pnt P2 = BRep_Tool::Pnt(VPoints(k + 1));
       if (P1.Distance(P2) < Precision::Confusion())
         continue;
-      TopoDS_Vertex V = BRepLib_MakeVertex(P2).Vertex();
-      Handle_Geom_TrimmedCurve aTC = GC_MakeSegment(P1,P2).Value();
-      TopoDS_Edge E = BRepLib_MakeEdge(aTC, aVertices.Last(), V).Edge();
-      aVertices.Append(V);
-      aBuilder.Add(E);
+      Handle_Geom_TrimmedCurve aTC = GC_MakeSegment(P1, P2).Value();
+      TopoDS_Edge E;
+      if ( k != VPoints.Size() - 2)
+        E = BRepLib_MakeEdge(aTC, VPoints(k), VPoints(k + 1)).Edge();
+      else
+        //the last edge => use first and last vertices
+        E = BRepLib_MakeEdge(aTC, VPoints.First(), VPoints.Value(VPoints.Upper() - 1)).Edge();
+      //Add edge to wire
+      //If SHP file is correct then the outer wire and the holes will have the correct orientations
+      BB.Add(W, E);
     }
-    
-    aBuilder.Build();
-    W = TopoDS::Wire(aBuilder.Shape());
+    //Wire must be closed anyway
+    W.Closed (BRep_Tool::IsClosed (W));
     W.Orientation(TopAbs_FORWARD);
-    BRepBuilderAPI_MakeFace aDB(pln, W);
-    TopoDS_Face aDummyFace = TopoDS::Face(aDB.Shape());
-    BRepTopAdaptor_FClass2d FClass(aDummyFace, Precision::PConfusion());
-    if ( i == 0 && FClass.PerformInfinitePoint() != TopAbs_OUT) 
-      W.Reverse();
-    if ( i > 0 && FClass.PerformInfinitePoint() != TopAbs_IN) 
-      W.Reverse();
-   
-    aWires.Append(W);
+    BB.Add(F, W);
   }
   
-  BRepBuilderAPI_MakeFace aFBuilder(pln, TopoDS::Wire(aWires(1)));
-  for (int i = 2; i <= aWires.Length(); i++)
-    aFBuilder.Add(TopoDS::Wire(aWires(i)));
-  F = TopoDS::Face(aFBuilder.Shape());
-
+  //Add surface to the face
+  BB.UpdateFace(F, aPlaneSur, TopLoc_Location(), Precision::Confusion());
+  F.Closed(Standard_True);
 }
 
 int HYDROData_ShapeFile::ImportPolygons(const QString theFileName, QStringList& thePolygonsList, TopTools_SequenceOfShape& theFaces, int& theShapeTypeOfFile)