Macro to compute center of mass

Show off your FreeCAD projects here!
Syres
Posts: 286
Joined: Thu Aug 09, 2018 11:14 am

Re: Macro to compute center of mass

Postby Syres » Wed May 22, 2019 8:27 am

hds wrote:
Tue May 21, 2019 1:28 pm
just found that for multiple arrays or clones thereof there needs to be an additional patch around line 276:

Code: Select all

            # Find the center of mass depending of the type of object.
            if hasattr(objs[sol], "CenterOfMass"):
                self.centerofmasses[sol] = objs[sol].CenterOfMass
            elif hasattr(objs[sol], "Solids"):
                    # self.centerofmasses[sol] = objs[sol].Solids[0].CenterOfMass
                    self.centerofmasses[sol] = FreeCAD.Vector(0,0,0)
                    for hds_sol in objs[sol].Solids:
                        self.centerofmasses[sol] += hds_sol.CenterOfMass
                    self.centerofmasses[sol] /= objs[sol].Solids.__len__()
            #estimate cdg of a mesh
            elif hasattr(objs[sol],"Points"):
Could you please test the attached file, not sure whether I incorrectly edited my macro file or I've found a 'special' case that produces an incorrect result.
Attachments
Test_CofM_Arrays.FCStd
(263.84 KiB) Downloaded 8 times
User avatar
hds
Posts: 97
Joined: Tue Feb 14, 2017 8:23 am

Re: Macro to compute center of mass

Postby hds » Wed May 22, 2019 9:18 am

Ball of steel award here you go. The output looks good to me. i've attached my macro if you want to play with. For production pls wait for shupins patched version.
BallOfSteel.png
BallOfSteel.png (209.78 KiB) Viewed 422 times
Attachments
CenterOfMass.FCMacro
(37.21 KiB) Downloaded 13 times
Test_CofM_Arrays.FCStd
(257.62 KiB) Downloaded 6 times
Syres
Posts: 286
Joined: Thu Aug 09, 2018 11:14 am

Re: Macro to compute center of mass

Postby Syres » Wed May 22, 2019 10:19 am

hds wrote:
Wed May 22, 2019 9:18 am
Ball of steel award here you go. The output looks good to me. i've attached my macro if you want to play with. For production pls wait for shupins patched version.
Thanks for the efforts and swift feedback.
Syres
Posts: 286
Joined: Thu Aug 09, 2018 11:14 am

Re: Macro to compute center of mass

Postby Syres » Wed May 22, 2019 10:43 am

schupin wrote:
Tue May 21, 2019 8:45 pm
Personalised macro locations
@schupin, something to bear in mind is the handling of custom macro folder locations (with regard to displaying icons on buttons), there's a couple of lines with:

Code: Select all

        macro_dir = FreeCAD.getUserMacroDir()
the first one I changed to:

Code: Select all

        macro_dir = FreeCAD.getUserMacroDir(True)
and commented out the second one as it seemed redundant. By all means give it a test but I believe this is how the AddonManager can handle the default macro locations on all platforms and also those of us who have changed the location to one of our choice.
schupin
Posts: 287
Joined: Sun Jan 21, 2018 1:43 pm

Re: Macro to compute center of mass

Postby schupin » Wed May 22, 2019 5:41 pm

hds wrote:
Wed May 22, 2019 9:18 am
For production pls wait for shupins patched version.
A new version has been pushed, you can get it through the addon manager.
I just add @syres and @hds modifications.

Feel free to test it and come back if you find anything !
(especially @syres modification because I just tried on one computer but not anywhere else)

Next things I'll do :
  • make the macro work when you're using part containers (by now it doesn't work if you move your container)
  • add a "set default density" button
  • add a button to allow users to change the value of the sphere radius
schupin
Posts: 287
Joined: Sun Jan 21, 2018 1:43 pm

Re: Macro to compute center of mass

Postby schupin » Sat May 25, 2019 1:43 pm

New "0.4.0" version is online.
(update is available through the addon manager)

Big part of modifications are coming from @hds proposals :
  • improvement: the center of mass of a solid put in a moved part container is well computed
  • add a cursor to modify the radius of spheres
  • add a spinbox to set the default density value
  • change the window managment to make the window expendable
User avatar
hds
Posts: 97
Joined: Tue Feb 14, 2017 8:23 am

Re: Macro to compute center of mass

Postby hds » Sun May 26, 2019 1:55 pm

thanks for all the work in it.
Some observations with my kubuntu/FC combo:

1) Icon/Box Layout still fixed and collapses to not beeing visible. As far as i can see you did introduce some global vars to set the icon and box sizes but you still use fixed numbers at the actual code lines.

2) When checking the default density box the logic is a bit broken. After you've made a first change then you need at minimum a double click afterwards for upgrading the fields. Sometimes even a recalculation with mouse button "Compute COM" is necessary.

3) The slider for the sphere size does only change the resulting sphere. The other body speheres are changed only when recomputing "Compute COM".

All in all very good job ! Well done.

FreeCAD latest version updated 26th of May.
-------------------------------------------
OS: Ubuntu 18.04.2 LTS (XFCE/xubuntu)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.
Build type: Release
Python version: 3.6.7
Qt version: 5.9.5
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: German/Germany (de_DE)