FreeCAD as pre-post processor for MBDyn

About the development of the FEM module/workbench.

Moderator: bernd

toni_lv
Posts: 11
Joined: Tue Nov 03, 2020 8:10 pm
Location: Spain

Re: FreeCAD as pre-post processor for MBDyn

Post by toni_lv »

carno wrote: Mon Mar 21, 2022 4:36 am Thanks JamesLiu.

I just downloaded 0.2 and checked. This error vanished and new error started. I will check further.

For information, the error is:

Code: Select all

Running the Python command 'MBdyn_AddStaticBody' failed:
Traceback (most recent call last):
  File "C:\Program Files\FreeCAD 0.20\Mod\freecad-mbdyn-dynamics-workbench-master\MBdynGui.py", line 1856, in Activated
    dyn.AddStaticBody(b[0])            
  File "C:\Program Files\FreeCAD 0.20\Mod\freecad-mbdyn-dynamics-workbench-master\dynamics.py", line 504, in AddStaticBody
    Dummybody(a, baseBody, baseBody.Label)
  File "C:\Program Files\FreeCAD 0.20\Mod\freecad-mbdyn-dynamics-workbench-master\dummybody.py", line 56, in __init__
    obj.addProperty("App::PropertyDistance","absolute center of mass X","Absolute center of mass","absolute center of mass X",1).absolute_center_of_mass_X = cmx

Invalid property name 'absolute center of mass X'
Addition of 'Rigid bodies' works fine.

Is this something related to Windows-10? I am on windows-10.

Thanks again ..
I am not sure but have you tried running the simulation with a PartDesign object or an imported STEP file? Maybe it is because using Part workbench object without solid geometry.

Hope it helps
pierreG92
Posts: 2
Joined: Sat Jul 02, 2022 2:49 am

Re: FreeCAD as pre-post processor for MBDyn

Post by pierreG92 »

Hello Carno,

Did you manage to find any solution to your issue? I am facing the same problem as you, with the same configuration and with the same error messages (on the same actions).
I've also tried with part design, STEP files, and I've done the same in ubuntu, always with the same resulting error.

I have also didn't notice any update by josegegas unfortunately.
I'm sure we are not far from solving this little bug but I can't move forward now.

Thanks for any help
pierreG92
Posts: 2
Joined: Sat Jul 02, 2022 2:49 am

Re: FreeCAD as pre-post processor for MBDyn

Post by pierreG92 »

In case anyone is still trying to solve this problem, I found a solution (I am currently using FreeCAD on windows 11).
You just need to go on line 56 of "dummybody.py" (path indicated in the error message) and write "absolute_center_of_mass_X" instead of "absolute center of mass X".
You should do the same for "absolute center of mass Y", "absolute center of mass Z" and in general for all the properties that have spaces in their name. Once you did that, you should close FreeCAD and restart it, and it will work. I corrected myself all the .py files that give an error and it is always the same cause, so it's pretty straightforward.
Some files are working (I guess they have already been corrected by someone), some aren't, but you will discover which they are by trying to use all of the workbench buttons in FreeCAD.

Hope this help
Rolf2010
Posts: 1
Joined: Tue Jul 19, 2022 7:00 pm

Re: FreeCAD as pre-post processor for MBDyn

Post by Rolf2010 »

Dear All,
I hope that all be ok with you.
I will try to reproduce "the simple pendulum" MBDYN exercise that Jose posted in the Youtube.
During that I get some errors, that I could correct them.
But when I try to add "Static node" (related file "structuralstaticnode.py") I have the following message:

12:10:52 Running the Python command 'MBdyn_AddStructuralStatic' failed:
Traceback (most recent call last):
File "D:\01_Softwares\FreeCAD-0.20.1-WIN-x64-portable-1\Mod\freecad-mbdyn-dynamics-workbench-master\MBdynGui.py", line 1742, in Activated
dyn.AddStructuralStaticNode(baseBody, referenceObject, referenceGeometry)
File "D:\01_Softwares\FreeCAD-0.20.1-WIN-x64-portable-1\Mod\freecad-mbdyn-dynamics-workbench-master\dynamics.py", line 538, in AddStructuralStaticNode
Structuralstaticnode(a,baseBody, referenceObject, referenceGeometry)
File "D:\01_Softwares\FreeCAD-0.20.1-WIN-x64-portable-1\Mod\freecad-mbdyn-dynamics-workbench-master\structuralstaticnode.py", line 72, in __init__
obj.addProperty("App::PropertySpeed","absolute velocity X","Initial conditions: absolute velocity","absolute velocity X")

Invalid property name 'absolute velocity X'


Can somebody knows which is the right way to declare it property???
Thanks in advance for your attention,
Regards

testing on:
Freecad 0.20.1
Windows7
vm4dim
Posts: 129
Joined: Tue Nov 23, 2021 1:05 am

Re: FreeCAD as pre-post processor for MBDyn

Post by vm4dim »

Rolf2010 wrote: Wed Aug 31, 2022 3:19 pm Invalid property name 'absolute velocity X'
Try set Boolean config BaseApp/Preferences/Document/AutoNameDynamicProperty to True.
https://wiki.freecad.org/Fine-tuning
josegegas
Posts: 241
Joined: Sat Feb 11, 2017 12:54 am
Location: New Zealand

Re: FreeCAD as pre-post processor for MBDyn

Post by josegegas »

Hello all.

I am trying to addapt PartDesign::CoordinateSystem to be used as a node (a scripted object) class for a new version of my workbench. This is the basic code:

Code: Select all

class Structuraldynamicnode: 
    def __init__(self, obj, baseBody):                              
        obj.addProperty(... 
        some long code here
        
    def execute(self, fp):        
        fp.absolute_position_X = ... 
        some more code here

Then, to create the object I do:

Code: Select all

node = FreeCAD.ActiveDocument.addObject("PartDesign::CoordinateSystem", "structural_1")
body = FreeCADGui.Selection.getSelection()[0]
Structuraldynamicnode (node, body)"
App.activeDocument().recompute(None,True,True)
It works perfectly, but there is one problem. The execute method is not executed on recomputation. I understand PartDesign::CoordinateSystem already has an execute method? How can I solve this?

Also, how can I add a "onDocumentRestored(self, fp)" method? Will it execute?

Thanks in advance!!
josegegas
Posts: 241
Joined: Sat Feb 11, 2017 12:54 am
Location: New Zealand

Re: FreeCAD as pre-post processor for MBDyn

Post by josegegas »

I am working on a new version of the “dynamics workbench”, applying several changes I have had in mind for a while now. In fact, I consider the currently available code only a proof of concept. A successful proof of concept, but only a proof of concept anyway. It is time to move to something more refined, and the first thing to do, in my opinion, is to use FreeCAD local coordinate systems as nodes and joints for MBDyn. The purpose of this post is to share what I have done so far, and hopefully hear your opinions.

There are two main flaws in the current version of the current workbench: 1) When I programed it I assumed all the nodes are orthonormal to the global reference frame. 2) The code is unnecessarily long because it does not make much use of the tools FreeCAD has to handle vectors and matrices.

I would like to mention the approach I am taking to solve these flaws:

The main thing is, as mentioned, to use FreeCAD LCSs as dynamic nodes and joints. Let me talk about nodes firs. In MBDyn, to define a dynamic node, one has to provide at least four parameters that define the initial conditions of the node: initial position, initial orientation, initial velocity and initial angular velocity. Of these, initial velocity and initial angular velocity present no complications, because these two are defined by the user. But, in order to integrate FreeCAD and MBDyn, the initial position and the initial orientation must be connected to the CAD geometry, because the user will want to place and orient the nodes at points and orientation of interest of the CAD assembly or mechanism to be simulated. This is one of the reasons to use LCSs as nodes. There are already several mechanisms to position and orient LCSs, by providing references to components (faces, edges) of the CAD geometry. This is my class for a structural dynamic node:

Code: Select all

import FreeCAD

class Structuraldynamicnode:  
    def __init__(self, obj, baseBody):  
        obj.addProperty("App::PropertyDistance","absolute_position_X","Initial conditions: absolute position",'The X component of the absolute position of the node. To access this variable type "Px_" followed by the number of node, for instance "Px_1".',1)
        obj.addProperty("App::PropertyDistance","absolute_position_Y","Initial conditions: absolute position",'The Y component of the absolute position of the node. To access this variable type "Px_" followed by the number of node, for instance "Py_1".',1)
        obj.addProperty("App::PropertyDistance","absolute_position_Z","Initial conditions: absolute position",'The Z component of the absolute position of the node. To access this variable type "Px_" followed by the number of node, for instance "Pz_1".',1)
        
        obj.addProperty("App::PropertyString","absolute_orientation_matrix","Initial conditions: absolute orientation","Orientation of the node in the absolute frame. The absolute orientation matrix is automatically computed from the global placement.",1)           
        
        obj.addProperty("App::PropertySpeed","absolute_velocity_X","Initial conditions: absolute velocity",'To access this variable type "Vx_" followed by the number of node, for instance "Vx_1".')
        obj.addProperty("App::PropertySpeed","absolute_velocity_Y","Initial conditions: absolute velocity",'To access this variable type "Vy_" followed by the number of node, for instance "Vy_1".')
        obj.addProperty("App::PropertySpeed","absolute_velocity_Z","Initial conditions: absolute velocity",'To access this variable type "Vz_" followed by the number of node, for instance "Vz_1".')
        
        obj.addProperty("App::PropertyQuantity","absolute_angular_velocity_X","Initial conditions: absolute angular velocity",'To access this variable type "Wx_" followed by the number of node, for instance "Wx_1".')
        obj.absolute_angular_velocity_X = FreeCAD.Units.Unit('deg/s') 
        obj.addProperty("App::PropertyQuantity","absolute_angular_velocity_Y","Initial conditions: absolute angular velocity",'To access this variable type "Wy_" followed by the number of node, for instance "Wy_1".')
        obj.absolute_angular_velocity_Y = FreeCAD.Units.Unit('deg/s') 
        obj.addProperty("App::PropertyQuantity","absolute_angular_velocity_Z","Initial conditions: absolute angular velocity",'To access this variable type "Wz_" followed by the number of node, for instance "Wz_1".')
        obj.absolute_angular_velocity_Z = FreeCAD.Units.Unit('deg/s') 
                
    def execute(self, fp):
        return

    def onDocumentRestored(self, fp):
        fp.absolute_angular_velocity_X = FreeCAD.Units.Unit('deg/s')
        fp.absolute_angular_velocity_Y = FreeCAD.Units.Unit('deg/s')
        fp.absolute_angular_velocity_Z = FreeCAD.Units.Unit('deg/s') 
Here, absolute_velocity_X, Y and Z; and absolute_angular_velocity_X, Y, and Z; are initialized with a value of 0.0, and the user simply types other value if needed. The absolute_position_X, Y and Z; and the absolute_orientation_matrix must be obtained from the placement of the LCS that represents the node, but before, I create the node as follows:

Code: Select all

node = FreeCAD.ActiveDocument.addObject("PartDesign::CoordinateSystem", "label")
Structuraldynamicnode (node, baseBody)
Where label is simply a unique label automatically generated to identify the node, for example “structural_1”, “structural_2”, “structural_3”, etc. baseBody is the rigid body to which the node is connected. I am quite happy with how rigid bodies work on the current version of the workbench, so not planning to do many changes here.

And here comes the first problem. The “onDocumentRestored” method is never executed when FreeCAD is started, and I would appreciate some help on why.

After the node has been created, it is basically a local coordinate system with some added properties, and whose placement is:

Code: Select all

Placement [Pos=(0,00), Yaw-Pitch-Roll=(0,0,0)]
The user now positions the node using the tools FreeCAD already has to position a LCS, or some other positioning mechanisms I have added; and the initial absolute position and absolute orientation matrix are obtained, from the LCS placement, as follows:

Code: Select all

absolute_position_X = node.placement.Matrix.A14
absolute_position_Y = node.placement.Matrix.A24
absolute_position_Z = node.placement.Matrix.A34

absolute_orientation_matrix = "matr, " + str(node.placement.Matrix.A11) + ", " + str(node.placement.Matrix.A12) + ", " + str(node.placement.Matrix.A13) + ", " + str(node.placement.Matrix.A21) + ", " + str(node.placement.Matrix.A22) + ", " + str(node.placement.Matrix.A23) + ", " + str(node.placement.Matrix.A31) + ", " + str(node.placement.Matrix.A32) + ", " + str(node.placement.Matrix.A33)
This is so the position and orientation match the format needed by MBDyn:

Code: Select all

<additional_args> ::= , { static | dynamic | modal } ,
[ position , ] (Vec3) <absolute_position> ,
[ orientation , ] (OrientationMatrix) <absolute_orientation_matrix> ,
[ velocity , ] (Vec3) <absolute_velocity> ,
[ angular velocity , ] (Vec3) <absolute_angular_velocity>
Where Vec3 is simply a vector, exactly the same as in FreeCAD, and orientation is the general-case orientation matrix, as explained in:

https://www.sky-engin.jp/en/MBDynTutori ... hap13.html

Except for the problem that the onDocumentRestored method is not executed when FreeCAD is loaded, the above approach to model a node using a LCS seems to work just fine, but anyway I would love to hear what people think, and any ideas on how to improve this part of the project will be much appreciated.
T-Garnier
Posts: 18
Joined: Sat Jun 27, 2020 5:30 am

Re: FreeCAD as pre-post processor for MBDyn

Post by T-Garnier »

Hi,

Try adding self.Object = obj or obj.Proxy = self in the _init__ function as per documentation recommandation.

https://wiki.freecadweb.org/Scripted_ob ... attributes
https://wiki.freecad.org/FeaturePython_methods
josegegas
Posts: 241
Joined: Sat Feb 11, 2017 12:54 am
Location: New Zealand

Re: FreeCAD as pre-post processor for MBDyn

Post by josegegas »

T-Garnier wrote: Tue Sep 20, 2022 3:47 am Hi,

Try adding self.Object = obj or obj.Proxy = self in the _init__ function as per documentation recommandation.

https://wiki.freecadweb.org/Scripted_ob ... attributes
https://wiki.freecad.org/FeaturePython_methods
Hi.

I have tried this before. Adding self.Object = obj or obj.Proxy = self in the _init__ function used to work well when using "Part::FeaturePython" to create the scripted object. When I use self.Object = obj with an object of "PartDesign::CoordinateSystem" I do not get any error, but the execute and the onDocumentRestored never get executed. When I use obj.Proxy = self with an object of "PartDesign::CoordinateSystem" the execute and the onDocumentRestored are never executed either, and I get the next error:

'Part.Feature' object has no attribute 'Proxy'

Code: Select all

OS: Windows 10 Version 2009
Word size of FreeCAD: 64-bit
Version: 0.21.29936 (Git)
Build type: Release
Branch: master
Hash: 3db79c907cd7ef2f75597efdd82ae288ac23ad01
Python 3.10.5, Qt 5.15.4, Coin 4.0.0, Vtk 9.1.0, OCC 7.6.2
Locale: Spanish/Ecuador (es_EC)
Installed mods: 
  * Assembly4
  * Curves 0.3.0
  * fasteners 0.3.50
  * fcgear
Another way to solve this would be if there was a method that gets executed every time the workbench is loaded?
T-Garnier
Posts: 18
Joined: Sat Jun 27, 2020 5:30 am

Re: FreeCAD as pre-post processor for MBDyn

Post by T-Garnier »

Not all the class can be subclassed with python.
Did you try to create a Part::FeaturePython + your coordinatesystem object and then link the coordinate to the FeaturePython object ? It's been a long time since I the freecad python api so I could be wrong.

https://wiki.freecadweb.org/FeaturePyth ... opertyLink
Post Reply