-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcutoptimizer.py
More file actions
191 lines (151 loc) · 5.68 KB
/
cutoptimizer.py
File metadata and controls
191 lines (151 loc) · 5.68 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env python
import os
import rectpack
import matplotlib.pyplot as plt
import matplotlib.patches as patches
class Packer(rectpack.PackerBBF):
"""rectpack packer wrapper to add user methods and views
Attributes:
rect_map (dict): Mapping of rectangle cuts which is
populated with pack run method.
"""
def __init__(self):
super().__init__()
self.rect_map = None
def pack(self):
"""Pack override
Runs parent pack method then generates rectangle cut
mapping.
Returns:
None
"""
super().pack()
self.rect_map = {}
for rect in self.rect_list():
self.rect_map[rect[5]] = {
'board': rect[0],
'x': rect[1],
'y': rect[2],
'width': rect[3],
'height': rect[4]
}
def get_rect(self, id):
"""Getter for rect information
Args:
id (str): Identification for cut rectangle;
declared with add_rect method.
Returns:
Dictionary of rectangle information including
bin, start width, start height, total width,
and total height.
Raises:
EnvironmentError: If the pack method hasn't been run.
"""
if not self.rect_map:
raise EnvironmentError('Packer has not yet been packed; '
'try self.pack() first')
return self.rect_map[id]
def cut_list(self):
"""Human readable cut list to work from
Returns:
instructions (str): Cut list as plain text.
Raises:
EnvironmentError: If the pack method hasn't been run.
"""
if not self.rect_map:
raise EnvironmentError('Packer has not yet been packed; '
'try self.pack() first')
instructions = ['-'*50, ' Cut List '.center(50, '-'), '-'*50, '']
for id, result in self.rect_map.items():
instructions.append(id)
instructions.append('-'*50)
instructions.append('\tboard:\t\t\t%s' % str(result['board'] + 1))
instructions.append('\tstart-width:\t%s' % str(result['x']))
instructions.append('\tstart-height:\t%s' % str(result['y']))
instructions.append('\twidth:\t\t\t%s' % str(result['width']))
instructions.append('\theight:\t\t\t%s' % str(result['height']))
instructions.append('')
return '\n'.join(instructions)
def _save_board(self, board_number, figure_path):
"""Internal method for generating board figure
Args:
board_number (int): Index for board to visualize.
figure_path (str): System path for figure output.
Should end in .png.
Returns:
None
"""
bid = board_number - 1
board = self[bid]
x = board.width
y = board.height
fig = plt.figure()
ax = fig.add_subplot(111, aspect='equal')
ax.set_xlim(0, x)
ax.set_ylim(0, y)
# ax.grid()
for rect in board:
ax.add_patch(
patches.Rectangle(
(rect.x, rect.y), # (x,y)
rect.width, # width
rect.height, # height
edgecolor='black'
)
)
center_x = rect.x + 0.5 * rect.width
center_y = rect.y + 0.5 * rect.height
plt.text(center_x, center_y, rect.rid)
fig.savefig(figure_path, dpi=90, bbox_inches='tight')
def save_boards(self, figure_folder):
board_number = 1
while True:
figure_path = os.path.join(figure_folder,
'board_%s.png' % board_number)
try:
self._save_board(board_number=board_number,
figure_path=figure_path)
except IndexError as e:
break
board_number += 1
def pack_test():
rectangles = [
{'width': 24, 'height': 38, 'rid': 'R1'},
{'width': 12, 'height': 38, 'rid': 'R2'},
{'width': 24, 'height': 38, 'rid': 'R3'},
{'width': 24, 'height': 38, 'rid': 'R4'},
{'width': 44, 'height': 13, 'rid': 'L1'},
{'width': 24, 'height': 38, 'rid': 'L3'},
{'width': 2.4, 'height': 34, 'rid': 'C1'},
{'width': 3.3, 'height': 7, 'rid': 'C2'},
{'width': 3.3, 'height': 7, 'rid': 'C3'},
{'width': 3.3, 'height': 7, 'rid': 'C4'},
{'width': 3.3, 'height': 7, 'rid': 'C5'},
{'width': 3.3, 'height': 12, 'rid': 'C6'},
{'width': 3.3, 'height': 26, 'rid': 'C7'},
{'width': 3.3, 'height': 34, 'rid': 'C8'},
{'width': 3.3, 'height': 7, 'rid': 'C9'},
{'width': 3.3, 'height': 2.3, 'rid': 'D8'},
{'width': 3.6, 'height': 66, 'rid': 'D9'},
{'width': 3.6, 'height': 66, 'rid': 'D9'},
{'width': 3.1, 'height': 3, 'rid': 'E8'},
{'width': 3.32, 'height': 27, 'rid': 'F8'},
{'width': 6, 'height': 10, 'rid': 'G8'},
{'width': 11, 'height': 22, 'rid': 'H8'},
{'width': 24, 'height': 24, 'rid': 'I8'},
{'width': 2, 'height': 31, 'rid': 'J8'},
{'width': 45, 'height': 7, 'rid': 'K8'},
]
bins = [
{'width': 48, 'height': 96},
{'width': 48, 'height': 96},
{'width': 48, 'height': 96}
]
packer = Packer()
[packer.add_rect(**x) for x in rectangles]
[packer.add_bin(**x) for x in bins]
packer.pack()
print(packer.cut_list())
packer.save_boards(figure_folder='boards')
if __name__ == '__main__':
pack_test()