-
Notifications
You must be signed in to change notification settings - Fork 5
/
Distance check.lua
176 lines (161 loc) · 5.17 KB
/
Distance check.lua
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
sim = require 'sim'
function sysCall_info()
return {autoStart = false, menu = 'Geometry / Mesh\nDistance check'}
end
function sysCall_addOnScriptSuspend()
return {cmd = 'cleanup'}
end
function close_callback()
leaveNow = true
end
function dist(ui, id)
restoreScene()
local entity1 = obj1
local entity2 = obj2
local entity1 = sim.createCollection(0)
local entity2 = sim.createCollection(0)
if obj1IsModel then
sim.addItemToCollection(entity1, sim.handle_tree, obj1, 0)
else
sim.addItemToCollection(entity1, sim.handle_single, obj1, 0)
end
if obj2 ~= -1 then
if obj2IsModel then
sim.addItemToCollection(entity2, sim.handle_tree, obj2, 0)
else
sim.addItemToCollection(entity2, sim.handle_single, obj2, 0)
end
else
sim.addItemToCollection(entity2, sim.handle_all, -1, 0)
if obj1IsModel then
sim.addItemToCollection(entity2, sim.handle_tree, obj1, 1)
else
sim.addItemToCollection(entity2, sim.handle_single, obj1, 1)
end
end
local r, distData, pair = sim.checkDistance(entity1, entity2)
sim.destroyCollection(entity1)
sim.destroyCollection(entity2)
restoreData = {time = sim.getSystemTime()}
if r > 0 then
sim.addLog(
sim.verbosity_scriptinfos,
string.format("measured distance is %.3f meters", distData[7])
)
restoreData.lineContainer = sim.addDrawingObject(sim.drawing_lines, 4, 0, -1, 99, {0, 0, 0})
sim.addDrawingObjectItem(restoreData.lineContainer, distData)
else
sim.addLog(sim.verbosity_scriptinfos, "no distance could be measured")
restoreData = nil
end
end
function sysCall_init()
simUI = require 'simUI'
sim.addLog(
sim.verbosity_scriptinfos,
"This add-on allows to quickly measure the distance between two entities, or between one entity and the environment. Just select one or two entities."
)
obj1 = -1
obj2 = -1
end
function showDlg()
if not ui and sim.isHandle(obj1) then
local pos = 'position="-50,50" placement="relative"'
if uiPos then
pos = 'position="' .. uiPos[1] .. ',' .. uiPos[2] .. '" placement="absolute"'
end
local txt1
if obj2 == -1 then
if obj1IsModel then
txt1 = "Check distance to the environment for model " .. sim.getObjectAlias(obj1, 5)
else
txt1 = "Check distance to the environment for object " ..
sim.getObjectAlias(obj1, 5)
end
else
txt1 = "Check distance between "
if obj1IsModel then
txt1 = txt1 .. "model "
else
txt1 = txt1 .. "object "
end
txt1 = txt1 .. sim.getObjectAlias(obj1, 5) .. " and "
if obj2IsModel then
txt1 = txt1 .. "model "
else
txt1 = txt1 .. "object "
end
txt1 = txt1 .. sim.getObjectAlias(obj2, 5)
end
local xml =
'<ui title="Distance check" activate="false" closeable="true" on-close="close_callback" layout="vbox" ' ..
pos .. '>'
xml = xml .. '<button text="' .. txt1 ..
'" on-click="dist" style="* {min-width: 300px; min-height: 50px;}" id="1"/>'
xml = xml .. '</ui>'
ui = simUI.create(xml)
end
end
function hideDlg()
if ui then
uiPos = {}
uiPos[1], uiPos[2] = simUI.getPosition(ui)
simUI.destroy(ui)
ui = nil
end
obj1 = -1
obj2 = -1
restoreScene()
end
function restoreScene()
if restoreData then
sim.removeDrawingObject(restoreData.lineContainer)
restoreData = nil
end
end
function sysCall_sensing()
return update()
end
function sysCall_nonSimulation()
if leaveNow then return {cmd = 'cleanup'} end
end
function sysCall_selChange(inData)
update()
end
function update()
local s = sim.getObjectSel()
local o1 = -1
local o2 = -1
if #s >= 1 and #s <= 2 then
obj1IsModel = (sim.getModelProperty(s[1]) & sim.modelproperty_not_model) == 0
local t = sim.getObjectType(s[1])
if t == sim.sceneobject_shape or t == sim.sceneobject_dummy or t == sim.sceneobject_octree or
t == sim.sceneobject_pointcloud or obj1IsModel then o1 = s[1] end
if #s == 2 then
obj2IsModel = (sim.getModelProperty(s[2]) & sim.modelproperty_not_model) == 0
local t = sim.getObjectType(s[2])
if t == sim.sceneobject_shape or t == sim.sceneobject_dummy or t ==
sim.sceneobject_octree or t == sim.sceneobject_pointcloud or obj2IsModel then
o2 = s[2]
else
o1 = -1
end
end
if obj1 ~= o1 or obj2 ~= o2 then hideDlg() end
end
obj1 = o1
obj2 = o2
if obj1 ~= -1 then
showDlg()
else
hideDlg()
end
if restoreData then if sim.getSystemTime() - restoreData.time > 1 then restoreScene() end end
end
function sysCall_cleanup()
hideDlg()
end
function sysCall_beforeInstanceSwitch()
hideDlg()
obj1 = -99
end