FreeCAD as pre-post processor for MBDyn

About the development of the FEM module/workbench.

Moderator: bernd

Post Reply
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: FreeCAD as pre-post procesor for MBDyn

Post by Kunda1 »

josegegas wrote: Sat Oct 05, 2019 9:13 am Continuing with this project, found a problem. Can't import Pandas (import pandas as pd) from the FreeCAD python console... What could the solution be?
What's the error ?
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
josegegas
Posts: 241
Joined: Sat Feb 11, 2017 12:54 am
Location: New Zealand

Re: FreeCAD as pre-post procesor for MBDyn

Post by josegegas »

[/quote]
What's the error ?
[/quote]

ImportError: No module named pandas. I'm sure pandas is installed aand works fine cause I don't have any issue using it from elsewhere than FreeCAD
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: FreeCAD as pre-post procesor for MBDyn

Post by vocx »

josegegas wrote: Sat Oct 05, 2019 1:35 pm ImportError: No module named pandas. I'm sure pandas is installed aand works fine cause I don't have any issue using it from elsewhere than FreeCAD
How did you install pandas? If you installed it through pip or some virtual Python environment, it's possible the base FreeCAD system doesn't find it.

Usually for these big packages it's best to install it through the package manager, for example,

Code: Select all

sudo apt install python3-pandas
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
josegegas
Posts: 241
Joined: Sat Feb 11, 2017 12:54 am
Location: New Zealand

Re: FreeCAD as pre-post procesor for MBDyn

Post by josegegas »

vocx wrote: Wed Sep 11, 2019 4:22 pm
mfasano wrote: Wed Sep 11, 2019 1:39 pm From the video it looks like you open shell and run a command.
Maybe that was a prototype. I presume jose is planning on setting this up from a single button press, just like the FEM Workbench does.

In any case, yes, Python can run arbitrary programs. The very old way of doing it, is using

Code: Select all

os.system("mbdyn -options input.file")
But the recommended way nowadays is with the subprocess module.

Code: Select all

import subprocess
subprocess.run(["mbdyn", "-options", "input.file"], capture_output=True)
os.system was working nicely but when I do:

subprocess.run(["mbdyn", "-f",__dir__+"/MBDyn/MBDynCase.mbd"])

It just doesn't work, don't really know why. However when I do:

subprocess.call(["mbdyn", "-f",__dir__+"/MBDyn/MBDynCase.mbd"])

It executes MBDyn well, bit I'd like to capture the MBDyn output and show it in FreeCAD gui, any ideas?
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: FreeCAD as pre-post procesor for MBDyn

Post by vocx »

josegegas wrote: Sun Oct 06, 2019 3:05 am It just doesn't work, don't really know why. However when I do:
"Just doesn't work" is not a good description of the problem. Please provide more information.

The subprocess functions have varied over the life of Python 3, so run() is not available before Python 3.5, but call() is. Maybe this is the problem?

Otherwise, check the different options in the manual.
It executes MBDyn well, bit I'd like to capture the MBDyn output and show it in FreeCAD gui, any ideas?
In this very thread we already told you to check the FEM code. In that case, they use Popen() which is a bit of a lower level call. Both run() and call() essentially use Popen().
vocx wrote: Thu Sep 12, 2019 12:23 am

Code: Select all

out = subprocess.run(["ls", "-l", "/usr/share/freecad"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf8")
print(out.stdout)
It seems FEM uses Popen(). https://github.com/berndhahnebach/FreeC ... ks.py#L105

This is the internal class, while the high level functions are run() (for Python 3.5 and above), or the older call(), check_call(), and check_output() (for Python 3.4 and below).

Code: Select all

process = subprocess.Popen(["ls", "-l", "/usr/share/freecad"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf8")
out, err = process.communicate()
print(out)
print(err)
The standard output or standard error then can be printed in the terminal, the FreeCAD console, or used in some Qt dialogs.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
josegegas
Posts: 241
Joined: Sat Feb 11, 2017 12:54 am
Location: New Zealand

Re: FreeCAD as pre-post procesor for MBDyn

Post by josegegas »

Hi all.

Solved the problem of calculating moments of inertia for complex bodies!! Now simulations can be done with any body/assembly. Have a look at my first try:

https://www.youtube.com/watch?v=BN07ZRm ... e=youtu.be

I know you've told me to have a loon at other workbenches, but I don't get the point of telling me where the answer is, if you probably already have the answer in mind and can save me time, time I can invest in getting this workbench done instead of reading someone else's code... So here goes the question: how do I put the messages MBDyn generates in the FreeCAD gui? Here's how I'm executing MBDyn:

process = subprocess.Popen(["mbdyn", "-f", __dir__+"/MBDyn/MBDynCase.mbd"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
print(out)

Cheers.
josegegas
Posts: 241
Joined: Sat Feb 11, 2017 12:54 am
Location: New Zealand

Re: FreeCAD as pre-post procesor for MBDyn

Post by josegegas »

Here's another video, with the reaction force vector and the data plotted for both, the nodes (bodies) and the joints:

https://www.youtube.com/watch?v=75jERiv ... e=youtu.be
User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: FreeCAD as pre-post procesor for MBDyn

Post by bernd »

We just try to attrac your attention to FEM solver code. In this code all (most) probplem you have with your subprocess code are solved allready and thre are smart Task panels and python API commands already. You just need to add a new FEM solver object and add the FEM solver task and writer modules and could use all atvantages the FEM solver system has.

Eventually it is up to you to write you own solver code.

I try to avoid coding with subprocess because I have run in to trouble a few times (like you). Thus I am not of a big help if it comes to subprocess. Happily the FEM solver framework which uses subprocess was done by someone else.
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: FreeCAD as pre-post procesor for MBDyn

Post by vocx »

josegegas wrote: Mon Oct 07, 2019 8:48 am .... So here goes the question: how do I put the messages MBDyn generates in the FreeCAD gui?
Well, this is a separate question. Your first question was how to get the output. Can you do this now? Okay. Now, how do we put this output inside a graphical interface? That depends. What type of interface? A message box, a task panel, a text editor? There are various ways. I think it would be good to put it in a task panel, just like FEM does.

Code: Select all

output="""
A bunch of output here.
A bunch of output here.
A bunch of output here.
A bunch of output here.
A bunch of output here.
A bunch of output here.
A bunch of output here.
A bunch of output here."""

import PySide
Text = PySide.QtGui.QTextEdit()
Text.setText(output)
Text.setFont(PySide.QtGui.QFont("Mono"))
Text.show()
By the way, when you are building paths, it's best to use Python's os.path functions. I would also avoid variables with double underscores as these are typically reserved for special internal properties.

Code: Select all

__dir__ + "/MBDyn/MBDynCase.mbd"

os.path.join(__dir__, "MBDyn/MBDynCase.mbd")
Last edited by vocx on Wed Oct 09, 2019 1:35 am, edited 1 time in total.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
Post Reply