Skip to content

Run a property-copy workflow from a GUI

A property-copy tool collects elements, source properties, and target properties in its interface. The shell selects the project; the script captures the inputs and runs a read/write operation in the background.

The excerpts below adapt an established NiceGUI property-copy workflow. They show the important boundaries rather than a new standalone application. Property-picker widgets, view rendering, and result dialogs are omitted.

Start the manager when the UI is ready

Create one MultiConn for the application shell after NiceGUI starts its event loop. Dispatch connection events back to that loop:

import asyncio

from multiconn_archicad import MultiConn


def create_conn() -> MultiConn:
    loop = asyncio.get_running_loop()
    return MultiConn(ui_mode=True, dispatcher=loop.call_soon_threadsafe)

Pass conn to the shell. Its project selector changes conn.primary. The script view receives the selected ConnHeader, not a new connection manager.

Rebuild project-specific inputs when context changes

The shell subscribes after creating its views. This excerpt takes the shell's existing rebuild_view method as a callback:

from collections.abc import Callable

from multiconn_archicad import ConnHeader, MultiConn


def start_project_updates(
    conn: MultiConn,
    rebuild_view: Callable[[ConnHeader | None], None],
) -> Callable[[], None]:
    def selected(previous, current):
        rebuild_view(conn.primary)

    def metadata_ready(header):
        if header is conn.primary:
            rebuild_view(header)

    stop_selection = conn.events.subscribe_primary_changed(selected)
    stop_metadata = conn.events.subscribe_metadata_resolved(metadata_ready)
    conn.refresh.all_ports()

    def close() -> None:
        stop_selection()
        stop_metadata()

    return close

rebuild_view clears old element/property choices and rebuilds the script view for the current header. The callback re-reads conn.primary, because a queued event may describe an intermediate selection. Metadata resolution can still represent a failed read; enable Run only after checking session readiness and Tapir installation.

Register the returned cleanup function with the view's deletion lifecycle, such as NiceGUI's ui.context.client.on_delete. Keep user settings that are independent of the project, but refresh project-derived IDs and query catalogues.

Capture the form's inputs

The UI owns picker widgets; the operation receives their values. A small Pydantic record makes those inputs explicit:

from pydantic import BaseModel, Field

from multiconn_archicad.models.tapir.types import ElementIdArrayItem, PropertyIdArrayItem


class CopySettings(BaseModel):
    elements: list[ElementIdArrayItem] = Field(min_length=1)
    source_properties: list[PropertyIdArrayItem] = Field(min_length=1)
    target_properties: list[PropertyIdArrayItem] = Field(min_length=1)

Build this record from the element and property pickers. Source and target properties are paired by their positions. The next operation overwrites target property cells where the source read succeeds. Unreadable sources are omitted; successful writes are not rolled back if another write fails.

Read, copy, and retain the results

The operation uses an API client and the captured settings. It has no widget or window dependencies:

from multiconn_archicad import UnifiedApi
from multiconn_archicad.utilities import BatchReport, PopulationResults


def copy_values(api: UnifiedApi, settings: CopySettings) -> BatchReport[ElementIdArrayItem]:
    if len(settings.source_properties) != len(settings.target_properties):
        raise ValueError("Pair each source property with one target property.")

    population = PopulationResults(settings.elements)
    properties = api.utilities.property
    read = population.record(
        "Read source properties",
        properties.get_property_values_per_element_result(settings.elements, settings.source_properties),
    )
    write = properties.set_property_values_per_element_sparse_result(
        settings.elements, settings.target_properties, read
    )
    population.record("Write target properties", write)
    return population.snapshot(completed=True)

This follows the read/optional-transform/write structure of the property-copy workflow. A transformation, when needed, belongs between the read and write and should preserve the result positions. The example copies values directly. See batch results for skipped cells and failures.

Run outside the UI thread

The script entry point checks the header and passes its client to the operation. The async view callback copies the settings before handing work to NiceGUI's worker helper:

from nicegui import run

from multiconn_archicad import ConnHeader, is_session_ready


def run_copy(header: ConnHeader, settings: CopySettings) -> BatchReport[ElementIdArrayItem]:
    if not is_session_ready(header):
        raise RuntimeError("Wait for a ready Archicad project.")
    if not header.tapir_info.is_installed:
        raise RuntimeError("Install Tapir in the selected Archicad instance.")
    return copy_values(header.unified, settings)


async def copy_from_view(header: ConnHeader, settings: CopySettings) -> BatchReport[ElementIdArrayItem]:
    captured = settings.model_copy(deep=True)
    return await run.io_bound(run_copy, header, captured)

No latest-Tapir requirement is imposed. If an added command needs a particular version, check that command's minimum at the script boundary.

The view disables Run and project switching while this work is active, then shows the returned report. For example, a summary may show 19 succeeded, 1 failed. The loop reaching its end does not mean every element succeeded. If your shell allows selection changes during a run, discard results belonging to an older selection before updating the display.