directly start Paraview from inside FreeCAD

About the development of the FEM module/workbench.

Moderator: bernd

wmayer
Site Admin
Posts: 16649
Joined: Thu Feb 19, 2009 10:32 am

Re: directly start Paraview from inside FreeCAD

Postby wmayer » Tue Feb 11, 2020 11:35 am

UR_ wrote:
Tue Feb 11, 2020 8:18 am
Please consider, that starting paraview has to be done via .bat file under certain circumstances.
Especially under windows and usage of hires monitors, some additional environment variables are needed.

Code: Select all

set QT_AUTO_SCREEN_SCALE_FACTOR=2
start paraview.exe
exit 
Otherwise started instance will be quiet unusable, because of unrecognizable icons.

That's valid only for version up to 5.7
I've no experience with upcoming version 5.8
You don't have to use .bat files because it won't be platform independent. You can use Qt's QProcess class where with setProcessEnvironment QT_AUTO_SCREEN_SCALE_FACTOR can be set.

https://doc.qt.io/qt-5/qprocess.html#se ... nvironment
HoWil
Posts: 1199
Joined: Sun Jun 14, 2015 7:31 pm
Location: Austria

Re: directly start Paraview from inside FreeCAD

Postby HoWil » Thu Mar 19, 2020 12:07 pm

Just saw that CfdOF can open Paraview from within FC for visualizing the mesh and results (See also the picture in the following tutorial on page 8, https://github.com/opensimsa/opensim/ra ... %20UAV.pdf).
Maybe some code can be reused here for opening general FC-result files as well. I know that CfdOF can use paraview as part of the openfoam-installation but we could also introduce a new settings plane in the FEM-Preferences.
User avatar
bernd
Posts: 10975
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: directly start Paraview from inside FreeCAD

Postby bernd » Thu Mar 19, 2020 2:10 pm

https://github.com/jaheyns/CfdOF/blob/1 ... #L859-L887

Code: Select all

def startParaview(case_path, script_name, consoleMessageFn):
    proc = QtCore.QProcess()
    # If using blueCFD, use paraview supplied
    if getFoamRuntime() == 'BlueCFD':
        paraview_cmd = '{}\\..\\AddOns\\ParaView\\bin\\paraview.exe'.format(getFoamDir())
    else:
        paraview_cmd = "paraview"
    arg = '--script={}'.format(script_name)
    # Otherwise, the command 'paraview' must be in the path. Possibly make path user-settable.
    # Test to see if it exists, as the exception thrown is cryptic on Windows if it doesn't
    if shutil.which(paraview_cmd) is None:
        # If not found, try to run from the OpenFOAM environment, in case a bundled version is available from there
        paraview_cmd = "$(which paraview)"  # 'which' required due to mingw weirdness(?) on Windows
        try:
            consoleMessageFn("Running " + paraview_cmd + " " + arg)
            proc = startFoamApplication([paraview_cmd, arg], case_path, log_name=None)
            consoleMessageFn("Paraview started")
        except QtCore.QProcess.ProcessError:
            consoleMessageFn("Error starting paraview")
    else:
        arg = '--script={}'.format(script_name)
        consoleMessageFn("Running " + paraview_cmd + " " + arg)
        proc.setWorkingDirectory(case_path)
        proc.start(paraview_cmd, [arg])
        if proc.waitForStarted():
            consoleMessageFn("Paraview started")
        else:
            consoleMessageFn("Error starting paraview")
    return proc
RatonLaveur
Posts: 817
Joined: Wed Mar 27, 2019 10:45 am

Re: directly start Paraview from inside FreeCAD

Postby RatonLaveur » Mon Jun 29, 2020 2:28 pm

I am so surprised the discussion about integrating Paraview communication in FC went here in the FEM forum since 2017, when in CfdOF it was implemented already there and then. I'm so glad you guys found it, as I was reading in horror today thinking "oh no, they must be banging their heads for so long when it was already done once quite well!"
HoWil
Posts: 1199
Joined: Sun Jun 14, 2015 7:31 pm
Location: Austria

Re: directly start Paraview from inside FreeCAD

Postby HoWil » Mon Jun 29, 2020 4:03 pm

Last edited by HoWil on Fri Jul 03, 2020 6:25 am, edited 1 time in total.
HoWil
Posts: 1199
Joined: Sun Jun 14, 2015 7:31 pm
Location: Austria

Re: directly start Paraview from inside FreeCAD

Postby HoWil » Tue Jun 30, 2020 8:31 pm

Let me post an old post to a macro here... should be a good starting point.
https://forum.freecadweb.org/viewtopic. ... 47#p151547
HoWil
Posts: 1199
Joined: Sun Jun 14, 2015 7:31 pm
Location: Austria

Re: directly start Paraview from inside FreeCAD

Postby HoWil » Fri Jul 03, 2020 11:08 am

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
HoWil
Posts: 1199
Joined: Sun Jun 14, 2015 7:31 pm
Location: Austria

Re: directly start Paraview from inside FreeCAD

Postby HoWil » Sun Jul 05, 2020 9:44 am

I hope you can imagine whats happening.
A statefile.psvm was created prior to the use of the macro and the "optional_statefile" was registered there.
The 1MB upload is a bit hindering a better video quality.
Peek 2020-07-05 11-30.webm
(382.46 KiB) Downloaded 30 times
:shock:
Attachments
statefile.zip
statefile.pvsm
(19.71 KiB) Downloaded 9 times
Tutorial_nonGUI_6 - Electrostatics - StatElecSolve, ElectricForce - m-based_fromBody.FCStd
Model without mesh and results pipeline
(61.66 KiB) Downloaded 8 times