Mass and Volume object properties.

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
edwilliams16
Veteran
Posts: 3191
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Mass and Volume object properties.

Post by edwilliams16 »

Where does density get set?
If I do

Code: Select all

 ### Begin command Std_SendToPythonConsole
>>> obj = App.getDocument("findMOItest").getObject("Box002")
>>> ### End command Std_SendToPythonConsole
>>> obj.Shape.Mass
6.0
>>> obj.Shape.Volume
6.0
There is an implied density of 1 kg/mm^3 (Internal units kg/mm/s/degree -- I think) which is of course unreal. Are the Mass and Volume properties defined equal, or can the object's density be set somewhere?
User avatar
onekk
Veteran
Posts: 6222
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Mass and Volume object properties.

Post by onekk »

I can't find any Mass, property of Part.Shape at least in
OS: Artix Linux (openbox)
Word size of FreeCAD: 64-bit
Version: 0.20.26683 (Git) AppImage
Build type: Release
Branch: (HEAD detached at 0388fbc)
Hash: 0388fbc98d49d874fb341b9037a743bc691d501f
Python version: 3.9.7
Qt version: 5.12.9
Coin version: 4.0.0
OCC version: 7.5.3
Locale: Italian/Italy (it_IT)
There is a Part.Solid.Mass property, that seems to be derived from TopoShape, but some search in sources (a quick search) lead to nothing more than refer to a property:

https://github.com/FreeCAD/FreeCAD/blob ... ePyImp.cpp

Code: Select all

Py::Float TopoShapePy::getLength(void) const
{
    const TopoDS_Shape& shape = getTopoShapePtr()->getShape();
    if (shape.IsNull())
        throw Py::RuntimeError("shape is invalid");
    GProp_GProps props;
    BRepGProp::LinearProperties(shape, props);
    return Py::Float(props.Mass());
}

Py::Float TopoShapePy::getArea(void) const
{
    const TopoDS_Shape& shape = getTopoShapePtr()->getShape();
    if (shape.IsNull())
        throw Py::RuntimeError("shape is invalid");
    GProp_GProps props;
    BRepGProp::SurfaceProperties(shape, props);
    return Py::Float(props.Mass());
}

Py::Float TopoShapePy::getVolume(void) const
{
    const TopoDS_Shape& shape = getTopoShapePtr()->getShape();
    if (shape.IsNull())
        throw Py::RuntimeError("shape is invalid");
    GProp_GProps props;
    BRepGProp::VolumeProperties(shape, props);
    return Py::Float(props.Mass());
}
And seems to the the same info returned for the three functions, even if derived from different BRepGProp porperties.

Maybe poking @wmayer that is the most autoritathive low level developer that could tell something, or maybe see if something is told on OCCT documentation.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
TheMarkster
Veteran
Posts: 5513
Joined: Thu Apr 05, 2018 1:53 am

Re: Mass and Volume object properties.

Post by TheMarkster »

There is no Density property I am aware of in FreeCAD. I suppose one could be added where Mass and Volume are defined. Then Mass becomes Volume * Density. The problem is when you start mixing objects of differing densities in boolean operations it becomes complicated to figure out what Density to assign to the boolean result. For example, suppose you have a cylinder of density = 2 and a cube of density = 4 (imagine the cylinder is some light aluminum alloy and the cube is some heavier steel alloy) and you do a boolean union. Which density does the fusion object get? A simple weighted average might work there. Now fillet all the edges. Some material is added in some places, some removed in some places, and it's different densities and we have really no idea which edges were density = 4 and which were density = 2 and which were common to both. Suppose the user then pockets a hole through the cylinder. What will be the density of the new pocket object? Well, we cut through the cylinder, so therefore, the material removed was density = 2, but how do we know the hole was through the cylinder? All FreeCAD would know is the fusion object had a hole cut through it, and not whether the hole went through the lighter cylinder or the heavier cube or through bits of each.
User avatar
onekk
Veteran
Posts: 6222
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Mass and Volume object properties.

Post by onekk »

From what is seems Mass and Volume seems to share same definition, but to be sure it involve to dig some more deeep in sources.

Ok for the Boolean Operation consideration.

Sol_Vol = Solid Volume
To_Vol = cut or fuse object Volume
S_D = Solid Volume Density
To_D = To_Vol Density

But if you subtract one solid to another, the mass is simply subtracted to the base solid, so the operation would be simply:

Code: Select all

(Solid_Vol - To_Vol) * S_D
For a boolean fuse:

Code: Select all

So_Vol * S_D + To_Vol * To_D 
For other operation, you have to what part of the resulting volume are belonging to the original solid.

The only problem I could not think a viable solution is a "common" operation, what Density you would use?

Or I'm forgotting some boolean operation "Part Section" is resembling "Common" but from the images it seems to extract wires from the original shape.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
TheMarkster
Veteran
Posts: 5513
Joined: Thu Apr 05, 2018 1:53 am

Re: Mass and Volume object properties.

Post by TheMarkster »

Your fusion solution is what I had in mind with a weighted average. With cuts, yes, we would only need to inherit the base density. For common it could be sensible to do a simple average since there will be equal volumes of both objects. XOR is basically just fuse first, then common, then cut the common from the fusion. It seems workable then for one single level of inheritance, where it can be determined the property density to use for a boolean result object. But still the issue remains for dealing with subsequent operations. Simply carrying forward the density determined for the boolean result is not going to produce accurate results in all cases because it will depend on which density is being removed and which is being augmented.

One alternative could be to have no inheritance ever, but only the Density property that defaults back to 1.0 for each new object, and it is up to the user to calculate and supply the proper Density. But this is something the user can already do, so it would be pointless to code it.

Or perhaps there could be a boolean property declaring whether the Density is inheritable. For example, for a cut it would be, but for a fusion of different Densities it would not be. If the Density is not inheritable then in the result object the Density gets set back to the default of 1.0. The problem with this is the user will not be expecting it and may rely upon an incorrect calculation.

I think the best thing to do is just not support a Density property until such time as OCCT incorporates it into their library. But even then it could be necessary for OCCT to rely upon FreeCAD to pass in the density as a parameter for each operation, so we're back at the same problem of figuring out which parts of the fusion were of which density.
edwilliams16
Veteran
Posts: 3191
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Mass and Volume object properties.

Post by edwilliams16 »

I more had in mind an assembly of objects, each with its own density, rather than the complications of Boolean combinations of different materials. To do meaningful dynamical calculations, this would would be minimal. Using the animator workbench to show the motion of an assembly according to Newton's laws would be entertaining - but clearly one would have to assign one's own density and mass properties, ignoring FreeCAD's. What does the FEM workbench do?

Another anomaly: If you select a cube, mass and volume are (numerically) equal. If you select an edge of that cube, volume is zero (good), but the mass is (numerically) equal to the length! Select a vertex, volume is zero (good), but mass is undefined! It is hard to make much sense of the mass property.
TheMarkster
Veteran
Posts: 5513
Joined: Thu Apr 05, 2018 1:53 am

Re: Mass and Volume object properties.

Post by TheMarkster »

You could add a dynamic property called Density to your object, this would be App::PropertyFloat, set to 1.0. Then add a property called Mass to the object of type App::PropertyVolume. Set an expression to automatically calculate the Mass. Select the object, Ctrl+Shift+P and enter into the python console:

Code: Select all

obj.addProperty("App::PropertyFloat","Density")
obj.Density = 1.0
obj.addProperty("App::PropertyVolume","Mass")
obj.setExpression("Mass",obj.Name+".Shape.Volume * "+obj.Name+".Density")
User avatar
onekk
Veteran
Posts: 6222
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Mass and Volume object properties.

Post by onekk »

edwilliams16 wrote: Wed Dec 29, 2021 8:34 pm Another anomaly: If you select a cube, mass and volume are (numerically) equal. If you select an edge of that cube, volume is zero (good), but the mass is (numerically) equal to the length! Select a vertex, volume is zero (good), but mass is undefined! It is hard to make much sense of the mass property.

In fact if you see the code above, strangely you have a getLength that is returning:

Code: Select all

 return Py::Float(props.Mass());
But as said more deep analysis is difficult, at least from me.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
grd
Posts: 328
Joined: Wed Apr 13, 2022 5:13 am
Location: Eindhoven, The Netherlands

Re: Mass and Volume object properties.

Post by grd »

Sorry for re-opening this old post, but...
TheMarkster wrote: Mon Dec 27, 2021 9:18 pm There is no Density property I am aware of in FreeCAD. I suppose one could be added where Mass and Volume are defined. Then Mass becomes Volume * Density. The problem is when you start mixing objects of differing densities in boolean operations it becomes complicated to figure out what Density to assign to the boolean result. For example, suppose you have a cylinder of density = 2 and a cube of density = 4 (imagine the cylinder is some light aluminum alloy and the cube is some heavier steel alloy) and you do a boolean union. Which density does the fusion object get? A simple weighted average might work there. Now fillet all the edges. Some material is added in some places, some removed in some places, and it's different densities and we have really no idea which edges were density = 4 and which were density = 2 and which were common to both. Suppose the user then pockets a hole through the cylinder. What will be the density of the new pocket object? Well, we cut through the cylinder, so therefore, the material removed was density = 2, but how do we know the hole was through the cylinder? All FreeCAD would know is the fusion object had a hole cut through it, and not whether the hole went through the lighter cylinder or the heavier cube or through bits of each.
Sorry, but this does not make sense to me. I have created dozens of (mostly sheet metal) parts that were welded together. But this then becomes an assembly. It doesn't make sense to have one file with bodies that have different density and you combine those two to make a fusion. That belongs in an assembly IMHO.

And this counts too for steel bearings cramped inside aluminum molds. That belongs in an assembly too. And after that you created the assembly you should be able to cut holes into that assembly for instance or cut it to length. But you can't increase the volume. You should only be able to cut, or bend or form or whatever.

That is how I look at the situation. In every commercial solid modeller, that I know, this is possible.

So that is why I think that density of a file should be possible, and this density should count for every body or part inside that file. You should only be able to put in one density, one material. IMHO this doesn't -- only -- belong inside the Arch wb or FEM wb.

This standardization decreases the calculation time drastically, because you can't make a mistake any more after you put in a material.

Do you guys agree? Or did I make a mistake. I am still new to FreeCAD.
About Nim. Latest Release 2.0.2. Here is Nim in 100 seconds and a Nim package. There are Qt and OCCT packages.
chrisb
Veteran
Posts: 54273
Joined: Tue Mar 17, 2015 9:14 am

Re: Mass and Volume object properties.

Post by chrisb »

grd wrote: Sun May 08, 2022 2:07 pm Do you guys agree?
You are most probably right as far as bodies or primitives are concerned. You may be right as far as Booleans are concerned. But you are probably wrong when it comes to compounds and you are definitely wrong when it comes to multiple objects inside of a Part container, leave alone a whole file.
Or did I make a mistake.
Please quote only what you directly refer to. That keeps topics shorter and improves legibility. Those who want to see more can easily follow the up-arrow link.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
Post Reply