Macro to compute center of mass

Show off your FreeCAD projects here!
Syres
Posts: 819
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 33 times
hds
Posts: 114
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 1141 times
Attachments
CenterOfMass.FCMacro
(37.21 KiB) Downloaded 46 times
Test_CofM_Arrays.FCStd
(257.62 KiB) Downloaded 29 times
Syres
Posts: 819
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: 819
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: 416
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: 416
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
hds
Posts: 114
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)
sante76
Posts: 3
Joined: Thu Apr 23, 2020 5:15 pm

Re: Macro to compute center of mass

Postby sante76 » Thu Apr 23, 2020 5:43 pm

Hi all.
I'm new here: new into the forum and new to FreeCad. I'm writing because I've troubles with the CenterOfMass macro.
My system is a Windows 10 and FreeCad says me he is running a "Python 3.6.6 | packaged by conda-forge | ".

Every time I try to run the macro, I have back this error:

Code: Select all

<unknown exception traceback><class 'IndentationError'>: ('unexpected indent', ('C:/Users/Gianluca/AppData/Roaming/FreeCAD/Macro/CenterOfMass.FCMacro', 56, 5, "     sys.setdefaultencoding('utf8')\n"))
I've tried the macro on a simple, default cube, nothing of special... Can someone help me, please?

Thank you all.
schupin
Posts: 416
Joined: Sun Jan 21, 2018 1:43 pm

Re: Macro to compute center of mass

Postby schupin » Fri Apr 24, 2020 11:13 am

sante76 wrote:
Thu Apr 23, 2020 5:43 pm

Code: Select all

<unknown exception traceback><class 'IndentationError'>: ('unexpected indent', ('C:/Users/Gianluca/AppData/Roaming/FreeCAD/Macro/CenterOfMass.FCMacro', 56, 5, "     sys.setdefaultencoding('utf8')\n"))
Hi,

It look like there is a problem on line 56 :
- can you check there is 4 "space character" before "sys.setdefaultencoding"
- did you install the macro with the addon manager ?
- which version number of the macro is writen on line 22 ?

For information, on this version of FreeCAD, it works for me :

Code: Select all

OS: Windows 10 (10.0)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.20415 (Git)
Build type: Release
Branch: master
Hash: 7a0606715045a255ef0774e835b0091eef300ac9
Python version: 3.8.2
Qt version: 5.12.5
Coin version: 4.0.0
OCC version: 7.4.0
Locale: French/France (fr_FR)
sante76
Posts: 3
Joined: Thu Apr 23, 2020 5:15 pm

Re: Macro to compute center of mass

Postby sante76 » Fri Apr 24, 2020 1:33 pm

schupin wrote:
Fri Apr 24, 2020 11:13 am
...
Hi,

It look like there is a problem on line 56 :
- can you check there is 4 "space character" before "sys.setdefaultencoding"
- did you install the macro with the addon manager ?
- which version number of the macro is writen on line 22 ?
...
Hi Shupin, nice to hear you.

I've checked the code. There are exactly four spaces before the "sys.setdefaultencoding" and the version on macro at line 22 is '0.4.2'.
I've dowloaded the macro using the addon manager and I've exploded the icons zip file in the same folder of the macro, in their own folder.

This are some other error I've see into the report:

Code: Select all

C:\Program Files\FreeCAD 0.18\bin\lib\site-packages\matplotlib\__init__.py:908: MatplotlibDeprecationWarning: The backend.qt4 rcParam was deprecated in version 2.2.  In order to force the use of a specific Qt binding, either import that binding first, or set the QT_API environment variable.
  mplDeprecation)
<unknown exception traceback><class 'SyntaxError'>: ("(unicode error) 'utf-8' codec can't decode byte 0xb3 in position 27: invalid start byte", ('C:/Users/Gianluca/AppData/Roaming/FreeCAD/Macro/CenterOfMass.FCMacro', 137, 4, None))
I've no knowledge about Qt libraries. Really I don't know if they are installed with the FreeCad or they was there before... So I'll not supriserd if the problem starts there...

Thank you for your interesting.

Information on my system follows:

Code: Select all

OS: Windows 10
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.18.4 (GitTag)
Build type: Release
Branch: releases/FreeCAD-0-18
Hash: 980bf9060e28555fecd9e3462f68ca74007b70f8
Python version: 3.6.6
Qt version: 5.6.2
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: Italian/Italy (it_IT)