From 17efca67801e7ebe612dbb467e035a0927282535 Mon Sep 17 00:00:00 2001
From: Karsten Rink <karsten.rink@ufz.de>
Date: Thu, 8 May 2014 17:58:41 +0200
Subject: [PATCH] added tetgen export option for 3D meshes to gui

---
 Gui/DataView/MshView.cpp | 53 +++++++++++++++++++++++++++++++---------
 Gui/DataView/MshView.h   |  2 ++
 2 files changed, 43 insertions(+), 12 deletions(-)

diff --git a/Gui/DataView/MshView.cpp b/Gui/DataView/MshView.cpp
index 71862bafaab..cd9db9cfd9d 100644
--- a/Gui/DataView/MshView.cpp
+++ b/Gui/DataView/MshView.cpp
@@ -22,6 +22,7 @@
 #include <QSettings>
 
 #include "Mesh.h"
+#include "Node.h"
 #include "MeshLayerEditDialog.h"
 #include "MeshValueEditDialog.h"
 #include "MshItem.h"
@@ -39,6 +40,7 @@
 #include "XmlIO/Boost/BoostVtuInterface.h"
 #include "Writer.h" // necessary to avoid Linker Error in Windows
 #include "SHPInterface.h"
+#include "TetGenInterface.h"
 
 MshView::MshView( QWidget* parent /*= 0*/ )
 	: QTreeView(parent)
@@ -120,11 +122,15 @@ void MshView::contextMenuEvent( QContextMenuEvent* event )
 		QAction*    editMeshAction = menu.addAction("Edit mesh...");
 		QAction*  editValuesAction = menu.addAction("Edit material groups...");
 		QAction* meshQualityAction = menu.addAction("Calculate element quality...");
-		QAction* surfaceMeshAction (NULL);
+		QAction* surfaceMeshAction (nullptr);
+		QAction* tetgenExportAction (nullptr);
 		if (mesh_dim==3)
-			     surfaceMeshAction = menu.addAction("Extract surface");
-		QAction* mesh2geoAction (NULL);
-		QAction* shapeExportAction (NULL);
+		{
+			surfaceMeshAction = menu.addAction("Extract surface");
+			tetgenExportAction = menu.addAction("Export to TetGen...");
+		}
+		QAction* mesh2geoAction (nullptr);
+		QAction* shapeExportAction (nullptr);
 		if (mesh_dim==2)
 		{
 			mesh2geoAction = menu.addAction("Convert to geometry");
@@ -136,17 +142,20 @@ void MshView::contextMenuEvent( QContextMenuEvent* event )
 		QAction*   addDirectAction = direct_cond_menu.addAction("Add...");
 		QAction*  loadDirectAction = direct_cond_menu.addAction("Load...");
 		//menu.addSeparator();
-		connect(editMeshAction,        SIGNAL(triggered()), this, SLOT(openMeshEditDialog()));
-		connect(editValuesAction,      SIGNAL(triggered()), this, SLOT(openValuesEditDialog()));
-		connect(meshQualityAction,     SIGNAL(triggered()), this, SLOT(checkMeshQuality()));
+		connect(editMeshAction,         SIGNAL(triggered()), this, SLOT(openMeshEditDialog()));
+		connect(editValuesAction,       SIGNAL(triggered()), this, SLOT(openValuesEditDialog()));
+		connect(meshQualityAction,      SIGNAL(triggered()), this, SLOT(checkMeshQuality()));
 		if (mesh_dim==3)
-			connect(surfaceMeshAction, SIGNAL(triggered()), this, SLOT(extractSurfaceMesh()));
-		connect(addDirectAction,	   SIGNAL(triggered()), this, SLOT(addDIRECTSourceTerms()));
-		connect(loadDirectAction,      SIGNAL(triggered()), this, SLOT(loadDIRECTSourceTerms()));
+		{
+			connect(surfaceMeshAction,  SIGNAL(triggered()), this, SLOT(extractSurfaceMesh()));
+			connect(tetgenExportAction, SIGNAL(triggered()), this, SLOT(exportToTetGen()));
+		}
+		connect(addDirectAction,	    SIGNAL(triggered()), this, SLOT(addDIRECTSourceTerms()));
+		connect(loadDirectAction,       SIGNAL(triggered()), this, SLOT(loadDIRECTSourceTerms()));
 		if (mesh_dim==2)
 		{
-			connect(mesh2geoAction,    SIGNAL(triggered()), this, SLOT(convertMeshToGeometry()));
-			connect(shapeExportAction, SIGNAL(triggered()), this, SLOT(exportToShapefile()));
+			connect(mesh2geoAction,     SIGNAL(triggered()), this, SLOT(convertMeshToGeometry()));
+			connect(shapeExportAction,  SIGNAL(triggered()), this, SLOT(exportToShapefile()));
 		}
 		menu.exec(event->globalPos());
 	}
@@ -212,6 +221,26 @@ void MshView::exportToShapefile() const
 			OGSError::box("Error exporting mesh\n to shapefile");
 }
 
+void MshView::exportToTetGen()
+{
+	QModelIndex index = this->selectionModel()->currentIndex();
+
+	if (!index.isValid())
+		return;
+	
+	const MeshLib::Mesh* mesh = static_cast<MshModel*>(this->model())->getMesh(index);
+	QSettings settings;
+	QString filename = QFileDialog::getSaveFileName(this, "Write TetGen input file to",
+													settings.value("lastOpenedTetgenFileDirectory").toString(),
+													"TetGen Geometry (*.smesh)");
+	if (!filename.isEmpty())
+	{
+		FileIO::TetGenInterface tg;
+		std::vector<MeshLib::Node> attr;
+		tg.writeTetGenSmesh(filename.toStdString(), *mesh, attr);
+	}
+}
+
 int MshView::writeToFile() const
 {
 	QModelIndex index = this->selectionModel()->currentIndex();
diff --git a/Gui/DataView/MshView.h b/Gui/DataView/MshView.h
index 124037ab295..4ff9c3e700d 100644
--- a/Gui/DataView/MshView.h
+++ b/Gui/DataView/MshView.h
@@ -73,6 +73,8 @@ private slots:
 
 	void extractSurfaceMesh();
 
+	void exportToTetGen();
+
 	void loadDIRECTSourceTerms();
 
 	void convertMeshToGeometry();
-- 
GitLab