Sha256: bebf77c89012839dc61600d68aafe4e845181318400359542c95923cff61e361

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

module Floorplanner

  class Document

    POINTS_QUERY = "/project/floors/floor/designs/design/area/line/points"
    LINE_POINTS_REGEXP = /^((\s*[-+]?[0-9]*\.?[0-9]+\s+){5,8}\s*[-+]?[0-9]*\.?[0-9]+\s*?(?:,)?)*$/

    def initialize(fml_fn)
      @xml = XML::Document.file(fml_fn)
    end

    def self.validate(doc)
      schema = XML::Schema.document(
        XML::Document.file(File.join(File.dirname(__FILE__), "..", "..", "xml", "fml-permissive.xsd"))
      )
      doc = XML::Document.file(doc) if doc.instance_of?(String)
      doc.validate_schema(schema) do |message,error|
        # TODO throw an exception
        puts message if error
      end
    end

  private

    def self.validate_line_points(doc)
      doc.find(POINTS_QUERY).each do |points_node|
        unless LINE_POINTS_REGEXP =~ points_node.children.to_s
          # TODO throw an exception
          puts "Elements points inside area's line failed to validate content."
        end
      end
    end
  end

  class DesignDocument

    def initialize(fml_fn)
      @xml = LibXML::XML::Document.file(fml_fn)
    end

    def update_heights(new_height)
      lines = @xml.find("/design/lines/line[type='default_wall' or type='normal_wall' or contains(type,'hedge') or contains(type,'fence')]")
      lines.each do |line|
        begin
          points = line.find("points").first
          next unless points.content.include? ","

          coords = points.content.strip.split(",")
          top_coords = coords[1].strip.split(/\s/).map{|c| c.to_f}

          top_coords[2] = new_height
          top_coords[5] = new_height
          if top_coords.length > 6
            top_coords[8] = new_height
          end

          coords[1] = top_coords.join(" ")
          points.content = coords.join(",")
        rescue; end
      end
    end

    def save(fn)
      @xml.save fn
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fml-0.2.1 lib/floorplanner/document.rb