Module: STL

Defined in:
lib/stl.rb,
lib/stl/parser.rb

Defined Under Namespace

Classes: Parser

Class Method Summary collapse

Class Method Details

.read(filename) ⇒ STL

Read an STL file

Parameters:

  • filename (String)

    The path to the file to read

Returns:

  • (STL)

    the resulting STL object



7
8
9
# File 'lib/stl.rb', line 7

def self.read(filename)
	File.open(filename, 'r') {|f| STL::Parser.parse(f) }
end

.write(filename, faces, format = :binary) ⇒ Object

Write to an STL file

Parameters:

  • filename (String)

    The path to write to

  • faces (Array)

    An array of faces to write: [[Normal, Triangle], …]

  • format (Symbol) (defaults to: :binary)

    Pass :ascii to write an ASCII formatted file, and :binary to write a binary file



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/stl.rb', line 15

def self.write(filename, faces, format=:binary)
	File.open(filename, 'w') do |file|
 if format == :ascii
		file.puts 'solid '
		faces.each do |normal, triangle|
  file.puts "    facet normal %E %E %E" % [*normal]
  file.puts "\touter loop"
  triangle.points.each do |point|
			file.puts "\t    vertex %E %E %E" % [*point]
  end
  file.puts "\tendloop"
  file.puts '    endfacet'
		end
		file.puts 'endsolid '
 elsif format == :binary
		file.write 'STL Ruby'.ljust(80, "\0")	# A meager header
		file.write [faces.length].pack('V')	# The triangle count

		faces.each do |normal, triangle|
  file.write normal.to_a.pack("FFF")

  triangle.points.each do |point|
			file.write point.to_a.pack("FFF")
  end

  file.write "\0\0"
		end
 end
	end
end