Path: blob/devel/ElmerGUI/Application/vtkpost/readepfile.cpp
3203 views
/*****************************************************************************1* *2* Elmer, A Finite Element Software for Multiphysical Problems *3* *4* Copyright 1st April 1995 - , CSC - IT Center for Science Ltd., Finland *5* *6* This program is free software; you can redistribute it and/or *7* modify it under the terms of the GNU General Public License *8* as published by the Free Software Foundation; either version 2 *9* of the License, or (at your option) any later version. *10* *11* This program is distributed in the hope that it will be useful, *12* but WITHOUT ANY WARRANTY; without even the implied warranty of *13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *14* GNU General Public License for more details. *15* *16* You should have received a copy of the GNU General Public License *17* along with this program (in file fem/GPL-2); if not, write to the *18* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *19* Boston, MA 02110-1301, USA. *20* *21*****************************************************************************/2223/*****************************************************************************24* *25* ElmerGUI readepfile *26* *27*****************************************************************************28* *29* Authors: Mikko Lyly, Juha Ruokolainen and Peter R�back *30* Email: [email protected] *31* Web: http://www.csc.fi/elmer *32* Address: CSC - IT Center for Science Ltd. *33* Keilaranta 14 *34* 02101 Espoo, Finland *35* *36* Original Date: 15 Mar 2008 *37* *38*****************************************************************************/39#if WITH_QT5 || WITH_QT640#include <QtWidgets>41#endif42#include <QtGui>43#include <iostream>44#include <vtkXMLUnstructuredGridReader.h>45#include <vtkUnstructuredGrid.h>46#include <vtkDataSet.h>47#include <vtkPointData.h>48#include <vtkCellData.h>49#include "readepfile.h"5051using namespace std;5253ReadEpFile::ReadEpFile(QWidget *parent)54: QDialog(parent)55{56ui.setupUi(this);5758connect(ui.browseButton, SIGNAL(clicked()), this, SLOT(browseButtonClickedSlot()));59connect(ui.applyButton, SIGNAL(clicked()), this, SLOT(applyButtonClickedSlot()));60connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(cancelButtonClickedSlot()));61connect(ui.okButton, SIGNAL(clicked()), this, SLOT(okButtonClickedSlot()));62connect(ui.allButton, SIGNAL(clicked()), this, SLOT(allButtonClickedSlot()));6364ui.nodesEdit->setEnabled(false);65ui.elementsEdit->setEnabled(false);66ui.timestepsEdit->setEnabled(false);67ui.dofsEdit->setEnabled(false);6869setWindowTitle("Read input file");70setWindowIcon(QIcon(":/icons/Mesh3D.png"));71}7273ReadEpFile::~ReadEpFile()74{75}7677void ReadEpFile::browseButtonClickedSlot()78{79QString fileName = QFileDialog::getOpenFileName(this, tr("Select input file"), "", tr("Postprocessor files (*.ep *.vtu);;ElmerPost files (*.ep);;Paraview files (*.vtu)"));8081ui.fileName->setText(fileName.trimmed());82ui.start->setValue(1);83ui.end->setValue(1);8485readHeader();86}8788void ReadEpFile::applyButtonClickedSlot()89{90QString fileName = ui.fileName->text().trimmed();9192if(fileName.isEmpty()) return;9394int start = ui.start->value();95int end = ui.end->value();96int maxSteps = ui.timestepsEdit->text().toInt();9798if(end > maxSteps) {99end = maxSteps;100ui.end->setValue(maxSteps);101}102103if(start > end) {104start = end;105ui.start->setValue(start);106}107108repaint();109110emit(readPostFileSignal(fileName));111}112113void ReadEpFile::cancelButtonClickedSlot()114{115close();116}117118void ReadEpFile::okButtonClickedSlot()119{120applyButtonClickedSlot();121cancelButtonClickedSlot();122}123124void ReadEpFile::readHeader()125{126QString fileName = ui.fileName->text().trimmed();127128QFile postFile(fileName);129130if(!postFile.open(QIODevice::ReadOnly | QIODevice::Text)) {131ui.fileName->setText("");132return;133}134135int nodes, elements, timesteps, components;136137if(ui.fileName->text().endsWith(".vtu", Qt::CaseInsensitive)){138139vtkXMLUnstructuredGridReader* reader = vtkXMLUnstructuredGridReader::New();140reader->SetFileName(ui.fileName->text().toLatin1().data());141reader->Update();142143nodes = reader->GetNumberOfPoints();144elements = reader->GetNumberOfCells();145components = 1;146timesteps = reader->GetNumberOfTimeSteps();147if(timesteps == 0) timesteps = 1;148components = 0;149vtkUnstructuredGrid *output = reader->GetOutput();150vtkPointData *pointData = output->GetPointData();151vtkCellData *cellData = output->GetCellData();152153for(int i = 0; i < reader->GetNumberOfPointArrays(); i++){154components += pointData->GetArray(reader->GetPointArrayName(i))->GetNumberOfComponents();155}156reader->Delete();157158159QFileInfo info(fileName);160QDir dir = info.dir();161QString name = info.fileName();162int l = name.length();163int i = 4;164while(name.at(l-i-1).isNumber() && i>0) i++;165QString filter = name.left(l-i) + "*.vtu";166cout << ".vtu file: " << filter.toLatin1().data() << endl;167168QStringList filterList;169filterList << "*.vtu";170vtuFileNameList = dir.entryList(filterList, QDir::Readable|QDir::Files|QDir::NoSymLinks, QDir::SortFlags(QDir::Name | QDir::IgnoreCase));171//for(int i=0; i < vtuFileNameList.length(); i++){172// cout << vtuFileNameList.at(i).toLatin1().data() << endl;173//}174vtuFileNameList = vtuFileNameList.mid(vtuFileNameList.indexOf(info.fileName()));175timesteps = vtuFileNameList.length();176177}else if(ui.fileName->text().endsWith(".ep", Qt::CaseInsensitive)){178179QTextStream post(&postFile);180181QTextStream txtStream;182QString tmpLine = post.readLine().trimmed();183while(tmpLine.isEmpty() || (tmpLine.at(0) == '#'))184tmpLine = post.readLine().trimmed();185txtStream.setString(&tmpLine);186187txtStream >> nodes >> elements >> components >> timesteps;188189postFile.close();190}191192ui.nodesEdit->setText(QString::number(nodes));193ui.elementsEdit->setText(QString::number(elements));194ui.timestepsEdit->setText(QString::number(timesteps));195ui.dofsEdit->setText(QString::number(components));196}197198void ReadEpFile::allButtonClickedSlot()199{200ui.start->setValue(1);201ui.end->setValue(ui.timestepsEdit->text().toInt());202203repaint();204}205206207