LAMPCAE/src/PluginMeshDataExchange/FEMdataExchange.cpp

170 lines
4.4 KiB
C++
Raw Normal View History

2023-05-08 06:32:41 +00:00
#include "FEMdataExchange.h"
2023-05-09 11:00:22 +00:00
#include "MainWindow/MainWindow.h"
#include "MeshData/meshKernal.h"
#include "MeshData/meshSet.h"
#include "MeshData/meshSingleton.h"
2023-05-08 06:32:41 +00:00
#include "MeshThreadBase.h"
2023-05-09 11:00:22 +00:00
#include <QDebug>
2023-05-08 06:32:41 +00:00
#include <QFile>
#include <QTextStream>
#include <string>
#include <vtkCellType.h>
2023-05-09 11:00:22 +00:00
#include <vtkSmartPointer.h>
#include <vtkUnstructuredGrid.h>
2023-05-08 06:32:41 +00:00
2023-05-09 11:00:22 +00:00
namespace MeshData {
FEMdataExchange::FEMdataExchange(const QString& filename, MeshOperation operation,
GUI::MainWindow* mw, int modelId)
: m_fileName(filename)
, m_operation(operation)
, m_meshData(MeshData::getInstance())
, MeshThreadBase(filename, operation, mw)
, m_modelId(modelId)
2023-05-08 06:32:41 +00:00
{
}
2023-05-09 11:00:22 +00:00
FEMdataExchange::~FEMdataExchange() {}
2023-05-08 06:32:41 +00:00
bool FEMdataExchange::read()
{
2023-05-09 11:00:22 +00:00
QFile file(m_fileName);
int a = m_fileName.lastIndexOf('/');
int b = m_fileName.lastIndexOf('.');
int nameSize = b - a - 1;
QString fileName = m_fileName.mid(a + 1, nameSize);
if(!file.exists())
return false;
2023-05-08 06:32:41 +00:00
2023-05-09 11:00:22 +00:00
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
return false;
2023-05-08 06:32:41 +00:00
QTextStream in(&file);
2023-05-09 11:00:22 +00:00
m_grid = vtkUnstructuredGrid::New();
2023-05-08 06:32:41 +00:00
vtkSmartPointer<vtkPoints> vtkPoint = vtkSmartPointer<vtkPoints>::New();
2023-05-09 11:00:22 +00:00
while(!in.atEnd()) {
QString line = in.readLine();
2023-05-08 06:32:41 +00:00
QStringList lineList = line.split(" ");
lineList.removeAll(QString(""));
2023-05-09 11:00:22 +00:00
if(lineList.isEmpty())
continue;
if(lineList[0] == "GRID") {
2023-05-08 06:32:41 +00:00
appendNode(lineList, vtkPoint);
2023-05-09 11:00:22 +00:00
} else if(lineList[0] == "CTRIA3") {
2023-05-08 06:32:41 +00:00
appendElement(lineList);
}
}
qDebug() << Num;
qDebug() << fileName;
m_grid->SetPoints(vtkPoint);
MeshKernal* k = new MeshKernal;
k->setName(fileName);
k->setPath(m_fileName);
k->setMeshData(m_grid);
m_meshData->appendMeshKernal(k);
return true;
}
bool FEMdataExchange::write()
{
return false;
}
void FEMdataExchange::run()
{
ModuleBase::ThreadTask::run();
bool result = false;
2023-05-09 11:00:22 +00:00
switch(m_operation) {
case MESH_READ:
emit showInformation(tr("Import FEM Mesh File From \"%1\"").arg(m_fileName));
result = read();
setReadResult(result);
break;
case MESH_WRITE:
emit showInformation(tr("Export FEM Mesh File From \"%1\"").arg(m_fileName));
result = write();
setWriteResult(result);
break;
2023-05-08 06:32:41 +00:00
}
defaultMeshFinished();
}
void FEMdataExchange::appendNode(QStringList lineList, vtkPoints* vtkPoint)
{
2023-05-09 11:00:22 +00:00
int lineSize = lineList.size();
// qDebug() << lineList;
2023-05-08 06:32:41 +00:00
double point[3];
2023-05-09 11:00:22 +00:00
switch(lineSize) {
case 3: {
2023-05-08 06:32:41 +00:00
point[0] = lineList[2].mid(0, 8).toDouble();
2023-05-09 11:00:22 +00:00
point[1] = lineList[2].mid(8, 8).toDouble();
point[2] = lineList[2].mid(16).toDouble();
break;
2023-05-08 06:32:41 +00:00
}
2023-05-09 11:00:22 +00:00
case 4: {
if(lineList[2].size() > 8) {
point[0] = lineList[2].mid(0, 8).toDouble();
point[1] = lineList[2].mid(8).toDouble();
point[2] = lineList[3].toDouble();
} else {
point[0] = lineList[2].toDouble();
point[1] = lineList[3].mid(0, 8).toDouble();
point[2] = lineList[3].mid(8).toDouble();
}
break;
}
case 5: {
2023-05-08 06:32:41 +00:00
point[0] = lineList[2].toDouble();
2023-05-09 11:00:22 +00:00
point[1] = lineList[3].toDouble();
point[2] = lineList[4].toDouble();
break;
2023-05-08 06:32:41 +00:00
}
2023-05-09 11:00:22 +00:00
default:
return;
2023-05-08 06:32:41 +00:00
}
2023-05-09 11:00:22 +00:00
// qDebug() << pointId << " " << point[0] << " " << point[1] << " " << point[2];
2023-05-08 06:32:41 +00:00
vtkPoint->InsertNextPoint(point);
2023-05-09 11:00:22 +00:00
m_pointHash.insert(lineList[1].toInt(), m_pointId);
2023-05-08 06:32:41 +00:00
m_pointId++;
}
void FEMdataExchange::appendElement(QStringList lineList)
{
2023-05-09 11:00:22 +00:00
if(lineList.size() < 6)
return;
2023-05-08 06:32:41 +00:00
2023-05-09 11:00:22 +00:00
VTKCellType cellType = VTK_EMPTY_CELL;
vtkSmartPointer<vtkIdList> IdList = vtkSmartPointer<vtkIdList>::New();
2023-05-08 06:32:41 +00:00
elementTypeSelect(lineList[0], cellType);
2023-05-09 11:00:22 +00:00
if(cellType == VTK_EMPTY_CELL)
return;
for(int i = 3; i < lineList.size(); i++) {
2023-05-08 06:32:41 +00:00
int p = m_pointHash.value(lineList[i].toInt());
IdList->InsertNextId(p);
}
2023-05-09 11:00:22 +00:00
m_grid->InsertNextCell(cellType, IdList);
2023-05-08 06:32:41 +00:00
}
2023-05-09 11:00:22 +00:00
void FEMdataExchange::elementTypeSelect(QString elementName, VTKCellType& type)
2023-05-08 06:32:41 +00:00
{
2023-05-09 11:00:22 +00:00
if(elementName == "CTRIA3") {
2023-05-08 06:32:41 +00:00
type = VTK_TRIANGLE;
2023-05-09 11:00:22 +00:00
} else if(elementName == "CTRIA6") {
2023-05-08 06:32:41 +00:00
type = VTK_QUADRATIC_TRIANGLE;
2023-05-09 11:00:22 +00:00
} else if(elementName == "CQUAD4") {
2023-05-08 06:32:41 +00:00
type = VTK_QUAD;
2023-05-09 11:00:22 +00:00
} else if(elementName == "CQUAD8") {
2023-05-08 06:32:41 +00:00
type = VTK_QUADRATIC_QUAD;
2023-05-09 11:00:22 +00:00
} else if(elementName == "CTETRA") {
2023-05-08 06:32:41 +00:00
type = VTK_TETRA;
2023-05-09 11:00:22 +00:00
} else if(elementName == "c") {
2023-05-08 06:32:41 +00:00
type = VTK_QUADRATIC_TETRA;
2023-05-09 11:00:22 +00:00
} else if(elementName == "CHEXA") {
2023-05-08 06:32:41 +00:00
type = VTK_HEXAHEDRON;
2023-05-09 11:00:22 +00:00
} else if(elementName == "cc") {
2023-05-08 06:32:41 +00:00
type = VTK_QUADRATIC_HEXAHEDRON;
}
}
2023-05-09 11:00:22 +00:00
} // namespace MeshData