Class: JennCad::Primitives::BooleanObject

Inherits:
Primitive show all
Defined in:
lib/jenncad/primitives/boolean_object.rb

Instance Attribute Summary

Attributes inherited from Primitive

#dimensions

Attributes inherited from Thing

#anchors, #angle, #calc_h, #calc_x, #calc_y, #calc_z, #csize, #diameter, #fn, #name, #opts, #parent, #parts, #pos, #shape, #sits_on, #transformations, #x, #y

Instance Method Summary collapse

Methods inherited from Primitive

#feed_opts, #handle_diameter, #handle_margins

Methods inherited from Thing

#anchor, #at, #auto_color, #auto_color!, #auto_extrude, #calculate_center_rotation, #calculated_h, #children_list, #color, #color_or_fallback, #color_parse, #copy_anchor, #copy_anchors, #cut_to, #dbg, #debug?, #find_calculated_h, #fixate, #flip, #flipc, #get_children, #get_contents, #ghost, #has_explicit_color?, #hide, #hl, #inherit_color, #init, #is_2d?, #is_3d?, #mhx, #mhy, #mhz, #mirror, #mix, #miy, #miz, #modify_values, #modify_values!, #move, #movea, #moveai, #moveh, #movei, #multmatrix, #mx, #my, #mz, #on_top_of, #only, #only_color?, #openscad, #openscad_modifier, #option, #parse_xyz_shortcuts, #radians, #referenced_z, #reset, #reset_last_move, #rotate, #rotate_around, #rx, #ry, #rz, #scale, #set_anchor, #set_anchor_from, #set_auto_color, #set_auto_color_for_children, #set_flag, #set_heights_for_auto_extrude, #set_option, #set_parent, #skew, #to_mod, #top_of, #transform, #unset_flag, #z, #z=, #z_margin

Constructor Details

#initialize(*parts) ⇒ BooleanObject

Returns a new instance of BooleanObject.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/jenncad/primitives/boolean_object.rb', line 3

def initialize(*parts)
  @transformations = []
  if parts.first.kind_of? Array
    @parts = parts.first
  else
    @parts = parts
  end

  if @parts.first.respond_to? :anchors
    @anchors = @parts.first.anchors
  end

  if parts.first && parts.first.respond_to?(:debug?) && parts.first.debug?
    $log.debug("Creating new #{self.class} for part #{parts.pretty_inspect}")
  end


  @parent = @parts.first.parent if @parts.first
  @csize = @parts.first.csize if @parts.first
  @csize ||= Size.new

  after_add
end

Instance Method Details

#add(part) ⇒ Object



39
40
41
42
43
# File 'lib/jenncad/primitives/boolean_object.rb', line 39

def add(part)
  return if part.nil?
  @parts << part
  after_add
end

#add_or_new(part) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jenncad/primitives/boolean_object.rb', line 27

def add_or_new(part)
  case @transformations
  when nil, []
    $log.debug("adding new part to existing boolean object") if part && part.debug?
    add(part)
    self
  else
    $log.debug("add_or_new: creating new boolean object") if part.debug?
    self.class.new(self, part)
  end
end

#after_addObject



45
46
47
48
49
50
51
# File 'lib/jenncad/primitives/boolean_object.rb', line 45

def after_add
  @parts.flatten!
  @parts.compact!
  inherit_debug
  inherit_z
  inherit_zref
end

#get_primitives(obj) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/jenncad/primitives/boolean_object.rb', line 84

def get_primitives(obj)
  res = []
  if obj.kind_of? Array
    obj.each do |part|
      res << part.children_list
    end
  else
    res << obj.children_list
  end
  res
end

#inherit_debugObject



53
54
55
56
57
# File 'lib/jenncad/primitives/boolean_object.rb', line 53

def inherit_debug
  if @parts.map{|l| l.option(:debug)}.include? true
    set_option(:debug, true)
  end
end

#inherit_zObject



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

def inherit_z
  heights = @parts.map{|l| l.calc_z.to_d}.uniq
  if heights.size > 1
    total_heights = []
    @parts.each do |p|
      total_heights << p.z.to_d + p.calc_z.to_d
    end
    @z = total_heights.max
    @calc_z = heights.min
  else
    @calc_z = heights.first.to_d
    @z = @parts.map(&:z).compact.max
  end
end

#inherit_zrefObject



74
75
76
77
78
79
80
81
82
# File 'lib/jenncad/primitives/boolean_object.rb', line 74

def inherit_zref
  return if @parts.first == nil
  #return if @parts.first.z.to_d == 0.0
  get_primitives(@parts[1..-1]).flatten.each do |part|
    if part.z.to_d == 0.0
      part.set_option :zref, @parts.first
    end
  end
end

#only_additives_of(obj) ⇒ Object



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

def only_additives_of(obj)
  res = []
  case obj
  when Array
    res << obj.map{|l| only_additives_of(l)}
  when SubtractObject
    # include the thing that something was subtracted from to get the Z height if that is behind another layer of SubtractObject
    res << only_additives_of(obj.parts.first)
  when IntersectionObject
  else
    res << obj
  end
  res.flatten
end