Skip to content

Instantly share code, notes, and snippets.

@thomas-p-wilson
Created January 1, 2024 20:19
Show Gist options
  • Save thomas-p-wilson/abfd4b7218ea29d32a8766ff2171b710 to your computer and use it in GitHub Desktop.
Save thomas-p-wilson/abfd4b7218ea29d32a8766ff2171b710 to your computer and use it in GitHub Desktop.
Generates a basic bill of materials in freecad
'''
Generates a basic Bill Of Materials in FreeCAD.
Features:
- Generate BOM from selected objects
- If no objects selected, generate BOM from visible objects
- Calculate multiple bodies where Array is present
'''
def filterObjects(o):
return hasattr(o, 'Shape') \
and o.Shape.Solids \
and ( \
o.TypeId == "PartDesign::Body" \
or o.TypeId == "Part::FeaturePython" \
) \
and "Part::FeaturePython" not in [r.TypeId for r in o.InList]
bom = App.ActiveDocument.getObject('BOM')
if bom is None:
bom = App.activeDocument().addObject('Spreadsheet::Sheet','BOM')
bom.clearAll()
# Get objects to generate BOM for
objects = [o for o in Gui.Selection.getSelection() if filterObjects(o)]
if len(objects) == 0:
objects = [o for o in App.ActiveDocument.Objects if filterObjects(o) and o.Visibility]
cell = 0
for obj in objects:
if obj.ViewObject.isVisible():
cell = cell + 1
# Set label
bom.set("A" + str(cell), obj.FullName + " (" + obj.Label + ")")
if obj.TypeId == "PartDesign::AdditiveBox":
bom.set("B" + str(cell), obj.Height.toStr())
bom.set("C" + str(cell), obj.Length.toStr())
bom.set("D" + str(cell), obj.Width.toStr())
elif obj.TypeId == "PartDesign::Body":
bom.set("B" + str(cell), obj.VisibleFeature.Shape.BoundBox.ZMax.__str__())
bom.set("C" + str(cell), obj.VisibleFeature.Shape.BoundBox.XMax.__str__())
bom.set("D" + str(cell), obj.VisibleFeature.Shape.BoundBox.YMax.__str__())
elif obj.TypeId == "Part::FeaturePython" and obj.ArrayType is not None:
bom.set("B" + str(cell), obj.Base.VisibleFeature.Shape.BoundBox.ZMax.__str__())
bom.set("C" + str(cell), obj.Base.VisibleFeature.Shape.BoundBox.XMax.__str__())
bom.set("D" + str(cell), obj.Base.VisibleFeature.Shape.BoundBox.YMax.__str__())
bom.set("E" + str(cell), obj.Count.__str__())
App.ActiveDocument.recompute()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment