Salome HOME
Copyright update 2022
[modules/paravis.git] / src / Plugins / MEDReader / plugin / MEDReaderIO / vtkPVMetaDataInformation.cxx
1 // Copyright (C) 2010-2022  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay
20
21 #include "vtkPVMetaDataInformation.h"
22
23 #include "vtkAlgorithm.h"
24 #include "vtkAlgorithmOutput.h"
25 #include "vtkClientServerStream.h"
26 #include "vtkExecutive.h"
27 #include "vtkDataObject.h"
28 #include "vtkGenericDataObjectReader.h"
29 #include "vtkGenericDataObjectWriter.h"
30 #include "vtkInformationDataObjectMetaDataKey.h"
31 #include "vtkInformation.h"
32 #include "vtkObjectFactory.h"
33
34 #include "vtkMEDReader.h"
35
36 vtkStandardNewMacro(vtkPVMetaDataInformation)
37 vtkCxxSetObjectMacro(vtkPVMetaDataInformation, InformationData, vtkDataObject)
38
39 //----------------------------------------------------------------------------
40 vtkPVMetaDataInformation::vtkPVMetaDataInformation()
41 {
42   this->InformationData = NULL;
43 }
44
45 //----------------------------------------------------------------------------
46 vtkPVMetaDataInformation::~vtkPVMetaDataInformation()
47 {
48   this->SetInformationData(NULL);
49 }
50
51 //----------------------------------------------------------------------------
52 void vtkPVMetaDataInformation::CopyFromObject(vtkObject* obj)
53 {
54   this->SetInformationData(NULL);
55
56   vtkAlgorithmOutput* algOutput = vtkAlgorithmOutput::SafeDownCast(obj);
57   if (!algOutput)
58     {
59     vtkAlgorithm* alg = vtkAlgorithm::SafeDownCast(obj);
60     if (alg)
61       {
62       algOutput = alg->GetOutputPort(0);
63       }
64
65     }
66   if (!algOutput)
67     {
68     vtkErrorMacro("Information can only be gathered from a vtkAlgorithmOutput.");
69     return;
70     }
71
72   vtkAlgorithm* reader = algOutput->GetProducer();
73   vtkInformation* info = reader->GetExecutive()->GetOutputInformation(
74     algOutput->GetIndex());
75
76   if (info && info->Has(vtkMEDReader::META_DATA()))
77     {
78     this->SetInformationData(vtkDataObject::SafeDownCast(info->Get(vtkMEDReader::META_DATA())));
79     }
80 }
81
82 //----------------------------------------------------------------------------
83 void vtkPVMetaDataInformation::CopyToStream(vtkClientServerStream* css)
84 {
85   css->Reset();
86   if (!this->InformationData)
87     {
88     *css << vtkClientServerStream::Reply
89          << vtkClientServerStream::InsertArray(
90            static_cast<unsigned char*>(NULL), 0)
91          << vtkClientServerStream::End;
92     return;
93     }
94
95   vtkDataObject* clone = this->InformationData->NewInstance();
96   clone->ShallowCopy(this->InformationData);
97
98   vtkGenericDataObjectWriter* writer = vtkGenericDataObjectWriter::New();
99   writer->SetFileTypeToBinary();
100   writer->WriteToOutputStringOn();
101   writer->SetInputData(clone);
102   writer->Write();
103
104   *css << vtkClientServerStream::Reply
105        << vtkClientServerStream::InsertArray(
106          writer->GetBinaryOutputString(),
107          writer->GetOutputStringLength())
108        << vtkClientServerStream::End;
109   writer->RemoveAllInputs();
110   writer->Delete();
111   clone->Delete();
112 }
113
114 //----------------------------------------------------------------------------
115 void vtkPVMetaDataInformation::CopyFromStream(const vtkClientServerStream* css)
116 {
117   this->SetInformationData(0);
118   vtkTypeUInt32 length;
119   if (css->GetArgumentLength(0, 0, &length) && length > 0)
120     {
121     unsigned char* raw_data = new unsigned char[length];
122     css->GetArgument(0, 0, raw_data, length);
123     vtkGenericDataObjectReader* reader = vtkGenericDataObjectReader::New();
124     reader->SetBinaryInputString(reinterpret_cast<const char*>(raw_data), length);
125     reader->ReadFromInputStringOn();
126     delete []raw_data;
127     reader->Update();
128     this->SetInformationData(reader->GetOutput());
129     reader->Delete();
130     }
131 }
132
133 void vtkPVMetaDataInformation::AddInformation(vtkPVInformation*)
134 {
135 }
136
137 //----------------------------------------------------------------------------
138 void vtkPVMetaDataInformation::PrintSelf(ostream& os, vtkIndent indent)
139 {
140   this->Superclass::PrintSelf(os, indent);
141   os << indent << "InformationData: " <<  this->InformationData << endl;
142 }