-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.py
145 lines (114 loc) · 3.21 KB
/
run.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
# main script file run by VoxelCut
from vc import *
import wx
import math
voxels_per_mm = float(0)
x = float(0)
y = float(0)
z = float(0)
rad = float(0)
def jump_to(nx, ny, nz):
global x
global y
global z
x, y, z = nx, ny, nz
def get_voxel_pos(nx, ny, nz):
global voxels_per_mm
vx = int(512 - 300 + nx * voxels_per_mm+0.5)
vy = int(512 - 300 + ny * voxels_per_mm+0.5)
vz = int(256 + (-10 - nz) * voxels_per_mm+0.5)
return vx, vy, vz
def subtract_tool_at(nx, ny, nz):
global rad
global voxels_per_mm
vx, vy, vz = get_voxel_pos(nx, ny, nz)
vrad = int(rad * voxels_per_mm)
setcylinder(vx, vy, vz, vx, vy, 0, vrad, -1)
def cut_to(nx, ny, nz):
global x
global y
global z
vx = float(nx) - x
vy = float(ny) - y
vz = float(nz) - z
magn = math.sqrt(vx*vx + vy*vy + vz*vz)
voxmagn = magn*voxels_per_mm
num_steps = int(voxmagn + 0.9)
for i in range(0, num_steps):
fraction = float(i+1)/num_steps
tx = x + fraction * vx
ty = y + fraction * vy
tz = z + fraction * vz
subtract_tool_at(tx, ty, tz)
repaint()
x, y, z = nx, ny, nz
def get_value(s, name):
# gets a value from the string, like: found, x = get_value('N1000G0X4.56Y34.78M6', 'X'), gives found = true, x = 4.56
pos = s.find(name)
if pos == -1: return False, 0.0
numstr = ''
for x in range(pos + 1, len(s)):
if s[x].isdigit() or s[x] == '.' or s[x] == '-':
numstr = numstr + s[x]
else: break
if len(numstr) == 0: return False, 0.0
return True, float(numstr)
def read_nc_file(fpath):
# set the tool position to z50mm
jump_to(0, 0, 50)
# set a tool diameter of 3mm
global rad
rad = 1.5
# open the nc file
f = open(fpath)
global x
global y
global z
nx = x
ny = y
nz = z
while (True):
line = f.readline()
if (len(line) == 0) : break
G0_found = (line.find('G0') != -1)
G1_found = (line.find('G1') != -1)
G2_found = (line.find('G2') != -1)
G3_found = (line.find('G3') != -1)
X_found, tx = get_value(line, 'X')
Y_found, ty = get_value(line, 'Y')
Z_found, tz = get_value(line, 'Z')
move_found = False
if X_found:
nx = tx
move_found = True
if Y_found:
ny = ty
move_found = True
if Z_found:
nz = tz
move_found = True
if move_found:
cut_to(nx, ny, nz)
f.close()
# clear the volume
setrect(0, 0, 0, 1024, 1024, 256, -1)
repaint()
# make a default block 50mm x 50mm x 10mm
global voxels_per_mm
vwidth = 600
mwidth = 50
voxels_per_mm = vwidth/mwidth
x0 = 512 - 300
y0 = 512 - 300
z0 = 256 - 10 * voxels_per_mm
x1 = 512 + 300
y1 = 512 + 300
z1 = 256
setrect(x0, y0, z0, x1, y1, z1, 0)
repaint()
# ask the user to choose an nc file
app = wx.App()
fd = wx.FileDialog(None, "Open NC File", "", "", "Tap Files (*.tap)|*.tap")
result = fd.ShowModal()
if result == wx.ID_OK:
read_nc_file(fd.GetPath())