-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
150 lines (121 loc) · 4.24 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import argparse, os, json
import numpy as np
from solid.solidpython import scad_render_to_file
from labyrinth_casing import LabyrinthCasing
from labyrinth_graph import LabyrinthGraph
from labyrinth_config import LabyrinthConfig
from labyrinth_map import LabyrinthMap
from subprocess import run
def exportStl(name: str):
run(["openscad", "-q", "--o", f"{name}.stl", f"{name}.scad"])
def check_path(path_name: str) -> str:
if os.path.isdir(path_name):
return path_name
else:
os.makedirs(path_name)
return path_name
def check_config(config_name: str) -> dict:
try:
return {
"config_name": config_name,
"config": getattr(LabyrinthConfig, config_name),
}
except AttributeError:
raise AttributeError(f'the config named "{config_name}" does not exist')
def str2int(string: str) -> int:
return int(string)
if __name__ == "__main__":
parser = argparse.ArgumentParser("Generate a Labyrinth Cube")
parser.add_argument("c", help="name of the config to use", type=check_config)
parser.add_argument("p", help="path for .scad and .stl output", type=check_path)
parser.add_argument(
"-s", dest="set_seed", help="seed for random generation", type=str2int
)
parser.set_defaults(set_seed=-1)
parser.add_argument(
"--vp",
help="output .scad file for labyrinth casing visualization",
dest="path_vis",
action="store_true",
)
parser.add_argument(
"--vc",
help="output .scad file for labyrinth path visualization",
dest="case_vis",
action="store_true",
)
parser.add_argument(
"--stl",
help="output .stl files labyrinth levels and casing",
dest="stl",
action="store_true",
)
parser.add_argument(
"--random",
help="change random generation seed to random value and print it",
dest="random",
action="store_true",
)
parser.add_argument(
"--no-windows",
help="add windows to all floors",
dest="windows",
action="store_false",
)
parser.add_argument(
"--map",
help="create a map of the levels",
dest="map",
action="store_true",
)
parser.set_defaults(path_vis=False)
parser.set_defaults(case_vis=False)
parser.set_defaults(stl=False)
parser.set_defaults(random=False)
parser.set_defaults(windows=True)
parser.set_defaults(maps=False)
args = parser.parse_args()
config = args.c["config"]
config_name = args.c["config_name"]
if args.random:
new_seed = np.random.randint(10000000)
config["seed"] = new_seed
if args.set_seed >= 0:
new_seed = args.set_seed
config["seed"] = new_seed
print("config = " + json.dumps(config, sort_keys=True, indent=4))
lgraph = LabyrinthGraph(config["cubeSize"])
lgraph.setRandomTree(config["seed"])
lcube = lgraph.getLabyrinthCube(
config["levelWallThickness"],
config["levelPathThickness"],
config["levelSpacing"],
)
print(f"Level width: {lcube.levels[0].levelSizeXY} mm")
if args.windows:
lcube.addAllWindows()
lcase = LabyrinthCasing(
lcube, config["casingWallThickness"], config["casingTolerance"]
)
path = lgraph.findPath(lgraph.topCornerNode, lgraph.bottomCornerNode)
if args.stl:
name = os.path.join(args.p, f"casing_{config_name}")
lcase.createScadFile(name)
exportStl(name)
for i, level in enumerate(lcube.levels):
name = os.path.join(args.p, f"level{i}_{config_name}")
level.createScadFile(name)
exportStl(name)
if args.case_vis:
lcube.spacing = config["levelSpacing"]
vis_output_path = os.path.join(args.p, "labyrinth_case_visualization.scad")
scad_render_to_file(lcase.getCubeInCasingSolid(), vis_output_path)
if args.path_vis:
lcube.spacing = config["viewSpacing"]
vis_output_path = os.path.join(args.p, "labyrinth_path_visualization.scad")
scad_render_to_file(
lcube.getCubeSolid() + lcube.getPathSolid(path), vis_output_path
)
if args.map:
lmap = LabyrinthMap(lcube=lcube)
lmap.render_png(args.p)