crystalscad 0.2.4 → 0.2.5

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.
data/examples/stack.rb ADDED
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/ruby1.9.3
2
+ require "rubygems"
3
+ require "crystalscad"
4
+ include CrystalScad
5
+
6
+
7
+ parts = [
8
+ Washer.new(4.3),
9
+ Nut.new(4),
10
+ Washer.new(4.3),
11
+ Nut.new(4)
12
+ ]
13
+ bolt = Bolt.new(4,16).show
14
+ bolt_assembly = bolt
15
+ bolt_assembly += stack({method:"output"}, *parts)
16
+
17
+ x,y,z = position(bolt)
18
+ bolt_assembly.translate(x:x*-1,y:y*-1,z:z*-1)
19
+
20
+ puts bolt_assembly.scad_output
21
+
22
+
@@ -1,5 +1,7 @@
1
1
  module CrystalScad
2
2
  class Assembly
3
+ attr_accessor :height
4
+
3
5
  def initialize(args={})
4
6
  @args = args if @args == nil
5
7
  @@bom.add(description) unless args[:no_bom] == true
@@ -22,7 +22,7 @@ module CrystalScad
22
22
  include CrystalScad::LinearBearing
23
23
 
24
24
 
25
- class ScadObject
25
+ class CrystalScadObject
26
26
  attr_accessor :args
27
27
  attr_accessor :transformations
28
28
  def initialize(*args)
@@ -51,7 +51,8 @@ module CrystalScad
51
51
 
52
52
  end
53
53
 
54
- class Primitive < ScadObject
54
+ class Primitive < CrystalScadObject
55
+ attr_accessor :children
55
56
 
56
57
  def rotate(args)
57
58
  # always make sure we have a z parameter; otherwise RubyScad will produce a 2-dimensional output
@@ -80,21 +81,7 @@ module CrystalScad
80
81
 
81
82
  end
82
83
 
83
- class TransformedObject < Primitive
84
- attr_accessor :scad
85
- def initialize(string)
86
- @scad = string
87
- @transformations = []
88
- end
89
-
90
- def to_rubyscad
91
- return @scad
92
- end
93
- alias :output :walk_tree
94
-
95
- end
96
-
97
- class Transformation < ScadObject
84
+ class Transformation < CrystalScadObject
98
85
  end
99
86
 
100
87
  class Rotate < Transformation
@@ -132,7 +119,6 @@ module CrystalScad
132
119
  def initialize(*args)
133
120
  super(args)
134
121
  @x,@y,@z = args[0][:size].map{|l| l.to_f}
135
-
136
122
  end
137
123
 
138
124
  def to_rubyscad
@@ -195,48 +181,177 @@ module CrystalScad
195
181
  end
196
182
  end
197
183
 
198
-
199
- def csg_operation(operation, code1, code2)
200
- ret = "#{operation}(){"
201
- ret +=code1
202
- ret +=code2
203
- ret +="}"
204
- return TransformedObject.new(ret)
184
+
185
+ class CSGModelling < Primitive
186
+ def initialize(*list)
187
+ @transformations = []
188
+ @children = list
189
+ end
190
+
191
+ def to_rubyscad
192
+ @children ||= []
193
+ ret = "#{@operation}(){"
194
+ @children.each do |child|
195
+ begin
196
+ ret +=child.walk_tree
197
+ rescue NoMethodError
198
+ end
199
+ end
200
+ ret +="}"
201
+ end
202
+ end
203
+
204
+ class Union < CSGModelling
205
+ def initialize(*list)
206
+ @operation = "union"
207
+ super(*list)
208
+ end
205
209
  end
206
210
 
211
+ class Difference < CSGModelling
212
+ def initialize(*list)
213
+ @operation = "difference"
214
+ super(*list)
215
+ end
216
+ end
217
+
218
+ class Intersection < CSGModelling
219
+ def initialize(*list)
220
+ @operation = "intersection"
221
+ super(*list)
222
+ end
223
+ end
224
+
225
+ class Hull < CSGModelling
226
+ def initialize(*list)
227
+ @operation = "hull"
228
+ super(*list)
229
+ end
230
+ end
231
+
232
+
207
233
  def +(args)
208
234
  return args if self == nil
209
- csg_operation("union",self.walk_tree,args.walk_tree)
235
+ Union.new(self,args)
210
236
  end
211
237
 
212
238
  def -(args)
213
239
  return args if self == nil
214
- csg_operation("difference",self.walk_tree,args.walk_tree)
240
+ Difference.new(self,args)
215
241
  end
216
242
 
217
243
  def *(args)
218
244
  return args if self == nil
219
- csg_operation("intersection",self.walk_tree,args.walk_tree)
245
+ Intersection.new(self,args)
220
246
  end
221
247
 
222
- def hull(part1,part2)
223
- csg_operation("hull",part1.walk_tree,part2.walk_tree)
248
+ def hull(*parts)
249
+ Hull.new(parts)
250
+ end
251
+
252
+ class CSGModifier < Primitive
253
+ def initialize(object, attributes)
254
+ @transformations = []
255
+ @children = [object]
256
+ @attributes = attributes
257
+ end
258
+
259
+ def to_rubyscad
260
+ ret = "#{@operation}(#{@attributes}){"
261
+ @children ||= []
262
+ @children.each do |child|
263
+ begin
264
+ ret +=child.walk_tree
265
+ rescue NoMethodError
266
+ end
267
+ end
268
+ ret +="}"
269
+ end
270
+ end
271
+
272
+ class Color < CSGModifier
273
+ def initialize(object, attributes)
274
+ @operation = "color"
275
+ if attributes.kind_of? String
276
+ attributes = "\"#{attributes}\""
277
+ elsif attributes.kind_of? Hash
278
+ # FIXME
279
+ end
280
+
281
+ super(object, attributes)
282
+ end
283
+ end
284
+
285
+ class LinearExtrude < CSGModifier
286
+ def initialize(object, attributes)
287
+ @operation = "linear_extrude"
288
+ super(object, attributes)
289
+ end
290
+ end
291
+
292
+ class RotateExtrude < CSGModifier
293
+ def initialize(object, attributes)
294
+ @operation = "rotate_extrude"
295
+ super(object, attributes)
296
+ end
224
297
  end
225
298
 
226
- # Fixme: currently just accepting named colors
227
299
  def color(args)
228
- ret = "color(\"#{args}\"){"
229
- ret +=self.walk_tree
230
- ret +="}"
231
- return TransformedObject.new(ret)
300
+ return Color.new(self,args)
232
301
  end
233
302
 
234
303
  def linear_extrude(args)
235
304
  args = args.collect { |k, v| "#{k} = #{v}" }.join(', ')
236
- ret = "linear_extrude(#{args}){"
237
- ret +=self.walk_tree
238
- ret +="}"
239
- return TransformedObject.new(ret)
305
+ return LinearExtrude.new(self,args)
306
+ end
307
+
308
+ def rotate_extrude(args)
309
+ args = args.collect { |k, v| "#{k} = #{v}" }.join(', ')
310
+ return RotateExtrude.new(self,args)
311
+ end
312
+
313
+
314
+ # Stacks parts along the Z axis
315
+ # works on all Assemblies that have a @height definition
316
+ def stack(args={}, *parts)
317
+ args[:method] ||= "show"
318
+ args[:additional_spacing] ||= 0
319
+ @assembly = nil
320
+ z = 0
321
+ parts.each do |part|
322
+ @assembly += (part.send args[:method]).translate(z:z)
323
+ z+= part.height + args[:additional_spacing]
324
+ end
325
+ @assembly
326
+ end
327
+
328
+ def get_position_rec(obj, level=0)
329
+ position = [0,0,0]
330
+ return position if obj == nil
331
+ obj.each do |o|
332
+ o.transformations.each do |t|
333
+ if t.class == Translate
334
+ t.args[:x] ||= 0
335
+ t.args[:y] ||= 0
336
+ t.args[:z] ||= 0
337
+ position[0] += t.args[:x]
338
+ position[1] += t.args[:y]
339
+ position[2] += t.args[:z]
340
+ end
341
+ end
342
+ # puts " " * level + position.inspect
343
+ x,y,z = get_position_rec(o.children,level+1)
344
+ position[0] += x
345
+ position[1] += y
346
+ position[2] += z
347
+ end
348
+ return position
349
+ end
350
+
351
+ # this is experimental, does only work on simple parts. example:
352
+ # The bolt head is on the -z plane, this will move it to "zero"
353
+ def position(obj)
354
+ get_position_rec(obj.children)
240
355
  end
241
356
 
242
357
  end
@@ -97,7 +97,7 @@ module CrystalScad::Hardware
97
97
  5.3 => {outer_diameter:10, height:1.0},
98
98
 
99
99
  }
100
-
100
+ @height = @chart_din125[@size][:height]
101
101
  super(args)
102
102
  end
103
103
 
@@ -184,8 +184,8 @@ module CrystalScad::Hardware
184
184
  end
185
185
  @@bom.add(description) unless args[:no_bom] == true
186
186
 
187
- return TransformedObject.new(single_profile.output) if @args[:configuration] == 1
188
- return TransformedObject.new(multi_profile.output)
187
+ return single_profile if @args[:configuration] == 1
188
+ return multi_profile
189
189
  end
190
190
 
191
191
  alias :show :output
@@ -232,7 +232,7 @@ module CrystalScad::Hardware
232
232
  alias tslot_output output
233
233
 
234
234
  def output(length=nil)
235
- tslot_output(length)-bolts
235
+ tslot_output(length)-bolts()
236
236
  end
237
237
 
238
238
  def show(length=nil)
@@ -240,7 +240,7 @@ module CrystalScad::Hardware
240
240
  end
241
241
 
242
242
  def bolts
243
- bolt = ScadObject.new
243
+ bolt = CrystalScadObject.new
244
244
  return bolt if @args[:holes] == nil
245
245
 
246
246
  if @args[:holes].include?("front")
@@ -1,4 +1,4 @@
1
1
  module CrystalScad
2
- VERSION = "0.2.4"
2
+ VERSION = "0.2.5"
3
3
  end
4
4
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crystalscad
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 29
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 4
10
- version: 0.2.4
9
+ - 5
10
+ version: 0.2.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Joachim Glauche
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2013-09-13 00:00:00 Z
18
+ date: 2013-09-14 00:00:00 Z
19
19
  dependencies: []
20
20
 
21
21
  description: Inspired by SolidPython, based on RubyScad
@@ -33,6 +33,7 @@ files:
33
33
  - README.md
34
34
  - Rakefile
35
35
  - crystalscad.gemspec
36
+ - examples/stack.rb
36
37
  - lib/crystalscad.rb
37
38
  - lib/crystalscad/Assembly.rb
38
39
  - lib/crystalscad/BillOfMaterial.rb