Well, here’s my first challenge entry ever. So i’m likely to overdo it a bit. this one renames an object by doubleclicking on an item in a list. I’ve prefilled the list with dutch construction-elements. you also get a wirecolor (optionally).
A cool thing i learned is using a callback to trigger the script to pop up.
Use:
Execute the script
Select a single object
Enter a questionmark in the name of the object
Script pops up
Doubleclick on a name in the script
This is easily also implemented as macro. Well here it is
--the main body
(
--put an item in one of these arrays to associate a color with it
local redWire = #("gevel","straat")
local greenWire = #("gras","raam","boom")
local blueWire = #("kozijn","water","rand","dakrand")
local cyanWire = #("glas")
local magentaWire = #("vloer","dorpel")
local yellowWire = #("rollaag")
local blackWire = #("trasraam","plafond")
local whiteWire = #("Script by Klaas","stoeprand","lamp")
local defaultWire = #("wand","binnenwand","balkon","stoep",
"dak","boeibord","waterslag","hek","spijl","beschoeiing",
"deur")
--the list of names, will be sorted alphabetically later on
local nameListArray = #()
local wireAssignArrays = #(redWire,greenWire,blueWire,cyanWire,magentaWire,yellowWire,blackWire,whiteWire,defaultWire)
for arr in wireAssignArrays do join nameListArray arr --combine the different arrays with names to a single one
local defaultColor = color 180 180 158 --use this color if the name is put in the defaultWire array
--associate a name with a wirecolor
function fn_getWireColor myName =
(
theRightColor = undefined
for arr in wireAssignArrays do
(
found = findItem arr myName
if found != 0 do
(
case arr of
(
redWire:theRightColor = red
greenWire:theRightColor = green
blueWire:theRightColor = blue
cyanWire:theRightColor = color 0 255 255
magentaWire:theRightColor = color 255 0 255
yellowWire:theRightColor = yellow
blackWire:theRightColor = black
whiteWire:theRightColor = white
defaultWire:theRightColor = defaultColor
)
)
)
theRightColor
)
--initialize the listView
function fn_initListview lv =
(
lv.columns.add "" 16 --the color column
lv.columns.add "" 80 --the name column
lv.View = (dotNetClass "System.Windows.Forms.View").Details
lv.HeaderStyle = lv.Headerstyle.none --i don't want to see the columnheader
lv.fullRowSelect = true
lv.multiselect = false
)
--populate the listview
function fn_populateListview names lv doColor =
(
lv.items.clear()
--sort the names alphabetically
sort names
local theItemArray = #()
for o in names do
(
--the first item is the color
theItem = dotNetObject "listViewItem"
theItem.text = ""
theItem.UseItemStyleForSubItems = false--don't use colors of the main item in the subitem
theItem.subItems.add o --the subItem hold the actual name
--if the user wants to see the wires: change the color of the text of the items in the list
if doColor == true do
(
theColor = fn_getWireColor o
theItem.backColor = (dotNetClass "System.Drawing.Color").fromARGB theColor.r theColor.g theColor.b
)
append theItemArray theItem
)
lv.items.addrange theItemArray
)
--define the rollout
rollout theWhatsMyNameRollout "whatsmyname"
(
label theLabel "Doubleclick to apply" align:#center --pos:[2,2]
label theLabel1 "the objectname." align:#center --pos:[2,18]
checkBox chkDefaultWire "Use wirecolor" checked:true offset:[-11,0]
checkBox chkCloseOnRename "Close on rename" offset:[-11,0] checked:true
dotNetControl lvNameList "System.Windows.Forms.ListView" width:120 height:450 offset:[-11,0]--pos:[2,35]
--init and populate the list when the rollout opens
on theWhatsMyNameRollout open do
(
fn_initListview lvNameList
fn_populateListview nameListArray lvNameList (chkDefaultWire.checked)
)
--reflect the use of color in the UI
on chkDefaultWire changed state do
(
case state of
(
true:fn_populateListview nameListArray lvNameList true
false:fn_populateListview nameListArray lvNameList false
)
)
--apply name and wirecolor when an item is doubleclicked
on lvNameList doubleclick control eventArgs do
(
--get the name of the clicked item
theName = control.selectedItems.item[0].subItems.item[1].text
--get the right wirecolor for a name and paint it on an object
if chkDefaultWire.checked == true do
(
theColor = fn_getWireColor theName
for o in selection where isProperty o #Wirecolor do o.wirecolor = theColor
)
--apply the doubleclicked name to every selected item
for o = 1 to selection.count do
(
--create a three-digit number with leading zeros
theNumber = o as string
if theNumber.count < 3 do
(
leadingZero = ""
for n = 1 to 3 - theNumber.count do append leadingZero "0"
theNumber = leadingZero + theNumber
)
--apply the name
selection[o].name = (theName + "_" + theNumber)
)
if chkCloseOnRename.checked == true do destroyDialog theWhatsMyNameRollout
)
)--end rollout
)--end body
function fn_popItUp =
(
local theParam = callbacks.notificationParam()
local triggerCharacter = "?"
triggered = findString theParam[2] triggerCharacter
if triggered != undefined do
(
thePos = mouse.screenpos
thePos += [-200,0]
if thePos.x < 0 do thePos.x = 0
if thePos.y > 600 do thePos.y = 600
global theWhatsMyNameRollout
try(destroyDialog theWhatsMyNameRollout)catch()
createDialog theWhatsMyNameRollout 125 535 pos:thePos modal:false
)
)
--callback mechanism
callbacks.removeScripts id:#klaasRenamer --remove the callback
callbacks.addScript #nodeNameSet "fn_popItUp()" id:#klaasRenamer --add the callback
Klaas