FlexScan3D User Manual - Version 3.3.4.x

You are here: API/SDK and Automation > Automation > LUA Basics

LUA Basics

This section covers the basics of scripting with Lua and FlexScan3D. It is not meant to be a comprehensive guide. For more in-depth information, refer to the Lua reference manual here: Lua Language Reference

Debugging

To check the value of a variable, use the PrintValue() function.

a = 4

PrintValue( a )

That would display PrintValue(variable="4") in the log window. Note that some variables cannot be displayed, such as lists. To display the contents of a list, loop through and print the values by their index in the list (PrintValue(myList[2])).

Comments

To add a comment line into the script, place "--" as the first non-whitespace characters on a line.

-- this is a comment

 

Variables

Variables in Lua can be defined without needing to specify the type - Lua will automatically set the variable to the correct type as needed.

Global

The default scope in Lua is global. These variables are persistent, so setting a variable in one script will allow that variable to be accessed in another script.

a = true

a = 9.37

 

Local

For variables which will only be used within the context of the current script, the local keyword can be specified.

local a = true

local a = 9.37

 

Conditionals/Booleans

To check if a statement is true or false, we can use conditional statements. Keywords include if, then, else, elseif, and not.

a = 4 * 6

if a < 20 then

    PrintValue("a is smaller than 20")

else

    PrintValue("a is larger than 20")

end

 

a = false

if not a then

    PrintValue("a is false")

end

 

Loops

for

Runs for a specific number of iterations in the format "for i=startIndex,endIndex,increment". If the increment is absent, it is presumed to be 1.

for i=1,10,1 do

    PrintValue( i )

end

 

while

Runs until a condition is no longer true.

count = 10

while count > 0 do

    PrintValue( i )

    count = count - 1

end

 

nil

Some functions will return nil. This is a null value and usually indicates that the function failed.

Strings

Text and other variables can be concatenated using ".." characters.

timeString = "The time taken was: "

totalTime = 937

PrintValue( timeString .. totalTime .. " milliseconds" )

 

Lists

Several functions return lists. These lists are based on the .NET System.Collections.Generic.List<string>, and can be accessed as such.

groupList = GetAllGroups()

PrintValue( "There are " .. groupList.Count .. " scans in the current project." )

 

New lists can be created from scratch using the NewListString() function. Here is an example using Add() (to add a single string) and AddRange() (to add a list of strings).

myList = NewListString()

myList:Add( "Item 1" )

myList:Add( "Item 2" )

newList = NewListString()

newList.AddRange( myList )

PrintValue( newList.Count )

-- displays: PrintValue(variable="2")

 

 

 

Copyright © 2015 LMI Technologies, Inc. All rights reserved.