Python Scripts in Vertec

Basics of Python Scripts

Product line

Standard

|

Expert

Operating mode

CLOUD ABO

|

ON-PREMISES

Modules

Services & CRM

Budget & Phases

Purchases

Resource Planning

Business Intelligence

Created: 22.02.2010
Updated: 19.08.2024 | Added Python console to table of contents.

Vertec has included a Python Engine to execute custom scripts and control Vertec. From a Vertec Python script, you can access the entire Vertec object model.

A Python script can be created either in the Script Editor or in a text editor. Script that are used repeatedly can be registered in Vertec (only Line Expert). They can then be retrieved from the Actions menu item or from the context menu with the right mouse button. See the article about registering scripts.

Further information:

Table of Contents

The script editor

To create and execute Python scripts, there is the Script Editor. You can open it from the menu Settings > Script Editor.

The Script Editor can also be opened via the Button Script. In this case it contains the script text of the corresponding script.

The script editor is described in detail in this separate article.

The Python console

An important part of the Script Editor is the ability to execute a Python statement on the current object and see the result, i.e. the console functionality. To make this easier, since Vertec 6.3 there is a Python console available for users with administrator rights.

You can show and hide the console from the Settings settings menu and the F3 keyboard shortcut:

The console shows at the bottom of the Vertec window:

The height of the console can be changed with the mouse:

Right-clicking on the Python console brings up a context menu with the option to delete the console content.

The variable “argobject”

The argobject variable returns the current object (the current entry) in Vertec. This is the entry that is selected as the main entry in the active Vertec window, e.g.:

project = argobject

Registered scripts can also be called on a single object in the list by right-clicking. The argobject is then the single object. However, if you want to access the current entry in Vertec, i.e. the container “Own projects” in the example, you can use the variable currentobject:

This difference can also be seen in Event scripts . If this is triggered, for example, when an custom field item changes on a specific additional class, argobject is the object of the triggering additional class. If the field were to be changed in the single window of another class, this object would be the currentobject and thus the current object in Vertec.

The module “vtcapp”

The Vertec installation comes with a Vertec Python library. This is automatically available after installation and is called vtcapp. For this table there is the help function help(vtcapp), which lists all the information:

Methods and functions

The vtcapp module has the following important methods and functions (only the most important ones are listed, the full list can be found in the article Vertec Python Functions ):

Method/Function description Sample code
createobject(class: string) Creates a new object of the specified class.
service=vtcapp.createobject(“OpenPerformance”)
currentlogin(): Project user Currently logged in user
>>> user = vtcapp.currentlogin() 
>>> editor.name
Christoph Keller
evalocl(expression: string): expressiontype Evaluate global OCL expression.
projectList = vtcapp.evalocl(“Project.allinstances->orderby(code)”)

Calling functions without parameters

When calling a function that does not expect any parameters, it is very important that the parentheses are not forgotten. Example:

vtcapp.currentlogin #Wrong, function is not called, an error message is issued
vtcapp.currentlogin() #Correct

“evalocl” (evaluate an OCL expression)

With eval, on an object or on a list of objects, a     OCL expression evaluated.

Example:

 # Invoice
 obj = argobject
 Services = obj.evalocl(“benefits”)

 # Total Services
 Totalbenefits =benefits.evalocl(“if self->first.account.accounted then\
 oclastype(BilledPerformance).valuetext else oclastype(OpenPerformance).valuetext endif->sum”)

Global OCL variables (that is, those that refer to the entire Vertec and not to a single object or list) are evaluated via vtcapp.evalocl (see The vtcapp module above).

Defining Functions

Within the script, functions are defined with def<funktion>::

def ChangeText(leist):
    leist.text = ...

After that, it can be accessed in the code:

ChangeText(argobject)

Calling functions from another script (module)

In Python it is possible to call a function from another script. Functions that are used more often or in several scripts should be stored in a separate script (module).

import <scriptname> #Scriptname of the module
<scriptname>.<funktion>

Example

  • vtcapp must be explicitly imported in these modules (import vtcapp)
  • systemcontext() is system-wide, even if you call something from another module in a script.
  • The called module must not have a space in its name.

Reload a module (Script)

In Vertec versions before 6.4, the imports are only loaded on the first call. Vertec remembers that this module (script) is loaded and does not reload it even if it is called again in the code. This means that if the module or script is changed in the background, these changes have no effect if the modified module has already been loaded.

The reload command can be used to reload a module. This can be executed once in the Python Editor or integrated directly in the script after the import command:

import scriptclass
reload(scriptclass)

Vertec versions from 6.4 do this automatically. In the same session, modified modules are reloaded the next time they are called. Other sessions recognize the modified script code via Notif Server , but do not reload the module.

Define global variables

Difference between local and global variables:

  • Local variables can only be used within the function and must be passed as parameters for each use in a different function.
  • Global variables are initialized once and are available in the same script in each function.

Global variables are declared outside of functions. To use a global variable within a function, it must be loaded with global:

Service = argobject # declare global variable

def ValueExtChange(amount):
    global service # use global variable
    Power.xValueExt = amount

def Handling(user):
    global service
    Performance.processor = user
Bitte wählen Sie Ihren Standort