1 // Copyright (C) 2010-2019 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, or (at your option) any later version.
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 <vtkInformation.h>
24 #include <vtkInformationVector.h>
25 #include <vtkObjectFactory.h>
26 #include <vtkStreamingDemandDrivenPipeline.h>
27 #include <vtkStringArray.h>
28 #include <vtkStringToNumeric.h>
30 #include <vtkVariantArray.h>
39 vtkStandardNewMacro(vtkVisuTableReader);
41 vtkVisuTableReader::vtkVisuTableReader()
44 this->SetNumberOfInputPorts(0);
45 this->SetNumberOfOutputPorts(1);
47 this->FileName = nullptr;
49 this->DetectNumericColumns = true;
50 this->FirstStringAsTitles = false;
52 this->TableNumber = 0;
54 this->ValueDelimiter = nullptr;
55 this->SetValueDelimiter(" ");
57 this->AvailableTables = vtkStringArray::New();
60 vtkVisuTableReader::~vtkVisuTableReader()
62 this->SetFileName(nullptr);
63 this->SetValueDelimiter(nullptr);
64 this->AvailableTables->Delete();
67 int vtkVisuTableReader::CanReadFile(const char* fname)
72 void vtkVisuTableReader::PrintSelf(ostream& os, vtkIndent indent)
74 this->Superclass::PrintSelf(os, indent);
75 os << indent << "FileName: " << (this->FileName ? this->FileName : "(none)") << endl;
76 os << indent << "DetectNumericColumns: " << (this->DetectNumericColumns ? "true" : "false")
78 os << indent << "ValueDelimiter: " << (this->ValueDelimiter ? this->ValueDelimiter : "(none)")
80 os << indent << "TableNumber: " << this->TableNumber << endl;
83 int vtkVisuTableReader::RequestData(
84 vtkInformation*, vtkInformationVector**, vtkInformationVector* outputVector)
86 vtkTable* const output_table = vtkTable::GetData(outputVector);
90 vtkInformation* const outInfo = outputVector->GetInformationObject(0);
91 if (outInfo->Has(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER()) &&
92 outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER()) > 0)
97 // If the filename is not defined
98 if (!this->FileName || this->TableNumber < 0)
103 // Read table with the given number from the file
105 GetTable(this->FileName, this->ValueDelimiter, this->TableNumber, this->FirstStringAsTitles);
108 output_table->GetInformation()->Set(vtkDataObject::FIELD_NAME(), table.myTitle.c_str());
110 int nbRows = table.myRows.size();
111 int nbCols = table.myRows[0].myValues.size();
113 for (int col = 0; col < nbCols; col++)
115 vtkStringArray* newCol = vtkStringArray::New();
116 newCol->SetNumberOfValues(nbRows);
119 for (int row = 0; row < nbRows; row++)
121 newCol->SetValue(row, table.myRows[row].myValues[col].c_str());
125 bool hasUnit = !table.myColumnUnits[col].empty();
127 if (table.myColumnTitles[col].empty())
129 std::stringstream buffer;
132 buffer << col << " [" << table.myColumnUnits[col].c_str() << "]";
138 newCol->SetName(buffer.str().c_str());
144 std::stringstream buffer;
145 buffer << table.myColumnTitles[col].c_str() << " [" << table.myColumnUnits[col].c_str()
147 newCol->SetName(buffer.str().c_str());
151 newCol->SetName(table.myColumnTitles[col].c_str());
155 output_table->AddColumn(newCol);
159 // Detect numeric columns if needed
160 if (this->DetectNumericColumns)
162 vtkStringToNumeric* convertor = vtkStringToNumeric::New();
163 vtkTable* clone = output_table->NewInstance();
164 clone->ShallowCopy(output_table);
165 convertor->SetInputData(clone);
168 output_table->ShallowCopy(convertor->GetOutputDataObject(0));
172 catch (std::exception& e)
174 vtkErrorMacro(<< "caught exception: " << e.what() << endl);
175 output_table->Initialize();
179 vtkErrorMacro(<< "caught unknown exception." << endl);
180 output_table->Initialize();
186 vtkStringArray* vtkVisuTableReader::GetAvailableTables()
188 this->AvailableTables->Initialize();
190 vector<string> titles =
191 GetTableNames(this->FileName, this->ValueDelimiter, this->FirstStringAsTitles);
193 for (int i = 0; i < titles.size(); i++)
195 this->AvailableTables->InsertNextValue(titles[i].c_str());
198 return this->AvailableTables;