dxf 0 → 0.1
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.
- data/dxf.gemspec +3 -1
- data/lib/dxf.rb +87 -1
- data/test/dxf/builder.rb +111 -0
- metadata +21 -3
data/dxf.gemspec
CHANGED
@@ -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
|
7
|
+
gem.version = 0.1
|
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}
|
@@ -15,4 +15,6 @@ Gem::Specification.new do |gem|
|
|
15
15
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
16
16
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
17
17
|
gem.require_paths = ["lib"]
|
18
|
+
|
19
|
+
gem.add_dependency 'sketch'
|
18
20
|
end
|
data/lib/dxf.rb
CHANGED
@@ -1,3 +1,89 @@
|
|
1
|
+
require 'geometry'
|
2
|
+
require 'sketch'
|
3
|
+
|
1
4
|
module DXF
|
2
|
-
|
5
|
+
=begin
|
6
|
+
Reading and writing of files using AutoCAD's {http://en.wikipedia.org/wiki/AutoCAD_DXF Drawing Interchange File} format.
|
7
|
+
|
8
|
+
{http://usa.autodesk.com/adsk/servlet/item?siteID=123112&id=12272454&linkID=10809853 DXF Specifications}
|
9
|
+
=end
|
10
|
+
|
11
|
+
class Builder
|
12
|
+
attr_accessor :container
|
13
|
+
|
14
|
+
# Initialize with a Sketch
|
15
|
+
# @param [Sketch] container A {Sketch} to export to DXF
|
16
|
+
def initialize(container=nil)
|
17
|
+
@container = container
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_s
|
21
|
+
from_sketch(container)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Convert a {Geometry::Line} into an entity array
|
25
|
+
# @overload line(Line, layer=0)
|
26
|
+
# @overload line(Point, Point, layer=0)
|
27
|
+
def line(*args)
|
28
|
+
if args[0].is_a?(Geometry::Line)
|
29
|
+
first, last = args[0].first, args[0].last
|
30
|
+
layer = args[1] ||= 0
|
31
|
+
else
|
32
|
+
first = args[0]
|
33
|
+
last = args[1]
|
34
|
+
layer = args[2] ||= 0
|
35
|
+
end
|
36
|
+
first = Point[first] unless first.is_a?(Geometry::Point)
|
37
|
+
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]
|
39
|
+
end
|
40
|
+
|
41
|
+
def section(name)
|
42
|
+
[0, 'SECTION', 2, name]
|
43
|
+
end
|
44
|
+
|
45
|
+
# Build a DXF from a Sketch
|
46
|
+
# @return [Array] Array of bytes to be written to a file
|
47
|
+
def from_sketch(sketch)
|
48
|
+
bytes = []
|
49
|
+
bytes.push section('HEADER')
|
50
|
+
bytes.push 0, 'ENDSEC'
|
51
|
+
bytes.push section('ENTITIES')
|
52
|
+
|
53
|
+
sketch.geometry.map do |element|
|
54
|
+
case element
|
55
|
+
when Geometry::Arc
|
56
|
+
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
|
62
|
+
when Geometry::Circle
|
63
|
+
bytes.push 0, 'CIRCLE'
|
64
|
+
bytes.push 10, element.center.x
|
65
|
+
bytes.push 20, element.center.y
|
66
|
+
bytes.push 40, element.radius
|
67
|
+
when Geometry::Line
|
68
|
+
bytes.push line(element.first, element.last)
|
69
|
+
when Geometry::Polyline
|
70
|
+
element.edges.map {|edge| bytes.push line(edge.first, edge.last) }
|
71
|
+
when Geometry::Rectangle
|
72
|
+
element.edges.map {|edge| bytes.push line(edge.first, edge.last) }
|
73
|
+
when Geometry::Square
|
74
|
+
points = element.points
|
75
|
+
points.each_cons(2) {|p1,p2| bytes.push line(p1,p2) }
|
76
|
+
bytes.push line(points.last, point.first)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
bytes.push 0, 'ENDSEC'
|
81
|
+
bytes.push 0, 'EOF'
|
82
|
+
bytes.join "\n"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def self.write(filename, sketch)
|
87
|
+
File.write(filename, Builder.new.from_sketch(sketch))
|
88
|
+
end
|
3
89
|
end
|
data/test/dxf/builder.rb
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'dxf'
|
3
|
+
|
4
|
+
describe DXF::Builder do
|
5
|
+
let(:builder) { DXF::Builder.new }
|
6
|
+
let(:eof) { ['0', 'EOF'] }
|
7
|
+
let(:empty_header) {['0', 'SECTION',
|
8
|
+
'2', 'HEADER',
|
9
|
+
'0', 'ENDSEC']}
|
10
|
+
let(:end_section) { ['0', 'ENDSEC'] }
|
11
|
+
let(:entities_header) { ['0', 'SECTION',
|
12
|
+
'2', 'ENTITIES'] }
|
13
|
+
|
14
|
+
describe "when give an empty Sketch object" do
|
15
|
+
it "must export a minimal file" do
|
16
|
+
skip
|
17
|
+
builder.container = Sketch.new
|
18
|
+
builder.to_s.must_equal (empty_header + entities_header + end_section + eof).join("\n")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "when exporting a Sketch" do
|
23
|
+
let(:sketch) { Sketch.new }
|
24
|
+
let(:square_lines) {
|
25
|
+
['0', 'LINE',
|
26
|
+
'8', '0',
|
27
|
+
'10', '0',
|
28
|
+
'20', '0',
|
29
|
+
'11', '1',
|
30
|
+
'21', '0',
|
31
|
+
'0', 'LINE',
|
32
|
+
'8', '0',
|
33
|
+
'10', '1',
|
34
|
+
'20', '0',
|
35
|
+
'11', '1',
|
36
|
+
'21', '1',
|
37
|
+
'0', 'LINE',
|
38
|
+
'8', '0',
|
39
|
+
'10', '1',
|
40
|
+
'20', '1',
|
41
|
+
'11', '0',
|
42
|
+
'21', '1',
|
43
|
+
'0', 'LINE',
|
44
|
+
'8', '0',
|
45
|
+
'10', '0',
|
46
|
+
'20', '1',
|
47
|
+
'11', '0',
|
48
|
+
'21', '0']
|
49
|
+
}
|
50
|
+
|
51
|
+
before do
|
52
|
+
builder.container = sketch
|
53
|
+
end
|
54
|
+
|
55
|
+
it "with a single Arc" do
|
56
|
+
sketch.push Geometry::Arc.new [0,0], 1, 0, 45
|
57
|
+
builder.to_s.must_equal (empty_header + entities_header + ['0', 'ARC',
|
58
|
+
'10', '0',
|
59
|
+
'20', '0',
|
60
|
+
'40', '1',
|
61
|
+
'50', '0',
|
62
|
+
'51', '45'] + end_section + eof).join("\n")
|
63
|
+
end
|
64
|
+
|
65
|
+
it "with a single Circle" do
|
66
|
+
sketch.push Geometry::Circle.new [0,0], 1
|
67
|
+
builder.to_s.must_equal (empty_header + entities_header + ['0', 'CIRCLE',
|
68
|
+
'10', '0',
|
69
|
+
'20', '0',
|
70
|
+
'40', '1'] + end_section + eof).join("\n")
|
71
|
+
end
|
72
|
+
|
73
|
+
it "with a single Line" do
|
74
|
+
sketch.push Geometry::Line[[0,0], [1,1]]
|
75
|
+
builder.to_s.must_equal (empty_header + entities_header +
|
76
|
+
['0', 'LINE',
|
77
|
+
'8', '0',
|
78
|
+
'10', '0',
|
79
|
+
'20', '0',
|
80
|
+
'11', '1',
|
81
|
+
'21', '1'] +
|
82
|
+
end_section + eof).join("\n")
|
83
|
+
end
|
84
|
+
|
85
|
+
it "with a single Polygon" do
|
86
|
+
sketch.push Geometry::Polygon.new [0,0], [1,0], [1,1], [0,1]
|
87
|
+
builder.to_s.must_equal (empty_header + entities_header +
|
88
|
+
square_lines +
|
89
|
+
end_section + eof).join("\n")
|
90
|
+
end
|
91
|
+
|
92
|
+
it "with a single Polyline" do
|
93
|
+
sketch.push Geometry::Polyline.new [0,0], [1,0], [1,1], [0,1]
|
94
|
+
builder.to_s.must_equal (empty_header + entities_header +
|
95
|
+
square_lines[0, 36] +
|
96
|
+
end_section + eof).join("\n")
|
97
|
+
end
|
98
|
+
|
99
|
+
it "with a single Rectangle" do
|
100
|
+
sketch.push Geometry::Rectangle.new [0,0], [1,1]
|
101
|
+
builder.to_s.must_equal (empty_header + entities_header + square_lines +
|
102
|
+
end_section + eof).join "\n"
|
103
|
+
end
|
104
|
+
|
105
|
+
it "with a single Square" do
|
106
|
+
sketch.push Geometry::Rectangle.new [0,0], [1,1]
|
107
|
+
builder.to_s.must_equal (empty_header + entities_header + square_lines +
|
108
|
+
end_section + eof).join "\n"
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
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'
|
4
|
+
version: '0.1'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,23 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
date: 2013-04-21 00:00:00.000000000 Z
|
13
|
-
dependencies:
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: sketch
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
14
30
|
description: Read and write DXF files using Ruby
|
15
31
|
email:
|
16
32
|
- bfoz@bfoz.net
|
@@ -24,6 +40,7 @@ files:
|
|
24
40
|
- Rakefile
|
25
41
|
- dxf.gemspec
|
26
42
|
- lib/dxf.rb
|
43
|
+
- test/dxf/builder.rb
|
27
44
|
homepage: http://github.com/bfoz/ruby-dxf
|
28
45
|
licenses: []
|
29
46
|
post_install_message:
|
@@ -48,4 +65,5 @@ rubygems_version: 1.8.25
|
|
48
65
|
signing_key:
|
49
66
|
specification_version: 3
|
50
67
|
summary: Tools for working with the popular DXF file format
|
51
|
-
test_files:
|
68
|
+
test_files:
|
69
|
+
- test/dxf/builder.rb
|