Module: SolidRuby

Includes:
Math, Assemblies, BillOfMaterial, CSGModelling, CSGModifiers, Extras, Helpers, Parameters, Primitives, PrintedThreads, ScrewThreads, Transformations
Defined in:
lib/solidruby/version.rb,
lib/solidruby/solidruby.rb,
lib/solidruby/rubyscad_bridge.rb,
lib/solidruby/solidruby_object.rb

Overview

This file is part of SolidRuby.

SolidRuby is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

SolidRuby is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with SolidRuby.  If not, see <http://www.gnu.org/licenses/>.

Defined Under Namespace

Modules: Assemblies, BillOfMaterial, CSGModelling, CSGModifiers, Extras, Helpers, Parameters, Primitives, PrintedThreads, ScrewThreads, Transformations Classes: ProjectCli, RubyScadBridge, SolidRubyObject

Constant Summary collapse

VERSION =
'0.3.0'.freeze

Instance Method Summary collapse

Methods included from Parameters

#params

Methods included from CSGModifiers

#color, #linear_extrude, #projection, #rotate_extrude

Methods included from CSGModelling

#*, #+, #-, #hull, #minkowski, #optimize_difference, #optimize_union

Methods included from Primitives

#circle, #cube, #cylinder, #import, #polygon, #polyhedron, #render, #sphere, #square, #surface, #text

Methods included from Helpers

#calculate_point_on, #chamfer, #corners, #fillet, #is_horizontal?, #is_vertical?, #is_x_dir?, #is_y_dir?, #normalise_edges, #rounded_cube, #translations_for_edge, #triangle

Methods included from Extras

#knurl, #knurled_cube, #knurled_cylinder

Methods included from ScrewThreads

#create_bolts

Methods included from BillOfMaterial

bom

Instance Method Details

#degrees(a) ⇒ Object



72
73
74
# File 'lib/solidruby/solidruby.rb', line 72

def degrees(a)
  a * 180 / Math::PI
end

#get_classes_from_file(filename) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/solidruby/solidruby.rb', line 131

def get_classes_from_file(filename)
  classes = []
  File.readlines(filename).find_all { |l| l.include?('class') }.each do |line|
    # strip all spaces, tabs
    line.strip!
    # ignore comments (Warning: will not worth with ruby multi line comments)
    next if line[0..0] == '#'
    #	strip class definition
    line = line[6..-1]
    # strip until space appears - or if not, to the end
    classes << Object.const_get(line[0..line.index(' ').to_i - 1])
  end

  classes
end

#get_position_rec(obj, level = 0) ⇒ Object



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

def get_position_rec(obj, level = 0)
  position = [0, 0, 0]
  return position if obj.nil?
  obj.each do |o|
    o.transformations.each do |t|
      next unless t.class == Translate
      t.args[:x] ||= 0
      t.args[:y] ||= 0
      t.args[:z] ||= 0
      position[0] += t.args[:x]
      position[1] += t.args[:y]
      position[2] += t.args[:z]
    end
    #		puts "  " * level + position.inspect
    x, y, z = get_position_rec(o.children, level + 1)
    position[0] += x
    position[1] += y
    position[2] += z
  end
  position
end

#long_slot(args) ⇒ Object

produces a hull() of 2 cylidners accepts d,r,h for cylinder options l long slot length



64
65
66
# File 'lib/solidruby/solidruby.rb', line 64

def long_slot(args)
  hull(cylinder(d: args[:d], r: args[:r], h: args[:h]), cylinder(d: args[:d], r: args[:r], h: args[:h]).translate(x: args[:l]))
end

#position(obj) ⇒ Object

this is experimental, does only work on simple parts. example: The bolt head is on the -z plane, this will move it to “zero”



57
58
59
# File 'lib/solidruby/solidruby.rb', line 57

def position(obj)
  get_position_rec(obj.children)
end

#radians(a) ⇒ Object



68
69
70
# File 'lib/solidruby/solidruby.rb', line 68

def radians(a)
  a / 180.0 * Math::PI
end

#save!Object



76
77
78
# File 'lib/solidruby/solidruby.rb', line 76

def save!
  Dir.glob('lib/**/*.rb').map { |l| get_classes_from_file(l) }.flatten.map { |l| save_all(l) }
end

#save_all(class_name, fn = $fn) ⇒ Object

Saves all files generated of a SolidRuby file Saves outputs of

  • show

  • output

  • view*



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
120
121
122
123
124
125
126
127
128
129
# File 'lib/solidruby/solidruby.rb', line 85

def save_all(class_name, fn = $fn)
  res = class_name.send :new

  # skip defined classes
  skip = class_name.send :get_skip
  skip = [] if skip.nil?
  skip << 'show_hardware'
  added_views = class_name.send :get_views

  # regexp for output* view* show*
  (res.methods.grep(Regexp.union(/^output/, /^view/, /^show/)) + added_views).each do |i|
    next if skip.include? i.to_s
    output = nil

    begin
      res.send :initialize # ensure default values are loaded at each interation
      output = res.send i

      # if previous call resulted in a SolidRubyObject, don't call the show method again,
      # otherwise call it.
      unless	output.is_a? SolidRubyObject
        output = if i.to_s.include? 'output'
                  res.output
                else
                  res.show
                end
      end

      output.save("output/#{res.class}_#{i}.scad", "$fn=#{fn};") unless output.nil?
    rescue Exception => e
      File.open("output/#{res.class}_#{i}.scad", "w") do |file|
        file <<
          <<~ERROR
             echo("--ERROR: #{e.message}");
             echo("#{::CGI.escapeHTML(e.backtrace.last)}");
             assert(false); // force stop rendering
             /*
             Full stack trace:
             #{e.backtrace.join("\n")}
             */
          ERROR
      end
    end
  end
end