-
Notifications
You must be signed in to change notification settings - Fork 16
/
shapebot3deck.py
246 lines (190 loc) · 7.38 KB
/
shapebot3deck.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
"""
G-code generation for the deck
"""
import boardcad.gui.jdk.BoardCAD
import boardcam.cutters.FlatCutter
import boardcam.cutters.STLCutter
import boardcam.BoardMachine
import cadcore.NurbsPoint
from javax.swing import *
import math
# Read machine configuration from shapebot.properties
machine=boardcam.BoardMachine()
machine.read_machine_data()
width_steps=machine.deckCuts
length_steps=50
filename=machine.deckFileName
zmax=machine.zMaxHeight
feedrate=machine.speed
feedrate_stringer=machine.stringerSpeed
feedrate_outline=machine.outlineSpeed
supportEnd=machine.endSupportPosition
supportEndX=supportEnd[0]
supportEndY=supportEnd[1]
supportEndZ=supportEnd[2]
cut_stringer = machine.cutStringer
stringer_offset = machine.stringerOffset
stringerOffsetZ=[10, 5, 0]
stringerCutoff=machine.stringerCutoff
outlineOffsetZ=[]
outlineOffsetY=machine.outlineOffset
# Create a cutter
cutter=boardcam.cutters.FlatCutter()
cutter.setRadius(10.0)
#cutter=boardcam.cutters.STLCutter()
#cutter.init(machine.toolName)
#cutter.scale(1.0,1.0,1.0)
# Get the deck surface from BoardCAD
boardhandler=boardcad.gui.jdk.BoardCAD.getInstance().getBoardHandler()
board=boardhandler.getActiveBoard();
deck=board.getDeck()
# 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
width_step=((deck.getMaxT()-deck.getMinT())/2-t_outline-0.01)/width_steps
length_step=(deck.getMaxS()-deck.getMinS())/length_steps
# calculate tool path stringer
stringer_cut=[]
s=deck.getMaxS()
t=(deck.getMaxT()-deck.getMinT())/2
p=deck.getPoint(s,t)
p=cutter.calcOffset(p,cadcore.NurbsPoint(1,0,0))
stringer_cut.append(p)
while (s>deck.getMinS()):
p=deck.getPoint(s,t)
n=deck.getNormal(s,t)
p=cutter.calcOffset(p,n)
stringer_cut.append(p)
s=s-length_step
s=deck.getMinS()
p=deck.getPoint(s,t)
p=cutter.calcOffset(p,cadcore.NurbsPoint(-1,0,0))
stringer_cut.append(p)
# calculate tool path outline
outline_cut=[]
t=t_outline
s=deck.getMinS()
while (s<deck.getMaxS()):
p=deck.getPoint(s,t)
n=deck.getNormal(s,t)
p=cutter.calcOffset(p,n)
outline_cut.append(p)
s=s+length_step
# calculate tool path deck
deck_cut=[]
s=deck.getMinS();
t=(deck.getMaxT()-deck.getMinT())/2-00.1
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-width_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-width_step
# Avoid stringer collision
if(cut_stringer==0):
stringerOffsetZ=[]
for p in outline_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))
for offsetZ in outlineOffsetZ:
f.write('(cutting outline right)\n')
f.write(zsafe)
toolpath.append(cadcore.NurbsPoint(p.x,zmax-supportEndZ,0.0))
p=outline_cut[0]
f.write('g0 x%.3f y%.3f\n' % (p.x+supportEndX, -p.z+supportEndY-outlineOffsetY-200))
toolpath.append(cadcore.NurbsPoint(p.x,zmax-supportEndZ,-p.z-outlineOffsetY-200))
f.write('g1 x%.3f y%.3f z%.3f f%d\n' % (p.x+supportEndX, -p.z+supportEndY-outlineOffsetY-200, p.y+supportEndZ+offsetZ, feedrate_outline))
toolpath.append(cadcore.NurbsPoint(p.x,p.y+offsetZ,-p.z-outlineOffsetY-200))
mycount=0
for p in outline_cut:
mycount=mycount+1
f.write('g1 x%.3f y%.3f z%.3f\n' % (p.x+supportEndX, -p.z+supportEndY-outlineOffsetY, p.y+supportEndZ+offsetZ))
toolpath.append(cadcore.NurbsPoint(p.x,p.y+offsetZ,-p.z-outlineOffsetY))
f.write('g1 x%.3f y%.3f z%.3f\n' % (p.x+supportEndX, -p.z+supportEndY-outlineOffsetY-200, p.y+supportEndZ+offsetZ))
toolpath.append(cadcore.NurbsPoint(p.x,p.y+offsetZ,-p.z-outlineOffsetY-200))
f.write('(cutting outline left)\n')
f.write(zsafe)
toolpath.append(cadcore.NurbsPoint(p.x,zmax-supportEndZ,-p.z-outlineOffsetY-200))
p=outline_cut[mycount-1]
f.write('g0 x%.3f y%.3f\n' % (p.x+supportEndX, p.z+supportEndY+outlineOffsetY+200))
toolpath.append(cadcore.NurbsPoint(p.x,zmax-supportEndZ, p.z-outlineOffsetY+200))
f.write('g1 x%.3f y%.3f z%.3f\n' % (p.x+supportEndX, p.z+supportEndY+outlineOffsetY+200, p.y+supportEndZ+offsetZ))
toolpath.append(cadcore.NurbsPoint(p.x, p.y+offsetZ, p.z-outlineOffsetY+200))
for i in range(mycount):
p=outline_cut[mycount-i-1]
f.write('g1 x%.3f y%.3f z%.3f\n' % (p.x+supportEndX, p.z+supportEndY+outlineOffsetY, p.y+supportEndZ+offsetZ))
toolpath.append(cadcore.NurbsPoint(p.x, p.y+offsetZ,p.z-outlineOffsetY))
f.write('g1 x%.3f y%.3f z%.3f\n' % (p.x+supportEndX, p.z+supportEndY+outlineOffsetY+200, p.y+supportEndZ+offsetZ))
toolpath.append(cadcore.NurbsPoint(p.x, p.y+offsetZ, p.z-outlineOffsetY+200))
toolpath.append(cadcore.NurbsPoint(p.x, zmax-supportEndZ, p.z-outlineOffsetY+200))
f.write(zsafe)
f.write('(cutting deck right)\n');
p=deck_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 deck_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 deck left)\n');
p=deck_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 deck_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.deck_cut[i]=p;
i=i+1
machine.nr_of_cuts_deck=i
JOptionPane.showMessageDialog(None, "Finished g-code generation")