© 2026 MVC Software Ltd. All rights reserved. Free to download. AutoStruct 0.1, made from the source of 2026-11-21 (1ed71f970). Scripting AutoStruct from Python

Scripting AutoStruct from Python

AutoStruct

24 October 2026

Abstract

AutoStruct carries its own Python, and a script run in it can do what a person at the program can: open a project, bring a level and a board forward, draw elements, run the computation, save, and read every number the computation wrote. This manual is about the module that makes that possible, autostruct: where a script runs, what each function answers and in what form, what comes back from a command and how to take it apart, what an error looks like, which board a command reaches, and what a script must not assume. The commands themselves are in Structural elements from TCL; the data a script reads is in The project’s data.

Contents

1 Where a script runs
2 The module
2.1 status()
3 What a command answers
3.1 Taking a list apart
3.2 Words that hold spaces
4 When a command refuses
5 Which board a command reaches
6 A whole script
7 Scripts the assistant writes
7.1 Leaving the answer in the form asked
8 What a script must not assume
References
References

1 Where a script runs

There is one Python inside AutoStruct, and three ways into it (figure 1). Each shares the same interpreter and the same namespace: a variable set in one is there in the others.

[Picture]

Figure 1: Three ways into one Python. Whichever a script comes in by, it is the same interpreter, and it reaches the program through autostruct.tcl().
The Python tab

One line at a time, as at Python’s own prompt: an expression’s value is shown. A pasted block runs as a whole.

The Notebook tab

A notebook in the project’s notebooks folder, whose kernel is the program’s own Python. A starter notebook is put there the first time the tab is opened on a project.

The command autostruct::python

exec <code> runs code and answers what it printed; run <code> starts it and answers a job number, which status, output and wait take; file <path> runs a file. This is how a batch of commands, or another program, runs Python in AutoStruct.

What a script prints A job’s output — what it printed and any traceback — is collected and shown when the job ends, not line by line. A script that runs for minutes is silent until it finishes.

How long it may run There is no time limit. Python runs on a thread of its own, so the window stays alive; but it runs one job at a time, so a long script holds up the Python tab, the notebook and the Help’s Ask tab until it ends. While a stage of the computation runs, the ribbon and the level tabs are locked and the window shows it is busy.

The environment The standard library is always there, and so are autostruct and autostructcodes (the codes of practice). AutoStruct comes with numpy, matplotlib and Jupyter, the notebook’s server and kernel, installed with it. Packages of your own go into AutoStruct’s Python environment, which it adds to the path when it starts. The working folder is not the project’s: build every path from autostruct.project_dir().

2 The module

function

what it does, and what it answers

tcl(script)

runs a command of the program — any Tcl — in the interpreter of the board in front (§5); answers its result as text; raises TclError when the command refuses

status()

a dict describing what is open (§2.1)

project()

the open project’s name, or None

project_dir()

the project’s folder as a pathlib.Path, or None

levels()

the level names, a list of str, lowest first

level()

the level in front, or None when the Building tab is

level(name)

brings that level forward; answers name

board()

the board in front: ’Design’, ’Draft’, ’Details’, ’3d’, ’Building’, or None

board(name)

brings that board forward; answers name

building()

brings the Building tab forward

open(name)

opens a project; answers name

save()

saves the project

create_model(level)

Create Model of one level, from the Design board

collect(), solve()

gathers the frame of every level; solves it

design(*levels)

designs the levels named, or every level

staging()

the whole computation in order — every level’s Create Model, the gather, the solve, the design — answering a dict with one entry per stage; it stops at the first refusal and raises it, and leaves the level and board it found in front

TclError

the exception tcl() and the functions above raise

save(), level(name) and board(name) change what is in front or on disk; create_model, collect, solve, design and staging run the computation, which writes the level documents and the project database. Everything else only reads.

A trap. from autostruct import * brings in the module’s open, which opens a project, in place of Python’s open, which opens a file. Write import autostruct and call autostruct.open(...).

2.1 status()

>>> autostruct.status() 
{'project': 'Office', 'level': 'First', 'board': 'Design', 'levels': 3, 
 'open': 'enabled', 'exit': 'enabled'}

key

value

project

the project’s name, or None with nothing open

level

the tab in front: a level’s name, or ’Building’

board

the board in front

levels

how many levels there are — a number, not their names; levels() gives the names

open, exit

’enabled’ or ’disabled’: whether a project can be opened, and the program left, at this moment

A trap. status()[’levels’] is a count. A script that loops over it, as if it were the list of levels, fails with “’int’ object is not iterable”. Loop over autostruct.levels().

3 What a command answers

tcl() answers text, always: the command’s result as the Tcl interpreter writes it. Three shapes come back.

A word or a number

’9’ from element::count. Make a number of it with int() or float().

A list

Several values are a Tcl list in one string: words separated by spaces, and a value that itself holds a space wrapped in braces (Tcl Core Team, 2026b). element::list beam answers ’{B 1} {B 2} {B 3}’.

A time

Commands that act — level, tab, save, close, open, action, element::remove, element::select and a few more — answer how many milliseconds they took, ’85’. It is not a result to use.

3.1 Taking a list apart

Splitting a Tcl list on spaces breaks every value that has one in it: ’{B 1} {B 2}’.split() gives four pieces, two of them with a brace. Let Tcl do it: ask it to join the list with new lines (Tcl Core Team, 2026a), and split on those.

import autostruct 
 
def tcl_list(command): 
    """A command's answer as a Python list of str, split by Tcl itself.""" 
    joined = autostruct.tcl('join [%s] "\\n"' % command) 
    return joined.split('\n') if joined else [] 
 
beams = tcl_list('autostruct::element::list beam -level {First}') 
# ['B 1', 'B 2', 'B 3'] 
x, y, dx, dy = map(float, tcl_list('autostruct::element::geometry ortho {O 1}'))

For numbers the computation wrote — forces, moments, steel — do not parse command answers at all: read them from the level document or the database, where they are numbers already (The project’s data).

3.2 Words that hold spaces

A level, a label or a section may have spaces in its name. In a command, wrap each such word in braces, as the examples do: -level {First Floor}, {B 1}. A word holding a brace or a bracket itself needs more care; the safe way is to build the word with Tcl’s own list command (Tcl Core Team, 2026b).

4 When a command refuses

A command that refuses raises autostruct.TclError, a kind of RuntimeError, whose text is the refusal:

import autostruct 
 
try: 
    autostruct.level('Nonesuch') 
except autostruct.TclError as e: 
    print('refused:', e)

the text

means

the command’s own reason, e.g. no project is open

the command understood and refused

wrong # args: should be "…"

too few or too many arguments; the text shows the usage

invalid command name "…"

no such command on the board in front — misspelt, or a command another board carries (§5)

the command line has closed

the program is closing

Catch TclError around a command you expect may refuse, and let the others raise: a refusal you did not expect is information, and a script that carries on past it computes with something that is not there.

5 Which board a command reaches

Each of the four boards — Design, Draft, Details, 3d — has an interpreter of its own, and tcl() runs the command in the one whose board is in front at that moment (figure 2). With no project or no board, it is Design’s.

[Picture]

Figure 2: A command goes to the interpreter of the board in front. Most commands are on all four; a few are on Design’s alone.

Most commands are carried by all four: save, close, quit, the element commands, the computation’s commands. Opening and creating a project, editing the levels, and the forms are Design’s alone. The functions of the module that run the computation bring the Design board forward themselves. In a script of raw commands, bring it forward first:

autostruct.level('First') 
autostruct.board('Design') 
autostruct.tcl('autostruct::element::create ortho 0 0 0.4 0.4 -level {First}')

After design() the Draft board of the first level designed is in front, so a script that goes on to draw names its level and board again.

6 A whole script

Three short scripts show the module and the data together. The first counts what each level holds:

import autostruct 
 
for name in autostruct.levels(): 
    counts = {kind: int(autostruct.tcl('autostruct::element::count %s -level {%s}' 
                                       % (kind, name))) 
              for kind in ('ortho', 'beam', 'foot')} 
    print(name, counts)

The second runs the whole computation and reports what each stage answered, stopping at a refusal:

import autostruct 
 
try: 
    for stage, answer in autostruct.staging().items(): 
        print(stage, '->', answer) 
except autostruct.TclError as e: 
    print('the computation stopped:', e)

The third draws a level’s plan from its level document with matplotlib (Hunter, 2007) — the code that drew figure 3:

import json 
import matplotlib.pyplot as plt 
from matplotlib.patches import Polygon 
import autostruct 
 
def plot_level(document, ax): 
    level = document['level'] 
    for s in level['slabs']: 
        if s['kind']['useType'] == 0: 
            continue 
        points = [(p['x'], p['y']) for p in s['boundaries']['outline']] 
        ax.add_patch(Polygon(points, closed=True, facecolor='#2a6f3b', alpha=0.08)) 
        c = s['boundaries']['centre'] 
        ax.text(c['x'], c['y'], 'P ' + s['id']['label'], ha='center', color='#2a6f3b') 
    for b in level['beams']: 
        x1, y1, x2, y2 = (b['coords'][k] for k in ('x1', 'y1', 'x2', 'y2')) 
        b0 = b['crossSection']['b0'] 
        length = ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5 
        nx, ny = -(y2 - y1) / length, (x2 - x1) / length     # the left of x1 -> x2 
        side = -b0                                           # b0 < 0: the left 
        body = [(x1, y1), (x2, y2), (x2 + nx * side, y2 + ny * side), (x1 + nx * side, y1 + ny * side)] 
        ax.add_patch(Polygon(body, closed=True, facecolor='#2c5f8a', alpha=0.35)) 
        ax.text((x1 + x2) / 2 + nx * side / 2, (y1 + y2) / 2 + ny * side / 2, 
                'B %d' % b['number'], fontsize=7, ha='center', va='center') 
    for c in level['columns']: 
        for o in c['orthos']: 
            ax.add_patch(Polygon([(p['x'], p['y']) for p in o['coords']], closed=True, 
                                 facecolor='white', edgecolor='#a11b1b')) 
        first = c['orthos'][0]['coords'][2] 
        ax.text(first['x'] + 0.05, first['y'] + 0.05, 'K%d' % c['number'], color='#a11b1b', fontsize=8) 
    ax.set_aspect('equal') 
    ax.autoscale_view() 
 
name = autostruct.level() 
path = autostruct.project_dir() / 'designs' / 'json' / (name + '.json') 
document = json.load(open(path, encoding='utf-8', errors='replace')) 
fig, ax = plt.subplots() 
plot_level(document, ax) 
fig.savefig(autostruct.project_dir() / (name + '-plan.pdf'))

PIC

Figure 3: A level drawn by plot_level from its level document: one bay of four columns, four beams drawn on their outer faces with their bodies inward, and a slab. A beam’s body is on the side its b0 names.

7 Scripts the assistant writes

The assistant writes scripts with this module, and the person decides whether to run them — a script run from the Python tab or a notebook is the person’s own act. When it is asked to run a script itself and correct it from its errors, the script may only read: the level documents, the database, and commands that read. Using the assistant describes that, and what is handed back when a script would change the project.

7.1 Leaving the answer in the form asked

A script that answers a question leaves the answer in a variable named result, and the answer is judged by what is in result, not by what was printed. Its form is the question’s:

the question says

result is

“two decimals”, “one decimal”

round(x, 2), round(x, 1): the number rounded, not the number printed rounded

“a whole number”

round(x), never int(x), which cuts towards zero: int(-206.6) is − 206, round(-206.6) is − 207

“as a pair”, “(start, end)”

a tuple of two, (a, b); not a list holding one tuple, [(a, b)]

“its number”

the number alone, 11; not the label, not a list of one

“list … sorted”

a Python list, sorted(...), one item per element

“in kN”, “in kNm”

the database’s N and Nm divided by 1000

“in size”, “the largest in size”

compared by abs(), and the value answered is that size, abs() too; a force asked for without “in size” keeps its sign

print(f’{n:.2f}’)        # shows 4.70 ...
result = n               # ... but leaves 4.703435605884323: wrong
result = round(n, 2)     # 4.7: right

A trap. Read the quantity off the question, not off the last script: “My” is the column My_a/My_b, “Mz” is Mz_a/Mz_b, and a script that answered one does not answer the other. Which moment bends about which axis is in The frame’s results.

8 What a script must not assume

  1. That an answer is a number or a list. It is text; convert it, or split it with Tcl (§3.1).
  2. That status()[’levels’] names the levels. It counts them.
  3. That the board in front is Design. Name it before drawing; after a design it is Draft.
  4. That an acting command answers its result. save, level, tab and their kind answer milliseconds.
  5. That the working folder is the project. Build paths from project_dir().
  6. That printing shows progress. Output appears when the script ends.
  7. That a refusal can be ignored. Catch the ones you expect; let the rest stop the script.
  8. That the frame’s results are still there. After any level’s Create Model they are gone until the frame is solved again (The project’s data).

References

Hunter, J. D. (2007). “Matplotlib: A 2D Graphics Environment”. In: Computing in Science & Engineering 9.3, pp. 90–95. doi: 10.1109/MCSE.2007.55.

Tcl Core Team (2026a). join — Create a string by joining together list elements. Tcl/Tk 8.6 Manual. url: https://www.tcl-lang.org/man/tcl8.6/TclCmd/join.htm (visited on 10/24/2026).

Tcl Core Team (2026b). list — Create a list. Tcl/Tk 8.6 Manual. url: https://www.tcl-lang.org/man/tcl8.6/TclCmd/list.htm (visited on 10/24/2026).