-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathnozzleboss.py
491 lines (344 loc) · 15.7 KB
/
nozzleboss.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
import bpy
import time
import os
from mathutils import Vector
import numpy as np
from bpy.props import (StringProperty,
BoolProperty,
PointerProperty,
FloatProperty,
IntProperty,
FloatVectorProperty
)
from bpy.types import (Panel,
Menu,
Operator,
PropertyGroup,
)
from bpy_extras.io_utils import ImportHelper
from .parser import *
from .utils import *
class gcode_settings(bpy.types.PropertyGroup):
flow_map: StringProperty(
name="Flow Multiplier weightmap - grayscale of vertex color gets mapped to min and max range",
default="Flow"
)
speed_map: StringProperty(
name="Speed Multiplier weightmap - grayscale of vertex color gets mapped to min and max range",
default="Speed"
)
T0: StringProperty(
name="Custom G-code per color",
default="T0"
)
T1: StringProperty(
name="Custom G-code per color",
default="T1"
)
start: StringProperty(
name="Start G-code to append to every exported G-code file",
default="Start"
)
end: StringProperty(
name="End G-code to append to every exported G-code file",
default="End"
)
split_layers: BoolProperty(
name="Split into Layers",
description="Save every layer as single Object in Collection",
default = False
)
subdivide: BoolProperty(
name="Subdivide",
description="Subdivide Segments that are bigger then value, needed to increase resolution when modifyng the mesh",
default = False
)
tool_color: BoolProperty(
name="Toolchange based on color:",
description="Will insert textblock associated with that color. Color value is taken from 'Tool' Vertex Color map",
default = False
)
max_segment_size: FloatProperty(
name = "",
description = "Only Segments bigger then this value get subdivided, too small values here can cause performance issues",
default = 1,
min = 0.1,
max = 999.0
)
min_flow: FloatProperty(
name = "",
description = "Factor the extrusion value is multiplied with based on the underlying vertex color of that segment",
default = 0.4
)
max_flow: FloatProperty(
name = "",
description = "Factor the extrusion value is multiplied with based on the underlying vertex color of that segment",
default = 1.
)
min_speed: FloatProperty(
name = "",
description = "Factor the extrusion speed value is multiplied with based on the underlying vertex color of that segment",
default = 0.2
)
max_speed: FloatProperty(
name = "",
description = "Factor the extrusion speed value is multiplied with based on the underlying vertex color of that segment",
default = 1.
)
nozzle_diameter: FloatProperty(
name = "",
description = "Nozzle diameter - used to calculate the extrusion width",
default = 0.4
)
travel_speed: IntProperty(
name = "",
description = "In mm/s. Exported G-code will have mm/min though",
default = 60
)
extrusion_speed: IntProperty(
name = "",
description = "In mm/s. Exported G-code will have mm/min though",
default = 30
)
color_T0: FloatVectorProperty(
name="Color",
description="When ever this color is reached on export, add custom G-code in T0 textblock to export file",
subtype='COLOR', size=3, min=0, max=1, precision=3, step=0.1,
default=(1.0, 1.0, 1.0)
)
color_T1: FloatVectorProperty(
name="Color",
description="When ever this color is reached on export, add custom G-code in T0 textblock to export file",
subtype='COLOR', size=3, min=0, max=1, precision=3, step=0.1,
default=(0.0, 0.0, 0.0)
)
class NOZZLEBOSS_PT_Panel(bpy.types.Panel):
bl_label = "nozzleboss"
bl_idname = 'NOZZLEBOSS_PT_Panel'
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = "nozzleboss"
# @classmethod
# def poll(cls, context):
# try: return context.object.type in ('MESH')
# except: return False
def draw(self, context):
layout = self.layout
scn = context.scene
nozzleboss = scn.nozzleboss
obj = context.object
col = layout.box().column(align=True)
col.label(text="Import settings:")
row = col.row(align=True)
row.prop(nozzleboss, 'subdivide')
sub = row.row()
sub.prop(nozzleboss, "max_segment_size")
sub.enabled = nozzleboss.subdivide #sub is not grayed out when 'linesegmentatio/subdivide/ bool is True
col2=col.column(align=True)
col2.scale_y=1.5
col2.separator(factor=1)
col2.operator("wm.gcode_import") #call it by id name, since specified, otherwise just takes class name
col.separator(factor=1)
col = layout.box().column(align=True)
col.label(text="Export settings:")
col.prop(nozzleboss, "nozzle_diameter", text='Nozzle Size')
col.prop(nozzleboss, "travel_speed", text='Travel Speed')
col.prop(nozzleboss, "extrusion_speed", text='Extrusion Speed')
col.separator(factor=1.5)
row=col.row() #aus col new row machen, beginnt neue zeile und startet dann in der reihe
row.label(text="Flow Multiplier:")
row.separator(factor=2)
row.label(text="min:")
row.label(text="max:")
row = col.row(align=True)
row.prop_search(nozzleboss, "flow_map", context.active_object.data, "vertex_colors", text="")
row.separator()
row.prop(nozzleboss, "min_flow")
row.prop(nozzleboss, "max_flow")
col.separator()
col.label(text="Speed Multiplier:")
row = col.row(align=True)
row.prop_search(nozzleboss, "speed_map", context.active_object.data, "vertex_colors", text="")
row.separator()
row.prop(nozzleboss, "min_speed")
row.prop(nozzleboss, "max_speed")
col.separator(factor=2)
#col.label(text="Tool change based on color:")
col.prop(nozzleboss, 'tool_color')
row = col.row(align=True)
row.enabled = nozzleboss.tool_color
row.prop(nozzleboss, "color_T0", text="")
row.prop_search(nozzleboss, "T0", bpy.data, "texts", text="")
row.separator(factor=2)
row.prop(nozzleboss, "color_T1", text="")
row.prop_search(nozzleboss, "T1", bpy.data, "texts", text="")
col.separator(factor=2)
row=col.row(align=True) #start new column in row mode (row mode every element gets put behind each other)
row.label(text='Start G-code:')
row.prop_search(nozzleboss, "start", bpy.data, "texts", text="")
row=col.row(align=True)
row.label(text='End G-code:')
row.prop_search(nozzleboss, "end", bpy.data, "texts", text="")
col.separator(factor=2)
row=col.row(align=True)
#row.label(text='Start G-code:')
row.scale_y= 1.5# only works on operators and not on labels i think
row.operator("wm.gcode_export")
def import_gcode(context, filepath):
scene = context.scene
nozzleboss = scene.nozzleboss
import time
then = time.time()
parse = GcodeParser()
model = parse.parseFile(filepath)
if nozzleboss.subdivide:
model.subdivide(nozzleboss.max_segment_size)
model.classifySegments()
if nozzleboss.split_layers:
model.draw(split_layers=True)
else:
model.draw(split_layers=False)
now=time.time()
print("then", then)
print("importing Gcode took", now-then)
return {'FINISHED'}
def export_gcode(context):
#auto create textblocks to, need if you model from scratch (if you import existing they get created in parser)
if not bpy.data.texts.get('T0'):
bpy.data.texts.new('T0')
bpy.data.texts['T0'].write('T0; switch to extruder T0 (any G-code macro can be passed here)\n')
if not bpy.data.texts.get('T1'):
bpy.data.texts.new('T1')
bpy.data.texts['T1'].write('T1; switch to extruder T1 (any G-code macro can be passed here)\n')
if not bpy.data.texts.get('Start'):
bpy.data.texts.new('Start')
bpy.data.texts['Start'].write(';nozzleboss\n')
bpy.data.texts['Start'].write('G28 ;homing\n')
bpy.data.texts['Start'].write('M104 S180 ;set hotend temp\n')
bpy.data.texts['Start'].write('M190 S50 ;wait for bed temp\n')
bpy.data.texts['Start'].write('M109 S200 ;wait for hotendtemp\n')
bpy.data.texts['Start'].write('M83; relative extrusion mode (REQUIRED)\n')
if not bpy.data.texts.get('End'):
bpy.data.texts.new('End')
bpy.data.texts['End'].write('G10 ;retract\n')
bpy.data.texts['End'].write('M104 S0 ;deactivate hotend\n')
bpy.data.texts['End'].write('M140 S0 ;deactivate bed\n')
bpy.data.texts['End'].write('G28 ;homing\n')
bpy.data.texts['End'].write('M84 ;turn off motors\n')
scene = context.scene
nozzleboss = scene.nozzleboss
then=time.time()
filename = bpy.path.basename(bpy.data.filepath)
filename = os.path.splitext(filename)[0] #strip .blend extension
gcode_txt = open(bpy.path.abspath("//")+bpy.path.basename(filename)+".gcode","w")
_txt = []
start_code = read_textblock('Start')+'\n'#'G28\nM140 S50\nM109 S190\nM83\nG1 F600\n;RGB,-1,-1,-1\nM163 S0 P0\nM163 S1 P0\nM163 S2 P1\nM163 S3 P1\nM163 S4 P1\nM164 S0\nT0\n'
_txt.append(start_code)
nozzle_diameter = nozzleboss.nozzle_diameter
extrusion_speed = nozzleboss.extrusion_speed
travel_speed = nozzleboss.travel_speed
obj = bpy.context.active_object
verts = read_verts(obj.data)
edges = read_edges(obj.data)
#initial values
P2=(0,0,0)
prev_F=-1
prev_tool_color = -1
#create vertex colors maps, most cases importer could already do that, but in case you have handdrawn/beveled extrusion path
if not obj.data.vertex_colors.get('Flow'):
obj.data.vertex_colors.new(name='Flow')
if not obj.data.vertex_colors.get('Speed'):
obj.data.vertex_colors.new(name='Speed')
if not obj.data.vertex_colors.get('Speed'):
obj.data.vertex_colors.new(name='Speed')
if not obj.data.vertex_colors.get('Tool'):
obj.data.vertex_colors.new(name='Tool')
#Read extrusion and speed multiplier vertex color map
#gets right loop color for every v_idx
extrusion_weights = read_weightmap_from_vcol(obj, 'Flow')
speed_weights = read_weightmap_from_vcol(obj, 'Speed')
tool_colors = read_weightmap_from_vcol(obj, 'Tool')
islands = find_islands(edges)
sorted_islands = sort_Z(islands, verts)
##islands of extrusions vert indices
for island in sorted_islands:
travel_dist = (Vector(verts[island[0]])-Vector(P2)).length #only retract when travel is longer than...
if travel_dist > 1:
_txt.append('G10 \n')
#travel only from island to island
_txt.append(travel(P2,verts[island[0]], travel_speed*60, extrusion_speed*60)) #verts[island[0]]=next coords
if travel_dist > 1:
_txt.append('G11 \n')
#extrusion and height edges per island, parallel arrays
e_edges = island[:int(len(island)/2)] #first half extrusion v_idx, real nozzle path
h_edges = island[int(len(island)/2):] #second half of island corresponding height v_idx
#print(h_edges)
#extrude between all poitns in island
for i in range(len(e_edges)):
if i == len(e_edges)-1:#break on last segment, if you want to close segment, 'rip' the last vert
break
#coord for seg length and height
P1 = verts[e_edges[i]]
P2 = verts[e_edges[i+1]]
P3 = verts[h_edges[i+1]]
#calcE
dist = np.linalg.norm(P2-P1)
height=np.linalg.norm(P3-P2)
width=nozzle_diameter*1.5
multiplier = extrusion_weights[e_edges[i+1]]#
multiplier = remap(multiplier, nozzleboss.min_flow, nozzleboss.max_flow)
E_volume=dist*height*width*multiplier
E=E_volume/2.405281875 ##E axis in mm not mm³, 2.405 is 1mm of 1.75mm filament (r*(PI*r), 0.875*PI*0.875
#calcF
speed_weight = remap(speed_weights[e_edges[i]], nozzleboss.min_speed, nozzleboss.max_speed)
F = extrusion_speed*speed_weight
#check if tool color changed and append corresponding textblock
if tool_colors[e_edges[i+1]]!=prev_tool_color:
if tool_colors[e_edges[i+1]]<0.5:
_txt.append(read_textblock('T1'))
else:
_txt.append(read_textblock('T0'))
prev_tool_color=tool_colors[e_edges[i+1]]
_txt.append(extrude(P1, P2, E, F, prev_F))
#print(_txt)
end_code = '\n'+read_textblock('End')
_txt.append(end_code)
gcode_txt.write(''.join(_txt))
gcode_txt.close()
print("took in seconds: ",time.time()-then)
return {'FINISHED'}
class WM_OT_gcode_import(Operator, ImportHelper):
"""Import Gcode, travel lines don't get drawn"""
bl_idname = "wm.gcode_import"
bl_label = "Import G-code"
# ImportHelper mixin class uses this
filename_ext = ".txt"
filter_glob: StringProperty(
default="*.*",
options={'HIDDEN'},
maxlen=255, # Max internal buffer length, longer would be clamped.
)
def execute(self, context):
return import_gcode(context, self.filepath)
class WM_OT_gcode_export(Operator):
bl_idname = "wm.gcode_export"
bl_label = "Export to G-code"
bl_description = "Export active Object to G-code. Result can be found in folder of current .blend file"
@classmethod
def poll(cls, context):
try:
return context.object.type in ('MESH')
except:
return False
def execute(self, context):
return export_gcode(context)
def register():
bpy.utils.register_class(NOZZLEBOSS_PT_Panel)
bpy.utils.register_class(gcode_settings)
bpy.utils.register_class(WM_OT_gcode_import)
bpy.utils.register_class(WM_OT_gcode_export)
bpy.types.Scene.nozzleboss = bpy.props.PointerProperty(type= gcode_settings)
def unregister():
bpy.utils.unregister_class(NOZZLEBOSS_PT_Panel)
if __name__ == "__main__":
register()