LUA-Events
Lua events enable to launch callback functions that have been defined in the file cnaevent.lua. Callback functions are functions that are called after a specific command in CadnaA has been triggered by the user. An example would be CadnaA saving the grid automatically with a specific file name after a grid calculation.
The file cnaevent.lua must be stored in the same directory as the CADNAA.INI file (path: C:\Users\USERNAME\AppData\Local\ Datakustik\CadnaA). Create and edit the file cnaevent.lua using a Lua editor (e.g. LuaEdit).
Presently, the following callback functions are available:
| Function | Parameter |
|---|---|
| Load | Filename |
| LoadTile | Filename, Rectangle (left,top,right,bottom), RadMax |
| Save | Filename |
| SaveTile | Filename |
| ChangeVariant | - |
| CalcGrid | - |
| CalcImm | - |
Lua events just have a single parameter (init). The user cannot pass this parameter, since controlled by CadnaA itself by calling the Lua event using
- init>0 prior to the CadnaA function and
- init<=0 after the CadnaA function.
Example
The following example includes two functions as CadnaA Lua events:
-
The first function saves the grid after a grid calculation using a filename containing the name of the calculated variant.
-
The second function exports - after the calculation at receivers - the calculation results for each receiver and for all evaluation parameters to a text file using a filename containing again the name of the calculated variant.
-- cnaevent.lua
function cnaevent.CalcGrid(init)
if init > 0 then
-- execute this part before any grid is calculated
else
-- execute this part after any grid is calculated
gridname= cna.run_macro("#(File,LPN)").."_".. cna.run_macro("#(Variante)").. ".cnr" cna.save_grid(gridname)
cna.redraw()
end
end`
function cnaevent.CalcImm(init)
if init > 0 then
-- execute this part before any receiver is calculated
else
-- execute this part after any receiver is calculated
filename= cna.run_macro("#(File,LPN)").."_results_"..
cna.run_macro("#(Variante)").. ".txt"
local file = io.open(filename, "w")
for imm in cna.tables.imm:all() do
file:write(imm.ID .. " " .. imm.LP1 .. " " .. imm.LP2 .. " " .. imm.LP3 .. " " .. imm.LP4 .. "\n")
end
file:close()
end
end
Macro commands for LUA Events
Beyond the CadnaA specific Lua commands also CadnaA macro commands (based on keywords) can be used in Lua Events (see Chapter 3 - Keywords in the manual „Attributes, Variables and Keywords“).
Basically, all the keywords can be used in Lua events, but not all necessarily make sense in this context.
Note
Macro commands may be used not only in Lua events, but also in normal Lua scripts.
Examples
| Syntax | Explanation |
cna.run_macro("#(File,LPN)“) |
returns the current filename using parameters LPN (see File in the manual „Attributes, Variables and Keywords“) |
cna.run_macro("#(Variante)") |
returns the short identifier of a variant (see Variants in the manual „Attributes, Variables and Keywords“) |
Execute
Activate the option „Call LUA Events“ on the Miscellaneous dialog (Options menu) and then quit CadnaA. Upon a restart of CadnaA the functions in the cnacevent.lua file are executed when one of the callback functions in this file is executed.
Further Information
- Lua events are always called as long as the file cnaevent.lua exists and the option „Call LUA Events“ on the Miscellaneous dialog (Options menu) is activated.
- Any changes in the cnaevent.lua file cause an effect only after a restart of CadnaA. This means that merely a change in the file cnaevent.lua does not cause a change in execution.
- CadnaA does not return any error messages (e.g. due to syntax errors in the file cnaevent.lua). In this case, CadnaA operates as usual, just not using the Lua Events. For syntax check it is recommended, therefore, to test the Lua functions during development within a Lua script which can be called via menu Extras|Execute Lua-Scripts.