# If you use IPython/Jupyter:
import sys
sys.path.append(r"C:\Program Files\DIgSILENT\PowerFactory 2023 SP5\Python\3.11"
# you may use a different directory
)
# Get the PF app
import powerfactory
import powfacpy
= powerfactory.GetApplication()
app
app.ActivateProject(r"powfacpy\powfacpy_tests_copy_where_tests_run"
# You may change the project path.
)
app.Show()
CGMES handling with model exchange interface
1 About CGMES
CGMES stands for Common Grid Model Exchange Standard. It is a standard developed to facilitate the exchange of grid models and data among different stakeholders in the power system industry, such as utilities, system operators, and software vendors.
CGMES is structured using several profiles, each contained in a separate file. An exemplary selection of profiles is given in Table 1.
Abbreviation | Description |
---|---|
SSH | Steady State Hypothesis (steady state Load Flow Data with changeable parameters) |
EQ | Equipment Model |
TP | Topology profile: contains all topology objects, i.e. how equipment is electrically connected. |
SV | State Variables |
DL | Diagram Layout |
For more information on CGMES, visit the following links: Video, ENTSO-E website, and Document.
2 Model exchange interface
The model exchange application of powfacpy contains functionalities to import and export CGMES data, as well as to update an existing CGMES grid with new data, for example from an SSH file.
This tutorials demonstrates how to
- Import CGMES
- Export CGMES
- Update CGMES using SSH and DL profiles
First, activate the PowerFactory project as outlined in the getting started tutorial.
Get powfacpy objects (CGMES interface class and ActiveProject
)
= CGMES(app)
pfcgmes = pfcgmes.act_prj pfp
Activate the study case:
= pfp.get_unique_obj(r'Study Cases\test_model_exchange_interfaces\Study Case')
study_case study_case.Activate()
1
The following grid exists in that project.

3 Export to CGMES
First, let’s export this grid to CGMES. The function cgmes_export()
exports the active grid to OUTPUT_PATH
.
= r"..\..\tests\tests_output\cgmes_export"
OUTPUT_PATH
="all", as_zip=True) pfcgmes.cgmes_export(OUTPUT_PATH, selected_profiles
Info: The CGMES export can trigger error messages in the PowerFactory output window, but these do not usually affect the correctness of the exported data.
Here we can see the exported file.

It is a .zip file, because we configured as_zip=True
.
We can also export the separate files directly into the folder by setting as_zip=False
. Additionally we can choose which profiles to export:
="ssh dl sv", as_zip=False) pfcgmes.cgmes_export(OUTPUT_PATH, selected_profiles
Info: The CGMES export can trigger error messages in the PowerFactory output window, but these do not usually affect the correctness of the exported data.
Now we exported the .xml files (not zipped) and only got SSH, DL and SV:

4 Import from CGMES
Now we could import our grid into another simulation software. Here, we just import again to PowerFactory.
We’ll demonstrate this by switching to a different study case and importing the grid again.
study_case.Deactivate()
= pfp.create_in_folder(
new_study_case "New Study Case.IntCase",
study_case.GetParent(),=True,
overwrite
) new_study_case.Activate()
0
Then import the previously exported CGMES files using cgmes_import()
= pfcgmes.cgmes_import(
new_grid + "\\"
OUTPUT_PATH + pfcgmes.exported_zip_name # default name that pfcgmes uses for exported zip file
+ ".zip"
)
new_grid.Activate()
0
The imported grid will have the same name as the original.
Import location is the root of the network data folder. The imported grid has a different folder structure than the original and a library folder with model types is imported together with the grid.

But the resulting grid model still looks the same as the original:

5 Update model with external data
Using CGMES, we can update a model using external data (e.g. SSH and DL).
In this case, we will think of study_case
as some external reference model. We will change its data and update new_study_case
accordingly using CGMES.
study_case.Activate()
0
All our loads have reactive power setpoints.

Let’s change them in the reference model. We will set them to 0, just as an example.
pfp.app.Hide()
= pfp.get_unique_obj(r'Network Model\Network Data\test_model_exchange_interfaces\Grid')
reference_grid for load in pfcgmes.act_prj.get_obj("*.ElmLod", include_subfolders=True, parent_folder=reference_grid):
= 0
load.qlini
pfp.app.Show()
Now the reactive power of all loads in the reference model are 0:

While in the new model it is still like it was before:

To update our new model with the change in reference model, we first export the reference model’s changes to CGMES using the SSH and DL profiles.
="ssh dl", as_zip=True) pfcgmes.cgmes_export(OUTPUT_PATH, selected_profiles
Info: The CGMES export can trigger error messages in the PowerFactory output window, but these do not usually affect the correctness of the exported data.
Then we take this to our new model.
new_study_case.Activate()
0
We need to supply the CIM archive that we used to import the new model. It is used as a base archive and needed for the update.
= pfcgmes.act_prj.get_unique_obj(
base_archive =pfcgmes.archive_folder
pfcgmes.import_archive_name, parent_folder )
And by running update_profiles()
we can update our new model.
pfcgmes.update_profiles(=OUTPUT_PATH + "\\" + pfcgmes.exported_zip_name + ".zip",
update_file_path=base_archive
base_archive )
Now, the changes made in the reference model are also present in our new model.
