1 // Copyright (C) 2021 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
19 // Author : Anthony Geay
21 #include "vtkPVMetaDataInformationERIO.h"
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"
34 #include "MEDCouplingRefCountObject.hxx"
38 vtkStandardNewMacro(vtkPVMetaDataInformationERIO);
39 vtkCxxSetObjectMacro(vtkPVMetaDataInformationERIO, InformationData, vtkDataObject);
41 static vtkInformationDataObjectMetaDataKey* GetMEDReaderMetaDataIfAny()
43 static const char ZE_KEY[] = "vtkMEDReader::META_DATA";
44 MEDCoupling::GlobalDict* gd(MEDCoupling::GlobalDict::GetInstance());
45 if (!gd->hasKey(ZE_KEY))
47 std::string ptSt(gd->value(ZE_KEY));
49 std::istringstream iss(ptSt);
51 return reinterpret_cast<vtkInformationDataObjectMetaDataKey*>(pt);
54 //----------------------------------------------------------------------------
55 vtkPVMetaDataInformationERIO::vtkPVMetaDataInformationERIO()
57 this->InformationData = NULL;
60 //----------------------------------------------------------------------------
61 vtkPVMetaDataInformationERIO::~vtkPVMetaDataInformationERIO()
63 this->SetInformationData(NULL);
66 //----------------------------------------------------------------------------
67 void vtkPVMetaDataInformationERIO::CopyFromObject(vtkObject* obj)
69 this->SetInformationData(NULL);
71 vtkAlgorithmOutput* algOutput = vtkAlgorithmOutput::SafeDownCast(obj);
74 vtkAlgorithm* alg = vtkAlgorithm::SafeDownCast(obj);
77 algOutput = alg->GetOutputPort(0);
83 vtkErrorMacro("Information can only be gathered from a vtkAlgorithmOutput.");
87 vtkAlgorithm* reader = algOutput->GetProducer();
88 vtkInformation* info = reader->GetExecutive()->GetOutputInformation(
89 algOutput->GetIndex());
91 if (info && info->Has(GetMEDReaderMetaDataIfAny()))
93 this->SetInformationData(vtkDataObject::SafeDownCast(info->Get(GetMEDReaderMetaDataIfAny())));
97 //----------------------------------------------------------------------------
98 void vtkPVMetaDataInformationERIO::CopyToStream(vtkClientServerStream* css)
101 if (!this->InformationData)
103 *css << vtkClientServerStream::Reply
104 << vtkClientServerStream::InsertArray(
105 static_cast<unsigned char*>(NULL), 0)
106 << vtkClientServerStream::End;
110 vtkDataObject* clone = this->InformationData->NewInstance();
111 clone->ShallowCopy(this->InformationData);
113 vtkGenericDataObjectWriter* writer = vtkGenericDataObjectWriter::New();
114 writer->SetFileTypeToBinary();
115 writer->WriteToOutputStringOn();
116 writer->SetInputData(clone);
119 *css << vtkClientServerStream::Reply
120 << vtkClientServerStream::InsertArray(
121 writer->GetBinaryOutputString(),
122 writer->GetOutputStringLength())
123 << vtkClientServerStream::End;
124 writer->RemoveAllInputs();
129 //----------------------------------------------------------------------------
130 void vtkPVMetaDataInformationERIO::CopyFromStream(const vtkClientServerStream* css)
132 this->SetInformationData(0);
133 vtkTypeUInt32 length;
134 if (css->GetArgumentLength(0, 0, &length) && length > 0)
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();
143 this->SetInformationData(reader->GetOutput());
148 void vtkPVMetaDataInformationERIO::AddInformation(vtkPVInformation*)
152 //----------------------------------------------------------------------------
153 void vtkPVMetaDataInformationERIO::PrintSelf(ostream& os, vtkIndent indent)
155 this->Superclass::PrintSelf(os, indent);
156 os << indent << "InformationData: " << this->InformationData << endl;