Module: Siren

Defined in:
lib/siren.rb,
lib/io/dxf.rb,
lib/io/ply.rb,
lib/io/stl.rb,
lib/io/svg.rb,
lib/io/plot.rb,
lib/siren2/version.rb

Overview

Gnuplot data file I/O method

Defined Under Namespace

Classes: Compound, Edge, Face, Shape, Shell, Solid, Vec, Vertex, Wire

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.bscurve(*args) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/siren.rb', line 79

def self.bscurve(*args)
  c = nil
  if args.size == 4
    c = BSCurve.new(*args)
  else
    c = BSCurve.new(*(args[0..4]))
  end
  if args.size == 7 # with limit paramters
    Edge.new c, args[5], args[6]
  else
    Edge.new c
  end
end

.bzcurve(*args) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/siren.rb', line 97

def self.bzcurve(*args)
  c = nil
  if args.size <= 2
    c = BzCurve.new(*args)
  else
    c = BzCurve.new(*(args[0..1]))
  end
  if args.size == 4 # with limit parameters
    Edge.new c, args[-2], args[-1]
  else
    Edge.new c
  end
end

.load_model(path) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/siren.rb', line 59

def self.load_model(path)
  e5 = path[-5, path.size].downcase
  e4 = path[-4, path.size].downcase
  if e5 == ".brep" || e4 == ".brp"
    Siren.load_brep path
  elsif e5 == ".iges" || e4 == ".igs"
    Siren.load_iges path
  elsif e5 == ".step" || e4 == ".stp"
    Siren.load_step path
  elsif e4 == ".stl"
    Siren.load_stl path
  else
    raise TypeError
  end
end

.save_dxf(shape, path, deflect = 1.0, angle = 5.0.to_rad) ⇒ Object



6
7
8
9
10
11
12
13
14
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/io/dxf.rb', line 6

def self.save_dxf(shape, path, deflect = 1.0, angle = 5.0.to_rad)
  File.open(path, "w") do |f|
    @ff = f
    def self.w(code, data)
      @ff.write "% 3d\n#{data}\n" % [code]
    end
    def self.wp(pt, ofs = 0)
      w 10 + ofs, pt.x
      w 20 + ofs, pt.y
      w 30 + ofs, pt.z
    end
    w 0, "SECTION"
    w 2, "ENTITIES"
    shape.vertices(Siren::Edge).each do |e|
      w 0, "POINT"
      w 8, "0"
      wp e.xyz
    end
    shape.edges(Siren::Face).each do |e|
      ps = e.to_pts(deflect)
      if ps.size == 2
        w 0, "LINE"
        w 8, "0"
        wp ps.first
        wp ps.last, 1
      else
        w 0, "POLYLINE"
        wp [0, 0, 0]
        w 8, "0"
        w 66, 1
        w 70, 8 # 3D=8,2D=0
        ps.each do |pt|
          w 0, "VERTEX"
          w 8, "0"
          wp pt
        end
        w 0, "SEQEND"
      end
    end
    shape.faces.each do |face|
      face.triangle(deflect, angle).each do |m|
        w 0, "3DFACE"
        w 8, "0"
        mm = m[0,3]
        wp mm[0]
        wp mm[1], 1
        wp mm[2], 2
        wp mm[0], 3
        w 70, 8
      end
    end
    w 0, "ENDSEC"
    w 0, "EOF"
  end
  nil
end

.save_model(shape, path) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/siren.rb', line 33

def self.save_model(shape, path)
  e5 = path[-5, path.size].downcase
  e4 = path[-4, path.size].downcase
  if e5 == ".brep" || e4 == ".brp"
    Siren.save_brep shape, path
  elsif e5 == ".iges" || e4 == ".igs"
    Siren.save_iges shape, path
  elsif e5 == ".step" || e4 == ".stp"
    Siren.save_step shape, path
  elsif e4 == ".stl"
    Siren.save_stl shape, path
  elsif e4 == ".ply"
    Siren.save_ply shape, path
  elsif e4 == ".dxf"
    Siren.save_dxf shape, path
  elsif e4 == ".dat"
    Siren.save_plot shape, path
  else
    raise TypeError
  end
end

.save_plot(shape, path, face_mode = false, deflect = 1.0, angle = 5.0.to_rad) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/io/plot.rb', line 6

def self.save_plot(shape, path, face_mode = false, deflect = 1.0, angle = 5.0.to_rad)
  File.open(path, "w") do |f|
    f.puts "# Created by siren - http://siren.xyz/"
    if face_mode
      shape.faces.map {|e|
        e.triangle(deflect, angle).map {|m|
          m[0,3]
        }
      }.flatten(1).each do |m|
        f.puts "%.f %.f %.f" % m[0]
        f.puts "%.f %.f %.f" % m[1]
        f.puts "%.f %.f %.f" % m[2]
        f.puts ""
      end
    else
      # Curve mode
      shape.edges.each do |e|
        e.to_pts(deflect).each do |pos|
          f.puts "%.f %.f %.f" % pos
        end
        f.puts ""
      end
    end
  end
  nil
end

.save_ply(shape, path, deflect = 1.0, angle = 5.0.to_rad) ⇒ Object



6
7
8
9
10
11
12
13
14
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
45
46
47
48
49
50
# File 'lib/io/ply.rb', line 6

def self.save_ply(shape, path, deflect = 1.0, angle = 5.0.to_rad)
  File.open(path, "w") do |f|
    fs = shape.faces.map {|e|
      e.triangle(deflect, angle).map {|m|
        m[0,3]
      }
    }.flatten(1)
    es = shape.edges(Siren::Face).map{|e|
      e.to_pts(deflect)
    }
    vs = [fs, es].flatten(2).sort.uniq
    f.puts "ply"
    f.puts "format ascii 1.0"
    f.puts "comment Created by siren - http://siren.xyz/"
    if vs.size > 0
      f.puts "element vertex %d" % vs.size
      f.puts "property float x"
      f.puts "property float y"
      f.puts "property float z"
    end
    if es.size > 0
      f.puts "element edge %d" % es.size
      f.puts "property uint vertex1"
      f.puts "property uint vertex2"
    end
    if fs.size > 0
      f.puts "element face %d" % fs.size
      f.puts "property list uchar uint vertex_indices"
    end
    f.puts "end_header"
    vs.each do |e|
      f.puts "%.f %.f %.f" % e
    end
    es.each do |e|
      sp = vs.index(e.first)
      tp = vs.index(e.last)
      f.puts "%d %d" % [sp, tp]
    end
    fs.each do |e|
      pts = e.map{|m| vs.index(m)}
      f.puts pts.size.to_s + " " + pts.join(" ")
    end
    nil
  end
end

.save_stl(shape, path, ascii = true, deflect = 1.0, angle = 5.0.to_rad) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/io/stl.rb', line 7

def self.save_stl(shape, path, ascii = true, deflect = 1.0, angle = 5.0.to_rad)
  File.open(path, "w") do |f|
    if ascii
      f.write "solid shape, STL ascii file built with siren. http://siren.xyz/\n"
      shape.faces.each do |face|
        face.triangle(deflect, angle).each do |mesh|
          f.write "facet normal %8e %8e %8e\n" % mesh[5]
          f.write "  outer loop\n"
          f.write "    vertex %8e %8e %8e\n" % mesh[0]
          f.write "    vertex %8e %8e %8e\n" % mesh[1]
          f.write "    vertex %8e %8e %8e\n" % mesh[2]
          f.write "  endloop\n"
          f.write "endfacet\n"
        end
      end
      f.write "endsolid shape\n"
    else
      raise NotImplementedError
    end
  end
  nil
end

.save_svg(shape, path, deflect = 1.0, angle = 5.0.to_rad) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/io/svg.rb', line 7

def self.save_svg(shape, path, deflect = 1.0, angle = 5.0.to_rad)
  File.open(path, "w") do |f|
    df = 0.5 # Depth factor
    header = <<-EOF
<?xml version='1.0' encoding='utf-8'?>
<!-- This SVG file built with siren. http://siren.xyz/ -->
<svg xmlns='http://www.w3.org/2000/svg'>
<defs>
  <style type='text/css'><![CDATA[
    path {
      fill: none;
      stroke: black;
      stroke-width: 1;
    }
  ]]></style>
</defs>
<g transform="matrix(1,0,0,1,0,0)">
    EOF
    f.write header
    shape.edges.each do |edge|
      pts = edge.to_pts(deflect)
      sp = pts.shift
      d = "M#{sp.y + sp.x * df},#{sp.z + sp.x * df}"
      pts.each {|pt| d << "L#{pt.y + pt.x * df},#{pt.z + pt.x * df}" }
      f.write "    <path d='#{d}' />\n"
    end
    f.write "  </g>\n"
    f.write "</svg>\n"
  end
  nil
end

.warn(*msg) ⇒ Object



28
29
30
31
# File 'lib/siren.rb', line 28

def self.warn(*msg)
  return if $VERBOSE.nil? && msg.empty?
  $stderr.puts("siren: warning: " + msg.join)
end

Instance Method Details

#bscurve(*args) ⇒ Object



93
94
95
# File 'lib/siren.rb', line 93

def bscurve(*args)
  Siren.bscurve(*args)
end

#bzcurve(*args) ⇒ Object



111
112
113
# File 'lib/siren.rb', line 111

def bzcurve(*args)
  Siren.bzcurve(*args)
end

#load_model(path) ⇒ Object



75
76
77
# File 'lib/siren.rb', line 75

def load_model(path)
  Siren.load_model(path)
end

#save_dxf(*args) ⇒ Object



63
64
65
# File 'lib/io/dxf.rb', line 63

def save_dxf(*args)
  Siren.save_dxf(*args)
end

#save_model(shape, path) ⇒ Object



55
56
57
# File 'lib/siren.rb', line 55

def save_model(shape, path)
  Siren.save_model(shape, path)
end

#save_plot(*args) ⇒ Object



33
34
35
# File 'lib/io/plot.rb', line 33

def save_plot(*args)
  Siren.save_plot(*args)
end

#save_ply(*args) ⇒ Object



52
53
54
# File 'lib/io/ply.rb', line 52

def save_ply(*args)
  Siren.save_ply(*args)
end

#save_stl(*args) ⇒ Object



30
31
32
# File 'lib/io/stl.rb', line 30

def save_stl(*args)
  Siren.save_stl(*args)
end

#save_svg(*args) ⇒ Object



39
40
41
# File 'lib/io/svg.rb', line 39

def save_svg(*args)
  Siren.save_svg(*args)
end