# Example POSTFEKO API Script

The easiest way to understand and get started with scripting is by analysing a working example. As part of the example, a number of important aspects are highlighted. The example can be copied into the script editor and executed as part of the demonstration.

## Open a Model

The first part of the example will get hold of the POSTFEKO application object, create a new project session and load a model. Try to understand what the script does and then read the section that explains the script.
app = pf.GetApplication()
app:NewProject()
FEKO_HOME = os.getenv("FEKO_HOME")
print("Feko is installed in " .. FEKO_HOME)
app:OpenFile(FEKO_HOME..[[/shared/Resources/Automation/startup.fek]])
printlist(app.Models:Items())

The code extract starts by accessing and storing the application object, using the GetApplication static function that is available under the pf namespace . All functions, objects, constants and enumerations are available in the pf namespace, although a subset of these are also globally available. These were added in a namespace so that they will not be replaced when loading external libraries. The namespace also makes it easier to use the auto completion feature in the editor (type pf. in the editor to see the auto completion menu). The application objects are stored in a variable app to make it easier to access further down in the script.

The NewProject() method of the Application object is then executed to create a new POSTFEKO project (session).
Note: That the method is accessed using a colon (:) and to access the static function use a full stop(.).

The FEKO_HOME environment variable is stored in a new variable by utilising the os (operating system) module’s getenv method. The os module is included as part of the Feko installation. The value of the variable is then printed to the screen for validation.

The OpenFile method of the Application object is then used to load the startup model (a demonstration model that is part of all Feko installations). Finally the models that have been loaded are printed to screen as confirmation that the model has indeed loaded correctly.

If the script is executed, POSTFEKO should have the startup model loaded. The rest of the example will illustrate basic tasks that can be performed using the startup model.

## Access the Model Configuration

The start and end frequency of the first (and only) configuration of the model is accessed and printed to the console in the next script example. This example follows on the previous example and assumes that it is executed in the same script.
c1 = app.Models["startup"].Configurations[1]
print(c1.EndFrequency)
print(c1.StartFrequency)

A variable, c1, is used to store a link to the first configuration of the model. The model that has been loaded is accessed using the Models property of the Application object. Properties, like static functions, are accessed using a full stop as indicated in the example. The Models property returns a collection. There are many collections in the POSTFEKO API, but the collections work the same and have the same methods and operators associated with them. An item in the collection is accessed by name or by index using the square bracket indexing ([]).

The example uses both indexing methods since it indexes the model by name and the configuration by number. The same result is achieved by accessing both the model and the configuration by name or by number.

The start and end frequency for the configuration is printed to demonstrate the model information is accessed.
Note: That it was not necessary to store the configuration in variable c1, but it makes it easier and shorter to access the configuration further down in the script.

## Create and Customise a Cartesian Graph

The following script extract creates a Cartesian graph, sets background and grid colours. The minor grid is also enabled for the graph.
cg = app.CartesianGraphs:Add()
cg.BackColour = pf.Enums.ColourEnum.Transparent
cg.Grid.BackColour = pf.Enums.ColourEnum.LightGrey
cg.Grid.Minor.Visible = true
cg.Grid.Minor.HorizontalLine.Style = pf.Enums.LineStyleEnum.SolidLine
cg.Grid.Minor.VerticalLine.Style = pf.Enums.LineStyleEnum.DashDotDotLine
cg.Grid.Major.HorizontalLine.Colour = pf.Enums.ColourEnum.Black
cg.Grid.Major.VerticalLine.Colour = pf.Enums.ColourEnum.Black
The only new concept that is introduced in this script extract is the use of enumerations to access predefined colours and line styles. Enumerations are accessed using the pf.Enums namespace.

## Add a Trace to a Graph

The graph has been created, but it does not contain any data. The next script extract will add the first near field in the model configuration to the Cartesian graph. The legend, horizontal and vertical titles are also styled.
cg.Traces:Add(c1.NearFields[1])
cg.Legend.Frame.BackColour = pf.Enums.ColourEnum.Transparent
cg.HorizontalAxis.Title.Frame.Line.Colour = pf.Enums.ColourEnum.Blue
cg.VerticalAxis.Title.Frame.Line.Colour = pf.Enums.ColourEnum.Blue

## Generate a PDF Report

The final script extract will create a PDF report and save it to disk. Once the report has been generated it will also be displayed.
report = app:CreateQuickReport([[Example_report]], pf.Enums.ReportDocumentTypeEnum.PDF)
report:Generate()