📄 axis.py
字号:
glTranslatef(0, 0, -0.1) glRotatef(90, 1, 0, 0) glScalef(0.2, 0.2, 0.2) hershey.plot_string("X", 0.5) glPopMatrix() glColor3f(*o.colors['axis_y']) glBegin(GL_LINES); glVertex3f(0.0,0.0,0.0) glVertex3f(0.0,1.0,0.0) glEnd(); if view != y: glPushMatrix() glTranslatef(0, 1.2, 0) if view == x: glTranslatef(0, 0, -0.1) glRotatef(90, 0, 1, 0) glRotatef(90, 0, 0, 1) glScalef(0.2, 0.2, 0.2) hershey.plot_string("Y", 0.5) glPopMatrix() glColor3f(*o.colors['axis_z']) glBegin(GL_LINES); glVertex3f(0.0,0.0,0.0) glVertex3f(0.0,0.0,1.0) glEnd(); if view != z: glPushMatrix() glTranslatef(0, 0, 1.2) if lathe: glRotatef(-90, 0, 1, 0) if view == x: glRotatef(90, 0, 1, 0) glRotatef(90, 0, 0, 1) elif view == y or view == p: glRotatef(90, 1, 0, 0) if lathe: glTranslatef(0, -.1, 0) glScalef(0.2, 0.2, 0.2) hershey.plot_string("Z", 0.5) glPopMatrix()def toggle_perspective(e): o.perspective = not o.perspective o.tkRedraw()def select_line(event): i = t.index("@%d,%d" % (event.x, event.y)) i = int(i.split('.')[0]) o.set_highlight_line(i) o.tkRedraw() return "break"def select_prev(event): if o.highlight_line is None: i = o.last_line else: i = max(1, o.highlight_line - 1) o.set_highlight_line(i) o.tkRedraw()def select_next(event): if o.highlight_line is None: i = 1 else: i = min(o.last_line, o.highlight_line + 1) o.set_highlight_line(i) o.tkRedraw()def scroll_up(event): t.yview_scroll(-2, "units")def scroll_down(event): t.yview_scroll(2, "units")select_program = program = highlight = Nonedef make_cone(): global cone_program cone_program = glGenLists(1) q = gluNewQuadric() glNewList(cone_program, GL_COMPILE) glEnable(GL_LIGHTING) glColor3f(*o.colors['cone']) gluCylinder(q, 0, .1, .25, 32, 1) glPushMatrix() glTranslatef(0,0,.25) gluDisk(q, 0, .1, 32, 1) glPopMatrix() glDisable(GL_LIGHTING) glEndList() gluDeleteQuadric(q)current_tool = Nonelathe_shapes = [ None, # 0 (1,-1), (1,1), (-1,1), (-1,-1), # 1..4 (0,-1), (1,0), (0,1), (-1,0), # 5..8 (0,0) # 9]def lathetool(): glDepthFunc(GL_ALWAYS) diameter, frontangle, backangle, orientation = current_tool[-4:] w = 3/8. radius = to_internal_linear_unit(diameter) / 2. glColor3f(*o.colors['lathetool']) glBegin(GL_LINES) glVertex3f(-radius/2.0,0.0,0.0) glVertex3f(radius/2.0,0.0,0.0) glVertex3f(0.0,0.0,-radius/2.0) glVertex3f(0.0,0.0,radius/2.0) glEnd() glNormal3f(0,1,0) if orientation == 9: glBegin(GL_TRIANGLE_FAN) for i in range(37): t = i * math.pi / 18 glVertex3f(radius * math.cos(t), 0.0, radius * math.sin(t)) glEnd() else: dx, dy = lathe_shapes[orientation] min_angle = min(backangle, frontangle) * math.pi / 180 max_angle = max(backangle, frontangle) * math.pi / 180 sinmax = sin(max_angle) cosmax = cos(max_angle) tanmax = cos(max_angle) sinmin = sin(min_angle) cosmin = cos(min_angle) tanmin = cos(min_angle) circleminangle = - pi/2 + min_angle circlemaxangle = - 3*pi/2 + max_angle d0 = 0 x1 = (w - d0) sz = max(w, 3*radius) glBegin(GL_TRIANGLE_FAN) glVertex3f( radius * dx + radius * math.sin(circleminangle) + sz * sinmin, 0, radius * dy + radius * math.cos(circleminangle) + sz * cosmin) for i in range(37): #t = circleminangle + i * (circlemaxangle - circleminangle)/36. t = circleminangle + i * (circlemaxangle - circleminangle)/36. glVertex3f(radius*dx + radius * math.sin(t), 0.0, radius*dy + radius * math.cos(t)) glVertex3f( radius * dx + radius * math.sin(circlemaxangle) + sz * sinmax, 0, radius * dy + radius * math.cos(circlemaxangle) + sz * cosmax) glEnd() glDepthFunc(GL_LESS)def make_selection_list(g): global select_program if select_program is None: select_program = glGenLists(1) glNewList(select_program, GL_COMPILE) g.draw(1) glEndList()def make_main_list(g): global program if program is None: program = glGenLists(1) glNewList(program, GL_COMPILE) g.draw(0) glEndList()import arraydef vupdate(var, val): try: if var.get() == val: return except ValueError: pass var.set(val)def colinear(p1, p2, p3): x, y, z = 0, 1, 2 p = p2[x] - p1[x], p2[y] - p1[y], p2[z] - p1[z] q = p3[x] - p2[x], p3[y] - p2[y], p3[z] - p2[z] dp = sqrt(p[x]**2 + p[y]**2 + p[z]**2) dq = sqrt(q[x]**2 + q[y]**2 + q[z]**2) if dp == 0 or dq == 0: return True dot = (p[x] * q[x] + p[y] * q[y] + p[z] * q[z]) / dp / dq return abs(1-dot) < 1e-8class LivePlotter: def __init__(self, window): self.win = window window.live_plot_size = 0 self.after = None self.error_after = None self.running = BooleanVar(window) self.running.set(False) self.lastpts = -1 self.last_speed = -1 self.last_limit = None self.last_motion_mode = None self.last_joint_position = None def start(self): if self.running.get(): return if not os.path.exists(emc.nmlfile): return False try: self.stat = emc.stat() except emc.error: return False def C(s): a = o.colors[s + "_alpha"] s = o.colors[s] return [int(x * 255) for x in s + (a,)] self.logger = emc.positionlogger(emc.stat(), C('backplotjog'), C('backplottraverse'), C('backplotfeed'), C('backplotarc'), C('backplottoolchange'), C('backplotprobing'), ) o.after_idle(lambda: thread.start_new_thread(self.logger.start, (.01,))) self.running.set(True) def stop(self): if not self.running.get(): return if hasattr(self, 'stat'): del self.stat if self.after is not None: self.win.after_cancel(self.after) self.after = None if self.error_after is not None: self.win.after_cancel(self.error_after) self.error_after = None self.logger.stop() self.running.set(True) def error_task(self): error = e.poll() if error: kind, text = error if kind in (emc.NML_ERROR, emc.OPERATOR_ERROR): root_window.tk.call("nf_dialog", ".error", _("AXIS error"), text, "error",0,_("OK")) else: # TEXT, DISPLAY result = root_window.tk.call("nf_dialog", ".error", _("AXIS Message"), text, "info", 0, _("OK")) self.error_after = self.win.after(20, self.error_task) def update(self): if not self.running.get(): return try: self.stat.poll() except emc.error, detail: print "error", detail del self.stat return self.after = self.win.after(20, self.update) self.win.set_current_line(self.stat.id) speed = self.stat.current_vel if (self.logger.npts != self.lastpts or self.stat.actual_position != o.last_position or self.stat.joint_actual_position != o.last_joint_position or self.stat.homed != o.last_homed or self.stat.origin != o.last_origin or self.stat.limit != o.last_limit or self.stat.tool_in_spindle != o.last_tool or self.stat.motion_mode != o.last_motion_mode or abs(speed - self.last_speed) > .01): o.redraw_soon() o.last_limit = self.stat.limit o.last_homed = self.stat.homed o.last_position = self.stat.actual_position o.last_origin = self.stat.origin o.last_motion_mode = self.stat.motion_mode o.last_tool = self.stat.tool_in_spindle o.last_joint_position = self.stat.joint_actual_position self.last_speed = speed self.lastpts = self.logger.npts root_window.update_idletasks() vupdate(vars.exec_state, self.stat.exec_state) vupdate(vars.interp_state, self.stat.interp_state) vupdate(vars.task_mode, self.stat.task_mode) vupdate(vars.task_state, self.stat.task_state) vupdate(vars.taskfile, self.stat.file) vupdate(vars.interp_pause, self.stat.paused) vupdate(vars.mist, self.stat.mist) vupdate(vars.flood, self.stat.flood) vupdate(vars.brake, self.stat.spindle_brake) vupdate(vars.spindledir, self.stat.spindle_direction) vupdate(vars.motion_mode, self.stat.motion_mode) vupdate(vars.optional_stop, self.stat.optional_stop) vupdate(vars.block_delete, self.stat.block_delete) if time.time() > spindlerate_blackout: vupdate(vars.spindlerate, int(100 * self.stat.spindlerate + .5)) if time.time() > feedrate_blackout: vupdate(vars.feedrate, int(100 * self.stat.feedrate + .5)) vupdate(vars.override_limits, self.stat.axis[0]['override_limits']) global current_tool current_tool = None for i in self.stat.tool_table: if i[0] == self.stat.tool_in_spindle: current_tool = i if self.stat.tool_in_spindle == 0: vupdate(vars.tool, _("No tool")) elif current_tool is None: vupdate(vars.tool, _("Unknown tool %d") % self.stat.tool_in_spindle) elif len(current_tool) == 7: if current_tool.xoffset == 0 and not lathe: vupdate(vars.tool, _("Tool %d, offset %g, diameter %g") % ( current_tool[0], current_tool[1], current_tool[3])) else: vupdate(vars.tool, _("Tool %d, zo %g, xo %g, dia %g") % current_tool[:4]) else: vupdate(vars.tool, _("Tool %d, offset %g, diameter %g") % current_tool[:3]) active_codes = [] for i in self.stat.gcodes[1:]: if i == -1: continue if i % 10 == 0: active_codes.append("G%d" % (i/10)) else: active_codes.append("G%d.%d" % (i/10, i%10)) for i in self.stat.mcodes[1:]: if i == -1: continue active_codes.append("M%d" % i) feed_str = "F%.1f" % self.stat.settings[1] if feed_str.endswith(".0"): feed_str = feed_str[:-2] active_codes.append(feed_str) active_codes.append("S%.0f" % self.stat.settings[2]) mid = len(active_codes)/2 a, b = active_codes[:mid], active_codes[mid:] codes = " ".join(a) + "\n" + " ".join(b) widgets.code_text.configure(state="normal") widgets.code_text.delete("0.0", "end") widgets.code_text.insert("end", codes) widgets.code_text.configure(state="disabled") def clear(self): self.logger.clear() o.redraw_soon()def running(do_poll=True):
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -