Salome HOME
++ shapefil
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImportPolylineOp.cxx
index 210ccf587af15d6c3465bcce95a49bd843a1a8cb..a3c7f833205e32a8f7d36fafb8656da35172235a 100644 (file)
@@ -41,7 +41,7 @@
 #include <QApplication>
 #include <QFile>
 #include <QFileInfo>
-#include <QMessageBox>
+#include <SUIT_MessageBox.h>
 
 
 HYDROGUI_ImportPolylineOp::HYDROGUI_ImportPolylineOp( HYDROGUI_Module* theModule )
@@ -77,173 +77,95 @@ void HYDROGUI_ImportPolylineOp::onApply()
     return;
   }
 
-  QString aFileName = myFileDlg->selectedFile();
-  if ( aFileName.isEmpty() )
+  QStringList aFileNames = myFileDlg->selectedFiles();
+  
+  QApplication::setOverrideCursor( Qt::WaitCursor );  
+  startDocOperation();
+
+  foreach (QString aFileName, aFileNames) 
   {
-    abort();
-    return;
-  }
+    if ( aFileName.isEmpty() )
+      continue;
 
-  QString anExt = aFileName.split('.', QString::SplitBehavior::SkipEmptyParts).back();
+    QString anExt = aFileName.split('.', QString::SkipEmptyParts).back();
 
-  if (anExt == "shp")
-  {
-    SHPHandle aHSHP;
-    aHSHP = SHPOpen( aFileName.toAscii().data(), "rb" );
-    Parse(aHSHP);
-    
-    QApplication::setOverrideCursor( Qt::WaitCursor );
-    
-    startDocOperation();
+    if (anExt == "shp")
+    {
+      SHPHandle aHSHP;
+      aHSHP = SHPOpen( aFileName.toAscii().data(), "rb" );
+      Parse(aHSHP);
 
-    HYDROData_Iterator anIter( doc() );
-    int anInd = 0;
-    QStringList anExistingNames;
-    std::vector<int> anAllowedIndexes;
-    for( ; anIter.More(); anIter.Next() )
-      anExistingNames.push_back(anIter.Current()->GetName());
+      HYDROData_Iterator anIter( doc() );
+      int anInd = 0;
+      QStringList anExistingNames;
+      std::vector<int> anAllowedIndexes;
+      for( ; anIter.More(); anIter.Next() )
+        anExistingNames.push_back(anIter.Current()->GetName());
 
-    QFileInfo aFileInfo(aFileName);
-    QString aBaseFileName = aFileInfo.baseName();
+      QFileInfo aFileInfo(aFileName);
+      QString aBaseFileName = aFileInfo.baseName();
 
-    if (aHSHP->nShapeType == 3 || aHSHP->nShapeType == 23)
-    {
-      anInd = 0;
-      for (;anAllowedIndexes.size() < mySHPObjects.size();)
+      if (aHSHP->nShapeType == 3 || aHSHP->nShapeType == 23)
       {
-        if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)))
+        anInd = 0;
+        for (;anAllowedIndexes.size() < mySHPObjects.size();)
         {
-          anAllowedIndexes.push_back(anInd);
-          anInd++;
+          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
-          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();)
+      else if (aHSHP->nShapeType == 13)
       {
-        if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)) &&
-            !anExistingNames.contains(aBaseFileName + "_Poly3D_" + QString::number(anInd)) &&
-            !anExistingNames.contains(aBaseFileName + "_Bath_" + QString::number(anInd)))
+        anInd = 0;
+        for (;anAllowedIndexes.size() < mySHPObjects.size();)
         {
-          anAllowedIndexes.push_back(anInd);
-          anInd++;
+          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++;
         }
-        else
-          anInd++;
+        for (size_t i = 0; i < mySHPObjects.size(); i++ )
+          ProcessSHPPoly3D(mySHPObjects[i], aBaseFileName, anAllowedIndexes[i]);
+      }
+      else  
+      {
+        SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "IMPORT_POLYLINE" ), "Cannot import polyline;\nThe shape type is incorrect" );
       }
-      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]);
+      for (size_t i = 0; i < mySHPObjects.size(); i++ )
+        free (mySHPObjects[i]);
 
-    mySHPObjects.clear();
-    SHPClose(aHSHP);
+      mySHPObjects.clear();
+      SHPClose(aHSHP);
+    }
   }
-  else if (anExt == "sx")
+  if (!aFileNames.empty())
   {
-    QFile aFile (aFileName);
-    aFile.open(QIODevice::ReadOnly);
-
-    Parse(aFile);
-
-    QApplication::setOverrideCursor( Qt::WaitCursor );
-
-    startDocOperation();
-
-    ProcessSX();
-
     commitDocOperation();
     commit();
-    aFile.close();
+    module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
   }
-
-  module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
+  else
+    abort();
   
   QApplication::restoreOverrideCursor();
 }
 
-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 ) );
-
-  Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( doc()->CreateObject( KIND_BATHYMETRY ) );
-  HYDROData_Bathymetry::AltitudePoints aAPoints;
-
-  int aNSect = myCurveBlocks.size();
-  for ( int i = 0 ; i < aNSect ; i++ )
-  {
-    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]);
-    }
-  }
-  QString aFileName = myFileDlg->selectedFile();
-  QFileInfo aFileInfo(aFileName);
-  QString aBaseFileName = aFileInfo.baseName();
-  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) ||
-        HYDROGUI_Tool::FindObjectByName( module(), aPoly3DName, KIND_POLYLINE);)
-  {
-    aBathName = aBaseFileName + "_bath_" + QString::number(anInd);
-    aPolyXYName = aBaseFileName + "_polyXY_" + QString::number(anInd);
-    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)
@@ -371,109 +293,6 @@ void HYDROGUI_ImportPolylineOp::ProcessSHPPoly3D(SHPObject* anObj, QString theFi
   module()->setIsToUpdate( aPolylineObj );
 }
 
-
-bool HYDROGUI_ImportPolylineOp::Parse( QFile& theFile)
-{
-  if ( !theFile.isOpen() )
-    return false;
-
-  QString aLine;
-  QString aBLine;
-  QStringList aList;
-  QStringList aBList;
-  myCurveBlocks.clear();
-  bool aTotStat = true;
-
-  aLine = theFile.readLine().simplified();
-  aList = aLine.split( ' ', QString::SkipEmptyParts );
-
-  for (;!theFile.atEnd();) 
-  {
-    if (aList[0] == "B" && (aList[1] == "C" || aList[1] == "P" || aList[1] == "N"))
-    {  
-      HYDROGUI_CurveBlock aCurveBlockInfo;
-      if (aList[1] == "C")
-        aCurveBlockInfo.myType = 1;
-      else if (aList[1] == "P")
-        aCurveBlockInfo.myType = 2;
-      else if (aList[1] == "N")
-        aCurveBlockInfo.myType = 2;
-
-      if (aList.size() == 9)
-      {
-        for (int j = 2; j < 8; j++)
-          aCurveBlockInfo.myRefCoords.push_back(aList[j].toDouble());
-        aCurveBlockInfo.myRefRatio = aList[8].toDouble();
-      }
-
-      QString Name;
-      do
-      {
-        aBLine = theFile.readLine().simplified();
-        aBList = aBLine.split( ' ', QString::SkipEmptyParts );
-         
-        if (aBList[0] == "CP")
-        {
-          if (aBList.size() == 2 && (aBList[1] == "0" || aBList[1] == "1" || aBList[1] == "2"))
-            aCurveBlockInfo.myCurvePlane = aBList[1].toInt();
-          else if (aBList.size() == 3 && (aBList[1] == "0" || aBList[1] == "1") && (aBList[2] == "0" || aBList[2] == "1"))
-          {
-            aCurveBlockInfo.myIsClosed = aBList[1].toInt();
-            aCurveBlockInfo.myIsClosed = aBList[2].toInt();
-          }
-          else
-          {
-            for (int j = 1; j < aBList.size(); j++)
-              aCurveBlockInfo.myAdditionalCurveInfo.push_back(aBList[j].toDouble());
-          }
-        }
-        if (aBList[0] == "CN")
-        {
-           for (int i = 1; i < aBList.size(); i++)
-             Name += aBList[i] + "_"; 
-           Name.remove(Name.size() - 1, 1);
-           aCurveBlockInfo.myName = Name;
-        }
-      } while (!theFile.atEnd() && aBLine[0] == 'C' );
-
-      bool aStat;
-      aTotStat = true;
-      do
-      {
-        if (aBList.size() >= 3 && aBLine[0] != 'B' && aBLine[0] != 'C') {
-          gp_XYZ anXYZ;
-          anXYZ.SetX (aBList[0].toDouble(&aStat));  
-          aTotStat = aTotStat && aStat;
-          anXYZ.SetY (aBList[1].toDouble(&aStat));
-          aTotStat = aTotStat && aStat;
-          anXYZ.SetZ (aBList[2].toDouble(&aStat));
-          aTotStat = aTotStat && aStat;
-
-          aCurveBlockInfo.myXYZPoints.push_back(anXYZ);
-          
-          aBLine = theFile.readLine().simplified();
-          aBList = aBLine.split( ' ', QString::SkipEmptyParts );
-        }
-        else 
-          break;
-    
-      } while (!theFile.atEnd()/* && aBLine[0] == 'B'*/ );
-      if (aTotStat)
-        myCurveBlocks.push_back(aCurveBlockInfo);
-
-    }
-    else
-    {
-      aLine = theFile.readLine().simplified();
-      aList = aLine.split( ' ', QString::SkipEmptyParts );
-    }
-
-  }
-
-  return true;
-
-}
-
 void HYDROGUI_ImportPolylineOp::Parse(SHPHandle theHandle)
 {
   int aShapeType;