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.
- #lwpolyline(points, closed, layer = 0, transformation = nil) ⇒ Object
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.
-
#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
57 58 59 60 |
# File 'lib/dxf/unparser.rb', line 57 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 |
# File 'lib/dxf/unparser.rb', line 51 def format_value(value) ("%g" % value.to(@units)) rescue ("%g" % value) 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 |
#lwpolyline(points, closed, layer = 0, transformation = nil) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/dxf/unparser.rb', line 36 def lwpolyline(points, closed, layer=0, transformation=nil) [0, 'LWPOLYLINE', 8, layer, 90, points.length, 70, closed ? 1 : 0, ] + points.map do |point| vertex = transformation ? transformation.transform(point) : point [10, 20].zip(vertex.first(2).map {|v| format_value(v)}) end end |
#radius(element, transformation = nil) ⇒ Object
Emit the group codes for the radius property of an element
63 64 65 |
# File 'lib/dxf/unparser.rb', line 63 def radius(element, transformation=nil) [40, format_value(transformation ? transformation.transform(element.radius) : element.radius)] end |
#section_end ⇒ Object
67 68 69 |
# File 'lib/dxf/unparser.rb', line 67 def section_end [0, 'ENDSEC'] end |
#section_start(name) ⇒ Object
71 72 73 |
# File 'lib/dxf/unparser.rb', line 71 def section_start(name) [0, 'SECTION', 2, name] end |
#to_array(element, transformation = nil) ⇒ Array
Convert an element to an Array
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/dxf/unparser.rb', line 79 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::Edge, Geometry::Line line(element.first, element.last, layer, transformation) when Geometry::Polyline lwpolyline(element.points, element.closed?, layer, transformation) when Geometry::Rectangle, Geometry::Square, Geometry::Triangle lwpolyline(element.points, true, 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
103 104 105 106 107 |
# File 'lib/dxf/unparser.rb', line 103 def unparse(output, sketch) output << (section_start('HEADER') + section_end + section_start('ENTITIES') + to_array(sketch) + section_end + [0, 'EOF']).join("\n") end |