Salome HOME
refs #640: correct width for non-georeferenced profile
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImportLandCoverOp.cxx
index d072688c30e53b983e8fee7cd6092135c181de2c..6b026e90349e3693d935514f2b18808b5a64e80a 100644 (file)
@@ -33,7 +33,7 @@
 
 #include <HYDROGUI_DataObject.h>
 #include <HYDROData_Iterator.h>
-
+#include <HYDROData_ShapeFile.h>
 #include <HYDROData_Profile.h>
 
 #include <SUIT_Desktop.h>
 #include <TopoDS.hxx>
 #include <TopoDS_Shape.hxx>
 #include <TopoDS_Wire.hxx>
-#include <BRepBuilderAPI_MakeEdge.hxx>
-#include <BRepBuilderAPI_MakeWire.hxx>
-#include <BRepBuilderAPI_MakeFace.hxx>
-#include <gp_Pln.hxx>
-#include <BRepLib.hxx>
-#include <ShapeFix_Shape.hxx>
+
 
 
 HYDROGUI_ImportLandCoverOp::HYDROGUI_ImportLandCoverOp( HYDROGUI_Module* theModule )
@@ -187,67 +182,6 @@ bool HYDROGUI_ImportLandCoverOp::processApply( int& theUpdateFlags,
 }
 
 
-void HYDROGUI_ImportLandCoverOp::Parse(SHPHandle theHandle)
-{
-  int aShapeType;
-  mySHPObjects.clear();
-  SHPGetInfo( theHandle, NULL, &aShapeType, NULL, NULL );
-  if (aShapeType == 5) 
-  {
-    for (int i = 0; i < theHandle->nRecords; i++) 
-      mySHPObjects.push_back(SHPReadObject(theHandle, i));
-  }
-}
-
-void HYDROGUI_ImportLandCoverOp::ProcessSHP(SHPObject* anObj, int i, TopoDS_Face& F)
-{
-  TopoDS_Wire W;
-  TopoDS_Edge E; 
-  int nParts = anObj->nParts;
-  gp_Pln pln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
-  BRepBuilderAPI_MakeFace aFBuilder(pln);
-
-  //Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
-  //sfs->FixFaceTool()->FixOrientationMode() = 1;
-  
-  for ( int i = 0 ; i < nParts ; i++ )
-  { 
-    BRepBuilderAPI_MakeWire aBuilder;
-    int StartIndex = anObj->panPartStart[i];
-    int EndIndex;
-    if (i != nParts - 1)
-      EndIndex = anObj->panPartStart[i + 1];
-    else
-      EndIndex = anObj->nVertices;
-
-    for ( int k = StartIndex; k < EndIndex - 1  ; k++ )
-    {
-      gp_Pnt P1 (anObj->padfX[k], anObj->padfY[k], 0);
-      gp_Pnt P2 (anObj->padfX[k+1], anObj->padfY[k+1], 0);
-      if (P1.Distance(P2) < Precision::Confusion())
-        continue;
-      BRepBuilderAPI_MakeEdge aMakeEdge(P1, P2);
-      aBuilder.Add(TopoDS::Edge(aMakeEdge.Shape()));
-    }
-    
-    aBuilder.Build();
-    W = TopoDS::Wire(aBuilder.Shape());
-    W.Reverse();
-    aFBuilder.Add(W);
-  }
-
-  aFBuilder.Build();
-  TopoDS_Face DF = aFBuilder.Face();
-  BRepLib::BuildCurves3d(DF);  
-  bool IsInf = DF.Infinite();
-  if(!DF.IsNull()) 
-  {
-    //sfs->Init ( DF );
-    //sfs->Perform();
-    F = DF; //TopoDS::Face(sfs->Shape());
-  }
-}
-
 void HYDROGUI_ImportLandCoverOp::onFileSelected()
 {
   HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
@@ -273,88 +207,89 @@ void HYDROGUI_ImportLandCoverOp::onFileSelected()
   QString anExt = aFileName.split('.', QString::SkipEmptyParts).back();
 
   if (anExt == "shp")
-  {
-    SHPHandle aHSHP;
-    aHSHP = SHPOpen( aFileName.toAscii().data(), "rb" );
-    Parse(aHSHP);
+  {     
+    startDocOperation();    
+    QApplication::setOverrideCursor(Qt::WaitCursor);
     
-    startDocOperation();
     QStringList aPolygonsList;
-    for (int i = 0; i < mySHPObjects.size(); i++)
-      aPolygonsList.append("polygon_" + QString::number(i + 1));
-    aPanel->setPolygonNames(aPolygonsList);
+    TopTools_SequenceOfShape aFaces;
+    HYDROData_ShapeFile anImporter;
 
     SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
-    if ( !aStudy ) {
+    if ( !aStudy )
       return;
-    }
 
     erasePreview();
 
     Handle(AIS_InteractiveContext) aCtx = NULL;
+    int aShapeTypeOfFile = -1;
+    int aStat = anImporter.ImportLandCovers(aFileName, aPolygonsList, aFaces, aShapeTypeOfFile);
+    if (aStat == 1)
+    {
+      aPanel->setPolygonNames(aPolygonsList);
 
-    LightApp_Application* anApp = module()->getApp();
-    if ( !getPreviewManager() )
-      setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
-    OCCViewer_ViewManager* aViewManager = getPreviewManager();
+      LightApp_Application* anApp = module()->getApp();
+      if ( !getPreviewManager() )
+        setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
+      OCCViewer_ViewManager* aViewManager = getPreviewManager();
 
-    if ( aViewManager )
-    {
-      if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
+      if ( aViewManager )
       {
-        aCtx = aViewer->getAISContext();
-        connect( aViewer, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
+        if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
+        {
+          aCtx = aViewer->getAISContext();
+          connect( aViewer, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
+        }
       }
-    }
 
-    QApplication::setOverrideCursor(Qt::WaitCursor);
-
-    TopTools_SequenceOfShape aFaces;
-    TopoDS_Face aF;
-    if (aHSHP->nShapeType == 5)
-    {
-      for (int i = 0; i < mySHPObjects.size(); i++) 
+      Handle(HYDROData_LandCover) aLC = Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) );
+      for ( int i = 1; i <= aFaces.Length(); i++ ) 
       {
-         ProcessSHP(mySHPObjects[i], i, aF);
-         aFaces.Append(aF);
+        TopoDS_Face aFace = TopoDS::Face(aFaces.Value( i ));
+
+        aLC->setShape( aFace );      
+        
+        if ( aViewManager && !aCtx.IsNull() )
+        {
+          HYDROGUI_Shape* aShape = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
+
+          aShape->setFillingColor( HYDROData_LandCover::DefaultFillingColor(), false, false );
+          aShape->setBorderColor( HYDROData_LandCover::DefaultBorderColor(), false, false );
+          if( !aFace.IsNull() )
+            aShape->setShape( aLC->GetShape() );
+          myPolygonName2PrsShape.insert( "polygon_" + QString::number(i), aShape);
+        }
       }
-    }
-    else
-      SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "IMPORT_LANDCOVER" ), "Cannot import land cover;\nThe shape type is not polygon" );
-    QApplication::restoreOverrideCursor();
+      aLC->Remove();
 
-    Handle(HYDROData_LandCover) aLC = Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) );
-    for ( int i = 1; i <= aFaces.Length(); i++ ) 
-    {
-      TopoDS_Face aFace = TopoDS::Face(aFaces.Value( i ));
-
-      aLC->setShape( aFace );      
-      
-      if ( aViewManager && !aCtx.IsNull() )
+      if ( !aCtx.IsNull() ) 
       {
-        HYDROGUI_Shape* aShape = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
-
-        aShape->setFillingColor( HYDROData_LandCover::DefaultFillingColor(), false, false );
-        aShape->setBorderColor( HYDROData_LandCover::DefaultBorderColor(), false, false );
-        if( !aFace.IsNull() )
-          aShape->setShape( aLC->GetShape() );
-        myPolygonName2PrsShape.insert( "polygon_" + QString::number(i), aShape);
+        UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, Graphic3d_ZLayerId_TopOSD );
+        aCtx->UpdateCurrentViewer();
       }
+      
+      QApplication::restoreOverrideCursor();
+      commitDocOperation();
     }
-    aLC->Remove();
-
-    if ( !aCtx.IsNull() ) {
-      UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, Graphic3d_ZLayerId_TopOSD );
-      aCtx->UpdateCurrentViewer();
+    else
+    {
+      erasePreview();
+      aPanel->setPolygonNames(QStringList());
+      aPanel->setObjectName("");
+      QApplication::restoreOverrideCursor();
+      QString aMess = "Cannot import land cover;\n";
+      if (aStat == -1)
+        aMess += "Cannot open SHP file";
+      else if (aStat == -2)
+        aMess += "Cannot open SHX file";
+      else 
+        aMess += "The shape type of file is " + anImporter.GetShapeTypeName(aShapeTypeOfFile);
+      SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "IMPORT_LANDCOVER" ), aMess);
+      commitDocOperation();
+      //abort();
     }
+    anImporter.Free();
 
-    commitDocOperation();
-    
-    for (size_t i = 0; i < mySHPObjects.size(); i++ )
-      free (mySHPObjects[i]);
-
-    mySHPObjects.clear();
-    SHPClose(aHSHP);
   }
   
 }