2 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 // + This file is part of enGrid. +
6 // + Copyright 2008-2012 enGits GmbH +
8 // + enGrid is free software: you can redistribute it and/or modify +
9 // + it under the terms of the GNU General Public License as published by +
10 // + the Free Software Foundation, either version 3 of the License, or +
11 // + (at your option) any later version. +
13 // + enGrid is distributed in the hope that it will be useful, +
14 // + but WITHOUT ANY WARRANTY; without even the implied warranty of +
15 // + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +
16 // + GNU General Public License for more details. +
18 // + You should have received a copy of the GNU General Public License +
19 // + along with enGrid. If not, see <http://www.gnu.org/licenses/>. +
21 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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>
65 #include "ui_guimainwindow.h"
66 #include "vtkEgBoundaryCodesFilter.h"
67 #include "vtkEgExtractVolumeCells.h"
68 #include "egvtkobject.h"
69 #include "boundarycondition.h"
70 #include "volumedefinition.h"
71 #include "physicalboundarycondition.h"
72 #include "checksurfaceintegrity.h"
73 #include "surfaceprojection.h"
74 #include "openfoamcase.h"
75 #include "guitransform.h"
76 #include "openfoamtools.h"
77 #include "std_includes.h"
78 #include "fixcadgeometry.h"
79 #include "xmlhandler.h"
82 * This is the main GUI class of enGrid.
84 class CLASS_LIBENGRID_DLL GuiMainWindow
: public QMainWindow
, public EgVtkObject
89 private: // attributes
91 XmlHandler
* m_XmlHandler
;
93 Ui::GuiMainWindow ui
; ///< The user interface definition -- created by QtDesigner.
94 vtkUnstructuredGrid
*m_Grid
; ///< The current state of the grid that is being generated.
96 vtkRenderer
*m_Renderer
; ///< The VTK renderer object, used for visualising the grid
98 vtkActor
* m_SurfaceActor
;
99 vtkActor
* m_SurfaceWireActor
;
100 vtkActor
* m_TetraActor
;
101 vtkActor
* m_WedgeActor
;
102 vtkActor
* m_PyramidActor
;
103 vtkActor
* m_HexaActor
;
104 vtkActor
* m_PolyhedraActor
;
105 vtkActor
* m_VolumeWireActor
;
107 vtkProperty
* m_BackfaceProperty
;
108 vtkLookupTable
* m_LookupTable
;
109 vtkScalarBarActor
* m_LegendActor
;
111 vtkPolyDataMapper
* m_SurfaceMapper
;
112 vtkPolyDataMapper
* m_SurfaceWireMapper
;
113 vtkPolyDataMapper
* m_TetraMapper
;
114 vtkPolyDataMapper
* m_PyramidMapper
;
115 vtkPolyDataMapper
* m_WedgeMapper
;
116 vtkPolyDataMapper
* m_HexaMapper
;
117 vtkPolyDataMapper
* m_PolyhedraMapper
;
118 vtkPolyDataMapper
* m_VolumeWireMapper
;
120 double m_ColTetraR
, m_ColTetraG
, m_ColTetraB
;
121 double m_ColPyraR
, m_ColPyraG
, m_ColPyraB
;
122 double m_ColPrismR
, m_ColPrismG
, m_ColPrismB
;
123 double m_ColHexR
, m_ColHexG
, m_ColHexB
;
124 double m_ColAR
, m_ColAG
, m_ColAB
;
125 double m_ColBR
, m_ColBG
, m_ColBB
;
127 vtkEgExtractVolumeCells
*m_ExtrVol
;
128 vtkEgExtractVolumeCells
*m_ExtrTetras
;
129 vtkEgExtractVolumeCells
*m_ExtrPyramids
;
130 vtkEgExtractVolumeCells
*m_ExtrWedges
;
131 vtkEgExtractVolumeCells
*m_ExtrHexes
;
132 vtkEgExtractVolumeCells
*m_ExtrPolyhedra
;
134 vtkGeometryFilter
*m_VolumeGeometry
;
135 vtkGeometryFilter
*m_TetraGeometry
;
136 vtkGeometryFilter
*m_PyramidGeometry
;
137 vtkGeometryFilter
*m_WedgeGeometry
;
138 vtkGeometryFilter
*m_HexaGeometry
;
139 vtkGeometryFilter
*m_PolyhedraGeometry
;
141 vtkIdType m_PickedPoint
; ///< Picked point
142 vtkIdType m_PickedCell
; ///< Picked cell
143 bool m_UseVTKInteractor
; ///< Boolean value specifying whether the VTK Interactor should be used or not
145 static QMutex m_Mutex
;
147 vtkGeometryFilter
* m_SurfaceFilter
; ///< VTK filter to extract the surface of the current grid.
148 double m_ReferenceSize
; ///< Size to use for picker objects and annotations
150 vector
<vtkTextActor
*> m_NodeText
; ///< 2D Text actor to display node IDs
151 vector
<vtkTextActor
*> m_CellText
; ///< 2D Text actor to display cell IDs
152 vector
<vtkVectorText
*> m_NodeTextVectorText
; ///< 3D Text actor to display node IDs
153 vector
<vtkPolyDataMapper
*> m_NodeTextPolyDataMapper
;
154 vector
<vtkFollower
*> m_NodeTextFollower
;
155 vector
<vtkVectorText
*> m_CellTextVectorText
; ///< 3D Text actor to display cell IDs
156 vector
<vtkPolyDataMapper
*> m_CellTextPolyDataMapper
;
157 vector
<vtkFollower
*> m_CellTextFollower
;
159 vtkPolyDataMapper
* m_PickMapper
; ///< VTK mapper to map pick marker
160 vtkActor
* m_PickActor
; ///< VTK actor to display pick marker
161 vtkSphereSource
* m_PickSphere
; ///< sphere to mark picked cell/points
162 vtkCubeAxesActor2D
* m_Axes
; ///< VTK actor to display the coordinate system
163 vtkEgBoundaryCodesFilter
* m_BCodesFilter
; ///< VTK filter to extract boundary elements with certain codes
164 vtkCellPicker
* m_CellPicker
; ///< VTK CellPicker to pick cells for various user interactions
165 vtkPointPicker
* m_PointPicker
; ///< VTK PointPicker to pick points for various user interactions
166 int m_PickedObject
; ///< 0=none, 1=node, 2=cell
168 QString m_CurrentFilename
; ///< The current file name of the grid.
169 int m_CurrentOperation
; ///< The current operation number. (used for undo/redo)
170 bool m_undo_redo_enabled
; ///< if true, undo/redo operations will be usable.
171 int m_LastOperation
; ///< The last operation number. (used for undo/redo)
172 QString m_LogDir
; ///< the log directory
173 QLabel
* m_StatusLabel
; ///< Label for the information in the status bar
174 QLabel
* m_StatusInfoLabel
;
175 QProgressBar
* m_StatusProgressBar
;
176 QSet
<int> m_DisplayBoundaryCodes
; ///< A QList with all active boundary codes.
177 QSet
<int> m_AllBoundaryCodes
; ///< A QList with all boundary codes.
178 bool m_Busy
; ///< flag to indicate that enGrid is busy with an operation
179 QString m_LogFileName
; ///< log file to collect program output for display in the output window
180 long int m_N_chars
; ///< number of lines that have been read from the log file
181 #if defined( __linux__ ) //for Linux
184 fpos_t m_SystemStdout_pos
;
185 fpos_t m_LogFileStdout_pos
;
186 #elif defined( _WIN32 ) //for Windows
187 //Windows always uses CON
188 FILE* m_SystemStdout
;
189 FILE* m_LogFileStdout
;
191 #error "Please define the proper way to save/recover the stdout."
194 QTimer m_GarbageTimer
;
197 QMap
<int, BoundaryCondition
> m_bcmap
; ///< mapping between numerical and symbolic boundary codes
198 QMap
<QString
, VolumeDefinition
> m_VolMap
; ///< all volume definitions
199 QMap
<QString
, PhysicalBoundaryCondition
> m_PhysicalBoundaryConditionsMap
; ///< all physical boundary conditions definitions
201 QMap
<int, SurfaceProjection
*> m_SurfProj
; ///< all surface projectors for surface meshing
203 QMap
<QAction
*, Operation
*> m_PluginOperations
;
204 QAction
* m_EscAction
;
206 int m_SolverIndex
;// deprecated
207 OpenFOAMTools m_OpenFoamTools
;
209 // recent file list support
211 QMap
<QString
,QDateTime
> m_RecentFiles
;
212 QMenu
* recentFileMenu() { return ui
.menuOpen_recent
; }
213 void readRecentFiles();
214 void writeRecentFiles();
215 void addRecentFile(QString file_name
, QDateTime date
);
217 void openRecent(QAction
*action
);
222 private: // static attributes
225 * Platform independant access to application settings.
226 * For a UNIX system the user preferences will be stored in the file
227 * folder ".config/enGits/enGrid.conf" in the user's home directory;
228 * on Windows preferences will be stored in the registry.
230 static QSettings m_qset
;
233 * The current working directory of enGrid
235 static QString m_cwd
;
238 * Is the current case unsaved?
240 static bool m_UnSaved
;
242 /** a static this pointer (somewhat ugly, but there is only one MainWindow) */
243 static GuiMainWindow
* THIS
;
248 static void pickCallBack( vtkObject
*caller
, unsigned long int eid
, void *clientdata
, void *calldata
);
249 void updateSurfaceActors( bool forced
);
250 void updateVolumeActors( bool forced
);
254 void setClipX( const QString
&txt
);
255 void setClipY( const QString
&txt
);
256 void setClipZ( const QString
&txt
);
257 void setClipNX( const QString
&txt
);
258 void setClipNY( const QString
&txt
);
259 void setClipNZ( const QString
&txt
);
264 void openPhysicalBoundaryConditions();
265 void savePhysicalBoundaryConditions();
267 void openGrid(QString file_name
);
273 GuiMainWindow();///< Default constructor.
274 GuiMainWindow(QString file_name
);///< Constructor which opens a file directly.
278 * This function connects the menu and toolbar actions and
279 * the VTK basics(i.e. renderer, actor, ...) will be set up.
280 * Furthermore preferences will be read from qset.
282 void setupGuiMainWindow();
287 * Preferences will be written back.
289 virtual ~GuiMainWindow();
292 * Get the VTK render window
293 * @return the VTK render window
295 vtkRenderWindow
* getRenderWindow();
298 * Get the VTK renderer
299 * @return the VTK renderer
301 vtkRenderer
* getRenderer();
304 * Get the Qt-VTK interactor
305 * @return the Qt-VTK interactor
307 QVTKInteractor
* getInteractor();
310 * Get a pointer to the current grid object
311 * @return a pointer to the current vtkUnstructuredGrid object
313 vtkUnstructuredGrid
* getGrid() { return m_Grid
; }
315 void setBusy() { m_Busy
= true; updateStatusBar(); }
316 void setIdle() { m_Busy
= false; updateStatusBar(); }
318 /// Returns log directory
319 QString
getLogDir() { return m_LogDir
; }
321 /// Returns the path to the currently loaded file
322 QString
getFilePath();
324 /// Returns the index of the solver to use. The index corresponds to the position in solvers.txt .
325 void setSolverIndex(int x
) {m_SolverIndex
= x
;}
326 int getSolverIndex() {return m_SolverIndex
;}
328 public: // static methods
331 * Get the current working directory.
332 * @return the current working directory
334 static QString
getCwd();
337 * Set the current working directory
338 * @param dir the current working directory
340 static void setCwd( QString dir
);
344 * @param unsaved Do you want to be asked where to save when clicking on save next time?
346 static void setUnsaved( bool unsaved
);
349 * Get the currently picked cell.
350 * @return the picked cell ID or -1 if no cell has been picked
352 vtkIdType
getPickedCell();
355 * Get the currently picked point.
356 * @return the picked point ID or -1 if no point has been picked
358 vtkIdType
getPickedPoint();
360 vtkIdType
getPickedObject() { return m_PickedObject
; }
363 * Access to the QSettings object
365 static QSettings
* settings() { return &m_qset
; }
367 BoundaryCondition
getBC( int bc
) { return m_bcmap
[bc
]; }
368 VolumeDefinition
getVol( QString volname
) { return m_VolMap
[volname
]; }
369 void clearBCs() { m_bcmap
.clear(); }
370 void addBC(int bc
, BoundaryCondition BC
) { m_bcmap
[bc
] = BC
; }
372 QList
<VolumeDefinition
> getAllVols();
373 void setAllVols( QList
<VolumeDefinition
> vols
);
374 void createDefaultVol();
376 QList
<PhysicalBoundaryCondition
> getAllPhysicalBoundaryConditions();
377 void setAllPhysicalBoundaryConditions (QList
<PhysicalBoundaryCondition
> physical_boundary_conditions
);
378 void setAllPhysicalBoundaryConditions (QMap
<QString
, PhysicalBoundaryCondition
> physical_boundary_conditions
);
379 bool physicalTypeDefined(QString name
) { return m_PhysicalBoundaryConditionsMap
.contains(name
); };
380 PhysicalBoundaryCondition
getPhysicalBoundaryCondition(QString name
) { return m_PhysicalBoundaryConditionsMap
[name
]; }
382 static GuiMainWindow
* pointer() { return THIS
; }
383 static void lock() { m_Mutex
.lock(); }
384 static void unlock() { m_Mutex
.unlock(); }
385 static bool tryLock() { return m_Mutex
.tryLock(); }
386 void getAllBoundaryCodes(QVector
<int> &bcs
);
387 QSet
<int> getAllBoundaryCodes();
388 void getDisplayBoundaryCodes(QSet
<int> &bcs
);
389 vtkPointPicker
* getPointPicker() { return ( m_PointPicker
);}
390 vtkSphereSource
* getPickSphere() { return ( m_PickSphere
);}
391 bool pickPoint( vtkIdType id_point
);
392 bool pickCell( vtkIdType id_cell
);
394 QString
getFilename() { return( m_CurrentFilename
); }
395 void setFilename(QString filename
) { m_CurrentFilename
= filename
; }
397 SurfaceProjection
* getSurfProj(int bc
);
398 void setSurfProj(SurfaceProjection
*surf_proj
, int bc
) { m_SurfProj
[bc
] = surf_proj
; }
399 bool checkSurfProj();
401 void setSystemOutput();
402 void setLogFileOutput();
404 void resetProgress(QString info_text
, int p_max
);
405 void setProgress(int p
);
409 void setUseVTKInteractor( int a_UseVTKInteractor
);
410 void setPickMode( bool a_UseVTKInteractor
, bool a_CellPickerMode
);
412 void exit(); ///< Exit the application
413 void importSTL(); ///< Import an STL file (ASCII or binary)
414 void importGmsh1Ascii(); ///< Import a Gmsh grid from an ASCII file -- using version 1.0 of the Gmsh file format
415 void exportGmsh1Ascii(); ///< Export a grid from to an ASCII Gmsh file -- using version 1.0 of the Gmsh file format
416 void importGmsh2Ascii(); ///< Import a Gmsh grid from an ASCII file -- using version 2.0 of the Gmsh file format
417 void exportGmsh2Ascii(); ///< Export a grid from to an ASCII Gmsh file -- using version 2.0 of the Gmsh file format
418 void exportNeutral(); ///< Export a grid to neutral format for NETGEN
419 void updateActors( bool force
= false ); ///< Update the VTK output
420 void forceUpdateActors(); ///< Force an update of the VTK output
421 void scaleToData(); ///< Scale to data
422 void zoomAll(); ///< Move the camera in order to show everything on the screen
423 void zoomOnPickedObject();
425 void printGrid() {cout
<< "PrintGrid() called!" << endl
; cout_grid( cout
, m_Grid
, true, true, true, true );}
431 void resetOperationCounter();
433 void open(); ///< Open an existing case
434 void open( QString file_name
, bool update_current_filename
= true ); ///< Open case file_name
435 void save(); ///< Save the current case
436 void saveAs(); ///< Save the current case -- using a different file name
437 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).
439 int quickSave(); ///< Save the current grid as a_filename_a_operation
440 void quickLoad( int a_operation
); ///< Load a_filename_a_operation
441 void updateStatusBar(); ///< Update the status bar
442 void selectBoundaryCodes(); ///< Select the boundary codes to be displayed/hidden
443 void updateBoundaryCodes( bool all_on
); ///< Update the boundary code book keeping (e.g. after reading a mesh).
444 void normalExtrusion(); ///< Normal extrusion of boundary elements (no validity check).
445 void setAxesVisibility(); ///< Toggle the visibility of the axes annotation.
446 void setViewingMode(); ///< Toggle orthogonal viewing mode.
447 void viewNodeIDs(); ///< Toggle node ID viewing mode.
448 void viewCellIDs(); ///< Toggle cell ID viewing mode.
449 void changeSurfaceOrientation(); ///< Change the orientation of all surface elements
450 void checkSurfaceOrientation(); ///< Check and, if required, change the orientation of all surface elements
451 void improveAspectRatio(); ///< Eliminate edges in order to improve the aspect ratio of the cells
452 void exportAsciiStl(); ///< Write surface elements to an ASCII STL file.
453 void exportBinaryStl(); ///< Write surface elements to a binary STL file.
454 void exportAsciiPly(); ///< Write surface elements to an ASCII PLY file.
455 void exportBinaryPly(); ///< Write surface elements to a binary PLY file.
456 void editBoundaryConditions(); ///< Edit boundary conditions (names and types)
457 void configure(); ///< Edit settings
458 void about(); ///< Display an about message
459 void markOutputLine(); ///< Mark the current position in the output window
461 QString
getXmlSection( QString name
); ///< Get a section from the XML case description
462 void setXmlSection( QString name
, QString contents
); ///< Set a section of the XML case description
471 void appendOutput( QString txt
) { ui
.textEditOutput
->append( txt
); }
472 void clearOutput() { ui
.textEditOutput
->clear(); }
474 void periodicUpdate();
476 void storeSurfaceProjection(bool nosave
= false);
477 void resetSurfaceProjection();
479 // SLOTS for all standard operations should be defined below;
480 // entries should look like this:
481 // void callOperationName() { EG_STDSLOT(OperationName); };
482 // The actual class name in this case, however, would be GuiOperationName.
484 // the following line can be used as a template:
485 // void call() { EG_STDSLOT(); };
486 // IMPORTANT: Using EG_STDSLOT sets lock_gui to true, while EG_STDINTERSLOT does not (default is lock_gui = false)
487 // This is important to determine whether an operation should try to lock the main mutex or not.
488 // 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.
490 // EG_STDSLOT = background operation (There can not be more than one background operation!)
491 // EG_STDINTERSLOT = foreground operation
492 // 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.
494 void callCreateSurfaceMesh() { EG_STDINTERSLOT( GuiCreateSurfaceMesh
); }
495 void callCreateBoundaryLayer() { EG_STDSLOT( GuiCreateBoundaryLayer
); }
496 void callDivideBoundaryLayer() { EG_STDSLOT( GuiDivideBoundaryLayer
); }
497 void callDeleteVolumeGrid() { EG_STDSLOT( DeleteVolumeGrid
); }
498 void callDeleteTetras() { EG_STDSLOT( DeleteTetras
); }
499 void callCreateVolumeMesh() { EG_STDSLOT( GuiCreateVolumeMesh
); }
500 void callSmoothVolumeGrid() { EG_STDSLOT( SmoothVolumeGrid
); }
501 void callSetBoundaryCode() { EG_STDINTERSLOT( GuiSetBoundaryCode
); }
502 void callDeleteBadAspectTris() { EG_STDINTERSLOT( GuiDeleteBadAspectTris
); }
503 void callDeletePickedCell() { EG_STDSLOT( DeletePickedCell
); }
504 void callMergeNodes();
505 void callInsertNewCell();
506 void callDeletePickedPoint();
507 void callBoxSelect() { EG_STDINTERSLOT( BoxSelect
); }
508 void callCheckSurfaceIntegrity() { EG_STDINTERSLOT( CheckSurfaceIntegrity
); }
509 void callPick_cell_point() { EG_STDINTERSLOT( GuiPick
); }
510 void callTransform() { EG_STDINTERSLOT( GuiTransform
); }
511 void callUpdateSurfProj() { EG_STDINTERSLOT( UpdateSurfProj
); }
512 void callImportOpenFoamCase() { EG_STDREADERSLOT(FoamReader
); }
513 void callMergeVolumes() { EG_STDSLOT(GuiMergeVolumes
); }
514 void callMirrorMesh() { EG_STDSLOT(GuiMirrorMesh
); }
515 void callOrthogonalityOptimiser() { EG_STDSLOT(OrthogonalityOptimiser
); }
516 void callCreateHexCore() { EG_STDSLOT( GuiCreateHexCore
); }
517 void callBooleanOperation() { EG_STDSLOT( GuiBooleanGeometryOperation
); }
521 void callFoamWriter() { EG_STDINTERSLOT( FoamWriter
); }
522 void callSimpleFoamWriter() { EG_STDINTERSLOT( SimpleFoamWriter
); }
523 void callFoamCaseWriter() { EG_STDINTERSLOT( OpenFOAMcase
); }
524 void callCgnsWriter() { EG_STDINTERSLOT( CgnsWriter
); }
525 void callVtkReader() { EG_STDREADERSLOT( VtkReader
); }
526 void callBlenderReader() { EG_STDREADERSLOT( BlenderReader
); }
527 void callBlenderWriter() { EG_STDREADERSLOT( BlenderWriter
); }
528 void callPolyDataReader() { EG_STDREADERSLOT( PolyDataReader
); }
529 void callReducedPolyDataReader() { EG_STDREADERSLOT( ReducedPolyDataReader
); }
530 void callSeligAirfoilReader() { EG_STDREADERSLOT( SeligAirfoilReader
); }
531 void callBrlcadReader() { EG_STDREADERSLOT( BrlcadReader
); }
532 void callExportSu2() { EG_STDREADERSLOT( Su2Writer
); }
533 void callExportDolfyn() { EG_STDREADERSLOT( DolfynWriter
); }
535 void callSurfaceMesher() { EG_STDSLOT(GuiSurfaceMesher
); }
536 void callReduceSurfaceTriangulation() { EG_STDSLOT(ReduceSurfaceTriangulation
); }
537 void callEliminateSmallBranches() { EG_STDSLOT(EliminateSmallBranches
); }
538 void callSmoothAndSwapSurface() { EG_STDSLOT(SmoothAndSwapSurface
); }
539 void callSharpenEdges() { EG_STDSLOT(SharpenEdges
); }
540 void callCheckForOverlap() { EG_STDSLOT(CheckForOverlap
); }
542 void callFixCADGeometry() { EG_STDSLOT(FixCadGeometry
); }