mersh 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -21,7 +21,7 @@ require 'json'
21
21
  require 'digest/md5'
22
22
 
23
23
  class Mersh
24
- VERSION = "0.0.2"
24
+ VERSION = "0.0.3"
25
25
  attr_accessor :vertices, :faces, :normals
26
26
 
27
27
  def initialize(file)
@@ -82,10 +82,17 @@ class Mersh
82
82
  private
83
83
 
84
84
  def parse
85
- if @file.readline =~ /^solid/i
86
- parse_ascii_stl
87
- else
85
+ # some binary stls begin with the word solid, so can't rely on that
86
+ # instead, try to read the face count as if it was a binary stl and see if it matches the file size
87
+ @file.rewind
88
+ @file.seek 80, IO::SEEK_CUR
89
+ face_count = @file.read(4).unpack("I")[0]
90
+ predicted_size = 80 + 4 + 50 * face_count
91
+
92
+ if File.size(@file.path) == predicted_size
88
93
  parse_binary_stl
94
+ else
95
+ parse_ascii_stl
89
96
  end
90
97
  end
91
98
 
@@ -95,11 +102,12 @@ class Mersh
95
102
  @normals = []
96
103
 
97
104
  face_count = 0
98
- vertex_count = 0
105
+ vertex_count = 0
99
106
  vertex_index = {}
100
107
 
101
- line_pos = 1
108
+ line_pos = 1
102
109
 
110
+ @file.rewind
103
111
  stl_data = @file.read
104
112
 
105
113
  stl_data.gsub!(/^\s*solid.*$/i, '')
@@ -0,0 +1 @@
1
+ translate([0,0,5]) cube([20, 20, 10], center=true);
@@ -1,86 +1,86 @@
1
- solid Default
2
- facet normal 0.000000e+00 0.000000e+00 -1.000000e+00
1
+ solid OpenSCAD_Model
2
+ facet normal -1 0 0
3
3
  outer loop
4
- vertex 1.000000e+01 1.000000e+01 0.000000e+00
5
- vertex -1.000000e+01 -1.000000e+01 0.000000e+00
6
- vertex -1.000000e+01 1.000000e+01 0.000000e+00
4
+ vertex -10 -10 10
5
+ vertex -10 10 10
6
+ vertex -10 -10 0
7
7
  endloop
8
8
  endfacet
9
- facet normal 0.000000e+00 0.000000e+00 -1.000000e+00
9
+ facet normal -1 0 0
10
10
  outer loop
11
- vertex -1.000000e+01 -1.000000e+01 0.000000e+00
12
- vertex 1.000000e+01 1.000000e+01 0.000000e+00
13
- vertex 1.000000e+01 -1.000000e+01 0.000000e+00
11
+ vertex -10 -10 0
12
+ vertex -10 10 10
13
+ vertex -10 10 0
14
14
  endloop
15
15
  endfacet
16
- facet normal 0.000000e+00 0.000000e+00 1.000000e+00
16
+ facet normal 0 0 1
17
17
  outer loop
18
- vertex 1.000000e+01 -1.000000e+01 1.000000e+01
19
- vertex -1.000000e+01 1.000000e+01 1.000000e+01
20
- vertex -1.000000e+01 -1.000000e+01 1.000000e+01
18
+ vertex -10 -10 10
19
+ vertex 10 -10 10
20
+ vertex 10 10 10
21
21
  endloop
22
22
  endfacet
23
- facet normal 0.000000e+00 0.000000e+00 1.000000e+00
23
+ facet normal 0 0 1
24
24
  outer loop
25
- vertex -1.000000e+01 1.000000e+01 1.000000e+01
26
- vertex 1.000000e+01 -1.000000e+01 1.000000e+01
27
- vertex 1.000000e+01 1.000000e+01 1.000000e+01
25
+ vertex -10 10 10
26
+ vertex -10 -10 10
27
+ vertex 10 10 10
28
28
  endloop
29
29
  endfacet
30
- facet normal 1.000000e+00 0.000000e+00 0.000000e+00
30
+ facet normal 0 -1 0
31
31
  outer loop
32
- vertex 1.000000e+01 1.000000e+01 0.000000e+00
33
- vertex 1.000000e+01 -1.000000e+01 1.000000e+01
34
- vertex 1.000000e+01 -1.000000e+01 0.000000e+00
32
+ vertex -10 -10 0
33
+ vertex 10 -10 0
34
+ vertex 10 -10 10
35
35
  endloop
36
36
  endfacet
37
- facet normal 1.000000e+00 0.000000e+00 0.000000e+00
37
+ facet normal 0 -1 0
38
38
  outer loop
39
- vertex 1.000000e+01 -1.000000e+01 1.000000e+01
40
- vertex 1.000000e+01 1.000000e+01 0.000000e+00
41
- vertex 1.000000e+01 1.000000e+01 1.000000e+01
39
+ vertex -10 -10 10
40
+ vertex -10 -10 0
41
+ vertex 10 -10 10
42
42
  endloop
43
43
  endfacet
44
- facet normal -0.000000e+00 -1.000000e+00 -0.000000e+00
44
+ facet normal 0 0 -1
45
45
  outer loop
46
- vertex 1.000000e+01 -1.000000e+01 1.000000e+01
47
- vertex -1.000000e+01 -1.000000e+01 0.000000e+00
48
- vertex 1.000000e+01 -1.000000e+01 0.000000e+00
46
+ vertex -10 10 0
47
+ vertex 10 10 0
48
+ vertex -10 -10 0
49
49
  endloop
50
50
  endfacet
51
- facet normal -0.000000e+00 -1.000000e+00 -0.000000e+00
51
+ facet normal 0 0 -1
52
52
  outer loop
53
- vertex -1.000000e+01 -1.000000e+01 0.000000e+00
54
- vertex 1.000000e+01 -1.000000e+01 1.000000e+01
55
- vertex -1.000000e+01 -1.000000e+01 1.000000e+01
53
+ vertex -10 -10 0
54
+ vertex 10 10 0
55
+ vertex 10 -10 0
56
56
  endloop
57
57
  endfacet
58
- facet normal -1.000000e+00 0.000000e+00 0.000000e+00
58
+ facet normal 0 1 0
59
59
  outer loop
60
- vertex -1.000000e+01 1.000000e+01 1.000000e+01
61
- vertex -1.000000e+01 -1.000000e+01 0.000000e+00
62
- vertex -1.000000e+01 -1.000000e+01 1.000000e+01
60
+ vertex -10 10 10
61
+ vertex 10 10 10
62
+ vertex -10 10 0
63
63
  endloop
64
64
  endfacet
65
- facet normal -1.000000e+00 0.000000e+00 0.000000e+00
65
+ facet normal 0 1 0
66
66
  outer loop
67
- vertex -1.000000e+01 -1.000000e+01 0.000000e+00
68
- vertex -1.000000e+01 1.000000e+01 1.000000e+01
69
- vertex -1.000000e+01 1.000000e+01 0.000000e+00
67
+ vertex -10 10 0
68
+ vertex 10 10 10
69
+ vertex 10 10 0
70
70
  endloop
71
71
  endfacet
72
- facet normal 0.000000e+00 1.000000e+00 0.000000e+00
72
+ facet normal 1 0 0
73
73
  outer loop
74
- vertex -1.000000e+01 1.000000e+01 1.000000e+01
75
- vertex 1.000000e+01 1.000000e+01 0.000000e+00
76
- vertex -1.000000e+01 1.000000e+01 0.000000e+00
74
+ vertex 10 -10 0
75
+ vertex 10 10 0
76
+ vertex 10 10 10
77
77
  endloop
78
78
  endfacet
79
- facet normal 0.000000e+00 1.000000e+00 0.000000e+00
79
+ facet normal 1 0 0
80
80
  outer loop
81
- vertex 1.000000e+01 1.000000e+01 0.000000e+00
82
- vertex -1.000000e+01 1.000000e+01 1.000000e+01
83
- vertex 1.000000e+01 1.000000e+01 1.000000e+01
81
+ vertex 10 -10 10
82
+ vertex 10 -10 0
83
+ vertex 10 10 10
84
84
  endloop
85
85
  endfacet
86
- endsolid Default
86
+ endsolid OpenSCAD_Model
Binary file
@@ -1 +1 @@
1
- {"normals":[[0.0,0.0,-1.0],[0.0,0.0,-1.0],[0.0,0.0,1.0],[0.0,0.0,1.0],[1.0,0.0,0.0],[1.0,0.0,0.0],[-0.0,-1.0,-0.0],[-0.0,-1.0,-0.0],[-1.0,0.0,0.0],[-1.0,0.0,0.0],[0.0,1.0,0.0],[0.0,1.0,0.0]],"vertices":[[10.0,10.0,0.0],[-10.0,-10.0,0.0],[-10.0,10.0,0.0],[10.0,-10.0,0.0],[10.0,-10.0,10.0],[-10.0,10.0,10.0],[-10.0,-10.0,10.0],[10.0,10.0,10.0]],"faces":[[0,1,2],[1,0,3],[4,5,6],[5,4,7],[0,4,3],[4,0,7],[4,1,3],[1,4,6],[5,1,6],[1,5,2],[5,0,2],[0,5,7]]}
1
+ {"faces":[[0,1,2],[2,1,3],[0,4,5],[1,0,5],[2,6,4],[0,2,4],[3,7,2],[2,7,6],[1,5,3],[3,5,7],[6,7,5],[4,6,5]],"vertices":[[-10.0,-10.0,10.0],[-10.0,10.0,10.0],[-10.0,-10.0,0.0],[-10.0,10.0,0.0],[10.0,-10.0,10.0],[10.0,10.0,10.0],[10.0,-10.0,0.0],[10.0,10.0,0.0]],"normals":[[-1.0,0.0,0.0],[-1.0,0.0,0.0],[0.0,0.0,1.0],[0.0,0.0,1.0],[0.0,-1.0,0.0],[0.0,-1.0,0.0],[0.0,0.0,-1.0],[0.0,0.0,-1.0],[0.0,1.0,0.0],[0.0,1.0,0.0],[1.0,0.0,0.0],[1.0,0.0,0.0]]}
@@ -1 +1 @@
1
- {"morphTargets":[],"uvs":[[]],"morphColors":[],"vertices":[10.0,10.0,0.0,-10.0,-10.0,0.0,-10.0,10.0,0.0,10.0,-10.0,0.0,10.0,-10.0,10.0,-10.0,10.0,10.0,-10.0,-10.0,10.0,10.0,10.0,10.0],"colors":[],"scale":1.0,"metadata":{"generatedBy":"Mersh","formatVersion":3},"materials":[],"normals":[],"faces":[0,0,1,2,0,1,0,3,0,4,5,6,0,5,4,7,0,0,4,3,0,4,0,7,0,4,1,3,0,1,4,6,0,5,1,6,0,1,5,2,0,5,0,2,0,0,5,7]}
1
+ {"morphTargets":[],"morphColors":[],"faces":[0,0,1,2,0,2,1,3,0,0,4,5,0,1,0,5,0,2,6,4,0,0,2,4,0,3,7,2,0,2,7,6,0,1,5,3,0,3,5,7,0,6,7,5,0,4,6,5],"metadata":{"generatedBy":"Mersh","formatVersion":3},"uvs":[[]],"colors":[],"materials":[],"vertices":[-10.0,-10.0,10.0,-10.0,10.0,10.0,-10.0,-10.0,0.0,-10.0,10.0,0.0,10.0,-10.0,10.0,10.0,10.0,10.0,10.0,-10.0,0.0,10.0,10.0,0.0],"scale":1.0,"normals":[]}
@@ -15,7 +15,7 @@ class MershTest < Test::Unit::TestCase
15
15
  box = Mersh.new(@ascii_stl)
16
16
  box_plain_json = box.to_json
17
17
  box_threejs_json = box.to_json('threejs')
18
-
18
+
19
19
  assert_equal String, box_threejs_json.class
20
20
  assert_equal JSON.parse(File.open(@threejs_json).read), JSON.parse(box_threejs_json)
21
21
 
@@ -27,7 +27,7 @@ class MershTest < Test::Unit::TestCase
27
27
  box = Mersh.new(@binary_stl)
28
28
  box_plain_json = box.to_json
29
29
  box_threejs_json = box.to_json('threejs')
30
-
30
+
31
31
  assert_equal String, box_threejs_json.class
32
32
  assert_equal JSON.parse(File.open(@threejs_json).read), JSON.parse(box_threejs_json)
33
33
 
@@ -55,4 +55,10 @@ class MershTest < Test::Unit::TestCase
55
55
 
56
56
  assert stl.vertices.size > 0
57
57
  end
58
+
59
+ def test_solid_header_binary
60
+ stl = Mersh.new(@current_dir + "/binary_solid_header.stl")
61
+
62
+ assert stl.vertices.size > 0
63
+ end
58
64
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mersh
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tony Buser
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-08-22 00:00:00 Z
18
+ date: 2013-04-02 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: json
@@ -42,6 +42,8 @@ extra_rdoc_files: []
42
42
  files:
43
43
  - lib/mersh.rb
44
44
  - bin/mersh
45
+ - test/binary_solid_header.stl
46
+ - test/box.scad
45
47
  - test/box_ascii.stl
46
48
  - test/box_binary.stl
47
49
  - test/box_plain.json