Custom FPScript Functions Tutorial
FlexPro offers you the option of programming your own functions in FPScript, saving them in a template database and then using them in FPScript formulas. An FPScript function is a formula that uses at least one argument and is written in FPScript. You can declare individual or multiple arguments with the Arguments statement.
In the following example, you will learn how to program a function to normalize a data set, to register it as an FPScript function and then use it.
Note: The example is limited to implementing a custom function in the FPScript language. If you want to access external libraries (DLLs) to develop the function or to make existing algorithms available to FlexPro, you should implement the custom FPScript function in VBA or another automation-compatible programming language. See A Custom FPScript Function.
Registering a Custom FPScript Function
1.Create a formula with the following FPScript code:
Arguments Data, NormalizeTo
If NormalizeTo == Empty Then
NormalizeTo = 1.
End
NormalizeTo * Data / Maximum(Data)
The Arguments statement turns the formula into a function, which must be provided with a list of arguments when called.
2.Select the formula in the Object List and click Home[Selected Objects] > Save as Template.
3.On the first screen of the Template Wizard, choose FPScript function as the template type, enter Normalize as the name and enter Normalizes a data set for the comment.
Note: You can only select the FPScript function template type if exactly one formula has been selected. In addition, the formula in the first line must contain the keyword Arguments and must not have any syntax errors.
4.Click Next. As the Description for the Data argument, enter The data set to be normalized. Select the Permitted data types 16-bit integer through 64-bit floating point value. Select as the Permitted data structures Data series and Signal.
5.Click Next. As the description for the NormalizeTo argument, enter Value to which the data set will be normalized. As the Permitted data types select 16-bit integer through 64-bit floating point value. Select as the Permitted data structures Scalar value. Enable the Default value option and enter 1.
6.Click Next. Check the settings on the last screen of the wizard and save the template in the Personal template database. Close the wizard.
7.The function Normalize is now stored in the template database. You can now delete it from your project database. To do this, select the function from the Object List and press the DELETE key on your keyboard.
Using a Custom FPScript Function
1.Select Insert[Data] > Signal > Sine to create a synthetic sine signal.
2.Next, click Insert[Data] > Formula to create an empty formula and then click Formula Tools/Design[Insert FPScript] > Function. Select the Customized category and then choose the function Normalize.
3.For the Data argument, enter the Signal data set to be normalized, and for the NormalizeTo argument, enter the value 10.
The code should now look like this: Normalize(Signal, 10)
4.Now click on the name of the formula in the Object List. The sine curve is now displayed in the preview with the extreme values +-10.
Providing a Custom FPScript Function on the Company Network
Note: This option is only available in FlexPro Professional and Developer Suite.
1.Create a new project database and save it under the name FPScript library in any folder on the network.
2.Click File > Info > Organizer and select the Templates tab from the Organizer dialog box.
3.Click Shared Template Databases to open the dialog box of the same name.
4.Click Paste and then on the button to the right of the newly created field in the list.
5.Select the previously saved database and then close both dialog boxes by clicking OK.
6.In the Organizer dialog box, select the template type FPScript function.
7.On the left side under On the left side under Items available in, select the FPScript library. If this is not displayed in the list, click Close Project Database and then Open Project Database and select the file.
8.On the right side under Items available in, select the Personal template database.
9.On the right side in the list box, select the function Normalize and then click Copy to copy it to the shared template database.
10.Now click Delete to remove the function from the personal template database.
Notes:
To allow your colleagues to use the FPScript functions stored in the FPScript library template database, you need to enter them into the Shared Template Databases list as described under steps 2 through 5.
Functions with the same name in different template databases are loaded from the template database with the highest priority. The following order applies:
1) Personal template database
2) Shared template databases in the order in which they are entered in the Shared Template Databases list.