solidruby 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +4 -0
  3. data/.travis.yml +3 -0
  4. data/COPYING +674 -0
  5. data/Gemfile +2 -0
  6. data/Guardfile +24 -0
  7. data/LICENSE.md +675 -0
  8. data/README.md +180 -0
  9. data/Rakefile +10 -0
  10. data/bin/solidruby +61 -0
  11. data/examples/chamfered_cube.rb +39 -0
  12. data/examples/filleted_cube.rb +39 -0
  13. data/examples/gear.rb +11 -0
  14. data/examples/knurls.rb +12 -0
  15. data/examples/nut_support.rb +17 -0
  16. data/examples/openscad_examples/old/example001.rb +42 -0
  17. data/examples/openscad_examples/old/example002.rb +45 -0
  18. data/examples/openscad_examples/old/example003.rb +37 -0
  19. data/examples/openscad_examples/old/example004.rb +22 -0
  20. data/examples/openscad_examples/old/example005.rb +45 -0
  21. data/examples/openscad_examples/old/example006.rb +85 -0
  22. data/examples/openscad_examples/old/example007.dxf +2410 -0
  23. data/examples/openscad_examples/old/example007.rb +107 -0
  24. data/examples/openscad_examples/old/example008.rb +61 -0
  25. data/examples/openscad_examples/old/example009.dxf +3894 -0
  26. data/examples/openscad_examples/old/example009.rb.old +48 -0
  27. data/examples/openscad_examples/old/example010.dat +51 -0
  28. data/examples/openscad_examples/old/example010.rb +23 -0
  29. data/examples/openscad_examples/old/example011.rb +42 -0
  30. data/examples/openscad_examples/old/example012.rb +22 -0
  31. data/examples/openscad_examples/old/example012.stl +450 -0
  32. data/examples/openscad_examples/old/example013.dxf +2276 -0
  33. data/examples/openscad_examples/old/example013.rb +30 -0
  34. data/examples/openscad_examples/old/example014.rb +30 -0
  35. data/examples/openscad_examples/old/example015.rb +46 -0
  36. data/examples/openscad_examples/old/example016.rb +75 -0
  37. data/examples/openscad_examples/old/example016.stl +0 -0
  38. data/examples/pipe.rb +38 -0
  39. data/examples/pipe_bug.rb +16 -0
  40. data/examples/printed_gear.rb +12 -0
  41. data/examples/printed_gear2.rb +16 -0
  42. data/examples/printed_thread.rb +9 -0
  43. data/examples/printed_thread2.rb +13 -0
  44. data/examples/stack.rb +21 -0
  45. data/examples/threads.rb +183 -0
  46. data/examples/threads2.rb +93 -0
  47. data/examples/threads3.rb +46 -0
  48. data/lib/solidruby.rb +60 -0
  49. data/lib/solidruby/assemblies/assembly.rb +176 -0
  50. data/lib/solidruby/assemblies/bolt.rb +158 -0
  51. data/lib/solidruby/assemblies/gear.rb +131 -0
  52. data/lib/solidruby/assemblies/linear_bearing.rb +84 -0
  53. data/lib/solidruby/assemblies/nut.rb +153 -0
  54. data/lib/solidruby/assemblies/pipe.rb +153 -0
  55. data/lib/solidruby/assemblies/ruler.rb +44 -0
  56. data/lib/solidruby/assemblies/tslot.rb +141 -0
  57. data/lib/solidruby/assemblies/tslot_machining.rb +62 -0
  58. data/lib/solidruby/assemblies/washer.rb +57 -0
  59. data/lib/solidruby/bill_of_material.rb +43 -0
  60. data/lib/solidruby/csg_modelling/csg_modelling.rb +43 -0
  61. data/lib/solidruby/csg_modelling/difference.rb +41 -0
  62. data/lib/solidruby/csg_modelling/hull.rb +23 -0
  63. data/lib/solidruby/csg_modelling/intersection.rb +24 -0
  64. data/lib/solidruby/csg_modelling/minkowski.rb +23 -0
  65. data/lib/solidruby/csg_modelling/union.rb +41 -0
  66. data/lib/solidruby/csg_modifiers/color.rb +41 -0
  67. data/lib/solidruby/csg_modifiers/csg_modifier.rb +50 -0
  68. data/lib/solidruby/csg_modifiers/linear_extrude.rb +34 -0
  69. data/lib/solidruby/csg_modifiers/projection.rb +29 -0
  70. data/lib/solidruby/csg_modifiers/rotate_extrude.rb +30 -0
  71. data/lib/solidruby/extra.rb +58 -0
  72. data/lib/solidruby/helpers/chamfer.rb +27 -0
  73. data/lib/solidruby/helpers/fillet.rb +28 -0
  74. data/lib/solidruby/helpers/position.rb +267 -0
  75. data/lib/solidruby/helpers/rounded_cube.rb +37 -0
  76. data/lib/solidruby/helpers/triangle.rb +167 -0
  77. data/lib/solidruby/primitives/circle.rb +26 -0
  78. data/lib/solidruby/primitives/cube.rb +123 -0
  79. data/lib/solidruby/primitives/cylinder.rb +72 -0
  80. data/lib/solidruby/primitives/import.rb +42 -0
  81. data/lib/solidruby/primitives/polygon.rb +26 -0
  82. data/lib/solidruby/primitives/polyhedron.rb +30 -0
  83. data/lib/solidruby/primitives/primitive.rb +19 -0
  84. data/lib/solidruby/primitives/render.rb +48 -0
  85. data/lib/solidruby/primitives/sphere.rb +49 -0
  86. data/lib/solidruby/primitives/square.rb +88 -0
  87. data/lib/solidruby/primitives/surface.rb +44 -0
  88. data/lib/solidruby/primitives/text.rb +43 -0
  89. data/lib/solidruby/printed_thread.rb +163 -0
  90. data/lib/solidruby/rubyscad_bridge.rb +376 -0
  91. data/lib/solidruby/screw_thread.rb +170 -0
  92. data/lib/solidruby/solidruby.rb +150 -0
  93. data/lib/solidruby/solidruby_object.rb +176 -0
  94. data/lib/solidruby/transformations/mirror.rb +22 -0
  95. data/lib/solidruby/transformations/rotate.rb +22 -0
  96. data/lib/solidruby/transformations/scale.rb +22 -0
  97. data/lib/solidruby/transformations/transformation.rb +29 -0
  98. data/lib/solidruby/transformations/translate.rb +32 -0
  99. data/lib/solidruby/version.rb +18 -0
  100. data/lib/templates/assembly.tt +13 -0
  101. data/lib/templates/gitignore.tt +3 -0
  102. data/lib/templates/guardfile.tt +4 -0
  103. data/lib/templates/main.tt +20 -0
  104. data/lib/templates/params.tt +3 -0
  105. data/lib/templates/printed.tt +9 -0
  106. data/manual/manual.html +417 -0
  107. data/solidruby.gemspec +35 -0
  108. metadata +277 -0
@@ -0,0 +1,150 @@
1
+ # This file is part of SolidRuby.
2
+ #
3
+ # SolidRuby is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # SolidRuby is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with SolidRuby. If not, see <http://www.gnu.org/licenses/>fre.
15
+
16
+ $fn = 64
17
+
18
+ module SolidRuby
19
+ include SolidRuby::BillOfMaterial
20
+ include SolidRuby::ScrewThreads
21
+ include SolidRuby::PrintedThreads
22
+ include SolidRuby::Extras
23
+ include SolidRuby::Helpers
24
+ include SolidRuby::Primitives
25
+ include SolidRuby::CSGModelling
26
+ include SolidRuby::CSGModifiers
27
+ include SolidRuby::Assemblies
28
+ include SolidRuby::Transformations
29
+ include Math
30
+
31
+ # Deprecated: Stacks parts along the Z axis
32
+ # works on all Assemblies that have a @height definition
33
+ # TODO: Make a better functionality similar to this, that is:
34
+ # - easier to use
35
+ # - throws better error messages
36
+ # - doesn't assume that everything falls down like gravity in every case
37
+ def stack(args = {}, *parts)
38
+ args[:method] ||= 'show'
39
+ args[:spacing] ||= 0
40
+ warn 'SolidRuby Warning: Please note that the stack method is deprecated and will be removed or replaced in the future'
41
+ @assembly = nil
42
+ z = 0
43
+ parts.each do |part|
44
+ item = (part.send args[:method])
45
+ next if item.nil? || !item.respond_to?('translate')
46
+ @assembly += item.translate(z: z)
47
+ z += part.height + args[:spacing]
48
+ end
49
+ @assembly
50
+ end
51
+
52
+ def get_position_rec(obj, level = 0)
53
+ position = [0, 0, 0]
54
+ return position if obj.nil?
55
+ obj.each do |o|
56
+ o.transformations.each do |t|
57
+ next unless t.class == Translate
58
+ t.args[:x] ||= 0
59
+ t.args[:y] ||= 0
60
+ t.args[:z] ||= 0
61
+ position[0] += t.args[:x]
62
+ position[1] += t.args[:y]
63
+ position[2] += t.args[:z]
64
+ end
65
+ # puts " " * level + position.inspect
66
+ x, y, z = get_position_rec(o.children, level + 1)
67
+ position[0] += x
68
+ position[1] += y
69
+ position[2] += z
70
+ end
71
+ position
72
+ end
73
+
74
+ # this is experimental, does only work on simple parts. example:
75
+ # The bolt head is on the -z plane, this will move it to "zero"
76
+ def position(obj)
77
+ get_position_rec(obj.children)
78
+ end
79
+
80
+ # produces a hull() of 2 cylidners
81
+ # accepts d,r,h for cylinder options
82
+ # l long slot length
83
+ def long_slot(args)
84
+ 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]))
85
+ end
86
+
87
+ def radians(a)
88
+ a / 180.0 * Math::PI
89
+ end
90
+
91
+ def degrees(a)
92
+ a * 180 / Math::PI
93
+ end
94
+
95
+ def save!
96
+ Dir.glob('lib/**/*.rb').map { |l| get_classes_from_file(l) }.flatten.map { |l| save_all(l) }
97
+ end
98
+
99
+ # Saves all files generated of a SolidRuby file
100
+ # Saves outputs of
101
+ # - show
102
+ # - output
103
+ # - view*
104
+ def save_all(class_name, fn = $fn)
105
+ res = class_name.send :new
106
+
107
+ # skip defined classes
108
+ skip = class_name.send :get_skip
109
+ skip = [] if skip.nil?
110
+ skip << 'show_hardware'
111
+ added_views = class_name.send :get_views
112
+
113
+ # regexp for output* view* show*
114
+ (res.methods.grep(Regexp.union(/^output/, /^view/, /^show/)) + added_views).each do |i|
115
+ next if skip.include? i.to_s
116
+ output = nil
117
+
118
+ res.send :initialize # ensure default values are loaded at each interation
119
+ output = res.send i
120
+
121
+ # if previous call resulted in a SolidRubyObject, don't call the show method again,
122
+ # otherwise call it.
123
+ unless output.is_a? SolidRubyObject
124
+ output = if i.to_s.include? 'output'
125
+ res.output
126
+ else
127
+ res.show
128
+ end
129
+ end
130
+
131
+ output.save("output/#{res.class}_#{i}.scad", "$fn=#{fn};") unless output.nil?
132
+ end
133
+ end
134
+
135
+ def get_classes_from_file(filename)
136
+ classes = []
137
+ File.readlines(filename).find_all { |l| l.include?('class') }.each do |line|
138
+ # strip all spaces, tabs
139
+ line.strip!
140
+ # ignore comments (Warning: will not worth with ruby multi line comments)
141
+ next if line[0..0] == '#'
142
+ # strip class definition
143
+ line = line[6..-1]
144
+ # strip until space appears - or if not, to the end
145
+ classes << Object.const_get(line[0..line.index(' ').to_i - 1])
146
+ end
147
+
148
+ classes
149
+ end
150
+ end
@@ -0,0 +1,176 @@
1
+ # This file is part of SolidRuby.
2
+ #
3
+ # SolidRuby is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # SolidRuby is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with SolidRuby. If not, see <http://www.gnu.org/licenses/>.
15
+
16
+ module SolidRuby
17
+ class SolidRubyObject
18
+ attr_accessor :attributes
19
+ attr_accessor :transformations
20
+ attr_accessor :children
21
+ attr_accessor :siblings
22
+
23
+ def initialize(*attributes)
24
+ @transformations = []
25
+ @siblings = []
26
+ @attributes = attributes.flatten
27
+ @attributes = @attributes[0] if @attributes[0].is_a? Hash
28
+ @debug_obj = false
29
+
30
+ @@attr_aliases ||= {}
31
+ @@attr_aliases[self.class.name] ||= {}
32
+ @@attr_aliases[self.class.name].each do |k, v|
33
+ @attributes[v] ||= @attributes.delete(k) unless @attributes[k].nil?
34
+ end
35
+ end
36
+
37
+ def rotate(args)
38
+ # always make sure we have a z parameter; otherwise RubyScad will produce a 2-dimensional output
39
+ # which can result in openscad weirdness
40
+ args[:z] = 0 if args[:z].nil?
41
+ @transformations ||= []
42
+ @transformations << Rotate.new(args)
43
+ self
44
+ end
45
+
46
+ def rotate_around(point, args)
47
+ x = point.x
48
+ y = point.y
49
+ z = point.z
50
+ translate(x: -x, y: -y, z: -z).rotate(args).translate(x: x, y: y, z: z)
51
+ end
52
+
53
+ def translate(args)
54
+ return self if (args[:x] || 0) == 0 && (args[:y] || 0) == 0 && (args[:z] || 0) == 0
55
+ @transformations ||= []
56
+ @transformations << Translate.new(args)
57
+ self
58
+ end
59
+
60
+ def union(args)
61
+ @transformations ||= []
62
+ @transformations << Union.new(args)
63
+ self
64
+ end
65
+
66
+ def mirror(args)
67
+ @transformations ||= []
68
+ @transformations << Mirror.new(args)
69
+ self
70
+ end
71
+
72
+ def scale(args)
73
+ args = { v: args } if args.is_a?(Numeric) || args.is_a?(Array)
74
+ @transformations ||= []
75
+ @transformations << Scale.new(args)
76
+ self
77
+ end
78
+
79
+ def place(args={})
80
+ return self if args.nil? || self.nil? || args[:onto].nil?
81
+
82
+ onto = args[:onto]
83
+ face = args[:face] || :top
84
+ edge = args[:edge] || :center
85
+ corner = args[:corner] || :center
86
+ face_offset = args[:face_offset] || 0
87
+ edge_offset = args[:edge_offset] || 0
88
+ corner_offset = args[:corner_offset] || 0
89
+
90
+ if onto.respond_to? :get_point_on
91
+ move_to = onto.get_point_on(
92
+ face: face,
93
+ edge: edge,
94
+ corner: corner,
95
+ face_offset: face_offset,
96
+ edge_offset: edge_offset,
97
+ corner_offset: corner_offset
98
+ )
99
+
100
+ if (self.respond_to? :get_point_on)
101
+ move_me = self.get_point_on(face: :center, edge: :center, corner: :center)
102
+ move_to[:x] -= move_me[:x]
103
+ move_to[:y] -= move_me[:y]
104
+ move_to[:z] -= move_me[:z]
105
+ end
106
+
107
+ self.translate(move_to)
108
+ end
109
+
110
+ self
111
+ end
112
+
113
+ def &(obj)
114
+ @siblings << obj
115
+ self
116
+ end
117
+
118
+ def debug
119
+ @debug_obj = true
120
+ self
121
+ end
122
+
123
+ def debug?
124
+ @debug_obj
125
+ end
126
+
127
+ def walk_tree
128
+ res = ''
129
+
130
+ @transformations.reverse.each do |trans|
131
+ res += trans.walk_tree
132
+ end
133
+ res += '#' if self.debug?
134
+ res += to_rubyscad.to_s + "\n"
135
+ @siblings.each do |s|
136
+ res += s.walk_tree
137
+ end
138
+ res
139
+ end
140
+
141
+ alias scad_output walk_tree
142
+
143
+ def walk_tree_classes
144
+ res = []
145
+ @transformations.reverse.each do |trans|
146
+ res += trans.walk_tree_classes
147
+ end
148
+ res << self.class
149
+ res
150
+ end
151
+
152
+ def to_rubyscad
153
+ ''
154
+ end
155
+
156
+ def save(filename, start_text = nil)
157
+ file = File.open(filename, 'w')
158
+ file.puts start_text unless start_text.nil?
159
+ file.puts scad_output
160
+ file.close
161
+ end
162
+
163
+ def self.alias_attr(long, short=nil)
164
+ short ||= long[0].downcase.to_sym
165
+ @@attr_aliases ||= {}
166
+ @@attr_aliases[self.name] ||= {}
167
+ @@attr_aliases[self.name][short] = long
168
+
169
+ self.class_eval {
170
+ define_method long do
171
+ @attributes[long]
172
+ end
173
+ }
174
+ end
175
+ end
176
+ end
@@ -0,0 +1,22 @@
1
+ # This file is part of SolidRuby.
2
+ #
3
+ # SolidRuby is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # SolidRuby is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with SolidRuby. If not, see <http://www.gnu.org/licenses/>.
15
+ #
16
+ module SolidRuby::Transformations
17
+ class Mirror < Transformation
18
+ def to_rubyscad
19
+ RubyScadBridge.new.mirror(@args)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ # This file is part of SolidRuby.
2
+ #
3
+ # SolidRuby is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # SolidRuby is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with SolidRuby. If not, see <http://www.gnu.org/licenses/>.
15
+ #
16
+ module SolidRuby::Transformations
17
+ class Rotate < Transformation
18
+ def to_rubyscad
19
+ RubyScadBridge.new.rotate(@args).delete('"')
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ # This file is part of SolidRuby.
2
+ #
3
+ # SolidRuby is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # SolidRuby is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with SolidRuby. If not, see <http://www.gnu.org/licenses/>.
15
+ #
16
+ module SolidRuby::Transformations
17
+ class Scale < Transformation
18
+ def to_rubyscad
19
+ RubyScadBridge.new.scale(@args)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,29 @@
1
+ # This file is part of SolidRuby.
2
+ #
3
+ # SolidRuby is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # SolidRuby is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with SolidRuby. If not, see <http://www.gnu.org/licenses/>.
15
+ #
16
+ module SolidRuby::Transformations
17
+ class Transformation
18
+ attr_accessor :args
19
+ def initialize(*args)
20
+ @args = args.flatten
21
+ @args = @args[0] if @args[0].is_a? Hash
22
+ end
23
+
24
+ def walk_tree
25
+ to_rubyscad.to_s + "\n"
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,32 @@
1
+ # This file is part of SolidRuby.
2
+ #
3
+ # SolidRuby is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # SolidRuby is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with SolidRuby. If not, see <http://www.gnu.org/licenses/>.
15
+ #
16
+ module SolidRuby::Transformations
17
+ attr_accessor :x, :y, :z
18
+ class Translate < Transformation
19
+ def initialize(args={})
20
+ super(args)
21
+ @x = args[:x] || 0
22
+ @y = args[:y] || 0
23
+ @z = args[:z] || 0
24
+ end
25
+
26
+ def to_rubyscad
27
+ #ignore empty transformations
28
+ return '' if @x == 0 && @y == 0 && @z == 0
29
+ RubyScadBridge.new.translate({x: @x, y: @y, z: @z}).delete('"')
30
+ end
31
+ end
32
+ end