> For the complete documentation index, see [llms.txt](https://www.opal-rt.com/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.opal-rt.com/docs/smart-power-simulation-software-sps/get-started/build-models-using-model-construction-commands.md).

# Build Models Using Model Construction Commands

This section shows how to use model construction commands to add blocks to your model and connect them.

## Add blocks with commands

Suppose you want to add a PI Section Line block and a Voltage Measurement Preview block to your model, connect the + terminal of the Voltage Measurement Preview block to the left end of the PI Section Line block, and connect the - terminal of the Voltage Measurement Preview block to the right end of the PI Section Line block.

{% stepper %}
{% step %}

### Add and position the two blocks

Use add\_block to add the two blocks, then set their positions with set\_param.

{% code overflow="wrap" lineNumbers="true" %}

```matlab
add_block('sps_lib/Power Grid Elements/Pi Section Line','Mymodel/Block1');
add_block('sps_lib/Sensors and Measurements/Voltage Measurement','Mymodel/Block2');
set_param('Mymodel/Block1','position',[340,84,420,106]);
set_param('Mymodel/Block2','position',[520,183,545,207]);
```

{% endcode %}
{% endstep %}

{% step %}

### Get the terminal port handles for each block

Retrieve the PortHandles for each block so you can reference their connectors when wiring them together.

{% code lineNumbers="true" %}

```matlab
Block1PortHandles = get_param('Mymodel/Block1','PortHandles');
Block2PortHandles = get_param('Mymodel/Block2','PortHandles');
```

{% endcode %}
{% endstep %}

{% step %}

### Connect the blocks using add\_line

The add\_line command uses the RConn and LConn fields of the PortHandles structures:

* RConn represents the right connectors of the block.
* LConn represents the left connectors of the block.

Specify the indices of the connectors you want to connect.

{% code lineNumbers="true" %}

```matlab
add_line('Mymodel',Block1PortHandles.LConn(1), Block2PortHandles.LConn(1));
add_line('Mymodel',Block1PortHandles.RConn(1), Block2PortHandles.LConn(2));
```

{% endcode %}
{% endstep %}
{% endstepper %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.opal-rt.com/docs/smart-power-simulation-software-sps/get-started/build-models-using-model-construction-commands.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
