-
Notifications
You must be signed in to change notification settings - Fork 1
/
tape_measure_tool.rb
291 lines (238 loc) · 7.45 KB
/
tape_measure_tool.rb
1
2
3
4
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
34
35
36
37
38
39
40
41
42
43
44
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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# frozen_string_literal: true
module Eneroth
module ScaledTapeMeasure
Sketchup.require "#{PLUGIN_ROOT}/tool"
Sketchup.require "#{PLUGIN_ROOT}/vendor/scale"
Sketchup.require "#{PLUGIN_ROOT}/vendor/refined_input_point"
Sketchup.require "#{PLUGIN_ROOT}/vendor/inference_lock"
using RefinedInputPoint
# Tool for measuring length with respect to custom scale.
class TapeMeasureTool < Tool
include InferenceLock
# Identifier of tool state for picking start point.
STATE_START = 0
# Identifier for tool state for picking end point.
STATE_MEASURE = 1
# Size of arrow head.
ARROW_HEAD_SIZE = 8
# ID for tool cursor.
CURSOR = UI.create_cursor("#{PLUGIN_ROOT}/images/cursor.svg", 6, 24)
# Scale to measure with respect to.
@@scale ||= Scale.new("1:1")
# @api
# @see https://ruby.sketchup.com/Sketchup/Tool.html
def activate
super
@state = STATE_START
@start_ip = Sketchup::InputPoint.new
@end_ip = Sketchup::InputPoint.new
update_status_text
end
# @api
# @see https://ruby.sketchup.com/Sketchup/Tool.html
def deactivate(view)
super
reset(view)
view.invalidate
end
# @api
# @see https://ruby.sketchup.com/Sketchup/Tool.html
def draw(view)
case @state
when STATE_START
view.tooltip = start_tooltip
when STATE_MEASURE
view.tooltip = output
# In native Tape Measure this line is infinite, but as this tool
# doesn't create GuidLines a line between the arrow end and InputPoint
# is more descriptive.
view.line_stipple = "_"
view.draw(GL_LINES, [@end_ip.position, measure_end])
view.line_stipple = ""
view.line_width = view.inference_locked? ? 3 : 1
draw_arrow(@start_ip.position, measure_end, view)
end
view.line_width = 1
@start_ip.draw(view)
@end_ip.draw(view)
end
# @api
# @see https://ruby.sketchup.com/Sketchup/Tool.html
def enableVCB?
@state == STATE_START
end
# @api
# @see https://ruby.sketchup.com/Sketchup/Tool.html
def getExtents
bb = Sketchup.active_model.bounds
bb.add(@start_ip.position)
bb.add(@end_ip.position)
bb.add(measure_end) if @end_ip.valid?
bb
end
# @see http://ruby.sketchup.com/Sketchup/Tool.html
def getInstructorContentDirectory
"#{PLUGIN_ROOT}/instructor/#{OB.lang}.html"
end
# @api
# @see https://ruby.sketchup.com/Sketchup/Tool.html
def onCancel(_reason, view)
reset(view)
view.invalidate
end
# @api
# @see https://ruby.sketchup.com/Sketchup/Tool.html
def onSetCursor
UI.set_cursor(CURSOR)
end
# @api
# @see https://ruby.sketchup.com/Sketchup/Tool.html
def onLButtonDown(_flags, _x, _y, view)
case @state
when STATE_START
@state = STATE_MEASURE
when STATE_MEASURE
reset(view)
view.invalidate
end
update_status_text
end
# @api
# @see https://ruby.sketchup.com/Sketchup/Tool.html
def onMouseMove(_flags, x, y, view)
super
case @state
when STATE_START
@start_ip.pick(view, x, y)
when STATE_MEASURE
@end_ip.pick(view, x, y)
Sketchup.vcb_value = output
end
view.invalidate
end
# @api
# @see https://ruby.sketchup.com/Sketchup/Tool.html
def onUserText(text, _view)
scale = Scale.new(text)
unless scale.valid?
UI.messagebox(OB["invalid_scale"])
return
end
@@scale = scale
end
# @api
# @see https://ruby.sketchup.com/Sketchup/Tool.html
def resume(view)
view.invalidate
update_status_text
end
# @api
# @see https://ruby.sketchup.com/Sketchup/Tool.html
def suspend(view)
view.invalidate
end
# @api
# @see https://extensions.sketchup.com/en/content/eneroth-tool-memory
def ene_tool_cycler_name
OB["tool_name"]
end
# @api
# @see https://extensions.sketchup.com/en/content/eneroth-tool-memory
def ene_tool_cycler_icon
File.join(PLUGIN_ROOT, "images", "icon.svg")
end
private
# @api
# @see `ToolInference`
def current_ip
@state == STATE_START ? @start_ip : @end_ip
end
# @api
# @see `ToolInference`
def start_ip
@start_ip
end
def draw_arrow(point1, point2, view)
return if point1 == point2
view.set_color_from_line(point1, point2)
view.draw(GL_LINES, [point1, point2])
draw_arrow_head(point1, measure_vector.reverse, view)
draw_arrow_head(point2, measure_vector, view)
end
def draw_arrow_head(tip, direction, view)
cam = view.camera
cam_v = cam.perspective? ? tip - cam.eye : cam.direction
perp_v = direction.normalize * cam_v
flattened = cam_v * perp_v
offset = view.pixels_to_model(ARROW_HEAD_SIZE, tip)
view.draw(GL_LINE_STRIP, [
tip.offset(Geom.linear_combination(1, perp_v, -1, flattened), offset),
tip,
tip.offset(Geom.linear_combination(-1, perp_v, -1, flattened), offset)
])
end
# @return [Length]
def length
(measure_vector.length * @@scale.factor).to_l
end
def measure_end
if @start_ip.degrees_of_freedom == 1 && @start_ip.freedom_constraint
plane = [@start_ip.position, @start_ip.freedom_constraint]
return @end_ip.position.project_to_plane(plane)
end
@end_ip.position
end
# @return [Geom::Vector3d]
def measure_vector
measure_end - @start_ip.position
end
def reset(view)
@end_ip.clear
@start_ip.clear
view.lock_inference
@state = STATE_START
end
# @return [String]
def output
return "" unless @end_ip.valid?
"#{length} (#{@@scale})"
end
# @return [String]
def start_tooltip
"#{@start_ip.tooltip}\n#{hover_info(@start_ip)}"
end
# @return [Length, nil]
def hovered_edge_length(ip)
edge = ip.source_edge
return unless edge
vector = edge.end.position - edge.start.position
vector.transform(ip.transformation).length
end
# @return [String]
def hover_info(ip)
edge_l = hovered_edge_length(ip)
return "#{(edge_l * @@scale.factor).to_l} (#{@@scale})" if edge_l
return unless ip.instance
return unless ip.instance.respond_to?(:definition)
bb = ip.instance.definition.bounds
# #height refers to depth and #depth to height in API.
"#{(bb.width * @@scale.factor).to_l} x "\
"#{(bb.height * @@scale.factor).to_l} x "\
"#{(bb.depth * @@scale.factor).to_l} x "\
"(#{@@scale})"
end
def update_status_text
case @state
when STATE_START
Sketchup.status_text = OB["status_start"]
Sketchup.vcb_label = OB["label_start"]
Sketchup.vcb_value = @@scale.to_s
when STATE_MEASURE
Sketchup.status_text = OB["status_measure"]
Sketchup.vcb_label = OB["label_measure"]
Sketchup.vcb_value = output
end
end
end
end
end