class Toolpath: def __init__(self): self.length = 0.0 self.lines = [] self.current_pos = 0.0 self.from_position = Point(0, 0, 0) self.current_point = self.from_position self.current_line_index = 0 self.tools = {} # dictionary, tool id to Tool object self.rapid_flag = True self.mm_per_sec = 50.0 self.running = False self.coords = Coords(0, 0, 0, 0, 0, 0) self.in_cut_to_position = False self.x = 0 self.y = 0 self.z = 50 self.t = None def add_line(self, p0, p1): self.lines.append(Line(p0, p1, self.rapid_flag, self.t)) def rewind(self): self.current_point = Point(0, 0, 0) if len(self.lines) > 0: self.current_point = self.lines[0].p0 self.current_pos = 0.0 self.current_line_index = 0 self.running = False def draw_tool(self): voxelcut.drawclear() index = self.current_line_index if index < 0: index = 0 if index >= len(self.lines): return tool_number = self.lines[index].tool_number rapid = self.lines[index].rapid if tool_number in self.tools: x, y, z = self.coords.mm_to_voxels(self.current_point.x, self.current_point.y, self.current_point.z) self.tools[tool_number].draw(x, y, z, rapid) def cut_point(self, p): x, y, z = self.coords.mm_to_voxels(p.x, p.y, p.z) index = self.current_line_index if index < 0: index = 0 tool_number = self.lines[index].tool_number rapid = self.lines[index].rapid if tool_number in self.tools: self.tools[tool_number].cut(x, y, z, rapid) def cut_line(self, line): length = line.Length() num_segments = int(1 + length * self.coords.voxels_per_mm * 0.2) step = length / num_segments dv = (line.p1 - line.p0) * (1.0 / num_segments) for i in range(0, num_segments + 1): p = line.p0 + (dv * i) self.cut_point(p) def cut_to_position(self, pos): if self.current_line_index >= len(self.lines): return if self.cut_to_position == True: import wx wx.MessageBox("in cut_to_position again!") self.in_cut_to_position = True start_pos = self.current_pos while self.current_line_index < len(self.lines): line = copy.copy(self.lines[self.current_line_index]) line.p0 = self.current_point line_length = line.Length() if line_length > 0: end_pos = self.current_pos + line_length if pos < end_pos: fraction = (pos - self.current_pos) / (end_pos - self.current_pos) line.p1 = line.p0 + ((line.p1 - line.p0) * fraction) self.cut_line(line) self.current_pos = pos self.current_point = line.p1 break self.cut_line(line) self.current_pos = end_pos self.current_point = line.p1 self.current_line_index = self.current_line_index + 1 self.in_cut_to_position = False def begin_ncblock(self): pass def end_ncblock(self): pass def add_text(self, s, col, cdata): pass def set_mode(self, units): pass def metric(self): pass def imperial(self): pass def begin_path(self, col): pass def end_path(self): pass def rapid(self, x=None, y=None, z=None, a=None, b=None, c=None): self.rapid_flag = True if x == None: x = self.x if y == None: y = self.y if z == None: z = self.z self.add_line(Point(self.x, self.y, self.z), Point(x, y, z)) self.x = x self.y = y self.z = z def feed(self, x=None, y=None, z=None, a=None, b=None, c=None): self.rapid_flag = False if x == None: x = self.x if y == None: y = self.y if z == None: z = self.z self.add_line(Point(self.x, self.y, self.z), Point(x, y, z)) self.x = x self.y = y self.z = z def arc(self, dir, x, y, z, i, j, k, r): self.rapid_flag = False if x == None: x = self.x if y == None: y = self.y if z == None: z = self.z area.set_units(0.05) curve = area.Curve() curve.append(area.Point(self.x, self.y)) curve.append(area.Vertex(dir, area.Point(x, y), area.Point(i, j))) curve.UnFitArcs() for span in curve.GetSpans(): self.add_line(Point(span.p.x, span.p.y, z), Point(span.v.p.x, span.v.p.y, z)) self.x = x self.y = y self.z = z def arc_cw(self, x=None, y=None, z=None, i=None, j=None, k=None, r=None): self.arc(-1, x, y, z, i, j, k, r) def arc_ccw(self, x=None, y=None, z=None, i=None, j=None, k=None, r=None): self.arc(1, x, y, z, i, j, k, r) def tool_change(self, id): self.t = id def current_tool(self): return self.t def spindle(self, s, clockwise): pass def feedrate(self, f): pass
class Toolpath: def __init__(self): self.length = 0.0 self.lines = [] self.current_pos = 0.0 self.current_point = Point(0, 0, 0) self.current_line_index = 0 self.tools = {} # dictionary, tool id to Tool object self.current_tool = 1 self.rapid = True self.mm_per_sec = 50.0 self.running = False self.coords = Coords(0, 0, 0, 0, 0, 0) self.in_cut_to_position = False def add_line(self, p0, p1): self.lines.append(Line(p0, p1, self.rapid, self.current_tool)) def load(self, nc_filepath): # this converts the G1s in an NC file into arcs with G2 or G3 pattern_main = re.compile( '([(!;].*|\s+|[a-zA-Z0-9_:](?:[+-])?\d*(?:\.\d*)?|\w\#\d+|\(.*?\)|\#\d+\=(?:[+-])?\d*(?:\.\d*)?)' ) self.lines = [] self.length = 0.0 file = open(nc_filepath, 'r') arc = 0 self.rapid = False curx = None cury = None curz = None while (True): line = file.readline().rstrip() if len(line) == 0: break move = False x = None y = None z = None i = None j = None words = pattern_main.findall(line) for word in words: word = word.upper() if word == 'G1' or word == 'G01': self.rapid = False arc = 0 elif word == 'G2' or word == 'G02': self.rapid = False arc = -1 elif word == 'G3' or word == 'G03': self.rapid = False arc = 1 elif word == 'G0' or word == 'G00': self.rapid = True arc = 0 elif word[0] == 'X': x = eval(word[1:]) move = True elif word[0] == 'Y': y = eval(word[1:]) move = True elif word[0] == 'Z': z = eval(word[1:]) move = True elif word[0] == 'I': i = float(eval(word[1:])) elif word[0] == 'J': j = float(eval(word[1:])) elif word[0] == 'T': self.current_tool = eval(word[1:]) if (curx != None) and (cury != None) and (curz != None): self.add_line(Point(curx, cury, curz), Point(curx, cury, 30.0)) curz = 30.0 elif word[0] == ';': break if move: if (curx != None) and (cury != None) and (curz != None): newx = curx newy = cury newz = curz if x != None: newx = float(x) if y != None: newy = float(y) if z != None: newz = float(z) if arc != 0: area.set_units(0.05) curve = area.Curve() curve.append(area.Point(curx, cury)) # next 4 lines were for Bridgeport. # this only works for LinuxCNC now #if (newx > curx) != (arc > 0): # j = -j #if (newy > cury) != (arc < 0): # i = -i curve.append( area.Vertex(arc, area.Point(newx, newy), area.Point(curx + i, cury + j))) curve.UnFitArcs() for span in curve.GetSpans(): self.add_line(Point(span.p.x, span.p.y, newz), Point(span.v.p.x, span.v.p.y, newz)) else: self.add_line(Point(curx, cury, curz), Point(newx, newy, newz)) if x != None: curx = float(x) if y != None: cury = float(y) if z != None: curz = float(z) for line in self.lines: self.length += line.Length() file.close() self.rewind() def rewind(self): self.current_point = Point(0, 0, 0) if len(self.lines) > 0: self.current_point = self.lines[0].p0 self.current_pos = 0.0 self.current_line_index = 0 self.running = False def draw_tool(self): voxelcut.drawclear() index = self.current_line_index - 1 if index < 0: index = 0 tool_number = self.lines[index].tool_number if tool_number in self.tools: x, y, z = self.coords.mm_to_voxels(self.current_point.x, self.current_point.y, self.current_point.z) self.tools[tool_number].draw(x, y, z) def cut_point(self, p): x, y, z = self.coords.mm_to_voxels(p.x, p.y, p.z) index = self.current_line_index - 1 if index < 0: index = 0 tool_number = self.lines[index].tool_number if tool_number in self.tools: self.tools[tool_number].cut(x, y, z) def cut_line(self, line): # self.cut_point(line.p0) # self.cut_point(line.p1) # voxelcut.remove_line(int(line.p0.x), int(line.p0.y), int(line.p0.z), int(line.p1.x), int(line.p1.y), int(line.p1.z), 5) length = line.Length() num_segments = int(1 + length * self.coords.voxels_per_mm * 0.06) step = length / num_segments dv = (line.p1 - line.p0) * (1.0 / num_segments) for i in range(0, num_segments + 1): p = line.p0 + (dv * i) self.cut_point(p) def cut_to_position(self, pos): if self.current_line_index >= len(self.lines): return if self.cut_to_position == True: import wx wx.MessageBox("in cut_to_position again!") self.in_cut_to_position = True start_pos = self.current_pos while self.current_line_index < len(self.lines): line = copy.copy(self.lines[self.current_line_index]) line.p0 = self.current_point line_length = line.Length() if line_length > 0: end_pos = self.current_pos + line_length if pos < end_pos: fraction = (pos - self.current_pos) / (end_pos - self.current_pos) line.p1 = line.p0 + ((line.p1 - line.p0) * fraction) self.cut_line(line) self.current_pos = pos self.current_point = line.p1 break self.cut_line(line) self.current_pos = end_pos self.current_point = line.p1 self.current_line_index = self.current_line_index + 1 self.in_cut_to_position = False
class Toolpath: def __init__(self): self.length = 0.0 self.lines = [] self.current_pos = 0.0 self.from_position = Point(0, 0, 0) self.current_point = self.from_position self.current_line_index = 0 self.tools = {} # dictionary, tool id to Tool object self.rapid_flag = True self.mm_per_sec = 50.0 self.running = False self.coords = Coords(0, 0, 0, 0, 0, 0) self.in_cut_to_position = False self.x = 0 self.y = 0 self.z = 50 self.t = None def add_line(self, p0, p1): self.lines.append(Line(p0, p1, self.rapid_flag, self.t)) def rewind(self): self.current_point = Point(0, 0, 0) if len(self.lines)>0: self.current_point = self.lines[0].p0 self.current_pos = 0.0 self.current_line_index = 0 self.running = False def draw_tool(self): voxelcut.drawclear() index = self.current_line_index if index < 0: index = 0 if index >= len(self.lines): return tool_number = self.lines[index].tool_number rapid = self.lines[index].rapid if tool_number in self.tools: x, y, z = self.coords.mm_to_voxels(self.current_point.x, self.current_point.y, self.current_point.z) self.tools[tool_number].draw(x, y, z, rapid) def cut_point(self, p): x, y, z = self.coords.mm_to_voxels(p.x, p.y, p.z) index = self.current_line_index if index < 0: index = 0 tool_number = self.lines[index].tool_number rapid = self.lines[index].rapid if tool_number in self.tools: self.tools[tool_number].cut(x, y, z, rapid) def cut_line(self, line): length = line.Length() num_segments = int(1 + length * self.coords.voxels_per_mm * 0.2) step = length/num_segments dv = (line.p1 - line.p0) * (1.0/num_segments) for i in range (0, num_segments + 1): p = line.p0 + (dv * i) self.cut_point(p) def cut_to_position(self, pos): if self.current_line_index >= len(self.lines): return if self.cut_to_position == True: import wx wx.MessageBox("in cut_to_position again!") self.in_cut_to_position = True start_pos = self.current_pos while self.current_line_index < len(self.lines): line = copy.copy(self.lines[self.current_line_index]) line.p0 = self.current_point line_length = line.Length() if line_length > 0: end_pos = self.current_pos + line_length if pos < end_pos: fraction = (pos - self.current_pos)/(end_pos - self.current_pos) line.p1 = line.p0 + ((line.p1 - line.p0) * fraction) self.cut_line(line) self.current_pos = pos self.current_point = line.p1 break self.cut_line(line) self.current_pos = end_pos self.current_point = line.p1 self.current_line_index = self.current_line_index + 1 self.in_cut_to_position = False def begin_ncblock(self): pass def end_ncblock(self): pass def add_text(self, s, col, cdata): pass def set_mode(self, units): pass def metric(self): pass def imperial(self): pass def begin_path(self, col): pass def end_path(self): pass def rapid(self, x=None, y=None, z=None, a=None, b=None, c=None): self.rapid_flag = True if x == None: x = self.x if y == None: y = self.y if z == None: z = self.z self.add_line(Point(self.x, self.y, self.z), Point(x, y, z)) self.x = x self.y = y self.z = z def feed(self, x=None, y=None, z=None, a=None, b=None, c=None): self.rapid_flag = False if x == None: x = self.x if y == None: y = self.y if z == None: z = self.z self.add_line(Point(self.x, self.y, self.z), Point(x, y, z)) self.x = x self.y = y self.z = z def arc(self, dir, x, y, z, i, j, k, r): self.rapid_flag = False if x == None: x = self.x if y == None: y = self.y if z == None: z = self.z area.set_units(0.05) curve = area.Curve() curve.append(area.Point(self.x, self.y)) curve.append(area.Vertex(dir, area.Point(x, y), area.Point(i, j))) curve.UnFitArcs() for span in curve.GetSpans(): self.add_line(Point(span.p.x, span.p.y, z), Point(span.v.p.x, span.v.p.y, z)) self.x = x self.y = y self.z = z def arc_cw(self, x=None, y=None, z=None, i=None, j=None, k=None, r=None): self.arc(-1, x, y, z, i, j, k, r) def arc_ccw(self, x=None, y=None, z=None, i=None, j=None, k=None, r=None): self.arc(1, x, y, z, i, j, k, r) def tool_change(self, id): self.t = id def current_tool(self): return self.t def spindle(self, s, clockwise): pass def feedrate(self, f): pass
class Toolpath: def __init__(self): self.length = 0.0 self.lines = [] self.current_pos = 0.0 self.current_point = Point(0, 0, 0) self.current_line_index = 0 self.tools = {} # dictionary, tool id to Tool object self.current_tool = 1 self.rapid = True self.mm_per_sec = 50.0 self.running = False self.coords = Coords(0, 0, 0, 0, 0, 0) self.in_cut_to_position = False def add_line(self, p0, p1): self.lines.append(Line(p0, p1, self.rapid, self.current_tool)) def load(self, nc_filepath): # this converts the G1s in an NC file into arcs with G2 or G3 pattern_main = re.compile('([(!;].*|\s+|[a-zA-Z0-9_:](?:[+-])?\d*(?:\.\d*)?|\w\#\d+|\(.*?\)|\#\d+\=(?:[+-])?\d*(?:\.\d*)?)') self.lines = [] self.length = 0.0 file = open(nc_filepath, 'r') arc = 0 self.rapid = False curx = None cury = None curz = None while(True): line = file.readline().rstrip() if len(line)== 0: break move = False x = None y = None z = None i = None j = None words = pattern_main.findall(line) for word in words: word = word.upper() if word == 'G1' or word == 'G01': self.rapid = False arc = 0 elif word == 'G2' or word == 'G02': self.rapid = False arc = -1 elif word == 'G3' or word == 'G03': self.rapid = False arc = 1 elif word == 'G0' or word == 'G00': self.rapid = True arc = 0 elif word[0] == 'X': x = eval(word[1:]) move = True elif word[0] == 'Y': y = eval(word[1:]) move = True elif word[0] == 'Z': z = eval(word[1:]) move = True elif word[0] == 'I': i = float(eval(word[1:])) elif word[0] == 'J': j = float(eval(word[1:])) elif word[0] == 'T': self.current_tool = eval(word[1:]) if (curx != None) and (cury != None) and (curz != None): self.add_line(Point(curx, cury, curz ), Point(curx, cury, 30.0)) curz = 30.0 elif word[0] == ';' : break if move: if (curx != None) and (cury != None) and (curz != None): newx = curx newy = cury newz = curz if x != None: newx = float(x) if y != None: newy = float(y) if z != None: newz = float(z) if arc != 0: area.set_units(0.05) curve = area.Curve() curve.append(area.Point(curx, cury)) # next 4 lines were for Bridgeport. # this only works for LinuxCNC now #if (newx > curx) != (arc > 0): # j = -j #if (newy > cury) != (arc < 0): # i = -i curve.append(area.Vertex(arc, area.Point(newx, newy), area.Point(curx+i, cury+j))) curve.UnFitArcs() for span in curve.GetSpans(): self.add_line(Point(span.p.x, span.p.y, newz), Point(span.v.p.x, span.v.p.y, newz)) else: self.add_line(Point(curx, cury, curz), Point(newx, newy, newz)) if x != None: curx = float(x) if y != None: cury = float(y) if z != None: curz = float(z) for line in self.lines: self.length += line.Length() file.close() self.rewind() def rewind(self): self.current_point = Point(0, 0, 0) if len(self.lines)>0: self.current_point = self.lines[0].p0 self.current_pos = 0.0 self.current_line_index = 0 self.running = False def draw_tool(self): voxelcut.drawclear() index = self.current_line_index - 1 if index < 0: index = 0 tool_number = self.lines[index].tool_number if tool_number in self.tools: x, y, z = self.coords.mm_to_voxels(self.current_point.x, self.current_point.y, self.current_point.z) self.tools[tool_number].draw(x, y, z) def cut_point(self, p): x, y, z = self.coords.mm_to_voxels(p.x, p.y, p.z) index = self.current_line_index - 1 if index < 0: index = 0 tool_number = self.lines[index].tool_number if tool_number in self.tools: self.tools[tool_number].cut(x, y, z) def cut_line(self, line): # self.cut_point(line.p0) # self.cut_point(line.p1) # voxelcut.remove_line(int(line.p0.x), int(line.p0.y), int(line.p0.z), int(line.p1.x), int(line.p1.y), int(line.p1.z), 5) length = line.Length() num_segments = int(1 + length * self.coords.voxels_per_mm * 0.06) step = length/num_segments dv = (line.p1 - line.p0) * (1.0/num_segments) for i in range (0, num_segments + 1): p = line.p0 + (dv * i) self.cut_point(p) def cut_to_position(self, pos): if self.current_line_index >= len(self.lines): return if self.cut_to_position == True: import wx wx.MessageBox("in cut_to_position again!") self.in_cut_to_position = True start_pos = self.current_pos while self.current_line_index < len(self.lines): line = copy.copy(self.lines[self.current_line_index]) line.p0 = self.current_point line_length = line.Length() if line_length > 0: end_pos = self.current_pos + line_length if pos < end_pos: fraction = (pos - self.current_pos)/(end_pos - self.current_pos) line.p1 = line.p0 + ((line.p1 - line.p0) * fraction) self.cut_line(line) self.current_pos = pos self.current_point = line.p1 break self.cut_line(line) self.current_pos = end_pos self.current_point = line.p1 self.current_line_index = self.current_line_index + 1 self.in_cut_to_position = False
class Toolpath: def __init__(self): self.length = 0.0 self.lines = [] self.current_pos = 0.0 self.current_point = Point(0, 0, 0) self.current_line_index = 0 self.tools = {} # dictionary, tool id to Tool object self.current_tool = 1 self.rapid = True self.mm_per_sec = 50.0 self.running = False self.coords = Coords(0, 0, 0, 0, 0, 0) self.in_cut_to_position = False def add_line(self, p0, p1): self.lines.append(Line(p0, p1, self.rapid, self.current_tool)) def Reset(self): global toolpath toolpath = self # get the box of all the solids box = wx.GetApp().program.stocks.GetBox() if box.valid: c = box.Center() width = box.Width() height = box.Height() depth = box.Depth() + 10 minz = box.MinZ() - 10 if width < 100: width = 100 if height < 100: height = 100 box.InsertBox( geom.Box3D(c.x - width / 2, c.y - height / 2, minz, c.x + width / 2, c.y + height / 2, minz + depth)) else: box.InsertBox(geom.Box3D(-100, -100, -50, 100, 100, 50)) self.coords = Coords(box.MinX(), box.MinY(), box.MinZ(), box.MaxX(), box.MaxY(), box.MaxZ()) self.coorfs.add_block(0, 0, -10, 100, 100, 10) # add each stock stocks = wx.GetApp().program.stocks.GetChildren() for stock in stocks: stock_box = stock.GetBox() sim.set_current_color(stock.GetColor().ref()) c = box.Center() self.coords.add_block(c.x, c.y, box.MinZ(), box.Width(), box.Height(), box.Depth()) tools = wx.GetApp().program.tools.GetChildren() for tool in tools: self.tools[tool.tool_number] = GetSimToolDefinition(tool) machine_module = __import__('nc.' + wx.GetApp().program.machine.reader, fromlist=['dummy']) parser = machine_module.Parser(self) parser.Parse(wx.GetApp().program.GetOutputFileName()) self.rewind() self.timer = wx.Timer(wx.GetApp().frame, wx.ID_ANY) self.timer.Start(33) wx.GetApp().frame.Bind(wx.EVT_TIMER, OnTimer) def rewind(self): self.current_point = Point(0, 0, 0) if len(self.lines) > 0: self.current_point = self.lines[0].p0 self.current_pos = 0.0 self.current_line_index = 0 self.running = False def draw_tool(self): sim.drawclear() index = self.current_line_index - 1 if index < 0: index = 0 tool_number = self.lines[index].tool_number if tool_number in self.tools: x, y, z = self.coords.mm_to_voxels(self.current_point.x, self.current_point.y, self.current_point.z) self.tools[tool_number].draw(x, y, z) def cut_point(self, p): x, y, z = self.coords.mm_to_voxels(p.x, p.y, p.z) index = self.current_line_index - 1 if index < 0: index = 0 tool_number = self.lines[index].tool_number if tool_number in self.tools: self.tools[tool_number].cut(x, y, z) def cut_line(self, line): # self.cut_point(line.p0) # self.cut_point(line.p1) # sim.remove_line(int(line.p0.x), int(line.p0.y), int(line.p0.z), int(line.p1.x), int(line.p1.y), int(line.p1.z), 5) length = line.Length() num_segments = int(1 + length * self.coords.voxels_per_mm * 0.06) step = length / num_segments dv = (line.p1 - line.p0) * (1.0 / num_segments) for i in range(0, num_segments + 1): p = line.p0 + (dv * i) self.cut_point(p) def cut_to_position(self, pos): if self.current_line_index >= len(self.lines): return if self.cut_to_position == True: import wx wx.MessageBox("in cut_to_position again!") self.in_cut_to_position = True start_pos = self.current_pos while self.current_line_index < len(self.lines): line = copy.copy(self.lines[self.current_line_index]) line.p0 = self.current_point line_length = line.Length() if line_length > 0: end_pos = self.current_pos + line_length if pos < end_pos: fraction = (pos - self.current_pos) / (end_pos - self.current_pos) line.p1 = line.p0 + ((line.p1 - line.p0) * fraction) self.cut_line(line) self.current_pos = pos self.current_point = line.p1 break self.cut_line(line) self.current_pos = end_pos self.current_point = line.p1 self.current_line_index = self.current_line_index + 1 self.in_cut_to_position = False