Class: Dxf::DesignToolPlanParser

Inherits:
Object
  • Object
show all
Defined in:
lib/dxf/design_tool_plan_parser.rb

Overview

Parses an XML floor-plan blob produced by the in-browser Floor-Heating
Design Tool into a Ruby object tree (rooms, fixtures, blocks) that the
server can convert into a RoomConfiguration and matching DXF/SVG
exports. Accepts either a :file_path or a raw :xml_data string.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ DesignToolPlanParser

Returns a new instance of DesignToolPlanParser.

Parameters:

  • options (Hash) (defaults to: {})

    parser input

Options Hash (options):

  • file_path (String)

    path to the plan XML file

  • xml_data (String)

    raw plan XML string

Raises:

  • (RuntimeError)

    when neither :file_path nor :xml_data is given, or the file is missing



13
14
15
16
17
# File 'lib/dxf/design_tool_plan_parser.rb', line 13

def initialize(options = {})
  @options = options
  raise "Need a :file_path or a :xml_data argument" if @options[:file_path].nil? && @options[:xml_data].nil?
  raise "File #{@options[:file_path]} does not exist" if @options[:file_path].present? && !File.exist?(@options[:file_path])
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/dxf/design_tool_plan_parser.rb', line 7

def options
  @options
end

#xml_dataObject (readonly)

Returns the value of attribute xml_data.



7
8
9
# File 'lib/dxf/design_tool_plan_parser.rb', line 7

def xml_data
  @xml_data
end

#xml_docObject (readonly)

Returns the value of attribute xml_doc.



7
8
9
# File 'lib/dxf/design_tool_plan_parser.rb', line 7

def xml_doc
  @xml_doc
end

Instance Method Details

#controlString

Returns the control placement.

Returns:

  • (String)

    the control placement



63
64
65
# File 'lib/dxf/design_tool_plan_parser.rb', line 63

def control
  xml_doc.at_xpath("//ControlPlacement").content
end

#dxf_rawString

Returns the generated DXF content.

Returns:

  • (String)

    the generated DXF content



33
34
35
36
37
# File 'lib/dxf/design_tool_plan_parser.rb', line 33

def dxf_raw
  initialize_xml
  fp = Dxf::FloorPlan.new(to_hash, room_type, flooring_type)
  fp.dxf
end

#flooring_typeString

Returns the flooring type.

Returns:

  • (String)

    the flooring type



51
52
53
# File 'lib/dxf/design_tool_plan_parser.rb', line 51

def flooring_type
  xml_doc.at_xpath("//FlooringType").content
end

#initialize_xmlNokogiri::XML::Document

Returns the parsed XML document.

Returns:

  • (Nokogiri::XML::Document)

    the parsed XML document



20
21
22
23
# File 'lib/dxf/design_tool_plan_parser.rb', line 20

def initialize_xml
  @xml_data = options[:xml_data] || File.read(options[:file_path])
  @xml_doc = Nokogiri::XML(@xml_data)
end

#line_coordonatesNokogiri::XML::NodeSet

Returns the RoomShape nodes.

Returns:

  • (Nokogiri::XML::NodeSet)

    the RoomShape nodes



56
57
58
59
60
# File 'lib/dxf/design_tool_plan_parser.rb', line 56

def line_coordonates
  # xml_doc.at_xpath("//Project//Room//DrawingInfo//UserFloorPlan//RoomShape").content
  xml_doc.xpath("//Project//Room//DrawingInfo//UserFloorPlan//RoomShape")
  #  lines = @xml_doc.xpath("//Project//Room//DrawingInfo//UserFloorPlan//RoomShape//Line")
end

#project_infoNokogiri::XML::NodeSet

Returns the Project nodes.

Returns:

  • (Nokogiri::XML::NodeSet)

    the Project nodes



68
69
70
# File 'lib/dxf/design_tool_plan_parser.rb', line 68

def project_info
  xml_doc.xpath("//Project", 'projectid' => 'projectid')
end

#room_typeString

Returns the room type.

Returns:

  • (String)

    the room type



46
47
48
# File 'lib/dxf/design_tool_plan_parser.rb', line 46

def room_type
  xml_doc.at_xpath("//RoomType").content
end

#save_to_file(file_handle) ⇒ Integer

Returns number of bytes written to the file handle.

Returns:

  • (Integer)

    number of bytes written to the file handle



26
27
28
29
30
# File 'lib/dxf/design_tool_plan_parser.rb', line 26

def save_to_file(file_handle)
  initialize_xml
  fp = Dxf::FloorPlan.new(to_hash, room_type, flooring_type)
  file_handle.write(fp.dxf)
end

#to_hashHash

Returns the parsed XML as a hash.

Returns:

  • (Hash)

    the parsed XML as a hash



40
41
42
43
# File 'lib/dxf/design_tool_plan_parser.rb', line 40

def to_hash
  parser = Nori.new(parser: :nokogiri)
  parser.parse @xml_data
end