I have created a maro-file which can be used to:
- automatically open the .vtu file created by Elmer solver in Paraview (until now supporting only "Beside .FCStd file" setting in FEM-Preferences)
- the configuration section in the macro offers support for the use of a .py or .pvsm state file which has to be loacted in the same folder as the .vtu file.
- several ElmerSolver objects in a .FCStd file are supported
Simply select the ElmerSolver object and run the macro.
Limitations
- everytime one runs the macro a new instance of Paraview will be opened. To circumvent that one can open the .vtu file in Paraview with or without using statefile and reload the 'Reload files' every time the results are regenerated.
- It was not tested on Window
Code: Select all
# -*- coding: utf-8 -*-
## Paraview postroccesing started from within FreeCAD
# Author: HoWil
# License: LGPL v 2.1
# Version: 20200703
# =============================================================================
# Description
# =============================================================================
# First, one hast to select a ElmerSolver element in the Tree-View in FreeCAD.
# Second, check the configuration below and run the macor to open the
# 'case_t0001.vtu' generated by ElmerSolver with the specified existing
# state-file in Paraview.
# =============================================================================
# CONFIGURATION
# =============================================================================
# Tested on Ubuntu 19.10
optional_path_to_paraview = '' # if empty use the installed paraview;
# e.g. '/home/user/Downloads/ParaView-5.2.0-Qt4-OpenGL2-MPI-Linux-64bit/bin/'
#optional_state_file = 'statefile.pvsm' # has to located next to the .vtu
#optional_state_file = 'statefile.py' # has to located next to the .vtu
# optional_state_file = '' # please use an empty string if not needed
# Windows not yet tested
# =============================================================================
# START MACRO
# =============================================================================
import FreeCADGui
import FreeCAD
from PySide import QtCore, QtGui
from platform import system
import os
import Fem
# Get the selected objects
selection = FreeCADGui.Selection.getSelectionEx()
if len(selection) == 1: # test if only one object is selected
sel = selection[0] #
print('\nSelected Object: ', sel.FullName)
if sel.TypeName == 'Fem::FemSolverObjectPython' and hasattr(sel.Object , 'ElmerOutput'): # test if the selected object is a solver object and has an 'ElmerOutput' attibute
solver = sel
print(solver.Object.FullName)
objectName = solver.ObjectName
filename, file_extension = os.path.splitext(sel.Object.Document.FileName)
head, tail = os.path.split(filename)
resultfile_dir = head + '/' + tail + '/' + objectName
print(os.getcwd())
os.chdir(resultfile_dir)
print(os.getcwd())
file_exists = False
if 'optional_state_file' in locals() and optional_state_file is not "":
state_file_param = resultfile_dir + '/' + optional_state_file
if not os.path.isfile(state_file_param):
FreeCAD.Console.PrintError('Error!! State file could not be found.')
QtGui.QMessageBox.critical(None, 'Error', 'Error!! State file could not be found. Please doublecheck your configuration in the macro file or report the error in the FreeCAD forum.', QtGui.QMessageBox.Abort)
else:
file_exists = True
paraview_command = optional_path_to_paraview + \
'paraview --state="' + state_file_param + '"'
else:
resultfilename_vtk = resultfile_dir + "/case_t0001.vtu"
if not os.path.isfile(resultfilename_vtk):
FreeCAD.Console.PrintError('Error!! Result file could not be found.')
QtGui.QMessageBox.critical(None, 'Error', 'Error!! Result file could not be found. Please doublecheck your configuration in the macro file or report the error in the FreeCAD forum.', QtGui.QMessageBox.Abort)
else:
file_exists = True
paraview_command = optional_path_to_paraview + \
'paraview --data="' + resultfilename_vtk + '"'
if file_exists:
try:
process = QtCore.QProcess()
process.startDetached(paraview_command)
FreeCAD.Console.PrintMessage('Running ' + paraview_command + '\n')
FreeCAD.Console.PrintMessage('Finished '+ paraview_command + '\n')
except:
FreeCAD.Console.PrintError('Error!!, sorry..')
QtGui.QMessageBox.critical(None, 'Error', 'Error!! Sorry. Please report the error in the FreeCAD forum', QtGui.QMessageBox.Abort)
else:
print('Please select a single ElmerSolver object')
QtGui.QMessageBox.information(None, 'Information', 'Please select a single ElmerSolver object', QtGui.QMessageBox.Abort)
else:
print('Please select a single ElmerSolver object')
QtGui.QMessageBox.information(None, 'Information', 'Please select a single ElmerSolver object', QtGui.QMessageBox.Abort)
EDIT: commented state file in macro