Class: EagleCAD::Symbol

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Symbol

Returns a new instance of Symbol.

Parameters:

  • name (String)

    The name of the Package



43
44
45
46
47
48
# File 'lib/eaglecad/symbol.rb', line 43

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

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



7
8
9
# File 'lib/eaglecad/symbol.rb', line 7

def description
  @description
end

#layersObject (readonly)

Returns the value of attribute layers.



8
9
10
# File 'lib/eaglecad/symbol.rb', line 8

def layers
  @layers
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/eaglecad/symbol.rb', line 7

def name
  @name
end

#pinsObject (readonly)

Returns the value of attribute pins.



8
9
10
# File 'lib/eaglecad/symbol.rb', line 8

def pins
  @pins
end

Class Method Details

.from_xml(element) ⇒ Object

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

Parameters:

  • element (Element)

    The REXML::Element to parse



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

def self.from_xml(element)
    symbol = Symbol.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 'circle'
		symbol.push layer_number, Geometry::Circle.from_xml(element)
	    when 'description'
		symbol.description = element.text
	    when 'pin'
		symbol.pins.push Geometry::Pin.from_xml(element)
	    when 'polygon'
		symbol.push layer_number, Geometry::Polygon.from_xml(element)
	    when 'rectangle'
		symbol.push layer_number, Geometry::Rectangle.from_xml(element)
	    when 'smd'
		symbol.push layer_number, Geometry::SMD.from_xml(element)
	    when 'text'
		symbol.push layer_number, Geometry::Text.from_xml(element)
	    when 'wire'
		symbol.push layer_number, Geometry::Line.from_xml(element)
	    else
		raise StandardError, "Unrecognized symbol element '#{element.name}'"
	end
    end

    symbol
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



53
54
55
56
# File 'lib/eaglecad/symbol.rb', line 53

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

#to_xmlREXML::Element

Generate XML for the EagleCAD::Symbol element

Returns:

  • (REXML::Element)


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

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

	pins.each {|pin| element.add_element pin.to_xml }

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