From b33ac7f3dd1cef3f221b7db1b1dee6ca3407f178 Mon Sep 17 00:00:00 2001 From: imsenthur Date: Fri, 31 Jul 2020 15:40:00 +0530 Subject: [PATCH] Naive fix with a safety net. --- processor.py | 83 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 52 insertions(+), 31 deletions(-) diff --git a/processor.py b/processor.py index fd2ab45..c4a643e 100644 --- a/processor.py +++ b/processor.py @@ -40,13 +40,15 @@ def batches(x, n): yield x[i:i + n] @staticmethod - def split_vertex(index): - layer.data.splines[index].bezier_points[1].select_control_point = True + def split_vertex(index, layer): + ops.curve.select_all(action='DESELECT') + layer.data.splines[index].bezier_points[-1].select_control_point = True + layer.data.splines[index].bezier_points[-2].select_control_point = True ops.curve.delete(type='SEGMENT') ops.curve.select_all(action='DESELECT') - index += 1 + layer.data.splines[-1].bezier_points[-1].select_control_point = True - layer.data.splines[index].bezier_points[0].select_control_point = True + index += 1 return index def execute(self, context): @@ -60,26 +62,32 @@ def execute(self, context): obj_collection = bpy.data.collections.new(filename) context.scene.collection.children.link(obj_collection) - filtered_lines=[[]] + layered_gcodes=[[]] vertices = [[]] - count = -1 - pattern = re.compile(r'(;LAYER:|G[0|1] [F|X][a-zA-Z0-9. ]+ [E|Y][0-9. ]+\d\s$)') - sub_pattern = re.compile(r'G(?P[0|1])\s?[a-zA-Z0-9]*\sX(?P[0-9.]*)\sY(?P[0-9.]*)') + pattern = re.compile(r';LAYER:|G[0|1]') + sub_pattern = re.compile(r'G(?P[0|1])\s?[a-zA-Z0-9.]*\sX(?P[0-9.]*)\sY(?P[0-9.]*)') - with open(self.filepath, 'r') as file: - line = file.readline() - while(line): + with open(self.filepath, 'r') as f: + lines = f.readlines() + + for i, line in enumerate(lines): matches = pattern.finditer(line) for match in matches: if(line[0] == ';'): - count += 1 - filtered_lines.append([]) + layered_gcodes.append([]) else: - filtered_lines[count].append(line) - line = file.readline() + if re.search(r'\w*[X|Y][0-9. a-zA-z]', line): + if re.search(r'E[0-9.]*', line): + layered_gcodes[-1].append(line) + else: + if i+10: index = 0 ops.curve.primitive_bezier_curve_add(location=_layer[0][1:],radius=0, enter_editmode=True) @@ -115,23 +128,31 @@ def execute(self, context): layer.data.splines[index].bezier_points[0].handle_left_type = 'VECTOR' layer.data.splines[index].bezier_points[0].handle_right_type = 'VECTOR' - for v in tqdm(_layer): + for v in tqdm(_layer[1:]): ops.curve.vertex_add(location=v[1:]) - if(v[0]==0): - try: - index = self.split_vertex(index) - except: - pass + if v[0] == 0: + ops.curve.select_all(action='DESELECT') + layer.data.splines[index].bezier_points[-1].select_control_point = True + layer.data.splines[index].bezier_points[-2].select_control_point = True + + ops.curve.delete(type='SEGMENT') + + ops.curve.select_all(action='DESELECT') + layer.data.splines[-1].bezier_points[-1].select_control_point = True + + index += 1 ops.object.editmode_toggle() context.object.data.twist_mode = 'Z_UP' - context.object.data.bevel_depth = self.nozzle_dia + context.object.data.bevel_depth = self.nozzle_dia/2 obj_collection.objects.link(layer) - bpy.data.collections[0].objects.unlink(layer) + bpy.data.collections['Collection'].objects.unlink(layer) ops.object.select_all(action='DESELECT') count += 1 + + print("\nEXPORTED "+ str(i) +" LAYERS TO 3D-VIEWPORT :)\n")