What's new

Part Weight in Title Blocks

Getting the part weight into the title block seems to have been a problem for a while now. Like this post from 2006. My current solution is to add a 1-line BOM with just the weight. I made a BOM template for it, but you can't hide the top row. If you delete the "Weight" in the top row, Alibre will replace it the next time you update, but you can fill it with an empty space to stop that. Once the BOM is in the drawing, I position it and change the line pattern to Dot (2x). I end up with a few scattered dots on the print, but I can live with those. There's probably a way to make a line type with 0wt, but I haven't figured it out yet.

Does anyone have any better ways to do this that are a bit more automatic? like an AHK script for placing the BOM and changing the line pattern? or a script to set the weight to a custom part property?

Thanks!
 

idslk

Alibre Super User
Ok :)

You can create a custom property.

1677180710847.png

Run a little script which fills the part weight into this property.
Python:
try:
  enviroment = CurrentPart()
except:
  enviroment = CurrentAssembly()
print 'Part weight:',enviroment.Name,enviroment.Mass
enviroment._Part.DesignProperties.CustomProperty('Weight',enviroment.Mass)

you may have to adjust the mass units using a factor in the Equation "gewicht" as i have a german installation...

Save the part

Create a drawing template with a Field
1677180977827.png
save it as a template.
if you use this template to create a new drawing, the field will be updated with the weight.
1677182256488.png


Regards
Stefan
 
Oh, that's perfect. Thanks @idslk! It looks like Alibre stores the mass in kg regardless of your part or assemblies units, so *2.205 gets me to lbs.

Everything makes sense except the script. It isn't putting the value into the custom property. Any idea what I'm missing?

1677186614796.png
 

idslk

Alibre Super User
please try this:
Python:
try:
  enviroment = CurrentPart()
except:
  enviroment = CurrentAssembly()
print 'Part weight:',enviroment.Name,enviroment.Mass*2.205
enviroment._Part.DesignProperties.CustomProperty('Weight',str(enviroment.Mass*2.205))

Regards
Stefan
 
@idslk

That did the trick. I also added [:4] to truncate the number a bit.
str(environment.Mass*2.205)[:4]

Thanks for the help, that was quick! What other handy bits of code have you been hiding from us? :) Has Alibre made a way to run scripts from a toolbar button rather than having to launch the alibre script window, and then run the script from there?
 

albie0803

Alibre Super User
More on topic: I would like it if Alibre auto calculated the weight of an object on save when a material has been set in a part.
Assemblies could then be auto summed for weight on save with a warning if any components have a zero weight, such as imported items.
In the case of imported items, such as bearings etc, the weight could be entered manually.
 
Looks like I jumped the gun on calling this a success. It doesn't work in Assemblies.

Part weight: New Assembly (1)Traceback (most recent call last):
File "<string>", line 5, in <module>
AttributeError: 'Assembly' object has no attribute 'Mass'

please try this:
Python:
try:
  enviroment = CurrentPart()
except:
  enviroment = CurrentAssembly()
print 'Part weight:',enviroment.Name,enviroment.Mass*2.205
enviroment._Part.DesignProperties.CustomProperty('Weight',str(enviroment.Mass*2.205))

Regards
Stefan
 

DavidJ

Administrator
Staff member
I thought this sounded familiar - I did something similar in response to a customer request some while ago. I used the custom property 'MassForDrawing' (a string) - the treatment of units might be helpful [EDIT, see comments later, the treatment of units doesn't work correctly ]- other than that it does similar things to Stefan's scripts. This script was written before it was so simple to extract properties using script, so could be simplified now.

Python:
unitDef = {
  "AD_KILOGRAMS" : 'kg',
  "AD_GRAMS" :  'g',
  "AD_POUNDMASS" :  'lb'
  }

MyPart = CurrentPart()
partAPI = MyPart._Part
Weight = MyPart.Mass
strWeight = str(Weight)

unit = partAPI.DesignProperties.MassUnits

strOutput = strWeight + ' ' + unitDef[str(unit)]
MyPart.SetCustomProperty('MassForDrawing', strOutput)


You could fairly easily sum all the part weights in a script (not forgetting any sub-assemblies) by using the construct from


to step through all the parts for the current assembly and then assign the summed result to your custom property.

Of course you can also attach a BoM to the assembly drawing, and use the summation tools in BoM to give the total (you can hide all rows except total if you choose). That avoids using script at all.
 
Last edited:

idslk

Alibre Super User
It doesn't work in Assemblies.
Sorry, it was intially written from me in early 2020. At this time Assemblies had their own mass...
It looks like Alibre stores the mass in kg regardless of your part or assemblies units
thats my observation too :)
i've edited my skript yesterday, but it was too late for me to post it...
Python:
def WalkTroughPartsinAssembly(assembly):
  global massensumme
  for p in assembly.Parts:
    massensumme = massensumme + p.Mass
  for sa in assembly.SubAssemblies:
    WalkTroughPartsinAssembly(sa)

#unit_compensation_factor = 2.205 # kg to lbm
unit_compensation_factor = 1 # kg

try:
  enviroment = CurrentPart()
  cusprop = str(enviroment.Mass)*unit_compensation_factor
  enviroment._Part.DesignProperties.CustomProperty('Weight',cusprop)
  print 'Part weight:',enviroment.Name,cusprop

except:
  enviroment = CurrentAssembly()
  massensumme = 0.0
  WalkTroughPartsinAssembly(enviroment)
  cusprop = str(massensumme)*unit_compensation_factor
  enviroment._Assembly.DesignProperties.CustomProperty('Weight',cusprop)
  print 'Assembly weight:',enviroment.Name,cusprop

Regards
Stefan
 
Last edited:

idslk

Alibre Super User
the treatment of units might be helpful
as Nate stated, the designproperty units are not reflected in the part.mass.
So if you change the part units in part properties and run the script with different units you will get the same numbers...
hint: If you use the inspect/physical properties from the ribbon it will respect the units...

Regards
Stefan
 

DavidJ

Administrator
Staff member
Stefan,
You are correct - now I don't know if my script was not tested thoroughly enough when I wrote it, or if something changed subsequently to break it. If I went to the trouble of adding the units treatment, I think I would have done some testing at the time (but I don't have any records to confirm one way or another).
 
hey Stefan @idslk

It looks like something changed in v27. It's started to include the weight of sub-assemblies, and as a bonus, it does it incorrectly. I've uninstalled v26 and can't test it again though.

Here's what I was using:

Python:
# Weight Update
# Updates custom property 'Weight' (text) of Assemblies and Parts
# Works with Sub-Assemblies, but does not update the Sub-Assembly's weight

def AssemblyWeight(assem):
  global assywt
  for P in assem.Parts:
    pwt = P.Mass
    assywt = assywt + pwt
    pwt = round(pwt*K,2)
    pwt = str(pwt) + ' lbs'
    if Values[0] == True:
      P.SetCustomProperty('Weight', pwt)
    print P.Name , '=', pwt
   
  for SA in assem.SubAssemblies:
    AssemblyWeight(SA)

Win = Windows()
K = 2.205

try:
  prt = CurrentPart()
  prtwt = round(prt.Mass*K,2)
  prtwt = str(prtwt) + ' lbs'
  prt.SetCustomProperty('Weight', prtwt)
  print prt.Name, '=' ,prtwt

except:
  Values = []
  Options = []

  Options.append(['Update Part Weight',WindowsInputTypes.Boolean, True])
  Options.append(['Update Assembly Weight',WindowsInputTypes.Boolean, True])
  Values = Win.OptionsDialog('Alibre Weight Updater', Options, 220)

  if Values == None:
    print 'Canceled'
    sys.exit()
     
  if Values[0] == False and Values[1] == False:
    print 'You want me to do nothing? Can do!'
    sys.exit()
 
  assywt = 0
  assy = CurrentAssembly()
  if Values[0] == True:
    print '\nUpdating Part Weight:'
  else:
    print '\nCalculating Assembly Weight:'
  AssemblyWeight(assy)
  assywt = round(assywt*K,2)
  assywt = str(assywt) + ' lbs'
  if Values[1] == True:
    assy.SetCustomProperty('Weight', assywt)
    print '\nUpdating Assembly Weight:'
  else:
    print '\nCalculating Assembly Weight:'
  print assy.Name, '=', assywt

And here's what I would get for a small assembly containing a sub-assembly:

Code:
Calculating Assembly Weight:
part1<1> = 5.65 lbs
subasy1<1> = 68.93 lbs
part2<1> = 0.25 lbs
part3<1> = 0.01 lbs
part3<2> = 0.01 lbs
subasy1part1<1> = 0.37 lbs
subasy1part2<1> = 0.5 lbs

Updating Assembly Weight:
assembly1 = 75.72 lbs

subasy1 contains subasy1part1 and subasy1part2, so it should total .87 lbs. Any thoughts on how to remedy this? Since it's calculating the SA weight incorrectly, I'm guessing the best course is to skip over the SA when looping through assem.Parts, but I'm unsure of how to accomplish that. Thanks.
 
As far as I can tell it doesn't print out any subassembly weights. Maybe you have a part accidentally named subasy1?
I recreated the subassembly, and now it's working as intended. I'm not sure what I was missing yesterday, so I guess I'll chalk it up to user error.
 
Top