Class: JennCad::Exporters::OpenScadObject

Inherits:
Object
  • Object
show all
Defined in:
lib/jenncad/exporters/openscad.rb

Instance Method Summary collapse

Constructor Details

#initialize(cmd, args, children = [], modifier = nil) ⇒ OpenScadObject

Returns a new instance of OpenScadObject.



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/jenncad/exporters/openscad.rb', line 3

def initialize(cmd, args, children=[], modifier=nil)
  @command = cmd
  @args = args
  @modifier = modifier || ""

  case children
  when Array
    @children = children
  else
    @children = [children]
  end
end

Instance Method Details

#handle_args(args) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/jenncad/exporters/openscad.rb', line 71

def handle_args(args)
  case args
  when String, Symbol
    return "\"#{args}\""
  when Array
    return args.map do |l|
      if l == nil
        0
      elsif l.kind_of? Array
        l # skipping check of 2-dmin Arrays for now (used in multmatrix)
      elsif l == 0
        0
      elsif l == l.to_i
        l.to_i
      else
        l.to_f
      end
    end
  when Hash
    res = []
    args.each do |k,v|
      if k.to_s == "fn"
        k = "$fn"
      end
      if v == nil
        next
      end
      if v.kind_of?(Symbol)
        v = v.to_s
      end
      if v.kind_of?(Array)
        v = handle_args(v)
      elsif !v.kind_of?(TrueClass) && !v.kind_of?(FalseClass) && v == v.to_i
        v = v.to_i
      elsif v.kind_of? BigDecimal
        v = v.to_f
      end
      if v.kind_of? String
        q = "\""
      else
        q = ""
      end
      res << "#{k}=#{q}#{v}#{q}"
    end
    res.join(",").gsub("size=","")
  else
    ""
  end
end

#handle_command(i = 1) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/jenncad/exporters/openscad.rb', line 45

def handle_command(i=1)
  case @children.size
  when 0
    "#{@modifier}#{@command}(#{handle_args(@args)});"
  when 1
    "#{@modifier}#{@command}(#{handle_args(@args)})#{@children.first.handle_command(i+1)}"
  when (1..)
    res = "#{@modifier}#{@command}(#{handle_args(@args)}){"
    res += nl
    inner = @children.map do |c|
      next if c == nil
      c.handle_command(i+1)
    end
    res += tabs(i, inner.compact)
    res += nl
    res += tabs(i-1,["}"])+nl
    res
  end
end

#handle_moduleObject



38
39
40
41
42
43
# File 'lib/jenncad/exporters/openscad.rb', line 38

def handle_module
  res = "module #{@args}(){"+nl
  res += tabs(1, @children.map{|c| c.handle_command(2) })
  res += "}"
  res
end

#nlObject



16
17
18
# File 'lib/jenncad/exporters/openscad.rb', line 16

def nl
  "\n"
end

#tabs(i, a) ⇒ Object



65
66
67
68
69
# File 'lib/jenncad/exporters/openscad.rb', line 65

def tabs(i,a)
  a.map{ |l|
    "  " * i + l
  }.join(nl)
end

#to_sObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/jenncad/exporters/openscad.rb', line 20

def to_s
  case @command
  when nil
    ""
  when :head
    res = "$fn=64;"+nl
    @children.each do |c|
      res += c.to_s+nl
    end
    res
  when :module
    handle_module
  when String, Symbol
    handle_command
  else
  end
end