Class: DxfIO::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/dxf_io/reader.rb

Overview

based on DXF AutoCAD 2008 documentation (images.autodesk.com/adsk/files/acad_dxf0.pdf)

Constant Summary collapse

SECTIONS_LIST =
DxfIO::Constants::SECTIONS_LIST
HEADER_NAME =
DxfIO::Constants::HEADER_NAME

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Reader

Returns a new instance of Reader.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/dxf_io/reader.rb', line 8

def initialize(options)
  if options.is_a? String
    @filename = options
  elsif options.is_a? Hash
    if options[:path].present?
      @filename = options[:path]
    else
      raise ArgumentError, 'options must contain a :path key'
    end
    @encoding = options[:encoding] || 'Windows-1251'
  end
end

Class Method Details

.open(options) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/dxf_io/reader.rb', line 22

def open(options)
  self.new(options).tap do |reader_instance|
    if block_given?
      yield reader_instance
    end
  end
end

Instance Method Details

#parse(filename = @filename, encoding = @encoding) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/dxf_io/reader.rb', line 50

def parse(filename = @filename, encoding = @encoding)
  read_flag = "r:#{encoding}:UTF-8"
  fp = File.open(filename, read_flag)
  dxf = {HEADER_NAME => {}}
  SECTIONS_LIST.each do |section_name|
    dxf[section_name] = []
  end
  #
  # main loop
  #
  begin
    while true
      c, v = read_codes(fp)
      break if v == 'EOF'
      if v == 'SECTION'
        c, v = read_codes(fp)
        if v == HEADER_NAME
          hdr = dxf[HEADER_NAME]
          while true
            c, v = read_codes(fp)
            break if v == 'ENDSEC' # or v == "BLOCKS" or v == "ENTITIES" or v == "EOF"
            if c == 9
              key = v
              hdr[key] = {}
            else
              add_att(hdr[key], c, v)
            end
          end # while
        elsif SECTIONS_LIST.include?(v)
          section = dxf[v]
          parse_entities(section, fp)
        end
      end # if in SECTION
    end # main loop
  ensure
    fp.close unless fp.nil?
  end

  dxf
end

#rerunObject



46
47
48
# File 'lib/dxf_io/reader.rb', line 46

def rerun
  @result_hash = parse
end

#runObject Also known as: to_hash, to_h



39
40
41
# File 'lib/dxf_io/reader.rb', line 39

def run
  @result_hash ||= parse
end