]> SALOME platform Git repositories - tools/paravisaddons_common.git/blob - src/ElectromagnetismRotation/plugin/ElectromagnetismRotationIO/vtkPVMetaDataInformation.cxx
Salome HOME
[EDF 23070] SerafinReader: Correction for vector identification
[tools/paravisaddons_common.git] / src / ElectromagnetismRotation / plugin / ElectromagnetismRotationIO / vtkPVMetaDataInformation.cxx
1 // Copyright (C) 2021  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 "MEDCouplingRefCountObject.hxx"
35
36 #include <sstream>
37
38 vtkStandardNewMacro(vtkPVMetaDataInformation);
39 vtkCxxSetObjectMacro(vtkPVMetaDataInformation, InformationData, vtkDataObject);
40
41 static vtkInformationDataObjectMetaDataKey* GetMEDReaderMetaDataIfAny()
42 {
43   static const char ZE_KEY[] = "vtkMEDReader::META_DATA";
44   MEDCoupling::GlobalDict* gd(MEDCoupling::GlobalDict::GetInstance());
45   if (!gd->hasKey(ZE_KEY))
46     return 0;
47   std::string ptSt(gd->value(ZE_KEY));
48   void* pt(0);
49   std::istringstream iss(ptSt);
50   iss >> pt;
51   return reinterpret_cast<vtkInformationDataObjectMetaDataKey*>(pt);
52 }
53
54 //----------------------------------------------------------------------------
55 vtkPVMetaDataInformation::vtkPVMetaDataInformation()
56 {
57   this->InformationData = NULL;
58 }
59
60 //----------------------------------------------------------------------------
61 vtkPVMetaDataInformation::~vtkPVMetaDataInformation()
62 {
63   this->SetInformationData(NULL);
64 }
65
66 //----------------------------------------------------------------------------
67 void vtkPVMetaDataInformation::CopyFromObject(vtkObject* obj)
68 {
69   this->SetInformationData(NULL);
70
71   vtkAlgorithmOutput* algOutput = vtkAlgorithmOutput::SafeDownCast(obj);
72   if (!algOutput)
73     {
74     vtkAlgorithm* alg = vtkAlgorithm::SafeDownCast(obj);
75     if (alg)
76       {
77       algOutput = alg->GetOutputPort(0);
78       }
79
80     }
81   if (!algOutput)
82     {
83     vtkErrorMacro("Information can only be gathered from a vtkAlgorithmOutput.");
84     return;
85     }
86
87   vtkAlgorithm* reader = algOutput->GetProducer();
88   vtkInformation* info = reader->GetExecutive()->GetOutputInformation(
89     algOutput->GetIndex());
90
91   if (info && info->Has(GetMEDReaderMetaDataIfAny()))
92     {
93     this->SetInformationData(vtkDataObject::SafeDownCast(info->Get(GetMEDReaderMetaDataIfAny())));
94     }
95 }
96
97 //----------------------------------------------------------------------------
98 void vtkPVMetaDataInformation::CopyToStream(vtkClientServerStream* css)
99 {
100   css->Reset();
101   if (!this->InformationData)
102     {
103     *css << vtkClientServerStream::Reply
104          << vtkClientServerStream::InsertArray(
105            static_cast<unsigned char*>(NULL), 0)
106          << vtkClientServerStream::End;
107     return;
108     }
109
110   vtkDataObject* clone = this->InformationData->NewInstance();
111   clone->ShallowCopy(this->InformationData);
112
113   vtkGenericDataObjectWriter* writer = vtkGenericDataObjectWriter::New();
114   writer->SetFileTypeToBinary();
115   writer->WriteToOutputStringOn();
116   writer->SetInputData(clone);
117   writer->Write();
118
119   *css << vtkClientServerStream::Reply
120        << vtkClientServerStream::InsertArray(
121          writer->GetBinaryOutputString(),
122          writer->GetOutputStringLength())
123        << vtkClientServerStream::End;
124   writer->RemoveAllInputs();
125   writer->Delete();
126   clone->Delete();
127 }
128
129 //----------------------------------------------------------------------------
130 void vtkPVMetaDataInformation::CopyFromStream(const vtkClientServerStream* css)
131 {
132   this->SetInformationData(0);
133   vtkTypeUInt32 length;
134   if (css->GetArgumentLength(0, 0, &length) && length > 0)
135     {
136     unsigned char* raw_data = new unsigned char[length];
137     css->GetArgument(0, 0, raw_data, length);
138     vtkGenericDataObjectReader* reader = vtkGenericDataObjectReader::New();
139     reader->SetBinaryInputString(reinterpret_cast<const char*>(raw_data), length);
140     reader->ReadFromInputStringOn();
141     delete []raw_data;
142     reader->Update();
143     this->SetInformationData(reader->GetOutput());
144     reader->Delete();
145     }
146 }
147
148 void vtkPVMetaDataInformation::AddInformation(vtkPVInformation*)
149 {
150 }
151
152 //----------------------------------------------------------------------------
153 void vtkPVMetaDataInformation::PrintSelf(ostream& os, vtkIndent indent)
154 {
155   this->Superclass::PrintSelf(os, indent);
156   os << indent << "InformationData: " <<  this->InformationData << endl;
157 }