forked from hornstein/boardcad-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shapebot5bottom.py
executable file
·271 lines (201 loc) · 6.5 KB
/
shapebot5bottom.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
"""
G-code generation for the bottom
"""
import boardcad.gui.jdk.BoardCAD
import boardcam.cutters.FlatCutter
import boardcam.BoardMachine
import cadcore.NurbsPoint
from javax.swing import *
import math
# Read machine configuration from shapebot.properties
machine=boardcam.BoardMachine()
width_steps=machine.bottomCuts
rail_steps=machine.bottomRailCuts
length_steps=50
filename=machine.bottomFileName
zmax=machine.zMaxHeight
feedrate=machine.speed
feedrate_stringer=machine.stringerSpeed
supportEnd=machine.endSupportPosition
supportEndX=supportEnd[0]
supportEndY=supportEnd[1]
supportEndZ=supportEnd[2]
cut_stringer = machine.cutStringer
stringer_offset = machine.stringerOffset
stringerOffsetZ=[5, 0]
stringerCutoff=machine.stringerCutoff
# Create a cutter
#cutter=boardcam.cutters.FlatCutter()
#cutter.setRadius(10.0)
cutter=boardcam.cutters.STLCutter()
cutter.init('flatcutter.stl')
#cutter.scale(1.0,1.0,1.0)
cutter.scale(machine.toolScaleX,machine.toolScaleY,machine.toolScaleZ);
# Get the bottom and deck surface from BoardCAD
boardhandler=boardcad.gui.jdk.BoardCAD.getInstance().getBoardHandler()
board=boardhandler.getActiveBoard();
deck=board.getDeck()
bottom=board.getBottom()
# find parameter value for outline
max_width = 0.0
t=deck.getMinT()
p = deck.getPoint((deck.getMaxS()-deck.getMinS())/2, t)
while (max_width - math.fabs(p.z) <= 0):
t=t+0.01
p = deck.getPoint((deck.getMaxS()-deck.getMinS())/2, t)
if (math.fabs(p.z) > max_width):
max_width = math.fabs(p.z)
t_outline=t
# Calculate step length
length_step=(bottom.getMaxS()-bottom.getMinS())/length_steps
width_step=((bottom.getMaxT()-bottom.getMinT())/2)/width_steps
if rail_steps>0:
rail_step=(t_outline-deck.getMinT())/rail_steps
#Find uniform distances for t and their angles
wval=[]
tval=[]
t=bottom.getMinT()
n=deck.getNormal((bottom.getMaxS()-bottom.getMinS())/2, t)
p=cutter.calcOffset(p,n)
max_width = math.fabs(p.z)+.1
i=math.fabs(p.z)
step=math.fabs(p.z)/width_steps
while i>0.001:
wval.append(p.z+i)
i=i-step
print wval
print '\nmax width = %0.3f\n' % max_width
for w in wval:
while (max_width - math.fabs(p.z) <= math.fabs(w)):
t=t+0.01
p=bottom.getPoint((deck.getMaxS()-deck.getMinS())/2, t)
n=bottom.getNormal((deck.getMaxS()-deck.getMinS())/2, t)
p=cutter.calcOffset(p,n)
tval.append(t)
print tval
# calculate tool path stringer
stringer_cut=[]
s=bottom.getMinS()
t=(bottom.getMaxT()-bottom.getMinT())/2
while (s<bottom.getMaxS()):
p=bottom.getPoint(s,t)
n=bottom.getNormal(s,t)
p=cutter.calcOffset(p,n)
stringer_cut.append(p)
s=s+length_step
# calculate tool path bottom
bottom_cut=[]
s=bottom.getMinS();
t=(bottom.getMaxT()-bottom.getMinT())/2
while tval:
t=tval.pop()
while (s<bottom.getMaxS()):
p=bottom.getPoint(s,t)
n=bottom.getNormal(s,t)
p=cutter.calcOffset(p,n)
bottom_cut.append(p)
s=s+length_step
t=tval.pop()
while (s>bottom.getMinS()):
p=bottom.getPoint(s,t)
n=bottom.getNormal(s,t)
p=cutter.calcOffset(p,n)
bottom_cut.append(p)
s=s-length_step
# calculate tool path rail
deck_cut=[]
if(rail_steps>0):
s=deck.getMinS();
t=deck.getMinT()
while (t<t_outline):
while (s<deck.getMaxS()):
p=deck.getPoint(s,t)
n=deck.getNormal(s,t)
p=cutter.calcOffset(p,n)
deck_cut.append(p)
s=s+length_step
t=t+rail_step
while (s>deck.getMinS()):
p=deck.getPoint(s,t)
n=deck.getNormal(s,t)
p=cutter.calcOffset(p,n)
deck_cut.append(p)
s=s-length_step
t=t+rail_step
else:
s=deck.getMinS();
t=deck.getMinT()
while (s<deck.getMaxS()):
p=deck.getPoint(s,t)
n=deck.getNormal(s,t)
p=cutter.calcOffset(p,n)
deck_cut.append(p)
s=s+length_step
while (s>deck.getMinS()):
p=deck.getPoint(s,t)
n=deck.getNormal(s,t)
p=cutter.calcOffset(p,n)
p.z=p.z-10
deck_cut.append(p)
s=s-length_step
# Avoid stringer collision
if(cut_stringer==0):
stringerOffsetZ=[]
for p in bottom_cut:
if(p.z<stringer_offset):
p.z=stringer_offset
for p in deck_cut:
if(p.z<stringer_offset):
p.z=stringer_offset
# Write g-code
toolpath=[]
f=open(filename, 'w')
zsafe='g0 z%d\n' %(zmax)
f.write('(cutting stringer)\n')
p=stringer_cut[0]
for offsetZ in stringerOffsetZ:
f.write(zsafe)
toolpath.append(cadcore.NurbsPoint(p.x,zmax-supportEndZ,0.0))
p=stringer_cut[0]
f.write('g0 x%.3f y%.3f\n' % (p.x+supportEndX, supportEndY))
toolpath.append(cadcore.NurbsPoint(p.x,zmax-supportEndZ,0.0))
f.write('g1 x%.3f z%.3f f%d\n' % (p.x+supportEndX, p.y+supportEndZ+offsetZ-stringerCutoff, feedrate_stringer))
toolpath.append(cadcore.NurbsPoint(p.x,p.y+offsetZ-stringerCutoff,0.0))
for p in stringer_cut:
f.write('g1 x%.3f z%.3f\n' % (p.x+supportEndX, p.y+supportEndZ+offsetZ))
toolpath.append(cadcore.NurbsPoint(p.x,p.y+offsetZ,0.0))
f.write('g1 x%.3f z%.3f\n' % (p.x+supportEndX, p.y+supportEndZ+offsetZ-stringerCutoff))
toolpath.append(cadcore.NurbsPoint(p.x,p.y+offsetZ-stringerCutoff,0.0))
f.write(zsafe)
toolpath.append(cadcore.NurbsPoint(p.x,zmax-supportEndZ,0.0))
f.write('(cutting bottom right)\n');
p=bottom_cut[0]
f.write('g0 x%.3f y%.3f\n' % (p.x+supportEndX, -p.z+supportEndY))
toolpath.append(cadcore.NurbsPoint(p.x, zmax-supportEndZ, p.z))
f.write('g1 x%.3f y%.3f z%.3f f%d\n' % (p.x+supportEndX, -p.z+supportEndY, p.y+supportEndZ, feedrate))
toolpath.append(cadcore.NurbsPoint(p.x, p.y, -p.z))
for p in bottom_cut:
f.write('g1 x%.3f y%.3f z%.3f\n' % (p.x+supportEndX, -p.z+supportEndY, p.y+supportEndZ))
toolpath.append(cadcore.NurbsPoint(p.x, p.y, -p.z))
f.write(zsafe)
toolpath.append(cadcore.NurbsPoint(p.x, zmax-supportEndZ, -p.z))
f.write('(cutting bottom left)\n');
p=bottom_cut[0]
f.write('g0 x%.3f y%.3f\n' % (p.x+supportEndX, p.z+supportEndY))
toolpath.append(cadcore.NurbsPoint(p.x, zmax-supportEndZ, -p.z))
f.write('g1 x%.3f y%.3f z%.3f f%d\n' % (p.x+supportEndX, p.z+supportEndY, p.y+supportEndZ, feedrate))
toolpath.append(cadcore.NurbsPoint(p.x, p.y, p.z))
for p in bottom_cut:
f.write('g1 x%.3f y%.3f z%.3f\n' % (p.x+supportEndX, p.z+supportEndY, p.y+supportEndZ))
toolpath.append(cadcore.NurbsPoint(p.x, p.y, p.z))
f.write(zsafe)
toolpath.append(cadcore.NurbsPoint(p.x, zmax-supportEndZ, p.z))
f.write("M2");
f.close()
# Visualize toolpath in BoardCAD
i=0
for p in toolpath:
machine.bottom_cut[i]=p;
i=i+1
machine.nr_of_cuts_bottom=i
JOptionPane.showMessageDialog(None, "Finished g-code generation")