Salome HOME
Adding a MEDReader Group Reader that reads a file series of .med into a multiblock
[modules/paravis.git] / src / Plugins / MEDReader / plugin / MEDReaderIO / vtkFileSeriesGroupReader.cxx
1 #include "vtkFileSeriesGroupReader.h"
2
3 #include <vtkInformation.h>
4 #include <vtkInformationVector.h>
5 #include <vtkMultiBlockDataSet.h>
6 #include <vtkObjectFactory.h>
7 #include <vtkSmartPointer.h>
8 #include <vtkStreamingDemandDrivenPipeline.h>
9
10 #include <vector>
11 #include <string>
12
13 vtkStandardNewMacro(vtkFileSeriesGroupReader);
14
15 //=============================================================================
16 struct vtkFileSeriesGroupReaderInternals
17 {
18   std::vector<std::string> FileNames;
19 };
20
21 //=============================================================================
22 vtkFileSeriesGroupReader::vtkFileSeriesGroupReader()
23   : Internals(new vtkFileSeriesGroupReaderInternals())
24 {
25   this->SetNumberOfInputPorts(0);
26   this->SetNumberOfOutputPorts(1);
27 }
28
29 //-----------------------------------------------------------------------------
30 vtkFileSeriesGroupReader::~vtkFileSeriesGroupReader() = default;
31
32 //----------------------------------------------------------------------------
33 void vtkFileSeriesGroupReader::AddFileName(const char* name)
34 {
35   // Make sure the reader always has a filename set
36   this->ReaderSetFileName(name);
37
38   this->AddFileNameInternal(name);
39   this->Modified();
40 }
41
42 //----------------------------------------------------------------------------
43 void vtkFileSeriesGroupReader::RemoveAllFileNames()
44 {
45   this->RemoveAllFileNamesInternal();
46   this->Modified();
47 }
48
49 //----------------------------------------------------------------------------
50 void vtkFileSeriesGroupReader::RemoveAllFileNamesInternal()
51 {
52   this->Internals->FileNames.clear();
53 }
54
55 //----------------------------------------------------------------------------
56 void vtkFileSeriesGroupReader::AddFileNameInternal(const char* name)
57 {
58   this->Internals->FileNames.emplace_back(name);
59 }
60
61 //----------------------------------------------------------------------------
62 unsigned int vtkFileSeriesGroupReader::GetNumberOfFileNames()
63 {
64   return static_cast<unsigned int>(this->Internals->FileNames.size());
65 }
66
67 //----------------------------------------------------------------------------
68 const char* vtkFileSeriesGroupReader::GetFileName(unsigned int idx)
69 {
70   if (idx >= this->Internals->FileNames.size())
71   {
72     return nullptr;
73   }
74   return this->Internals->FileNames[idx].c_str();
75 }
76
77 //----------------------------------------------------------------------------
78 int vtkFileSeriesGroupReader::CanReadFile(const char* filename)
79 {
80   if (!this->Reader)
81   {
82     return 0;
83   }
84
85   return this->ReaderCanReadFile(filename);
86 }
87
88 //----------------------------------------------------------------------------
89 int vtkFileSeriesGroupReader::RequestInformation(
90   vtkInformation* request, vtkInformationVector** inputVector, vtkInformationVector* outputVector)
91 {
92   return this->Reader->ProcessRequest(request, inputVector, outputVector);
93 }
94
95 //----------------------------------------------------------------------------
96 int vtkFileSeriesGroupReader::RequestData(vtkInformation* vtkNotUsed(request),
97   vtkInformationVector** vtkNotUsed(inputVector), vtkInformationVector* outputVector)
98 {
99   auto output = vtkMultiBlockDataSet::GetData(outputVector, 0);
100   output->SetNumberOfBlocks(this->GetNumberOfFileNames());
101
102   vtkInformation* info = outputVector->GetInformationObject(0);
103   double time = info->Get(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEP());
104
105   for (unsigned int i = 0; i < this->GetNumberOfFileNames(); i++)
106   {
107     this->ReaderSetFileName(this->GetFileName(i));
108     this->Reader->UpdateTimeStep(time);
109     vtkDataObject* outputReader = this->Reader->GetOutputDataObject(0);
110     vtkSmartPointer<vtkDataObject> outputLeaf = vtkSmartPointer<vtkDataObject>::Take(outputReader->NewInstance());
111     outputLeaf->DeepCopy(outputReader);
112     output->SetBlock(i, outputLeaf);
113   }
114
115   return 1;
116 }
117  
118 //------------------------------------------------------------------------------
119 int vtkFileSeriesGroupReader::FillOutputPortInformation(
120   int vtkNotUsed(port), vtkInformation* info)
121 {
122   info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkMultiBlockDataSet");
123   return 1;
124 }
125
126 //-----------------------------------------------------------------------------
127 void vtkFileSeriesGroupReader::PrintSelf(ostream& os, vtkIndent indent)
128 {
129   this->Superclass::PrintSelf(os, indent);
130
131   os << indent << "MetaFileName: " << (this->_MetaFileName ? this->_MetaFileName : "(none)")
132      << endl;
133 }