Macro to compute center of mass

Show off your FreeCAD projects here!
User avatar
PrzemoF
Posts: 2598
Joined: Fri Jul 25, 2014 4:52 pm
Contact:

Re: Macro to compute center of mass

Postby PrzemoF » Thu Mar 07, 2019 10:23 pm

I changed version of FreeCAD & updated your macro - now works. It was not finding any materials - only custom & default were available, but no calcs were performed. Thanks!
schupin
Posts: 287
Joined: Sun Jan 21, 2018 1:43 pm

Re: Macro to compute center of mass

Postby schupin » Thu Mar 14, 2019 9:50 pm

I just add a new button to automatically change the color of shapes depending on the density indicated :

colorify_1.png
colorify_1.png (161.6 KiB) Viewed 736 times

It might help on big model to identify materials.
(maybe it already exists on the BIM workbench, I didn't look at it...)
Syres
Posts: 286
Joined: Thu Aug 09, 2018 11:14 am

Re: Macro to compute center of mass

Postby Syres » Fri Mar 15, 2019 10:10 am

Thanks for the update and the colours should prove helpful. Just for info I had to change line 27 to:

Code: Select all

__Icon__ = 'https://www.freecadweb.org/wiki/images/4/41/Macro_CenterOfMass.png'
in order to stop this error :

Code: Select all

<unknown exception traceback><class 'SyntaxError'>: ("(unicode error) 'utf-8' codec can't decode byte 0xb3 in position 27: invalid start byte", ('E:/Data/FreeCAD/Macro/CenterOfMass.FCMacro', 126, 4, None))
I'm guessing there's a non-English character in there causing the error.

OS: Windows 7
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.18.16079 (Git)
Build type: Release
Branch: master
Hash: 6363c90a20b296ab69d7b52230009928199d90df
Python version: 3.6.6
Qt version: 5.6.2
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: English/UnitedKingdom (en_GB)
schupin
Posts: 287
Joined: Sun Jan 21, 2018 1:43 pm

Re: Macro to compute center of mass

Postby schupin » Fri Mar 15, 2019 8:46 pm

Thanks for the feedback, I'm always struggling with unicode issues...

I've to check some other things, it seems that it doesn't download icon files while using the addon manager.
schupin
Posts: 287
Joined: Sun Jan 21, 2018 1:43 pm

Re: Macro to compute center of mass

Postby schupin » Wed May 15, 2019 7:50 pm

I looked at the video here : https://www.youtube.com/watch?v=nKZuiw2DVZ8 and tried to do the same exercice using the center of mass macro.

It worked pretty well :
Question5.png
Question5.png (61.23 KiB) Viewed 478 times

I also continue the test through the question 6 and 7 using A2plus :
Question7.png
Question7.png (151.88 KiB) Viewed 478 times

I'm pretty pleased to see that FreeCAD tools are dealing perfectly with this "standard" SW exercice:)
User avatar
hds
Posts: 97
Joined: Tue Feb 14, 2017 8:23 am

Re: Macro to compute center of mass

Postby hds » Tue May 21, 2019 6:38 am

Thanks for this macro and i'am using it currently to get the COM of my VIIC. Two issues i did find though but maybe it is just me beeing to much oldschool and not OO compatible ...

1) There might be a bug within the polar array calculation. The macro does not step through the solids in the array but it occurs to me that it takes the center of rotation as the center of mass which is not necessarily coincident.

Center of mass when calculated on array:
CenterOfMassWrong.png
CenterOfMassWrong.png (126.3 KiB) Viewed 370 times

Center of mass with dicretes solids:
CenterOfMassCorrect.png
CenterOfMassCorrect.png (148.12 KiB) Viewed 370 times


2) With FC 0.19 at least on my screen the height of the output lines and buttons collapses to be barely visible. I changed the calculation of the COM Popup to be in % of screen height and introduced a few global variables including the output height. Not sure if this is elegant or whatever.

CenterOfMassCollapsed.png
CenterOfMassCollapsed.png (145.31 KiB) Viewed 370 times

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)
Attachments
CenterOfMass.FCMacro
(36.07 KiB) Downloaded 6 times
UR_
Posts: 857
Joined: Tue Jan 03, 2017 8:42 pm

Re: Macro to compute center of mass

Postby UR_ » Tue May 21, 2019 7:10 am

hds wrote:
Tue May 21, 2019 6:38 am
... height of the output lines and buttons collapses to be barely visible...
Seems to be a Ubuntu "feature" :)

Annotation 2019-05-21 090429.png
Annotation 2019-05-21 090429.png (60.67 KiB) Viewed 362 times

OS: Windows 10
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.16810 (Git)
Build type: Release
Branch: master
Hash: fc37053210d2abc058c6e14b06d7b8e28aeee8ea
Python version: 3.6.6
Qt version: 5.6.2
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: German/Germany (de_DE)
User avatar
hds
Posts: 97
Joined: Tue Feb 14, 2017 8:23 am

Re: Macro to compute center of mass

Postby hds » Tue May 21, 2019 11:26 am

Around line 300 this patch seems to work. But i'am not 100% certain. May be a professional could have a closer look at it.

Code: Select all

            #2: an array is selected
            if hasattr(sel[sol], 'ArrayType'):
                if sel[sol].ArrayType == 'polar' and sel[sol].NumberPolar > 0:
                    # self.centerofmasses[sol] = sel[sol].Center
                    self.centerofmasses[sol] = FreeCAD.Vector(0,0,0)
                    for hds_sol in sel[sol].Shape.Solids:
                        self.centerofmasses[sol] += hds_sol.CenterOfMass
                        #print(hds_sol.CenterOfMass)
                    self.centerofmasses[sol] /= sel[sol].Shape.Solids.__len__()
                else:
COMpatched.png
COMpatched.png (152.58 KiB) Viewed 325 times
Attachments
COMtest.FCStd
(16.82 KiB) Downloaded 7 times
User avatar
hds
Posts: 97
Joined: Tue Feb 14, 2017 8:23 am

Re: Macro to compute center of mass

Postby hds » 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"):
MultipleArraysClones.png
MultipleArraysClones.png (124.46 KiB) Viewed 304 times
Attachments
AkkuTest.FCStd
(21.73 KiB) Downloaded 7 times
schupin
Posts: 287
Joined: Sun Jan 21, 2018 1:43 pm

Re: Macro to compute center of mass

Postby schupin » Tue May 21, 2019 8:45 pm

Thanks a lot for your corrections HDS !

I hope it will help your boat to float :)

I'll investigate on your graphic bug : I'm on ubuntu (kde), the Freecad-Daily works well.

Code: Select all

OS: Ubuntu 19.04 (KDE/plasma)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.
Build type: Release
Python version: 3.7.3
Qt version: 5.12.2
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: French/France (fr_FR)
Seems you have an older version of Qt than me (but younger than UR_).

I'll take a deeper look.