Class: JennCad::Primitives::Square

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

Direct Known Subclasses

Cube

Instance Attribute Summary collapse

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(args) ⇒ Square

Returns a new instance of Square.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/jenncad/primitives/square.rb', line 5

def initialize(args)
  @opts = {
    x: 0,
    y: 0,
    margins: {
      x: 0,
      y: 0,
    },
    center: true,
    center_y: false,
    center_x: false,
  }
  if args.kind_of? Array
    args.each do |a|
      feed_opts(parse_xyz_shortcuts(a))
    end
  else
    feed_opts(parse_xyz_shortcuts(args))
  end
  init(args)

  handle_margins
  set_anchors
  @x = args[:x]
  @y = args[:y]
  @dimensions = [:x, :y]
  @sits_on = :bottom
  @csize = Size.new(x: @opts[:x], y: @opts[:y])
end

Instance Attribute Details

#cornersObject

Returns the value of attribute corners.



3
4
5
# File 'lib/jenncad/primitives/square.rb', line 3

def corners
  @corners
end

#sidesObject

Returns the value of attribute sides.



3
4
5
# File 'lib/jenncad/primitives/square.rb', line 3

def sides
  @sides
end

Instance Method Details

#centered_axisObject



165
166
167
168
169
170
171
172
# File 'lib/jenncad/primitives/square.rb', line 165

def centered_axis
  return [:x, :y] if @opts[:center]
  a = []
  a << :x if @opts[:center_x]
  a << :y if @opts[:center_y]
  a << :z if @opts[:center_z]
  a
end

#cxObject Also known as: center_x



149
150
151
152
153
154
# File 'lib/jenncad/primitives/square.rb', line 149

def cx
  nc
  @opts[:center_x] = true
  set_anchors
  self
end

#cyObject Also known as: center_y



157
158
159
160
161
162
# File 'lib/jenncad/primitives/square.rb', line 157

def cy
  nc
  @opts[:center_y] = true
  set_anchors
  self
end

#inner_anchors(dist, prefix = :inner_, recreate = false) ⇒ Object



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
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/jenncad/primitives/square.rb', line 88

def inner_anchors(dist, prefix=:inner_, recreate=false)
  if dist.nil?
    $log.error "Distance of nil passed to inner anchors. Please check the variable name you passed along"
    return self
  end

  @inner_anchor_defs ||= []
  @inner_anchor_defs << { "dist": dist, "prefix": prefix } unless recreate

#      $log.info "dist: #{dist}, prefix: #{prefix}"
  sides = {
    left: {x: dist, y: 0},
    right: {x: -dist, y: 0},
    top: {x: 0, y: -dist},
    bottom: {x: 0, y: dist},
  }
  corners = {
    top_left: {x: dist, y: -dist},
    top_right: {x: -dist, y: -dist},
    bottom_left: {x: dist, y: dist},
    bottom_right: {x: -dist, y: dist},
  }
  new_sides = []
  new_corners = []

  sides.merge(corners).each do |key, vals|
    new_dist = anchor(key).dup
    new_dist[:x] += vals[:x]
    new_dist[:y] += vals[:y]
    name = [prefix, key].join.to_sym
#        $log.info "Set anchor #{name} , new dist #{new_dist}"
    set_anchor name, new_dist
    if sides.include? key
      new_sides << name
    end
    if corners.include? key
      new_corners << name
    end
  end

  sides_name = [prefix, "sides"].join
  corners_name = [prefix, "corners"].join
  all_name = [prefix, "all"].join
  self.class.__send__(:attr_accessor, sides_name.to_sym)
  self.class.__send__(:attr_accessor, corners_name.to_sym)
  self.class.__send__(:attr_accessor, all_name.to_sym)
  self.__send__("#{sides_name}=", new_sides)
  self.__send__("#{corners_name}=", new_corners)
  self.__send__("#{all_name}=", new_corners+new_sides)


  self
end

#not_centeredObject Also known as: nc



142
143
144
145
146
# File 'lib/jenncad/primitives/square.rb', line 142

def not_centered
  @opts[:center] = false
  set_anchors
  self
end

#set_anchorsObject



41
42
43
# File 'lib/jenncad/primitives/square.rb', line 41

def set_anchors
  set_anchors_2d
end

#set_anchors_2dObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/jenncad/primitives/square.rb', line 45

def set_anchors_2d
  @anchors = {} # this resets anchors
  @corners = [:top_right, :top_left, :bottom_right, :bottom_left]
  @sides = [:left, :right, :top, :bottom]

  if @opts[:center] || @opts[:center_x]
    left = -@opts[:x] / 2.0
    right = @opts[:x] / 2.0
    mid_x = 0
  else
    left = 0
    right =  @opts[:x]
    mid_x = @opts[:x] / 2.0
  end
  if @opts[:center] || @opts[:center_y]
    bottom = -@opts[:y] / 2.0
    top = @opts[:y] / 2.0
    mid_y = 0
  else
    bottom = 0
    top = @opts[:y]
    mid_y = @opts[:y] / 2.0
  end

  set_anchor :left, x: left, y: mid_y
  set_anchor :right, x: right, y: mid_y
  set_anchor :top, x: mid_x, y: top
  set_anchor :bottom, x: mid_x, y: bottom
  set_anchor :top_left, x: left, y: top
  set_anchor :top_right, x: right, y: top
  set_anchor :bottom_left, x: left, y: bottom
  set_anchor :bottom_right, x: right, y: bottom

  # we need to re-do the inner ones, if they were defined
  if @inner_anchor_defs && @inner_anchor_defs.size > 0
    @inner_anchor_defs.each do |anch|
     inner_anchors(anch[:dist], anch[:prefix], true)
    end
  end

  self
end

#sizeObject

used for openscad export



37
38
39
# File 'lib/jenncad/primitives/square.rb', line 37

def size
  [@x, @y]
end

#to_openscadObject



174
175
176
# File 'lib/jenncad/primitives/square.rb', line 174

def to_openscad
  self.mh(centered_axis.to_h{|a| [a, -@opts[a]] }.merge(prepend: true)) # center cube
end