1 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 // + This file is part of enGrid. +
5 // + Copyright 2008-2014 enGits GmbH +
7 // + enGrid is free software: you can redistribute it and/or modify +
8 // + it under the terms of the GNU General Public License as published by +
9 // + the Free Software Foundation, either version 3 of the License, or +
10 // + (at your option) any later version. +
12 // + enGrid is distributed in the hope that it will be useful, +
13 // + but WITHOUT ANY WARRANTY; without even the implied warranty of +
14 // + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +
15 // + GNU General Public License for more details. +
17 // + You should have received a copy of the GNU General Public License +
18 // + along with enGrid. If not, see <http://www.gnu.org/licenses/>. +
20 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24 #define VTK_USE_RENDERING
27 #if defined(LIBENGRID_EXPORTS) || defined(libengrid_EXPORTS)
28 #define LIBENGRID_DLL __declspec(dllexport)
30 #define LIBENGRID_DLL __declspec(dllimport)
32 #define CLASS_LIBENGRID_DLL LIBENGRID_DLL
35 #define CLASS_LIBENGRID_DLL
40 #include <QMainWindow>
44 #include <QFileSystemWatcher>
47 #include <QDockWidget>
48 #include <QDomDocument>
49 #include <QProgressBar>
51 #include <vtkUnstructuredGrid.h>
53 #include <vtkPolyDataMapper.h>
54 #include <vtkGeometryFilter.h>
55 #include <vtkCubeAxesActor2D.h>
56 #include <vtkCellPicker.h>
57 #include <vtkPointPicker.h>
58 #include <vtkSphereSource.h>
59 #include <vtkTextActor.h>
60 #include <vtkVectorText.h>
61 #include <vtkFollower.h>
62 #include <vtkScalarBarActor.h>
63 #include <vtkLookupTable.h>
64 #include <vtkDataSetSurfaceFilter.h>
66 #include "ui_guimainwindow.h"
67 #include "vtkEgBoundaryCodesFilter.h"
68 #include "vtkEgExtractVolumeCells.h"
69 #include "egvtkobject.h"
70 #include "boundarycondition.h"
71 #include "volumedefinition.h"
72 #include "physicalboundarycondition.h"
73 #include "checksurfaceintegrity.h"
75 #include "cadinterface.h"
77 #include "openfoamcase.h"
78 #include "guitransform.h"
79 #include "openfoamtools.h"
80 #include "std_includes.h"
81 #include "fixcadgeometry.h"
82 #include "xmlhandler.h"
85 * This is the main GUI class of enGrid.
87 class CLASS_LIBENGRID_DLL GuiMainWindow
: public QMainWindow
, public EgVtkObject
92 private: // attributes
94 XmlHandler
* m_XmlHandler
;
96 Ui::GuiMainWindow ui
; ///< The user interface definition -- created by QtDesigner.
97 vtkUnstructuredGrid
*m_Grid
; ///< The current state of the grid that is being generated.
99 vtkRenderer
*m_Renderer
; ///< The VTK renderer object, used for visualising the grid
101 vtkActor
* m_SurfaceActor
;
102 vtkActor
* m_SurfaceWireActor
;
103 vtkActor
* m_TetraActor
;
104 vtkActor
* m_WedgeActor
;
105 vtkActor
* m_PyramidActor
;
106 vtkActor
* m_HexaActor
;
107 vtkActor
* m_PolyhedraActor
;
108 vtkActor
* m_VolumeWireActor
;
110 vtkProperty
* m_BackfaceProperty
;
111 vtkLookupTable
* m_LookupTable
;
112 vtkScalarBarActor
* m_LegendActor
;
114 vtkPolyDataMapper
* m_SurfaceMapper
;
115 vtkPolyDataMapper
* m_SurfaceWireMapper
;
116 vtkPolyDataMapper
* m_TetraMapper
;
117 vtkPolyDataMapper
* m_PyramidMapper
;
118 vtkPolyDataMapper
* m_WedgeMapper
;
119 vtkPolyDataMapper
* m_HexaMapper
;
120 vtkPolyDataMapper
* m_PolyhedraMapper
;
121 vtkPolyDataMapper
* m_VolumeWireMapper
;
123 double m_ColTetraR
, m_ColTetraG
, m_ColTetraB
;
124 double m_ColPyraR
, m_ColPyraG
, m_ColPyraB
;
125 double m_ColPrismR
, m_ColPrismG
, m_ColPrismB
;
126 double m_ColHexR
, m_ColHexG
, m_ColHexB
;
127 double m_ColPolyR
, m_ColPolyG
, m_ColPolyB
;
128 double m_ColAR
, m_ColAG
, m_ColAB
;
129 double m_ColBR
, m_ColBG
, m_ColBB
;
131 vtkEgExtractVolumeCells
*m_ExtrVol
;
132 vtkEgExtractVolumeCells
*m_ExtrTetras
;
133 vtkEgExtractVolumeCells
*m_ExtrPyramids
;
134 vtkEgExtractVolumeCells
*m_ExtrWedges
;
135 vtkEgExtractVolumeCells
*m_ExtrHexes
;
136 vtkEgExtractVolumeCells
*m_ExtrPolyhedra
;
138 vtkDataSetSurfaceFilter
*m_VolumeGeometry
;
139 vtkDataSetSurfaceFilter
*m_TetraGeometry
;
140 vtkDataSetSurfaceFilter
*m_PyramidGeometry
;
141 vtkDataSetSurfaceFilter
*m_WedgeGeometry
;
142 vtkDataSetSurfaceFilter
*m_HexaGeometry
;
143 vtkDataSetSurfaceFilter
*m_PolyhedraGeometry
;
145 vtkIdType m_PickedPoint
; ///< Picked point
146 vtkIdType m_PickedCell
; ///< Picked cell
147 bool m_UseVTKInteractor
; ///< Boolean value specifying whether the VTK Interactor should be used or not
149 static QMutex m_Mutex
;
151 vtkDataSetSurfaceFilter
* m_SurfaceFilter
; ///< VTK filter to extract the surface of the current grid.
152 double m_ReferenceSize
; ///< Size to use for picker objects and annotations
154 vector
<vtkTextActor
*> m_NodeText
; ///< 2D Text actor to display node IDs
155 vector
<vtkTextActor
*> m_CellText
; ///< 2D Text actor to display cell IDs
156 vector
<vtkVectorText
*> m_NodeTextVectorText
; ///< 3D Text actor to display node IDs
157 vector
<vtkPolyDataMapper
*> m_NodeTextPolyDataMapper
;
158 vector
<vtkFollower
*> m_NodeTextFollower
;
159 vector
<vtkVectorText
*> m_CellTextVectorText
; ///< 3D Text actor to display cell IDs
160 vector
<vtkPolyDataMapper
*> m_CellTextPolyDataMapper
;
161 vector
<vtkFollower
*> m_CellTextFollower
;
163 vtkPolyDataMapper
* m_PickMapper
; ///< VTK mapper to map pick marker
164 vtkActor
* m_PickActor
; ///< VTK actor to display pick marker
165 vtkSphereSource
* m_PickSphere
; ///< sphere to mark picked cell/points
166 vtkCubeAxesActor2D
* m_Axes
; ///< VTK actor to display the coordinate system
167 vtkEgBoundaryCodesFilter
* m_BCodesFilter
; ///< VTK filter to extract boundary elements with certain codes
168 vtkCellPicker
* m_CellPicker
; ///< VTK CellPicker to pick cells for various user interactions
169 vtkPointPicker
* m_PointPicker
; ///< VTK PointPicker to pick points for various user interactions
170 int m_PickedObject
; ///< 0=none, 1=node, 2=cell
172 QString m_CurrentFilename
; ///< The current file name of the grid.
173 int m_CurrentOperation
; ///< The current operation number. (used for undo/redo)
174 bool m_undo_redo_enabled
; ///< if true, undo/redo operations will be usable.
175 int m_LastOperation
; ///< The last operation number. (used for undo/redo)
176 QLabel
* m_StatusLabel
; ///< Label for the information in the status bar
177 QLabel
* m_StatusInfoLabel
;
178 QProgressBar
* m_StatusProgressBar
;
179 QSet
<int> m_DisplayBoundaryCodes
; ///< A QList with all active boundary codes.
180 QSet
<int> m_AllBoundaryCodes
; ///< A QList with all boundary codes.
181 bool m_Busy
; ///< flag to indicate that enGrid is busy with an operation
183 stringbuf m_CoutBuffer
;
184 streambuf
*m_OriginalCoutBuffer
;
186 QTimer m_GarbageTimer
;
190 QMap
<int, BoundaryCondition
> m_bcmap
; ///< mapping between numerical and symbolic boundary codes
191 QMap
<QString
, VolumeDefinition
> m_VolMap
; ///< all volume definitions
192 QMap
<QString
, PhysicalBoundaryCondition
> m_PhysicalBoundaryConditionsMap
; ///< all physical boundary conditions definitions
194 QMap
<int, CadInterface
*> m_CadInterfaces
; ///< all CAD interfaces
195 CadInterface
* m_UniCadInterface
; ///< universal CAD interface for all boundary conditions
197 QMap
<QAction
*, Operation
*> m_PluginOperations
;
198 QAction
* m_EscAction
;
200 int m_SolverIndex
;// deprecated
201 OpenFOAMTools m_OpenFoamTools
;
203 // recent file list support
205 QMap
<QString
,QDateTime
> m_RecentFiles
;
206 QMenu
* recentFileMenu() { return ui
.menuOpen_recent
; }
207 void readRecentFiles();
208 void writeRecentFiles();
209 void addRecentFile(QString file_name
, QDateTime date
);
211 void openRecent(QAction
*action
);
216 private: // static attributes
219 * Platform independant access to application settings.
220 * For a UNIX system the user preferences will be stored in the file
221 * folder ".config/enGits/enGrid.conf" in the user's home directory;
222 * on Windows preferences will be stored in the registry.
224 static QSettings m_qset
;
227 * The current working directory of enGrid
229 static QString m_cwd
;
232 * Is the current case unsaved?
234 static bool m_UnSaved
;
236 /** a static this pointer (somewhat ugly, but there is only one MainWindow) */
237 static GuiMainWindow
* THIS
;
242 static void pickCallBack( vtkObject
*caller
, unsigned long int eid
, void *clientdata
, void *calldata
);
243 void updateSurfaceActors( bool forced
);
244 void updateVolumeActors( bool forced
);
248 void setClipX( const QString
&txt
);
249 void setClipY( const QString
&txt
);
250 void setClipZ( const QString
&txt
);
251 void setClipNX( const QString
&txt
);
252 void setClipNY( const QString
&txt
);
253 void setClipNZ( const QString
&txt
);
258 void openPhysicalBoundaryConditions();
259 void savePhysicalBoundaryConditions();
261 void openGrid(QString file_name
);
267 GuiMainWindow();///< Default constructor.
268 GuiMainWindow(QString file_name
);///< Constructor which opens a file directly.
272 * This function connects the menu and toolbar actions and
273 * the VTK basics(i.e. renderer, actor, ...) will be set up.
274 * Furthermore preferences will be read from qset.
276 void setupGuiMainWindow();
281 * Preferences will be written back.
283 virtual ~GuiMainWindow();
286 * Get the VTK render window
287 * @return the VTK render window
289 vtkRenderWindow
* getRenderWindow();
292 * Get the VTK renderer
293 * @return the VTK renderer
295 vtkRenderer
* getRenderer();
298 * Get the Qt-VTK interactor
299 * @return the Qt-VTK interactor
301 QVTKInteractor
* getInteractor();
304 * Get a pointer to the current grid object
305 * @return a pointer to the current vtkUnstructuredGrid object
307 vtkUnstructuredGrid
* getGrid() { return m_Grid
; }
309 void setBusy() { m_Busy
= true; updateStatusBar(); }
310 void setIdle() { m_Busy
= false; updateStatusBar(); }
312 /// Returns the path to the currently loaded file
313 QString
getFilePath();
315 /// Returns the index of the solver to use. The index corresponds to the position in solvers.txt .
316 void setSolverIndex(int x
) {m_SolverIndex
= x
;}
317 int getSolverIndex() {return m_SolverIndex
;}
319 public: // static methods
322 * Get the current working directory.
323 * @return the current working directory
325 static QString
getCwd();
328 * Set the current working directory
329 * @param dir the current working directory
331 static void setCwd( QString dir
);
335 * @param unsaved Do you want to be asked where to save when clicking on save next time?
337 static void setUnsaved( bool unsaved
);
340 * Get the currently picked cell.
341 * @return the picked cell ID or -1 if no cell has been picked
343 vtkIdType
getPickedCell();
346 * Get the currently picked point.
347 * @return the picked point ID or -1 if no point has been picked
349 vtkIdType
getPickedPoint();
351 vtkIdType
getPickedObject() { return m_PickedObject
; }
354 * Access to the QSettings object
356 static QSettings
* settings() { return &m_qset
; }
358 BoundaryCondition
getBC( int bc
) { return m_bcmap
[bc
]; }
359 VolumeDefinition
getVol( QString volname
) { return m_VolMap
[volname
]; }
360 void clearBCs() { m_bcmap
.clear(); }
361 void addBC(int bc
, BoundaryCondition BC
) { m_bcmap
[bc
] = BC
; }
363 QList
<VolumeDefinition
> getAllVols();
364 void setAllVols( QList
<VolumeDefinition
> vols
);
365 void createDefaultVol();
367 QList
<PhysicalBoundaryCondition
> getAllPhysicalBoundaryConditions();
368 void setAllPhysicalBoundaryConditions (QList
<PhysicalBoundaryCondition
> physical_boundary_conditions
);
369 void setAllPhysicalBoundaryConditions (QMap
<QString
, PhysicalBoundaryCondition
> physical_boundary_conditions
);
370 bool physicalTypeDefined(QString name
) { return m_PhysicalBoundaryConditionsMap
.contains(name
); }
371 PhysicalBoundaryCondition
getPhysicalBoundaryCondition(QString name
) { return m_PhysicalBoundaryConditionsMap
[name
]; }
373 static GuiMainWindow
* pointer() { return THIS
; }
375 static void unlock();
376 static bool tryLock();
377 void getAllBoundaryCodes(QVector
<int> &bcs
);
378 QSet
<int> getAllBoundaryCodes();
379 void getDisplayBoundaryCodes(QSet
<int> &bcs
);
380 vtkPointPicker
* getPointPicker() { return ( m_PointPicker
);}
381 vtkSphereSource
* getPickSphere() { return ( m_PickSphere
);}
382 bool pickPoint( vtkIdType id_point
);
383 bool pickCell( vtkIdType id_cell
);
385 QString
getFilename() { return( m_CurrentFilename
); }
386 void setFilename(QString filename
) { m_CurrentFilename
= filename
; }
388 CadInterface
* getCadInterface(int bc
, bool allow_null
= false);
389 void setCadInterface(CadInterface
* cad_interface
, int bc
) { m_CadInterfaces
[bc
] = cad_interface
; }
390 void setUniversalCadInterface(CadInterface
* cad_interface
);
391 bool checkCadInterfaces();
393 void resetProgress(QString info_text
, int p_max
);
394 void setProgress(int p
);
398 void setUseVTKInteractor( int a_UseVTKInteractor
);
399 void setPickMode( bool a_UseVTKInteractor
, bool a_CellPickerMode
);
401 void exit(); ///< Exit the application
402 void importSTL(); ///< Import an STL file (ASCII or binary)
403 void importGmsh1Ascii(); ///< Import a Gmsh grid from an ASCII file -- using version 1.0 of the Gmsh file format
404 void exportGmsh1Ascii(); ///< Export a grid from to an ASCII Gmsh file -- using version 1.0 of the Gmsh file format
405 void importGmsh2Ascii(); ///< Import a Gmsh grid from an ASCII file -- using version 2.0 of the Gmsh file format
406 void exportGmsh2Ascii(); ///< Export a grid from to an ASCII Gmsh file -- using version 2.0 of the Gmsh file format
407 void exportNeutral(); ///< Export a grid to neutral format for NETGEN
408 void updateActors( bool force
= false ); ///< Update the VTK output
409 void forceUpdateActors(); ///< Force an update of the VTK output
410 void scaleToData(); ///< Scale to data
411 void zoomAll(); ///< Move the camera in order to show everything on the screen
412 void zoomOnPickedObject();
414 void printGrid() {cout
<< "PrintGrid() called!" << endl
; cout_grid( cout
, m_Grid
, true, true, true, true );}
420 void resetOperationCounter();
422 void open(); ///< Open an existing case
423 void open( QString file_name
, bool update_current_filename
= true ); ///< Open case file_name
424 void save(); ///< Save the current case
425 void saveAs(); ///< Save the current case -- using a different file name
426 QString
saveAs( QString file_name
, bool update_current_filename
= true ); ///< Save the current case as file_name. Returns name under which file was saved (with missing .egc extension for example).
428 int quickSave(); ///< Save the current grid as a_filename_a_operation
429 void quickLoad( int a_operation
); ///< Load a_filename_a_operation
430 void updateStatusBar(); ///< Update the status bar
431 void selectBoundaryCodes(); ///< Select the boundary codes to be displayed/hidden
432 void updateBoundaryCodes( bool all_on
); ///< Update the boundary code book keeping (e.g. after reading a mesh).
433 void normalExtrusion(); ///< Normal extrusion of boundary elements (no validity check).
434 void setAxesVisibility(); ///< Toggle the visibility of the axes annotation.
435 void setViewingMode(); ///< Toggle orthogonal viewing mode.
436 void viewNodeIDs(); ///< Toggle node ID viewing mode.
437 void viewCellIDs(); ///< Toggle cell ID viewing mode.
438 void changeSurfaceOrientation(); ///< Change the orientation of all surface elements
439 void checkSurfaceOrientation(); ///< Check and, if required, change the orientation of all surface elements
440 void improveAspectRatio(); ///< Eliminate edges in order to improve the aspect ratio of the cells
441 void exportAsciiStl(); ///< Write surface elements to an ASCII STL file.
442 void exportBinaryStl(); ///< Write surface elements to a binary STL file.
443 void exportAsciiPly(); ///< Write surface elements to an ASCII PLY file.
444 void exportBinaryPly(); ///< Write surface elements to a binary PLY file.
445 void editBoundaryConditions(); ///< Edit boundary conditions (names and types)
446 void configure(); ///< Edit settings
447 void about(); ///< Display an about message
448 void markOutputLine(); ///< Mark the current position in the output window
450 QString
getXmlSection( QString name
); ///< Get a section from the XML case description
451 void setXmlSection( QString name
, QString contents
); ///< Set a section of the XML case description
460 void appendOutput( QString txt
) { ui
.textEditOutput
->append( txt
); }
461 void clearOutput() { ui
.textEditOutput
->clear(); }
463 void periodicUpdate();
465 void storeCadInterfaces(bool nosave
= false);
466 void resetCadInterfaces();
468 void logOn() { m_CoutBuffer
.str(""); m_Log
= true; }
469 void logOff() { m_Log
= false; }
471 // SLOTS for all standard operations should be defined below;
472 // entries should look like this:
473 // void callOperationName() { EG_STDSLOT(OperationName); };
474 // The actual class name in this case, however, would be GuiOperationName.
476 // the following line can be used as a template:
477 // void call() { EG_STDSLOT(); };
478 // IMPORTANT: Using EG_STDSLOT sets lock_gui to true, while EG_STDINTERSLOT does not (default is lock_gui = false)
479 // This is important to determine whether an operation should try to lock the main mutex or not.
480 // If lock_gui is true, the operation will try to lock the main mutex. If it fails (mutex locked by other operation), the operation is stopped.
482 // EG_STDSLOT = background operation (There can not be more than one background operation!)
483 // EG_STDINTERSLOT = foreground operation
484 // Note: In practice, EG_STDINTERSLOT locks everything, while EG_STDSLOT prevents other operations, but doesn't lock the text output or prevent minimizing the window.
486 void callCreateSurfaceMesh() { EG_STDINTERSLOT( GuiCreateSurfaceMesh
); }
487 void callCreateBoundaryLayer() { EG_STDSLOT( GuiCreateBoundaryLayer
); }
488 void callDivideBoundaryLayer() { EG_STDSLOT( GuiDivideBoundaryLayer
); }
489 void callDeleteVolumeGrid() { EG_STDSLOT( DeleteVolumeGrid
); }
490 void callDeleteTetras() { EG_STDSLOT( DeleteTetras
); }
491 void callCreateVolumeMesh() { EG_STDSLOT( GuiCreateVolumeMesh
); }
492 void callSmoothVolumeGrid() { EG_STDSLOT( SmoothVolumeGrid
); }
493 void callSetBoundaryCode() { EG_STDINTERSLOT( GuiSetBoundaryCode
); }
494 void callDeleteBadAspectTris() { EG_STDINTERSLOT( GuiDeleteBadAspectTris
); }
495 void callDeletePickedCell() { EG_STDSLOT( DeletePickedCell
); }
496 void callMergeNodes();
497 void callInsertNewCell();
498 void callDeletePickedPoint();
499 void callBoxSelect() { EG_STDINTERSLOT( BoxSelect
); }
500 void callCheckSurfaceIntegrity() { EG_STDINTERSLOT( CheckSurfaceIntegrity
); }
501 void callPick_cell_point() { EG_STDINTERSLOT( GuiPick
); }
502 void callTransform() { EG_STDINTERSLOT( GuiTransform
); }
503 void callUpdateSurfProj() { EG_STDINTERSLOT( UpdateSurfProj
); }
504 void callImportOpenFoamCase() { EG_STDREADERSLOT(FoamReader
); }
505 void callMergeVolumes() { EG_STDSLOT(GuiMergeVolumes
); }
506 void callMirrorMesh() { EG_STDSLOT(GuiMirrorMesh
); }
507 void callCreateHexCore() { EG_STDSLOT( GuiCreateHexCore
); }
508 void callCreateHexIbMesh() { EG_STDSLOT( GuiCreateHexIbMesh
); }
509 void callFillPlane() { EG_STDSLOT( GuiFillPlane
); }
510 void callConvertToPolyMesh() { EG_STDSLOT(GuiConvertToPolyMesh
); }
511 void callCreateHexShellMesh() { EG_STDSLOT(GuiCreateHexShell
); }
512 void callRestrictToAvailableVolumeCells() { EG_STDSLOT(RestrictToAvailableVolumeCells
); }
516 void callFoamWriter() { EG_STDINTERSLOT( FoamWriter
); }
517 void callSimpleFoamWriter() { EG_STDINTERSLOT( SimpleFoamWriter
); }
518 void callFoamCaseWriter() { EG_STDINTERSLOT( OpenFOAMcase
); }
519 void callCgnsWriter() { EG_STDINTERSLOT( CgnsWriter
); }
520 void callVtkReader() { EG_STDREADERSLOT( VtkReader
); }
521 void callBlenderReader() { EG_STDREADERSLOT( BlenderReader
); }
522 void callBlenderWriter() { EG_STDREADERSLOT( BlenderWriter
); }
523 void callPolyDataReader() { EG_STDREADERSLOT( PolyDataReader
); }
524 void callReducedPolyDataReader() { EG_STDREADERSLOT( ReducedPolyDataReader
); }
525 void callSeligAirfoilReader() { EG_STDREADERSLOT( SeligAirfoilReader
); }
526 void callMultiSolidAsciiStlReader() { EG_STDREADERSLOT( MultiSolidAsciiStlReader
); }
528 #ifdef BRLCAD_SUPPORT
529 void callBrlcadReader() { EG_STDREADERSLOT( BrlcadReader
); }
532 void callExportSu2() { EG_STDREADERSLOT( Su2Writer
); }
533 void callExportDolfyn() { EG_STDREADERSLOT( DolfynWriter
); }
536 void callExportTau() { EG_STDREADERSLOT( TauWriter
); }
539 void callExportDrNum() { EG_STDREADERSLOT( DrNumWriter
); }
541 void callSurfaceMesher() { EG_STDSLOT(GuiSurfaceMesher
); }
542 void callReduceSurfaceTriangulation() { EG_STDSLOT(ReduceSurfaceTriangulation
); }
543 void callEliminateSmallBranches() { EG_STDSLOT(EliminateSmallBranches
); }
544 void callSmoothAndSwapSurface() { EG_STDSLOT(SmoothAndSwapSurface
); }
545 void callCheckForOverlap() { EG_STDSLOT(CheckForOverlap
); }
547 void callFixCADGeometry() { EG_STDSLOT(FixCadGeometry
); }