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 »

UR_ wrote: Sat Nov 17, 2018 12:25 pm I had to do something like this, to get it to work

CenterOfMassUR.FCMacro

EDIT: Perhaps for reference: https://forum.freecadweb.org/viewtopic. ... 62#p267686
Thanks for your "fix".
I'll see how I can make it work everywhere (will certainly do the same thing as you)
UR_
Veteran
Posts: 1354
Joined: Tue Jan 03, 2017 8:42 pm

Re: Macro to compute center of mass

Post by UR_ »

mario52 wrote: Tue Nov 13, 2018 3:49 pm done i create the wiki page Macro_CenterOfMass
wiki page says under Script section
Script

icon file download and paste in the same directory of the macro
But this didn't work for me
Instead i had to use this layout

│ CenterOfMass.FCMacro

├───CenterOfMass
│ com.png
│ compute.png
│ import.png
│ material.png
│ save.png

└───__pycache__
schupin
Posts: 476
Joined: Sun Jan 21, 2018 1:43 pm

Re: Macro to compute center of mass

Post by schupin »

You're right.

The location of the icons moved when it arrives on github.
I'll correct the wiki.

Also, in your version the macro looks for the cdg.png file (not the com.png). It have been modified but not pulled yet.
schupin
Posts: 476
Joined: Sun Jan 21, 2018 1:43 pm

Re: Macro to compute center of mass

Post by schupin »

I add the support of meshes.

Now, you can select meshes and/or parts (from part design or part WB) and it will give you the total weight and center of mass.
balanceMeshes.png
balanceMeshes.png (74.8 KiB) Viewed 3924 times
I also did some bug corrections and change a little the layout management.

Updates are available through the addon manager
triplus
Veteran
Posts: 9471
Joined: Mon Dec 12, 2011 4:45 pm

Re: Macro to compute center of mass

Post by triplus »

Nice!
kisolre
Veteran
Posts: 4159
Joined: Wed Nov 21, 2018 1:13 pm

Re: Macro to compute center of mass

Post by kisolre »

I found this macro and tried to use it but I get

Code: Select all

Traceback (most recent call last):
  File "C:/Users/Wivern/AppData/Roaming/FreeCAD/Macro/CenterOfMass.FCMacro", line 47, in <module>
    reload(sys)
<class 'NameError'>: name 'reload' is not defined
So I did some digging and found that in Python3 there is a change where "reload" is located. I imported importlib, changed reload to importlib.reload bur now the sys module is broken.

Code: Select all

Traceback (most recent call last):
  File "C:/Users/Wivern/AppData/Roaming/FreeCAD/Macro/CenterOfMass.FCMacro", line 49, in <module>
    sys.setdefaultencoding('utf8')
<class 'AttributeError'>: module 'sys' has no attribute 'setdefaultencoding'


Should I just use the regular version and not the Conda/Py3 builds if I use the macros? Is it generaly hard to make the macro work with Py2 and Py3?

OS: Windows 8.1
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.18.15639 (Git)
Build type: Release
Branch: master
Hash: 3f51e25b26ed19a2b17e218c8a0d438eb2e13b26
Python version: 3.6.6
Qt version: 5.6.2
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: Bulgarian/Bulgaria (bg_BG)
schupin
Posts: 476
Joined: Sun Jan 21, 2018 1:43 pm

Re: Macro to compute center of mass

Post by schupin »

kisolre wrote: Sun Jan 20, 2019 10:58 am Should I just use the regular version and not the Conda/Py3 builds if I use the macros? Is it generaly hard to make the macro work with Py2 and Py3?
I don't have tried it on the conda version yet. So I guess you're right pointing a compatibility problem.
If you can use the Py2 version, it should work...

I'll try to correct it work as soon as possible.
schupin
Posts: 476
Joined: Sun Jan 21, 2018 1:43 pm

Re: Macro to compute center of mass

Post by schupin »

kisolre wrote: Sun Jan 20, 2019 10:58 am Should I just use the regular version and not the Conda/Py3 builds if I use the macros? Is it generaly hard to make the macro work with Py2 and Py3?
I finally update the macro and for me it's working under python 3.
You can find the last version here : https://github.com/chupins/FreeCAD-macr ... ss.FCMacro (or through an upgrade in the addon manager)
User avatar
PrzemoF
Veteran
Posts: 3515
Joined: Fri Jul 25, 2014 4:52 pm
Contact:

Re: Macro to compute center of mass

Post by PrzemoF »

Did you test it under python2 as well or it's python3 only now? I can't make it work.
schupin
Posts: 476
Joined: Sun Jan 21, 2018 1:43 pm

Re: Macro to compute center of mass

Post by schupin »

PrzemoF wrote: Thu Mar 07, 2019 7:33 pm Did you test it under python2 as well or it's python3 only now? I can't make it work.
With

OS: macOS High Sierra (10.13)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.18.14976 (Git)
Build type: Release
Branch: (HEAD detached at e79a19828)
Hash: e79a19828e2e92269ff96110dfeadaa245ad17c2
Python version: 2.7.15
Qt version: 5.11.2
Coin version: 4.0.0a
OCC version: 7.2.0
Locale: French/France (fr_FR)

I've got an issue while loading a .csv file but everything else works.

I'm looking to solve it.

with open(filename, 'r', encoding="utf-8") as csvfile: doesn't work under python2
(should use with open(filename, 'r') as csvfile:).
But then, I've got another error :

Code: Select all

    reader = csv.DictReader(csvfile, delimiter='\t')
  File "/Applications/FreeCAD.app/Contents/lib/python2.7/csv.py", line 79, in __init__
    self.reader = reader(f, dialect, *args, **kwds)
TypeError: "delimiter" must be string, not unicode
I'm investigating all changes I might have done...

EDIT : I have something that is working
replacing

Code: Select all

            with open(filename, 'r', encoding="utf-8") as csvfile:
by

Code: Select all

            with open(filename, 'r') as csvfile:
                reader = csv.DictReader(csvfile, delimiter=str('\t').encode('utf-8'))
I'm writing it to make it work automatically for both python2 and 3 and push it.

I didn't asked : Was that your problem ?

EDIT 2 : modification pushed (available in the addon manager)
Post Reply