-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgcode_importer.py
200 lines (159 loc) · 6.02 KB
/
gcode_importer.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
import bpy
import os
from bpy.props import StringProperty
from bpy_extras.io_utils import ImportHelper
from bpy.types import Operator
bl_info = {
"name": "GCode Importer",
"author": "Kevin Nunley",
"version": (1, 0, 0),
"blender": (2, 90, 0),
"location": "File > Import",
"description": "Import GCode files and visualize them as 3D models",
"warning": "",
"doc_url": "https://github.com/kmnunley/Blender-GCode-Importer",
"category": "Import-Export",
}
def create_paths(gcode_lines):
# Initialize the toolhead position and extruder temperature
toolhead_pos = (0, 0, 0)
# Create an empty collection to store the paths
collection = bpy.data.collections.new("Paths")
absolute_coord = True
absolute_extrude = False
x = 0
y = 0
z = 0
e = 0
max_e = 0
point_data = []
def get_params(params):
coord = [None, None, None, None]
for param in params:
try:
if param[0] == "X":
coord[0] = float(param[1:])
elif param[0] == "Y":
coord[1] = float(param[1:])
elif param[0] == "Z":
coord[2] = float(param[1:])
elif param[0] == "E":
coord[3] = float(param[1:])
except:
pass
return tuple(coord)
# Iterate through the gcode instructions
for line in gcode_lines:
# Skip comments
if line[0] == ";":
continue
# Split the line into words
words = line.split()
if not words:
continue
# Extract the command and parameters
command = words[0]
params = words[1:]
# Handle the movement command
if command == "G1" or command == "G0":
coord = get_params(params)
if absolute_coord:
toolhead_pos = (
toolhead_pos[0] if coord[0] is None else coord[0],
toolhead_pos[1] if coord[1] is None else coord[1],
toolhead_pos[2] if coord[2] is None else coord[2]
)
else:
toolhead_pos = toolhead_pos + (
0 if coord[0] is None else coord[0],
0 if coord[1] is None else coord[1],
0 if coord[2] is None else coord[2]
)
if coord[3] is not None:
if absolute_extrude:
e = coord[3]
else:
e = e + coord[3]
if e >= max_e:
# Update the toolhead position and add the point to the curve data
point_data.append(toolhead_pos)
max_e = e
else:
# Check if there are enough points to create a curve
if len(point_data) >= 2:
# Dump the curve data to a new curve object
# Create a new curve object
curve_data = bpy.data.curves.new("Path", type='CURVE')
curve_data.dimensions = '3D'
curve_data.resolution_u = 1
# Create a curve spline and add the toolhead position as a control point
curve_spline = curve_data.splines.new('BEZIER')
for index, point in enumerate(point_data):
if index == 0:
curve_spline.bezier_points[0].co = point
else:
curve_spline.bezier_points.add(1)
curve_spline.bezier_points[-1].co = point
# Create a new object to hold the curve data
curve_object = bpy.data.objects.new("Path", curve_data)
#curve_object.location = (0, 0, 0)
# Link the object to the scene and the collection
bpy.context.collection.objects.link(curve_object)
collection.objects.link(curve_object)
# Reset the point data
point_data = []
# Handle mode commands
elif command == "M82":
absolute_extrude = True
elif command == "M83":
absolute_extrude = False
elif command == "G90":
absolute_coord = True
absolute_extrude = True
elif command == "G91":
absolute_coord = False
absolute_extrude = False
elif command == "G92":
coord = get_params(params)
toolhead_pos = (
toolhead_pos[0] if coord[0] is None else coord[0],
toolhead_pos[1] if coord[1] is None else coord[1],
toolhead_pos[2] if coord[2] is None else coord[2]
)
if coord[3] is not None:
e = coord[3]
max_e = e
def import_gcode(filepath):
# Load the gcode file
gcode_file = open(filepath, "r")
gcode_lines = gcode_file.readlines()
# Create the geometry
create_paths(gcode_lines)
# Define the operator class
class ImportGCodeOperator(Operator, ImportHelper):
bl_idname = "import_gcode.operator"
bl_label = "Import GCode"
filter_glob: StringProperty(
default="*.gcode",
options={'HIDDEN'},
)
def execute(self, context):
filename, extension = os.path.splitext(self.filepath)
import_gcode(self.filepath)
return {'FINISHED'}
@bpy.app.handlers.persistent
def register():
# Register the operator
bpy.utils.register_class(ImportGCodeOperator)
# Add the operator to the File > Import menu
bpy.types.TOPBAR_MT_file_import.append(menu_func)
@bpy.app.handlers.persistent
def unregister():
# Remove the operator from the File > Import menu
bpy.types.TOPBAR_MT_file_import.remove(menu_func)
# Unregister the operator
bpy.utils.unregister_class(ImportGCodeOperator)
def menu_func(self, context):
self.layout.operator(ImportGCodeOperator.bl_idname, text="GCode (.gcode)")
if __name__ == "__main__":
register()