npCalc plugin

About this plugin

With this plugin you can include and calculated one or more customized math expresions on your publications. The expresions can contain numbers, common math operations, variables, constants and subroutines with or without arguments.

As you can see on the plugin samples the plugin is very easy to use, and you can doing simple math expresions calculations or very complete, all depend on you and your publications needs. You can control all expresions identifiers, values, and more on every Calc objects instances.

Thirparty

This plugin are imposible without the aid of this people:

Thanks a lot!

Plugin actions index

npCalcCreate

Create a new instance of an Calc object. The result variable store the numeric ID of the new created Calc object instance. You need this ID on other plugins actions.

↑↑

npCalcDestroy

Destroy a previously created instanace of an Calc object. The result variable store "True" if everything is OK, or "False" if not. In this last case the [LastError] contain information about the error.

↑↑

npCalcDestroyAll

Destroy all previously created instances of Calc objects.

↑↑

npCalcRun

Run a Calc object providing the expresion to be executed. Provide the expresion to be execute and run the calculation task. The expresion can contain numbers, simple math operations (+, -, *, /), constants, variables and functions/subroutines.

Everytime that the Calc object found an unknow identifier an event/subroutine is fired. You can set what subroutine is fired with the npCalcRemoveConst action. Carefully see this action for more details.

Also I recommended you to play with plugin samples. Finally the result variable store "True" if everything is OK, or "False" if not. In this last case the [LastError] contain information about the error.

↑↑

npCalcAddConst

Add a constant to be available on the Calc object when run their expresion. With this action you can add constants values on your expresions. Even when you can use npOnCalcGetValue to get information about unknow expresion identifiers, with this action you can add constants identifiers and values at the same time.

Note that the refered event only is fired when an unknow identifier is found on the expresion, so you cannot change the constants values once you added. Of course you can remove and then add constants when needed. The result variable store "True" if everything is OK, or "False" if not. In this last case the [LastError] contain information about the error.

Also note that constants cannot has a type, like variables or function arguments. Constanst types are deduced from their context use. So, if you use numbers on an expresions with constants, the constant type are numeric. In other case constants are of string type.

↑↑

npCalcRemoveConst

Remove a previosly added constant from a Calc object. Once you remove a constant from the Calc object, the constant is no more taken as a know identifier, and the appropiate event is fired (See npOnCalcGetValue). The result variable store "True" if everything is OK, or "False" if not. In this last case the [LastError] contain information about the error.

↑↑

npCalcClearConsts

Remove all previosly added constants from a Calc object. See also npCalcClearConsts. The result variable store "True" if everything is OK, or "False" if not. In this last case the [LastError] contain information about the error.

↑↑

npCalcAddSub

Add a subroutine to be available on the Calc object when run their expresion. Provide the subroutine name, and the params types. Use the action properties dialog, double click on the params types list and your choice are automagically added on the needed action argument.

The params types currently available are: varString and varInteger. Note that the function params count is decuded by the plugin from the params types: if you choose two params types, then the function/subroutine need/has two params.

When the subroutine you determine is executed, some variables are available. And, like a NeoPlugins convention, just after the subroutine is executed these variables are clear from memory. This avoid variables flood. You can use this variables on every subroutine you add:

So you can play with this variables and, finally, set the expresion function/subroutine result. For do this just use the [Calc.Result] variable. So here is a simple sample that supose a subroutine "MySum" with two arguments availables:

:MySum
  .Just sum the arguments and return it
  SetVar "[Calc.Result]" "[Calc.Args0] + [Calc.Args1]"
Return

Of course you can, for example, test if certain argument is into some range, etc. And maybe inform the user of this kind of errors. Finally the result variable store "True" if everything is OK, or "False" if not. In this last case the [LastError] contain information about the error.

↑↑

npCalcRemoveSub

Remove a previosly added subroutine from a Calc object. Once you remove a subroutine from the Calc object, the subroutine is no more taken as a know identifier, and the appropiate event is fired (See npOnCalcGetValue). The result variable store "True" if everything is OK, or "False" if not. In this last case the [LastError] contain information about the error.

↑↑

npCalcClearSubs

Remove all previosly added subroutines from a Calc object. See also npCalcClearSubs. The result variable store "True" if everything is OK, or "False" if not. In this last case the [LastError] contain information about the error.

↑↑

npOnCalcGetValue

Set a subroutine to be execute when a unknow identifier is found on the Calc object running expresion. When this subroutine is execute you can found some variables at your service, so, is easy to found what identifier is not found (by name), what value assign to it, if needed, etc. Here is the list of variables that you can use on this subroutine:

Apart of the above variables, you can set another two variables into the subroutine:

Take a look at this last variables. The "Done" is an important variable, because if you dont set this to true, the expresion calculation fail and an "Unknow identifier" error is raised. So when you assign "True" to "Done" you want to said: "Ok, I put a value for this identifier, go ahead with the resul of expresion". I recomend you that play a little with the plugins examples that deal with this.

The current supported value types are: varString and varInteger. You can assign to the value type variable the identifier value type that you set on the value variable. If you dont assign any value the Calc object take as the default: varInteger. Supose your expresion is: "A + B", when A = 1 and B = 2. If you chose "varString" types for this identifiers then the expresion result is "12", not "3".

Please, note that the object ID variable store the Calc object instance ID that fire the event. This variable is not on the above list because his name is can be defined by you when edit this action. The result variable store "True" if everything is OK, or "False" if not. In this last case the [LastError] contain information about the error.

↑↑

Action errors subroutine

All the NeoPlugins deal with errors in the same way that NeoBook does: when the plugin found an action error the [LastError] variable store information about the error, so you can take care about this variable when execute an action.

But all the NeoPlugins also incorporate an advanced way to deal with possible action errors. You can define a subroutine named OnNeoPluginActionError in order to be executed when some action error are found and you can use this variables inside:

Note that this error handling subroutine are shared for all the NeoPlugins, so you no need to specify a subroutine for every plugin you use in your publication because the same subroutine are recognized and automagically used by every NeoPlugin. Below you can view a sample of this subroutine code:

:OnNeoPluginActionError
  AlertBox "NeoPlugin Error" "Error [LastError] in plugin: [PluginName]"
Return

Also note that the use of this NeoPlugins error handling subroutine is completelly optional. You can continue using the [LastError] variable as usual and even use the both methods at the same time.

↑↑