How can I create Data Extensions via Python?

Category:
Scripting
Answer

For various use cases, additional data fields in existing object classes or completely new object classes may be needed. In PowerFactory, this is possible by using Data Extensions. They can be defined manually; in many cases, however, it is more convenient to create them using a script.

Creating, removing or editing data extensions must be initiated by calling BeginDataExtensionModification() on the active project. After this, existing data extension configurations can be retrieved and new configurations can be added. At the end of the modification it is necessary to call EndDataExtensionModification() on the active project to store the changes, as they will otherwise be discarded. The following code shows how a parameter is added to an existing object class:

# Start the modification
prj = app.GetActiveProject()
ext_data = prj.BeginDataExtensionModification()

# Add a new string parameter to terminals
config = ext_data.AddConfiguration('ElmTerm','Terminal')
config.AddString('shape','Shape of Busbar','','hollow tube')

# Finish the modification
prj.EndDataExtensionModification()

If the object class, specified in AddConfiguration, is not existent, a new object class with the additional prefix "Ext" will be created. The code

# Create a new configuration
config = ext_data.AddConfiguration('BusbarGroup','Busbar Group')

would therefore create a new object class called "ExtBusbarGroup". Other possible parameter types for a data extension are Integer, Double, Objects or vectors of these types. Additionally, it is possible to define constraints for the parameters that may be entered by users. The function SetConstraint can be used to define minimum and maximum values or to add a drop-down list containing allowed values.

An example project with a script that creates a new object class "ExtBusbarGroup" and  extends the parameters of terminals is attached to this entry. The project also contains a second script for creating an element with the new object class and linking an existing busbar to the new object using an additional object parameter.

Back