Salome HOME
Merge branch 'BR_LAND_COVER' of ssh://git.salome-platform.org/modules/hydro into...
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImportPolylineOp.cxx
index f13933dd37fba2678e5eec2dfb0a385c92c9816c..210ccf587af15d6c3465bcce95a49bd843a1a8cb 100644 (file)
@@ -30,6 +30,7 @@
 #include <HYDROData_Polyline3D.h>
 #include <HYDROGUI_DataObject.h>
 #include <HYDROData_Bathymetry.h>
+#include <HYDROData_Iterator.h>
 
 #include <HYDROData_Profile.h>
 
@@ -40,6 +41,8 @@
 #include <QApplication>
 #include <QFile>
 #include <QFileInfo>
+#include <QMessageBox>
+
 
 HYDROGUI_ImportPolylineOp::HYDROGUI_ImportPolylineOp( HYDROGUI_Module* theModule )
 : HYDROGUI_Operation( theModule )
@@ -81,30 +84,99 @@ void HYDROGUI_ImportPolylineOp::onApply()
     return;
   }
 
-  QFile aFile (aFileName);
-  aFile.open(QIODevice::ReadOnly);
+  QString anExt = aFileName.split('.', QString::SplitBehavior::SkipEmptyParts).back();
+
+  if (anExt == "shp")
+  {
+    SHPHandle aHSHP;
+    aHSHP = SHPOpen( aFileName.toAscii().data(), "rb" );
+    Parse(aHSHP);
+    
+    QApplication::setOverrideCursor( Qt::WaitCursor );
+    
+    startDocOperation();
+
+    HYDROData_Iterator anIter( doc() );
+    int anInd = 0;
+    QStringList anExistingNames;
+    std::vector<int> anAllowedIndexes;
+    for( ; anIter.More(); anIter.Next() )
+      anExistingNames.push_back(anIter.Current()->GetName());
 
-  Parse(aFile);
+    QFileInfo aFileInfo(aFileName);
+    QString aBaseFileName = aFileInfo.baseName();
+
+    if (aHSHP->nShapeType == 3 || aHSHP->nShapeType == 23)
+    {
+      anInd = 0;
+      for (;anAllowedIndexes.size() < mySHPObjects.size();)
+      {
+        if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)))
+        {
+          anAllowedIndexes.push_back(anInd);
+          anInd++;
+        }
+        else
+          anInd++;
+      }
+      
+      for (size_t i = 0; i < mySHPObjects.size(); i++ )
+      {
+        ProcessSHPPolyXY(mySHPObjects[i], aBaseFileName, anAllowedIndexes[i]);
+      }
+    }
+    else if (aHSHP->nShapeType == 13)
+    {
+      anInd = 0;
+      for (;anAllowedIndexes.size() < mySHPObjects.size();)
+      {
+        if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)) &&
+            !anExistingNames.contains(aBaseFileName + "_Poly3D_" + QString::number(anInd)) &&
+            !anExistingNames.contains(aBaseFileName + "_Bath_" + QString::number(anInd)))
+        {
+          anAllowedIndexes.push_back(anInd);
+          anInd++;
+        }
+        else
+          anInd++;
+      }
+      for (size_t i = 0; i < mySHPObjects.size(); i++ )
+        ProcessSHPPoly3D(mySHPObjects[i], aBaseFileName, anAllowedIndexes[i]);
+    }
+    commitDocOperation();
+    commit();
+    
+    for (size_t i = 0; i < mySHPObjects.size(); i++ )
+      free (mySHPObjects[i]);
 
-  QApplication::setOverrideCursor( Qt::WaitCursor );
+    mySHPObjects.clear();
+    SHPClose(aHSHP);
+  }
+  else if (anExt == "sx")
+  {
+    QFile aFile (aFileName);
+    aFile.open(QIODevice::ReadOnly);
 
-  startDocOperation();
+    Parse(aFile);
 
-  Process();
+    QApplication::setOverrideCursor( Qt::WaitCursor );
 
-  commitDocOperation();
-  commit();
+    startDocOperation();
 
-  module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
+    ProcessSX();
 
-  aFile.close();
+    commitDocOperation();
+    commit();
+    aFile.close();
+  }
 
+  module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
+  
   QApplication::restoreOverrideCursor();
 }
 
-void HYDROGUI_ImportPolylineOp::Process()
+void HYDROGUI_ImportPolylineOp::ProcessSX()
 {
-
   Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( doc()->CreateObject( KIND_POLYLINEXY ) );
 
   Handle(HYDROData_Polyline3D) aPolylineObj = Handle(HYDROData_Polyline3D)::DownCast( doc()->CreateObject( KIND_POLYLINE ) );
@@ -118,15 +190,15 @@ void HYDROGUI_ImportPolylineOp::Process()
     bool aSectClosure = true;
     HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE; 
     aPolylineXY->AddSection( TCollection_AsciiString(myCurveBlocks[i].myName.toStdString().c_str()), aSectType, myCurveBlocks[i].myIsConnected );
-
+  
     for ( int k = 0 ; k < myCurveBlocks[i].myXYZPoints.size() ; k+=3 )
     {
       HYDROData_PolylineXY::Point aSectPoint;
       aSectPoint.SetX( myCurveBlocks[i].myXYZPoints[k].X() );
       aSectPoint.SetY( myCurveBlocks[i].myXYZPoints[k].Y() );
       aPolylineXY->AddPoint( i, aSectPoint );
-
-         aAPoints.Append(myCurveBlocks[i].myXYZPoints[k]);
+  
+      aAPoints.Append(myCurveBlocks[i].myXYZPoints[k]);
     }
   }
   QString aFileName = myFileDlg->selectedFile();
@@ -135,7 +207,7 @@ void HYDROGUI_ImportPolylineOp::Process()
   QString aBathName = aBaseFileName + "_bath_1";
   QString aPolyXYName = aBaseFileName + "_polyXY_1";
   QString aPoly3DName = aBaseFileName + "_poly3D_1";
-
+  
   int anInd = 2;
   for (;HYDROGUI_Tool::FindObjectByName( module(), aBathName, KIND_BATHYMETRY) || 
         HYDROGUI_Tool::FindObjectByName( module(), aPolyXYName, KIND_POLYLINEXY) ||
@@ -146,6 +218,133 @@ void HYDROGUI_ImportPolylineOp::Process()
     aPoly3DName = aBaseFileName + "_poly3D_" + QString::number(anInd);
     anInd++;
   }
+  
+  aPolylineXY->SetName( aPolyXYName );
+  aPolylineXY->SetWireColor(HYDROData_PolylineXY::DefaultWireColor());
+  aPolylineXY->Update();
+  
+  aBath->SetAltitudePoints(aAPoints);
+  aBath->SetName( aBathName );
+  
+  aPolylineObj->SetPolylineXY (aPolylineXY, false);
+  aPolylineObj->SetAltitudeObject(aBath);
+  
+  aPolylineObj->SetBorderColor( HYDROData_Polyline3D::DefaultBorderColor() );
+  aPolylineObj->SetName( aPoly3DName );
+  
+  aPolylineObj->Update();
+  
+  size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
+  if ( anActiveViewId == 0 )
+    anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
+  
+  module()->setObjectVisible( anActiveViewId, aPolylineXY, true );
+  module()->setObjectVisible( anActiveViewId, aPolylineObj, true );
+  
+  module()->setIsToUpdate( aPolylineObj );
+}
+
+void HYDROGUI_ImportPolylineOp::ProcessSHPPolyXY(SHPObject* anObj, QString theFileName, int theInd)
+{
+  //if (anObj->nSHPType != SHPT_ARC && anObj->nSHPType != SHPT_ARCM)
+  // return false;
+  Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( doc()->CreateObject( KIND_POLYLINEXY ) );
+  
+  int nParts = anObj->nParts;
+  for ( int i = 0 ; i < nParts ; i++ )
+  {
+    int StartIndex = anObj->panPartStart[i];
+    int EndIndex;
+    if (i != nParts - 1)
+      EndIndex = anObj->panPartStart[i + 1];
+    else
+      EndIndex = anObj->nVertices;
+
+    bool IsClosed = false;
+    HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE; 
+    if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
+        anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] )
+    {
+      IsClosed = true;
+      aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true);
+    }
+    else
+      aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, false);
+    
+    if (IsClosed)
+      EndIndex--;
+    for ( int k = StartIndex; k < EndIndex ; k++ )
+    {
+      HYDROData_PolylineXY::Point aSectPoint;
+      aSectPoint.SetX( anObj->padfX[k] );
+      aSectPoint.SetY( anObj->padfY[k] );
+      aPolylineXY->AddPoint( i, aSectPoint );
+    }
+
+  }
+  
+  aPolylineXY->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
+  aPolylineXY->SetName( theFileName + "_PolyXY_" + QString::number(theInd) );
+  
+  aPolylineXY->Update();
+  
+  size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
+  if ( anActiveViewId == 0 )
+    anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
+  
+  module()->setObjectVisible( anActiveViewId, aPolylineXY, true );
+  
+  module()->setIsToUpdate( aPolylineXY );
+}
+
+void HYDROGUI_ImportPolylineOp::ProcessSHPPoly3D(SHPObject* anObj, QString theFileName, int theInd)
+{
+  Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( doc()->CreateObject( KIND_POLYLINEXY ) );
+
+  Handle(HYDROData_Polyline3D) aPolylineObj = Handle(HYDROData_Polyline3D)::DownCast( doc()->CreateObject( KIND_POLYLINE ) );
+
+  Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( doc()->CreateObject( KIND_BATHYMETRY ) );
+  HYDROData_Bathymetry::AltitudePoints aAPoints;
+
+  int nParts = anObj->nParts;
+  for ( int i = 0 ; i < nParts ; i++ )
+  {
+    //bool aSectClosure = true;
+    int StartIndex = anObj->panPartStart[i];
+    int EndIndex;
+    if (i != nParts - 1)
+      EndIndex = anObj->panPartStart[i + 1];
+    else
+      EndIndex = anObj->nVertices;
+
+    bool IsClosed = false;
+    HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE; 
+    if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
+        anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] &&
+        anObj->padfZ[StartIndex] == anObj->padfZ[EndIndex - 1])
+    {
+      IsClosed = true;
+      aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true );
+    }
+    else
+      aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, false  );
+    
+    if (IsClosed)
+      EndIndex--;
+    for ( int k = StartIndex ; k < EndIndex ; k++ )
+    {
+      HYDROData_PolylineXY::Point aSectPoint;
+      aSectPoint.SetX( anObj->padfX[k] );
+      aSectPoint.SetY( anObj->padfY[k] );
+      aPolylineXY->AddPoint( i, aSectPoint );
+      aAPoints.Append(gp_XYZ (anObj->padfX[k], anObj->padfY[k], anObj->padfZ[k]));
+    }
+  }
+
+
+  QString aBathName = theFileName + "_bath_" + QString::number(theInd);
+  QString aPolyXYName = theFileName + "_polyXY_" + QString::number(theInd);
+  QString aPoly3DName = theFileName + "_poly3D_" + QString::number(theInd);
 
   aPolylineXY->SetName( aPolyXYName );
   aPolylineXY->SetWireColor(HYDROData_PolylineXY::DefaultWireColor());
@@ -162,7 +361,6 @@ void HYDROGUI_ImportPolylineOp::Process()
   
   aPolylineObj->Update();
 
-
   size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
   if ( anActiveViewId == 0 )
     anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
@@ -173,6 +371,7 @@ void HYDROGUI_ImportPolylineOp::Process()
   module()->setIsToUpdate( aPolylineObj );
 }
 
+
 bool HYDROGUI_ImportPolylineOp::Parse( QFile& theFile)
 {
   if ( !theFile.isOpen() )
@@ -273,4 +472,16 @@ bool HYDROGUI_ImportPolylineOp::Parse( QFile& theFile)
 
   return true;
 
+}
+
+void HYDROGUI_ImportPolylineOp::Parse(SHPHandle theHandle)
+{
+  int aShapeType;
+  mySHPObjects.clear();
+  SHPGetInfo( theHandle, NULL, &aShapeType, NULL, NULL );
+  if (aShapeType == 3 || aShapeType == 13 || aShapeType == 23) 
+  {
+    for (int i = 0; i < theHandle->nRecords; i++) 
+      mySHPObjects.push_back(SHPReadObject(theHandle, i));
+  }
 }
\ No newline at end of file