Newer
Older
# import standard libraries
# import classes from the classes module in functions package
from functions.classes import LegoComponent
from functions.classes import LegoAssembly
from functions.classes import ComponentCategory
from functions.classes import AggregationLayer
from functions.classes import KPIEncoder
from functions.classes import print_assembly_tree
from functions.calculation_rules import kpi_sum
# Test manually creating some assemblies and components

Hock, Martin
committed
battery = LegoComponent("nice battery", ComponentCategory.BATTERY, "bat42", 1, 2, 3)
motor = LegoComponent("motor goes brrr", ComponentCategory.MOTOR, "motor", 1, 2, 3)
wheel = LegoComponent("much round wheel", ComponentCategory.WHEEL, "round", 1, 2, 3)
# Create subassemblies and combine to assembly (currently no parts present for this)
chassis = LegoAssembly("Chassis", AggregationLayer.ASSEMBLY)
door1 = LegoAssembly("Door 1", AggregationLayer.SUBASSEMBLY)
door2 = LegoAssembly("Door 2", AggregationLayer.SUBASSEMBLY)
chassis.add_assembly(door1)
chassis.add_assembly(door2)
chassis.properties
engine = LegoAssembly("Engine", AggregationLayer.ASSEMBLY)
# Showcase cloning - one motor for each axis?
fuel_tank = LegoAssembly("Fuel Tank", AggregationLayer.ASSEMBLY)
fuel_tank.add_component(battery.clone())

Hock, Martin
committed
wheels = LegoAssembly("Wheels", AggregationLayer.ASSEMBLY)
for _ in range(4):
wheels.add_component(wheel.clone())

Hock, Martin
committed
# Create and assemble the system level car
car = LegoAssembly("Car", AggregationLayer.SYSTEM)
car.add_assembly(chassis)
car.add_assembly(engine)
car.add_assembly(fuel_tank)
car.add_assembly(wheels)

Hock, Martin
committed
## Printing

Hock, Martin
committed
# Dump to json file
with open("car.json", "w") as fp:
json.dump(car.to_dict(), fp, cls=KPIEncoder)

Hock, Martin
committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
## Create components from datasheet
# Need to run the script from datasheets folder first
# Test loading dicts from datasheet jsons
with open('datasheets/Achsen.json') as json_file:
Achsen = json.load(json_file)
print((Achsen["Achse 5 studs"]))
# This is hard to read, prettier please!
pprint.pprint(Achsen)
# Okay I'll use the 5 stud one
Achse5 = Achsen["Achse 5 studs"] # I'd prefer to call it via "Designnummer"
print(Achsen["Achse 5 studs"]["Designnummer"])
with open('datasheets/Räder.json') as json_file:
Raeder = json.load(json_file)
# Get me the expensive one
Radpreis = []
for key in Raeder: # a list would work easier here than a dict
Radpreis.append(Raeder[key]["Preis [Euro]"])
Radpreis = max(Radpreis)
for key in Raeder:
if Raeder[key]["Preis [Euro]"] == Radpreis:
TeuresRad=Raeder[key]
# Also need a frame
with open('datasheets/Gestell.json') as json_file:
Gestelle = json.load(json_file)
Gestellbeschreibung = "Technic, Brick 1 x 8 with Holes"
Gestell = Gestelle[Gestellbeschreibung]
# Create Components from these items
# LegoComponent(name: str, category: ComponentCategory, lego_id: str, cost: float,
# mass: float, delivery_time: int, layer: AggregationLayer = AggregationLayer.COMPONENT, **properties)
scooterframe = LegoComponent("running board", ComponentCategory.FRAME,
Gestell["Designnummer"],
Gestell["Preis [Euro]"], Gestell["Gewicht [g]"],
0)
scooterwheel = LegoComponent("Vorderrad", ComponentCategory.WHEEL,
TeuresRad["Designnummer"],
TeuresRad["Preis [Euro]"], TeuresRad["Gewicht [g]"],
0)
# Cloning is necessary because each lego object gets a unique id so you can't use the same part twice
scooterwheel2 = scooterwheel.clone()
scooterwheel2.description = "Hinterrad"
print([scooterwheel.uuid, scooterwheel2.uuid])
# Lets Assembly, first the Assembly object, directly add the frame?
scooter = LegoAssembly("my first scooter", AggregationLayer.SYSTEM)
scooter.add_component(scooterframe) # only one per call for now
scooter.add_component(scooterwheel)
scooter.add_component(scooterwheel2)
# <Assembly>.add_assembly() works in the same way
# Look at our work:
print(scooter)
print(scooter.children())
## First KPI mass should be a sum
listofmasses= []
for c in scooter.components:
listofmasses.append(c.mass)
for a in scooter.assemblies:
# only checking one layer deep here
listofmasses.append(a.mass)
print(listofmasses)
print(kpi_sum(listofmasses))
print("theend")