示例#1
0
文件: webd.py 项目: DevMK2/tools
def load_preview(f, canon, unitcode, initcode, interpname=""):
    try:
        result, seq = gcode.parse(f, canon, unitcode, initcode, interpname)
    except KeyboardInterrupt:
        result, seq = 0, 0

    if result <= gcode.MIN_ERROR:
        canon.progress.nextphase(1)
        canon.calc_extents()

    return result, seq
示例#2
0
    def _file_event(self, file_path):
        """" This function gets notified about files begin loaded """
        self.loaded_file = file_path

        self.canon = PropertiesCanon()

        if os.path.exists(self.parameter_file):
            shutil.copy(self.parameter_file, self.temp_parameter_file)

        self.canon.parameter_file = self.temp_parameter_file

        # Some initialization g-code to set the units and optional user code
        unitcode = "G%d" % (20 + (self.stat.linear_units == 1))
        initcode = self.ini.find("RS274NGC", "RS274NGC_STARTUP_CODE") or ""

        # THIS IS WHERE IT ALL HAPPENS: load_preview will execute the code,
        # call back to the canon with motion commands, and record a history
        # of all the movements.
        try:
            result, seq = gcode.parse(self.loaded_file, self.canon, unitcode,
                                      initcode)

            if result > gcode.MIN_ERROR:
                msg = gcode.strerror(result)
                LOG.debug(f"Error in {self.loaded_file} line {seq - 1}\n{msg}")
        except Exception as e:
            LOG.debug(f"Error {e}")

        # clean up temp var file and the backup
        os.unlink(self.temp_parameter_file)
        os.unlink(self.temp_parameter_file + '.bak')

        file_name = self.loaded_file
        file_size = os.stat(self.loaded_file).st_size
        file_lines = self.canon.num_lines

        tool_calls = self.canon.tool_calls

        g0 = sum(self.dist(l[0][:3], l[1][:3]) for l in self.canon.traverse)
        g1 = (sum(self.dist(l[0][:3], l[1][:3]) for l in self.canon.feed) +
              sum(self.dist(l[0][:3], l[1][:3]) for l in self.canon.arcfeed))

        self.file_name.setValue(file_name)
        self.file_size.setValue(file_size)
        self.file_lines.setValue(file_lines)

        self.tool_calls_num.setValue(tool_calls)

        self.file_rapid_distance.setValue(g0)
        self.file_feed_distance.setValue(g1)

        self.file_work_planes.setValue(self.canon.work_planes)
        self.file_rigid_taps.setValue(self.canon.rigid_taps)
        self.file_offsets.setValue(self.canon.g5x_offset_dict)
示例#3
0
    def load(self, filename=None, *args, **kwargs):
        # args and kwargs are passed to the canon init method

        filename = filename or self.last_filename
        if filename is None:
            self.stat.poll()
            filename = self.stat.file

        if filename is None or not os.path.isfile(filename):
            self.canon = None
            self.notification.setNotify(
                "3D plot",
                "Can't load backplot, invalid file: {}".format(filename))
            # raise ValueError("Can't load backplot, invalid file: {}".format(filename))

        self.last_filename = filename

        # create the object which handles the canonical motion callbacks
        # (straight_feed, straight_traverse, arc_feed, rigid_tap, etc.)
        self.canon = self.canon_class(*args, **kwargs)

        if os.path.exists(self.parameter_file):
            shutil.copy(self.parameter_file, self.temp_parameter_file)

        self.canon.parameter_file = self.temp_parameter_file

        # Some initialization g-code to set the units and optional user code
        unitcode = "G%d" % (20 + (self.stat.linear_units == 1))
        initcode = self.ini.find("RS274NGC", "RS274NGC_STARTUP_CODE") or ""

        # THIS IS WHERE IT ALL HAPPENS: load_preview will execute the code,
        # call back to the canon with motion commands, and record a history
        # of all the movements.

        try:
            result, seq = gcode.parse(filename, self.canon, unitcode, initcode)

            if result > gcode.MIN_ERROR:
                msg = gcode.strerror(result)
                fname = os.path.basename(filename)
                self.notification.setNotify(
                    "3D plot",
                    "Error in {} line {}\n{}".format(fname, seq - 1, msg))
                # raise SyntaxError("Error in %s line %i: %s" % (fname, seq - 1, msg))

        except KeyboardInterrupt:
            # probably raised by an (AXIS, stop) comment in the G-code file
            # abort generating the backplot
            pass

        # clean up temp var file and the backup
        os.unlink(self.temp_parameter_file)
        os.unlink(self.temp_parameter_file + '.bak')
    def load_preview(self, f, canon, unitcode, initcode):
        self.set_canon(canon)
        result, seq = gcode.parse(f, canon, unitcode, initcode)

        if result <= gcode.MIN_ERROR:
            self.canon.progress.nextphase(1)
            canon.calc_extents()
            self.stale_dlist('program_rapids')
            self.stale_dlist('program_norapids')
            self.stale_dlist('select_rapids')
            self.stale_dlist('select_norapids')

        return result, seq
示例#5
0
def run(filename,canon, unitcode, initcode):
    result, last_sequence_number = gcode.parse(filename, canon, unitcode, initcode)

    # XXX mystery attributes
    print "gcodes", gcode.linecode.gcodes
    print "sequence_number", gcode.linecode.sequence_number

    if result > gcode.MIN_ERROR:
        print " gcode error, line %d: %s " % (canon.lineno, gcode.strerror(result))
        print " last_sequence_number ",last_sequence_number
    else:
        # XXX: unclear how this is supposed to work
        minxt,maxxt,min_t_xt,max_t_xt = gcode.calc_extents()
        print "X extent: %.2f .. %.2f" % (minxt[0],maxxt[0])
        print "Y extent: %.2f .. %.2f" % (minxt[1],maxxt[1])
        print "Z extent: %.2f .. %.2f" % (minxt[0],maxxt[2])
        print "X extent w/tool: %.2f .. %.2f" % (min_t_xt[0],max_t_xt[0])
        print "Y extent w/tool: %.2f .. %.2f" % (min_t_xt[1],max_t_xt[1])
        print "Z extent w/tool: %.2f .. %.2f" % (min_t_xt[0],max_t_xt[2])
示例#6
0
def run(filename, canon, unitcode, initcode):
    result, last_sequence_number = gcode.parse(filename, canon, unitcode,
                                               initcode)

    # XXX mystery attributes
    print "gcodes", gcode.linecode.gcodes
    print "sequence_number", gcode.linecode.sequence_number

    if result > gcode.MIN_ERROR:
        print " gcode error, line %d: %s " % (canon.lineno,
                                              gcode.strerror(result))
        print " last_sequence_number ", last_sequence_number
    else:
        # XXX: unclear how this is supposed to work
        minxt, maxxt, min_t_xt, max_t_xt = gcode.calc_extents()
        print "X extent: %.2f .. %.2f" % (minxt[0], maxxt[0])
        print "Y extent: %.2f .. %.2f" % (minxt[1], maxxt[1])
        print "Z extent: %.2f .. %.2f" % (minxt[0], maxxt[2])
        print "X extent w/tool: %.2f .. %.2f" % (min_t_xt[0], max_t_xt[0])
        print "Y extent w/tool: %.2f .. %.2f" % (min_t_xt[1], max_t_xt[1])
        print "Z extent w/tool: %.2f .. %.2f" % (min_t_xt[0], max_t_xt[2])
示例#7
0
        return inner

    # this is just noisy
    def next_line(self, linecode):
        pass

    # These can't just return None...
    def get_external_length_units(self):
        return 1.0

    def get_external_angular_units(self):
        return 1.0

    def get_axis_mask(self):
        return 7  # (x y z)

    def get_block_delete(self):
        return False

    def get_tool(self, pocket):
        return -1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0


parameter = tempfile.NamedTemporaryFile()
canon = Canon()
canon.parameter_file = parameter.name
result, seq = gcode.parse(sys.argv[1], canon, '', '', '')
if result > gcode.MIN_ERROR:
    raise SystemExit, gcode.strerror(result)
示例#8
0
def float_fmt(f):
    if isinstance(f, float): return "% 5.1g" % f
    return "%5s" % f

class Canon:
    def __getattr__(self, attr):
        """Assume that any unknown attribute is a canon call; just print
        its args and return None"""

        def inner(*args):
            args = map(float_fmt, args)
            print "%-17s %s" % (attr, " ".join(args))
        return inner

    # this is just noisy
    def next_line(self, linecode): pass

    # These can't just return None...
    def get_external_length_units(self): return 1.0
    def get_external_angular_units(self): return 1.0
    def get_axis_mask(self): return 7 # (x y z)
    def get_block_delete(self): return False
    def get_tool(self, pocket):
        return -1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0

parameter = tempfile.NamedTemporaryFile()
canon = Canon()
canon.parameter_file = parameter.name
result, seq = gcode.parse(sys.argv[1], canon, '', '', '')
if result > gcode.MIN_ERROR: raise SystemExit, gcode.strerror(result)