Bounding box for .stl mesh

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
umardastgir
Posts: 32
Joined: Mon Apr 22, 2019 9:29 pm

Bounding box for .stl mesh

Post by umardastgir »

I am working on a project to input an .stl mesh and calculate the volume and bounding box using a python script. I believe FreeCAD has a python console that spits out a code for whatever operation someone performs. I turned the bounding box attribute to true and a bounding box appeared with the appropriate dimensions. The following line of code appeared in the console:

Code: Select all

FreeCADGui.getDocument("Unnamed").getObject("out").BoundingBox = True
Although this command displays the bounding box, how can I extract the individual dimensions?
chrisb
Veteran
Posts: 53930
Joined: Tue Mar 17, 2015 9:14 am

Re: Bounding box for .stl mesh

Post by chrisb »

You don't get the BoundingBox from the GUI, use the active document instead, e.g. for a Cube named "Box" I get the BoundingBox with

Code: Select all

App.ActiveDocument.Box.Shape.BoundBox
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
mario52
Veteran
Posts: 4673
Joined: Wed May 16, 2012 2:13 pm

Re: Bounding box for .stl mesh

Post by mario52 »

hi

try Macro_BoundingBox_Tracing

and see Code_snippets#Search_and_data_extraction

Code: Select all

objs = FreeCADGui.Selection.getSelection()                                 # select object with getSelection()

if len(objs) >= 1:
    if hasattr(objs[0], "Shape"):
        s = objs[0].Shape
    elif hasattr(objs[0], "Mesh"):      # upgrade with wmayer thanks #http://forum.freecadweb.org/viewtopic.php?f=13&t=22331
        s = objs[0].Mesh
    elif hasattr(objs[0], "Points"):
        s = objs[0].Points

#sel = FreeCADGui.Selection.getSelection()                                 # select object with getSelection()
#boundBox_= sel[0].Shape.BoundBox                                          # BoundBox of the object
boundBox_= s.BoundBox                                          # BoundBox of the object
App.Console.PrintMessage("boundBox_      : "+str(boundBox_)+"\n")
 
boundBoxLX  = boundBox_.XLength                                           # Length x boundBox rectangle
boundBoxLY  = boundBox_.YLength                                           # Length y boundBox rectangle
boundBoxLZ  = boundBox_.ZLength                                           # Length z boundBox rectangle
boundBoxDiag= boundBox_.DiagonalLength                                    # Diagonal Length boundBox rectangle

App.Console.PrintMessage("boundBoxLX     : "+str(boundBoxLX)+"\n")
App.Console.PrintMessage("boundBoxLY     : "+str(boundBoxLY)+"\n")
App.Console.PrintMessage("boundBoxLZ     : "+str(boundBoxLZ)+"\n")
App.Console.PrintMessage("boundBoxDiag   : "+str(boundBoxDiag)+"\n\n")

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.
umardastgir
Posts: 32
Joined: Mon Apr 22, 2019 9:29 pm

Re: Bounding box for .stl mesh

Post by umardastgir »

Thanks a lot for your response! How do I calculate the volume though?
kisolre
Veteran
Posts: 4163
Joined: Wed Nov 21, 2018 1:13 pm

Re: Bounding box for .stl mesh

Post by kisolre »

umardastgir wrote: Tue May 21, 2019 4:11 pm Thanks a lot for your response! How do I calculate the volume though?
X*Y*Z ?
Or did you mean the volume of the STL? It should first become a solid I think.
umardastgir
Posts: 32
Joined: Mon Apr 22, 2019 9:29 pm

Re: Bounding box for .stl mesh

Post by umardastgir »

Volume of the part (stl mesh). And I also need the surface area.
I have done the following:

Code: Select all

FreeCAD.getDocument("Unnamed").Objects[0].Mesh.Area
FreeCAD.getDocument("Unnamed").Objects[0].Mesh.Volume
And this does give me the area and volume, but is this the right approach?
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Bounding box for .stl mesh

Post by openBrain »

umardastgir wrote: Tue May 21, 2019 5:39 pm Volume of the part (stl mesh). And I also need the surface area.
I have done the following:

Code: Select all

FreeCAD.getDocument("Unnamed").Objects[0].Mesh.Area
FreeCAD.getDocument("Unnamed").Objects[0].Mesh.Volume
And this does give me the area and volume, but is this the right approach?
Seems correct to me. But nothing better than checking to be sure. Just model a 1 m3 cube, export to STL, then reimport and check your macro on it.
schupin
Posts: 476
Joined: Sun Jan 21, 2018 1:43 pm

Re: Bounding box for .stl mesh

Post by schupin »

umardastgir wrote: Tue May 21, 2019 5:39 pm Volume of the part (stl mesh). And I also need the surface area.

Code: Select all

FreeCAD.getDocument("Unnamed").Objects[0].Mesh.Area
FreeCAD.getDocument("Unnamed").Objects[0].Mesh.Volume
I think Mesh.Volume on meshes returns nothing ?
I had to code something by myself to do it on the center of mass macro.

Then, you can use the center of mass macro : https://forum.freecadweb.org/viewtopic. ... 30#p270248
Just select your mesh and press the compute button, it will give you the mass (depending on the density you entered).

If you want the volume, you can :
  • choose en density of 1
  • or export the result in a .csv file (there is button for that). You''ll get the volume in a collumn
Post Reply