-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcsv2stl.py
197 lines (158 loc) · 5.88 KB
/
csv2stl.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
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
192
193
194
195
196
197
import numpy as np
import pandas as pd
# Reading the data from a CSV file using pandas
alturas = pd.read_csv('files/alturas-final.csv', sep=',', header=0)
alturas.head(10)
maps = np.zeros((3, 256, 256))
bigmaps = np.zeros ((3, 256, 256, 256))
for index, dd in alturas.iterrows():
dd['height'] &= 0x0f
#print (f"{dd}")
if (dd['Level'] >= 0):
maps[dd['Level'], dd['X'], dd['Y']] = max(dd['height'], maps[dd['Level'], dd['X'], dd['Y']])
bigmaps[dd['Level'], dd['X'], dd['Y'], dd['height']] = 1
import seaborn as sns
import matplotlib.pylab as plt
print(maps[0])
dd = maps[0] # [:150][:200]
# dd = np.random.rand(10, 12)
fig = plt.figure(figsize=(10,10),facecolor = 'white', dpi=100)
ax = sns.heatmap(dd, linewidth=0)
plt.show()
import math
import stl
from stl import mesh
import numpy
# find the max dimensions, so we can know the bounding box, getting the height,
# width, length (because these are the step size)...
def find_mins_maxs(obj):
minx = maxx = miny = maxy = minz = maxz = None
for p in obj.points:
# p contains (x, y, z)
if minx is None:
minx = p[stl.Dimension.X]
maxx = p[stl.Dimension.X]
miny = p[stl.Dimension.Y]
maxy = p[stl.Dimension.Y]
minz = p[stl.Dimension.Z]
maxz = p[stl.Dimension.Z]
else:
maxx = max(p[stl.Dimension.X], maxx)
minx = min(p[stl.Dimension.X], minx)
maxy = max(p[stl.Dimension.Y], maxy)
miny = min(p[stl.Dimension.Y], miny)
maxz = max(p[stl.Dimension.Z], maxz)
minz = min(p[stl.Dimension.Z], minz)
return minx, maxx, miny, maxy, minz, maxz
def translate(_solid, step, padding, multiplier, axis):
if axis == 'x':
items = [0, 3, 6]
elif axis == 'y':
items = [1, 4, 7]
elif axis == 'z':
items = [2, 5, 8]
for p in _solid.points:
# point items are ((x, y, z), (x, y, z), (x, y, z))
for i in range(3):
p[items[i]] += (step * multiplier) + (padding * multiplier)
def copy_obj(obj, dims, num_rows, num_cols, num_layers):
w, l, h = dims
copies = []
for layer in range(num_layers):
for row in range(num_rows):
for col in range(num_cols):
# skip the position where original being copied is
if row == 0 and col == 0 and layer == 0:
continue
_copy = mesh.Mesh(obj.data.copy())
# pad the space between objects by 10% of the dimension being
# translated
if col != 0:
translate(_copy, w, 0, col, 'x')
if row != 0:
translate(_copy, l, 0, row, 'y')
if layer != 0:
translate(_copy, h, 0, layer, 'z')
copies.append(_copy)
return copies
# Using an existing stl file:
main_body = mesh.Mesh.from_file('files/cube.stl')
# Dont want to rotate along Y
# main_body.rotate([0.0, 0.5, 0.0], math.radians(90))
minx, maxx, miny, maxy, minz, maxz = find_mins_maxs(main_body)
w1 = maxx - minx
l1 = maxy - miny
h1 = maxz - minz
copies = copy_obj(main_body, (w1, l1, h1), 1, 1, 1)
# I wanted to add another related STL to the final STL
twist_lock = mesh.Mesh.from_file('files/cube.stl')
minx, maxx, miny, maxy, minz, maxz = find_mins_maxs(twist_lock)
w2 = maxx - minx
l2 = maxy - miny
h2 = maxz - minz
translate(twist_lock, w1, 0, 3, 'x')
copies2 = copy_obj(twist_lock, (w2, l2, h2), 2, 2, 2)
combined = mesh.Mesh(numpy.concatenate([main_body.data, twist_lock.data] +
[copy.data for copy in copies] +
[copy.data for copy in copies2]))
combined.save('combined.stl', mode=stl.Mode.BINARY) # save as bin
# Optionally render the rotated cube faces
from matplotlib import pyplot
from mpl_toolkits import mplot3d
# Create a new plot
figure = pyplot.figure()
axes = mplot3d.Axes3D(figure)
# Render the cube
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(combined.vectors))
# Auto scale to the mesh size
scale = combined.points.flatten('C')
axes.auto_scale_xyz(scale, scale, scale)
# Show the plot to the screen
pyplot.show()
for ii in range(0, 3):
level = mesh.Mesh.from_file('files/cube.stl')
copies = []
for keys, val in np.ndenumerate(bigmaps[ii]):
if (val == 1):
for hh in range(0,keys[2]+1):
_copy = mesh.Mesh(level.data.copy())
translate(_copy, 1, 0, keys[1], 'x')
translate(_copy, 1, 0, keys[0], 'y')
translate(_copy, 1, 0, keys[2]-hh, 'z')
copies.append(_copy)
combined2 = mesh.Mesh(numpy.concatenate([copy.data for copy in copies]))
combined2.save(f"level{ii}.stl", mode=stl.Mode.BINARY)
# Create a new plot
figure = pyplot.figure()
axes = mplot3d.Axes3D(figure)
# Render the cube
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(combined2.vectors))
# Auto scale to the mesh size
scale = combined2.points.flatten('C')
print(scale)
axes.auto_scale_xyz(scale, scale, scale)
# Show the plot to the screen
pyplot.show()
# testing the rooms around the first room (23)
level = mesh.Mesh.from_file('files/cube.stl')
copies = []
for keys, val in np.ndenumerate(bigmaps[0]):
if (val == 1):
_copy = mesh.Mesh(level.data.copy())
translate(_copy, 1, 0, keys[0] - 100, 'x')
translate(_copy, 1, 0, keys[1] - 100, 'y')
translate(_copy, 1, 0, keys[2], 'z')
copies.append(_copy)
combined2 = mesh.Mesh(numpy.concatenate([copy.data for copy in copies]))
combined2.save(f"test.stl", mode=stl.Mode.BINARY)
# Create a new plot
figure = pyplot.figure()
axes = mplot3d.Axes3D(figure)
# Render the cube
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(combined2.vectors))
# Auto scale to the mesh size
scale = combined2.points.flatten('C')
print(scale)
axes.auto_scale_xyz(scale, scale, range(20))
# Show the plot to the screen
pyplot.show()