Visual basic is one of the scripting languages you can use to write code for interfacing with the PCB design software Altium, through it’s provided API.
VBScript is not case-sensitive. That is, the variable myInt is the same as the variable myint. This said, I prefer to capitalise the start of any system word in VBScript (e.g. And, For, If, End If, Dim).
There is no end of command/end of line delimiter. This is probably one of the first things you will notice when coming from another language such as C.
Variables are defined with the keyword Dim. However, by default, you do not need to do this before using them (you can create them on the fly).
Forcing You To Declare Variables
By default, VBScript does not force you to declare variables. This can generate hard-to-find bugs when you accidentally type a variable name in incorrectly and end up creating a new variable rather than using an old one. To prevent this from occurring, add the following line of code to the top of every script file:
This forces you to declare every variable with Dim before using it. I make sure to add this to the top of every file!
Single-line comments are started with the ’ character, and continue until a new line occurs.
Strings are delimited by double-quotation marks (").
You can add non-printable characters the using system constants.
You can format doubles into strings with the function
Regex is particularly easy in VBScript, as it comes with a built-in regex engine (as of VBScript v5.0). Regex is performed through iteration with the RegExp object. The regex syntax in VBScript is very similar to that used for JScript.
The RegExp object provides 3 properties and 3 methods. The properties are:
|Pattern||A string containing the regex pattern to match input with. This is a core part of the object! This must be set before any RegExp methods are called.|
|IgnoreCase||A boolean that when True ignores the case (case insensitive) when matching the pattern.|
|Global||A boolean that if True, will match only the first pattern it finds. If False, the regex engine will find all patterns. Default is False.|
The methods are:
|Test(searchString)||Returns True if the regex pattern set by Pattern can be matched in searchString, otherwise False.|
|Replace(searchString, replaceString)||Replaces any match of Pattern in searchString by replaceString, and returns the new string. If no matches were found, the original string (searchString) is returned.|
|Execute(searchString)||Returns a Matches collection object, containing a Match object for each successful match of Pattern in searchString. It doesn't modify searchString.|
The Match object is returned when the Execute method of the RegExp object is called. It has three properties:
|FirstIndex||The position within the original string where the match occurred. This is zero-based (the first position is 0).|
|Length||The total length of the matched string.|
|Value||A string of the matched text. This is the default property when accessing the Match object.|
The following example uses regex to search for a component designator.
A unary operator for easy increment/decrement is not supported (like ++ and – in C), so the best way is to just use:
Function pointers, in the C style of using them, are not available in VBScript. However, you can use the GetRef() function to get similar functionality.
You pass into
GetRef(funcNameString) a string of the name of the function you want to get a reference to (i.e. pointer).
GetRef() is one of the functions you can’t use when doing Altium Scripting.
Sets (The MkSet()/InSet() Functions)
Sets of objects are supported in VBScript through the MkSet() and InSet() functions. It creates a set of the given input objects. You can have a maximum of 32 objects in a set. You can also create an empty set, by providing no input variables.
Sets are a common design object used in a number of other scripting languages such as DelphiScript, JScript, C++Script and C#Script.
VBScript has no built in value for Pi. What?!? The best way to do this is to use the following expression:
This will give Pi to the maximum precision available on the platform you a running on. You can’t ask for better!
Thankfully, VBScript has built in support for the
sin() family of functions. They take an input angle in radians. To convert between radians and degrees you can use the following expressions:
This work is licensed under a Creative Commons Attribution 4.0 International License .