Class: DXF::Unparser

Inherits:
Object
  • Object
show all
Defined in:
lib/dxf/unparser.rb

Instance Attribute Summary collapse

Element Formatters collapse

Property Converters collapse

Instance Method Summary collapse

Constructor Details

#initialize(units = :mm) ⇒ Unparser

Initialize with a Sketch

Parameters:

  • units (String, Symbol) (defaults to: :mm)

    The units to convert length values to (:inches or :millimeters)



12
13
14
# File 'lib/dxf/unparser.rb', line 12

def initialize(units=:mm)
    @units = units
end

Instance Attribute Details

#containerObject

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

Parameters:

  • point (Point)

    The center point to format



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

Returns:

  • (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_endObject



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

Parameters:

  • transformation (Transformation) (defaults to: nil)

    The transformation to apply to each geometry element

Returns:

  • (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_sObject



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

Parameters:

  • output (IO)

    A writable IO-like object

  • sketch (Sketch)

    The Sketch to unparse



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