dxf 0.1 → 0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  # DXF
2
2
 
3
- TODO: Write a gem description
3
+ Tools for working with the popular DXF file format
4
4
 
5
5
  ## Installation
6
6
 
@@ -14,16 +14,18 @@ And then execute:
14
14
 
15
15
  Or install it yourself as:
16
16
 
17
- $ gem install DXF
17
+ $ gem install dxf
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ ```ruby
22
+ require 'dxf'
22
23
 
23
- ## Contributing
24
+ # To export the my_sketch object in inches
25
+ DXF.write('filename.dxf', my_sketch, :inches)
26
+ ```
24
27
 
25
- 1. Fork it
26
- 2. Create your feature branch (`git checkout -b my-new-feature`)
27
- 3. Commit your changes (`git commit -am 'Add some feature'`)
28
- 4. Push to the branch (`git push origin my-new-feature`)
29
- 5. Create new Pull Request
28
+ License
29
+ -------
30
+
31
+ Copyright 2012-2013 Brandon Fosdick <bfoz@bfoz.net> and released under the BSD license.
data/Rakefile CHANGED
@@ -1 +1,8 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs.push "lib"
6
+ t.test_files = FileList['test/**/*.rb']
7
+ t.verbose = true
8
+ end
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |gem|
6
6
  gem.name = "dxf"
7
- gem.version = 0.1
7
+ gem.version = '0.2'
8
8
  gem.authors = ["Brandon Fosdick"]
9
9
  gem.email = ["bfoz@bfoz.net"]
10
10
  gem.description = %q{Read and write DXF files using Ruby}
@@ -17,4 +17,5 @@ Gem::Specification.new do |gem|
17
17
  gem.require_paths = ["lib"]
18
18
 
19
19
  gem.add_dependency 'sketch'
20
+ gem.add_dependency 'units', '~> 2.1'
20
21
  end
data/lib/dxf.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'geometry'
2
2
  require 'sketch'
3
+ require 'units'
3
4
 
4
5
  module DXF
5
6
  =begin
@@ -12,9 +13,19 @@ Reading and writing of files using AutoCAD's {http://en.wikipedia.org/wiki/AutoC
12
13
  attr_accessor :container
13
14
 
14
15
  # Initialize with a Sketch
15
- # @param [Sketch] container A {Sketch} to export to DXF
16
- def initialize(container=nil)
17
- @container = container
16
+ # @param [String,Symbol] units The units to convert length values to (:inches or :millimeters)
17
+ def initialize(units=:mm)
18
+ @units = units
19
+ end
20
+
21
+ # Convert the given value to the correct units and return it as a formatted string
22
+ # @return [String]
23
+ def format_value(value)
24
+ if value.is_a? Units::Literal
25
+ "%g" % value.send("to_#{@units}".to_sym)
26
+ else
27
+ "%g" % value
28
+ end
18
29
  end
19
30
 
20
31
  def to_s
@@ -35,7 +46,12 @@ Reading and writing of files using AutoCAD's {http://en.wikipedia.org/wiki/AutoC
35
46
  end
36
47
  first = Point[first] unless first.is_a?(Geometry::Point)
37
48
  last = Point[last] unless last.is_a?(Geometry::Point)
38
- [0, 'LINE', 8, layer, 10, first.x, 20, first.y, 11, last.x, 21, last.y]
49
+ [ 0, 'LINE',
50
+ 8, layer,
51
+ 10, format_value(first.x),
52
+ 20, format_value(first.y),
53
+ 11, format_value(last.x),
54
+ 21, format_value(last.y)]
39
55
  end
40
56
 
41
57
  def section(name)
@@ -54,16 +70,16 @@ Reading and writing of files using AutoCAD's {http://en.wikipedia.org/wiki/AutoC
54
70
  case element
55
71
  when Geometry::Arc
56
72
  bytes.push 0, 'ARC'
57
- bytes.push 10, element.center.x
58
- bytes.push 20, element.center.y
59
- bytes.push 40, element.radius
60
- bytes.push 50, element.start_angle
61
- bytes.push 51, element.end_angle
73
+ bytes.push 10, format_value(element.center.x)
74
+ bytes.push 20, format_value(element.center.y)
75
+ bytes.push 40, format_value(element.radius)
76
+ bytes.push 50, format_value(element.start_angle)
77
+ bytes.push 51, format_value(element.end_angle)
62
78
  when Geometry::Circle
63
79
  bytes.push 0, 'CIRCLE'
64
- bytes.push 10, element.center.x
65
- bytes.push 20, element.center.y
66
- bytes.push 40, element.radius
80
+ bytes.push 10, format_value(element.center.x)
81
+ bytes.push 20, format_value(element.center.y)
82
+ bytes.push 40, format_value(element.radius)
67
83
  when Geometry::Line
68
84
  bytes.push line(element.first, element.last)
69
85
  when Geometry::Polyline
@@ -83,7 +99,11 @@ Reading and writing of files using AutoCAD's {http://en.wikipedia.org/wiki/AutoC
83
99
  end
84
100
  end
85
101
 
86
- def self.write(filename, sketch)
87
- File.write(filename, Builder.new.from_sketch(sketch))
102
+ # Export a {Sketch} to a DXF file
103
+ # @param [String] filename The path to write to
104
+ # @param [Sketch] sketch The {Sketch} to export
105
+ # @param [Symbol] units Convert all values to the specified units (:inches or :mm)
106
+ def self.write(filename, sketch, units=:mm)
107
+ File.write(filename, Builder.new(units).from_sketch(sketch))
88
108
  end
89
109
  end
@@ -13,7 +13,6 @@ describe DXF::Builder do
13
13
 
14
14
  describe "when give an empty Sketch object" do
15
15
  it "must export a minimal file" do
16
- skip
17
16
  builder.container = Sketch.new
18
17
  builder.to_s.must_equal (empty_header + entities_header + end_section + eof).join("\n")
19
18
  end
@@ -107,5 +106,67 @@ describe DXF::Builder do
107
106
  builder.to_s.must_equal (empty_header + entities_header + square_lines +
108
107
  end_section + eof).join "\n"
109
108
  end
109
+
110
+ describe "when the sketch has units" do
111
+ let(:sketch) { Sketch.new }
112
+
113
+ describe "when exporting to inches" do
114
+ subject { DXF::Builder.new :inches }
115
+ let(:square_inches) { File.read('test/fixtures/square_inches.dxf') }
116
+
117
+ before do
118
+ subject.container = sketch
119
+ end
120
+
121
+ describe "when the units are all inches" do
122
+ before do
123
+ sketch.push Geometry::Polygon.new [0.inches, 0.inches], [1.inches, 0.inches], [1.inches, 1.inches], [0.inches, 1.inches]
124
+ end
125
+
126
+ it "must not convert the values" do
127
+ subject.to_s.must_equal square_inches
128
+ end
129
+ end
130
+
131
+ describe "when the units are all metric" do
132
+ before do
133
+ sketch.push Geometry::Polygon.new [0.mm, 0.mm], [25.4.mm, 0.mm], [25.4.mm, 25.4.mm], [0.mm, 25.4.mm]
134
+ end
135
+
136
+ it "must convert the values" do
137
+ subject.to_s.must_equal square_inches
138
+ end
139
+ end
140
+ end
141
+
142
+ describe "when exporting to millimeters" do
143
+ subject { DXF::Builder.new :mm }
144
+ let(:square_millimeters) { File.read('test/fixtures/square_millimeters.dxf') }
145
+
146
+ before do
147
+ subject.container = sketch
148
+ end
149
+
150
+ describe "when the units are all inches" do
151
+ before do
152
+ sketch.push Geometry::Polygon.new [0.inches, 0.inches], [1.inches, 0.inches], [1.inches, 1.inches], [0.inches, 1.inches]
153
+ end
154
+
155
+ it "must convert the values" do
156
+ subject.to_s.must_equal square_millimeters
157
+ end
158
+ end
159
+
160
+ describe "when the units are all metric" do
161
+ before do
162
+ sketch.push Geometry::Polygon.new [0.mm, 0.mm], [25.4.mm, 0.mm], [25.4.mm, 25.4.mm], [0.mm, 25.4.mm]
163
+ end
164
+
165
+ it "must not convert the values" do
166
+ subject.to_s.must_equal square_millimeters
167
+ end
168
+ end
169
+ end
170
+ end
110
171
  end
111
172
  end
@@ -0,0 +1,62 @@
1
+ 0
2
+ SECTION
3
+ 2
4
+ HEADER
5
+ 0
6
+ ENDSEC
7
+ 0
8
+ SECTION
9
+ 2
10
+ ENTITIES
11
+ 0
12
+ LINE
13
+ 8
14
+ 0
15
+ 10
16
+ 0
17
+ 20
18
+ 0
19
+ 11
20
+ 1
21
+ 21
22
+ 0
23
+ 0
24
+ LINE
25
+ 8
26
+ 0
27
+ 10
28
+ 1
29
+ 20
30
+ 0
31
+ 11
32
+ 1
33
+ 21
34
+ 1
35
+ 0
36
+ LINE
37
+ 8
38
+ 0
39
+ 10
40
+ 1
41
+ 20
42
+ 1
43
+ 11
44
+ 0
45
+ 21
46
+ 1
47
+ 0
48
+ LINE
49
+ 8
50
+ 0
51
+ 10
52
+ 0
53
+ 20
54
+ 1
55
+ 11
56
+ 0
57
+ 21
58
+ 0
59
+ 0
60
+ ENDSEC
61
+ 0
62
+ EOF
@@ -0,0 +1,62 @@
1
+ 0
2
+ SECTION
3
+ 2
4
+ HEADER
5
+ 0
6
+ ENDSEC
7
+ 0
8
+ SECTION
9
+ 2
10
+ ENTITIES
11
+ 0
12
+ LINE
13
+ 8
14
+ 0
15
+ 10
16
+ 0
17
+ 20
18
+ 0
19
+ 11
20
+ 25.4
21
+ 21
22
+ 0
23
+ 0
24
+ LINE
25
+ 8
26
+ 0
27
+ 10
28
+ 25.4
29
+ 20
30
+ 0
31
+ 11
32
+ 25.4
33
+ 21
34
+ 25.4
35
+ 0
36
+ LINE
37
+ 8
38
+ 0
39
+ 10
40
+ 25.4
41
+ 20
42
+ 25.4
43
+ 11
44
+ 0
45
+ 21
46
+ 25.4
47
+ 0
48
+ LINE
49
+ 8
50
+ 0
51
+ 10
52
+ 0
53
+ 20
54
+ 25.4
55
+ 11
56
+ 0
57
+ 21
58
+ 0
59
+ 0
60
+ ENDSEC
61
+ 0
62
+ EOF
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dxf
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.2'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-21 00:00:00.000000000 Z
12
+ date: 2013-04-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sketch
@@ -27,6 +27,22 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: units
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '2.1'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '2.1'
30
46
  description: Read and write DXF files using Ruby
31
47
  email:
32
48
  - bfoz@bfoz.net
@@ -41,6 +57,8 @@ files:
41
57
  - dxf.gemspec
42
58
  - lib/dxf.rb
43
59
  - test/dxf/builder.rb
60
+ - test/fixtures/square_inches.dxf
61
+ - test/fixtures/square_millimeters.dxf
44
62
  homepage: http://github.com/bfoz/ruby-dxf
45
63
  licenses: []
46
64
  post_install_message:
@@ -67,3 +85,6 @@ specification_version: 3
67
85
  summary: Tools for working with the popular DXF file format
68
86
  test_files:
69
87
  - test/dxf/builder.rb
88
+ - test/fixtures/square_inches.dxf
89
+ - test/fixtures/square_millimeters.dxf
90
+ has_rdoc: