Sha256: 3b900b3519ac737decfc5b094e7b8c955b8de3f8c13d0330a3f84f8b961685bd

Contents?: true

Size: 1.55 KB

Versions: 2

Compression:

Stored size: 1.55 KB

Contents

require 'rint_core/g_code/codes'
require 'active_support/core_ext/object/blank'

module RintCore
  module GCode
    class Line
      include RintCore::GCode::Codes

      attr_accessor :imperial, :relative, :f
      attr_reader :raw
      attr_writer :x, :y, :z, :e

      def initialize(line)
        @coordinates = ['X','Y','Z','E','F']
        @number_pattern = /[-]?\d+[.]?\d*/
        @raw = line.upcase.strip
        @raw = @raw.split(COMMENT_SYMBOL).first.strip if line.include?(COMMENT_SYMBOL)

        parse_coordinates
      end

      def to_mm(number)
        number *= 25.4 if number.present? && @imperial
        number
      end

      def x
        to_mm @x
      end

      def y
        to_mm @y
      end

      def z
        to_mm @z
      end

      def e
        to_mm @e
      end

      def command
        if @raw.present?
          @raw.split(' ').first
        else
          ''
        end
      end

      def get_float(axis)
        @raw.split(axis).last.scan(@number_pattern).first.to_f
      end

      def parse_coordinates
        @coordinates.each do |axis|
          send(axis.downcase+'=', get_float(axis)) if @raw.include?(axis)
        end
      end

      def is_move?
        @raw.start_with?(RAPID_MOVE) || @raw.start_with?(CONTROLLED_MOVE)
      end

      def travel_move?
        is_move? && !@e.present?
      end

      def extrusion_move?
        is_move? && @e.present? && @e > 0
      end

      def full_home?
        command == HOME && @x.blank? && @y.blank? && @z.blank?
      end

      def to_s
        @raw
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rintcore-0.0.3 lib/rint_core/g_code/line.rb
rintcore-0.0.2 lib/rint_core/g_code/line.rb