1 // Copyright (C) 2016 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 (EDF R&D)
21 #include "vtkMEDWriter.h"
23 #include "vtkAdjacentVertexIterator.h"
24 #include "vtkIntArray.h"
25 #include "vtkCellData.h"
26 #include "vtkPointData.h"
27 #include "vtkFloatArray.h"
28 #include "vtkCellArray.h"
30 #include "vtkStreamingDemandDrivenPipeline.h"
31 #include "vtkInformationDataObjectMetaDataKey.h"
32 #include "vtkUnstructuredGrid.h"
33 #include "vtkMultiBlockDataSet.h"
34 #include "vtkRectilinearGrid.h"
35 #include "vtkInformationStringKey.h"
36 #include "vtkAlgorithmOutput.h"
37 #include "vtkObjectFactory.h"
38 #include "vtkMutableDirectedGraph.h"
39 #include "vtkMultiBlockDataSet.h"
40 #include "vtkPolyData.h"
41 #include "vtkDataSet.h"
42 #include "vtkInformationVector.h"
43 #include "vtkInformation.h"
44 #include "vtkDataArraySelection.h"
45 #include "vtkTimeStamp.h"
46 #include "vtkInEdgeIterator.h"
47 #include "vtkInformationDataObjectKey.h"
48 #include "vtkExecutive.h"
49 #include "vtkVariantArray.h"
50 #include "vtkStringArray.h"
51 #include "vtkDoubleArray.h"
52 #include "vtkCharArray.h"
53 #include "vtkUnsignedCharArray.h"
54 #include "vtkDataSetAttributes.h"
55 #include "vtkDemandDrivenPipeline.h"
56 #include "vtkDataObjectTreeIterator.h"
57 #include "vtkWarpScalar.h"
59 #include "MEDFileMesh.hxx"
60 #include "MEDFileField.hxx"
61 #include "MEDFileData.hxx"
62 #include "MEDCouplingMemArray.hxx"
63 #include "MEDCouplingFieldDouble.hxx"
64 #include "MEDCouplingAutoRefCountObjectPtr.hxx"
70 using ParaMEDMEM::MEDFileData;
71 using ParaMEDMEM::MEDFileMesh;
72 using ParaMEDMEM::MEDFileCMesh;
73 using ParaMEDMEM::MEDFileUMesh;
74 using ParaMEDMEM::MEDFileFields;
75 using ParaMEDMEM::MEDFileMeshes;
77 using ParaMEDMEM::MEDFileIntField1TS;
78 using ParaMEDMEM::MEDFileField1TS;
79 using ParaMEDMEM::MEDFileIntFieldMultiTS;
80 using ParaMEDMEM::MEDFileFieldMultiTS;
81 using ParaMEDMEM::MEDFileAnyTypeFieldMultiTS;
82 using ParaMEDMEM::DataArray;
83 using ParaMEDMEM::DataArrayInt;
84 using ParaMEDMEM::DataArrayDouble;
85 using ParaMEDMEM::MEDCouplingMesh;
86 using ParaMEDMEM::MEDCouplingUMesh;
87 using ParaMEDMEM::MEDCouplingCMesh;
88 using ParaMEDMEM::MEDCouplingFieldDouble;
89 using ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr;
91 vtkStandardNewMacro(vtkMEDWriter);
95 class MZCException : public std::exception
98 MZCException(const std::string& s):_reason(s) { }
99 virtual const char *what() const throw() { return _reason.c_str(); }
100 virtual ~MZCException() throw() { }
107 std::map<int,int> ComputeMapOfType()
109 std::map<int,int> ret;
110 int nbOfTypesInMC(sizeof(MEDCouplingUMesh::PARAMEDMEM2VTKTYPETRADUCER)/sizeof(int));
111 for(int i=0;i<nbOfTypesInMC;i++)
113 int vtkId(MEDCouplingUMesh::PARAMEDMEM2VTKTYPETRADUCER[i]);
120 std::string GetMeshNameWithContext(const std::vector<int>& context)
122 static const char DFT_MESH_NAME[]="Mesh";
124 return DFT_MESH_NAME;
125 std::ostringstream oss; oss << DFT_MESH_NAME;
126 for(std::vector<int>::const_iterator it=context.begin();it!=context.end();it++)
131 DataArrayInt *ConvertVTKArrayToMCArrayInt(vtkDataArray *data)
134 throw MZCException("ConvertVTKArrayToMCArrayInt : internal error !");
135 int nbTuples(data->GetNumberOfTuples()),nbComp(data->GetNumberOfComponents());
136 std::size_t nbElts(nbTuples*nbComp);
137 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New());
138 ret->alloc(nbTuples,nbComp);
139 for(int i=0;i<nbComp;i++)
141 const char *comp(data->GetComponentName(i));
143 ret->setInfoOnComponent(i,comp);
145 int *ptOut(ret->getPointer());
146 vtkIntArray *d0(vtkIntArray::SafeDownCast(data));
149 const int *pt(d0->GetPointer(0));
150 std::copy(pt,pt+nbElts,ptOut);
153 std::ostringstream oss;
154 oss << "ConvertVTKArrayToMCArrayInt : unrecognized array \"" << typeid(*data).name() << "\" type !";
155 throw MZCException(oss.str());
158 DataArrayDouble *ConvertVTKArrayToMCArrayDouble(vtkDataArray *data)
161 throw MZCException("ConvertVTKArrayToMCArrayDouble : internal error !");
162 int nbTuples(data->GetNumberOfTuples()),nbComp(data->GetNumberOfComponents());
163 std::size_t nbElts(nbTuples*nbComp);
164 MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret(DataArrayDouble::New());
165 ret->alloc(nbTuples,nbComp);
166 for(int i=0;i<nbComp;i++)
168 const char *comp(data->GetComponentName(i));
170 ret->setInfoOnComponent(i,comp);
172 double *ptOut(ret->getPointer());
173 vtkFloatArray *d0(vtkFloatArray::SafeDownCast(data));
176 const float *pt(d0->GetPointer(0));
177 for(std::size_t i=0;i<nbElts;i++)
181 vtkDoubleArray *d1(vtkDoubleArray::SafeDownCast(data));
184 const double *pt(d1->GetPointer(0));
185 std::copy(pt,pt+nbElts,ptOut);
188 std::ostringstream oss;
189 oss << "ConvertVTKArrayToMCArrayDouble : unrecognized array \"" << typeid(*data).name() << "\" type !";
190 throw MZCException(oss.str());
193 DataArray *ConvertVTKArrayToMCArray(vtkDataArray *data)
196 throw MZCException("ConvertVTKArrayToMCArray : internal error !");
197 vtkFloatArray *d0(vtkFloatArray::SafeDownCast(data));
198 vtkDoubleArray *d1(vtkDoubleArray::SafeDownCast(data));
200 return ConvertVTKArrayToMCArrayDouble(data);
201 vtkIntArray *d2(vtkIntArray::SafeDownCast(data));
203 return ConvertVTKArrayToMCArrayInt(data);
204 std::ostringstream oss;
205 oss << "ConvertVTKArrayToMCArray : unrecognized array \"" << typeid(*data).name() << "\" type !";
206 throw MZCException(oss.str());
209 MEDCouplingUMesh *BuildMeshFromCellArray(vtkCellArray *ca, DataArrayDouble *coords, int meshDim, INTERP_KERNEL::NormalizedCellType type)
211 MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> subMesh(MEDCouplingUMesh::New("",meshDim));
212 subMesh->setCoords(coords); subMesh->allocateCells();
213 int nbCells(ca->GetNumberOfCells());
216 vtkIdType nbEntries(ca->GetNumberOfConnectivityEntries());
217 const vtkIdType *conn(ca->GetPointer());
218 for(int i=0;i<nbCells;i++)
221 subMesh->insertNextCell(type,sz,conn);
224 return subMesh.retn();
227 MEDCouplingUMesh *BuildMeshFromCellArrayTriangleStrip(vtkCellArray *ca, DataArrayDouble *coords, MEDCouplingAutoRefCountObjectPtr<DataArrayInt>& ids)
229 MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> subMesh(MEDCouplingUMesh::New("",2));
230 subMesh->setCoords(coords); subMesh->allocateCells();
231 int nbCells(ca->GetNumberOfCells());
234 vtkIdType nbEntries(ca->GetNumberOfConnectivityEntries());
235 const vtkIdType *conn(ca->GetPointer());
236 ids=DataArrayInt::New() ; ids->alloc(0,1);
237 for(int i=0;i<nbCells;i++)
243 for(int j=0;j<nbTri;j++,conn++)
245 subMesh->insertNextCell(INTERP_KERNEL::NORM_TRI3,3,conn);
246 ids->pushBackSilent(i);
251 std::ostringstream oss; oss << "BuildMeshFromCellArrayTriangleStrip : on cell #" << i << " the triangle stip looks bab !";
252 throw MZCException(oss.str());
256 return subMesh.retn();
262 MicroField(const MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh>& m, const std::vector<MEDCouplingAutoRefCountObjectPtr<DataArray> >& cellFs):_m(m),_cellFs(cellFs) { }
263 MicroField(const std::vector< MicroField >& vs);
264 void setNodeFields(const std::vector<MEDCouplingAutoRefCountObjectPtr<DataArray> >& nf) { _nodeFs=nf; }
265 MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> getMesh() const { return _m; }
266 std::vector<MEDCouplingAutoRefCountObjectPtr<DataArray> > getCellFields() const { return _cellFs; }
268 MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> _m;
269 std::vector<MEDCouplingAutoRefCountObjectPtr<DataArray> > _cellFs;
270 std::vector<MEDCouplingAutoRefCountObjectPtr<DataArray> > _nodeFs;
273 MicroField::MicroField(const std::vector< MicroField >& vs)
275 std::size_t sz(vs.size());
276 std::vector<const MEDCouplingUMesh *> vs2(sz);
277 std::vector< std::vector< MEDCouplingAutoRefCountObjectPtr<DataArray> > > arrs2(sz);
279 for(std::size_t ii=0;ii<sz;ii++)
281 vs2[ii]=vs[ii].getMesh();
282 arrs2[ii]=vs[ii].getCellFields();
284 nbElts=arrs2[ii].size();
286 if(arrs2[ii].size()!=nbElts)
287 throw MZCException("MicroField cstor : internal error !");
289 _cellFs.resize(nbElts);
290 for(int ii=0;ii<nbElts;ii++)
292 std::vector<const DataArray *> arrsTmp(sz);
293 for(std::size_t jj=0;jj<sz;jj++)
295 arrsTmp[jj]=arrs2[jj][ii];
297 _cellFs[ii]=DataArray::Aggregate(arrsTmp);
299 _m=MEDCouplingUMesh::MergeUMeshesOnSameCoords(vs2);
302 void AppendMCFieldFrom(ParaMEDMEM::TypeOfField tf, MEDCouplingMesh *mesh, MEDFileData *mfd, MEDCouplingAutoRefCountObjectPtr<DataArray> da, const DataArrayInt *n2oPtr)
304 static const char FAMFIELD_FOR_CELLS[]="FamilyIdCell";
305 static const char FAMFIELD_FOR_NODES[]="FamilyIdNode";
306 if(!da || !mesh || !mfd)
307 throw MZCException("AppendMCFieldFrom : internal error !");
308 MEDFileFields *fs(mfd->getFields());
309 MEDFileMeshes *ms(mfd->getMeshes());
311 throw MZCException("AppendMCFieldFrom : internal error 2 !");
312 MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> dad(ParaMEDMEM::DynamicCast<DataArray,DataArrayDouble>(da));
313 DataArrayDouble *dadPtr(dad);
314 std::string fieldName;
317 fieldName=dadPtr->getName();
318 MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> f(MEDCouplingFieldDouble::New(tf));
319 f->setName(fieldName);
324 MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> dad2(dadPtr->selectByTupleId(n2oPtr->begin(),n2oPtr->end()));
328 MEDCouplingAutoRefCountObjectPtr<MEDFileFieldMultiTS> fmts(MEDFileFieldMultiTS::New());
329 MEDCouplingAutoRefCountObjectPtr<MEDFileField1TS> f1ts(MEDFileField1TS::New());
330 f1ts->setFieldNoProfileSBT(f);
331 fmts->pushBackTimeStep(f1ts);
335 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> dai(ParaMEDMEM::DynamicCast<DataArray,DataArrayInt>(da));
336 DataArrayInt *daiPtr(dai);
339 fieldName=daiPtr->getName();
340 if((fieldName!=FAMFIELD_FOR_CELLS || tf!=ParaMEDMEM::ON_CELLS) && (fieldName!=FAMFIELD_FOR_NODES || tf!=ParaMEDMEM::ON_NODES))
342 MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> f(MEDCouplingFieldDouble::New(tf));
343 f->setName(fieldName);
345 MEDCouplingAutoRefCountObjectPtr<MEDFileIntFieldMultiTS> fmts(MEDFileIntFieldMultiTS::New());
346 MEDCouplingAutoRefCountObjectPtr<MEDFileIntField1TS> f1ts(MEDFileIntField1TS::New());
348 f1ts->setFieldNoProfileSBT(f,daiPtr);
351 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> dai2(daiPtr->selectByTupleId(n2oPtr->begin(),n2oPtr->end()));
352 f1ts->setFieldNoProfileSBT(f,dai2);
354 fmts->pushBackTimeStep(f1ts);
358 else if(fieldName==FAMFIELD_FOR_CELLS && tf==ParaMEDMEM::ON_CELLS)
360 MEDFileMesh *mm(ms->getMeshWithName(mesh->getName()));
362 throw MZCException("AppendMCFieldFrom : internal error 3 !");
364 mm->setFamilyFieldArr(mesh->getMeshDimension()-mm->getMeshDimension(),daiPtr);
367 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> dai2(daiPtr->selectByTupleId(n2oPtr->begin(),n2oPtr->end()));
368 mm->setFamilyFieldArr(mesh->getMeshDimension()-mm->getMeshDimension(),dai2);
371 else if(fieldName==FAMFIELD_FOR_NODES || tf==ParaMEDMEM::ON_NODES)
373 MEDFileMesh *mm(ms->getMeshWithName(mesh->getName()));
375 throw MZCException("AppendMCFieldFrom : internal error 4 !");
377 mm->setFamilyFieldArr(1,daiPtr);
380 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> dai2(daiPtr->selectByTupleId(n2oPtr->begin(),n2oPtr->end()));
381 mm->setFamilyFieldArr(1,dai2);
387 void PutAtLevelDealOrder(MEDFileData *mfd, int meshDimRel, const MicroField& mf)
390 throw MZCException("PutAtLevelDealOrder : internal error !");
391 MEDFileMesh *mm(mfd->getMeshes()->getMeshAtPos(0));
392 MEDFileUMesh *mmu(dynamic_cast<MEDFileUMesh *>(mm));
394 throw MZCException("PutAtLevelDealOrder : internal error 2 !");
395 MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> mesh(mf.getMesh());
396 mesh->setName(mfd->getMeshes()->getMeshAtPos(0)->getName());
397 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> o2n(mesh->sortCellsInMEDFileFrmt());
398 const DataArrayInt *o2nPtr(o2n);
399 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> n2o;
400 mmu->setMeshAtLevel(meshDimRel,mesh);
401 const DataArrayInt *n2oPtr(0);
404 n2o=o2n->invertArrayO2N2N2O(mesh->getNumberOfCells());
406 if(n2oPtr && n2oPtr->isIdentity2(mesh->getNumberOfCells()))
409 mm->setRenumFieldArr(meshDimRel,n2o);
412 std::vector<MEDCouplingAutoRefCountObjectPtr<DataArray> > cells(mf.getCellFields());
413 for(std::vector<MEDCouplingAutoRefCountObjectPtr<DataArray> >::const_iterator it=cells.begin();it!=cells.end();it++)
415 MEDCouplingAutoRefCountObjectPtr<DataArray> da(*it);
416 AppendMCFieldFrom(ParaMEDMEM::ON_CELLS,mesh,mfd,da,n2oPtr);
420 void AssignSingleGTMeshes(MEDFileData *mfd, const std::vector< MicroField >& ms)
423 throw MZCException("AssignSingleGTMeshes : internal error !");
424 MEDFileMesh *mm0(mfd->getMeshes()->getMeshAtPos(0));
425 MEDFileUMesh *mm(dynamic_cast<MEDFileUMesh *>(mm0));
427 throw MZCException("AssignSingleGTMeshes : internal error 2 !");
428 int meshDim(-std::numeric_limits<int>::max());
429 std::map<int, std::vector< MicroField > > ms2;
430 for(std::vector< MicroField >::const_iterator it=ms.begin();it!=ms.end();it++)
432 const MEDCouplingUMesh *elt((*it).getMesh());
435 int myMeshDim(elt->getMeshDimension());
436 meshDim=std::max(meshDim,myMeshDim);
437 ms2[myMeshDim].push_back(*it);
442 for(std::map<int, std::vector< MicroField > >::const_iterator it=ms2.begin();it!=ms2.end();it++)
444 const std::vector< MicroField >& vs((*it).second);
447 PutAtLevelDealOrder(mfd,(*it).first-meshDim,vs[0]);
451 MicroField merge(vs);
452 PutAtLevelDealOrder(mfd,(*it).first-meshDim,merge);
457 DataArrayDouble *BuildCoordsFrom(vtkPointSet *ds)
460 throw MZCException("BuildCoordsFrom : internal error !");
461 vtkPoints *pts(ds->GetPoints());
463 throw MZCException("BuildCoordsFrom : internal error 2 !");
464 vtkDataArray *data(pts->GetData());
466 throw MZCException("BuildCoordsFrom : internal error 3 !");
467 MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> coords(ConvertVTKArrayToMCArrayDouble(data));
468 return coords.retn();
471 void AddNodeFields(MEDFileData *mfd, vtkDataSetAttributes *dsa)
474 throw MZCException("AddNodeFields : internal error !");
475 MEDFileMesh *mm(mfd->getMeshes()->getMeshAtPos(0));
476 MEDFileUMesh *mmu(dynamic_cast<MEDFileUMesh *>(mm));
478 throw MZCException("AddNodeFields : internal error 2 !");
479 MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> mesh(mmu->getMeshAtLevel(0));
480 int nba(dsa->GetNumberOfArrays());
481 for(int i=0;i<nba;i++)
483 vtkDataArray *arr(dsa->GetArray(i));
484 const char *name(arr->GetName());
487 MEDCouplingAutoRefCountObjectPtr<DataArray> da(ConvertVTKArrayToMCArray(arr));
489 AppendMCFieldFrom(ParaMEDMEM::ON_NODES,mesh,mfd,da,0);
493 std::vector<MEDCouplingAutoRefCountObjectPtr<DataArray> > AddPartFields(const DataArrayInt *part, vtkDataSetAttributes *dsa)
495 std::vector< MEDCouplingAutoRefCountObjectPtr<DataArray> > ret;
498 int nba(dsa->GetNumberOfArrays());
499 for(int i=0;i<nba;i++)
501 vtkDataArray *arr(dsa->GetArray(i));
504 const char *name(arr->GetName());
505 int nbCompo(arr->GetNumberOfComponents());
506 vtkIdType nbTuples(arr->GetNumberOfTuples());
507 MEDCouplingAutoRefCountObjectPtr<DataArray> mcarr(ConvertVTKArrayToMCArray(arr));
509 mcarr=mcarr->selectByTupleId(part->begin(),part->end());
510 mcarr->setName(name);
511 ret.push_back(mcarr);
516 std::vector<MEDCouplingAutoRefCountObjectPtr<DataArray> > AddPartFields2(int bg, int end, vtkDataSetAttributes *dsa)
518 std::vector< MEDCouplingAutoRefCountObjectPtr<DataArray> > ret;
521 int nba(dsa->GetNumberOfArrays());
522 for(int i=0;i<nba;i++)
524 vtkDataArray *arr(dsa->GetArray(i));
527 const char *name(arr->GetName());
528 int nbCompo(arr->GetNumberOfComponents());
529 vtkIdType nbTuples(arr->GetNumberOfTuples());
530 MEDCouplingAutoRefCountObjectPtr<DataArray> mcarr(ConvertVTKArrayToMCArray(arr));
531 mcarr=mcarr->selectByTupleId2(bg,end,1);
532 mcarr->setName(name);
533 ret.push_back(mcarr);
538 void ConvertFromRectilinearGrid(MEDFileData *ret, vtkRectilinearGrid *ds, const std::vector<int>& context)
541 throw MZCException("ConvertFromRectilinearGrid : internal error !");
543 MEDCouplingAutoRefCountObjectPtr<MEDFileMeshes> meshes(MEDFileMeshes::New());
544 ret->setMeshes(meshes);
545 MEDCouplingAutoRefCountObjectPtr<MEDFileFields> fields(MEDFileFields::New());
546 ret->setFields(fields);
548 MEDCouplingAutoRefCountObjectPtr<MEDFileCMesh> cmesh(MEDFileCMesh::New());
549 meshes->pushMesh(cmesh);
550 MEDCouplingAutoRefCountObjectPtr<MEDCouplingCMesh> cmeshmc(MEDCouplingCMesh::New());
551 vtkDataArray *cx(ds->GetXCoordinates()),*cy(ds->GetYCoordinates()),*cz(ds->GetZCoordinates());
554 MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> arr(ConvertVTKArrayToMCArrayDouble(cx));
555 cmeshmc->setCoordsAt(0,arr);
559 MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> arr(ConvertVTKArrayToMCArrayDouble(cy));
560 cmeshmc->setCoordsAt(1,arr);
564 MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> arr(ConvertVTKArrayToMCArrayDouble(cz));
565 cmeshmc->setCoordsAt(2,arr);
567 std::string meshName(GetMeshNameWithContext(context));
568 cmeshmc->setName(meshName);
569 cmesh->setMesh(cmeshmc);
570 std::vector<MEDCouplingAutoRefCountObjectPtr<DataArray> > cellFs(AddPartFields(0,ds->GetCellData()));
571 for(std::vector<MEDCouplingAutoRefCountObjectPtr<DataArray> >::const_iterator it=cellFs.begin();it!=cellFs.end();it++)
573 MEDCouplingAutoRefCountObjectPtr<DataArray> da(*it);
574 AppendMCFieldFrom(ParaMEDMEM::ON_CELLS,cmeshmc,ret,da,0);
576 std::vector<MEDCouplingAutoRefCountObjectPtr<DataArray> > nodeFs(AddPartFields(0,ds->GetPointData()));
577 for(std::vector<MEDCouplingAutoRefCountObjectPtr<DataArray> >::const_iterator it=nodeFs.begin();it!=nodeFs.end();it++)
579 MEDCouplingAutoRefCountObjectPtr<DataArray> da(*it);
580 AppendMCFieldFrom(ParaMEDMEM::ON_NODES,cmeshmc,ret,da,0);
584 void ConvertFromPolyData(MEDFileData *ret, vtkPolyData *ds, const std::vector<int>& context)
587 throw MZCException("ConvertFromPolyData : internal error !");
589 MEDCouplingAutoRefCountObjectPtr<MEDFileMeshes> meshes(MEDFileMeshes::New());
590 ret->setMeshes(meshes);
591 MEDCouplingAutoRefCountObjectPtr<MEDFileFields> fields(MEDFileFields::New());
592 ret->setFields(fields);
594 MEDCouplingAutoRefCountObjectPtr<MEDFileUMesh> umesh(MEDFileUMesh::New());
595 meshes->pushMesh(umesh);
596 MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> coords(BuildCoordsFrom(ds));
597 umesh->setCoords(coords);
598 umesh->setName(GetMeshNameWithContext(context));
601 std::vector< MicroField > ms;
602 vtkCellArray *cd(ds->GetVerts());
605 MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> subMesh(BuildMeshFromCellArray(cd,coords,0,INTERP_KERNEL::NORM_POINT1));
606 if((const MEDCouplingUMesh *)subMesh)
608 std::vector<MEDCouplingAutoRefCountObjectPtr<DataArray> > cellFs(AddPartFields2(offset,offset+subMesh->getNumberOfCells(),ds->GetCellData()));
609 offset+=subMesh->getNumberOfCells();
610 ms.push_back(MicroField(subMesh,cellFs));
613 vtkCellArray *cc(ds->GetLines());
616 MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> subMesh(BuildMeshFromCellArray(cc,coords,1,INTERP_KERNEL::NORM_SEG2));
617 if((const MEDCouplingUMesh *)subMesh)
619 std::vector<MEDCouplingAutoRefCountObjectPtr<DataArray> > cellFs(AddPartFields2(offset,offset+subMesh->getNumberOfCells(),ds->GetCellData()));
620 offset+=subMesh->getNumberOfCells();
621 ms.push_back(MicroField(subMesh,cellFs));
624 vtkCellArray *cb(ds->GetPolys());
627 MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> subMesh(BuildMeshFromCellArray(cb,coords,2,INTERP_KERNEL::NORM_POLYGON));
628 if((const MEDCouplingUMesh *)subMesh)
630 std::vector<MEDCouplingAutoRefCountObjectPtr<DataArray> > cellFs(AddPartFields2(offset,offset+subMesh->getNumberOfCells(),ds->GetCellData()));
631 offset+=subMesh->getNumberOfCells();
632 ms.push_back(MicroField(subMesh,cellFs));
635 vtkCellArray *ca(ds->GetStrips());
638 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ids;
639 MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> subMesh(BuildMeshFromCellArrayTriangleStrip(ca,coords,ids));
640 if((const MEDCouplingUMesh *)subMesh)
642 std::vector<MEDCouplingAutoRefCountObjectPtr<DataArray> > cellFs(AddPartFields(ids,ds->GetCellData()));
643 offset+=subMesh->getNumberOfCells();
644 ms.push_back(MicroField(subMesh,cellFs));
647 AssignSingleGTMeshes(ret,ms);
648 AddNodeFields(ret,ds->GetPointData());
651 void ConvertFromUnstructuredGrid(MEDFileData *ret, vtkUnstructuredGrid *ds, const std::vector<int>& context)
654 throw MZCException("ConvertFromUnstructuredGrid : internal error !");
656 MEDCouplingAutoRefCountObjectPtr<MEDFileMeshes> meshes(MEDFileMeshes::New());
657 ret->setMeshes(meshes);
658 MEDCouplingAutoRefCountObjectPtr<MEDFileFields> fields(MEDFileFields::New());
659 ret->setFields(fields);
661 MEDCouplingAutoRefCountObjectPtr<MEDFileUMesh> umesh(MEDFileUMesh::New());
662 meshes->pushMesh(umesh);
663 MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> coords(BuildCoordsFrom(ds));
664 umesh->setCoords(coords);
665 umesh->setName(GetMeshNameWithContext(context));
666 vtkIdType nbCells(ds->GetNumberOfCells());
667 vtkCellArray *ca(ds->GetCells());
670 vtkIdType nbEnt(ca->GetNumberOfConnectivityEntries());
671 vtkIdType *caPtr(ca->GetPointer());
672 vtkUnsignedCharArray *ct(ds->GetCellTypesArray());
674 throw MZCException("ConvertFromUnstructuredGrid : internal error");
675 vtkIdTypeArray *cla(ds->GetCellLocationsArray());
676 const vtkIdType *claPtr(cla->GetPointer(0));
678 throw MZCException("ConvertFromUnstructuredGrid : internal error 2");
679 const unsigned char *ctPtr(ct->GetPointer(0));
680 std::map<int,int> m(ComputeMapOfType());
681 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> lev(DataArrayInt::New()) ; lev->alloc(nbCells,1);
682 int *levPtr(lev->getPointer());
683 for(vtkIdType i=0;i<nbCells;i++)
685 std::map<int,int>::iterator it(m.find(ctPtr[i]));
688 const INTERP_KERNEL::CellModel& cm(INTERP_KERNEL::CellModel::GetCellModel((INTERP_KERNEL::NormalizedCellType)(*it).second));
689 levPtr[i]=cm.getDimension();
693 std::ostringstream oss; oss << "ConvertFromUnstructuredGrid : at pos #" << i << " unrecognized VTK cell with type =" << ctPtr[i];
694 throw MZCException(oss.str());
698 int meshDim(lev->getMaxValue(dummy));
699 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> levs(lev->getDifferentValues());
700 std::vector< MicroField > ms;
701 vtkIdTypeArray *faces(ds->GetFaces()),*faceLoc(ds->GetFaceLocations());
702 for(const int *curLev=levs->begin();curLev!=levs->end();curLev++)
704 MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> m0(MEDCouplingUMesh::New("",*curLev));
705 m0->setCoords(coords); m0->allocateCells();
706 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> cellIdsCurLev(lev->getIdsEqual(*curLev));
707 for(const int *cellId=cellIdsCurLev->begin();cellId!=cellIdsCurLev->end();cellId++)
709 std::map<int,int>::iterator it(m.find(ctPtr[*cellId]));
710 vtkIdType offset(claPtr[*cellId]);
711 vtkIdType sz(caPtr[offset]);
712 INTERP_KERNEL::NormalizedCellType ct((INTERP_KERNEL::NormalizedCellType)(*it).second);
713 if(ct!=INTERP_KERNEL::NORM_POLYHED)
714 m0->insertNextCell(ct,sz,caPtr+offset+1);
717 if(!faces || !faceLoc)
718 throw MZCException("ConvertFromUnstructuredGrid : faces are expected when there are polyhedra !");
719 const vtkIdType *facPtr(faces->GetPointer(0)),*facLocPtr(faceLoc->GetPointer(0));
720 std::vector<int> conn;
721 int off0(facLocPtr[*cellId]);
722 int nbOfFaces(facPtr[off0++]);
723 for(int k=0;k<nbOfFaces;k++)
725 int nbOfNodesInFace(facPtr[off0++]);
726 std::copy(facPtr+off0,facPtr+off0+nbOfNodesInFace,std::back_inserter(conn));
727 off0+=nbOfNodesInFace;
731 m0->insertNextCell(ct,conn.size(),&conn[0]);
734 std::vector<MEDCouplingAutoRefCountObjectPtr<DataArray> > cellFs(AddPartFields(cellIdsCurLev,ds->GetCellData()));
735 ms.push_back(MicroField(m0,cellFs));
737 AssignSingleGTMeshes(ret,ms);
738 AddNodeFields(ret,ds->GetPointData());
743 vtkMEDWriter::vtkMEDWriter():WriteAllTimeSteps(0),FileName(0),IsTouched(false)
747 vtkMEDWriter::~vtkMEDWriter()
751 vtkInformationDataObjectMetaDataKey *GetMEDReaderMetaDataIfAny()
753 static const char ZE_KEY[]="vtkMEDReader::META_DATA";
754 ParaMEDMEM::GlobalDict *gd(ParaMEDMEM::GlobalDict::GetInstance());
755 if(!gd->hasKey(ZE_KEY))
757 std::string ptSt(gd->value(ZE_KEY));
759 std::istringstream iss(ptSt); iss >> pt;
760 return reinterpret_cast<vtkInformationDataObjectMetaDataKey *>(pt);
763 bool IsInformationOK(vtkInformation *info)
765 vtkInformationDataObjectMetaDataKey *key(GetMEDReaderMetaDataIfAny());
768 // Check the information contain meta data key
772 vtkMutableDirectedGraph *sil(vtkMutableDirectedGraph::SafeDownCast(info->Get(key)));
776 vtkAbstractArray *verticesNames(sil->GetVertexData()->GetAbstractArray("Names",idNames));
777 vtkStringArray *verticesNames2(vtkStringArray::SafeDownCast(verticesNames));
780 for(int i=0;i<verticesNames2->GetNumberOfValues();i++)
782 vtkStdString &st(verticesNames2->GetValue(i));
783 if(st=="MeshesFamsGrps")
792 Grp(const std::string& name):_name(name) { }
793 void setFamilies(const std::vector<std::string>& fams) { _fams=fams; }
794 std::string getName() const { return _name; }
795 std::vector<std::string> getFamilies() const { return _fams; }
798 std::vector<std::string> _fams;
804 Fam(const std::string& name)
806 static const char ZE_SEP[]="@@][@@";
807 std::size_t pos(name.find(ZE_SEP));
808 std::string name0(name.substr(0,pos)),name1(name.substr(pos+strlen(ZE_SEP)));
809 std::istringstream iss(name1);
813 std::string getName() const { return _name; }
814 int getID() const { return _id; }
820 void LoadFamGrpMapInfo(vtkMutableDirectedGraph *sil, std::string& meshName, std::vector<Grp>& groups, std::vector<Fam>& fams)
823 throw MZCException("LoadFamGrpMapInfo : internal error !");
825 vtkAbstractArray *verticesNames(sil->GetVertexData()->GetAbstractArray("Names",idNames));
826 vtkStringArray *verticesNames2(vtkStringArray::SafeDownCast(verticesNames));
829 for(int i=0;i<verticesNames2->GetNumberOfValues();i++)
831 vtkStdString &st(verticesNames2->GetValue(i));
832 if(st=="MeshesFamsGrps")
839 throw INTERP_KERNEL::Exception("There is an internal error ! The tree on server side has not the expected look !");
840 vtkAdjacentVertexIterator *it0(vtkAdjacentVertexIterator::New());
841 sil->GetAdjacentVertices(id0,it0);
843 while(it0->HasNext())
845 vtkIdType id1(it0->Next());
846 std::string mName((const char *)verticesNames2->GetValue(id1));
848 vtkAdjacentVertexIterator *it1(vtkAdjacentVertexIterator::New());
849 sil->GetAdjacentVertices(id1,it1);
850 vtkIdType idZeGrps(it1->Next());//zeGroups
851 vtkAdjacentVertexIterator *itGrps(vtkAdjacentVertexIterator::New());
852 sil->GetAdjacentVertices(idZeGrps,itGrps);
853 while(itGrps->HasNext())
855 vtkIdType idg(itGrps->Next());
856 Grp grp((const char *)verticesNames2->GetValue(idg));
857 vtkAdjacentVertexIterator *itGrps2(vtkAdjacentVertexIterator::New());
858 sil->GetAdjacentVertices(idg,itGrps2);
859 std::vector<std::string> famsOnGroup;
860 while(itGrps2->HasNext())
862 vtkIdType idgf(itGrps2->Next());
863 famsOnGroup.push_back(std::string((const char *)verticesNames2->GetValue(idgf)));
865 grp.setFamilies(famsOnGroup);
867 groups.push_back(grp);
870 vtkIdType idZeFams(it1->Next());//zeFams
872 vtkAdjacentVertexIterator *itFams(vtkAdjacentVertexIterator::New());
873 sil->GetAdjacentVertices(idZeFams,itFams);
874 while(itFams->HasNext())
876 vtkIdType idf(itFams->Next());
877 Fam fam((const char *)verticesNames2->GetValue(idf));
885 int vtkMEDWriter::RequestInformation(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
887 //std::cerr << "########################################## vtkMEDWriter::RequestInformation ########################################## " << (const char *) this->FileName << std::endl;
891 void WriteMEDFileFromVTKDataSet(MEDFileData *mfd, vtkDataSet *ds, const std::vector<int>& context)
894 throw MZCException("Internal error in WriteMEDFileFromVTKDataSet.");
895 vtkPolyData *ds2(vtkPolyData::SafeDownCast(ds));
896 vtkUnstructuredGrid *ds3(vtkUnstructuredGrid::SafeDownCast(ds));
897 vtkRectilinearGrid *ds4(vtkRectilinearGrid::SafeDownCast(ds));
900 ConvertFromPolyData(mfd,ds2,context);
904 ConvertFromUnstructuredGrid(mfd,ds3,context);
908 ConvertFromRectilinearGrid(mfd,ds4,context);
911 throw MZCException("Unrecognized vtkDataSet ! Sorry ! Try to convert it to UnstructuredGrid to be able to write it !");
914 void WriteMEDFileFromVTKMultiBlock(MEDFileData *mfd, vtkMultiBlockDataSet *ds, const std::vector<int>& context)
917 throw MZCException("Internal error in WriteMEDFileFromVTKMultiBlock.");
918 int nbBlocks(ds->GetNumberOfBlocks());
919 if(nbBlocks==1 && context.empty())
921 vtkDataObject *uniqueElt(ds->GetBlock(0));
923 throw MZCException("Unique elt in multiblock is NULL !");
924 vtkDataSet *uniqueEltc(vtkDataSet::SafeDownCast(uniqueElt));
927 WriteMEDFileFromVTKDataSet(mfd,uniqueEltc,context);
931 for(int i=0;i<nbBlocks;i++)
933 vtkDataObject *elt(ds->GetBlock(i));
934 std::vector<int> context2;
935 context2.push_back(i);
938 std::ostringstream oss; oss << "In context ";
939 std::copy(context.begin(),context.end(),std::ostream_iterator<int>(oss," "));
940 oss << " at pos #" << i << " elt is NULL !";
941 throw MZCException(oss.str());
943 vtkDataSet *elt1(vtkDataSet::SafeDownCast(elt));
946 WriteMEDFileFromVTKDataSet(mfd,elt1,context);
949 vtkMultiBlockDataSet *elt2(vtkMultiBlockDataSet::SafeDownCast(elt));
952 WriteMEDFileFromVTKMultiBlock(mfd,elt2,context);
955 std::ostringstream oss; oss << "In context ";
956 std::copy(context.begin(),context.end(),std::ostream_iterator<int>(oss," "));
957 oss << " at pos #" << i << " elt not recognized data type !";
958 throw MZCException(oss.str());
962 void WriteMEDFileFromVTKGDS(MEDFileData *mfd, vtkDataObject *input)
965 throw MZCException("WriteMEDFileFromVTKGDS : internal error !");
966 std::vector<int> context;
967 vtkDataSet *input1(vtkDataSet::SafeDownCast(input));
970 WriteMEDFileFromVTKDataSet(mfd,input1,context);
973 vtkMultiBlockDataSet *input2(vtkMultiBlockDataSet::SafeDownCast(input));
976 WriteMEDFileFromVTKMultiBlock(mfd,input2,context);
979 throw MZCException("WriteMEDFileFromVTKGDS : not recognized data type !");
982 void PutFamGrpInfoIfAny(MEDFileData *mfd, const std::string& meshName, const std::vector<Grp>& groups, const std::vector<Fam>& fams)
988 MEDFileMeshes *meshes(mfd->getMeshes());
991 if(meshes->getNumberOfMeshes()!=1)
993 MEDFileMesh *mm(meshes->getMeshAtPos(0));
996 mm->setName(meshName);
997 for(std::vector<Fam>::const_iterator it=fams.begin();it!=fams.end();it++)
998 mm->setFamilyId((*it).getName(),(*it).getID());
999 for(std::vector<Grp>::const_iterator it=groups.begin();it!=groups.end();it++)
1000 mm->setFamiliesOnGroup((*it).getName(),(*it).getFamilies());
1001 MEDFileFields *fields(mfd->getFields());
1004 for(int i=0;i<fields->getNumberOfFields();i++)
1006 MEDFileAnyTypeFieldMultiTS *fmts(fields->getFieldAtPos(i));
1009 fmts->setMeshName(meshName);
1013 int vtkMEDWriter::RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
1015 //std::cerr << "########################################## vtkMEDWriter::RequestData ########################################## " << (const char *) this->FileName << std::endl;
1018 vtkInformation *inputInfo(inputVector[0]->GetInformationObject(0));
1019 std::string meshName;
1020 std::vector<Grp> groups; std::vector<Fam> fams;
1021 if(IsInformationOK(inputInfo))
1023 vtkMutableDirectedGraph *famGrpGraph(vtkMutableDirectedGraph::SafeDownCast(inputInfo->Get(GetMEDReaderMetaDataIfAny())));
1024 LoadFamGrpMapInfo(famGrpGraph,meshName,groups,fams);
1026 vtkInformation *outInfo(outputVector->GetInformationObject(0));
1027 vtkDataObject *input(vtkDataObject::SafeDownCast(inputInfo->Get(vtkDataObject::DATA_OBJECT())));
1029 throw MZCException("Not recognized data object in input of the MEDWriter ! Maybe not implemented yet !");
1030 MEDCouplingAutoRefCountObjectPtr<MEDFileData> mfd(MEDFileData::New());
1031 WriteMEDFileFromVTKGDS(mfd,input);
1032 PutFamGrpInfoIfAny(mfd,meshName,groups,fams);
1033 mfd->write(this->FileName,this->IsTouched?0:2); this->IsTouched=true;
1034 outInfo->Set(vtkDataObject::DATA_OBJECT(),input);
1036 catch(MZCException& e)
1038 std::ostringstream oss;
1039 oss << "Exception has been thrown in vtkMEDWriter::RequestData : During writing of \"" << (const char *) this->FileName << "\", the following exception has been thrown : "<< e.what() << std::endl;
1040 if(this->HasObserver("ErrorEvent") )
1041 this->InvokeEvent("ErrorEvent",const_cast<char *>(oss.str().c_str()));
1043 vtkOutputWindowDisplayErrorText(const_cast<char *>(oss.str().c_str()));
1044 vtkObject::BreakOnError();
1050 void vtkMEDWriter::PrintSelf(ostream& os, vtkIndent indent)
1052 this->Superclass::PrintSelf(os, indent);
1055 int vtkMEDWriter::Write()