Help wanted. Python script for second moment of area

Post here if you have a FreeCAD-related job to offer to the FreeCAD community. This can include programming or modeling.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
ajarivas72
Posts: 8
Joined: Sun Sep 11, 2016 10:35 pm

Help wanted. Python script for second moment of area

Post by ajarivas72 »

Hello

I am looking for a Python script that computes the second moment of area of a surface.

I need the second moment of area to compute the bending stress with the formula:

S = M * y / Iyy

where

M = bending moment
y : distance from the center of area
Iyy: second moment of area

The quantity I need is Iyy

Example:

For a rectangle of height "h" and base "b", the moment of area with respect to the center of mass is:

Iyy = b * h^3 / 12

The code must compute the Iyy correctly having a surface as an input to the script. The programmer must show several examples proving the correctness of her/his code and send the price to pay for her/his code.

We must agree to price, delivery time, and all the pertinent details.

Best regards.

Alejandro
UR_
Veteran
Posts: 1354
Joined: Tue Jan 03, 2017 8:42 pm

Re: Help wanted. Python script for second moment of area

Post by UR_ »

This is really not a big task.
Please see this quick'n'dirty example. It's for free :lol:


Macro:

Code: Select all

import math

obj = App.getDocument("test_face").getObject("DWire")
shp = App.getDocument("test_face").getObject("DWire").Shape


MoI = shp.MatrixOfInertia
Ixx = MoI.A11
Iyy = MoI.A22
Izz = MoI.A33


Area = shp.Area
CoM = shp.CenterOfMass


Ix = shp.StaticMoments[0]
Iy = shp.StaticMoments[1]
Iz = shp.StaticMoments[2]


Ixxtrans = Ixx + Area * pow(CoM.x, 2.)
Iyytrans = Iyy + Area * pow(CoM.y, 2.)
Izztrans = Izz + Area * pow(CoM.z, 2.)



print "area ", Area
print "center ", CoM.x, " ", CoM.y, " ", CoM.z
print "staticmoments ", Ix, " ", Iy, " ", Iz
print "Inertia about [Gx,Gy,Gz] ", Ixx, " ", Iyy, " ", Izz
print "Inertia about [0,0,0] ", Ixxtrans, " ", Iyytrans, " ", Izztrans

Inertia.FCMacro
(718 Bytes) Downloaded 169 times
screenshot translated circle.png
screenshot translated circle.png (60.2 KiB) Viewed 5844 times
tree.png
tree.png (4.84 KiB) Viewed 5844 times

File:
test face.FCStd
(5.12 KiB) Downloaded 131 times

Done with:
OS: Windows 10
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.17.13005 (Git)
Build type: Release
Branch: master
Hash: 29533320fc514029f3ce1af6bbf53cc03fb93049
Python version: 2.7.8
Qt version: 4.8.7
Coin version: 4.0.0a
OCC version: 7.1.0
Locale: German/Germany (de_DE)
mario52
Veteran
Posts: 4673
Joined: Wed May 16, 2012 2:13 pm

Re: Help wanted. Python script for second moment of area

Post by mario52 »

hi UR_

same result with Macro_FCInfo

interesting your macro

i replace your two first line with (for use directly with object selected)

Code: Select all

#obj = App.getDocument("test_face").getObject("DWire")
#shp = App.getDocument("test_face").getObject("DWire").Shape

shp = FreeCADGui.Selection.getSelection()[0].Shape

I can add these code lines to my macro ?

Code: Select all

print "staticmoments ", Ix, " ", Iy, " ", Iz
print "Inertia about [0,0,0] ", Ixxtrans, " ", Iyytrans, " ", Izztrans
mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
ajarivas72
Posts: 8
Joined: Sun Sep 11, 2016 10:35 pm

Re: Help wanted. Python script for second moment of area

Post by ajarivas72 »

UR_ and mario52

I thank you very much.
ajarivas72
Posts: 8
Joined: Sun Sep 11, 2016 10:35 pm

Re: Help wanted. Python script for second moment of area

Post by ajarivas72 »

UR_

Hello.

How do you create DWire object from the Sketch in your file ?
chrisb
Veteran
Posts: 53919
Joined: Tue Mar 17, 2015 9:14 am

Re: Help wanted. Python script for second moment of area

Post by chrisb »

In Draft workbench is a tool DraftToSketch which converts in both directions depending on the selected input.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
ajarivas72
Posts: 8
Joined: Sun Sep 11, 2016 10:35 pm

Re: Help wanted. Python script for second moment of area

Post by ajarivas72 »

chrisb

Thanks a lot. It worked beautifully
chrisb
Veteran
Posts: 53919
Joined: Tue Mar 17, 2015 9:14 am

Re: Help wanted. Python script for second moment of area

Post by chrisb »

ajarivas72 wrote: Sun Jan 28, 2018 9:00 am Thanks a lot. It worked beautifully
You're welcome. I use it usually in the other direction to convert from svg files to sketches.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
lukassturm
Posts: 5
Joined: Fri Mar 02, 2018 3:18 pm
Location: Darmstadt/Germany

Re: Help wanted. Python script for second moment of area

Post by lukassturm »

Hello forum, I am still really new to FC but I already spent lots of time using it.
Specifically I spent lots of hours on the problem of computing the 2nd moment of area (german: Flächenträgheitsmoment [unit: m^4] ).


I want to design an extruded aluminum profile for minimum weight and maximum bending stiffness.
The cross section will be far away from a combination of simple shapes, so I cannot calculate it by hand.
But I want to use the tool from _UR to compute Ixx.

My workflow becomes problematic towards the end:
Sketcher:
I design only one half of the cross section. I will try to attach this file.
Draft:
I use Draft2Sketch to get a DWire from the sketch.
Then I mirror the DWire at the x-axis (horizontally).
Then I use the upgrade tool to get one whole DWire (upper half and its mirrored lower half). But this does not work for me. I guess I do something wrong in the way I select parts during the upgrade procedure.

My goal: Have the whole profile cross section as DWire and use the script from UR_ to compute Ixx.

For the very simple profile (only its upper half is sketched) the result calculated by hand is Ixx=266cm^4.

Can somebody tell me what I am doing wrong?

OS: Windows 10
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.17.13340 (Git)
Build type: Release
Branch: master
Hash: 29864ff82173e501e220e754ccfe845b53990908
Python version: 2.7.14
Qt version: 4.8.7
Coin version: 4.0.0a
OCC version: 7.1.0
Locale: German/Germany (de_DE)
Attachments
Profil_200x10.FCStd
(7.57 KiB) Downloaded 101 times
ajarivas72
Posts: 8
Joined: Sun Sep 11, 2016 10:35 pm

Re: Help wanted. Python script for second moment of area

Post by ajarivas72 »

Hallo lukassturm

I did not see your message until today.

Tomorrow I will give you a look to your question.

Best regards

Auf Wiederhören.

Alejandro
Post Reply