Salome HOME
TShell cannot be disconnected; use TCompound of shells/faces instead
[modules/hydro.git] / src / HYDROData / HYDROData_ShapeFile.cxx
index 3aa6ac7e589ef88c5e4d1bb13abdcae9c1a68c6b..b6a4e737c336a6bff237188c8653a794f6831e70 100644 (file)
@@ -1,8 +1,4 @@
-// Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
-//
-// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
-//
+// Copyright (C) 2014-2015  EDF-R&D
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 // License as published by the Free Software Foundation; either
@@ -26,6 +22,7 @@
 #include <HYDROData_Bathymetry.h>
 #include <HYDROData_Profile.h>
 #include <HYDROData_Iterator.h>
+#include <HYDROData_LandCoverMap.h>
 
 #include <QFile>
 #include <QFileInfo>
@@ -36,6 +33,7 @@
 #include <TopoDS_Edge.hxx>
 #include <TopoDS_Face.hxx>
 #include <BRep_Tool.hxx>
+#include <BRepTools.hxx>
 #include <Precision.hxx>
 #include <Handle_Geom_Curve.hxx>
 #include <Handle_Geom_Line.hxx>
 #include <TopTools_SequenceOfShape.hxx>
 #include <QColor>
 #include <BRepTopAdaptor_FClass2d.hxx>
+#include <TopExp.hxx>
+
+#ifdef WIN32
+  #pragma warning( disable: 4996 )
+#endif
 
 HYDROData_ShapeFile::HYDROData_ShapeFile() : myHSHP(NULL)
 { 
@@ -63,10 +66,8 @@ HYDROData_ShapeFile::~HYDROData_ShapeFile()
 void HYDROData_ShapeFile::Export(const QString& aFileName, 
   NCollection_Sequence<Handle_HYDROData_PolylineXY> aPolyXYSeq,
   NCollection_Sequence<Handle_HYDROData_Polyline3D> aPoly3DSeq,
-  const Handle_HYDROData_LandCoverMap& aLCSeq,
   QStringList& aNonExpList)
 {
-  /*TODO
   SHPHandle hSHPHandle;
   if (!aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty())
   {
@@ -74,7 +75,6 @@ void HYDROData_ShapeFile::Export(const QString& aFileName,
     for (int i = 1; i <= aPolyXYSeq.Size(); i++)
       if (WriteObjectPolyXY(hSHPHandle, aPolyXYSeq(i)) != 1)
         aNonExpList.append(aPolyXYSeq(i)->GetName());
-
   }
   else if (aPolyXYSeq.IsEmpty() && !aPoly3DSeq.IsEmpty())
   {
@@ -83,12 +83,31 @@ void HYDROData_ShapeFile::Export(const QString& aFileName,
       if (WriteObjectPoly3D(hSHPHandle, aPoly3DSeq(i)) != 1)
         aNonExpList.append(aPoly3DSeq(i)->GetName());
   }
-  else if (aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty() && !aLCSeq.IsEmpty())
+  if (hSHPHandle->nRecords > 0)
+    SHPClose( hSHPHandle );
+  else
+  {
+    SHPClose( hSHPHandle );
+    QString aFN = aFileName.simplified();
+    remove (aFN.toStdString().c_str());
+    remove (aFN.replace( ".shp", ".shx", Qt::CaseInsensitive).toStdString().c_str());
+  }
+}
+
+void HYDROData_ShapeFile::Export(const QString& aFileName,
+  const Handle_HYDROData_LandCoverMap& aLCM, QStringList& aNonExpList)
+{
+  SHPHandle hSHPHandle;
+  if ( !aLCM.IsNull() && !aLCM->IsEmpty())
   {
     hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_POLYGON );
-    for (int i = 1; i <= aLCSeq.Size(); i++)
-      if (WriteObjectLC(hSHPHandle, aLCSeq(i)) != 1)
-        aNonExpList.append(aLCSeq(i)->GetName());
+    HYDROData_LandCoverMap::Explorer It( aLCM );
+    for( ; It.More(); It.Next() )
+    {
+      TopoDS_Face aFace = It.Face();
+      if (WriteObjectPolygon(hSHPHandle, aFace) != 1)
+        aNonExpList.append(aLCM->GetName() + "_" +  QString::number(It.Index()));
+    }
   }
   if (hSHPHandle->nRecords > 0)
     SHPClose( hSHPHandle );
@@ -98,9 +117,10 @@ void HYDROData_ShapeFile::Export(const QString& aFileName,
     QString aFN = aFileName.simplified();
     remove (aFN.toStdString().c_str());
     remove (aFN.replace( ".shp", ".shx", Qt::CaseInsensitive).toStdString().c_str());
-  }*/
+  }
 }
 
+
 int HYDROData_ShapeFile::WriteObjectPolyXY(SHPHandle theShpHandle, Handle_HYDROData_PolylineXY thePoly )
 {
   SHPObject    *aSHPObj;
@@ -168,12 +188,11 @@ int HYDROData_ShapeFile::WriteObjectPoly3D(SHPHandle theShpHandle, Handle_HYDROD
   return 1;
 }
 
-/*TODO:int HYDROData_ShapeFile::WriteObjectLC(SHPHandle theShpHandle, Handle_HYDROData_LandCover theLC )
+int HYDROData_ShapeFile::WriteObjectPolygon(SHPHandle theShpHandle, const TopoDS_Shape& theInputShape )
 {
-  TopoDS_Shape aSh = theLC->GetShape();
-  if (aSh.IsNull())
+  if (theInputShape.IsNull())
     return 0;
-  TopExp_Explorer anEdgeEx(aSh, TopAbs_EDGE);
+  TopExp_Explorer anEdgeEx(theInputShape, TopAbs_EDGE);
   for (; anEdgeEx.More(); anEdgeEx.Next()) 
   {
     TopoDS_Edge E = TopoDS::Edge(anEdgeEx.Current());
@@ -194,13 +213,13 @@ int HYDROData_ShapeFile::WriteObjectPoly3D(SHPHandle theShpHandle, Handle_HYDROD
     }
 
   }
-  if (aSh.ShapeType() == TopAbs_FACE)
+  if (theInputShape.ShapeType() == TopAbs_FACE)
   {
-    ProcessFace(TopoDS::Face(aSh), theShpHandle);
+    ProcessFace(TopoDS::Face(theInputShape), theShpHandle);
   }
-  else if (aSh.ShapeType() == TopAbs_COMPOUND)
+  else if (theInputShape.ShapeType() == TopAbs_COMPOUND)
   {
-    TopExp_Explorer Ex(aSh, TopAbs_FACE);
+    TopExp_Explorer Ex(theInputShape, TopAbs_FACE);
     for (; Ex.More(); Ex.Next()) 
     {
       TopoDS_Face aF = TopoDS::Face(Ex.Current());   
@@ -215,57 +234,91 @@ int HYDROData_ShapeFile::WriteObjectPoly3D(SHPHandle theShpHandle, Handle_HYDROD
   return 1;
  
 }
-*/
+
 void HYDROData_ShapeFile::ProcessFace(TopoDS_Face theFace, SHPHandle theShpHandle)
 {
+  if (theFace.ShapeType() != TopAbs_FACE)
+     return;
   SHPObject    *aSHPObj;
   std::vector<double> x, y;
   std::vector<int> anPartStart;
-  if (theFace.ShapeType() == TopAbs_FACE)
+  TopoDS_Wire OuterW = BRepTools::OuterWire(theFace);
+  NCollection_Sequence<TopoDS_Wire> aWires;
+
+  //write an outer wire first
+  aWires.Append(OuterW); 
+  TopExp_Explorer Ex(theFace, TopAbs_WIRE);  
+  for (; Ex.More(); Ex.Next()) 
   {
-    TopExp_Explorer Ex(theFace, TopAbs_WIRE);
-    int NbWires = 0;
-    for (; Ex.More(); Ex.Next()) 
+    TopoDS_Wire aW = TopoDS::Wire(Ex.Current());
+    if (aW.IsEqual(OuterW))
+      continue;
+    aWires.Append(aW); 
+  }
+
+  //TopExp_Explorer Ex(theFace, TopAbs_WIRE);
+  int NbWires = 0;
+  for (int k = 1; k <= aWires.Length(); k++) 
+  {
+    TopoDS_Wire aW = aWires(k);   
+    if (aW.IsNull())
+      continue;
+    NbWires++;
+    // Try to reorder edges
+    Handle(ShapeFix_Wire) aSFW = new ShapeFix_Wire( aW, theFace, Precision::Confusion() );
+    aSFW->FixReorder();
+    Handle(ShapeExtend_WireData) aSEWD = aSFW->WireData();
+    Standard_Integer nbE = aSEWD->NbEdges();
+    //
+    anPartStart.push_back(x.size());
+    NCollection_Sequence<gp_Pnt2d> aPnts;
+    for (Standard_Integer i = 1; i <= nbE; i++)
     {
-      TopoDS_Wire aW = TopoDS::Wire(Ex.Current());   
-      if (aW.IsNull())
+      TopoDS_Edge E = aSEWD->Edge(i);
+      TopoDS_Vertex aV = TopExp::LastVertex(E, 1);
+      if (aV.IsNull())
         continue;
-      NbWires++;
-      anPartStart.push_back(x.size());
-      TopExp_Explorer aVEx(aW, TopAbs_VERTEX);
-      NCollection_Sequence<gp_Pnt2d> aPnts;
-      for (; aVEx.More(); aVEx.Next())
-      {
-        TopoDS_Vertex aV = TopoDS::Vertex(aVEx.Current()); 
-        if (aV.IsNull())
-          continue;
-        gp_Pnt P = BRep_Tool::Pnt(aV);
-        aPnts.Append(gp_Pnt2d(P.X(), P.Y()));
-      }
-      NCollection_Sequence<gp_Pnt2d> aNPnts;
-      aNPnts.Append(aPnts.First());
-      for (int j = 1; j <= aPnts.Size() - 1; j++)
-      {
-        if (!aPnts(j).IsEqual(aPnts(j + 1), Precision::Confusion())) 
-          aNPnts.Append(aPnts(j + 1));
-      }
+      gp_Pnt P = BRep_Tool::Pnt(aV);
+      aPnts.Append(gp_Pnt2d(P.X(), P.Y()));
+    }
+    NCollection_Sequence<gp_Pnt2d> aNPnts;
+    aNPnts.Append(aPnts.First());
+    for (int j = 1; j <= aPnts.Size() - 1; j++)
+    {
+      if (!aPnts(j).IsEqual(aPnts(j + 1), Precision::Confusion())) 
+        aNPnts.Append(aPnts(j + 1));
+    }
 
+    if (k == 1)
+    {
+      //an outer wire
+      //clockwise direction
       for (int j = 1; j <= aNPnts.Size(); j++)
       { 
         x.push_back( aNPnts(j).X());
         y.push_back( aNPnts(j).Y()); 
       }
-      //x.push_back( aNPnts(1).X());
-      //y.push_back( aNPnts(1).Y()); 
-
+      x.push_back( aNPnts(1).X());
+      y.push_back( aNPnts(1).Y()); 
+    }
+    else
+    {
+      //holes
+      //counter-clockwise direction
+      for (int j = aNPnts.Size(); j > 0; j--)
+      { 
+        x.push_back( aNPnts(j).X());
+        y.push_back( aNPnts(j).Y()); 
+      }
+      x.push_back( aNPnts(aNPnts.Size()).X());
+      y.push_back( aNPnts(aNPnts.Size()).Y()); 
     }
-    
-    aSHPObj = SHPCreateObject( SHPT_POLYGON, -1, NbWires, &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
-    SHPWriteObject( theShpHandle, -1, aSHPObj );
-    SHPDestroyObject( aSHPObj );
   }
-  else
-    return;
+  
+  aSHPObj = SHPCreateObject( SHPT_POLYGON, -1, NbWires, &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
+  SHPWriteObject( theShpHandle, -1, aSHPObj );
+  SHPDestroyObject( aSHPObj );
+
 }
 
 bool HYDROData_ShapeFile::Parse(SHPHandle theHandle, ShapeType theType, int& theShapeTypeOfFile)
@@ -333,18 +386,12 @@ void HYDROData_ShapeFile::ReadSHPPolygon(SHPObject* anObj, int i, TopoDS_Face& F
   BRepBuilderAPI_MakeFace aFBuilder(pln, TopoDS::Wire(aWires(1)));
   for (int i = 2; i <= aWires.Length(); i++)
     aFBuilder.Add(TopoDS::Wire(aWires(i)));
-  TopoDS_Face DF = TopoDS::Face(aFBuilder.Shape());
+  F = TopoDS::Face(aFBuilder.Shape());
 
-  BRepLib::BuildCurves3d(DF);  
-  if(!DF.IsNull()) 
-  {
-    //sfs->Init ( DF );
-    //sfs->Perform();
-    F = DF; //TopoDS::Face(sfs->Shape());
-  }
+  BRepLib::BuildCurves3d(F);  
 }
 
-int HYDROData_ShapeFile::ImportLandCovers(const QString theFileName, QStringList& thePolygonsList, TopTools_SequenceOfShape& theFaces, int& theShapeTypeOfFile)
+int HYDROData_ShapeFile::ImportPolygons(const QString theFileName, QStringList& thePolygonsList, TopTools_SequenceOfShape& theFaces, int& theShapeTypeOfFile)
 {
   Free();
   int Stat = TryOpenShapeFile(theFileName);