Skip to content

Instantly share code, notes, and snippets.

@terabyte128
Last active May 12, 2021 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terabyte128/c1c1dccb08fa13a55e7af840a3766372 to your computer and use it in GitHub Desktop.
Save terabyte128/c1c1dccb08fa13a55e7af840a3766372 to your computer and use it in GitHub Desktop.
Generate cut list for 2x4s in Fusion 360.
#! /usr/local/bin/python3
# assumes the the BOM has been generated from https://github.com/macmanpb/CSV-BOM
# in 360: install plugin, "create" menu > create BOM, defaults should be fine
import csv
import argparse
boards = []
parser = argparse.ArgumentParser(description='Find cuts to make in boards based on given dimensions')
parser.add_argument('file', type=str, help='file from which to read board lengths')
parser.add_argument('length', type=int, help='length of board you will buy (inches)')
parser.add_argument('--outfile', type=str, help='CSV file to write cuts to, if desired')
args = parser.parse_args()
BOARD_LENGTH = args.length # inches
with open(args.file) as f:
reader = csv.reader(f)
next(reader)
for row in reader:
name = row[0]
dims = sorted([ float(i) for i in row[2:5] ])
if dims[0] == 1.5 and dims[1] == 3.5:
print("counting", name, dims)
boards.append((name, dims[2], dims))
else:
print("skipping", name, dims)
placed_boards = []
sorted_boards = sorted(boards, key=lambda b: b[1], reverse=True)
left = BOARD_LENGTH
cuts = []
leftover = 0
while len(sorted_boards) != 0:
found = False # have we found a size that will fit in what's left of the current board?
for i, b in enumerate(sorted_boards):
if b[1] <= left:
# look through all the sizes until we find one sufficiently short
# then chop it off the current board
cuts.append(b[0:2])
left -= b[1]
sorted_boards.pop(i)
print("popping", b)
found = True
break
if not found or len(sorted_boards) == 0:
print("NEW BOARD")
# when there are no more sizes that fit, start with a new board
leftover += left
placed_boards.append(cuts)
cuts = []
left = BOARD_LENGTH
for i, p in enumerate(placed_boards):
print("BOARD", i+1)
for cuts in p:
print("\t{}\" ({})".format(cuts[1], cuts[0]))
if args.outfile:
print("\nwriting cuts to {}".format(args.outfile))
with open("cuts.csv", 'w', newline='') as w:
writer = csv.writer(w)
for i, p in enumerate(placed_boards):
for cuts in p:
writer.writerow([i+1, cuts[1], cuts[0]])
print()
print("utilization: {0:.1f}%".format((100 - leftover / (BOARD_LENGTH * len(placed_boards)) * 100)))
print("wasted board: {}\"".format(leftover))
print("{}\" boards required: {}".format(BOARD_LENGTH, len(placed_boards)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment