-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAlias change.lua
88 lines (76 loc) · 2.25 KB
/
Alias change.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
sim = require 'sim'
function sysCall_info()
return {autoStart = false, menu = 'Developer tools\nAlias change'}
end
function sysCall_addOnScriptSuspend()
return {cmd = 'cleanup'}
end
function sysCall_init()
simUI = require 'simUI'
sim.addLog(
sim.verbosity_scriptinfos, "This tool allows to replace/change aliases of selected objects."
)
end
function sysCall_nonSimulation()
if leaveNow then return {cmd = 'cleanup'} end
end
function sysCall_selChange(inData)
if #inData.sel >= 1 then
showDlg()
else
hideDlg()
end
end
function sysCall_beforeSimulation()
hideDlg()
end
function sysCall_cleanup()
hideDlg()
end
function sysCall_beforeInstanceSwitch()
hideDlg()
end
function showDlg()
if not ui then
local pos = 'position="-50,50" placement="relative"'
if uiPos then
pos = 'position="' .. uiPos[1] .. ',' .. uiPos[2] .. '" placement="absolute"'
end
local xml =
'<ui title="Alias change tool" activate="false" closeable="true" on-close="close_callback" ' ..
pos .. [[>
<group layout="form" flat="true">
<label text="Replace occurences of"/>
<edit value="originalString" id="1" />
<label text="with string"/>
<edit value="replacementString" id="2" />
</group>
<button text="Perform operation on selected objects" on-click="replace_callback" id="3"/>
</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
end
function replace_callback(ui, id, v)
local selectedObjects = sim.getObjectSel()
local originalString = simUI.getEditValue(ui, 1)
local replacementString = simUI.getEditValue(ui, 2)
if #originalString > 0 then
for i, handle in ipairs(selectedObjects) do
local name = sim.getObjectAlias(handle)
local newName, r = string.gsub(name, originalString, replacementString)
if r > 0 then sim.setObjectAlias(handle, newName) end
end
sim.announceSceneContentChange()
end
end
function close_callback()
leaveNow = true
end