2 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 // + This file is part of enGrid. +
6 // + Copyright 2008-2013 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"
74 #include "cadinterface.h"
76 #include "openfoamcase.h"
77 #include "guitransform.h"
78 #include "openfoamtools.h"
79 #include "std_includes.h"
80 #include "fixcadgeometry.h"
81 #include "xmlhandler.h"
84 * This is the main GUI class of enGrid.
86 class CLASS_LIBENGRID_DLL GuiMainWindow
: public QMainWindow
, public EgVtkObject
91 private: // attributes
93 XmlHandler
* m_XmlHandler
;
95 Ui::GuiMainWindow ui
; ///< The user interface definition -- created by QtDesigner.
96 vtkUnstructuredGrid
*m_Grid
; ///< The current state of the grid that is being generated.
98 vtkRenderer
*m_Renderer
; ///< The VTK renderer object, used for visualising the grid
100 vtkActor
* m_SurfaceActor
;
101 vtkActor
* m_SurfaceWireActor
;
102 vtkActor
* m_TetraActor
;
103 vtkActor
* m_WedgeActor
;
104 vtkActor
* m_PyramidActor
;
105 vtkActor
* m_HexaActor
;
106 vtkActor
* m_PolyhedraActor
;
107 vtkActor
* m_VolumeWireActor
;
109 vtkProperty
* m_BackfaceProperty
;
110 vtkLookupTable
* m_LookupTable
;
111 vtkScalarBarActor
* m_LegendActor
;
113 vtkPolyDataMapper
* m_SurfaceMapper
;
114 vtkPolyDataMapper
* m_SurfaceWireMapper
;
115 vtkPolyDataMapper
* m_TetraMapper
;
116 vtkPolyDataMapper
* m_PyramidMapper
;
117 vtkPolyDataMapper
* m_WedgeMapper
;
118 vtkPolyDataMapper
* m_HexaMapper
;
119 vtkPolyDataMapper
* m_PolyhedraMapper
;
120 vtkPolyDataMapper
* m_VolumeWireMapper
;
122 double m_ColTetraR
, m_ColTetraG
, m_ColTetraB
;
123 double m_ColPyraR
, m_ColPyraG
, m_ColPyraB
;
124 double m_ColPrismR
, m_ColPrismG
, m_ColPrismB
;
125 double m_ColHexR
, m_ColHexG
, m_ColHexB
;
126 double m_ColAR
, m_ColAG
, m_ColAB
;
127 double m_ColBR
, m_ColBG
, m_ColBB
;
129 vtkEgExtractVolumeCells
*m_ExtrVol
;
130 vtkEgExtractVolumeCells
*m_ExtrTetras
;
131 vtkEgExtractVolumeCells
*m_ExtrPyramids
;
132 vtkEgExtractVolumeCells
*m_ExtrWedges
;
133 vtkEgExtractVolumeCells
*m_ExtrHexes
;
134 vtkEgExtractVolumeCells
*m_ExtrPolyhedra
;
136 vtkGeometryFilter
*m_VolumeGeometry
;
137 vtkGeometryFilter
*m_TetraGeometry
;
138 vtkGeometryFilter
*m_PyramidGeometry
;
139 vtkGeometryFilter
*m_WedgeGeometry
;
140 vtkGeometryFilter
*m_HexaGeometry
;
141 vtkGeometryFilter
*m_PolyhedraGeometry
;
143 vtkIdType m_PickedPoint
; ///< Picked point
144 vtkIdType m_PickedCell
; ///< Picked cell
145 bool m_UseVTKInteractor
; ///< Boolean value specifying whether the VTK Interactor should be used or not
147 static QMutex m_Mutex
;
149 vtkGeometryFilter
* m_SurfaceFilter
; ///< VTK filter to extract the surface of the current grid.
150 double m_ReferenceSize
; ///< Size to use for picker objects and annotations
152 vector
<vtkTextActor
*> m_NodeText
; ///< 2D Text actor to display node IDs
153 vector
<vtkTextActor
*> m_CellText
; ///< 2D Text actor to display cell IDs
154 vector
<vtkVectorText
*> m_NodeTextVectorText
; ///< 3D Text actor to display node IDs
155 vector
<vtkPolyDataMapper
*> m_NodeTextPolyDataMapper
;
156 vector
<vtkFollower
*> m_NodeTextFollower
;
157 vector
<vtkVectorText
*> m_CellTextVectorText
; ///< 3D Text actor to display cell IDs
158 vector
<vtkPolyDataMapper
*> m_CellTextPolyDataMapper
;
159 vector
<vtkFollower
*> m_CellTextFollower
;
161 vtkPolyDataMapper
* m_PickMapper
; ///< VTK mapper to map pick marker
162 vtkActor
* m_PickActor
; ///< VTK actor to display pick marker
163 vtkSphereSource
* m_PickSphere
; ///< sphere to mark picked cell/points
164 vtkCubeAxesActor2D
* m_Axes
; ///< VTK actor to display the coordinate system
165 vtkEgBoundaryCodesFilter
* m_BCodesFilter
; ///< VTK filter to extract boundary elements with certain codes
166 vtkCellPicker
* m_CellPicker
; ///< VTK CellPicker to pick cells for various user interactions
167 vtkPointPicker
* m_PointPicker
; ///< VTK PointPicker to pick points for various user interactions
168 int m_PickedObject
; ///< 0=none, 1=node, 2=cell
170 QString m_CurrentFilename
; ///< The current file name of the grid.
171 int m_CurrentOperation
; ///< The current operation number. (used for undo/redo)
172 bool m_undo_redo_enabled
; ///< if true, undo/redo operations will be usable.
173 int m_LastOperation
; ///< The last operation number. (used for undo/redo)
174 QString m_LogDir
; ///< the log directory
175 QLabel
* m_StatusLabel
; ///< Label for the information in the status bar
176 QLabel
* m_StatusInfoLabel
;
177 QProgressBar
* m_StatusProgressBar
;
178 QSet
<int> m_DisplayBoundaryCodes
; ///< A QList with all active boundary codes.
179 QSet
<int> m_AllBoundaryCodes
; ///< A QList with all boundary codes.
180 bool m_Busy
; ///< flag to indicate that enGrid is busy with an operation
181 QString m_LogFileName
; ///< log file to collect program output for display in the output window
182 long int m_N_chars
; ///< number of lines that have been read from the log file
183 #if defined( __linux__ ) //for Linux
186 fpos_t m_SystemStdout_pos
;
187 fpos_t m_LogFileStdout_pos
;
188 #elif defined( _WIN32 ) //for Windows
189 //Windows always uses CON
190 FILE* m_SystemStdout
;
191 FILE* m_LogFileStdout
;
193 #error "Please define the proper way to save/recover the stdout."
196 QTimer m_GarbageTimer
;
199 QMap
<int, BoundaryCondition
> m_bcmap
; ///< mapping between numerical and symbolic boundary codes
200 QMap
<QString
, VolumeDefinition
> m_VolMap
; ///< all volume definitions
201 QMap
<QString
, PhysicalBoundaryCondition
> m_PhysicalBoundaryConditionsMap
; ///< all physical boundary conditions definitions
203 QMap
<int, CadInterface
*> m_CadInterfaces
; ///< all CAD interfaces
204 CadInterface
* m_UniCadInterface
; ///< universal CAD interface for all boundary conditions
206 QMap
<QAction
*, Operation
*> m_PluginOperations
;
207 QAction
* m_EscAction
;
209 int m_SolverIndex
;// deprecated
210 OpenFOAMTools m_OpenFoamTools
;
212 // recent file list support
214 QMap
<QString
,QDateTime
> m_RecentFiles
;
215 QMenu
* recentFileMenu() { return ui
.menuOpen_recent
; }
216 void readRecentFiles();
217 void writeRecentFiles();
218 void addRecentFile(QString file_name
, QDateTime date
);
220 void openRecent(QAction
*action
);
225 private: // static attributes
228 * Platform independant access to application settings.
229 * For a UNIX system the user preferences will be stored in the file
230 * folder ".config/enGits/enGrid.conf" in the user's home directory;
231 * on Windows preferences will be stored in the registry.
233 static QSettings m_qset
;
236 * The current working directory of enGrid
238 static QString m_cwd
;
241 * Is the current case unsaved?
243 static bool m_UnSaved
;
245 /** a static this pointer (somewhat ugly, but there is only one MainWindow) */
246 static GuiMainWindow
* THIS
;
251 static void pickCallBack( vtkObject
*caller
, unsigned long int eid
, void *clientdata
, void *calldata
);
252 void updateSurfaceActors( bool forced
);
253 void updateVolumeActors( bool forced
);
257 void setClipX( const QString
&txt
);
258 void setClipY( const QString
&txt
);
259 void setClipZ( const QString
&txt
);
260 void setClipNX( const QString
&txt
);
261 void setClipNY( const QString
&txt
);
262 void setClipNZ( const QString
&txt
);
267 void openPhysicalBoundaryConditions();
268 void savePhysicalBoundaryConditions();
270 void openGrid(QString file_name
);
276 GuiMainWindow();///< Default constructor.
277 GuiMainWindow(QString file_name
);///< Constructor which opens a file directly.
281 * This function connects the menu and toolbar actions and
282 * the VTK basics(i.e. renderer, actor, ...) will be set up.
283 * Furthermore preferences will be read from qset.
285 void setupGuiMainWindow();
290 * Preferences will be written back.
292 virtual ~GuiMainWindow();
295 * Get the VTK render window
296 * @return the VTK render window
298 vtkRenderWindow
* getRenderWindow();
301 * Get the VTK renderer
302 * @return the VTK renderer
304 vtkRenderer
* getRenderer();
307 * Get the Qt-VTK interactor
308 * @return the Qt-VTK interactor
310 QVTKInteractor
* getInteractor();
313 * Get a pointer to the current grid object
314 * @return a pointer to the current vtkUnstructuredGrid object
316 vtkUnstructuredGrid
* getGrid() { return m_Grid
; }
318 void setBusy() { m_Busy
= true; updateStatusBar(); }
319 void setIdle() { m_Busy
= false; updateStatusBar(); }
321 /// Returns log directory
322 QString
getLogDir() { return m_LogDir
; }
324 /// Returns the path to the currently loaded file
325 QString
getFilePath();
327 /// Returns the index of the solver to use. The index corresponds to the position in solvers.txt .
328 void setSolverIndex(int x
) {m_SolverIndex
= x
;}
329 int getSolverIndex() {return m_SolverIndex
;}
331 public: // static methods
334 * Get the current working directory.
335 * @return the current working directory
337 static QString
getCwd();
340 * Set the current working directory
341 * @param dir the current working directory
343 static void setCwd( QString dir
);
347 * @param unsaved Do you want to be asked where to save when clicking on save next time?
349 static void setUnsaved( bool unsaved
);
352 * Get the currently picked cell.
353 * @return the picked cell ID or -1 if no cell has been picked
355 vtkIdType
getPickedCell();
358 * Get the currently picked point.
359 * @return the picked point ID or -1 if no point has been picked
361 vtkIdType
getPickedPoint();
363 vtkIdType
getPickedObject() { return m_PickedObject
; }
366 * Access to the QSettings object
368 static QSettings
* settings() { return &m_qset
; }
370 BoundaryCondition
getBC( int bc
) { return m_bcmap
[bc
]; }
371 VolumeDefinition
getVol( QString volname
) { return m_VolMap
[volname
]; }
372 void clearBCs() { m_bcmap
.clear(); }
373 void addBC(int bc
, BoundaryCondition BC
) { m_bcmap
[bc
] = BC
; }
375 QList
<VolumeDefinition
> getAllVols();
376 void setAllVols( QList
<VolumeDefinition
> vols
);
377 void createDefaultVol();
379 QList
<PhysicalBoundaryCondition
> getAllPhysicalBoundaryConditions();
380 void setAllPhysicalBoundaryConditions (QList
<PhysicalBoundaryCondition
> physical_boundary_conditions
);
381 void setAllPhysicalBoundaryConditions (QMap
<QString
, PhysicalBoundaryCondition
> physical_boundary_conditions
);
382 bool physicalTypeDefined(QString name
) { return m_PhysicalBoundaryConditionsMap
.contains(name
); };
383 PhysicalBoundaryCondition
getPhysicalBoundaryCondition(QString name
) { return m_PhysicalBoundaryConditionsMap
[name
]; }
385 static GuiMainWindow
* pointer() { return THIS
; }
386 static void lock() { m_Mutex
.lock(); }
387 static void unlock() { m_Mutex
.unlock(); }
388 static bool tryLock() { return m_Mutex
.tryLock(); }
389 void getAllBoundaryCodes(QVector
<int> &bcs
);
390 QSet
<int> getAllBoundaryCodes();
391 void getDisplayBoundaryCodes(QSet
<int> &bcs
);
392 vtkPointPicker
* getPointPicker() { return ( m_PointPicker
);}
393 vtkSphereSource
* getPickSphere() { return ( m_PickSphere
);}
394 bool pickPoint( vtkIdType id_point
);
395 bool pickCell( vtkIdType id_cell
);
397 QString
getFilename() { return( m_CurrentFilename
); }
398 void setFilename(QString filename
) { m_CurrentFilename
= filename
; }
400 CadInterface
* getCadInterface(int bc
, bool allow_null
= false);
401 void setCadInterface(CadInterface
* cad_interface
, int bc
) { m_CadInterfaces
[bc
] = cad_interface
; }
402 void setUniversalCadInterface(CadInterface
* cad_interface
);
403 bool checkCadInterfaces();
405 void setSystemOutput();
406 void setLogFileOutput();
408 void resetProgress(QString info_text
, int p_max
);
409 void setProgress(int p
);
413 void setUseVTKInteractor( int a_UseVTKInteractor
);
414 void setPickMode( bool a_UseVTKInteractor
, bool a_CellPickerMode
);
416 void exit(); ///< Exit the application
417 void importSTL(); ///< Import an STL file (ASCII or binary)
418 void importGmsh1Ascii(); ///< Import a Gmsh grid from an ASCII file -- using version 1.0 of the Gmsh file format
419 void exportGmsh1Ascii(); ///< Export a grid from to an ASCII Gmsh file -- using version 1.0 of the Gmsh file format
420 void importGmsh2Ascii(); ///< Import a Gmsh grid from an ASCII file -- using version 2.0 of the Gmsh file format
421 void exportGmsh2Ascii(); ///< Export a grid from to an ASCII Gmsh file -- using version 2.0 of the Gmsh file format
422 void exportNeutral(); ///< Export a grid to neutral format for NETGEN
423 void updateActors( bool force
= false ); ///< Update the VTK output
424 void forceUpdateActors(); ///< Force an update of the VTK output
425 void scaleToData(); ///< Scale to data
426 void zoomAll(); ///< Move the camera in order to show everything on the screen
427 void zoomOnPickedObject();
429 void printGrid() {cout
<< "PrintGrid() called!" << endl
; cout_grid( cout
, m_Grid
, true, true, true, true );}
435 void resetOperationCounter();
437 void open(); ///< Open an existing case
438 void open( QString file_name
, bool update_current_filename
= true ); ///< Open case file_name
439 void save(); ///< Save the current case
440 void saveAs(); ///< Save the current case -- using a different file name
441 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).
443 int quickSave(); ///< Save the current grid as a_filename_a_operation
444 void quickLoad( int a_operation
); ///< Load a_filename_a_operation
445 void updateStatusBar(); ///< Update the status bar
446 void selectBoundaryCodes(); ///< Select the boundary codes to be displayed/hidden
447 void updateBoundaryCodes( bool all_on
); ///< Update the boundary code book keeping (e.g. after reading a mesh).
448 void normalExtrusion(); ///< Normal extrusion of boundary elements (no validity check).
449 void setAxesVisibility(); ///< Toggle the visibility of the axes annotation.
450 void setViewingMode(); ///< Toggle orthogonal viewing mode.
451 void viewNodeIDs(); ///< Toggle node ID viewing mode.
452 void viewCellIDs(); ///< Toggle cell ID viewing mode.
453 void changeSurfaceOrientation(); ///< Change the orientation of all surface elements
454 void checkSurfaceOrientation(); ///< Check and, if required, change the orientation of all surface elements
455 void improveAspectRatio(); ///< Eliminate edges in order to improve the aspect ratio of the cells
456 void exportAsciiStl(); ///< Write surface elements to an ASCII STL file.
457 void exportBinaryStl(); ///< Write surface elements to a binary STL file.
458 void exportAsciiPly(); ///< Write surface elements to an ASCII PLY file.
459 void exportBinaryPly(); ///< Write surface elements to a binary PLY file.
460 void editBoundaryConditions(); ///< Edit boundary conditions (names and types)
461 void configure(); ///< Edit settings
462 void about(); ///< Display an about message
463 void markOutputLine(); ///< Mark the current position in the output window
465 QString
getXmlSection( QString name
); ///< Get a section from the XML case description
466 void setXmlSection( QString name
, QString contents
); ///< Set a section of the XML case description
475 void appendOutput( QString txt
) { ui
.textEditOutput
->append( txt
); }
476 void clearOutput() { ui
.textEditOutput
->clear(); }
478 void periodicUpdate();
480 void storeCadInterfaces(bool nosave
= false);
481 void resetCadInterfaces();
483 // SLOTS for all standard operations should be defined below;
484 // entries should look like this:
485 // void callOperationName() { EG_STDSLOT(OperationName); };
486 // The actual class name in this case, however, would be GuiOperationName.
488 // the following line can be used as a template:
489 // void call() { EG_STDSLOT(); };
490 // IMPORTANT: Using EG_STDSLOT sets lock_gui to true, while EG_STDINTERSLOT does not (default is lock_gui = false)
491 // This is important to determine whether an operation should try to lock the main mutex or not.
492 // 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.
494 // EG_STDSLOT = background operation (There can not be more than one background operation!)
495 // EG_STDINTERSLOT = foreground operation
496 // 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.
498 void callCreateSurfaceMesh() { EG_STDINTERSLOT( GuiCreateSurfaceMesh
); }
499 void callCreateBoundaryLayer() { EG_STDSLOT( GuiCreateBoundaryLayer
); }
500 void callDivideBoundaryLayer() { EG_STDSLOT( GuiDivideBoundaryLayer
); }
501 void callDeleteVolumeGrid() { EG_STDSLOT( DeleteVolumeGrid
); }
502 void callDeleteTetras() { EG_STDSLOT( DeleteTetras
); }
503 void callCreateVolumeMesh() { EG_STDSLOT( GuiCreateVolumeMesh
); }
504 void callSmoothVolumeGrid() { EG_STDSLOT( SmoothVolumeGrid
); }
505 void callSetBoundaryCode() { EG_STDINTERSLOT( GuiSetBoundaryCode
); }
506 void callDeleteBadAspectTris() { EG_STDINTERSLOT( GuiDeleteBadAspectTris
); }
507 void callDeletePickedCell() { EG_STDSLOT( DeletePickedCell
); }
508 void callMergeNodes();
509 void callInsertNewCell();
510 void callDeletePickedPoint();
511 void callBoxSelect() { EG_STDINTERSLOT( BoxSelect
); }
512 void callCheckSurfaceIntegrity() { EG_STDINTERSLOT( CheckSurfaceIntegrity
); }
513 void callPick_cell_point() { EG_STDINTERSLOT( GuiPick
); }
514 void callTransform() { EG_STDINTERSLOT( GuiTransform
); }
515 void callUpdateSurfProj() { EG_STDINTERSLOT( UpdateSurfProj
); }
516 void callImportOpenFoamCase() { EG_STDREADERSLOT(FoamReader
); }
517 void callMergeVolumes() { EG_STDSLOT(GuiMergeVolumes
); }
518 void callMirrorMesh() { EG_STDSLOT(GuiMirrorMesh
); }
519 void callCreateHexCore() { EG_STDSLOT( GuiCreateHexCore
); }
523 void callFoamWriter() { EG_STDINTERSLOT( FoamWriter
); }
524 void callSimpleFoamWriter() { EG_STDINTERSLOT( SimpleFoamWriter
); }
525 void callFoamCaseWriter() { EG_STDINTERSLOT( OpenFOAMcase
); }
526 void callCgnsWriter() { EG_STDINTERSLOT( CgnsWriter
); }
527 void callVtkReader() { EG_STDREADERSLOT( VtkReader
); }
528 void callBlenderReader() { EG_STDREADERSLOT( BlenderReader
); }
529 void callBlenderWriter() { EG_STDREADERSLOT( BlenderWriter
); }
530 void callPolyDataReader() { EG_STDREADERSLOT( PolyDataReader
); }
531 void callReducedPolyDataReader() { EG_STDREADERSLOT( ReducedPolyDataReader
); }
532 void callSeligAirfoilReader() { EG_STDREADERSLOT( SeligAirfoilReader
); }
533 void callBrlcadReader() { EG_STDREADERSLOT( BrlcadReader
); }
534 void callExportSu2() { EG_STDREADERSLOT( Su2Writer
); }
535 void callExportDolfyn() { EG_STDREADERSLOT( DolfynWriter
); }
536 void callExportTau() { EG_STDREADERSLOT( TauWriter
); }
538 void callSurfaceMesher() { EG_STDSLOT(GuiSurfaceMesher
); }
539 void callReduceSurfaceTriangulation() { EG_STDSLOT(ReduceSurfaceTriangulation
); }
540 void callEliminateSmallBranches() { EG_STDSLOT(EliminateSmallBranches
); }
541 void callSmoothAndSwapSurface() { EG_STDSLOT(SmoothAndSwapSurface
); }
542 void callCheckForOverlap() { EG_STDSLOT(CheckForOverlap
); }
544 void callFixCADGeometry() { EG_STDSLOT(FixCadGeometry
); }