Class: EagleCAD::Package

Inherits:
Object
  • Object
show all
Defined in:
lib/eaglecad/package.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Package

Returns a new instance of Package.

Parameters:



41
42
43
44
45
46
47
# File 'lib/eaglecad/package.rb', line 41

def initialize(name)
    @holes = []
    @layers = {}
    @layers.default_proc = proc {|hash, key| hash[key] = []}
    @name = name
    @pads = []
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



10
11
12
# File 'lib/eaglecad/package.rb', line 10

def description
  @description
end

#holesObject (readonly)

Returns the value of attribute holes.



11
12
13
# File 'lib/eaglecad/package.rb', line 11

def holes
  @holes
end

#layersObject (readonly)

Returns the value of attribute layers.



11
12
13
# File 'lib/eaglecad/package.rb', line 11

def layers
  @layers
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/eaglecad/package.rb', line 10

def name
  @name
end

#padsObject (readonly)

Returns the value of attribute pads.



11
12
13
# File 'lib/eaglecad/package.rb', line 11

def pads
  @pads
end

Class Method Details

.from_xml(element) ⇒ Object

Create a new EagleCAD::Package from an REXML::Element

Parameters:

  • element (REXML::Element)

    The REXML::Element to parse



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/eaglecad/package.rb', line 15

def self.from_xml(element)
    package = Package.new element.attributes['name']

    element.elements.each do |element|
	layer_number = element.attributes['layer'].to_i if element.attributes.has_key?('layer')
	case element.name
	    when 'description'
		package.description = element.text
	    when 'hole'
		package.holes.push Geometry::Hole.from_xml(element)
	    when 'pad'
		package.pads.push Geometry::Pad.from_xml(element)
	    else
		g = Geometry.from_xml(element)
		if g
		    package.push layer_number, g
		else
		    raise StandardError, "Unrecognized package element '#{element.name}'"
		end
	end
    end

    package
end

Instance Method Details

#push(layer_number, element) ⇒ Object

Push a new element to the given layer number

Parameters:

  • layer_number (Numeric)

    The layer to add the element to

  • element (Object)

    The thing to push



52
53
54
55
# File 'lib/eaglecad/package.rb', line 52

def push(layer_number, element)
    layer = @layers[layer_number]
    layer.push element
end

#to_xmlREXML::Element

Generate XML for the EagleCAD::Package element

Returns:

  • (REXML::Element)


59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/eaglecad/package.rb', line 59

def to_xml
    REXML::Element.new('package').tap do |element|
	element.add_attribute('name', name)
	element.add_element('description').text = description

	holes.each {|hole| element.add_element hole.to_xml }
	pads.each {|pad| element.add_element pad.to_xml }

	layers.each do |number, layer|
	    layer.each {|obj| element.add_element(obj.to_xml, {'layer' => number}) }
	end
    end
end