Macro to compute center of mass

Show off your FreeCAD projects here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
schupin
Posts: 476
Joined: Sun Jan 21, 2018 1:43 pm

Re: Macro to compute center of mass

Post by schupin »

Kunda1 wrote: Wed Feb 16, 2022 1:04 pm @SyProLei That's awesome! Welcome!

Unfortunately this thread became inactive ~2 years ago. Also it's in the User Showcase subforum and we [refer for questions/discussions not to happen too much in this subforum. Feel free to start a new forum thread in the Python Scripting & Macros subforum (then x-post a link to it in this thread so people can follow the discussion)
Indeed it's not very active anymore.
But I start correcting some things 2 weeks ago. I just have to find a little more time to finish it.

I'll be glad if you can be part of this macro @SyProLei.
I think you can fork the macro repository and ask for merge request with your improments.

But, perhaps it might be simplier to "start over" and put all this in a workbench rather than a macro...
SyProLei
Posts: 12
Joined: Tue Feb 15, 2022 9:43 am
Contact:

Re: Macro to compute center of mass

Post by SyProLei »

Dear schupin, dear community!

I am happy to share our proposal for a redesigned macro. It has been completely rebuilt by Saarland University.
I hope the massive changes in code did not introduce regressions for you. At the moment, 28 objects in ArchDetail example are recognised correctly. Beside some objects left, a bug with style of QDoubleSpinBox is yet remaining.
Screenshot.png
Screenshot.png (193.7 KiB) Viewed 5501 times
A Changelog summary:

0.5-preview by SyProLei / 2022-03-11:
New: Code base, Requirement raised to Qt5.12+ and Python3 (FreeCAD 0.19)
New: Show mass of each solid
New: Window is docked by default (you can find a switch in code)
Improved: FreeCAD alike Gui look and feel
Improved: Scaled color palette (from green to red) to colorify shapes
Improved: Visualising the displacement of centre of mass to the geometry
Improved: Internal tracking of units
Fix: Handling of groups and group objects
Fix: Replaced deprecated Qt class

We will test the tool with different use cases in the next weeks. Please let me know if your case is not covered any more or anything not working correctly.

Attachment removed because the final version is available
Last edited by SyProLei on Thu Apr 21, 2022 5:03 pm, edited 1 time in total.
We are part of the technology transfer program for lightweight engineering funded by German Federal Ministry for Economic Affairs and Climate Action.
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: Macro to compute center of mass

Post by Kunda1 »

This looks awesome!
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
User avatar
Zolko
Veteran
Posts: 2213
Joined: Mon Dec 17, 2018 10:02 am

Re: Macro to compute center of mass

Post by Zolko »

SyProLei wrote: Fri Mar 11, 2022 11:46 am I am happy to share our proposal for a redesigned macro. It has been completely rebuilt by Saarland University.
This is most welcome. I didn't yet test it, but may I suggest a cosmetic change: could you make the file begin with (or equivalent):

Code: Select all

#!/usr/bin/env python
# coding: utf-8
# 
# Compute and show the center of mass for multiple solids
#
...
try the Assembly4 workbench for FreCAD — tutorials here and here
User avatar
Zolko
Veteran
Posts: 2213
Joined: Mon Dec 17, 2018 10:02 am

Re: Macro to compute center of mass

Post by Zolko »

SyProLei wrote: Fri Mar 11, 2022 11:46 am Please let me know if your case is not covered any more or anything not working correctly.
the macro doesn't follow App::Link : if the selected object has links to other objects, those are not detected. I tried to add:

Code: Select all

if s_.TypeId == 'App::Part' or s_.TypeId == 'App::Link':
in the function validSelection() but that doesn't change anything. I checked that an App::Link does have a Shape and Shape.Volume, and that they're calculated correctly

Also, I think that a standalone window would be easier to see than an embedded panel. Also, the window takes a long time to load and for a while nothing happens (I guess that it's because of the parsing of the function validSelection() ). From a user feedback perspective, it would be nicer to load the window, even empty, and then launch the calculation.
try the Assembly4 workbench for FreCAD — tutorials here and here
User avatar
ppemawm
Veteran
Posts: 1240
Joined: Fri May 17, 2013 3:54 pm
Location: New York NY USA

Re: Macro to compute center of mass

Post by ppemawm »

Zolko wrote: Mon Mar 14, 2022 3:07 pm I think that a standalone window would be easier to see than an embedded panel. it would be nicer to load the window, even empty, and then launch the calculation.
+1
Thank you for the updates.
It would also be helpful if you are able to minimize the window.
"It is a poor workman who blames his tools..." ;)
edwilliams16
Veteran
Posts: 3108
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Macro to compute center of mass

Post by edwilliams16 »

getGlobalPlacement() doesn't work properly with links. @realthunder tells us to use https://forum.freecadweb.org/viewtopic. ... 83#p569083

Code: Select all

for sel in Gui.Selection.getSelectionEx('',0):
  for path in sel.SubElementNames if sel.SubElementNames else ['']:
    placement = sel.Object.getSubObject(path, retType=3) # retType 3 returns the placement. Check the doc string for more info
    print('%s.%s -- %s' % (sel.Object.Name, path, placement))

Alternatively https://github.com/FreeCAD/FreeCAD-macr ... nt.FCMacro
SyProLei
Posts: 12
Joined: Tue Feb 15, 2022 9:43 am
Contact:

Re: Macro to compute center of mass

Post by SyProLei »

@schupin, @Zolko, @ppemawm, @edwilliams16 Thank you for your replies!
You can find the updated version here:
https://github.com/s-quirin/CenterOfMassFCMacro
@schupin is just doing the merge to the repository

You can now set your prefered setting for the window in the Parameter Editor.
Zolko wrote: Mon Mar 14, 2022 3:07 pm the macro doesn't follow App::Link : if the selected object has links to other objects, those are not detected.
App:Link lacks the .getGlobalPlacement() method and childShapes() do not equal, so .InListRecursive and .OutList is now used for that as a workaround. Need to investigate the proposed solution @realthunder for future work.
Zolko wrote: Mon Mar 14, 2022 3:07 pm the window takes a long time to load and for a while nothing happens (I guess that it's because of the parsing of the function validSelection() )
The processing time is intrinsic, caused by getting .Volume and .CenterOfMass. I placed a FreeCAD.Base.ProgressIndicator() around it for user feedback.

Additional improvements:
New: Selection matches up tree view or is sorted by name (can be set in Parameter Editor)
New: Option to save densities in document

Enjoy!
We are part of the technology transfer program for lightweight engineering funded by German Federal Ministry for Economic Affairs and Climate Action.
mario52
Veteran
Posts: 4673
Joined: Wed May 16, 2012 2:13 pm

Re: Macro to compute center of mass

Post by mario52 »

Hi

@SyProLei

Code: Select all

__Author__ = 'chupins, s-quirin'
you make one error on author : chupins instead schupin

https://github.com/s-quirin/CenterOfMas ... ss.FCMacro

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.
SyProLei
Posts: 12
Joined: Tue Feb 15, 2022 9:43 am
Contact:

Re: Macro to compute center of mass

Post by SyProLei »

@mario52
In our case, Github and Forum User names are not the same.
We are part of the technology transfer program for lightweight engineering funded by German Federal Ministry for Economic Affairs and Climate Action.
Post Reply