1 // Copyright (C) 2010-2012 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "vtkVisuTableReader.h"
21 #include "TableParser.h"
23 #include "vtkObjectFactory.h"
25 #include "vtkInformationVector.h"
26 #include "vtkInformation.h"
27 #include "vtkStreamingDemandDrivenPipeline.h"
28 #include "vtkVariantArray.h"
29 #include "vtkStringArray.h"
30 #include "vtkStringToNumeric.h"
32 #include <vtksys/stl/stdexcept>
33 #include <vtksys/ios/sstream>
35 #include <vector> // STL include
36 #include <string> // STL include
39 //vtkCxxRevisionMacro(vtkVisuTableReader, "$Revision$");
40 vtkStandardNewMacro(vtkVisuTableReader);
42 vtkVisuTableReader::vtkVisuTableReader():
45 this->SetNumberOfInputPorts(0);
46 this->SetNumberOfOutputPorts(1);
48 this->FileName = NULL;
50 this->DetectNumericColumns = true;
51 this->FirstStringAsTitles = false;
53 this->TableNumber = 0;
55 this->ValueDelimiter = 0;
56 this->SetValueDelimiter(" ");
58 this->AvailableTables = vtkStringArray::New();
61 vtkVisuTableReader::~vtkVisuTableReader()
64 this->SetValueDelimiter(0);
65 this->AvailableTables->Delete();
68 int vtkVisuTableReader::CanReadFile(const char* fname)
73 void vtkVisuTableReader::PrintSelf(ostream& os, vtkIndent indent)
75 this->Superclass::PrintSelf(os, indent);
76 os << indent << "FileName: "
77 << (this->FileName ? this->FileName : "(none)") << endl;
78 os << indent << "DetectNumericColumns: "
79 << (this->DetectNumericColumns? "true" : "false") << endl;
80 os << indent << "ValueDelimiter: "
81 << (this->ValueDelimiter ? this->ValueDelimiter : "(none)") << endl;
82 os << indent << "TableNumber: " << this->TableNumber<< endl;
85 int vtkVisuTableReader::RequestData(vtkInformation*,
86 vtkInformationVector**,
87 vtkInformationVector* outputVector)
89 vtkTable* const output_table = vtkTable::GetData(outputVector);
93 vtkInformation* const outInfo = outputVector->GetInformationObject(0);
94 if(outInfo->Has(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER()) &&
95 outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER()) > 0)
100 // If the filename is not defined
101 if(!this->FileName || this->TableNumber < 0)
106 // Read table with the given number from the file
107 Table2D table = GetTable(this->FileName, this->ValueDelimiter,
108 this->TableNumber, this->FirstStringAsTitles);
111 output_table->GetInformation()->Set(vtkDataObject::FIELD_NAME(),
112 table.myTitle.c_str());
114 int nbRows = table.myRows.size();
115 int nbCols = table.myRows[0].myValues.size();
117 for (int col=0; col < nbCols; col++)
119 vtkStringArray* newCol = vtkStringArray::New();
120 newCol->SetNumberOfValues(nbRows);
123 for (int row=0; row < nbRows; row++)
125 newCol->SetValue(row, table.myRows[row].myValues[col].c_str());
129 bool hasUnit = !table.myColumnUnits[col].empty();
131 if (table.myColumnTitles[col].empty())
133 vtksys_ios::stringstream buffer;
136 buffer << col <<" [" << table.myColumnUnits[col].c_str() << "]";
142 newCol->SetName(buffer.str().c_str());
148 vtksys_ios::stringstream buffer;
149 buffer << table.myColumnTitles[col].c_str()
150 <<" [" << table.myColumnUnits[col].c_str() << "]";
151 newCol->SetName(buffer.str().c_str());
155 newCol->SetName(table.myColumnTitles[col].c_str());
159 output_table->AddColumn(newCol);
163 // Detect numeric columns if needed
164 if (this->DetectNumericColumns)
166 vtkStringToNumeric* convertor = vtkStringToNumeric::New();
167 vtkTable* clone = output_table->NewInstance();
168 clone->ShallowCopy(output_table);
169 convertor->SetInputData(clone);
172 output_table->ShallowCopy(convertor->GetOutputDataObject(0));
176 catch(vtksys_stl::exception& e)
178 vtkErrorMacro(<< "caught exception: " << e.what() << endl);
179 output_table->Initialize();
183 vtkErrorMacro(<< "caught unknown exception." << endl);
184 output_table->Initialize();
190 vtkStringArray* vtkVisuTableReader::GetAvailableTables()
192 this->AvailableTables->Initialize();
194 vector<string> titles =
195 GetTableNames(this->FileName, this->ValueDelimiter,
196 this->FirstStringAsTitles);
198 for(int i = 0; i < titles.size(); i++)
200 this->AvailableTables->InsertNextValue(titles[i].c_str());
203 return this->AvailableTables;