forked from zignig/blender-gcode-reader
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathio_import_gcode.py
504 lines (428 loc) · 15.3 KB
/
io_import_gcode.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
492
493
494
495
496
497
498
499
500
501
502
503
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
## blendish python ripped from fro io_import_dxf
import bpy
bl_info = {
'name': 'Import GCode for FDM .gcode',
'author': 'Simon Kirkby',
'version': (0,0,3),
'blender': (2, 5, 6),
'api': 32738,
'location': 'File > Import-Export > Gcode',
'description': 'Import gcode files for reprap FDM printers .gcode',
'warning': 'may not work',
"wiki_url": "",
"tracker_url": "",
'category': 'Import-Export'}
__version__ = '.'.join([str(s) for s in bl_info['version']])
# gcode parser for blender 2.5
# Simon Kirkby
# 201102051305
#modified by David Anderson to handle Skeinforge comments
# Thanks Simon!!!
# modified by Alessandro Ranellucci (2011-10-14)
# to make it compatible with Blender 2.59
# and with modern 5D GCODE
import string,os
import bpy
import mathutils
#file_name ='/home/zignig/cube_export.gcode'
#file_name = 'c:/davelandia/blender/bre_in.gcode'
extrusion_diameter = 0.4
class tool:
def __init__(self,name='null tool'):
self.name = name
class move():
def __init__(self,pos):
self.pos = pos
p = []
p.append(self.pos['X'])
p.append(self.pos['Y'])
p.append(self.pos['Z'])
self.point = p
class fast_move(move):
def __init__(self,pos):
move.__init__(self,pos)
class tool_on:
def __init__(self,val):
pass
class tool_off:
def __init__(self,val):
pass
class layer:
def __init__(self):
print('layer')
pass
class setting:
def __init__(self,val):
pass
class set_temp(setting):
def __init__(self,val):
setting.__init__(self,val)
class tool_change():
def __init__(self,val):
self.val = val
class undef:
def __init__(self,val):
pass
codes = {
'(':{
'</surroundingLoop>)' : undef,
'<surroundingLoop>)' : undef,
'<boundaryPoint>' : undef,
'<loop>)' : undef,
'</loop>)' : undef,
'<layer>)' : undef,
'</layer>)' : undef,
'<layer>' : undef,
'<perimeter>)' : undef,
'</perimeter>)' : undef,
'<bridgelayer>)' : undef,
'</bridgelayer>)' : undef,
'</extrusion>)' :undef
},
'G':{
'0': fast_move,
'1': move,
'01' : move,
'04': undef,
'21': setting,
'28': setting, # go home
'92': setting, # not sure what this is
'90': setting
},
'M':{
'01' : undef,
'6' : undef,
'101' : tool_on,
'103' : tool_off,
'104' : set_temp,
'105' : undef,
'106' : undef,
'107' : undef,
'108' : undef,
'109' : undef, # what is this ?
'113' : undef, # what is this ?
'115' : undef, # what is this ?
'116' : undef, # what is this ?
'117' : undef, # what is this ?
'84' : undef,
'' : undef
},
'T':{
'0;' : tool_change
}
}
class driver:
# takes action object list and runs through it
def __init__(self):
pass
def drive(self):
pass
def load_data(self,data):
self.data = data
def vertsToPoints(Verts):
# main vars
vertArray = []
for v in Verts:
vertArray += v
vertArray.append(0)
return vertArray
def create_poly(verts,counter):
name = 'skein'+str(counter)
pv = vertsToPoints(verts)
# create curve
scene = bpy.context.scene
newCurve = bpy.data.curves.new(name, type = 'CURVE')
newSpline = newCurve.splines.new('POLY')
newSpline.points.add(int(len(pv)*0.25 - 1))
newSpline.points.foreach_set('co',pv)
newSpline.use_endpoint_u = True
# create object with newCurve
newCurve.bevel_object = bpy.data.objects['profile']
newCurve.dimensions = '3D'
new_obj = bpy.data.objects.new(name, newCurve) # object
scene.objects.link(new_obj) # place in active scene
return new_obj
class blender_driver(driver):
def __init__(self):
driver.__init__(self)
def drive(self):
print('building curves')
# info
count = 0
for i in self.data:
if isinstance(i,layer):
count += 1
print('has '+str(count)+' layers')
print('createing poly lines')
if 'profile' in bpy.data.objects:
print('profile exisits')
else:
bpy.ops.curve.primitive_bezier_circle_add()
curve = bpy.context.selected_objects[0]
d = extrusion_diameter
curve.scale = [d,d,d]
curve.name = 'profile'
curve.data.resolution_u = 2
curve.data.render_resolution_u = 2
poly = []
layers = []
this_layer = []
counter = 1
global thing
for i in self.data:
if isinstance(i,move):
#print(i.point)
poly.append(i.point)
if isinstance(i,tool_off):
if len(poly) > 0:
counter += 1
print('creating poly ' + str(counter))
pobj = create_poly(poly,counter)
this_layer.append(pobj)
poly = []
if isinstance(i,layer):
print('layer '+str(len(layers)))
layers.append(this_layer)
this_layer = []
layers.append(this_layer)
print('animating build')
s = bpy.context.scene
# make the material
if 'Extrusion' in bpy.data.materials:
mt = bpy.data.materials['Extrusion']
else:
# make new material
bpy.ops.material.new()
mt = bpy.data.materials[-1]
mt.name = 'Extrusion'
s.frame_end = len(layers)
# hide everything at frame 0
s.frame_set(0)
for i in range(len(layers)):
for j in layers[i]:
j.hide = True
j.hide_render = True
j.keyframe_insert("hide")
j.keyframe_insert("hide_render")
# assign the material
j.active_material = mt
# go through the layers and make them reappear
for i in range(len(layers)):
s.frame_set(i)
print('frame '+str(i))
for j in layers[i]:
j.hide = False
j.hide_render = False
j.keyframe_insert("hide")
j.keyframe_insert("hide_render")
class machine:
def __init__(self,axes):
self.axes = axes
self.axes_num = len(axes)
self.data = []
self.cur = {}
self.tools = []
self.commands = []
self.driver = driver()
def add_tool(self,the_tool):
self.tools.append(the_tool)
def remove_comments(self):
tempd=[]
for st in self.data:
startcommentidx= st.find('(')
if startcommentidx == 0 : #line begins with a comment
split1=st.partition(')')
st = ''
if startcommentidx > 0: # line has an embedded comment to remove
split1=st.partition('(')
split2=split1[2].partition(')')
st = split1[0]+split2[2]
if st != '':
tempd.append(st)
#print("...>",st)
self.data=tempd
def import_file(self,file_name):
print('opening '+file_name)
f = open(file_name)
#for line in f:
# self.data.append(self.remove_comments(line))
self.data=f.readlines()
f.close()
self.remove_comments()
# uncomment to see the striped file
#k = open('c:/davelandia/blender/out1.txt','w')
#k.writelines(self.data)
#k.close()
print(str(len(self.data))+' lines')
def process(self):
# zero up the machine
pos = {}
for i in self.axes:
pos[i] = 0
self.cur[i] = 0
for i in self.data:
i=i.strip()
print( "Parsing Gcode line ", i)
print(pos)
tmp = i.split()
# print("tmp: " + tmp.__str__)
if tmp:
# and len(tmp) != 0:
command = tmp[0][0]
com_type = tmp[0][1:]
if command in codes:
if com_type in codes[command]:
#print('good =>'+command+com_type)
for j in tmp[1:]:
axis = j[0]
if axis == ';':
# ignore comments
break
if axis in self.axes:
val = float(j[1:])
pos[axis] = val
if self.cur['Z'] != pos['Z']:
self.commands.append(layer())
self.commands.append(tool_off(pos))
self.cur[axis] = val
# create action object
#print(pos)
if (pos['E'] == 0):
act = tool_off(pos)
else:
act = codes[command][com_type](pos)
#print(act)
self.commands.append(act)
#if isinstance(act,move):
#print(act.coord())
else:
print(i)
print(' G/M/T Code for this line is unknowm ' + com_type)
#break
else:
print(' line does not have a G/M/T Command ')
# + str(command))
#break
self.commands.append(tool_off(pos))
def import_gcode(file_name):
print('hola')
m = machine(['X','Y','Z','E'])
m.import_file(file_name)
m.process()
d = blender_driver()
d.load_data(m.commands)
d.drive()
print('finished parsing... done')
DEBUG= False
from bpy.props import *
def tripleList(list1):
list3 = []
for elt in list1:
list3.append((elt,elt,elt))
return list3
theMergeLimit = 4
theCodec = 1
theCircleRes = 1
class IMPORT_OT_gcode(bpy.types.Operator):
'''Imports Reprap FDM gcode'''
bl_idname = "import_scene.gocde"
bl_description = 'Gcode reader, reads tool moves and animates layer build'
bl_label = "Import gcode" +' v.'+ __version__
bl_space_type = "PROPERTIES"
bl_region_type = "WINDOW"
filepath = StringProperty(name="File Path", description="Filepath used for importing the GCode file", maxlen= 1024, default= "")
#new_scene = BoolProperty(name="Replace scene", description="Replace scene", default=toggle&T_NewScene)
#new_scene = BoolProperty(name="New scene", description="Create new scene", default=toggle&T_NewScene)
#curves = BoolProperty(name="Draw curves", description="Draw entities as curves", default=toggle&T_Curves)
#thic_on = BoolProperty(name="Thic ON", description="Support THICKNESS", default=toggle&T_ThicON)
#
# merge = BoolProperty(name="Remove doubles", description="Merge coincident vertices", default=toggle&T_Merge)
# mergeLimit = FloatProperty(name="Limit", description="Merge limit", default = theMergeLimit*1e4,min=1.0, soft_min=1.0, max=100.0, soft_max=100.0)
# draw_one = BoolProperty(name="Merge all", description="Draw all into one mesh-object", default=toggle&T_DrawOne)
# circleResolution = IntProperty(name="Circle resolution", description="Circle/Arc are aproximated will this factor", default = theCircleRes,
# min=4, soft_min=4, max=360, soft_max=360)
# codecs = tripleList(['iso-8859-15', 'utf-8', 'ascii'])
# codec = EnumProperty(name="Codec", description="Codec", items=codecs, default = 'ascii')
# debug = BoolProperty(name="Debug", description="Unknown DXF-codes generate errors", default=toggle&T_Debug)
# verbose = BoolProperty(name="Verbose", description="Print debug info", default=toggle&T_Verbose)
##### DRAW #####
def draw(self, context):
layout0 = self.layout
#layout0.enabled = False
#col = layout0.column_flow(2,align=True)
# layout = layout0.box()
# col = layout.column()
# #col.prop(self, 'KnotType') waits for more knottypes
# #col.label(text="import Parameters")
# #col.prop(self, 'replace')
# col.prop(self, 'new_scene')
#
# row = layout.row(align=True)
# row.prop(self, 'curves')
# row.prop(self, 'circleResolution')
#
# row = layout.row(align=True)
# row.prop(self, 'merge')
# if self.merge:
# row.prop(self, 'mergeLimit')
# row = layout.row(align=True)
#row.label('na')
# row.prop(self, 'draw_one')
# row.prop(self, 'thic_on')
#
# col = layout.column()
# col.prop(self, 'codec')
#
# row = layout.row(align=True)
# row.prop(self, 'debug')
# if self.debug:
# row.prop(self, 'verbose')
#
def execute(self, context):
global toggle, theMergeLimit, theCodec, theCircleRes
#O_Merge = T_Merge if self.properties.merge else 0
#O_Replace = T_Replace if self.properties.replace else 0
#O_NewScene = T_NewScene if self.properties.new_scene else 0
#O_Curves = T_Curves if self.properties.curves else 0
#O_ThicON = T_ThicON if self.properties.thic_on else 0
#O_DrawOne = T_DrawOne if self.properties.draw_one else 0
#O_Debug = T_Debug if self.properties.debug else 0
#O_Verbose = T_Verbose if self.properties.verbose else 0
#toggle = O_Merge | O_DrawOne | O_NewScene | O_Curves | O_ThicON | O_Debug | O_Verbose
#theMergeLimit = self.properties.mergeLimit*1e-4
#theCircleRes = self.properties.circleResolution
#theCodec = self.properties.codec
import_gcode(self.properties.filepath)
return {'FINISHED'}
def invoke(self, context, event):
wm = context.window_manager
wm.fileselect_add(self)
return {'RUNNING_MODAL'}
def menu_func(self, context):
self.layout.operator(IMPORT_OT_gcode.bl_idname, text="Reprap GCode (.gcode)").filepath = "*.gcode"
def register():
bpy.utils.register_module(__name__)
bpy.types.INFO_MT_file_import.append(menu_func)
def unregister():
bpy.utils.unregister_module(__name__)
bpy.types.INFO_MT_file_import.remove(menu_func)
if __name__ == "__main__":
register()