Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable FreeCAD to generate mesh in different formats #1

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion src/Mod/Fem/App/FemMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ FemMesh& FemMesh::operator=(const FemMesh& mesh)
void FemMesh::copyMeshData(const FemMesh& mesh)
{
_Mtrx = mesh._Mtrx;
_FileExt = mesh._FileExt;

// See file SMESH_I/SMESH_Gen_i.cxx in the git repo of smesh at
// https://git.salome-platform.org
Expand Down Expand Up @@ -1879,6 +1880,44 @@ void FemMesh::readZ88(const std::string& FileName)
Base::TimeElapsed::diffTimeF(Start, Base::TimeElapsed()));
}

void FemMesh::readMsh(const std::string& FileName)
{
Base::TimeElapsed Start;
Base::Console().Log("Start: FemMesh::readMsh() =================================\n");


Base::FileInfo File(FileName);
_FileExt = File.extension();

PyObject* module = PyImport_ImportModule("feminout.importMshMesh");
if (!module) {
return;
}
try {
Py::Module mshmod(module, true);
Py::Callable method(mshmod.getAttr("read"));
Py::Tuple args(1);
args.setItem(0, Py::String(FileName));
Py::Object mesh(method.apply(args));
if (PyObject_TypeCheck(mesh.ptr(), &FemMeshPy::Type)) {
FemMeshPy* fempy = static_cast<FemMeshPy*>(mesh.ptr());
FemMesh* fem = fempy->getFemMeshPtr();
*this = *fem; // the deep copy should be avoided, a pointer swap method could be
// implemented see
// https://forum.freecad.org/viewtopic.php?f=10&t=31999&start=10#p274241
}
else {
throw Base::FileException("Problems reading file");
}
}
catch (Py::Exception& e) {
e.clear();
}
Base::Console().Log(" %f: Done \n",
Base::TimeElapsed::diffTimeF(Start, Base::TimeElapsed()));
}


void FemMesh::read(const char* FileName)
{
Base::FileInfo File(FileName);
Expand Down Expand Up @@ -1924,9 +1963,15 @@ void FemMesh::read(const char* FileName)
// read Z88 mesh file
readZ88(File.filePath());
}
else if (File.hasExtension("msh")) {
// read .msh mesh file
readMsh(File.filePath());
}
else {
throw Base::FileException("Unknown extension");
}

_FileExt = File.extension();
}

void FemMesh::writeABAQUS(const std::string& Filename,
Expand Down Expand Up @@ -2562,10 +2607,20 @@ unsigned int FemMesh::getMemSize() const

void FemMesh::Save(Base::Writer& writer) const
{
std::string filename;

if (_FileExt.empty()) {
filename = "FemMesh.unv";
}
else {
filename = "FemMesh." + _FileExt;
}


if (!writer.isForceXML()) {
// See SaveDocFile(), RestoreDocFile()
writer.Stream() << writer.ind() << "<FemMesh file=\"";
writer.Stream() << writer.addFile("FemMesh.unv", this) << "\"";
writer.Stream() << writer.addFile(filename.c_str(), this) << "\"";
writer.Stream() << " a11=\"" << _Mtrx[0][0] << "\" a12=\"" << _Mtrx[0][1] << "\" a13=\""
<< _Mtrx[0][2] << "\" a14=\"" << _Mtrx[0][3] << "\"";
writer.Stream() << " a21=\"" << _Mtrx[1][0] << "\" a22=\"" << _Mtrx[1][1] << "\" a23=\""
Expand Down
2 changes: 2 additions & 0 deletions src/Mod/Fem/App/FemMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,13 @@ class FemExport FemMesh: public Data::ComplexGeoData
void readNastran95(const std::string& Filename);
void readZ88(const std::string& Filename);
void readAbaqus(const std::string& Filename);
void readMsh(const std::string& Filename);

private:
/// positioning matrix
Base::Matrix4D _Mtrx;
SMESH_Mesh* myMesh;
std::string _FileExt;

std::list<SMESH_HypothesisPtr> hypoth;
static SMESH_Gen* _mesh_gen;
Expand Down
3 changes: 3 additions & 0 deletions src/Mod/Fem/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ SET(FemInOut_SRCS
feminout/importCcxFrdResults.py
feminout/importFenicsMesh.py
feminout/importInpMesh.py
feminout/importMshMesh.py
feminout/importPyMesh.py
feminout/importToolsFem.py
feminout/importVTKResults.py
Expand Down Expand Up @@ -399,6 +400,8 @@ SET(FemTestsMesh_SRCS
femtest/data/mesh/tetra10_mesh.vtk
femtest/data/mesh/tetra10_mesh.yml
femtest/data/mesh/tetra10_mesh.z88
femtest/data/mesh/tetra10_mesh.msh
femtest/data/mesh/beam_mesh.msh
)

SET(FemTestsMystran_SRCS
Expand Down
51 changes: 51 additions & 0 deletions src/Mod/Fem/Gui/DlgSettingsFemGmsh.ui
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,52 @@
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="l_mesh_file_format">
<property name="text">
<string>Mesh format</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="Gui::PrefComboBox" name="cb_mesh_file_format">
<property name="minimumSize">
<size>
<width>148</width>
<height>0</height>
</size>
</property>
<property name="toolTip">
<string>Enter the mesh file format</string>
</property>
<property name="prefEntry" stdset="0">
<cstring>MeshFileFormat</cstring>
</property>
<property name="prefPath" stdset="0">
<cstring>Mod/Fem/Gmsh</cstring>
</property>
<item>
<property name="text">
<string>.unv</string>
</property>
</item>
<item>
<property name="text">
<string>.vtk</string>
</property>
</item>
<item>
<property name="text">
<string>.inp</string>
</property>
</item>
<item>
<property name="text">
<string>.msh</string>
</property>
</item>
</widget>
</item>
</layout>
</item>
</layout>
Expand Down Expand Up @@ -143,6 +189,11 @@
<extends>QCheckBox</extends>
<header>Gui/PrefWidgets.h</header>
</customwidget>
<customwidget>
<class>Gui::PrefComboBox</class>
<extends>QComboBox</extends>
<header>Gui/PrefWidgets.h</header>
</customwidget>
</customwidgets>
<resources>
<include location="Resources/Fem.qrc"/>
Expand Down
34 changes: 32 additions & 2 deletions src/Mod/Fem/Gui/DlgSettingsFemGmshImp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@
#ifndef _PreComp_
#include <QMessageBox>
#endif

#include <App/Application.h>
#include "DlgSettingsFemGmshImp.h"
#include "ui_DlgSettingsFemGmsh.h"

#include <App/Application.h>
#include <Base/Console.h>

using namespace FemGui;

Expand All @@ -49,14 +50,41 @@ DlgSettingsFemGmshImp::~DlgSettingsFemGmshImp() = default;

void DlgSettingsFemGmshImp::saveSettings()
{

ui->cb_gmsh_binary_std->onSave();
ui->fc_gmsh_binary_path->onSave();
ui->cb_mesh_file_format->onSave();

ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath(
"User parameter:BaseApp/Preferences/Mod/Fem/Gmsh");
std::string meshFormats[] = {".unv", ".vtk", ".inp", ".msh"};
int ind = ui->cb_mesh_file_format->currentIndex();
hGrp->SetASCII("MeshFileFormat", meshFormats[ind]);
}

void DlgSettingsFemGmshImp::loadSettings()
{
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath(
"User parameter:BaseApp/Preferences/Mod/Fem/Gmsh");
std::string str = hGrp->GetASCII("MeshFileFormat", ".unv");
int ind = 0;
if (str == ".unv") {
ind = 0;
}
else if (str == ".vtk") {
ind = 1;
}
else if (str == ".inp") {
ind = 2;
}
else if (str == ".msh") {
ind = 3;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be an error instead of silently using the default.


ui->cb_mesh_file_format->setCurrentIndex(ind);
ui->cb_gmsh_binary_std->onRestore();
ui->fc_gmsh_binary_path->onRestore();
ui->cb_mesh_file_format->onRestore();
}

/**
Expand All @@ -65,7 +93,9 @@ void DlgSettingsFemGmshImp::loadSettings()
void DlgSettingsFemGmshImp::changeEvent(QEvent* e)
{
if (e->type() == QEvent::LanguageChange) {
int c_index = ui->cb_mesh_file_format->currentIndex();
ui->retranslateUi(this);
ui->cb_mesh_file_format->setCurrentIndex(c_index);
}
else {
QWidget::changeEvent(e);
Expand Down
Loading