]> SALOME platform Git repositories - modules/visu.git/commitdiff
Salome HOME
Implementation of Import/Export CSV tables (0020664). V5_1_main_20100318
authorskl <skl@opencascade.com>
Wed, 17 Mar 2010 09:37:10 +0000 (09:37 +0000)
committerskl <skl@opencascade.com>
Wed, 17 Mar 2010 09:37:10 +0000 (09:37 +0000)
16 files changed:
doc/salome/gui/VISU/images/exporttables1.png [new file with mode: 0644]
doc/salome/gui/VISU/images/exporttables2.png [new file with mode: 0644]
doc/salome/gui/VISU/images/importtables.png [new file with mode: 0644]
doc/salome/gui/VISU/input/importing_exporting_tables_page.doc
idl/VISU_Gen.idl
src/CONVERTOR/VISU_TableReader.cxx
src/CONVERTOR/VISU_TableReader.hxx
src/ENGINE/VISU_Engine_i.cc
src/ENGINE/VISU_Engine_i.hh
src/VISUGUI/VISU_msg_en.ts
src/VISUGUI/VisuGUI.cxx
src/VISU_I/VISU_DumpPython.cc
src/VISU_I/VISU_Gen_i.cc
src/VISU_I/VISU_Gen_i.hh
src/VISU_I/VISU_Table_i.cc
src/VISU_I/VISU_Table_i.hh

diff --git a/doc/salome/gui/VISU/images/exporttables1.png b/doc/salome/gui/VISU/images/exporttables1.png
new file mode 100644 (file)
index 0000000..8f13bb0
Binary files /dev/null and b/doc/salome/gui/VISU/images/exporttables1.png differ
diff --git a/doc/salome/gui/VISU/images/exporttables2.png b/doc/salome/gui/VISU/images/exporttables2.png
new file mode 100644 (file)
index 0000000..07093b1
Binary files /dev/null and b/doc/salome/gui/VISU/images/exporttables2.png differ
diff --git a/doc/salome/gui/VISU/images/importtables.png b/doc/salome/gui/VISU/images/importtables.png
new file mode 100644 (file)
index 0000000..8b903c2
Binary files /dev/null and b/doc/salome/gui/VISU/images/importtables.png differ
index 84de7c948f62c6bdfd66f4aeed79e85524a8feb9..9b9a4fd11591574a6446f8996d46a62917364f07 100644 (file)
@@ -10,10 +10,14 @@ select <b>Table from File</b> item.</li>
 
 <li>From the following standard dialog box:
 
-\image html importtablefromfile.png
+\image html importtables.png
+
+choose the necessary *.txt, *.tab or *.csv file containing tables and
+click \b Open button.
+
+Checkbox "Use first string as title" is checked if it is needed to use
+information from first string of file as titles of columns of table.
 
-choose the necessary *.txt or *.tab file containing tables and
-click \b Open button.  
 </li>
 
 <li>In the Object Browser \b Post-Pro will create a new folder having
@@ -30,7 +34,13 @@ tables.</li>
 contain your exported table, and click \b OK button. Your table will
 be exported.
 
-\image html exporttable.png
+You can perform export table to *.txt or *.tab file
+
+\image html exporttables1.png
+
+Or you can perform export table to *.csv file
+
+\image html exporttables2.png
 
 </li>
 </ul>
index 8946a982c5680248c3692229249d38b9c29c39f3..dded558f81903dfdd1eeb280aecf7b884e22cedb 100644 (file)
@@ -2167,9 +2167,10 @@ module VISU {
     ViewManager GetViewManager();
 
     /*!
-     * Imports tables from a file and create TableAttribute in Sudy
+     * Imports tables from a file and create TableAttribute in Study
      */
-    SALOMEDS::SObject ImportTables(in string theFileName);
+    SALOMEDS::SObject ImportTables(in string theFileName,
+                                   in boolean theFirstStrAsTitle);
 
     /*!
      * Export table to a file
index cb431dab5177bb53ca87649bf1772aa896ef6b67..50d21b1b91a3ca0a63125064ba2da6189dc9e05c 100644 (file)
@@ -135,18 +135,107 @@ namespace
 
     return !theStmIn.eof();
   }
+
+  //=======================================================================
+  //function : findNextCell
+  //purpose  : auxilary for ImportCSVTable
+  //=======================================================================
+  bool findNextCell(std::ifstream& aStmIn, QString& aStr,
+                   QString& aCell, const char theSeparator)
+  {
+    aCell = "";
+    int index, tmpind = 0;
+    if( aStr.at(0) == theSeparator ) {
+      aStr.remove(0,1);
+      aStr = aStr.trimmed();
+      if(aStr.size()==0) return true;
+    }
+    QString aTmp = aStr;
+    if( aTmp.at(0) == '"' ) {
+      // find closed "
+      while( !aStmIn.eof() ) {
+        tmpind = aTmp.indexOf('"',1);
+        if( tmpind < 0 ) {
+          while( !aStmIn.eof() ) {
+            aCell.push_back(aTmp);
+            aCell.push_back('\n');
+            ::getLine( aStmIn, aTmp );
+            tmpind = aTmp.indexOf('"',1);
+            if( tmpind >= 0 ) {
+              break;
+            }
+          }
+        }
+        if(tmpind < 0)
+          return false;
+        // read next symbol
+        if( aTmp.at(tmpind+1) == '"' ) {
+          // "" is found => need to continue
+          aCell.push_back(aTmp.left(tmpind+1));
+          aTmp = aTmp.mid(tmpind+2);
+        }
+        else if( aTmp.at(tmpind+1) == theSeparator ) {
+          index = tmpind+1;
+          break;
+        }
+        else {
+          return false;
+        }
+      }
+    }
+    else {
+      // find index for separator
+      index = aTmp.indexOf( theSeparator );
+    }
+    if( index < 0 ) {
+      aCell += aTmp;
+      aStr = "";
+    }
+    else {
+      if(index>0) aCell += aTmp.left(index);
+      aStr = aTmp.mid(index).trimmed();
+    }
+    if( aCell.size()>0 && aCell.at(0) == '"' ) {
+      // remove first and last symbols
+      int last = aCell.size()-1;
+      aCell.remove(last,1);
+      aCell.remove(0,1);
+    }
+    // replace "" to " in aCell
+    QChar ctmp = '"';
+    QString tmp(ctmp);
+    tmp += ctmp;
+    index = aCell.indexOf(tmp);
+    while(index>=0) {
+      aCell.remove(index,1);
+      index = aCell.indexOf(tmp);
+    }
+    return true;
+  }
+
 }
 
 
 //---------------------------------------------------------------
 void 
-VISU::ImportTables( const char* theFileName, TTableContainer& theContainer )
+VISU::ImportTables( const char* theFileName, TTableContainer& theContainer,
+                    bool theFirstStrAsTitle)
 {
   std::ifstream aStmIn;
   QFileInfo aFileInfo( theFileName );
   if( !aFileInfo.isFile() || !aFileInfo.isReadable() || !aFileInfo.size() )
     return;
 
+  QString tmp(theFileName);
+  tmp = tmp.trimmed();
+  tmp = tmp.right(3).trimmed();
+
+  if( tmp == QString("csv") ) {
+    const char separator = ',';
+    ImportCSVTable(theFileName, theContainer, theFirstStrAsTitle, separator);
+    return;
+  }
+
   aStmIn.open( theFileName );
   QString aTmp;
   do {
@@ -157,6 +246,7 @@ VISU::ImportTables( const char* theFileName, TTableContainer& theContainer )
     TTable2D& aTable2D = *aTableIDMapper;
     if(MYDEBUG) std::cout << "New table is found" << std::endl;
 
+    bool IsFirst = true;
     while( !aStmIn.eof() && aTmp.trimmed() != "" ){
       QString data = aTmp.trimmed();
       QString cmt = "";
@@ -231,20 +321,31 @@ VISU::ImportTables( const char* theFileName, TTableContainer& theContainer )
       else {
         TTable2D::TRow aRow;
         if(MYDEBUG) std::cout << "...New row is found: " << std::endl;
-        if ( !cmt.isEmpty() ) {
-          aRow.myTitle = cmt.toLatin1().constData();
-          if(MYDEBUG) std::cout << "......ROW TITLE is: " << cmt.toLatin1().constData() << std::endl;
-        }
         QString datar1 = data.replace(QRegExp("\t"), " ");
         QStringList aValList = datar1.split( " ", QString::SkipEmptyParts );
-        for ( int i = 0; i < aValList.count(); i++ ) {
-          if ( aValList[i].trimmed() != "" ) {
-            TTable2D::TValue aVal = aValList[i].trimmed().toLatin1().constData();
-            aRow.myValues.push_back( aVal );
+        if( aTable2D.myColumnTitles.size()==0 && IsFirst && theFirstStrAsTitle ) {
+          for ( int i = 0; i < aValList.count(); i++ ) {
+            QString tmpstr = aValList[ i ].trimmed();
+            aTable2D.myColumnTitles.push_back( tmpstr.toLatin1().constData() );
           }
         }
-        if( aRow.myValues.size() > 0 )
-          aTable2D.myRows.push_back( aRow );
+        else {
+          if ( !cmt.isEmpty() ) {
+            aRow.myTitle = cmt.toLatin1().constData();
+            if(MYDEBUG) std::cout << "......ROW TITLE is: " << cmt.toLatin1().constData() << std::endl;
+          }
+          //QString datar1 = data.replace(QRegExp("\t"), " ");
+          //QStringList aValList = datar1.split( " ", QString::SkipEmptyParts );
+          for ( int i = 0; i < aValList.count(); i++ ) {
+            if ( aValList[i].trimmed() != "" ) {
+              TTable2D::TValue aVal = aValList[i].trimmed().toLatin1().constData();
+              aRow.myValues.push_back( aVal );
+            }
+          }
+          if( aRow.myValues.size() > 0 )
+            aTable2D.myRows.push_back( aRow );
+        }
+        IsFirst = false;
         // ************** OLD CODE ******************
         /*
         TValue aVal;
@@ -271,6 +372,81 @@ VISU::ImportTables( const char* theFileName, TTableContainer& theContainer )
 }
 
 
+//=======================================================================
+//function : ImportCSVTable
+//purpose  : 
+//=======================================================================
+void VISU::ImportCSVTable(const char* theFileName, TTableContainer& theContainer,
+                          bool theFirstStrAsTitle, const char theSeparator)
+{
+  std::ifstream aStmIn;
+  QFileInfo aFileInfo( theFileName );
+  if( !aFileInfo.isFile() || !aFileInfo.isReadable() || !aFileInfo.size() )
+    return;
+  aStmIn.open( theFileName );
+  QString aTmp;
+  do {
+    // find beginning of table (tables are separated by empty lines)
+    while( ::getLine( aStmIn, aTmp ) && aTmp.trimmed() == "");
+
+    PTableIDMapper aTableIDMapper( new TTableIDMapper() );
+    TTable2D& aTable2D = *aTableIDMapper;
+    if(MYDEBUG) std::cout << "New table is found" << std::endl;
+
+    bool IsFirst = true;
+    QStringList aValList;
+
+    while( !aStmIn.eof() ) {
+      QString aCell = "";
+      if( !(::findNextCell(aStmIn, aTmp, aCell, theSeparator)) ) {
+        return;
+      }
+      if( aTmp.size()==0 ) {
+        // make table row
+        aValList.push_back(aCell);
+        if( IsFirst && theFirstStrAsTitle ) {
+          for ( int i = 0; i < aValList.count(); i++ ) {
+            aTable2D.myColumnTitles.push_back( aValList[i].trimmed().toLatin1().constData() );
+          }
+        }
+        else {
+          TTable2D::TRow aRow;
+          for ( int i = 0; i < aValList.count(); i++ ) {
+            if ( aValList[i].trimmed() != "" ) {
+              TTable2D::TValue aVal = aValList[i].trimmed().toLatin1().constData();
+              aRow.myValues.push_back( aVal );
+            }
+            else {
+              aRow.myValues.push_back( "Empty" );
+            }
+          }
+          if( aRow.myValues.size() > 0 ) {
+            aTable2D.myRows.push_back( aRow );
+          }
+        }
+        // clear list of values and read next string
+        aValList.clear();
+        ::getLine( aStmIn, aTmp );
+        IsFirst = false;
+      }
+      else {
+        // put value to table cell
+        aValList.push_back(aCell);
+      }
+    }
+
+    if( aTable2D.Check() ) {
+      if(MYDEBUG) std::cout << "aTable2D is checked OK " << aTable2D.myTitle << std::endl;
+      theContainer.push_back( aTableIDMapper );
+    }
+
+  } while ( !aStmIn.eof() );
+  aStmIn.close();
+
+  if(MYDEBUG) std::cout << "After close" << std::endl;
+}
+
+
 //---------------------------------------------------------------
 VISU::TTableIDMapper
 ::TTableIDMapper():
@@ -404,3 +580,4 @@ VISU::TTableIDMapper
   myOutput->ShallowCopy( aFilter->GetOutput() );
   aFilter->Delete();
 }
+
index d01d916d09957fde88fdbd29fd7893be8e06aaac..4e6c5114c774e23a9b63bfc2b8a3fcce63f62b26 100644 (file)
@@ -102,8 +102,13 @@ namespace VISU
   //---------------------------------------------------------------
   typedef std::vector<PTableIDMapper> TTableContainer;
   VISU_CONVERTOR_EXPORT 
-    void ImportTables( const char* theFileName, TTableContainer& theContainer );
+    void ImportTables( const char* theFileName, TTableContainer& theContainer,
+                       bool theFirstStrAsTitle = false);
 
+  void ImportCSVTable(const char* theFileName,
+                      TTableContainer& theContainer,
+                      bool theFirstStrAsTitle,
+                      const char theSeparator);
 
   //---------------------------------------------------------------
 }
index 2d0f66ad2a4cdd5fe770b9e29db33fcb1af18c47..117e547399123b073bbe8d0e40bcec8243d8d95f 100644 (file)
@@ -165,8 +165,10 @@ namespace VISU{
   }
 
 
-  SALOMEDS::SObject_ptr VISU_Gen_i::ImportTables(const char* theFileName){
-    return myVisuGen->ImportTables(theFileName);
+  SALOMEDS::SObject_ptr VISU_Gen_i::ImportTables(const char* theFileName,
+                                                 bool theFirstStrAsTitle)
+  {
+    return myVisuGen->ImportTables(theFileName,theFirstStrAsTitle);
   }
 
 
index 950246d38a81944eebe208deb66e21973d8354f1..97cff3cd0d858d831b12ca02f996689aa081b3dc 100644 (file)
@@ -57,8 +57,10 @@ namespace VISU
 
     virtual ViewManager_ptr GetViewManager();
 
-    virtual SALOMEDS::SObject_ptr ImportTables(const char* theFileName);
-    virtual CORBA::Boolean ExportTableToFile(SALOMEDS::SObject_ptr theTable, const char* theFileName);
+    virtual SALOMEDS::SObject_ptr ImportTables(const char* theFileName,
+                                               bool theFirstStrAsTitle = false);
+    virtual CORBA::Boolean ExportTableToFile(SALOMEDS::SObject_ptr theTable,
+                                             const char* theFileName);
 
     //Create Result
     virtual Result_ptr ImportFile(const char* theFileName);
@@ -68,15 +70,18 @@ namespace VISU
     virtual Result_ptr ImportMedField(SALOME_MED::FIELD_ptr theField);
 
     //Create Presentation Of Submeshes
-    virtual Mesh_ptr MeshOnEntity(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity);
+    virtual Mesh_ptr MeshOnEntity(Result_ptr theResult, const char* theMeshName,
+                                  VISU::Entity theEntity);
     virtual Mesh_ptr FamilyMeshOnEntity(Result_ptr theResult, const char* theMeshName,
                                        VISU::Entity theEntity, const char* theFamilyName);
-    virtual Mesh_ptr GroupMesh(Result_ptr theResult, const char* theMeshName, const char* theGroupName);
+    virtual Mesh_ptr GroupMesh(Result_ptr theResult, const char* theMeshName,
+                               const char* theGroupName);
 
     //Rename Presentation Of Submeshes
     virtual void RenameEntityInStudy(Result_ptr theResult, const char* theMeshName,
                                     VISU::Entity theEntity, const char* theNewName);
-    virtual void RenameFamilyInStudy(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity,
+    virtual void RenameFamilyInStudy(Result_ptr theResult, const char* theMeshName,
+                                     VISU::Entity theEntity,
                                     const char* theFamilyName, const char* theNewName);
     virtual void RenameGroupInStudy (Result_ptr theResult, const char* theMeshName,
                                     const char* theGroupName, const char* theNewName);
index d6fa0e1cb6d018638fb65bb970a2bf61db7ff16a..22d1baf4e9f2c2a335f36f56c4b0230890fc7e3e 100644 (file)
@@ -987,7 +987,7 @@ Please, refer to the documentation.</translation>
         </message>
         <message>
             <source>FLT_TABLE_FILES</source>
-            <translation>Tables (*.txt *.tab)</translation>
+            <translation>Tables (*.txt *.tab *.csv)</translation>
         </message>
         <message>
             <source>IMPORT_FROM_FILE</source>
@@ -1388,6 +1388,10 @@ Please, refer to the documentation.</translation>
             <source>USE_BUILD_PROGRESS</source>
             <translation>Use build progress</translation>
         </message>
+        <message>
+            <source>FIRST_STR_AS_TITLE</source>
+            <translation>Use first string as title</translation>
+        </message>
         <message>
             <source>MEN_LOAD_COMPONENT_DATA</source>
             <translation>Load Component Data</translation>
index 792634a7eb6d84b1403a800190f480132609b0f0..97ba5489a79b251a22eb737a9e2d75c7a5389d25 100644 (file)
@@ -327,14 +327,32 @@ VisuGUI
   aFilter.append( tr("FLT_TABLE_FILES") );
   aFilter.append( tr("FLT_ALL_FILES") );
 
-  QString anInitialPath = "";
-  if ( SUIT_FileDlg::getLastVisitedPath().isEmpty() )
-    anInitialPath = QDir::currentPath();
-
-  QStringList aFiles = SUIT_FileDlg::getOpenFileNames(GetDesktop(this),
-                                                      anInitialPath,
-                                                      aFilter,
-                                                      tr("TLT_IMPORT_TABLE"));
+  //QString anInitialPath = "";
+  //if ( SUIT_FileDlg::getLastVisitedPath().isEmpty() )
+  //  anInitialPath = QDir::currentPath();
+
+  //QStringList aFiles = SUIT_FileDlg::getOpenFileNames(GetDesktop(this),
+  //                                                    anInitialPath,
+  //                                                    aFilter,
+  //                                                    tr("TLT_IMPORT_TABLE"));
+
+  SUIT_ResourceMgr* aResourceMgr = GetResourceMgr();
+  bool aFirstStrAsTitle = aResourceMgr->booleanValue("VISU", "first_str_as_title", false);
+
+  SalomeApp_CheckFileDlg fd( GetDesktop(this), true, tr("FIRST_STR_AS_TITLE") );
+  fd.setWindowTitle( tr( "IMPORT_FROM_FILE" ) );
+  fd.setFileMode( SalomeApp_CheckFileDlg::ExistingFiles );
+  fd.setFilters( aFilter );
+  fd.SetChecked( aFirstStrAsTitle );
+  if ( SalomeApp_CheckFileDlg::getLastVisitedPath().isEmpty() )
+    fd.setDirectory( QDir::currentPath() );
+
+  QStringList aFiles;
+  if ( !fd.exec() )
+    return; // cancelled, return
+  aFiles = fd.selectedFiles();
+  aFirstStrAsTitle = fd.IsChecked();
+
   if ( aFiles.count() == 0 )
     return; // nothing selected
 
@@ -351,7 +369,9 @@ VisuGUI
         QString anInfo( tr("TLT_IMPORT_TABLE") + " " + aFileInfo.filePath() + " ..." );
         application()->putInfo( anInfo );
 
-        CORBA::Object_var anObject = GetVisuGen(this)->ImportTables(aFileInfo.filePath().toLatin1().constData());
+        CORBA::Object_var anObject =
+          GetVisuGen(this)->ImportTables(aFileInfo.filePath().toLatin1().constData(),
+                                         aFirstStrAsTitle);
 
         if (CORBA::is_nil(anObject.in())) {
           errors.append( QString( "%1 :\n\t%2" ).arg( aFileInfo.filePath() ).
@@ -444,6 +464,7 @@ VisuGUI
       // get name for the file
       QStringList aFilter;
       aFilter.append("Table Files (*.txt *.tab)");
+      aFilter.append("CSV Table Files (*.csv)");
 
       QFileInfo aFileInfo =
         SUIT_FileDlg::getFileName(GetDesktop(this),
index e07a785f600e5951e2e1d30a395a631ae8e08258..b8356d8519a836429142ee15ebef68a42bf0e2a3 100644 (file)
@@ -1662,8 +1662,13 @@ namespace VISU
          if (aTypeName == "ImportTables") {
            QString aFileName = VISU::Storable::FindValue(aMap,"myFileName",&anIsExist);
            if(anIsExist){
-             std::string aName = GenerateName(theSObject,theName2EntryMap,theEntry2NameMap);
-             theStr<<thePrefix<<aName<<" = aVisu.ImportTables('"<<aFileName.toLatin1().data()<<"')"<<endl;
+             std::string aName =
+                GenerateName(theSObject,theName2EntryMap,theEntry2NameMap);
+              QString aFirstStrAsTitle =
+                VISU::Storable::FindValue(aMap,"myFirstStrAsTitle",&anIsExist);
+             theStr<<thePrefix<<aName<<" = aVisu.ImportTables('"
+                    <<aFileName.toLatin1().data()<<"',"
+                    <<aFirstStrAsTitle.toLatin1().data()<<")"<<endl;
              theStr<<thePrefix<<"if "<<aName<<":"<<endl;
              thePrefix += PREFIX;
 
index 3c2304e0011a8e114c541bf65283c383072ba0c0..1935c4ed0e74db6c64a1871df8a022aa60b8c07c 100644 (file)
@@ -598,12 +598,13 @@ namespace VISU
   //----------------------------------------------------------------------------
   SALOMEDS::SObject_ptr 
   VISU_Gen_i
-  ::ImportTables(const char* theFileName)
+  ::ImportTables(const char* theFileName, bool theFirstStrAsTitle)
   {
     if(myStudyDocument->GetProperties()->IsLocked())
       return SALOMEDS::SObject::_nil();
 
-    SALOMEDS::SObject_var aRes = VISU::ImportTables(theFileName,myStudyDocument);
+    SALOMEDS::SObject_var aRes = VISU::ImportTables(theFileName,myStudyDocument,
+                                                    theFirstStrAsTitle);
 
     SALOMEDS::Study_var aStudy = aRes->GetStudy();
     SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator(aRes);
index 32eec815397bb2a4119dd0bd7eb361887f4326cb..951688c973933df12ac31075fb0b03cb19abcf1f 100644 (file)
@@ -78,7 +78,7 @@ namespace VISU
 
     virtual
     SALOMEDS::SObject_ptr
-    ImportTables(const char* theFileName);
+    ImportTables(const char* theFileName, bool theFirstStrAsTitle = false);
 
     virtual
     CORBA::Boolean
index fa9e334a16874830a82516541661e5df93a38e46..7b1f7a0ed9befb7152eebfc04944178613f40232 100644 (file)
@@ -1196,13 +1196,14 @@ SALOMEDS::SObject_var VISU::Container_i::GetSObject()
 }
 
 SALOMEDS::SObject_var
-VISU::ImportTables(const char* theFileName, SALOMEDS::Study_ptr theStudy)
+VISU::ImportTables(const char* theFileName, SALOMEDS::Study_ptr theStudy,
+                   bool theFirstStrAsTitle)
 {
   // Set "C" numeric locale to import numbers correctly
   Kernel_Utils::Localizer loc;
 
   TTableContainer aContainer;
-  ImportTables( theFileName, aContainer );
+  ImportTables( theFileName, aContainer, theFirstStrAsTitle );
   if ( aContainer.empty() ) 
     return SALOMEDS::SObject::_nil();
 
@@ -1217,10 +1218,10 @@ VISU::ImportTables(const char* theFileName, SALOMEDS::Study_ptr theStudy)
   anAttr = aStudyBuilder->FindOrCreateAttribute(aFileObject, "AttributeString");
   SALOMEDS::AttributeString_var aComment = SALOMEDS::AttributeString::_narrow(anAttr);
   QString aString;
-  aString.sprintf("myComment=ImportTables;myFileName=%s",
-                 aFileInfo.absoluteFilePath().toLatin1().data());
+  aString.sprintf("myComment=ImportTables;myFileName=%s;myFirstStrAsTitle=%d",
+                 aFileInfo.absoluteFilePath().toLatin1().data(),theFirstStrAsTitle);
   aComment->SetValue(aString.toLatin1().data());
-  for(int i = 0, iEnd = aContainer.size(); i < iEnd; i++){
+  for(int i = 0, iEnd = aContainer.size(); i < iEnd; i++) {
     PTableIDMapper aTableIDMapper = aContainer[i];
     const TTable2D& aTable2D = *aTableIDMapper;
     SALOMEDS::SObject_var aRealObject = aStudyBuilder->NewObject(aFileObject);
@@ -1229,7 +1230,8 @@ VISU::ImportTables(const char* theFileName, SALOMEDS::Study_ptr theStudy)
     if(MYDEBUG) MESSAGE("aTable2D.myTitle = "<<aTable2D.myTitle);
     if ( aTable2D.myTitle != "" ) {
       aName->SetValue(aTable2D.myTitle.c_str());
-    } else {
+    }
+    else {
       QString aNewName;
       aNewName.sprintf("Table:%d",i);
       aName->SetValue(aNewName.toLatin1().data());
@@ -1242,10 +1244,10 @@ VISU::ImportTables(const char* theFileName, SALOMEDS::Study_ptr theStudy)
     aTable2D.getColumns(aNewTable2D);
     int kEnd = aNewTable2D.myRows[0].myValues.size();
     aTableOfReal->SetNbColumns(kEnd);
-    for(int j = 0, jEnd = aNewTable2D.myRows.size(); j < jEnd; j++){
+    for(int j = 0, jEnd = aNewTable2D.myRows.size(); j < jEnd; j++) {
       if(MYDEBUG) MESSAGE("j = "<<j<<"; kEnd = "<<kEnd);
 
-      for(int k = 0; k < kEnd; k++){
+      for(int k = 0; k < kEnd; k++) {
        QString aVal = aNewTable2D.myRows[j].myValues[k].c_str();
        bool anIsOk = false;
        double aValue = aVal.toDouble(&anIsOk);
@@ -1262,6 +1264,32 @@ VISU::ImportTables(const char* theFileName, SALOMEDS::Study_ptr theStudy)
   return aFileObject;
 }
 
+
+//=======================================================================
+//function : updateStrForCSV
+//purpose  : auxilary for ExportTableToFile
+//=======================================================================
+void updateStrForCSV(QString& aStr, const char aSep)
+{
+  int index = aStr.indexOf('"');
+  while(index>=0) {
+    aStr.insert(index,'"');
+    if( index+2 >= aStr.size() ) break;
+    index = aStr.indexOf('"',index+2);
+  }
+  index = aStr.indexOf(aSep);
+  if(index>=0) {
+    // current string contains separator => need to use "..."
+    aStr.insert(0,'"');
+    aStr.push_back('"');
+  }
+}
+
+
+//=======================================================================
+//function : ExportTableToFile
+//purpose  : 
+//=======================================================================
 template<class TTableAttr> bool ExportTableToFile(const TTableAttr& aTabAttr,
                                                  const char* theFileName)
 {
@@ -1284,6 +1312,49 @@ template<class TTableAttr> bool ExportTableToFile(const TTableAttr& aTabAttr,
   SALOMEDS::StringSeq_var aRowUnits = aTabAttr->GetRowUnits();
   SALOMEDS::StringSeq_var aColumnTitles = aTabAttr->GetColumnTitles();
 
+  //--------------------------------------------------
+  //    write as *.csv file if it is needed
+  //--------------------------------------------------
+  QString tmp(theFileName);
+  tmp = tmp.trimmed();
+  tmp = tmp.right(3).trimmed();
+  if( tmp == QString("csv") ) {
+    const char aSep = ',';
+    // write column titles
+    QString aLine(aRowTitles[0]);
+    updateStrForCSV(aLine,aSep);
+    for(int i=1; i<aRowsNb; i++) {
+      aLine += aSep;
+      QString aTmp(aRowTitles[i]);
+      updateStrForCSV(aTmp,aSep);
+      aLine += aTmp;
+    }
+    aLine += "\n";
+    aFile.write(aLine.toLatin1() );
+    // write table data
+    QString aValue;
+    for (int j = 1; j <= aColNb; j++) {
+      QString aLine = "";
+      if(aTabAttr->HasValue(j,1)) {
+        aLine = aValue.sprintf("%.16g",(double)aTabAttr->GetValue(1,j));
+      }
+      for (int i = 2; i <= aRowsNb; i++) {
+        if(aTabAttr->HasValue(i,j)) {
+          aLine += aSep + aValue.sprintf("%.16g",(double)aTabAttr->GetValue(i,j));
+        }
+        else aLine += aSep;
+      }
+      aLine += "\n";
+      aFile.write(aLine.toLatin1() );
+    }
+
+    aFile.close();
+    return true;
+  }
+  //--------------------------------------------------
+  //       end of writing as *.csv file
+  //--------------------------------------------------
+
   /* The given table is rare (some cells is empty) or not? */
   bool isRareTable = false;
   for (int i = 1; i <= aRowsNb; i++)
@@ -1312,7 +1383,13 @@ template<class TTableAttr> bool ExportTableToFile(const TTableAttr& aTabAttr,
 
        /* COLUMN_TITLES */
        if ( anAbscissTitle.length() || anOrdinate.length() ) {
-         aLine = "#COLUMN_TITLES: " + anAbscissTitle + " | " + anOrdinate + "\n";
+         aLine = "#COLUMN_TITLES: " + anAbscissTitle + " | " + anOrdinate;
+          int tmpind = aLine.indexOf("\n");
+          while(tmpind>=0) {
+            aLine.remove(tmpind,1);
+            tmpind = aLine.indexOf("\n");
+          }
+         aLine += "\n";
          aFile.write(aLine.toLatin1() );
        }
 
@@ -1352,17 +1429,21 @@ template<class TTableAttr> bool ExportTableToFile(const TTableAttr& aTabAttr,
     QString aUnitsSep  = "";
     QString aTitlesStr = "#COLUMN_TITLES: ";
     QString aUnitsStr  = "#COLUMN_UNITS: ";
-    for (int i = 1; i <= aRowsNb; i++)
-      {
-       if (!QString(aRowTitles[i-1]).trimmed().isEmpty()) {
-         aTitlesStr += (aTitlesSep + aRowTitles[i-1]);
-         if (aTitlesSep.isEmpty()) aTitlesSep = " | ";
-       }
-       if (!QString(aRowUnits[i-1]).trimmed().isEmpty()) {
-         aUnitsStr += (aUnitsSep + aRowUnits[i-1]);
-         if (aUnitsSep.isEmpty()) aUnitsSep = " ";
-       }
+    for (int i = 1; i <= aRowsNb; i++) {
+      if (!QString(aRowTitles[i-1]).trimmed().isEmpty()) {
+        aTitlesStr += (aTitlesSep + aRowTitles[i-1]);
+        if (aTitlesSep.isEmpty()) aTitlesSep = " | ";
+      }
+      if (!QString(aRowUnits[i-1]).trimmed().isEmpty()) {
+        aUnitsStr += (aUnitsSep + aRowUnits[i-1]);
+        if (aUnitsSep.isEmpty()) aUnitsSep = " ";
       }
+    }
+    int tmpind = aTitlesStr.indexOf("\n");
+    while(tmpind>=0) {
+      aTitlesStr.remove(tmpind,1);
+      tmpind = aTitlesStr.indexOf("\n");
+    }
     aTitlesStr += "\n";
     aUnitsStr  += "\n";
     aFile.write(aTitlesStr.toLatin1());
@@ -1370,22 +1451,20 @@ template<class TTableAttr> bool ExportTableToFile(const TTableAttr& aTabAttr,
 
     /* CURVE COORDINATES */
     QString aSep, aValue, aColTitle;
-    for (int j = 1; j <= aColNb; j++)
-      {
-       aLine = ""; aSep  = "";
-       for (int i = 1; i <= aRowsNb; i++)
-         {
-           aLine += (aSep + aValue.sprintf("%.16g", (double)(aTabAttr->GetValue(i,j))));
-           if (aSep.isEmpty()) aSep = " ";
-         }
-       if (!aLine.trimmed().isEmpty()) {
-         aColTitle = aColumnTitles[j-1];
-         if (!aColTitle.trimmed().isEmpty())
-           aLine = aLine + "  #TITLE: " + aColTitle;
-         aLine += "\n";
-         aFile.write(aLine.toLatin1());
-       }
+    for (int j = 1; j <= aColNb; j++) {
+      aLine = ""; aSep  = "";
+      for (int i = 1; i <= aRowsNb; i++) {
+        aLine += (aSep + aValue.sprintf("%.16g", (double)(aTabAttr->GetValue(i,j))));
+        if (aSep.isEmpty()) aSep = " ";
       }
+      if (!aLine.trimmed().isEmpty()) {
+        aColTitle = aColumnTitles[j-1];
+        if (!aColTitle.trimmed().isEmpty())
+          aLine = aLine + "  #TITLE: " + aColTitle;
+        aLine += "\n";
+        aFile.write(aLine.toLatin1());
+      }
+    }
   } //end of else
 
   aFile.close();
@@ -1396,17 +1475,15 @@ bool VISU::ExportTableToFile(SALOMEDS::SObject_ptr theTable, const char* theFile
 {
   //Find table
   SALOMEDS::GenericAttribute_var anAttr ;
-  if (theTable->FindAttribute(anAttr, "AttributeTableOfReal"))
-    {
-      SALOMEDS::AttributeTableOfReal_var aTabAttr = SALOMEDS::AttributeTableOfReal ::_narrow(anAttr);
-      return ExportTableToFile ( aTabAttr , theFileName);
-
-    }
+  if (theTable->FindAttribute(anAttr, "AttributeTableOfReal")) {
+    SALOMEDS::AttributeTableOfReal_var aTabAttr =
+      SALOMEDS::AttributeTableOfReal ::_narrow(anAttr);
+    return ExportTableToFile ( aTabAttr , theFileName);
+  }
   else if (theTable->FindAttribute(anAttr, "AttributeTableOfInteger")) {
-
-    SALOMEDS::AttributeTableOfInteger_var aTabAttr = SALOMEDS::AttributeTableOfInteger ::_narrow(anAttr);
+    SALOMEDS::AttributeTableOfInteger_var aTabAttr =
+      SALOMEDS::AttributeTableOfInteger ::_narrow(anAttr);
     return ExportTableToFile ( aTabAttr , theFileName);
-
   }
   return false;
 }
index 4303f6b6ceb3e72712c7efcd46f9b26d3e43c5ea..78c6776a0baad284834162fdf5f673a884636f1c 100644 (file)
@@ -84,7 +84,8 @@ namespace VISU{
 
     virtual std::string GetObjectEntry();
   };
-  SALOMEDS::SObject_var ImportTables(const char* theFileName, SALOMEDS::Study_ptr theStudy);
+  SALOMEDS::SObject_var ImportTables(const char* theFileName, SALOMEDS::Study_ptr theStudy,
+                                     bool theFirstStrAsTitle = false);
   bool ExportTableToFile(SALOMEDS::SObject_ptr theTable, const char* theFileName);
   //==============================================================================
   class VISU_I_EXPORT Curve_i : public virtual POA_VISU::Curve,