Back to index

salome-paravis  6.5.0
vtkVisuTableReader.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2010-2012  CEA/DEN, EDF R&D
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #include "vtkVisuTableReader.h"
00021 #include "TableParser.h"
00022 
00023 #include "vtkObjectFactory.h"
00024 #include "vtkTable.h"
00025 #include "vtkInformationVector.h"
00026 #include "vtkInformation.h"
00027 #include "vtkStreamingDemandDrivenPipeline.h"
00028 #include "vtkVariantArray.h"
00029 #include "vtkStringArray.h"
00030 #include "vtkStringToNumeric.h"
00031 
00032 #include <vtkstd/stdexcept>
00033 #include <vtksys/ios/sstream>
00034 
00035 #include <vector> // STL include
00036 #include <string> // STL include
00037 using namespace std;
00038 
00039 vtkCxxRevisionMacro(vtkVisuTableReader, "$Revision: 1.1.2.2.12.1 $");
00040 vtkStandardNewMacro(vtkVisuTableReader);
00041 
00042 vtkVisuTableReader::vtkVisuTableReader():
00043   FileName(0)
00044 {
00045   this->SetNumberOfInputPorts(0);
00046   this->SetNumberOfOutputPorts(1);
00047 
00048   this->FileName = NULL;
00049   
00050   this->DetectNumericColumns = true;
00051   this->FirstStringAsTitles = false;
00052 
00053   this->TableNumber = 0;
00054 
00055   this->ValueDelimiter = 0;
00056   this->SetValueDelimiter(" ");
00057   
00058   this->AvailableTables = vtkStringArray::New();
00059 }
00060 
00061 vtkVisuTableReader::~vtkVisuTableReader()
00062 {
00063   this->SetFileName(0);
00064   this->SetValueDelimiter(0);
00065   this->AvailableTables->Delete();
00066 }
00067 
00068 int vtkVisuTableReader::CanReadFile(const char* fname)
00069 {
00070   return 1;
00071 }
00072 
00073 void vtkVisuTableReader::PrintSelf(ostream& os, vtkIndent indent)
00074 {
00075   this->Superclass::PrintSelf(os, indent);
00076   os << indent << "FileName: " 
00077      << (this->FileName ? this->FileName : "(none)") << endl;
00078   os << indent << "DetectNumericColumns: "
00079      << (this->DetectNumericColumns? "true" : "false") << endl;
00080   os << indent << "ValueDelimiter: "
00081      << (this->ValueDelimiter ? this->ValueDelimiter : "(none)") << endl;
00082   os << indent << "TableNumber: " << this->TableNumber<< endl;
00083 }
00084 
00085 int vtkVisuTableReader::RequestData(vtkInformation*, 
00086                                 vtkInformationVector**, 
00087                                 vtkInformationVector* outputVector)
00088 {
00089   vtkTable* const output_table = vtkTable::GetData(outputVector);
00090 
00091   try
00092     {
00093       vtkInformation* const outInfo = outputVector->GetInformationObject(0);
00094       if(outInfo->Has(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER()) &&
00095         outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER()) > 0) 
00096        {
00097          return 1;
00098        }
00099 
00100       // If the filename is not defined
00101       if(!this->FileName || this->TableNumber < 0) 
00102        {
00103          return 1;
00104        }
00105 
00106       // Read table with the given number from the file
00107       Table2D table = GetTable(this->FileName, this->ValueDelimiter,
00108                             this->TableNumber, this->FirstStringAsTitles);
00109 
00110       // Set table name
00111       output_table->GetInformation()->Set(vtkDataObject::FIELD_NAME(), 
00112                                      table.myTitle.c_str());
00113       
00114       int nbRows = table.myRows.size();
00115       int nbCols = table.myRows[0].myValues.size();
00116 
00117       for (int col=0; col < nbCols; col++) 
00118        {
00119          vtkStringArray* newCol = vtkStringArray::New();
00120          newCol->SetNumberOfValues(nbRows);
00121 
00122          // Set value
00123          for (int row=0; row < nbRows; row++) 
00124            {
00125              newCol->SetValue(row, table.myRows[row].myValues[col].c_str());
00126            }
00127            
00128          // Set title
00129          bool hasUnit = !table.myColumnUnits[col].empty();
00130        
00131          if (table.myColumnTitles[col].empty()) 
00132            {
00133              vtkstd::stringstream buffer;
00134              if (hasUnit) 
00135               {
00136                 buffer << col <<" [" << table.myColumnUnits[col].c_str() << "]";
00137               }
00138              else 
00139               {
00140                 buffer << col;
00141               }
00142              newCol->SetName(buffer.str().c_str());
00143            }
00144        else 
00145          {
00146            if (hasUnit) 
00147              {
00148               vtkstd::stringstream buffer;
00149               buffer << table.myColumnTitles[col].c_str() 
00150                      <<" [" << table.myColumnUnits[col].c_str() << "]";
00151               newCol->SetName(buffer.str().c_str());
00152              }
00153          else 
00154            {
00155              newCol->SetName(table.myColumnTitles[col].c_str());
00156            }
00157          }
00158        
00159          output_table->AddColumn(newCol);
00160          newCol->Delete();
00161        }
00162       
00163       // Detect numeric columns if needed
00164       if (this->DetectNumericColumns) 
00165        {
00166          vtkStringToNumeric* convertor = vtkStringToNumeric::New();
00167          vtkTable* clone = output_table->NewInstance();
00168          clone->ShallowCopy(output_table);
00169          convertor->SetInput(clone);
00170          convertor->Update();
00171          clone->Delete();
00172          output_table->ShallowCopy(convertor->GetOutputDataObject(0));
00173          convertor->Delete();
00174        }
00175     } 
00176   catch(vtkstd::exception& e) 
00177     {
00178       vtkErrorMacro(<< "caught exception: " << e.what() << endl);
00179       output_table->Initialize();
00180     }
00181   catch(...) 
00182     {
00183       vtkErrorMacro(<< "caught unknown exception." << endl);
00184       output_table->Initialize();
00185     }
00186   
00187   return 1;
00188 }
00189 
00190 vtkStringArray* vtkVisuTableReader::GetAvailableTables()
00191 {
00192   this->AvailableTables->Initialize();
00193   
00194   vector<string> titles = 
00195     GetTableNames(this->FileName, this->ValueDelimiter,
00196                 this->FirstStringAsTitles);
00197   
00198   for(int i = 0; i < titles.size(); i++)
00199     {
00200       this->AvailableTables->InsertNextValue(titles[i].c_str());
00201     }
00202   
00203   return this->AvailableTables;
00204 }