Class: DXF::Unparser
- Inherits:
-
Object
- Object
- DXF::Unparser
- Defined in:
- lib/dxf/unparser.rb
Instance Attribute Summary collapse
-
#container ⇒ Object
Returns the value of attribute container.
Element Formatters collapse
-
#line(first, last, layer = 0, transformation = nil) ⇒ Object
Convert a Geometry::Line into group codes.
Property Converters collapse
-
#center(point, transformation) ⇒ Object
Emit the group codes for the center property of an element.
-
#format_value(value) ⇒ String
Convert the given value to the correct units and return it as a formatted string.
-
#radius(element, transformation = nil) ⇒ Object
Emit the group codes for the radius property of an element.
- #section_end ⇒ Object
- #section_start(name) ⇒ Object
Instance Method Summary collapse
-
#initialize(units = :mm) ⇒ Unparser
constructor
Initialize with a Sketch.
- #text(position, content, layer = 0, transformation = nil) ⇒ Object
-
#to_array(element, transformation = nil) ⇒ Array
Convert an element to an Array.
- #to_s ⇒ Object
-
#unparse(output, sketch) ⇒ Object
Convert a Sketch to a DXF file and write it to the given output.
Constructor Details
#initialize(units = :mm) ⇒ Unparser
Initialize with a Sketch
12 13 14 |
# File 'lib/dxf/unparser.rb', line 12 def initialize(units=:mm) @units = units end |
Instance Attribute Details
#container ⇒ Object
Returns the value of attribute container.
8 9 10 |
# File 'lib/dxf/unparser.rb', line 8 def container @container end |
Instance Method Details
#center(point, transformation) ⇒ Object
Emit the group codes for the center property of an element
61 62 63 64 |
# File 'lib/dxf/unparser.rb', line 61 def center(point, transformation) point = transformation.transform(point) if transformation [10, format_value(point.x), 20, format_value(point.y)] end |
#format_value(value) ⇒ String
Convert the given value to the correct units and return it as a formatted string
51 52 53 54 55 56 57 |
# File 'lib/dxf/unparser.rb', line 51 def format_value(value) if value.is_a? Units::Numeric "%g" % value.send("to_#{@units}".to_sym) else "%g" % value end end |
#line(first, last, layer = 0, transformation = nil) ⇒ Object
Convert a Geometry::Line into group codes
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/dxf/unparser.rb', line 24 def line(first, last, layer=0, transformation=nil) first, last = Geometry::Point[first], Geometry::Point[last] first, last = [first, last].map {|point| transformation.transform(point) } if transformation [ 0, 'LINE', 8, layer, 10, format_value(first.x), 20, format_value(first.y), 11, format_value(last.x), 21, format_value(last.y)] end |
#radius(element, transformation = nil) ⇒ Object
Emit the group codes for the radius property of an element
67 68 69 |
# File 'lib/dxf/unparser.rb', line 67 def radius(element, transformation=nil) [40, format_value(transformation ? transformation.transform(element.radius) : element.radius)] end |
#section_end ⇒ Object
71 72 73 |
# File 'lib/dxf/unparser.rb', line 71 def section_end [0, 'ENDSEC'] end |
#section_start(name) ⇒ Object
75 76 77 |
# File 'lib/dxf/unparser.rb', line 75 def section_start(name) [0, 'SECTION', 2, name] end |
#text(position, content, layer = 0, transformation = nil) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/dxf/unparser.rb', line 37 def text(position, content, layer=0, transformation=nil) position = transformation.transform(position) if transformation [0, 'TEXT', 8, layer, 10, format_value(position.x), 20, format_value(position.y), 1, content, 7, 'NewTextStyle_4'] end |
#to_array(element, transformation = nil) ⇒ Array
Convert an element to an Array
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/dxf/unparser.rb', line 83 def to_array(element, transformation=nil) layer = 0; case element when Geometry::Arc [ 0, 'ARC', center(element.center, transformation), radius(element), 50, format_value(element.start_angle), 51, format_value(element.end_angle)] when Geometry::Circle [0, 'CIRCLE', 8, layer, center(element.center, transformation), radius(element)] when Geometry::Text text(element.position, element.content, layer) when Geometry::Edge, Geometry::Line line(element.first, element.last, layer, transformation) when Geometry::Polyline element.edges.map {|edge| line(edge.first, edge.last, layer, transformation) } when Geometry::Rectangle element.edges.map {|edge| line(edge.first, edge.last, layer, transformation) } when Geometry::Square points = element.points points.each_cons(2).map {|p1,p2| line(p1,p2, layer, transformation) } + line(points.last, points.first, layer, transformation) when Sketch transformation = transformation ? (transformation + element.transformation) : element.transformation element.geometry.map {|e| to_array(e, transformation)} end end |
#to_s ⇒ Object
16 17 18 19 20 |
# File 'lib/dxf/unparser.rb', line 16 def to_s io = StringIO.new unparse(io, container) io.string end |
#unparse(output, sketch) ⇒ Object
Convert a Sketch to a DXF file and write it to the given output
112 113 114 115 116 |
# File 'lib/dxf/unparser.rb', line 112 def unparse(output, sketch) output << (section_start('HEADER') + section_end + section_start('ENTITIES') + to_array(sketch) + section_end + [0, 'EOF']).join("\n") end |