📄 gcodemodule.cc
字号:
// This is a component of AXIS, a front-end for emc// Copyright 2004, 2005, 2006 Jeff Epler <jepler@unpythonic.net> and // Chris Radek <chris@timeguy.com>//// 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA#include <Python.h>#include <structmember.h>#include "rs274ngc.hh"#include "interp_return.hh"#include "canon.hh"#include "config.h" // LINELEN#define active_settings interp_new.active_settings#define active_g_codes interp_new.active_g_codes#define active_m_codes interp_new.active_m_codes#define interp_init interp_new.init#define interp_open interp_new.open#define interp_close interp_new.close#define interp_read interp_new.read#define interp_execute interp_new.executechar _parameter_file_name[LINELEN];/* This definition of offsetof avoids the g++ warning * 'invalid offsetof from non-POD type'. */#undef offsetof#define offsetof(T,x) (size_t)(-1+(char*)&(((T*)1)->x))extern char *_rs274ngc_errors[];#define iserror(x) ((x) < 0 || (x) >= RS274NGC_MIN_ERROR)static PyObject *int_array(int *arr, int sz) { PyObject *res = PyTuple_New(sz); for(int i = 0; i < sz; i++) { PyTuple_SET_ITEM(res, i, PyInt_FromLong(arr[i])); } return res;}extern PyTypeObject LineCodeType, DelayType, VelocityType;extern PyTypeObject LinearMoveType, CircularMoveType, UnknownMessageType;typedef struct { PyObject_HEAD double settings[ACTIVE_SETTINGS]; int gcodes[ACTIVE_G_CODES]; int mcodes[ACTIVE_M_CODES];} LineCode;PyObject *LineCode_gcodes(LineCode *l) { return int_array(l->gcodes, ACTIVE_G_CODES);}PyObject *LineCode_mcodes(LineCode *l) { return int_array(l->mcodes, ACTIVE_M_CODES);}PyGetSetDef LineCodeGetSet[] = { {"gcodes", (getter)LineCode_gcodes}, {"mcodes", (getter)LineCode_mcodes}, {NULL, NULL},};PyMemberDef LineCodeMembers[] = { {"sequence_number", T_INT, offsetof(LineCode, gcodes[0]), READONLY}, {"feed_rate", T_DOUBLE, offsetof(LineCode, settings[1]), READONLY}, {"speed", T_DOUBLE, offsetof(LineCode, settings[2]), READONLY}, {"motion_mode", T_INT, offsetof(LineCode, gcodes[1]), READONLY}, {"block", T_INT, offsetof(LineCode, gcodes[2]), READONLY}, {"plane", T_INT, offsetof(LineCode, gcodes[3]), READONLY}, {"cutter_side", T_INT, offsetof(LineCode, gcodes[4]), READONLY}, {"units", T_INT, offsetof(LineCode, gcodes[5]), READONLY}, {"distance_mode", T_INT, offsetof(LineCode, gcodes[6]), READONLY}, {"feed_mode", T_INT, offsetof(LineCode, gcodes[7]), READONLY}, {"origin", T_INT, offsetof(LineCode, gcodes[8]), READONLY}, {"tool_length_offset", T_INT, offsetof(LineCode, gcodes[9]), READONLY}, {"retract_mode", T_INT, offsetof(LineCode, gcodes[10]), READONLY}, {"path_mode", T_INT, offsetof(LineCode, gcodes[11]), READONLY}, {"stopping", T_INT, offsetof(LineCode, mcodes[1]), READONLY}, {"spindle", T_INT, offsetof(LineCode, mcodes[2]), READONLY}, {"toolchange", T_INT, offsetof(LineCode, mcodes[3]), READONLY}, {"mist", T_INT, offsetof(LineCode, mcodes[4]), READONLY}, {"flood", T_INT, offsetof(LineCode, mcodes[5]), READONLY}, {"overrides", T_INT, offsetof(LineCode, mcodes[6]), READONLY}, {NULL}};PyTypeObject LineCodeType = { PyObject_HEAD_INIT(NULL) 0, /*ob_size*/ "gcode.linecode", /*tp_name*/ sizeof(LineCode), /*tp_basicsize*/ 0, /*tp_itemsize*/ /* methods */ 0, /*tp_dealloc*/ 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ 0, /*tp_compare*/ 0, /*tp_repr*/ 0, /*tp_as_number*/ 0, /*tp_as_sequence*/ 0, /*tp_as_mapping*/ 0, /*tp_hash*/ 0, /*tp_call*/ 0, /*tp_str*/ 0, /*tp_getattro*/ 0, /*tp_setattro*/ 0, /*tp_as_buffer*/ Py_TPFLAGS_DEFAULT, /*tp_flags*/ 0, /*tp_doc*/ 0, /*tp_traverse*/ 0, /*tp_clear*/ 0, /*tp_richcompare*/ 0, /*tp_weaklistoffset*/ 0, /*tp_iter*/ 0, /*tp_iternext*/ 0, /*tp_methods*/ LineCodeMembers, /*tp_members*/ LineCodeGetSet, /*tp_getset*/ 0, /*tp_base*/ 0, /*tp_dict*/ 0, /*tp_descr_get*/ 0, /*tp_descr_set*/ 0, /*tp_dictoffset*/ 0, /*tp_init*/ 0, /*tp_alloc*/ PyType_GenericNew, /*tp_new*/ 0, /*tp_free*/ 0, /*tp_is_gc*/};PyObject *callback;int interp_error;int last_sequence_number;int plane;bool metric;double _pos_x, _pos_y, _pos_z, _pos_a, _pos_b, _pos_c, _pos_u, _pos_v, _pos_w;double tool_xoffset, tool_zoffset;Interp interp_new;void maybe_new_line() { if(interp_error) return; LineCode *new_line_code = (LineCode*)(PyObject_New(LineCode, &LineCodeType)); active_settings(new_line_code->settings); active_g_codes(new_line_code->gcodes); active_m_codes(new_line_code->mcodes); int sequence_number = interp_new.sequence_number(); new_line_code->gcodes[0] = sequence_number; if(sequence_number == last_sequence_number) { Py_DECREF(new_line_code); return; } last_sequence_number = sequence_number; PyObject *result = PyObject_CallMethod(callback, "next_line", "O", new_line_code); Py_DECREF(new_line_code); if(result == NULL) interp_error ++; Py_XDECREF(result);}void ARC_FEED(double first_end, double second_end, double first_axis, double second_axis, int rotation, double axis_end_point, double a_position, double b_position, double c_position, double u_position, double v_position, double w_position) { // XXX: set _pos_* if(metric) { first_end /= 25.4; second_end /= 25.4; first_axis /= 25.4; second_axis /= 25.4; axis_end_point /= 25.4; u_position /= 25.4; v_position /= 25.4; w_position /= 25.4; } maybe_new_line(); if(interp_error) return; PyObject *result = PyObject_CallMethod(callback, "arc_feed", "ffffifffffff", first_end, second_end, first_axis, second_axis, rotation, axis_end_point, a_position, b_position, c_position, u_position, v_position, w_position); if(result == NULL) interp_error ++; Py_XDECREF(result);}void STRAIGHT_FEED(double x, double y, double z, double a, double b, double c, double u, double v, double w) { _pos_x=x; _pos_y=y; _pos_z=z; _pos_a=a; _pos_b=b; _pos_c=c; _pos_u=u; _pos_v=v; _pos_w=w; if(metric) { x /= 25.4; y /= 25.4; z /= 25.4; u /= 25.4; v /= 25.4; w /= 25.4; } maybe_new_line(); if(interp_error) return; PyObject *result = PyObject_CallMethod(callback, "straight_feed", "fffffffff", x, y, z, a, b, c, u, v, w); if(result == NULL) interp_error ++; Py_XDECREF(result);}void STRAIGHT_TRAVERSE(double x, double y, double z, double a, double b, double c, double u, double v, double w) { _pos_x=x; _pos_y=y; _pos_z=z; _pos_a=a; _pos_b=b; _pos_c=c; _pos_u=u; _pos_v=v; _pos_w=w; if(metric) { x /= 25.4; y /= 25.4; z /= 25.4; u /= 25.4; v /= 25.4; w /= 25.4; } maybe_new_line(); if(interp_error) return; PyObject *result = PyObject_CallMethod(callback, "straight_traverse", "fffffffff", x, y, z, a, b, c, u, v, w); if(result == NULL) interp_error ++; Py_XDECREF(result);}void SET_ORIGIN_OFFSETS(double x, double y, double z, double a, double b, double c, double u, double v, double w) { if(metric) { x /= 25.4; y /= 25.4; z /= 25.4; u /= 25.4; v /= 25.4; w /= 25.4; } maybe_new_line(); if(interp_error) return; PyObject *result = PyObject_CallMethod(callback, "set_origin_offsets", "fffffffff", x, y, z, a, b, c, u, v, w); if(result == NULL) interp_error ++; Py_XDECREF(result);}void USE_LENGTH_UNITS(CANON_UNITS u) { metric = u == CANON_UNITS_MM; }void SET_LENGTH_UNITS(CANON_UNITS u) { metric = u == CANON_UNITS_MM; }void SELECT_PLANE(CANON_PLANE pl) { maybe_new_line(); if(interp_error) return; PyObject *result = PyObject_CallMethod(callback, "set_plane", "i", pl); if(result == NULL) interp_error ++; Py_XDECREF(result);}void SET_TRAVERSE_RATE(double rate) { maybe_new_line(); if(interp_error) return; PyObject *result = PyObject_CallMethod(callback, "set_traverse_rate", "f", rate); if(result == NULL) interp_error ++; Py_XDECREF(result);}void SET_FEED_MODE(int mode) {#if 0 maybe_new_line(); if(interp_error) return; PyObject *result = PyObject_CallMethod(callback, "set_feed_mode", "i", mode); if(result == NULL) interp_error ++; Py_XDECREF(result);#endif}/* XXX: This needs to be re-thought. Sometimes feed rate is not in linear * units--e.g., it could be inverse time feed mode. in that case, it's wrong * to convert from mm to inch here. but the gcode time estimate gets inverse * time feed wrong anyway.. */void SET_FEED_RATE(double rate) { maybe_new_line(); if(interp_error) return; if(metric) rate /= 25.4; PyObject *result = PyObject_CallMethod(callback, "set_feed_rate", "f", rate); if(result == NULL) interp_error ++; Py_XDECREF(result);}void DWELL(double time) { maybe_new_line(); if(interp_error) return; PyObject *result = PyObject_CallMethod(callback, "dwell", "f", time); if(result == NULL) interp_error ++; Py_XDECREF(result);}void MESSAGE(char *comment) { maybe_new_line(); if(interp_error) return; PyObject *result = PyObject_CallMethod(callback, "message", "s", comment); if(result == NULL) interp_error ++; Py_XDECREF(result);}void SYSTEM(char *comment) { maybe_new_line(); if(interp_error) return; PyObject *result = PyObject_CallMethod(callback, "system", "s", comment); if(result == NULL) interp_error ++; Py_XDECREF(result);}void COMMENT(char *comment) { maybe_new_line(); if(interp_error) return; PyObject *result = PyObject_CallMethod(callback, "comment", "s", comment); if(result == NULL) interp_error ++; Py_XDECREF(result);}void USE_TOOL_LENGTH_OFFSET(double xoffset, double zoffset) { tool_zoffset = zoffset; tool_xoffset = xoffset;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -