← All Pull Requests

AdomFloat: parse ASCII dot-floats without locale-sensitive StrToFloat #4

Closed opened by Carlos.v Ferri Pereira

altium-bridge 0.2.91 · Altium Designer Professional 25.3.3 (build 25.3.3.23) · Adom Bridge 2.1.279 · Windows 11 (Portuguese/Brazil locale, HKCU\Control Panel\International sDecimal=,)

Summary

On Windows machines whose regional setting uses a comma as the decimal separator (sDecimal = ,, e.g. Portuguese, Spanish, German, French locales), AdomFloat in src/agent/AdomAgent.pas.tmpl validates that the caller string uses an ASCII dot (AdomIsNumber(T, True)) and then calls DelphiScript's StrToFloat(T):

Function AdomFloat(S : String) : Double;
Var
    T : String;
Begin
    Result := 0;
    T := Trim(S);
    If AdomIsNumber(T, True) Then Result := StrToFloat(T)
    Else If gInputError = '' Then gInputError := T;
End;

Because Delphi's StrToFloat uses the OS locale's DecimalSeparator (,), passing '-0.9100' from a .fp.spec in build_footprint (or any other float parameter) raises:

EConvertError
'-0.9100' is not a valid floating point value

Note that line 5896 of AdomAgent.pas.tmpl already noted the output half of this exact locale hazard ({ micrometres as integers: FloatToStr writes a comma decimal on some Windows locales, which breaks JSON }), while AdomFloat on the input side still called StrToFloat(T).

Fix

Parse the validated ASCII sign, integer digits, and . fractional digits directly in AdomFloat (Ord(C) - 48) instead of calling locale-dependent StrToFloat(T).

Workaround verified on the live machine

Setting HKCU\Control Panel\International sDecimal to . (and sThousand to ,) and broadcasting WM_SETTINGCHANGE ("intl") updates Delphi's DecimalSeparator inside the running Altium process immediately without restarting Altium.

Diff Skip to comments (1)

--- a/src/agent/AdomAgent.pas.tmpl+++ b/src/agent/AdomAgent.pas.tmpl@@ -1,10840 +1,10862 @@⋯ 68 unchanged lines ⋯  Function AdomFloat(S : String) : Double; Var-    T : String;-Begin-    Result := 0;-    T := Trim(S);-    If AdomIsNumber(T, True) Then Result := StrToFloat(T)-    Else If gInputError = '' Then gInputError := T;-End;--Function AdomInt(S : String) : Integer;-Var-    T : String;-Begin-    Result := 0;-    T := Trim(S);-    If AdomIsNumber(T, False) Then Result := StrToInt(T)-    Else If gInputError = '' Then gInputError := T;-End;--{ ---------------------------------------------------------------- files ---- }--Function ReadAll(Path : String) : String;-Var-    L : TStringList;-    K, H : Integer;-Begin-    Result := '';-    { NEVER let LoadFromFile raise: the script debugger breaks on ANY exception-      even inside Try/Except and draws an Error modal (EFOpenError "being used-      by another process" when the bridge is mid-write, measured 2026-08-30).-      Probe with FileOpen, which returns -1 instead of raising, until the file-      is readable, then load. }-    For K := 1 To 100 Do-    Begin-        H := FileOpen(Path, fmOpenRead Or fmShareDenyNone);-        If H >= 0 Then-        Begin-            FileClose(H);-            Break;-        End;-        Sleep(20);-    End;-    If H < 0 Then Exit;-    L := TStringList.Create;-    Try-        L.LoadFromFile(Path);-        Result := L.Text;-    Finally-        L.Free;-    End;-End;--Procedure WriteAtomic(Path : String; Content : String);-Var-    L    : TStringList;-    Tmp  : String;-    Done : Boolean;-    K, TA, TB, TC : Integer;-Begin-    Tmp := Path + '.tmp';-    L := TStringList.Create;-    Try-        L.Text := Content;-        TA := GetTickCount;-        gLastSaveTries := 0;-        gLastSaveExcept := 0;-        Done := False;-        K := 0;-        { NOTE: never `Break` out of a Try..Except here - this engine implements Break as an-          exception, the Except swallows it, and the loop runs all 20 iterations (~220 ms). }-        While (Not Done) And (K < 20) Do-        Begin-            K := K + 1;-            gLastSaveTries := K;-            Try-                L.SaveToFile(Tmp);-                Done := True;-            Except-                gLastSaveExcept := gLastSaveExcept + 1;-                Sleep(10);-            End;-        End;-        TB := GetTickCount;-    Finally-        L.Free;-    End;-    If FileExists(Path) Then DeleteFile(Path);-    TC := GetTickCount;-    RenameFile(Tmp, Path);-    gLastSaveMs := TB - TA;-    gLastDeleteMs := TC - TB;-    gLastRenameMs := GetTickCount - TC;-End;--{ Direct write, no tmp+rename (A/B against WriteAtomic for the response path). Readers tolerate a-  partially written file by retrying the JSON parse. }-Procedure WriteDirect(Path : String; Content : String);-Var-    L    : TStringList;-    Done : Boolean;-    K    : Integer;-    T0   : Integer;-Begin-    L := TStringList.Create;-    Try-        L.Text := Content;-        T0 := GetTickCount;-        gDirectTries := 0;-        gDirectExcept := 0;-        Done := False;-        K := 0;-        While (Not Done) And (K < 20) Do-        Begin-            K := K + 1;-            gDirectTries := K;-            Try-                L.SaveToFile(Path);-                Done := True;-            Except-                gDirectExcept := gDirectExcept + 1;-                Sleep(10);-            End;-        End;-        gDirectMs := GetTickCount - T0;-    Finally-        L.Free;-    End;-End;--{ Runs of plain characters are copied whole: appending one character at a time is quadratic in-  DelphiScript, and one part's long parameter (a Mini PC example IC) took minutes to escape (2026-10-21). }-Function JsonEsc(S : String) : String;-Var-    I, Start, N : Integer;-    C : Char;-    E : String;-Begin-    Result := '';-    N := Length(S);-    Start := 1;-    For I := 1 To N Do-    Begin-        C := S[I];-        E := '';-        If C = '"' Then E := '\"'-        Else If C = '\' Then E := '\\'-        Else If C = #13 Then E := '\r'-        Else If C = #10 Then E := '\n'-        Else If C = #9 Then E := '\t'-        Else If Ord(C) < 32 Then E := ' ';-        If E <> '' Then-        Begin-            If I > Start Then Result := Result + Copy(S, Start, I - Start);-            Result := Result + E;-            Start := I + 1;-        End;-    End;-    If Start <= N Then Result := Result + Copy(S, Start, N - Start + 1);-End;--Function JStr(Key : String; Val : String) : String;-Begin-    Result := '"' + Key + '":"' + JsonEsc(Val) + '"';-End;--Function JRaw(Key : String; Val : String) : String;-Begin-    Result := '"' + Key + '":' + Val;-End;--Function JBool(B : Boolean) : String;-Begin-    If B Then Result := 'true' Else Result := 'false';-End;--{ An edit made through the scripting API does not mark a schematic document as changed (no * in the title, 2026-10-27):-  a save:false edit then looked unchanged and was lost when the document closed. Mark it after every edit. }-Function BaseNameOf(S : String) : String;-Var-    I : Integer;-Begin-    Result := S;-    For I := Length(S) DownTo 1 Do-        If (S[I] = '\') Or (S[I] = '/') Then Begin Result := Copy(S, I + 1, Length(S) - I); Exit; End;-End;--Function MarkSchModified(Doc : ISch_Document) : String;-Var-    SM : IServerModule;-    D  : IServerDocument;-    I, J : Integer;-    Nm : String;-Begin-    { by file name: a sheet's DocumentName and its server document's FileName are not always the same form, and the-      agent's own script view is the "current view" while it runs }-    Result := '';-    Try-        Nm := UpperCase(BaseNameOf(Doc.DocumentName));-        Result := Nm + ':';-        For I := 0 To Client.Count - 1 Do-        Begin-            SM := Client.ServerModule[I];-            If SM = Nil Then Continue;-            For J := 0 To SM.DocumentCount - 1 Do-            Begin-                D := SM.Documents[J];-                If (D <> Nil) And (UpperCase(BaseNameOf(D.FileName)) = Nm) Then Begin D.Modified := True; Result := Result + 'set;'; End;-            End;-        End;-    Except-        Result := Result + 'raised';-    End;-End;----{ Close a dangling PCBServer transaction. A command that halted between-  PreProcess and PostProcess leaves Altium in "a command is currently active",-  and the next save draws the save-a-copy Warning. Measured 2026-08-30: an-  unbalanced PostProcess is harmless and clears it. }-Procedure TxClear;-Begin-    Try-        PCBServer.PostProcess;-    Except-    End;-End;--{ hb.txt MUST be written tmp+rename: readers (the bridge's is_alive poll) hold the final path-  open, so a direct SaveToFile onto it raises EFCreateError and halts the engine with a modal.-  Response files use WriteDirect - a fresh unique name nobody has open yet. }-Procedure Heartbeat(Force : Boolean);-Var-    T : Integer;-Begin-    T := GetTickCount;-    If Force Or ((T - gLastHbTick) >= HEARTBEAT_MS) Then-    Begin-        Try-            WriteAtomic(WORKSPACE + 'hb.txt', IntToStr(T) + #13#10 + 'requests=' + IntToStr(gRequests) + #13#10 + 'ticks=' + IntToStr(gTicks) + #13#10 + 'version=' + AGENT_VERSION + #13#10 + 'gen=' + AGENT_GEN);-        Except-        End;-        gLastHbTick := T;-    End;-End;--{ --------------------------------------------------------- request parse --- }--{ Requests are "key=value" lines.  Param(Lines, 'command') etc. }-Function Param(Lines : TStringList; Key : String) : String;-Var-    I : Integer;-    L : String;-Begin-    Result := '';-    For I := 0 To Lines.Count - 1 Do-    Begin-        L := Lines[I];-        If Pos(Key + '=', L) = 1 Then-        Begin-            Result := Copy(L, Length(Key) + 2, Length(L));-            Break;-        End;-    End;-End;--{ ------------------------------------------------------------- handlers --- }--Function CmdPing(Lines : TStringList) : String;-Begin-    Result := '{' + JRaw('ok', 'true') + ',' + JStr('agent', AGENT_VERSION) + ',' + JRaw('requests', IntToStr(gRequests)) + ',' + JRaw('tick', IntToStr(GetTickCount)) + '}';-End;--{ Every open document in the workspace + which one has focus. }-Function CmdState(Lines : TStringList) : String;-Var-    WS   : IWorkspace;-    Prj  : IProject;-    Doc  : IDocument;-    I, J : Integer;-    Docs : String;-    Focused : String;-    PrjName : String;-Begin-    Docs := '';-    Focused := '';-    PrjName := '';-    WS := GetWorkspace;-    If WS <> Nil Then-    Begin-        Try-            Doc := WS.DM_FocusedDocument;-            If Doc <> Nil Then Focused := Doc.DM_FullPath;-        Except-        End;-        Try-            Prj := WS.DM_FocusedProject;-            If Prj <> Nil Then PrjName := Prj.DM_ProjectFullPath;-        Except-        End;-        For I := 0 To WS.DM_ProjectCount - 1 Do-        Begin-            Prj := WS.DM_Projects(I);-            For J := 0 To Prj.DM_LogicalDocumentCount - 1 Do-            Begin-                Doc := Prj.DM_LogicalDocuments(J);-                If Docs <> '' Then Docs := Docs + ',';-                Docs := Docs + '{' + JStr('path', Doc.DM_FullPath) + ',' + JStr('kind', Doc.DM_DocumentKind) + ',' + JStr('project', Prj.DM_ProjectFileName) + ',' + JRaw('open', JBool(Doc.DM_DocumentIsLoaded)) + '}';-            End;-        End;-    End;-    Result := '{' + JRaw('ok', 'true') + ',' + JStr('focusedDocument', Focused) + ',' + JStr('focusedProject', PrjName) + ',' + JRaw('documents', '[' + Docs + ']') + '}';-End;--{ Run any server process from the 1,109-entry catalog: command=PCB:Zoom  params=Action=All|... }-Function CmdRunProcess(Lines : TStringList) : String;-Var-    Cmd, P, Rest, Pair : String;-    K : Integer;-Begin-    Cmd := Param(Lines, 'process');-    P := Param(Lines, 'params');-    If Cmd = '' Then-    Begin-        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'process is required, e.g. PCB:Zoom') + '}';-        Exit;-    End;-    { RunProcessWithParameters halted the debugger at this line on AD 26.9.1-      (2026-08-29) — the supported form is ResetParameters + one-      AddStringParameter per Name=Value pair, then RunProcess(Command). }-    ResetParameters;-    Rest := P;-    While Rest <> '' Do-    Begin-        K := Pos('|', Rest);-        If K > 0 Then-        Begin-            Pair := Copy(Rest, 1, K - 1);-            Rest := Copy(Rest, K + 1, Length(Rest));-        End-        Else-        Begin-            Pair := Rest;-            Rest := '';-        End;-        K := Pos('=', Pair);-        If K > 0 Then AddStringParameter(Copy(Pair, 1, K - 1), Copy(Pair, K + 1, Length(Pair)));-    End;-    RunProcess(Cmd);-    Result := '{' + JRaw('ok', 'true') + ',' + JStr('process', Cmd) + ',' + JStr('params', P) + '}';-End;--{ a path in one spelling, to compare two: backslashes, upper case }-Function SamePathText(P : String) : String;-Var-    K : Integer;-Begin-    Result := '';-    For K := 1 To Length(P) Do-        If Copy(P, K, 1) = '/' Then Result := Result + '\' Else Result := Result + Copy(P, K, 1);-    Result := UpperCase(Result);-End;--{ The document open at Path under any spelling of it (C:/x/a.PcbLib is C:\x\a.PcbLib), Nil when none. GetDocumentByPath-  matches the spelling exactly, so a second open made Altium hold a second copy of the file, and an edit on one copy-  was never seen by a read of the other (three copies of Battery.PcbLib, 2026-10-30). }-Function DocAt(Path : String) : IServerDocument;-Var-    SM   : IServerModule;-    D    : IServerDocument;-    I, J : Integer;-Begin-    Result := Nil;-    Try Result := Client.GetDocumentByPath(Path); Except Result := Nil; End;-    If Result <> Nil Then Exit;-    For I := 0 To Client.Count - 1 Do-    Begin-        SM := Client.ServerModule[I];-        If SM = Nil Then Continue;-        For J := 0 To SM.DocumentCount - 1 Do-        Begin-            D := SM.Documents[J];-            If (D <> Nil) And (Result = Nil) Then-                If SamePathText(D.FileName) = SamePathText(Path) Then Result := D;-        End;-    End;-End;--{ Open a document or project by path. Client.OpenDocument('', path) returns Nil on-  AD 26.9.1 (measured), so go through the WorkspaceManager process launcher, then-  look the document up and show it. .PrjPcb/.PrjScr/.PrjMbd open as a Project. }-Function CmdOpen(Lines : TStringList) : String;-Var-    Path, Ext, Kind : String;-    Doc  : IServerDocument;-    K    : Integer;-Begin-    Path := Param(Lines, 'path');-    If Not FileExists(Path) Then-    Begin-        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'file not found: ' + Path) + '}';-        Exit;-    End;-    Ext := '';-    K := Length(Path);-    While (K > 0) And (Path[K] <> '.') Do K := K - 1;-    If K > 0 Then Ext := UpperCase(Copy(Path, K + 1, Length(Path)));-    { Altium wants the DOCUMENT KIND, not a generic word. Passing ObjectKind='Document' makes it-      answer "File type not recognised." with a modal on the user's screen (measured twice on-      2026-09-01) even for a perfectly valid .PcbDoc. Map the extension to the kind Altium uses,-      and open through Client.OpenDocument — the sibling of ShowDocument, which this unit already-      relies on — falling back to the OpenObject process only if that raises. }-    Kind := '';-    If (Ext = 'PRJPCB') Or (Ext = 'PRJSCR') Or (Ext = 'PRJMBD') Or (Ext = 'PRJHAR') Or (Ext = 'LIBPKG') Then-        Kind := 'Project'-    Else If Ext = 'PCBDOC' Then Kind := 'PCB'-    Else If Ext = 'SCHDOC' Then Kind := 'SCH'-    Else If Ext = 'PCBLIB' Then Kind := 'PCBLIB'-    Else If Ext = 'SCHLIB' Then Kind := 'SCHLIB'-    Else If Ext = 'OUTJOB' Then Kind := 'OUTPUTJOB'-    Else If Ext = 'PCB3DLIB' Then Kind := 'PCB3DLIB'-    Else If (Ext = 'TXT') Or (Ext = 'PAS') Or (Ext = 'REP') Then Kind := 'TEXT';-    If Kind = '' Then-    Begin-        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'unsupported document type: .' + Ext) + ',' +-            JStr('supported', 'PcbDoc, SchDoc, PcbLib, SchLib, OutJob, PrjPcb, PrjScr, txt/pas/rep') + '}';-        Exit;-    End;-    If Kind <> 'Project' Then-    Begin-        { open under another spelling of the path, it is this file: open it by ITS spelling, and Altium answers that-          document rather than a second copy. OpenDocument still runs: showing the found document without it left no-          current board, and DRC on it crashed in ADVPCB.DLL (two batteries, 2026-10-30) }-        Doc := DocAt(Path);-        If Doc <> Nil Then Path := Doc.FileName;-        Doc := Nil;-        Try-            Doc := Client.OpenDocument(Kind, Path);-        Except-            Doc := Nil;-        End;-        If Doc <> Nil Then-        Begin-            Try-                Client.ShowDocumentDontFocus(Doc);-            Except-                Client.ShowDocument(Doc);-            End;-            Result := '{' + JRaw('ok', 'true') + ',' + JStr('path', Path) + ',' + JStr('objectKind', Kind) + ',' +-                JStr('kind', Doc.Kind) + ',' + JRaw('shown', 'true') + ',' + JStr('lane', 'OpenDocument') + '}';-            Exit;-        End;-    End;-    { fallback: the process lane, with the mapped kind rather than a generic one }-    ResetParameters;-    AddStringParameter('ObjectKind', Kind);-    AddStringParameter('FileName', Path);-    RunProcess('WorkspaceManager:OpenObject');-    Doc := Nil;-    Try-        Doc := Client.GetDocumentByPath(Path);-    Except-    End;-    If Doc <> Nil Then-    Begin-        Try-            Client.ShowDocumentDontFocus(Doc);-        Except-            Client.ShowDocument(Doc);-        End;-        Result := '{' + JRaw('ok', 'true') + ',' + JStr('path', Path) + ',' + JStr('objectKind', Kind) + ',' + JStr('kind', Doc.Kind) + ',' + JRaw('shown', 'true') + '}';-    End-    Else-        Result := '{' + JRaw('ok', 'true') + ',' + JStr('path', Path) + ',' + JStr('objectKind', Kind) + ',' + JRaw('shown', 'false') + ',' + JStr('note', 'OpenObject ran; document handle not resolved (projects have no document view)') + '}';-End;--{ Close ONE open document by path, never saving (a caller who wants a save saves first). Goes-  through the document HANDLE: the menu processes are broken for this on ADDevelop - RunProcess-  Client:CloseDocument no-ops, and Client:CloseAllOpenDocuments unregisters documents while-  their views stay open (both measured 2026-09-02, adom/altium-bridge issue 7). }-{ Before a NEW document is named onto Path: close any document already open at that path, unless it has changes.-  Altium holds a second document at the same path otherwise, and AD 26 (which reopens its last session) then crashed-  in ADVPCB.DLL when DRC ran on the battery's board (measured 2026-10-27, twice). }-Procedure FreePath(Path : String);-Var-    SM : IServerModule;-    D  : IServerDocument;-    I, J : Integer;-    Changed : Boolean;-Begin-    If Path = '' Then Exit;-    For I := 0 To Client.Count - 1 Do-    Begin-        SM := Client.ServerModule[I];-        If SM = Nil Then Continue;-        For J := SM.DocumentCount - 1 DownTo 0 Do-        Begin-            D := SM.Documents[J];-            If (D = Nil) Or (SamePathText(D.FileName) <> SamePathText(Path)) Then Continue;-            Changed := True;-            Try Changed := D.Modified; Except End;-            If Not Changed Then Try Client.CloseDocument(D); Except End;-        End;-    End;-End;--Function CmdCloseDocument(Lines : TStringList) : String;-Var-    Path : String;-    Doc, D : IServerDocument;-    SM   : IServerModule;-    I, J : Integer;-Begin-    Path := Param(Lines, 'path');-    Doc := Nil;-    Try-        Doc := Client.GetDocumentByPath(Path);-    Except-    End;-    { a document made in memory and saved under a name afterwards (altium_panelize's panel) is not found by path on-      Develop (2026-10-26): walk every server's documents by file name (the bridge sends backslashes) }-    If Doc = Nil Then-    Begin-        For I := 0 To Client.Count - 1 Do-        Begin-            SM := Client.ServerModule[I];-            If SM = Nil Then Continue;-            For J := 0 To SM.DocumentCount - 1 Do-            Begin-                D := SM.Documents[J];-                If (D <> Nil) And (SamePathText(D.FileName) = SamePathText(Path)) Then Doc := D;-            End;-        End;-    End;-    If Doc = Nil Then-    Begin-        Result := '{' + JRaw('ok', 'true') + ',' + JRaw('closed', 'false') + ',' + JStr('note', 'not open: ' + Path) + '}';-        Exit;-    End;-    Try-        Client.CloseDocument(Doc);-        Result := '{' + JRaw('ok', 'true') + ',' + JRaw('closed', 'true') + ',' + JStr('path', Path) + '}';-    Except-        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'CloseDocument raised for ' + Path) + '}';-    End;-End;--{ Inline statements: RunScriptText from INSIDE the agent (measured separately;-  if the engine refuses nested scripts this returns the error in-band). }-Function CmdEval(Lines : TStringList; Payload : String) : String;-Var-    Text : String;-Begin-    Text := Payload;-    If Text = '' Then Text := Param(Lines, 'text');-    If Text = '' Then-    Begin-        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'text (statements only, Begin ... End;) is required') + '}';-        Exit;-    End;-    ResetParameters;-    AddStringParameter('Text', Text);-    RunProcess('ScriptingSystem:RunScriptText');-    Result := '{' + JRaw('ok', 'true') + ',' + JStr('note', 'RunScriptText dispatched; an optional Var block, then Begin ... End;; capture output by writing a file') + '}';-End;---{ ------------------------------------------------------------ libraries --- }--{ List components of the CURRENT library document (open it first). kind=sch|pcb }-Function CmdLibList(Lines : TStringList) : String;-Var-    Kind  : String;-    SLib  : ISch_Lib;-    Iter  : ISch_Iterator;-    Comp  : ISch_Component;-    PLib  : IPCB_Library;-    I, N  : Integer;-    Names : String;-    Limit : Integer;-Begin-    Kind := Param(Lines, 'kind');-    Limit := StrToIntDef(Param(Lines, 'limit'), 200);-    Names := '';-    N := 0;-    If Kind = 'sch' Then-    Begin-        SLib := SchServer.GetCurrentSchDocument;-        If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then-        Begin-            Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'current document is not a schematic library') + '}';-            Exit;-        End;-        Iter := SLib.SchLibIterator_Create;-        Iter.AddFilter_ObjectSet(MkSet(eSchComponent));-        Comp := Iter.FirstSchObject;-        While Comp <> Nil Do-        Begin-            If N < Limit Then-            Begin-                If Names <> '' Then Names := Names + ',';-                Names := Names + '"' + JsonEsc(Comp.LibReference) + '"';-            End;-            N := N + 1;-            Comp := Iter.NextSchObject;-        End;-        SLib.SchIterator_Destroy(Iter);-    End-    Else-    Begin-        PLib := PCBServer.GetCurrentPCBLibrary;-        If PLib = Nil Then-        Begin-            Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'current document is not a PCB library') + '}';-            Exit;-        End;-        For I := 0 To PLib.ComponentCount - 1 Do-        Begin-            If N < Limit Then-            Begin-                If Names <> '' Then Names := Names + ',';-                Names := Names + '"' + JsonEsc(PLib.GetComponent(I).Name) + '"';-            End;-            N := N + 1;-        End;-    End;-    Result := '{' + JRaw('ok', 'true') + ',' + JStr('kind', Kind) + ',' + JRaw('count', IntToStr(N)) + ',' + JRaw('names', '[' + Names + ']') + '}';-End;--{ Make one component current in the open library editor. kind=sch|pcb name=<LibReference|footprint>-  (name '' = the first one), then zoom all. }-Function CmdLibSelect(Lines : TStringList) : String;-Var-    Kind, Name, Found : String;-    SLib  : ISch_Lib;-    Iter  : ISch_Iterator;-    Comp  : ISch_Component;-    PLib  : IPCB_Library;-    PComp : IPCB_LibComponent;-    I     : Integer;-Begin-    Kind := Param(Lines, 'kind');-    Name := Param(Lines, 'name');-    Found := '';-    If Kind = 'sch' Then-    Begin-        SLib := SchServer.GetCurrentSchDocument;-        If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then-        Begin-            Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'current document is not a schematic library') + '}';-            Exit;-        End;-        Iter := SLib.SchLibIterator_Create;-        Iter.AddFilter_ObjectSet(MkSet(eSchComponent));-        Comp := Iter.FirstSchObject;-        While Comp <> Nil Do-        Begin-            If (Name = '') Or (UpperCase(Comp.LibReference) = UpperCase(Name)) Then-            Begin-                SLib.CurrentSchComponent := Comp;-                Found := Comp.LibReference;-                Break;-            End;-            Comp := Iter.NextSchObject;-        End;-        SLib.SchIterator_Destroy(Iter);-        If Found <> '' Then-        Begin-            SLib.GraphicallyInvalidate;-            ResetParameters;-            AddStringParameter('Action', 'All');-            RunProcess('Sch:Zoom');-        End;-    End-    Else-    Begin-        PLib := PCBServer.GetCurrentPCBLibrary;-        If PLib = Nil Then-        Begin-            Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'current document is not a PCB library') + '}';-            Exit;-        End;-        For I := 0 To PLib.ComponentCount - 1 Do-        Begin-            PComp := PLib.GetComponent(I);-            If (Name = '') Or (UpperCase(PComp.Name) = UpperCase(Name)) Then-            Begin-                PLib.CurrentComponent := PComp;-                Found := PComp.Name;-                Break;-            End;-        End;-        If Found <> '' Then-        Begin-            PLib.RefreshView;-            ResetParameters;-            AddStringParameter('Action', 'All');-            RunProcess('PCB:Zoom');-        End;-    End;-    If Found = '' Then-        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'component not found: ' + Name) + ',' + JStr('hint', 'lib_list to see names') + '}'-    Else-        Result := '{' + JRaw('ok', 'true') + ',' + JStr('kind', Kind) + ',' + JStr('selected', Found) + '}';-End;--{ ------------------------------------------------------------- outputs --- }--{ Compile the focused project (= ERC for schematics) and return the Messages panel.-  IMessagesManager (Workspace Manager API): MessagesCount, Messages(i) -> IMessageItem-  with MsgClass, Text, Source, Document. DM_MessageCount does not exist (measured).-  NEVER nest braces inside a brace comment - the inner one ends the comment early-  and the rest becomes code ("Error in declaration block"). }-Function CmdCompile(Lines : TStringList) : String;-Var-    WS   : IWorkspace;-    MM   : IDMMessagesManager;-    I, N : Integer;-    Msgs : String;-    Kind : String;-    Limit: Integer;-Begin-    Kind := Param(Lines, 'objectKind');-    If Kind = '' Then Kind := 'Project';-    Limit := StrToIntDef(Param(Lines, 'limit'), 200);-    ResetParameters;-    AddStringParameter('Action', 'Compile');-    AddStringParameter('ObjectKind', Kind);-    RunProcess('WorkspaceManager:Compile');-    Msgs := '';-    N := 0;-    WS := GetWorkspace;-    If WS <> Nil Then-    Begin-        MM := WS.DM_MessagesManager;-        If MM <> Nil Then-        Begin-            N := MM.MessagesCount;-            For I := 0 To N - 1 Do-            Begin-                If I >= Limit Then Break;-                If Msgs <> '' Then Msgs := Msgs + ',';-                Msgs := Msgs + '{' + JStr('class', MM.Messages(I).MsgClass) + ',' + JStr('source', MM.Messages(I).Source) + ',' + JStr('document', MM.Messages(I).Document) + ',' + JStr('text', MM.Messages(I).Text) + '}';-            End;-        End;-    End;-    Result := '{' + JRaw('ok', 'true') + ',' + JStr('objectKind', Kind) + ',' + JRaw('messageCount', IntToStr(N)) + ',' + JRaw('messages', '[' + Msgs + ']') + '}';-End;--{ messages: the Messages panel as it stands, compiling nothing (log_tail reads it). limit= caps the count. }-Function CmdMessages(Lines : TStringList) : String;-Var-    WS   : IWorkspace;-    MM   : IDMMessagesManager;-    I, N, Limit : Integer;-    Msgs : String;-Begin-    Limit := StrToIntDef(Param(Lines, 'limit'), 2000);-    Msgs := ''; N := 0;-    WS := GetWorkspace;-    If WS <> Nil Then-    Begin-        MM := WS.DM_MessagesManager;-        If MM <> Nil Then-        Begin-            N := MM.MessagesCount;-            For I := 0 To N - 1 Do-            Begin-                If I >= Limit Then Break;-                If Msgs <> '' Then Msgs := Msgs + ',';-                Msgs := Msgs + '{' + JStr('class', MM.Messages(I).MsgClass) + ',' + JStr('source', MM.Messages(I).Source) + ',' + JStr('document', MM.Messages(I).Document) + ',' + JStr('text', MM.Messages(I).Text) + '}';-            End;-        End;-    End;-    Result := '{' + JRaw('ok', 'true') + ',' + JRaw('messageCount', IntToStr(N)) + ',' + JRaw('messages', '[' + Msgs + ']') + '}';-End;--{ DRC violations currently on the focused board (run PCB:DesignRuleCheck first). }-Function CmdViolations(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    Iter  : IPCB_BoardIterator;-    V     : IPCB_Violation;-    N     : Integer;-    Items : String;-    Limit : Integer;-Begin-    Limit := StrToIntDef(Param(Lines, 'limit'), 200);-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then-    Begin-        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'no PCB document is current') + '}';-        Exit;-    End;-    Items := '';-    N := 0;-    Iter := Board.BoardIterator_Create;-    Iter.AddFilter_ObjectSet(MkSet(eViolationObject));-    Iter.AddFilter_LayerSet(AllLayers);-    Iter.AddFilter_Method(eProcessAll);-    V := Iter.FirstPCBObject;-    While V <> Nil Do-    Begin-        If N < Limit Then-        Begin-            If Items <> '' Then Items := Items + ',';-            Items := Items + '{' + JStr('rule', V.Rule.Name) + ',' + JStr('description', V.Description) + '}';-        End;-        N := N + 1;-        V := Iter.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(Iter);-    Result := '{' + JRaw('ok', 'true') + ',' + JStr('board', Board.FileName) + ',' + JRaw('violationCount', IntToStr(N)) + ',' + JRaw('violations', '[' + Items + ']') + '}';-End;--{ Run an OutJob medium. path=<.OutJob>  medium=<OutputMedium name, e.g. Folder Structure> }-Function CmdOutJob(Lines : TStringList) : String;-Var-    Path, Medium, Kind : String;-    Doc : IServerDocument;-Begin-    Path := Param(Lines, 'path');-    Medium := Param(Lines, 'medium');-    Kind := Param(Lines, 'objectKind');-    If Kind = '' Then Kind := 'OutputBatch';-    If Not FileExists(Path) Then-    Begin-        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'OutJob not found: ' + Path) + '}';-        Exit;-    End;-    { the OutJob's own document kind: ObjectKind 'Document' is the generic word Altium answers with-      "File type not recognised" (see CmdOpen) }-    Doc := Nil;-    Try-        Doc := Client.OpenDocument('OUTPUTJOB', Path);-    Except-        Doc := Nil;-    End;-    If Doc = Nil Then-    Begin-        ResetParameters;-        AddStringParameter('ObjectKind', 'OUTPUTJOB');-        AddStringParameter('FileName', Path);-        RunProcess('WorkspaceManager:OpenObject');-        Doc := Client.GetDocumentByPath(Path);-    End;-    If Doc <> Nil Then Client.ShowDocument(Doc);-    ResetParameters;-    AddStringParameter('Action', 'Run');-    AddStringParameter('ObjectKind', Kind);-    If Medium <> '' Then AddStringParameter('OutputMedium', Medium);-    AddStringParameter('DisableDialog', 'True');-    RunProcess('WorkspaceManager:GenerateReport');-    Result := '{' + JRaw('ok', 'true') + ',' + JStr('outjob', Path) + ',' + JStr('medium', Medium) + ',' + JStr('objectKind', Kind) + '}';-End;--{ ------------------------------------------------------- library authoring --- }--{ Split "a;b;c" into a TStringList (one item per ';'). }-Function SplitSemi(S : String) : TStringList;-Var-    K : Integer;-    Part : String;-Begin-    Result := TStringList.Create;-    While S <> '' Do-    Begin-        K := Pos(';', S);-        If K > 0 Then-        Begin-            Part := Copy(S, 1, K - 1);-            S := Copy(S, K + 1, Length(S));-        End-        Else-        Begin-            Part := S;-            S := '';-        End;-        If Trim(Part) <> '' Then Result.Add(Trim(Part));-    End;-End;--{ Field N (1-based) of a comma list. }-Function Field(S : String; N : Integer) : String;-Var-    I, K : Integer;-Begin-    Result := '';-    For I := 1 To N Do-    Begin-        K := Pos(',', S);-        If K > 0 Then-        Begin-            Result := Copy(S, 1, K - 1);-            S := Copy(S, K + 1, Length(S));-        End-        Else-        Begin-            Result := S;-            S := '';-        End;-    End;-    Result := Trim(Result);-End;--{ Open an existing library or create + save a new one. kind=PCBLIB|SCHLIB  path=<file> }-Function EnsureLibrary(Kind : String; Path : String) : String;-Var-    Doc : IServerDocument;-Begin-    If FileExists(Path) Then-    Begin-        ResetParameters;-        AddStringParameter('ObjectKind', 'Document');-        AddStringParameter('FileName', Path);-        RunProcess('WorkspaceManager:OpenObject');-        Doc := Client.GetDocumentByPath(Path);-        If Doc <> Nil Then Client.ShowDocument(Doc);-        Result := 'opened';-    End-    Else-    Begin-        Doc := CreateNewDocumentFromDocumentKind(Kind);-        If Doc = Nil Then-        Begin-            Result := 'error: could not create ' + Kind;-            Exit;-        End;-        Client.ShowDocument(Doc);-        Doc.DoSafeChangeFileNameAndSave(Path, Kind);-        Result := 'created';-    End;-End;--{ create_footprint: name, libraryPath, description,-  pads = des,x_mil,y_mil,w_mil,h_mil,shape(rect|round),hole_mil ; ...   (hole 0 = SMD) }-Function CmdCreateFootprint(Lines : TStringList) : String;-Var-    PLib  : IPCB_Library;-    Comp  : IPCB_LibComponent;-    Pad   : IPCB_Pad;-    Pads  : TStringList;-    I     : Integer;-    Name, LibPath, Desc, Spec, Shape, LibState : String;-    Hole  : Integer;-Begin-    Name := Param(Lines, 'name');-    LibPath := Param(Lines, 'libraryPath');-    Desc := Param(Lines, 'description');-    If (Name = '') Or (LibPath = '') Then-    Begin-        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'name and libraryPath are required') + '}';-        Exit;-    End;-    LibState := EnsureLibrary('PCBLIB', LibPath);-    PLib := PCBServer.GetCurrentPCBLibrary;-    If PLib = Nil Then-    Begin-        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'no PCB library is current (' + LibState + ')') + '}';-        Exit;-    End;-    PCBServer.PreProcess;-    Comp := PCBServer.CreatePCBLibComp;-    Comp.Name := Name;-    Comp.Description := Desc;-    Pads := SplitSemi(Param(Lines, 'pads'));-    For I := 0 To Pads.Count - 1 Do-    Begin-        Spec := Pads[I];-        Pad := PCBServer.PCBObjectFactory(ePadObject, eNoDimension, eCreate_Default);-        Pad.Name := Field(Spec, 1);-        Pad.X := MilsToCoord(AdomFloat(Field(Spec, 2)));-        Pad.Y := MilsToCoord(AdomFloat(Field(Spec, 3)));-        Pad.TopXSize := MilsToCoord(AdomFloat(Field(Spec, 4)));-        Pad.TopYSize := MilsToCoord(AdomFloat(Field(Spec, 5)));-        Shape := LowerCase(Field(Spec, 6));-        If Shape = 'round' Then Pad.TopShape := eRounded Else Pad.TopShape := eRectangular;-        Hole := 0;-        If Field(Spec, 7) <> '' Then Hole := AdomInt(Field(Spec, 7));-        If Hole > 0 Then-        Begin-            Pad.HoleSize := MilsToCoord(Hole);-            Pad.Layer := eMultiLayer;-        End-        Else-        Begin-            Pad.HoleSize := 0;-            Pad.Layer := eTopLayer;-        End;-        Comp.AddPCBObject(Pad);-    End;-    Pads.Free;-    PLib.RegisterComponent(Comp);-    PLib.CurrentComponent := Comp;-    PCBServer.PostProcess;-    PLib.Board.ViewManager_FullUpdate;-    PLib.RefreshView;-    ResetParameters;-    AddStringParameter('Action', 'All');-    RunProcess('PCB:Zoom');-    Result := '{' + JRaw('ok', 'true') + ',' + JStr('footprint', Name) + ',' + JStr('library', LibPath) + ',' + JStr('libraryState', LibState) + ',' + JRaw('pads', IntToStr(Comp.GetPrimitiveCount(MkSet(ePadObject)))) + '}';-End;--{ create_symbol: name, libraryPath, designator(U), description, body_w_mil, body_h_mil,-  pins = des,name,side(left|right|top|bottom),offset_mil ; ...  }-Function CmdCreateSymbol(Lines : TStringList) : String;-Var-    SLib  : ISch_Lib;-    Comp  : ISch_Component;-    Rect  : ISch_Rectangle;-    Pin   : ISch_Pin;-    Pins  : TStringList;-    I, W, H, Off, Parts, P : Integer;-    Name, LibPath, Desc, Des, Spec, Side, LibState : String;-Begin-    Name := Param(Lines, 'name');-    LibPath := Param(Lines, 'libraryPath');-    Desc := Param(Lines, 'description');-    Des := Param(Lines, 'designator');-    If Des = '' Then Des := 'U';-    W := StrToIntDef(Param(Lines, 'body_w_mil'), 800);-    H := StrToIntDef(Param(Lines, 'body_h_mil'), 600);-    If (Name = '') Or (LibPath = '') Then-    Begin-        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'name and libraryPath are required') + '}';-        Exit;-    End;-    LibState := EnsureLibrary('SCHLIB', LibPath);-    SLib := SchServer.GetCurrentSchDocument;-    If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then-    Begin-        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'no schematic library is current (' + LibState + ')') + '}';-        Exit;-    End;-    Comp := SchServer.SchObjectFactory(eSchComponent, eCreate_Default);-    Comp.CurrentPartID := 1;-    Comp.DisplayMode := 0;-    Comp.LibReference := Name;-    Comp.ComponentDescription := Desc;-    Comp.Designator.Text := Des;-    Pins := SplitSemi(Param(Lines, 'pins'));-    { multi-part: a pin's 5th field is its part (1..N; 0 = on every part, e.g. power); one body per part }-    Parts := 1;-    For I := 0 To Pins.Count - 1 Do-        If StrToIntDef(Field(Pins[I], 5), 1) > Parts Then Parts := StrToIntDef(Field(Pins[I], 5), 1);-    If Parts > 1 Then Comp.PartCount := Parts;-    For P := 1 To Parts Do-    Begin-        Rect := SchServer.SchObjectFactory(eRectangle, eCreate_Default);-        Rect.OwnerPartId := P;-        Rect.OwnerPartDisplayMode := 0;-        Rect.Location := Point(MilsToCoord(0), MilsToCoord(0));-        Rect.Corner := Point(MilsToCoord(W), MilsToCoord(H));-        Rect.IsSolid := True;-        Comp.AddSchObject(Rect);-    End;-    For I := 0 To Pins.Count - 1 Do-    Begin-        Spec := Pins[I];-        Pin := SchServer.SchObjectFactory(ePin, eCreate_Default);-        Pin.OwnerPartId := StrToIntDef(Field(Spec, 5), 1);-        Pin.OwnerPartDisplayMode := 0;-        Pin.Designator := Field(Spec, 1);-        Pin.Name := Field(Spec, 2);-        Side := LowerCase(Field(Spec, 3));-        Off := StrToIntDef(Field(Spec, 4), 100);-        Pin.PinLength := MilsToCoord(300);-        If Side = 'right' Then-        Begin-            Pin.Orientation := eRotate0;-            Pin.Location := Point(MilsToCoord(W), MilsToCoord(H - Off));-        End-        Else If Side = 'top' Then-        Begin-            Pin.Orientation := eRotate90;-            Pin.Location := Point(MilsToCoord(Off), MilsToCoord(H));-        End-        Else If Side = 'bottom' Then-        Begin-            Pin.Orientation := eRotate270;-            Pin.Location := Point(MilsToCoord(Off), MilsToCoord(0));-        End-        Else-        Begin-            Pin.Orientation := eRotate180;-            Pin.Location := Point(MilsToCoord(0), MilsToCoord(H - Off));-        End;-        { a 6th field hides the pin on that net: shared power pins of a multi-part symbol (part 0) are hidden and-          joined to their net by name, or every part shows them and the compiler reports duplicate pins (measured-          2026-10-23: Altium's own placement of visible part-0 pins gave the same warnings) }-        If Trim(Field(Spec, 6)) <> '' Then-        Begin-            Pin.IsHidden := True;-            Pin.HiddenNetName := Trim(Field(Spec, 6));-        End;-        Comp.AddSchObject(Pin);-    End;-    I := Pins.Count;-    Pins.Free;-    SLib.AddSchComponent(Comp);-    SchServer.RobotManager.SendMessage(SLib.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, Comp.I_ObjectAddress);-    SLib.CurrentSchComponent := Comp;-    SLib.GraphicallyInvalidate;-    ResetParameters;-    AddStringParameter('Action', 'All');-    RunProcess('Sch:Zoom');-    Result := '{' + JRaw('ok', 'true') + ',' + JStr('symbol', Name) + ',' + JStr('library', LibPath) + ',' + JStr('libraryState', LibState) + ',' + JRaw('pins', IntToStr(I)) + ',' + JRaw('parts', IntToStr(Parts)) + '}';-End;--{ --------------------------------------------- library management (Barrett port) --- }--{ Register a library in Altium's installed-libraries list (IntLib, SchLib, PcbLib, DbLib). }-{ Every library in Altium's Installed Libraries list, as a JSON array of paths. }-Function CmdListInstalledLibs(Lines : TStringList) : String;-Var-    I, N : Integer;-    S : String;-Begin-    N := IntegratedLibraryManager.InstalledLibraryCount;-    S := '';-    For I := 0 To N - 1 Do-    Begin-        If S <> '' Then S := S + ',';-        S := S + '"' + JsonEsc(IntegratedLibraryManager.InstalledLibraryPath(I)) + '"';-    End;-    Result := '{' + JRaw('ok','true') + ',' + JRaw('count', IntToStr(N)) + ',' + JRaw('libraries', '[' + S + ']') + '}';-End;--{ Unregister installed libraries by exact path (paths=a;b). The files are never touched. A-  library that is open as a document is closed first: an installed SchLib loads as a document-  that comes up modified, and a close that discards it drops the unregistration (measured by-  John on Altium Develop, 2026-09-23). Removal takes effect live. }-Function CmdUninstallLibrary(Lines : TStringList) : String;-Var-    PL : TStringList;-    K : Integer;-    P, Removed, Closed : String;-    Doc : IServerDocument;-Begin-    PL := SplitSemi(Param(Lines, 'paths'));-    Removed := ''; Closed := '';-    For K := 0 To PL.Count - 1 Do-    Begin-        P := PL[K];-        Doc := Nil;-        Try-            Doc := Client.GetDocumentByPath(P);-        Except-            Doc := Nil;-        End;-        If Doc <> Nil Then-        Begin-            Try-                Client.CloseDocument(Doc);-                If Closed <> '' Then Closed := Closed + ',';-                Closed := Closed + '"' + JsonEsc(P) + '"';-            Except-            End;-        End;-        IntegratedLibraryManager.UninstallLibrary(P);-        If Removed <> '' Then Removed := Removed + ',';-        Removed := Removed + '"' + JsonEsc(P) + '"';-    End;-    PL.Free;-    Result := '{' + JRaw('ok','true') + ',' + JRaw('removed', '[' + Removed + ']') + ',' +-        JRaw('closedDocuments', '[' + Closed + ']') + ',' +-        JRaw('remaining', IntToStr(IntegratedLibraryManager.InstalledLibraryCount)) + '}';-End;--Function CmdInstallLibrary(Lines : TStringList) : String;-Var-    Path : String;-Begin-    Path := Param(Lines, 'path');-    If Not FileExists(Path) Then-    Begin-        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'library not found: ' + Path) + '}';-        Exit;-    End;-    IntegratedLibraryManager.InstallLibrary(Path);-    Result := '{' + JRaw('ok', 'true') + ',' + JStr('installed', Path) + '}';-End;--{ Compile a .LibPkg into a .IntLib: path=<LibPkg> output=<dir> }-Function CmdCompileIntLib(Lines : TStringList) : String;-Var-    Path, OutDir : String;-    Proj : IProject;-Begin-    Path := Param(Lines, 'path');-    OutDir := Param(Lines, 'output');-    If Not FileExists(Path) Then-    Begin-        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'LibPkg not found: ' + Path) + '}';-        Exit;-    End;-    Proj := GetWorkspace.DM_OpenProject(Path, True);-    If Proj = Nil Then-    Begin-        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'could not open LibPkg') + '}';-        Exit;-    End;-    Proj.DM_Compile;-    IntegratedLibraryManager.CreateIntegratedLibrary(Proj, OutDir, True);-    Result := '{' + JRaw('ok', 'true') + ',' + JStr('libpkg', Path) + ',' + JStr('outputDir', OutDir) + '}';-End;--{ The folder part of a path, ending in '\'. Built from Copy/Length only: SysUtils names that-  this engine does not expose raise an Error box and park the script on a breakpoint, so the agent-  never calls one that is not already proven elsewhere in this file. }-Function AdomDirOf(P : String) : String;-Var-    I : Integer;-Begin-    Result := '';-    For I := Length(P) DownTo 1 Do-    Begin-        If (Copy(P, I, 1) = '\') Or (Copy(P, I, 1) = '/') Then-        Begin-            Result := Copy(P, 1, I);-            Break;-        End;-    End;-End;--{ Extract editable SchLib/PcbLib sources from a compiled .IntLib: path=<IntLib> }-Function CmdExtractIntLib(Lines : TStringList) : String;-Var-    Path, P, Acc, Dir, UP : String;-    WS : IWorkspace;-    Prj : IProject;-    Doc : IDocument;-    I, J : Integer;-Begin-    Path := Param(Lines, 'path');-    If Not FileExists(Path) Then-    Begin-        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'IntLib not found: ' + Path) + '}';-        Exit;-    End;-    IntegratedLibraryManager.ExtractSources(Path);-    { ExtractSources writes the editable libraries BESIDE the .IntLib. This used to report every-      .SchLib/.PcbLib open anywhere in the workspace, so a caller with other libraries open was-      handed paths that had nothing to do with this IntLib. Scope it to the IntLib's own folder. }-    Dir := UpperCase(AdomDirOf(Path));-    Acc := '';-    WS := GetWorkspace;-    If WS <> Nil Then-        For I := 0 To WS.DM_ProjectCount - 1 Do-        Begin-            Prj := WS.DM_Projects(I);-            For J := 0 To Prj.DM_LogicalDocumentCount - 1 Do-            Begin-                Doc := Prj.DM_LogicalDocuments(J);-                P := Doc.DM_FullPath;-                UP := UpperCase(P);-                If ((Pos('.SCHLIB', UP) > 0) Or (Pos('.PCBLIB', UP) > 0)) And-                   ((Dir = '') Or (Pos(Dir, UP) = 1)) Then-                Begin-                    If Acc <> '' Then Acc := Acc + ',';-                    Acc := Acc + '"' + JsonEsc(P) + '"';-                End;-            End;-        End;-    Result := '{' + JRaw('ok', 'true') + ',' + JStr('intlib', Path) + ',' +-        JStr('searchedUnder', AdomDirOf(Path)) + ',' + JRaw('sources', '[' + Acc + ']') + '}';-End;--{ Set/replace parameters on a component of the CURRENT SchLib and optionally link a footprint.-  name=<LibReference>  params=Key=Value;Key=Value  footprint=<PcbLib footprint name>  comment=<text> }-Procedure SetSchParam(Cmp : ISch_Component; PName : String; PVal : String);-Var-    It : ISch_Iterator;-    Pm, Existing : ISch_Parameter;-Begin-    Existing := Nil;-    It := Cmp.SchIterator_Create;-    It.AddFilter_ObjectSet(MkSet(eParameter));-    Pm := It.FirstSchObject;-    While Pm <> Nil Do-    Begin-        If Pm.Name = PName Then-        Begin-            Existing := Pm;-            Break;-        End;-        Pm := It.NextSchObject;-    End;-    Cmp.SchIterator_Destroy(It);-    If Existing <> Nil Then-        Existing.Text := PVal-    Else-    Begin-        Pm := SchServer.SchObjectFactory(eParameter, eCreate_GlobalCopy);-        Pm.Name := PName;-        Pm.Text := PVal;-        { library-part parameters are metadata; visible ones pile up over the symbol (measured 2026-08-30) }-        Pm.IsHidden := True;-        Cmp.AddSchObject(Pm);-    End;-End;--Function CmdSetParams(Lines : TStringList) : String;-Var-    SLib : ISch_Lib;-    Iter : ISch_Iterator;-    Comp : ISch_Component;-    Impl : ISch_Implementation;-    C2   : ISch_Component;-    Name, Fp, Cmt, Spec, K, V : String;-    Items : TStringList;-    I, P, N : Integer;-Begin-    Name := Param(Lines, 'name');-    Fp := Param(Lines, 'footprint');-    Cmt := Param(Lines, 'comment');-    SLib := SchServer.GetCurrentSchDocument;-    If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then-    Begin-        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'current document is not a schematic library') + '}';-        Exit;-    End;-    { With no name this used to take whichever component the iterator yielded FIRST, so a-      set_params against a multi-symbol library silently wrote the footprint and comment onto-      an arbitrary part. An omitted name may only mean "the one component in here". }-    Comp := Nil;-    N := 0;-    Iter := SLib.SchLibIterator_Create;-    Iter.AddFilter_ObjectSet(MkSet(eSchComponent));-    C2 := Iter.FirstSchObject;-    While C2 <> Nil Do-    Begin-        N := N + 1;-        If Name = '' Then-        Begin-            If Comp = Nil Then Comp := C2;-        End-        Else If UpperCase(C2.LibReference) = UpperCase(Name) Then Comp := C2;-        C2 := Iter.NextSchObject;-    End;-    SLib.SchIterator_Destroy(Iter);-    If (Name = '') And (N > 1) Then-    Begin-        Result := '{' + JRaw('ok', 'false') + ',' + JStr('errorCode', 'name_required') + ',' +-            JStr('error', 'this library holds ' + IntToStr(N) + ' components: name the one to set') + '}';-        Exit;-    End;-    If Comp = Nil Then-    Begin-        Result := '{' + JRaw('ok', 'false') + ',' + JStr('errorCode', 'name_not_found') + ',' +-            JStr('error', 'component not found: ' + Name) + '}';-        Exit;-    End;-    N := 0;-    Items := SplitSemi(Param(Lines, 'params'));-    For I := 0 To Items.Count - 1 Do-    Begin-        Spec := Items[I];-        P := Pos('=', Spec);-        If P > 0 Then-        Begin-            K := Trim(Copy(Spec, 1, P - 1));-            V := Trim(Copy(Spec, P + 1, Length(Spec)));-            SetSchParam(Comp, K, V);-            N := N + 1;-        End;-    End;-    Items.Free;-    If Cmt <> '' Then Comp.Comment.Text := Cmt;-    If Fp <> '' Then-    Begin-        Impl := Comp.AddSchImplementation;-        Impl.ClearAllDatafileLinks;-        Impl.ModelName := Fp;-        Impl.ModelType := 'PCBLIB';-        Impl.AddDataFileLink(Fp, '', 'PCBLIB');-        Impl.IsCurrent := True;-    End;-    SLib.GraphicallyInvalidate;-    Result := '{' + JRaw('ok', 'true') + ',' + JStr('component', Comp.LibReference) + ',' + JRaw('paramsSet', IntToStr(N)) + ',' + JStr('footprint', Fp) + '}';-End;--{ Add a 3D body to a footprint of the CURRENT PcbLib: name=<footprint>-  step=<path.step>  OR  extruded box: bw_mil,bh_mil,h_mil,so_mil }-{ ------------------------------------------------- diagnostics: pcb_report --- }--Function BodyJson(Prim : IPCB_Primitive) : String;-Var-    R : TCoordRect;-    HasModel : String;-Begin-    R := Prim.BoundingRectangle;-    HasModel := 'false';-    Try-        If Prim.Model <> Nil Then HasModel := 'true';-    Except-    End;-    Result := '[' + IntToStr(Prim.Layer) + ',' +-        IntToStr(Round(CoordToMils(R.Right - R.Left))) + ',' +-        IntToStr(Round(CoordToMils(R.Top - R.Bottom))) + ',' +-        IntToStr(Round(CoordToMils(R.Left))) + ',' +-        IntToStr(Round(CoordToMils(R.Bottom))) + ',' +-        IntToStr(Round(CoordToMils(Prim.StandoffHeight))) + ',' +-        IntToStr(Round(CoordToMils(Prim.OverallHeight))) + ',' +-        HasModel + ']';-End;--Function GroupBodies(Grp : IPCB_Group) : String;-Var-    GIt  : IPCB_GroupIterator;-    Prim : IPCB_Primitive;-    S    : String;-Begin-    S := '';-    GIt := Grp.GroupIterator_Create;-    GIt.AddFilter_ObjectSet(MkSet(eComponentBodyObject));-    GIt.AddFilter_LayerSet(AllLayers);-    Prim := GIt.FirstPCBObject;-    While Prim <> Nil Do-    Begin-        If S <> '' Then S := S + ',';-        S := S + BodyJson(Prim);-        Prim := GIt.NextPCBObject;-    End;-    Grp.GroupIterator_Destroy(GIt);-    Result := '[' + S + ']';-End;--Function GroupCensus(Grp : IPCB_Group) : String;-Var-    GIt  : IPCB_GroupIterator;-    Prim : IPCB_Primitive;-    N    : Integer;-    S    : String;-Begin-    N := 0;-    S := '';-    GIt := Grp.GroupIterator_Create;-    GIt.AddFilter_LayerSet(AllLayers);-    Prim := GIt.FirstPCBObject;-    While Prim <> Nil Do-    Begin-        N := N + 1;-        If S <> '' Then S := S + ';';-        S := S + IntToStr(Prim.ObjectId);-        Prim := GIt.NextPCBObject;-    End;-    Grp.GroupIterator_Destroy(GIt);-    Result := IntToStr(N) + ':' + S;-End;--Function GroupCount(Grp : IPCB_Group; Kind : Integer) : Integer;-Var-    GIt  : IPCB_GroupIterator;-    Prim : IPCB_Primitive;-    N    : Integer;-Begin-    N := 0;-    GIt := Grp.GroupIterator_Create;-    GIt.AddFilter_ObjectSet(MkSet(Kind));-    GIt.AddFilter_LayerSet(AllLayers);-    Prim := GIt.FirstPCBObject;-    While Prim <> Nil Do-    Begin-        N := N + 1;-        Prim := GIt.NextPCBObject;-    End;-    Grp.GroupIterator_Destroy(GIt);-    Result := N;-End;---Function BoardBodyCount(Board : IPCB_Board) : Integer;-Var-    BIt  : IPCB_BoardIterator;-    Prim : IPCB_Primitive;-    N    : Integer;-Begin-    N := 0;-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eComponentBodyObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Prim := BIt.FirstPCBObject;-    While Prim <> Nil Do-    Begin-        N := N + 1;-        Prim := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-    Result := N;-End;--Function CmdAddBody(Lines : TStringList) : String;-Var-    PLib : IPCB_Library;-    Fp   : IPCB_LibComponent;-    Fp2  : IPCB_LibComponent;-    Body : IPCB_ComponentBody;-    Mdl  : IPCB_Model;-    Cont : IPCB_Contour;-    BIt  : IPCB_BoardIterator;-    Old  : IPCB_Primitive;-    Name, StepPath, Note, KeepS : String;-    I, BW, BH, HG, SO : Integer;-    NRep, NBefore, NSame : Integer;-    Found, Keep : Boolean;-Begin-    Name := Param(Lines, 'name');-    StepPath := Param(Lines, 'step');-    { keep=1 is the deliberate-second-body opt-out; anything else REPLACES the footprint's bodies }-    Keep := (Param(Lines, 'keep') = '1');-    If Keep Then KeepS := 'true' Else KeepS := 'false';-    PLib := PCBServer.GetCurrentPCBLibrary;-    If PLib = Nil Then-    Begin-        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'current document is not a PCB library') + '}';-        Exit;-    End;-    { An omitted name may only mean "the one footprint in here": adding a 3D body to whichever-      footprint happened to be first is a silent edit of the wrong part. }-    If (Name = '') And (PLib.ComponentCount > 1) Then-    Begin-        Result := '{' + JRaw('ok', 'false') + ',' + JStr('errorCode', 'name_required') + ',' +-            JStr('error', 'this library holds ' + IntToStr(PLib.ComponentCount) + ' footprints: name the one to add the body to') + '}';-        Exit;-    End;-    Found := False;-    For I := 0 To PLib.ComponentCount - 1 Do-    Begin-        Fp := PLib.GetComponent(I);-        If (Name = '') Or (UpperCase(Fp.Name) = UpperCase(Name)) Then-        Begin-            Found := True;-            Break;-        End;-    End;-    If Not Found Then-    Begin-        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'footprint not found: ' + Name) + '}';-        Exit;-    End;-    { footprints sharing this name: more than one means the body went to the FIRST of them only }-    NSame := 0;-    For I := 0 To PLib.ComponentCount - 1 Do-    Begin-        Fp2 := PLib.GetComponent(I);-        If UpperCase(Fp2.Name) = UpperCase(Fp.Name) Then NSame := NSame + 1;-    End;-    PCBServer.PreProcess;-    Body := PCBServer.PCBObjectFactory(eComponentBodyObject, eNoDimension, eCreate_Default);-    { A primitive without a layer is silently dropped on AddPCBObject (measured-      2026-08-30: add returned ok, census showed 16 pads and 0 bodies). Component-      bodies live on a mechanical layer. }-    Body.Layer := eMechanical1;-    { An invisible body is indistinguishable from a missing one: opacity defaults-      to 0 in this engine. Make sure the mech layer is enabled and the body opaque. }-    PLib.Board.LayerStack.LayerObject[eMechanical1].MechanicalLayerEnabled := True;-    PLib.Board.LayerIsDisplayed[eMechanical1] := True;-    Body.BodyOpacity3D := 1.0;-    If StepPath <> '' Then-    Begin-        Mdl := Body.ModelFactory_FromFilename(StepPath, False);-        If Mdl = Nil Then-        Begin-            PCBServer.PostProcess;-            Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'could not load model: ' + StepPath) + '}';-            Exit;-        End;-        Body.SetState_FromModel;-        Body.Model := Mdl;-        Note := 'step';-    End-    Else-    Begin-        BW := StrToIntDef(Param(Lines, 'bw_mil'), 100);-        BH := StrToIntDef(Param(Lines, 'bh_mil'), 100);-        HG := StrToIntDef(Param(Lines, 'h_mil'), 40);-        SO := StrToIntDef(Param(Lines, 'so_mil'), 0);-        Body.BodyProjection := eBoardSide_Top;-        Body.StandoffHeight := MilsToCoord(SO);-        Body.OverallHeight := MilsToCoord(SO + HG);-        Body.BodyColor3D := $00202020;-        Cont := PCBServer.PCBContourFactory;-        Cont.AddPoint(MilsToCoord(-(BW Div 2)), MilsToCoord(-(BH Div 2)));-        Cont.AddPoint(MilsToCoord((BW Div 2)), MilsToCoord(-(BH Div 2)));-        Cont.AddPoint(MilsToCoord((BW Div 2)), MilsToCoord((BH Div 2)));-        Cont.AddPoint(MilsToCoord(-(BW Div 2)), MilsToCoord((BH Div 2)));-        Body.SetOutlineContour(Cont);-        Note := 'extruded';-    End;-    { The stored component is NOT the editing surface: a direct Fp.AddPCBObject-      counted 1 body immediately but the editor's canvas copy overwrote it on-      save/reopen - libBodies read back 0 (measured 2026-08-30). Make Fp current-      FIRST, then add to the CANVAS (PLib.Board); the editor owns the canvas and-      syncs it into the stored component on save. }-    PLib.CurrentComponent := Fp;-    NBefore := GroupCount(Fp, eComponentBodyObject);-    { a part imported again REPLACES its 3D body, the way build_symbol_modes replaces its symbol:-      adding on top left the old model and the fixed one inside each other on the board-      (seen 2026-10-16 on AMPLH5030S). Only now that the new model has loaded, so a bad step-      path keeps the old body. }-    NRep := 0;-    If Not Keep Then-    Begin-        { delete them the way the editor does: RemovePCBObject on the canvas or on the footprint-          left them in place after save and reopen (measured 2026-10-20: the stored footprint kept-          every body). Select exactly the canvas's bodies, end this command, run Edit>Delete-          (PCB:Clear) on the selection, then start a new command for the add. Nothing else-          may be selected: Clear deletes the whole selection. }-        BIt := PLib.Board.BoardIterator_Create;-        BIt.AddFilter_LayerSet(AllLayers);-        BIt.AddFilter_Method(eProcessAll);-        Old := BIt.FirstPCBObject;-        While Old <> Nil Do-        Begin-            If Old.Selected Then Old.Selected := False;-            Old := BIt.NextPCBObject;-        End;-        PLib.Board.BoardIterator_Destroy(BIt);-        BIt := PLib.Board.BoardIterator_Create;-        BIt.AddFilter_ObjectSet(MkSet(eComponentBodyObject));-        BIt.AddFilter_LayerSet(AllLayers);-        BIt.AddFilter_Method(eProcessAll);-        Old := BIt.FirstPCBObject;-        While Old <> Nil Do-        Begin-            Old.Selected := True;-            NRep := NRep + 1;-            Old := BIt.NextPCBObject;-        End;-        PLib.Board.BoardIterator_Destroy(BIt);-        If NRep > 0 Then-        Begin-            PCBServer.PostProcess;-            ResetParameters;-            RunProcess('PCB:Clear');-            PCBServer.PreProcess;-        End;-    End;-    PLib.Board.AddPCBObject(Body);-    PCBServer.SendMessageToRobots(PLib.Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Body.I_ObjectAddress);-    Note := Note + ':canvas';-    PCBServer.PostProcess;-    PLib.Board.ViewManager_FullUpdate;-    PLib.RefreshView;-    Result := '{' + JRaw('ok', 'true') + ',' + JStr('footprint', Fp.Name) + ',' + JStr('body', Note) + ',' +-        JRaw('replacedBodies', IntToStr(NRep)) + ',' + JRaw('keptExisting', KeepS) + ',' +-        JRaw('bodiesBefore', IntToStr(NBefore)) + ',' + JRaw('sameNameFootprints', IntToStr(NSame)) + ',' +-        JRaw('bodiesNow', IntToStr(GroupCount(Fp, eComponentBodyObject))) + ',' +-        JRaw('bodiesOnCanvas', IntToStr(BoardBodyCount(PLib.Board))) + '}';-End;--{ --------------------------------------------------- demo: place on sch + pcb --- }--{ Create a new schematic sheet and place the CH340G symbol (Replicated from the-  currently-open SchLib) at the centre. sch=<save path> libref=<component> }-{ true/false for JSON }-Function BoolStr(B : Boolean) : String;-Begin-    If B Then Result := 'true' Else Result := 'false';-End;--{ Set (or add) a DOCUMENT parameter on a sheet: Title / DocumentNumber / Revision / DrawnBy feed the-  standard title block through its special strings. }-Procedure SetDocParam(Sheet : ISch_Document; PName : String; PVal : String);-Var-    It : ISch_Iterator;-    Pm, Existing : ISch_Parameter;-Begin-    Existing := Nil;-    It := Sheet.SchIterator_Create;-    It.AddFilter_ObjectSet(MkSet(eParameter));-    Pm := It.FirstSchObject;-    While Pm <> Nil Do-    Begin-        If Pm.Name = PName Then-        Begin-            Existing := Pm;-            Break;-        End;-        Pm := It.NextSchObject;-    End;-    Sheet.SchIterator_Destroy(It);-    If Existing <> Nil Then-        Existing.Text := PVal-    Else-    Begin-        Pm := SchServer.SchObjectFactory(eParameter, eCreate_Default);-        Pm.Name := PName;-        Pm.Text := PVal;-        Pm.IsHidden := True;-        Sheet.AddSchObject(Pm);-        SchServer.RobotManager.SendMessage(Sheet.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, Pm.I_ObjectAddress);-    End;-End;--{ Split on single spaces }-Function SplitSpace(S : String) : TStringList;-Var-    K : Integer;-    Part : String;-Begin-    Result := TStringList.Create;-    S := Trim(S);-    While S <> '' Do-    Begin-        K := Pos(' ', S);-        If K > 0 Then-        Begin-            Part := Copy(S, 1, K - 1);-            S := Trim(Copy(S, K + 1, Length(S)));-        End-        Else-        Begin-            Part := S;-            S := '';-        End;-        If Part <> '' Then Result.Add(Part);-    End;-End;--{ Draw the chip-outline artwork (chip-thumbnailer polylines, as baked into the KiCad symbol) into a-  SchLib component. Args: libref, kicadRect=x0,y0,x1,y1 (mils, the KiCad body rectangle). Payload:-  one polyline per line, 'x,y x,y ...' in mils of the KiCad frame. The KiCad body rectangle is mapped-  onto the component's own rectangle, so the outline lands between the pin-name columns exactly as-  it does in KiCad and Fusion (John, 2026-09-19). }-Function CmdAddSymbolArt(Lines : TStringList; Payload : String) : String;-Var-    SLib   : ISch_Lib;-    Iter   : ISch_Iterator;-    Comp   : ISch_Component;-    Rect   : ISch_Rectangle;-    Poly   : ISch_Polyline;-    Seg    : ISch_Line;-    Prim   : ISch_GraphicalObject;-    Victims : TInterfaceList;-    PL, Pts : TStringList;-    I, J, N, NPoly, NPts, NCleared : Integer;-    Name, KR, Ln, Tok, Mode : String;-    KX0, KY0, KX1, KY1, AX0, AY0, AX1, AY1, SX, SY, MX, MY, PX, PY, Tmp : Double;-    UseLines : Boolean;-Begin-    Name := Param(Lines, 'libref');-    KR := Param(Lines, 'kicadRect');-    Mode := Param(Lines, 'mode');-    { lines (default): every polyline becomes N-1 eLine segments, the primitive BuildSymbolModes has-      always drawn with; polyline: ISch_Polyline objects (measured 2026-09-19: they render on one-      part and stay invisible on another with identical geometry). }-    UseLines := (Mode <> 'polyline');-    SLib := SchServer.GetCurrentSchDocument;-    If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then-    Begin-        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'current document is not a schematic library') + '}';-        Exit;-    End;-    Comp := Nil;-    Iter := SLib.SchLibIterator_Create;-    Iter.AddFilter_ObjectSet(MkSet(eSchComponent));-    Comp := Iter.FirstSchObject;-    While Comp <> Nil Do-    Begin-        If UpperCase(Comp.LibReference) = UpperCase(Name) Then Break;-        Comp := Iter.NextSchObject;-    End;-    SLib.SchIterator_Destroy(Iter);-    If Comp = Nil Then-    Begin-        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'component not found: ' + Name) + '}';-        Exit;-    End;-    Rect := Nil;-    Iter := Comp.SchIterator_Create;-    Iter.AddFilter_ObjectSet(MkSet(eRectangle));-    Rect := Iter.FirstSchObject;-    If Rect <> Nil Then-    Begin-        AX0 := CoordToMils(Rect.Location.X); AY0 := CoordToMils(Rect.Location.Y);-        AX1 := CoordToMils(Rect.Corner.X); AY1 := CoordToMils(Rect.Corner.Y);-    End;-    Comp.SchIterator_Destroy(Iter);-    If Rect = Nil Then-    Begin-        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'component has no body rectangle to fit the artwork into') + '}';-        Exit;-    End;-    KX0 := AdomFloat(Field(KR, 1)); KY0 := AdomFloat(Field(KR, 2));-    KX1 := AdomFloat(Field(KR, 3)); KY1 := AdomFloat(Field(KR, 4));-    If KX1 < KX0 Then Begin Tmp := KX0; KX0 := KX1; KX1 := Tmp; End;-    If KY1 < KY0 Then Begin Tmp := KY0; KY0 := KY1; KY1 := Tmp; End;-    If AX1 < AX0 Then Begin Tmp := AX0; AX0 := AX1; AX1 := Tmp; End;-    If AY1 < AY0 Then Begin Tmp := AY0; AY0 := AY1; AY1 := Tmp; End;-    If (KX1 - KX0 < 1) Or (KY1 - KY0 < 1) Then-    Begin-        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'kicadRect is degenerate: ' + KR) + '}';-        Exit;-    End;-    SX := (AX1 - AX0) / (KX1 - KX0);-    SY := (AY1 - AY0) / (KY1 - KY0);-    SchServer.ProcessControl.PreProcess(SLib, '');-    { replace, never stack: every earlier polyline and every line that is not part of the pins-      or the body outline is art from a previous run. Collect first, remove after the walk. }-    NCleared := 0;-    If Param(Lines, 'clear') <> '0' Then-    Begin-        Victims := TInterfaceList.Create;-        Iter := Comp.SchIterator_Create;-        Iter.AddFilter_ObjectSet(MkSet(ePolyline, eLine));-        Prim := Iter.FirstSchObject;-        While Prim <> Nil Do-        Begin-            Victims.Add(Prim);-            Prim := Iter.NextSchObject;-        End;-        Comp.SchIterator_Destroy(Iter);-        For I := 0 To Victims.Count - 1 Do-        Begin-            Prim := Victims[I];-            Comp.RemoveSchObject(Prim);-            SchServer.DestroySchObject(Prim);-            NCleared := NCleared + 1;-        End;-        Victims.Free;-    End;-    PL := TStringList.Create;-    PL.Text := Payload;-    NPoly := 0; NPts := 0;-    For I := 0 To PL.Count - 1 Do-    Begin-        Ln := Trim(PL[I]);-        If Ln = '' Then Continue;-        Pts := SplitSpace(Ln);-        N := Pts.Count;-        If N >= 2 Then-        Begin-            If UseLines Then-            Begin-                PX := 0; PY := 0;-                For J := 1 To N Do-                Begin-                    Tok := Pts[J - 1];-                    MX := AX0 + (AdomFloat(Field(Tok, 1)) - KX0) * SX;-                    MY := AY0 + (AdomFloat(Field(Tok, 2)) - KY0) * SY;-                    If J > 1 Then-                    Begin-                        Seg := SchServer.SchObjectFactory(eLine, eCreate_Default);-                        Seg.OwnerPartId := 1;-                        Seg.OwnerPartDisplayMode := 0;-                        Seg.LineWidth := eSmall;-                        Seg.Color := $8A8A8A;-                        Seg.Location := Point(MilsToCoord(PX), MilsToCoord(PY));-                        Seg.Corner := Point(MilsToCoord(MX), MilsToCoord(MY));-                        Comp.AddSchObject(Seg);-                        SchServer.RobotManager.SendMessage(Comp.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, Seg.I_ObjectAddress);-                    End;-                    PX := MX; PY := MY;-                End;-            End-            Else-            Begin-                Poly := SchServer.SchObjectFactory(ePolyline, eCreate_Default);-                Poly.OwnerPartId := 1;-                Poly.OwnerPartDisplayMode := 0;-                Poly.LineWidth := eSmall;-                Poly.Color := $8A8A8A;-                Poly.VerticesCount := N;-                For J := 1 To N Do-                Begin-                    Tok := Pts[J - 1];-                    MX := AX0 + (AdomFloat(Field(Tok, 1)) - KX0) * SX;-                    MY := AY0 + (AdomFloat(Field(Tok, 2)) - KY0) * SY;-                    Poly.Vertex[J] := Point(MilsToCoord(MX), MilsToCoord(MY));-                End;-                Comp.AddSchObject(Poly);-                SchServer.RobotManager.SendMessage(Comp.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, Poly.I_ObjectAddress);-            End;-            NPoly := NPoly + 1;-            NPts := NPts + N;-        End;-        Pts.Free;-    End;-    SchServer.ProcessControl.PostProcess(SLib, '');-    MarkSchModified(SLib);-    PL.Free;-    SLib.GraphicallyInvalidate;-    Result := '{' + JRaw('ok', 'true') + ',' + JStr('libref', Comp.LibReference) + ',' + JRaw('polylines', IntToStr(NPoly)) + ',' +-        JRaw('points', IntToStr(NPts)) + ',' + JRaw('cleared', IntToStr(NCleared)) + ',' + JStr('mode', Mode) + ',' +-        JRaw('bodyRectMil', '[' + IntToStr(Round(AX0)) + ',' + IntToStr(Round(AY0)) + ',' + IntToStr(Round(AX1)) + ',' + IntToStr(Round(AY1)) + ']') + '}';-End;--{ Read a SchLib component drawing: primitive counts by kind, the body rectangle, the polyline-  bounding box and the footprint models. Args: libref }-{ Regression test for the script-error safety net: raise a REAL runtime error inside the agent's-  own code, on purpose. The bridge must photograph the Error box, stop the halted script itself-  and answer agent_script_error; a timeout here means the net is broken. Never call this except-  through altium_selftest_script_error. }-Function CmdSelfTestHalt(Lines : TStringList) : String;-Begin-    AdomDeliberateUndeclaredIdentifierForSelfTest(1);-    Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'the engine did not halt: this Altium build tolerated an undeclared identifier') + '}';-End;--Function CmdSymbolPrims(Lines : TStringList) : String;-Var-    AnyR0 : Boolean;-    SLib  : ISch_Lib;-    Iter  : ISch_Iterator;-    Comp  : ISch_Component;-    Prim  : ISch_GraphicalObject;-    Impl  : ISch_Implementation;-    Name, Models : String;-    NPin, NRect, NLine, NPoly, NOther, I : Integer;-    R : TCoordRect;-    PX0, PY0, PX1, PY1, RX0, RY0, RX1, RY1 : Integer;-    AnyP, AnyR : Boolean;-Begin-    Name := Param(Lines, 'libref');-    SLib := SchServer.GetCurrentSchDocument;-    If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then-    Begin-        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'current document is not a schematic library') + '}';-        Exit;-    End;-    Comp := Nil;-    Iter := SLib.SchLibIterator_Create;-    Iter.AddFilter_ObjectSet(MkSet(eSchComponent));-    Comp := Iter.FirstSchObject;-    While Comp <> Nil Do-    Begin-        If UpperCase(Comp.LibReference) = UpperCase(Name) Then Break;-        Comp := Iter.NextSchObject;-    End;-    SLib.SchIterator_Destroy(Iter);-    If Comp = Nil Then-    Begin-        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'component not found: ' + Name) + '}';-        Exit;-    End;-    NPin := 0; NRect := 0; NLine := 0; NPoly := 0; NOther := 0;-    AnyP := False; AnyR := False; AnyR0 := False;-    PX0 := 0; PY0 := 0; PX1 := 0; PY1 := 0; RX0 := 0; RY0 := 0; RX1 := 0; RY1 := 0;-    Iter := Comp.SchIterator_Create;-    Prim := Iter.FirstSchObject;-    While Prim <> Nil Do-    Begin-        If Prim.ObjectID = ePin Then NPin := NPin + 1-        Else If Prim.ObjectID = eRectangle Then-        Begin-            NRect := NRect + 1;-            { the body of the mode the symbol shows (the sheet draws mode 0): the union of that-              mode's rectangles (a capacitor's two plates are two). The first rectangle found-              could be any mode's: a 10-mode symbol answered a wider mode's body. }-            R := Prim.BoundingRectangle;-            If Prim.OwnerPartDisplayMode = Comp.DisplayMode Then-            Begin-                If Not AnyR0 Then-                Begin-                    RX0 := Round(CoordToMils(R.Left)); RY0 := Round(CoordToMils(R.Bottom));-                    RX1 := Round(CoordToMils(R.Right)); RY1 := Round(CoordToMils(R.Top));-                End-                Else-                Begin-                    If Round(CoordToMils(R.Left)) < RX0 Then RX0 := Round(CoordToMils(R.Left));-                    If Round(CoordToMils(R.Bottom)) < RY0 Then RY0 := Round(CoordToMils(R.Bottom));-                    If Round(CoordToMils(R.Right)) > RX1 Then RX1 := Round(CoordToMils(R.Right));-                    If Round(CoordToMils(R.Top)) > RY1 Then RY1 := Round(CoordToMils(R.Top));-                End;-                AnyR0 := True; AnyR := True;-            End-            Else If Not AnyR Then-            Begin-                RX0 := Round(CoordToMils(R.Left)); RY0 := Round(CoordToMils(R.Bottom));-                RX1 := Round(CoordToMils(R.Right)); RY1 := Round(CoordToMils(R.Top));-                AnyR := True;-            End;-        End-        Else If Prim.ObjectID = eLine Then NLine := NLine + 1-        Else If Prim.ObjectID = ePolyline Then-        Begin-            NPoly := NPoly + 1;-            R := Prim.BoundingRectangle;-            If Not AnyP Then-            Begin-                PX0 := Round(CoordToMils(R.Left)); PY0 := Round(CoordToMils(R.Bottom));-                PX1 := Round(CoordToMils(R.Right)); PY1 := Round(CoordToMils(R.Top));-                AnyP := True;-            End-            Else-            Begin-                If Round(CoordToMils(R.Left)) < PX0 Then PX0 := Round(CoordToMils(R.Left));-                If Round(CoordToMils(R.Bottom)) < PY0 Then PY0 := Round(CoordToMils(R.Bottom));-                If Round(CoordToMils(R.Right)) > PX1 Then PX1 := Round(CoordToMils(R.Right));-                If Round(CoordToMils(R.Top)) > PY1 Then PY1 := Round(CoordToMils(R.Top));-            End;-        End-        Else NOther := NOther + 1;-        Prim := Iter.NextSchObject;-    End;-    Comp.SchIterator_Destroy(Iter);-    Models := '';-    If Param(Lines, 'models') = '1' Then-    Begin-        { ISch_Component has no ImplementationCount/Implementation[] in DelphiScript on every AD-          build (26.9.1 halts the script there); the eImplementation child iterator works on all. }-        Iter := Comp.SchIterator_Create;-        Iter.AddFilter_ObjectSet(MkSet(eImplementation));-        Impl := Iter.FirstSchObject;-        While Impl <> Nil Do-        Begin-            If Models <> '' Then Models := Models + ',';-            Models := Models + '"' + Impl.ModelType + ':' + Impl.ModelName + '"';-            Impl := Iter.NextSchObject;-        End;-        Comp.SchIterator_Destroy(Iter);-    End;-    Result := '{' + JRaw('ok', 'true') + ',' + JStr('libref', Comp.LibReference) + ',' +-        JRaw('pins', IntToStr(NPin)) + ',' + JRaw('rectangles', IntToStr(NRect)) + ',' + JRaw('lines', IntToStr(NLine)) + ',' +-        JRaw('polylines', IntToStr(NPoly)) + ',' + JRaw('other', IntToStr(NOther)) + ',' +-        JRaw('displayMode', IntToStr(Comp.DisplayMode)) + ',' + JRaw('displayModeCount', IntToStr(Comp.DisplayModeCount)) + ',' +-        JRaw('currentPartId', IntToStr(Comp.CurrentPartID)) + ',' +-        JRaw('bodyRectMil', '[' + IntToStr(RX0) + ',' + IntToStr(RY0) + ',' + IntToStr(RX1) + ',' + IntToStr(RY1) + ']') + ',' +-        JRaw('polyBboxMil', '[' + IntToStr(PX0) + ',' + IntToStr(PY0) + ',' + IntToStr(PX1) + ',' + IntToStr(PY1) + ']') + ',' +-        JRaw('models', '[' + Models + ']') + '}';-End;--{ Replace every footprint model on a SchLib component with ONE PCBLIB model (set_params-  footprint APPENDS an implementation per call, which is how a symbol ends up with two).-  Args: libref, footprint }-Function CmdSetModels(Lines : TStringList) : String;-Var-    SLib  : ISch_Lib;-    Iter  : ISch_Iterator;-    Comp  : ISch_Component;-    Impl  : ISch_Implementation;-    Name, Fp : String;-    Removed : Integer;-Begin-    Name := Param(Lines, 'libref');-    Fp := Param(Lines, 'footprint');-    SLib := SchServer.GetCurrentSchDocument;-    If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then-    Begin-        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'current document is not a schematic library') + '}';-        Exit;-    End;-    Comp := Nil;-    Iter := SLib.SchLibIterator_Create;-    Iter.AddFilter_ObjectSet(MkSet(eSchComponent));-    Comp := Iter.FirstSchObject;-    While Comp <> Nil Do-    Begin-        If UpperCase(Comp.LibReference) = UpperCase(Name) Then Break;-        Comp := Iter.NextSchObject;-    End;-    SLib.SchIterator_Destroy(Iter);-    If Comp = Nil Then-    Begin-        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'component not found: ' + Name) + '}';-        Exit;-    End;-    Removed := 0;-    { Same rule as CmdSymbolPrims: walk implementations with the eImplementation iterator. One-      iterator per removal, because removing under a live iterator invalidates it. }-    While Removed < 64 Do-    Begin-        Iter := Comp.SchIterator_Create;-        Iter.AddFilter_ObjectSet(MkSet(eImplementation));-        Impl := Iter.FirstSchObject;-        Comp.SchIterator_Destroy(Iter);-        If Impl = Nil Then Break;-        Comp.RemoveSchImplementation(Impl);-        Removed := Removed + 1;-    End;-    If Fp <> '' Then-    Begin-        Impl := Comp.AddSchImplementation;-        Impl.ClearAllDatafileLinks;-        Impl.ModelName := Fp;-        Impl.ModelType := 'PCBLIB';-        Impl.AddDataFileLink(Fp, '', 'PCBLIB');-        Impl.IsCurrent := True;-    End;-    SLib.GraphicallyInvalidate;-    Result := '{' + JRaw('ok', 'true') + ',' + JStr('libref', Comp.LibReference) + ',' + JRaw('removed', IntToStr(Removed)) + ',' + JStr('footprint', Fp) + '}';-End;--Function CmdPlaceOnSch(Lines : TStringList) : String;-Var-    SLib   : ISch_Lib;-    Iter   : ISch_Iterator;-    PIt    : ISch_Iterator;-    LibCmp : ISch_Component;-    Inst   : ISch_Component;-    Prim   : ISch_GraphicalObject;-    Cpy    : ISch_GraphicalObject;-    SD     : IServerDocument;-    Sheet  : ISch_Document;-    SchPath, LibRef, Missing, Placed, Title, DocNum, Rev, Drawn, Desc : String;-    NCopied, K, W, H, XK, YK, BCX, BCY, BH : Integer;-    PWs, CXs, CYs, TPs, BTs : TStringList;-    R : TCoordRect;-    TotalW, MaxH, Gap, Margin, GapA, MarginA, Cur, DX, DY, BBL, BBB, BBR, BBT, BTop, BBot : Integer;-    Append : Boolean;-    EIt    : ISch_Iterator;-    ECmp   : ISch_Component;-    NExist, ExtX, SumY, GW : Integer;-    Grown  : Boolean;-    RefList : TStringList;-    RIt    : ISch_Iterator;-    LRect  : ISch_Rectangle;-    Pm     : ISch_Parameter;-    TF     : ISch_TextFrame;-Begin-    SchPath := Param(Lines, 'sch');-    LibRef := Param(Lines, 'libref');-    Title := Param(Lines, 'title');-    DocNum := Param(Lines, 'number');-    Rev := Param(Lines, 'revision');-    Drawn := Param(Lines, 'drawnBy');-    Desc := Param(Lines, 'description');-    { libref may be a ';' list: every symbol lands on ONE new sheet as U1..Un, 5000 mil apart-      (John, 2026-09-19: three ToF sensors on one schematic for the demo). }-    RefList := SplitSemi(LibRef);-    SLib := SchServer.GetCurrentSchDocument;-    If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the SchLib first (it must be the current document)') + '}';-        Exit;-    End;-    Missing := '';-    For K := 0 To RefList.Count - 1 Do-    Begin-        LibCmp := Nil;-        Iter := SLib.SchLibIterator_Create;-        Iter.AddFilter_ObjectSet(MkSet(eSchComponent));-        LibCmp := Iter.FirstSchObject;-        While LibCmp <> Nil Do-        Begin-            If UpperCase(LibCmp.LibReference) = UpperCase(RefList[K]) Then Break;-            LibCmp := Iter.NextSchObject;-        End;-        SLib.SchIterator_Destroy(Iter);-        If LibCmp = Nil Then Missing := Missing + RefList[K] + ' ';-    End;-    If (RefList.Count = 0) Or (Missing <> '') Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','component not found in SchLib: ' + Trim(Missing + LibRef)) + '}';-        RefList.Free;-        Exit;-    End;-    { Append mode (issue 11): sch names an EXISTING .SchDoc -> open it and add the parts to-      the right of what is there; the sheet keeps its size and title block. }-    SD := Nil;-    Append := (SchPath <> '') And FileExists(SchPath);-    If (SchPath <> '') And (Not Append) Then-    Begin-        SD := Nil;-        Try-            SD := Client.GetDocumentByPath(SchPath);-        Except-            SD := Nil;-        End;-        If SD <> Nil Then Append := True;-    End;-    If Append Then-    Begin-        If SD = Nil Then SD := Client.OpenDocument('SCH', SchPath);-        If SD = Nil Then-        Begin-            Result := '{' + JRaw('ok','false') + ',' + JStr('error','could not open the existing schematic: ' + SchPath) + '}';-            RefList.Free;-            Exit;-        End;-        Client.ShowDocument(SD);-    End-    Else-    Begin-        SD := CreateNewDocumentFromDocumentKind('SCH');-        If SD = Nil Then-        Begin-            Result := '{' + JRaw('ok','false') + ',' + JStr('error','could not create a schematic sheet') + '}';-            RefList.Free;-            Exit;-        End;-        Client.ShowDocument(SD);-    End;-    Sheet := SchServer.GetCurrentSchDocument;-    If Sheet = Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current schematic after open') + '}';-        RefList.Free;-        Exit;-    End;-    { existing symbols: count (designators continue) and the rightmost anchor + height band }-    NExist := 0; ExtX := 0; SumY := 0; Grown := False;-    If Append Then-    Begin-        EIt := Sheet.SchIterator_Create;-        EIt.AddFilter_ObjectSet(MkSet(eSchComponent));-        ECmp := EIt.FirstSchObject;-        While ECmp <> Nil Do-        Begin-            NExist := NExist + 1;-            If (NExist = 1) Or (Round(CoordToMils(ECmp.Location.X)) > ExtX) Then ExtX := Round(CoordToMils(ECmp.Location.X));-            SumY := SumY + Round(CoordToMils(ECmp.Location.Y));-            ECmp := EIt.NextSchObject;-        End;-        Sheet.SchIterator_Destroy(EIt);-    End;-    { Pass 1: MEASURE every part (union of its primitives' bounding rectangles, pins-      included, +250 mil a side for the pin-number text that hangs outside the stub).-      John, 2026-09-19, reviewing three 5000 mil bodies squeezed onto one sheet:-      "you are smooshing these symbols together such that their pin names overlap; no-      real EE would ever do that, they'd space them out". So the sheet is sized FROM the-      parts, never the parts from the sheet. }-    PWs := TStringList.Create; CXs := TStringList.Create; CYs := TStringList.Create;-    TPs := TStringList.Create; BTs := TStringList.Create;-    TotalW := 0; MaxH := 0;-    For K := 0 To RefList.Count - 1 Do-    Begin-        LibCmp := Nil;-        Iter := SLib.SchLibIterator_Create;-        Iter.AddFilter_ObjectSet(MkSet(eSchComponent));-        LibCmp := Iter.FirstSchObject;-        While LibCmp <> Nil Do-        Begin-            If UpperCase(LibCmp.LibReference) = UpperCase(RefList[K]) Then Break;-            LibCmp := Iter.NextSchObject;-        End;-        SLib.SchIterator_Destroy(Iter);-        BBL := 0; BBB := 0; BBR := 0; BBT := 0; BTop := 0; BBot := 0; NCopied := 0;-        PIt := LibCmp.SchIterator_Create;-        Prim := PIt.FirstSchObject;-        While Prim <> Nil Do-        Begin-            If (Prim.ObjectID = ePin) Or (Prim.ObjectID = eRectangle) Or-               (Prim.ObjectID = eLine) Or (Prim.ObjectID = eArc) Or-               (Prim.ObjectID = eEllipse) Or (Prim.ObjectID = ePolygon) Or-               (Prim.ObjectID = ePolyline) Or (Prim.ObjectID = eLabel) Then-            Begin-                R := Prim.BoundingRectangle;-                If (NCopied = 0) Or (Round(CoordToMils(R.Left)) < BBL) Then BBL := Round(CoordToMils(R.Left));-                If (NCopied = 0) Or (Round(CoordToMils(R.Bottom)) < BBB) Then BBB := Round(CoordToMils(R.Bottom));-                If (NCopied = 0) Or (Round(CoordToMils(R.Right)) > BBR) Then BBR := Round(CoordToMils(R.Right));-                If (NCopied = 0) Or (Round(CoordToMils(R.Top)) > BBT) Then BBT := Round(CoordToMils(R.Top));-                If Prim.ObjectID = eRectangle Then-                Begin-                    BTop := Round(CoordToMils(R.Top)); BBot := Round(CoordToMils(R.Bottom));-                End;-                NCopied := NCopied + 1;-            End;-            Prim := PIt.NextSchObject;-        End;-        LibCmp.SchIterator_Destroy(PIt);-        If BTop = 0 Then Begin BTop := BBT; BBot := BBB; End;-        PWs.Add(IntToStr((BBR - BBL) + 500));-        CXs.Add(IntToStr((BBL + BBR) Div 2));-        CYs.Add(IntToStr((BBB + BBT) Div 2));-        TPs.Add(IntToStr(BTop)); BTs.Add(IntToStr(BBot));-        TotalW := TotalW + (BBR - BBL) + 500;-        If (BBT - BBB) > MaxH Then MaxH := BBT - BBB;-    End;-    { Sheet from the parts: 1000 mil margins, at least 1500 mil of air between parts, and-      room above (1700 mil description strip) and below (title block) the row. Sheet.SheetStyle-      does NOT take on ADDevelop (measured 2026-09-19: the sheet stayed the 9500 x 7500 mil-      default), so the sheet is a CUSTOM size, never smaller than A4 (11500 x 7600). }-    { John, 2026-09-19 (after the 1500 mil cut): "put those symbols a bit closer together so-      we get a better screenshot" - 500 mil of air between parts, 600 mil margins, and the-      sheet hugs the row so a zoom-all fills the frame. }-    Gap := 500; Margin := 600;-    W := TotalW + Gap * (RefList.Count - 1) + 2 * Margin;-    If W < 11500 Then W := 11500;-    H := MaxH + 3600;-    If H < 7600 Then H := 7600;-    If H * 2 < W Then H := W Div 2;-    If Not Append Then-    Begin-    Sheet.UseCustomSheet := True;-    Sheet.CustomX := MilsToCoord(W);-    Sheet.CustomY := MilsToCoord(H);-    Sheet.CustomXZones := (W + 2499) Div 2500;-    Sheet.CustomYZones := (H + 2499) Div 2500;-    Sheet.CustomMarginWidth := MilsToCoord(200);-    End;-    { Do NOT read SheetSizeX/Y back here: on ADDevelop they still report the 9500 x 7500 mil-      default after the custom size is set (measured 2026-09-19: the read-back shrank W to-      9500 and squeezed three 5800 mil parts into it, pin numbers on top of each other,-      while the sheet itself drew at the custom size). W and H ARE the sheet. }-    { spread the slack evenly: the row is centred and every gap grows by the same amount }-    GapA := Gap; MarginA := Margin;-    If W > TotalW + Gap * (RefList.Count - 1) + 2 * Margin Then-    Begin-        GapA := Gap + (W - (TotalW + Gap * (RefList.Count - 1) + 2 * Margin)) Div (RefList.Count + 1);-        MarginA := (W - TotalW - GapA * (RefList.Count - 1)) Div 2;-    End;-    Placed := '';-    NCopied := 0;-    Cur := MarginA;-    If Append Then Cur := ExtX + 4000;-    For K := 0 To RefList.Count - 1 Do-    Begin-    XK := Cur + AdomInt(PWs[K]) Div 2;-    Cur := Cur + AdomInt(PWs[K]) + GapA;-    { the row sits a little below centre: the description strip takes the top 1700 mil;-      appending, it sits in line with the existing symbols instead }-    YK := (H Div 2) - 300;-    If Append Then-    Begin-        If NExist > 0 Then YK := SumY Div NExist Else YK := 3500;-    End;-    DX := XK - AdomInt(CXs[K]);-    DY := YK - AdomInt(CYs[K]);-    LibCmp := Nil;-    Iter := SLib.SchLibIterator_Create;-    Iter.AddFilter_ObjectSet(MkSet(eSchComponent));-    LibCmp := Iter.FirstSchObject;-    While LibCmp <> Nil Do-    Begin-        If UpperCase(LibCmp.LibReference) = UpperCase(RefList[K]) Then Break;-        LibCmp := Iter.NextSchObject;-    End;-    SLib.SchIterator_Destroy(Iter);-    { Build a fresh component instance and copy the library part's primitives into it.-      ISch_Component.Replicate does NOT re-parent the pins/rectangle onto a normal-      sheet (measured 2026-08-30: only the designator/comment text placed), so copy-      each child primitive with primitive-level Replicate + AddSchObject. }-    Inst := SchServer.SchObjectFactory(eSchComponent, eCreate_Default);-    Inst.CurrentPartID := 1;-    Inst.DisplayMode := 0;-    Inst.LibReference := LibCmp.LibReference;-    Inst.ComponentDescription := LibCmp.ComponentDescription;-    Inst.Location := Point(MilsToCoord(XK), MilsToCoord(YK));-    { body rectangle centre (mil, library frame) for the designator / comment columns }-    BCX := AdomInt(CXs[K]); BCY := AdomInt(CYs[K]);-    RIt := LibCmp.SchIterator_Create;-    RIt.AddFilter_ObjectSet(MkSet(eRectangle));-    LRect := RIt.FirstSchObject;-    If LRect <> Nil Then-        BCX := Round(CoordToMils(LRect.Location.X + LRect.Corner.X) / 2);-    LibCmp.SchIterator_Destroy(RIt);-    PIt := LibCmp.SchIterator_Create;-    Prim := PIt.FirstSchObject;-    While Prim <> Nil Do-    Begin-        If (Prim.ObjectID = ePin) Or (Prim.ObjectID = eRectangle) Or-           (Prim.ObjectID = eLine) Or (Prim.ObjectID = eArc) Or-           (Prim.ObjectID = eEllipse) Or (Prim.ObjectID = ePolygon) Or-           (Prim.ObjectID = ePolyline) Or (Prim.ObjectID = eLabel) Then-        Begin-            Cpy := Prim.Replicate;-            Cpy.OwnerPartId := 1;-            Cpy.OwnerPartDisplayMode := 0;-            { copied primitives keep the library coordinates, Inst.Location does not carry them-              (measured 2026-09-19: three parts drew on top of each other), so shift each child }-            Cpy.MoveByXY(MilsToCoord(DX), MilsToCoord(DY));-            Inst.AddSchObject(Cpy);-            NCopied := NCopied + 1;-        End;-        Prim := PIt.NextSchObject;-    End;-    LibCmp.SchIterator_Destroy(PIt);-    { Designator centred ABOVE the body and the comment centred BELOW it, whatever the-      library part did with them (measured 2026-09-19: +200 mil put U1 on top of the chip-      outline inside a 3700 mil body). }-    Inst.Designator.Text := 'U' + IntToStr(NExist + K + 1);-    Inst.Designator.IsHidden := False;-    Inst.Designator.Justification := eJustify_BottomCenter;-    Inst.Designator.Location := Point(MilsToCoord(BCX + DX), MilsToCoord(AdomInt(TPs[K]) + DY + 100));-    { never leave the star placeholder: the comment is the part number }-    Inst.Comment.Text := LibCmp.LibReference;-    Inst.Comment.IsHidden := False;-    Inst.Comment.Justification := eJustify_TopCenter;-    Inst.Comment.Location := Point(MilsToCoord(BCX + DX), MilsToCoord(AdomInt(BTs[K]) + DY - 100));-    Sheet.RegisterSchObjectInContainer(Inst);-    SchServer.RobotManager.SendMessage(Sheet.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, Inst.I_ObjectAddress);-    If Placed <> '' Then Placed := Placed + ';';-    Placed := Placed + RefList[K];-    End;-    { Appended parts extend the row to the right; grow the sheet so they stay inside the-      drawing frame (a fixed sheet left the appended part out in the gray, measured-      2026-10-15). Never shrinks a sheet. }-    If Append Then-    Begin-        GW := Cur + 600;-        If Sheet.UseCustomSheet Then-        Begin-            If GW > Round(CoordToMils(Sheet.CustomX)) Then-            Begin-                Sheet.CustomX := MilsToCoord(GW);-                Sheet.CustomXZones := (GW + 2499) Div 2500;-                Grown := True;-            End;-        End-        Else-        Begin-            Sheet.UseCustomSheet := True;-            Sheet.CustomX := MilsToCoord(GW);-            Sheet.CustomY := MilsToCoord(H);-            Sheet.CustomXZones := (GW + 2499) Div 2500;-            Sheet.CustomYZones := (H + 2499) Div 2500;-            Sheet.CustomMarginWidth := MilsToCoord(200);-            Grown := True;-        End;-        If Grown Then W := GW;-    End;-    { Title block: the standard sheet renders the document parameters Title, DocumentNumber,-      Revision and DrawnBy through its special strings; an empty block is a demo defect. }-    If Append Then-    Begin-        Title := ''; DocNum := ''; Rev := ''; Drawn := ''; Desc := '';-    End;-    If Title <> '' Then SetDocParam(Sheet, 'Title', Title);-    If DocNum <> '' Then SetDocParam(Sheet, 'DocumentNumber', DocNum);-    If Rev <> '' Then SetDocParam(Sheet, 'Revision', Rev);-    If Drawn <> '' Then SetDocParam(Sheet, 'DrawnBy', Drawn);-    If Desc <> '' Then-    Begin-        SetDocParam(Sheet, 'Description', Desc);-        TF := SchServer.SchObjectFactory(eTextFrame, eCreate_Default);-        { inside the drawing frame: the border + zone strip take ~400 mil, so the frame-          runs from 700 mil below the top edge down 1000 mil, and stops 700 mil short of-          the right edge }-        TF.Location := Point(MilsToCoord(1000), MilsToCoord(H - 1700));-        TF.Corner := Point(MilsToCoord(W - 700), MilsToCoord(H - 700));-        TF.Text := Desc;-        TF.WordWrap := True;-        TF.ShowBorder := False;-        Sheet.RegisterSchObjectInContainer(TF);-        SchServer.RobotManager.SendMessage(Sheet.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, TF.I_ObjectAddress);-    End;-    Sheet.GraphicallyInvalidate;-    ResetParameters; AddStringParameter('Action','All'); RunProcess('Sch:Zoom');-    If SchPath <> '' Then-    Begin-        If Not Append Then Begin FreePath(SchPath); SD.SetFileName(SchPath); End;-        SD.DoFileSave('');-    End;-    PWs.Free; CXs.Free; CYs.Free; TPs.Free; BTs.Free;-    Result := '{' + JRaw('ok','true') + ',' + JStr('sch', SchPath) + ',' + JRaw('sheetMil', '[' + IntToStr(W) + ',' + IntToStr(H) + ']') + ',' +-        JRaw('titleBlock', '{' + JRaw('title', BoolStr(Title <> '')) + ',' + JRaw('number', BoolStr(DocNum <> '')) + ',' + JRaw('revision', BoolStr(Rev <> '')) + ',' + JRaw('drawnBy', BoolStr(Drawn <> '')) + ',' + JRaw('description', BoolStr(Desc <> '')) + '}') + ',' +-        JStr('placed', Placed) + ',' + JStr('designator','U' + IntToStr(NExist + 1)) + ',' + JRaw('appended', BoolStr(Append)) + ',' + JRaw('sheetGrown', BoolStr(Grown)) + ',' + JRaw('existingParts', IntToStr(NExist)) + ',' + JRaw('count', IntToStr(RefList.Count)) + ',' + JRaw('primitives', IntToStr(NCopied)) + '}';-    RefList.Free;-End;--Function CmdPlaceOnPcb(Lines : TStringList) : String;-Var-    PLib   : IPCB_Library;-    LibFp  : IPCB_LibComponent;-    Inst   : IPCB_Component;-    Board  : IPCB_Board;-    SD     : IServerDocument;-    GIt    : IPCB_GroupIterator;-    Prim   : IPCB_Primitive;-    Cpy    : IPCB_Primitive;-    PcbPath, FpName : String;-    I, NCopied : Integer;-    Found  : Boolean;-    R      : TCoordRect;-    DX, DY, CL, CB, CR, CT : Integer;-    AnyP   : Boolean;-Begin-    PcbPath := Param(Lines, 'pcb');-    FpName := Param(Lines, 'footprint');-    PLib := PCBServer.GetCurrentPCBLibrary;-    If PLib = Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the PcbLib first (it must be the current document)') + '}';-        Exit;-    End;-    Found := False;-    For I := 0 To PLib.ComponentCount - 1 Do-    Begin-        LibFp := PLib.GetComponent(I);-        If UpperCase(LibFp.Name) = UpperCase(FpName) Then Begin Found := True; Break; End;-    End;-    If Not Found Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','footprint not found in PcbLib: ' + FpName) + '}';-        Exit;-    End;-    { ALWAYS create a fresh PcbDoc: with a PcbLib focused, GetCurrentPCBBoard-      returns the LIBRARY internal board - a reuse path put the part into the lib-      canvas and renamed the lib doc, measured 2026-08-30. }-    SD := CreateNewDocumentFromDocumentKind('PCB');-    If SD = Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','could not create a PCB document') + '}';-        Exit;-    End;-    Client.ShowDocument(SD);-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board') + '}';-        Exit;-    End;-    Board.LayerStack.LayerObject[eMechanical1].MechanicalLayerEnabled := True;-    Board.LayerIsDisplayed[eMechanical1] := True;-    WriteAtomic(WORKSPACE + '\stage.txt', 'board_ready');-    { Group Replicate of an IPCB_LibComponent DISSOLVES on a board: census showed-      16 free pads, no eComponentObject, no body - measured 2026-08-30. Build a-      fresh board component and copy each child primitive, the same cure the-      schematic placement needed. }-    PCBServer.PreProcess;-    Inst := PCBServer.PCBObjectFactory(eComponentObject, eNoDimension, eCreate_Default);-    Inst.Layer := eTopLayer;-    { The factory component's auto Designator1/Comment text is enormous and-      hijacks Zoom All - hide both. }-    Inst.NameOn := False;-    Inst.CommentOn := False;-    { ORDER MATTERS: fill the group BEFORE the board add. Children added to an-      already-registered component exist in data but never draw, whatever address-      they are robot-registered to (measured 2026-08-30). This mirrors-      create_footprint, where pads go in before RegisterComponent. }-    NCopied := 0;-    GIt := LibFp.GroupIterator_Create;-    GIt.AddFilter_LayerSet(AllLayers);-    Prim := GIt.FirstPCBObject;-    While Prim <> Nil Do-    Begin-        Cpy := Prim.Replicate;-        Inst.AddPCBObject(Cpy);-        NCopied := NCopied + 1;-        Prim := GIt.NextPCBObject;-    End;-    LibFp.GroupIterator_Destroy(GIt);-    Board.AddPCBObject(Inst);-    PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Inst.I_ObjectAddress);-    WriteAtomic(WORKSPACE + '\stage.txt', 'component_registered');-    WriteAtomic(WORKSPACE + '\stage.txt', 'children_copied');-    { Copied children keep their LIBRARY coordinates (lib origin ~50000,50000 mil);-      anchor the fresh component onto them BEFORE the move so MoveToXY translates-      the whole group to the target (bbox was 50146 mil wide without this). }-    { Anchor the component BEFORE normalizing children: assigning Inst.x/y-      afterwards re-translates the whole group by the same delta (measured-      2026-08-30: everything landed at 8000,6000 instead of 4000,3000). }-    Inst.x := MilsToCoord(4000);-    Inst.y := MilsToCoord(3000);-    { Normalize positions: the lib stores pads around the canvas origin-      (~50000,50000 mil) but an authored body contour around 0,0 - one group move-      cannot land both (measured 2026-08-30: body at 4000, pads at 54000). Move-      the pad cluster and each body to the target independently. }-    AnyP := False;-    CL := 0; CB := 0; CR := 0; CT := 0;-    GIt := Inst.GroupIterator_Create;-    GIt.AddFilter_ObjectSet(MkSet(ePadObject));-    GIt.AddFilter_LayerSet(AllLayers);-    Prim := GIt.FirstPCBObject;-    While Prim <> Nil Do-    Begin-        R := Prim.BoundingRectangle;-        If Not AnyP Then-        Begin-            CL := R.Left; CB := R.Bottom; CR := R.Right; CT := R.Top; AnyP := True;-        End-        Else-        Begin-            If R.Left < CL Then CL := R.Left;-            If R.Bottom < CB Then CB := R.Bottom;-            If R.Right > CR Then CR := R.Right;-            If R.Top > CT Then CT := R.Top;-        End;-        Prim := GIt.NextPCBObject;-    End;-    Inst.GroupIterator_Destroy(GIt);-    DX := 0; DY := 0;-    If AnyP Then-    Begin-        DX := MilsToCoord(4000) - ((CL + CR) Div 2);-        DY := MilsToCoord(3000) - ((CB + CT) Div 2);-    End;-    GIt := Inst.GroupIterator_Create;-    GIt.AddFilter_LayerSet(AllLayers);-    Prim := GIt.FirstPCBObject;-    While Prim <> Nil Do-    Begin-        If Prim.ObjectId = eComponentBodyObject Then-        Begin-            R := Prim.BoundingRectangle;-            Prim.MoveByXY(MilsToCoord(4000) - ((R.Left + R.Right) Div 2),-                          MilsToCoord(3000) - ((R.Bottom + R.Top) Div 2));-        End-        Else If Prim.ObjectId <> eTextObject Then-            Prim.MoveByXY(DX, DY);-        Prim := GIt.NextPCBObject;-    End;-    Inst.GroupIterator_Destroy(GIt);-    { Hidden auto Name/Comment texts otherwise bloat the bbox by ~50000 mil. }-    Inst.Name.MoveToXY(MilsToCoord(4000), MilsToCoord(3250));-    Inst.Comment.MoveToXY(MilsToCoord(4000), MilsToCoord(2750));-    PCBServer.PostProcess;-    WriteAtomic(WORKSPACE + '\stage.txt', 'moved');-    Board.ViewManager_FullUpdate;-    ResetParameters; AddStringParameter('Action','All'); RunProcess('PCB:Zoom');-    If PcbPath <> '' Then-    Begin-        FreePath(PcbPath);-        SD.SetFileName(PcbPath);-        SD.DoFileSave('');-    End;-    WriteAtomic(WORKSPACE + '\stage.txt', 'saved');-    Result := '{' + JRaw('ok','true') + ',' + JStr('pcb', PcbPath) + ',' + JStr('placed', FpName) + ',' +-        JStr('libFile', PLib.Board.FileName) + ',' +-        JRaw('libBodies', IntToStr(GroupCount(LibFp, eComponentBodyObject))) + ',' +-        JRaw('primitives', IntToStr(NCopied)) + ',' +-        JRaw('padsOnComp', IntToStr(GroupCount(Inst, ePadObject))) + ',' +-        JRaw('bodiesOnComp', IntToStr(GroupCount(Inst, eComponentBodyObject))) + '}';-End;--Function BoardCensus(Board : IPCB_Board) : String;-Var-    BIt  : IPCB_BoardIterator;-    Prim : IPCB_Primitive;-    N    : Integer;-    S    : String;-Begin-    N := 0;-    S := '';-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(AllObjects);-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Prim := BIt.FirstPCBObject;-    While (Prim <> Nil) And (N < 200) Do-    Begin-        N := N + 1;-        If S <> '' Then S := S + ';';-        S := S + IntToStr(Prim.ObjectId);-        Prim := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-    Result := IntToStr(N) + ':' + S;-End;---Function GroupPadBbox(Grp : IPCB_Group) : String;-Var-    GIt  : IPCB_GroupIterator;-    Prim : IPCB_Primitive;-    R    : TCoordRect;-    L, B, Rt, T : Integer;-    Any  : Boolean;-Begin-    Any := False;-    L := 0; B := 0; Rt := 0; T := 0;-    GIt := Grp.GroupIterator_Create;-    GIt.AddFilter_ObjectSet(MkSet(ePadObject));-    GIt.AddFilter_LayerSet(AllLayers);-    Prim := GIt.FirstPCBObject;-    While Prim <> Nil Do-    Begin-        R := Prim.BoundingRectangle;-        If Not Any Then-        Begin-            L := R.Left; B := R.Bottom; Rt := R.Right; T := R.Top; Any := True;-        End-        Else-        Begin-            If R.Left < L Then L := R.Left;-            If R.Bottom < B Then B := R.Bottom;-            If R.Right > Rt Then Rt := R.Right;-            If R.Top > T Then T := R.Top;-        End;-        Prim := GIt.NextPCBObject;-    End;-    Grp.GroupIterator_Destroy(GIt);-    If Not Any Then-    Begin-        Result := '[]';-        Exit;-    End;-    Result := '[' + IntToStr(Round(CoordToMils(L))) + ',' + IntToStr(Round(CoordToMils(B))) + ',' +-        IntToStr(Round(CoordToMils(Rt))) + ',' + IntToStr(Round(CoordToMils(T))) + ']';-End;---{ Net names on the pads that matter for the swap proof: "des=net;..." for pads-  1 and 49 (every pad named 49 listed), plus the track count and their nets. }-Function CompNets(Comp : IPCB_Component) : String;-Var-    PadIt : IPCB_GroupIterator;-    Pad : IPCB_Pad;-    S, N : String;-Begin-    S := '';-    PadIt := Comp.GroupIterator_Create;-    PadIt.AddFilter_ObjectSet(MkSet(ePadObject));-    PadIt.AddFilter_LayerSet(AllLayers);-    Pad := PadIt.FirstPCBObject;-    While Pad <> Nil Do-    Begin-        If (Pad.Name = '1') Or (Pad.Name = '49') Then-        Begin-            N := '';-            If Pad.Net <> Nil Then N := Pad.Net.Name;-            If S <> '' Then S := S + ';';-            S := S + Pad.Name + '=' + N;-        End;-        Pad := PadIt.NextPCBObject;-    End;-    Comp.GroupIterator_Destroy(PadIt);-    Result := S;-End;--Function BoardTracks(Board : IPCB_Board) : String;-Var-    BIt : IPCB_BoardIterator;-    T   : IPCB_Track;-    S, N : String;-Begin-    S := '';-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eTrackObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    T := BIt.FirstPCBObject;-    While T <> Nil Do-    Begin-        N := '';-        If T.Net <> Nil Then N := T.Net.Name;-        If S <> '' Then S := S + ';';-        S := S + N + '@' + IntToStr(Round(CoordToMils(T.X1))) + ',' + IntToStr(Round(CoordToMils(T.Y1)));-        T := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-    Result := S;-End;--{ The board outline's extent in mil as '[left,bottom,right,top]', read from the outline's own-  vertices. BoardOutline.BoundingRectangle is a cached rectangle that does not follow a reshape-  in the same session on AD 26.9.1 or Altium Develop (it kept the 6000 x 4000 mil default while-  the view showed the new shape, measured 2026-10-15); the vertices are what the view draws. }-Function OutlineMilFromVertices(BO : IPCB_BoardOutline) : String;-Var-    Seg : TPolySegment;-    K, L, B, R, T : Integer;-Begin-    L := 0; B := 0; R := 0; T := 0;-    For K := 0 To BO.PointCount - 1 Do-    Begin-        Seg := BO.Segments[K];-        If (K = 0) Or (Seg.vx < L) Then L := Seg.vx;-        If (K = 0) Or (Seg.vy < B) Then B := Seg.vy;-        If (K = 0) Or (Seg.vx > R) Then R := Seg.vx;-        If (K = 0) Or (Seg.vy > T) Then T := Seg.vy;-    End;-    Result := '[' + IntToStr(Round(CoordToMils(L))) + ',' + IntToStr(Round(CoordToMils(B))) + ',' +-        IntToStr(Round(CoordToMils(R))) + ',' + IntToStr(Round(CoordToMils(T))) + ']';-End;--Function CmdPcbReport(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    PLib  : IPCB_Library;-    Fp    : IPCB_LibComponent;-    Comp  : IPCB_Component;-    BIt   : IPCB_BoardIterator;-    R     : TCoordRect;-    Name, S, OM : String;-    I     : Integer;-Begin-    PLib := PCBServer.GetCurrentPCBLibrary;-    Name := Param(Lines, 'name');-    { GetCurrentPCBLibrary answers even when a BOARD is focused; want=board skips-      the library branch so the report describes the focused board. }-    If Param(Lines, 'want') = 'board' Then PLib := Nil;-    If PLib <> Nil Then-    Begin-        S := '';-        For I := 0 To PLib.ComponentCount - 1 Do-        Begin-            Fp := PLib.GetComponent(I);-            If (Name = '') Or (UpperCase(Fp.Name) = UpperCase(Name)) Then-            Begin-                If S <> '' Then S := S + ',';-                S := S + '{' + JStr('name', Fp.Name) + ',' +-                    JRaw('pads', IntToStr(GroupCount(Fp, ePadObject))) + ',' +-                    JRaw('padsBboxMil', GroupPadBbox(Fp)) + ',' +-                    JStr('census', GroupCensus(Fp)) + ',' +-                    JRaw('bodies', GroupBodies(Fp)) + '}';-            End;-        End;-        Result := '{' + JRaw('ok','true') + ',' + JStr('kind','library') + ',' +-            JRaw('components', '[' + S + ']') + ',' +-            JStr('bodyCols','layerId,wMil,hMil,leftMil,bottomMil,standoffMil,overallMil,hasModel') + '}';-        Exit;-    End;-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','current document is neither a PCB library nor a board') + '}';-        Exit;-    End;-    S := '';-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eComponentObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Comp := BIt.FirstPCBObject;-    While Comp <> Nil Do-    Begin-        If S <> '' Then S := S + ',';-        R := Comp.BoundingRectangle;-        S := S + '{' + JStr('pattern', Comp.Pattern) + ',' +-            JRaw('xMil', IntToStr(Round(CoordToMils(Comp.x)))) + ',' +-            JRaw('yMil', IntToStr(Round(CoordToMils(Comp.y)))) + ',' +-            JRaw('wMil', IntToStr(Round(CoordToMils(R.Right - R.Left)))) + ',' +-            JRaw('pads', IntToStr(GroupCount(Comp, ePadObject))) + ',' +-            JRaw('padsBboxMil', GroupPadBbox(Comp)) + ',' +-            JStr('nets', CompNets(Comp)) + ',' +-            JRaw('bodies', GroupBodies(Comp)) + '}';-        Comp := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-    { A library's internal board has no outline; touching BoundingRectangle on the-      nil BoardOutline halts the engine (measured 2026-08-30). }-    If Board.BoardOutline <> Nil Then-        OM := OutlineMilFromVertices(Board.BoardOutline)-    Else-    Begin-        R := Board.BoundingRectangle;-        OM := '[' + IntToStr(Round(CoordToMils(R.Left))) + ',' + IntToStr(Round(CoordToMils(R.Bottom))) + ',' +-             IntToStr(Round(CoordToMils(R.Right))) + ',' + IntToStr(Round(CoordToMils(R.Top))) + ']';-    End;-    Result := '{' + JRaw('ok','true') + ',' + JStr('kind','board') + ',' +-        JRaw('outlineMil', OM) + ',' +-        JRaw('components', '[' + S + ']') + ',' +-        JStr('census', BoardCensus(Board)) + ',' +-        JStr('tracks', BoardTracks(Board)) + ',' +-        JStr('bodyCols','layerId,wMil,hMil,leftMil,bottomMil,standoffMil,overallMil,hasModel') + '}';-End;--{ ------------------------------------------------ build_footprint (spec file) --- }--{ Pipe-separated field N (1-based) of S. }-Function FieldP(S : String; N : Integer) : String;-Var-    I, Cnt, Start : Integer;-Begin-    Cnt := 1; Start := 1; Result := '';-    For I := 1 To Length(S) Do-    Begin-        If S[I] = '|' Then-        Begin-            If Cnt = N Then Begin Result := Copy(S, Start, I - Start); Exit; End;-            Cnt := Cnt + 1; Start := I + 1;-        End;-    End;-    If Cnt = N Then Result := Copy(S, Start, Length(S) - Start + 1);-End;--Function MmC(S : String) : Integer;-Begin-    Result := MMsToCoord(AdomFloat(S));-End;--Function SpecLayer(Tag : String) : Integer;-Begin-    If Tag = 'overlay' Then Result := eTopOverlay-    Else If Tag = 'fab' Then Result := eMechanical13-    Else If Tag = 'courtyard' Then Result := eMechanical15-    Else If Tag = 'balls' Then Result := eMechanical9-    Else If Tag = 'comment' Then Result := eMechanical1-    Else If Tag = 'multi' Then Result := eMultiLayer-    Else Result := eTopLayer;-End;--{ Build a footprint from a spec file (tools/kicad2altium.py output):-  META|name|desc  PAD|des|x|y|w|h|shape|hole|layer|crpct  LINE|layer|x1|y1|x2|y2|w-  ARC|layer|cx|cy|r|w|a0|a1  DISC|layer|cx|cy|r.  Units mm, Y up. Args:-  libraryPath, spec=<windows path>. Pads with a repeated designator are legal in-  Altium and all map to that pin (the heatsink-via convention). }-Function CmdBuildFootprint(Lines : TStringList) : String;-Var-    PLib  : IPCB_Library;-    Comp  : IPCB_LibComponent;-    Pad   : IPCB_Pad;-    Trk   : IPCB_Track;-    Arc   : IPCB_Arc;-    Spec  : TStringList;-    I, NPads, NTh, NPrims : Integer;-    L, Kind, Name, Desc, LibState, LibPath, SpecPath, Shape : String;-    R : Integer;-Begin-    LibPath := Param(Lines, 'libraryPath');-    SpecPath := Param(Lines, 'spec');-    If (LibPath = '') Or (SpecPath = '') Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','libraryPath and spec are required') + '}';-        Exit;-    End;-    LibState := EnsureLibrary('PCBLIB', LibPath);-    PLib := PCBServer.GetCurrentPCBLibrary;-    If PLib = Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','no PCB library is current (' + LibState + ')') + '}';-        Exit;-    End;-    Spec := TStringList.Create;-    Spec.LoadFromFile(SpecPath);-    Name := ''; Desc := '';-    NPads := 0; NTh := 0; NPrims := 0;-    { mech layers this spec may use: enable + name them once per library }-    PLib.Board.LayerStack.LayerObject[eMechanical1].MechanicalLayerEnabled := True;-    PLib.Board.LayerStack.LayerObject[eMechanical9].MechanicalLayerEnabled := True;-    PLib.Board.LayerStack.LayerObject[eMechanical9].Name := 'Adom.SolderBalls';-    PLib.Board.LayerStack.LayerObject[eMechanical13].MechanicalLayerEnabled := True;-    PLib.Board.LayerStack.LayerObject[eMechanical13].Name := 'Fab';-    PLib.Board.LayerStack.LayerObject[eMechanical15].MechanicalLayerEnabled := True;-    PLib.Board.LayerStack.LayerObject[eMechanical15].Name := 'Courtyard';-    PLib.Board.LayerIsDisplayed[eMechanical9] := False;-    PLib.Board.LayerIsDisplayed[eMechanical13] := True;-    PLib.Board.LayerIsDisplayed[eMechanical15] := True;-    PCBServer.PreProcess;-    Comp := PCBServer.CreatePCBLibComp;-    For I := 0 To Spec.Count - 1 Do-    Begin-        L := Spec[I];-        Kind := FieldP(L, 1);-        If Kind = 'META' Then-        Begin-            Name := FieldP(L, 2); Desc := FieldP(L, 3);-            Comp.Name := Name; Comp.Description := Desc;-        End-        Else If Kind = 'PAD' Then-        Begin-            Pad := PCBServer.PCBObjectFactory(ePadObject, eNoDimension, eCreate_Default);-            Pad.Name := FieldP(L, 2);-            Pad.X := MmC(FieldP(L, 3));-            Pad.Y := MmC(FieldP(L, 4));-            Pad.TopXSize := MmC(FieldP(L, 5));-            Pad.TopYSize := MmC(FieldP(L, 6));-            Shape := FieldP(L, 7);-            If Shape = 'rect' Then Pad.TopShape := eRectangular-            Else If Shape = 'roundrect' Then Pad.TopShape := eRoundedRectangular-            Else Pad.TopShape := eRounded;-            Pad.HoleSize := MmC(FieldP(L, 8));-            If FieldP(L, 9) = 'multi' Then-            Begin-                Pad.Layer := eMultiLayer;-                NTh := NTh + 1;-            End-            Else Pad.Layer := eTopLayer;-            R := StrToIntDef(FieldP(L, 10), 0);-            If (Shape = 'roundrect') And (R > 0) Then-                Pad.SetState_StackCRPctOnLayer(eTopLayer, R);-            Comp.AddPCBObject(Pad);-            NPads := NPads + 1;-        End-        Else If Kind = 'LINE' Then-        Begin-            Trk := PCBServer.PCBObjectFactory(eTrackObject, eNoDimension, eCreate_Default);-            Trk.Layer := SpecLayer(FieldP(L, 2));-            Trk.X1 := MmC(FieldP(L, 3)); Trk.Y1 := MmC(FieldP(L, 4));-            Trk.X2 := MmC(FieldP(L, 5)); Trk.Y2 := MmC(FieldP(L, 6));-            Trk.Width := MmC(FieldP(L, 7));-            Comp.AddPCBObject(Trk);-            NPrims := NPrims + 1;-        End-        Else If Kind = 'ARC' Then-        Begin-            Arc := PCBServer.PCBObjectFactory(eArcObject, eNoDimension, eCreate_Default);-            Arc.Layer := SpecLayer(FieldP(L, 2));-            Arc.XCenter := MmC(FieldP(L, 3)); Arc.YCenter := MmC(FieldP(L, 4));-            Arc.Radius := MmC(FieldP(L, 5));-            Arc.LineWidth := MmC(FieldP(L, 6));-            Arc.StartAngle := AdomFloat(FieldP(L, 7)); Arc.EndAngle := AdomFloat(FieldP(L, 8));-            Comp.AddPCBObject(Arc);-            NPrims := NPrims + 1;-        End-        Else If Kind = 'DISC' Then-        Begin-            { a filled disc of radius r = a full arc at r/2 with stroke r }-            Arc := PCBServer.PCBObjectFactory(eArcObject, eNoDimension, eCreate_Default);-            Arc.Layer := SpecLayer(FieldP(L, 2));-            Arc.XCenter := MmC(FieldP(L, 3)); Arc.YCenter := MmC(FieldP(L, 4));-            Arc.Radius := MmC(FieldP(L, 5)) Div 2;-            Arc.LineWidth := MmC(FieldP(L, 5));-            Arc.StartAngle := 0; Arc.EndAngle := 360;-            Comp.AddPCBObject(Arc);-            NPrims := NPrims + 1;-        End;-    End;-    Spec.Free;-    PLib.RegisterComponent(Comp);-    PLib.CurrentComponent := Comp;-    PCBServer.PostProcess;-    PLib.Board.ViewManager_FullUpdate;-    PLib.RefreshView;-    { no PCB:Zoom here: an interactive process left 'a command active' and the-      next save drew Altium's save-a-copy Warning (measured 2026-08-30). }-    Result := '{' + JRaw('ok', 'true') + ',' + JStr('footprint', Name) + ',' + JStr('description', Desc) + ',' +-        JStr('library', LibPath) + ',' + JStr('libraryState', LibState) + ',' +-        JRaw('pads', IntToStr(NPads)) + ',' + JRaw('throughHolePads', IntToStr(NTh)) + ',' +-        JRaw('drawingPrimitives', IntToStr(NPrims)) + '}';-End;---{ ------------------------------------------- build_symbol_modes (spec file) --- }--{ ONE component with N display modes (the Altium alternate-symbol mechanism:-  Normal + Alternate 1..N-1). Spec (tools/symbol_modes.py), mils, Y up:-  META|libref|desc|designator  MODE|i|name  RECT|mode|x1|y1|x2|y2-  PIN|mode|number|name|x|y|orient|len  LINE|mode|x1|y1|x2|y2-  Args: libraryPath, spec=<windows path>. Every primitive carries-  OwnerPartId 1 and OwnerPartDisplayMode = its mode. }-Function CmdBuildSymbolModes(Lines : TStringList) : String;-Var-    EType : String;-    SLib  : ISch_Lib;-    Comp  : ISch_Component;-    Rect  : ISch_Rectangle;-    Pin   : ISch_Pin;-    Ln    : ISch_Line;-    Ar    : ISch_Arc;-    Spec  : TStringList;-    Old   : ISch_Component;-    OIt   : ISch_Iterator;-    NOld  : Integer;-    I, NModes, NPins, NLines, Md : Integer;-    L, Kind, LibRef, Desc, Des, LibPath, SpecPath, LibState, Orient : String;-Begin-    LibPath := Param(Lines, 'libraryPath');-    SpecPath := Param(Lines, 'spec');-    If (LibPath = '') Or (SpecPath = '') Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','libraryPath and spec are required') + '}';-        Exit;-    End;-    LibState := EnsureLibrary('SCHLIB', LibPath);-    SLib := SchServer.GetCurrentSchDocument;-    If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','no schematic library is current (' + LibState + ')') + '}';-        Exit;-    End;-    Spec := TStringList.Create;-    Spec.LoadFromFile(SpecPath);-    NModes := 0; NPins := 0; NLines := 0;-    LibRef := ''; Desc := ''; Des := 'U';-    Comp := SchServer.SchObjectFactory(eSchComponent, eCreate_Default);-    Comp.CurrentPartID := 1;-    Comp.DisplayMode := 0;-    For I := 0 To Spec.Count - 1 Do-    Begin-        L := Spec[I];-        Kind := FieldP(L, 1);-        If Kind = 'META' Then-        Begin-            LibRef := FieldP(L, 2); Desc := FieldP(L, 3); Des := FieldP(L, 4);-            Comp.LibReference := LibRef;-            Comp.ComponentDescription := Desc;-            Comp.Designator.Text := Des + '?';-            Comp.Comment.Text := LibRef;-        End-        Else If Kind = 'MODE' Then-        Begin-            Md := AdomInt(FieldP(L, 2));-            If Md + 1 > NModes Then NModes := Md + 1;-        End-        Else If Kind = 'RECT' Then-        Begin-            Md := AdomInt(FieldP(L, 2));-            Rect := SchServer.SchObjectFactory(eRectangle, eCreate_Default);-            Rect.OwnerPartId := 1;-            Rect.OwnerPartDisplayMode := Md;-            Rect.Location := Point(MilsToCoord(AdomInt(FieldP(L, 3))), MilsToCoord(AdomInt(FieldP(L, 4))));-            Rect.Corner := Point(MilsToCoord(AdomInt(FieldP(L, 5))), MilsToCoord(AdomInt(FieldP(L, 6))));-            Rect.IsSolid := True;-            Comp.AddSchObject(Rect);-        End-        Else If Kind = 'PIN' Then-        Begin-            Md := AdomInt(FieldP(L, 2));-            Pin := SchServer.SchObjectFactory(ePin, eCreate_Default);-            Pin.OwnerPartId := 1;-            Pin.OwnerPartDisplayMode := Md;-            Pin.Designator := FieldP(L, 3);-            Pin.Name := FieldP(L, 4);-            Pin.PinLength := MilsToCoord(AdomInt(FieldP(L, 8)));-            Orient := FieldP(L, 7);-            { orientation = the direction the pin points AWAY from the body }-            If Orient = 'left' Then Pin.Orientation := eRotate180-            Else If Orient = 'right' Then Pin.Orientation := eRotate0-            Else If Orient = 'up' Then Pin.Orientation := eRotate90-            Else Pin.Orientation := eRotate270;-            Pin.Location := Point(MilsToCoord(AdomInt(FieldP(L, 5))), MilsToCoord(AdomInt(FieldP(L, 6))));-            { the KiCad pin type (field 10): until 0.2.52 every pin was Passive, so Altium's ERC-              and AI Flow's pin-type check could not see an output, an input or a power pin }-            EType := FieldP(L, 10);-            If EType = 'input' Then Pin.Electrical := eElectricInput-            Else If EType = 'output' Then Pin.Electrical := eElectricOutput-            Else If EType = 'bidirectional' Then Pin.Electrical := eElectricIO-            Else If EType = 'tri_state' Then Pin.Electrical := eElectricHiZ-            Else If EType = 'open_collector' Then Pin.Electrical := eElectricOpenCollector-            Else If EType = 'open_emitter' Then Pin.Electrical := eElectricOpenEmitter-            Else If (EType = 'power_in') Or (EType = 'power_out') Then Pin.Electrical := eElectricPower-            Else Pin.Electrical := eElectricPassive;-            { a small part's pins carry no visible name or number, as a library resistor's do }-            If FieldP(L, 9) = 'hide' Then-            Begin-                Pin.ShowName := False;-                Pin.ShowDesignator := False;-            End;-            Comp.AddSchObject(Pin);-            NPins := NPins + 1;-        End-        Else If Kind = 'ARC' Then-        Begin-            { ARC|mode|cx|cy|r|startDeg|endDeg, counterclockwise }-            Md := AdomInt(FieldP(L, 2));-            Ar := SchServer.SchObjectFactory(eArc, eCreate_Default);-            Ar.OwnerPartId := 1;-            Ar.OwnerPartDisplayMode := Md;-            Ar.Location := Point(MilsToCoord(AdomInt(FieldP(L, 3))), MilsToCoord(AdomInt(FieldP(L, 4))));-            Ar.Radius := MilsToCoord(AdomInt(FieldP(L, 5)));-            Ar.StartAngle := AdomInt(FieldP(L, 6));-            Ar.EndAngle := AdomInt(FieldP(L, 7));-            Comp.AddSchObject(Ar);-            NLines := NLines + 1;-        End-        Else If Kind = 'LINE' Then-        Begin-            Md := AdomInt(FieldP(L, 2));-            Ln := SchServer.SchObjectFactory(eLine, eCreate_Default);-            Ln.OwnerPartId := 1;-            Ln.OwnerPartDisplayMode := Md;-            Ln.Location := Point(MilsToCoord(AdomInt(FieldP(L, 3))), MilsToCoord(AdomInt(FieldP(L, 4))));-            Ln.Corner := Point(MilsToCoord(AdomInt(FieldP(L, 5))), MilsToCoord(AdomInt(FieldP(L, 6))));-            Comp.AddSchObject(Ln);-            NLines := NLines + 1;-        End;-    End;-    Spec.Free;-    Comp.DisplayModeCount := NModes;-    { a part imported again REPLACES its symbol: a second component with the same LibReference-      left the old one first in the library, so every reader kept drawing the old pins-      (measured 2026-10-15: a fixed 2-pin capacitor still placed with 1 pin) }-    NOld := 0;-    Repeat-        Old := Nil;-        OIt := SLib.SchLibIterator_Create;-        OIt.AddFilter_ObjectSet(MkSet(eSchComponent));-        Old := OIt.FirstSchObject;-        While Old <> Nil Do-        Begin-            If (Old <> Comp) And (UpperCase(Old.LibReference) = UpperCase(LibRef)) Then Break;-            Old := OIt.NextSchObject;-        End;-        SLib.SchIterator_Destroy(OIt);-        If Old <> Nil Then Begin SLib.RemoveSchComponent(Old); NOld := NOld + 1; End;-    Until (Old = Nil) Or (NOld > 20);-    SLib.AddSchComponent(Comp);-    SchServer.RobotManager.SendMessage(SLib.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, Comp.I_ObjectAddress);-    SLib.CurrentSchComponent := Comp;-    SLib.GraphicallyInvalidate;-    Result := '{' + JRaw('ok', 'true') + ',' + JStr('libref', LibRef) + ',' + JStr('library', LibPath) + ',' + JRaw('replaced', IntToStr(NOld)) + ',' +-        JStr('libraryState', LibState) + ',' + JRaw('modes', IntToStr(NModes)) + ',' +-        JRaw('pins', IntToStr(NPins)) + ',' + JRaw('lines', IntToStr(NLines)) + ',' +-        JRaw('displayModeCount', IntToStr(Comp.DisplayModeCount)) + '}';-End;--{ Switch the display mode of the current library component (for frames). mode=<int> }-Function CmdSymbolMode(Lines : TStringList) : String;-Var-    SLib : ISch_Lib;-    Comp : ISch_Component;-    Md   : Integer;-Begin-    SLib := SchServer.GetCurrentSchDocument;-    If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','no schematic library is current') + '}';-        Exit;-    End;-    Comp := SLib.CurrentSchComponent;-    If Comp = Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current component') + '}';-        Exit;-    End;-    Md := StrToIntDef(Param(Lines, 'mode'), 0);-    Comp.DisplayMode := Md;-    SLib.GraphicallyInvalidate;-    Result := '{' + JRaw('ok','true') + ',' + JRaw('mode', IntToStr(Comp.DisplayMode)) + ',' +-        JRaw('displayModeCount', IntToStr(Comp.DisplayModeCount)) + ',' + JStr('libref', Comp.LibReference) + '}';-End;---{ ------------------------------------------------------------- add_models --- }--{ Attach PCB footprint models (name|pcblib|description;...) and parameters-  (k=v;...) to a SchLib component; set comment + description. Altium keeps-  several footprints on one component and the user picks the current one on-  the placed part (Properties > Footprint) - the CHANGE PACKAGE equivalent. }-Function CmdAddModels(Lines : TStringList) : String;-Var-    SLib   : ISch_Lib;-    Iter   : ISch_Iterator;-    Cmp    : ISch_Component;-    Impl   : ISch_Implementation;-    Models, Params, Name, Item, K, V, Cmt, Desc : String;-    ML, PL : TStringList;-    I, NModels, NParams : Integer;-Begin-    Name := Param(Lines, 'name');-    Models := Param(Lines, 'models');-    Params := Param(Lines, 'params');-    Cmt := Param(Lines, 'comment');-    Desc := Param(Lines, 'description');-    SLib := SchServer.GetCurrentSchDocument;-    If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the SchLib first') + '}';-        Exit;-    End;-    Cmp := Nil;-    Iter := SLib.SchLibIterator_Create;-    Iter.AddFilter_ObjectSet(MkSet(eSchComponent));-    Cmp := Iter.FirstSchObject;-    While Cmp <> Nil Do-    Begin-        If UpperCase(Cmp.LibReference) = UpperCase(Name) Then Break;-        Cmp := Iter.NextSchObject;-    End;-    SLib.SchIterator_Destroy(Iter);-    If Cmp = Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','component not found: ' + Name) + '}';-        Exit;-    End;-    SchServer.ProcessControl.PreProcess(SLib, '');-    If Cmt <> '' Then Cmp.Comment.Text := Cmt;-    If Desc <> '' Then Cmp.ComponentDescription := Desc;-    NModels := 0;-    ML := SplitSemi(Models);-    For I := 0 To ML.Count - 1 Do-    Begin-        Item := ML[I];-        If FieldP(Item, 1) = '' Then Continue;-        Impl := Cmp.AddSchImplementation;-        Impl.ModelName := FieldP(Item, 1);-        Impl.ModelType := 'PCBLIB';-        Impl.Description := FieldP(Item, 3);-        Impl.IsCurrent := (NModels = 0);-        If FieldP(Item, 2) <> '' Then Impl.AddDataFileLink(FieldP(Item, 1), FieldP(Item, 2), 'PCBLIB');-        NModels := NModels + 1;-    End;-    ML.Free;-    NParams := 0;-    PL := SplitSemi(Params);-    For I := 0 To PL.Count - 1 Do-    Begin-        Item := PL[I];-        If Pos('=', Item) = 0 Then Continue;-        K := Copy(Item, 1, Pos('=', Item) - 1);-        V := Copy(Item, Pos('=', Item) + 1, Length(Item));-        SetSchParam(Cmp, K, V);-        NParams := NParams + 1;-    End;-    PL.Free;-    SchServer.ProcessControl.PostProcess(SLib, '');-    MarkSchModified(SLib);-    SLib.GraphicallyInvalidate;-    Result := '{' + JRaw('ok','true') + ',' + JStr('libref', Cmp.LibReference) + ',' +-        JRaw('models', IntToStr(NModels)) + ',' + JRaw('params', IntToStr(NParams)) + '}';-End;---{ ------------------------------------------------------ plate_vias --- }--{ Every drilled pad in the current PcbLib (all footprints, or name=<filter>) becomes a-  plated MULTI-LAYER pad. adom-lbr writes heatsink vias as pads with a hole, and Altium-  loaded them on the Top Layer (John, 2026-09-20: "did Altium support bringing in those-  heatsink vias?" - the holes came, the layer did not). Saving the library afterwards-  also yields a file whose bytes show the encoding Altium wants. }-Function CmdPlateVias(Lines : TStringList) : String;-Var-    PLib : IPCB_Library;-    Fp   : IPCB_LibComponent;-    GIt  : IPCB_GroupIterator;-    Pad  : IPCB_Pad;-    I, NFixed, NHoles : Integer;-    Name, S : String;-Begin-    PLib := PCBServer.GetCurrentPCBLibrary;-    Name := Param(Lines, 'name');-    If PLib = Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','no PCB library is current') + '}';-        Exit;-    End;-    NFixed := 0; NHoles := 0; S := '';-    PCBServer.PreProcess;-    For I := 0 To PLib.ComponentCount - 1 Do-    Begin-        Fp := PLib.GetComponent(I);-        If (Name <> '') And (UpperCase(Fp.Name) <> UpperCase(Name)) Then Continue;-        GIt := Fp.GroupIterator_Create;-        GIt.AddFilter_ObjectSet(MkSet(ePadObject));-        GIt.AddFilter_LayerSet(AllLayers);-        Pad := GIt.FirstPCBObject;-        While Pad <> Nil Do-        Begin-            If Pad.HoleSize > 0 Then-            Begin-                NHoles := NHoles + 1;-                If Pad.Layer <> eMultiLayer Then-                Begin-                    Pad.BeginModify;-                    Pad.Layer := eMultiLayer;-                    Pad.Plated := True;-                    Pad.EndModify;-                    NFixed := NFixed + 1;-                    If S <> '' Then S := S + ';';-                    S := S + Fp.Name + ':' + Pad.Name;-                End;-            End;-            Pad := GIt.NextPCBObject;-        End;-        Fp.GroupIterator_Destroy(GIt);-    End;-    PCBServer.PostProcess;-    PLib.Board.ViewManager_FullUpdate;-    PLib.RefreshView;-    Result := '{' + JRaw('ok','true') + ',' + JRaw('holes', IntToStr(NHoles)) + ',' + JRaw('plated', IntToStr(NFixed)) + ',' + JStr('fixed', S) + '}';-End;---{ ------------------------------------------------------ normalize_bodies --- }--{ The PcbLib canvas origin sits 50000 mil from the stored component origin, so a-  body added through the canvas lands 50000 mil away from the pads in the stored-  frame (measured: body left -50138 vs pads +/-157) - Zoom All then frames 50-  inches of nothing and the editor paints an empty canvas. Align every body-  centre onto the pad-cluster centre of its footprint. name=<optional filter> }-Function CmdNormalizeBodies(Lines : TStringList) : String;-Var-    PLib : IPCB_Library;-    Fp   : IPCB_LibComponent;-    GIt  : IPCB_GroupIterator;-    Prim : IPCB_Primitive;-    R    : TCoordRect;-    I, NMoved : Integer;-    PL, PB, PR, PT, DX, DY : Integer;-    AnyP : Boolean;-    Name, S : String;-Begin-    PLib := PCBServer.GetCurrentPCBLibrary;-    Name := Param(Lines, 'name');-    If PLib = Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','no PCB library is current') + '}';-        Exit;-    End;-    NMoved := 0; S := '';-    PCBServer.PreProcess;-    For I := 0 To PLib.ComponentCount - 1 Do-    Begin-        Fp := PLib.GetComponent(I);-        If (Name <> '') And (UpperCase(Fp.Name) <> UpperCase(Name)) Then Continue;-        AnyP := False; PL := 0; PB := 0; PR := 0; PT := 0;-        GIt := Fp.GroupIterator_Create;-        GIt.AddFilter_ObjectSet(MkSet(ePadObject));-        GIt.AddFilter_LayerSet(AllLayers);-        Prim := GIt.FirstPCBObject;-        While Prim <> Nil Do-        Begin-            R := Prim.BoundingRectangle;-            If Not AnyP Then Begin PL := R.Left; PB := R.Bottom; PR := R.Right; PT := R.Top; AnyP := True; End-            Else-            Begin-                If R.Left < PL Then PL := R.Left;-                If R.Bottom < PB Then PB := R.Bottom;-                If R.Right > PR Then PR := R.Right;-                If R.Top > PT Then PT := R.Top;-            End;-            Prim := GIt.NextPCBObject;-        End;-        Fp.GroupIterator_Destroy(GIt);-        If Not AnyP Then Continue;-        GIt := Fp.GroupIterator_Create;-        GIt.AddFilter_ObjectSet(MkSet(eComponentBodyObject));-        GIt.AddFilter_LayerSet(AllLayers);-        Prim := GIt.FirstPCBObject;-        While Prim <> Nil Do-        Begin-            R := Prim.BoundingRectangle;-            DX := ((PL + PR) Div 2) - ((R.Left + R.Right) Div 2);-            DY := ((PB + PT) Div 2) - ((R.Bottom + R.Top) Div 2);-            If (Abs(DX) > MilsToCoord(5)) Or (Abs(DY) > MilsToCoord(5)) Then-            Begin-                Prim.MoveByXY(DX, DY);-                NMoved := NMoved + 1;-                If S <> '' Then S := S + ';';-                S := S + Fp.Name + ':' + IntToStr(Round(CoordToMils(DX))) + ',' + IntToStr(Round(CoordToMils(DY)));-            End;-            Prim := GIt.NextPCBObject;-        End;-        Fp.GroupIterator_Destroy(GIt);-    End;-    PCBServer.PostProcess;-    PLib.Board.ViewManager_FullUpdate;-    PLib.RefreshView;-    Result := '{' + JRaw('ok','true') + ',' + JRaw('moved', IntToStr(NMoved)) + ',' + JStr('shifts', S) + '}';-End;---{ ------------------------------------------ test_board / swap_footprint --- }--{ Copy a library footprint onto Board as a fresh component centred at (CX,CY)-  coords, per-primitive (group Replicate dissolves), children BEFORE the board-  add, pad cluster and bodies normalised to the target independently. }-Function PlaceCopy(Board : IPCB_Board; LibFp : IPCB_LibComponent; CX, CY : Integer) : IPCB_Component;-Var-    Inst : IPCB_Component;-    GIt  : IPCB_GroupIterator;-    Prim, Cpy : IPCB_Primitive;-    R    : TCoordRect;-    DX, DY, CL, CB, CR, CT : Integer;-    AnyP : Boolean;-Begin-    Inst := PCBServer.PCBObjectFactory(eComponentObject, eNoDimension, eCreate_Default);-    Inst.Layer := eTopLayer;-    Inst.NameOn := False;-    Inst.CommentOn := False;-    GIt := LibFp.GroupIterator_Create;-    GIt.AddFilter_LayerSet(AllLayers);-    Prim := GIt.FirstPCBObject;-    While Prim <> Nil Do-    Begin-        Cpy := Prim.Replicate;-        Inst.AddPCBObject(Cpy);-        Prim := GIt.NextPCBObject;-    End;-    LibFp.GroupIterator_Destroy(GIt);-    Board.AddPCBObject(Inst);-    PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Inst.I_ObjectAddress);-    Inst.x := CX; Inst.y := CY;-    AnyP := False; CL := 0; CB := 0; CR := 0; CT := 0;-    GIt := Inst.GroupIterator_Create;-    GIt.AddFilter_ObjectSet(MkSet(ePadObject));-    GIt.AddFilter_LayerSet(AllLayers);-    Prim := GIt.FirstPCBObject;-    While Prim <> Nil Do-    Begin-        R := Prim.BoundingRectangle;-        If Not AnyP Then Begin CL := R.Left; CB := R.Bottom; CR := R.Right; CT := R.Top; AnyP := True; End-        Else-        Begin-            If R.Left < CL Then CL := R.Left;-            If R.Bottom < CB Then CB := R.Bottom;-            If R.Right > CR Then CR := R.Right;-            If R.Top > CT Then CT := R.Top;-        End;-        Prim := GIt.NextPCBObject;-    End;-    Inst.GroupIterator_Destroy(GIt);-    DX := 0; DY := 0;-    If AnyP Then Begin DX := CX - ((CL + CR) Div 2); DY := CY - ((CB + CT) Div 2); End;-    GIt := Inst.GroupIterator_Create;-    GIt.AddFilter_LayerSet(AllLayers);-    Prim := GIt.FirstPCBObject;-    While Prim <> Nil Do-    Begin-        If Prim.ObjectId = eComponentBodyObject Then-        Begin-            R := Prim.BoundingRectangle;-            Prim.MoveByXY(CX - ((R.Left + R.Right) Div 2), CY - ((R.Bottom + R.Top) Div 2));-        End-        Else If Prim.ObjectId <> eTextObject Then-            Prim.MoveByXY(DX, DY);-        Prim := GIt.NextPCBObject;-    End;-    Inst.GroupIterator_Destroy(GIt);-    Inst.Name.MoveToXY(CX, CY + MilsToCoord(250));-    Inst.Comment.MoveToXY(CX, CY - MilsToCoord(250));-    Result := Inst;-End;--Function FindNet(Board : IPCB_Board; NetName : String) : IPCB_Net;-Var-    BIt : IPCB_BoardIterator;-    N   : IPCB_Net;-Begin-    Result := Nil;-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eNetObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    N := BIt.FirstPCBObject;-    While N <> Nil Do-    Begin-        If N.Name = NetName Then Begin Result := N; Break; End;-        N := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-End;--{ Assign nets to pads by designator: assigns=<des>=<net>;... (all pads sharing a-  designator get the net - the heatsink-via convention). }-Procedure AssignPadNets(Board : IPCB_Board; Inst : IPCB_Component; Assigns : String);-Var-    GIt  : IPCB_GroupIterator;-    Pad  : IPCB_Pad;-    AL   : TStringList;-    I    : Integer;-    Des, NetName : String;-    Net  : IPCB_Net;-Begin-    AL := SplitSemi(Assigns);-    For I := 0 To AL.Count - 1 Do-    Begin-        If Pos('=', AL[I]) = 0 Then Continue;-        Des := Copy(AL[I], 1, Pos('=', AL[I]) - 1);-        NetName := Copy(AL[I], Pos('=', AL[I]) + 1, Length(AL[I]));-        Net := FindNet(Board, NetName);-        If Net = Nil Then-        Begin-            { a net that is only added (not registered) lives in memory: the save drops it and-              Altium builds no ratsnest for it (measured 2026-10-15: reopened board, 0 nets) }-            Net := PCBServer.PCBObjectFactory(eNetObject, eNoDimension, eCreate_Default);-            Net.Name := NetName;-            Board.AddPCBObject(Net);-            PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Net.I_ObjectAddress);-        End;-        GIt := Inst.GroupIterator_Create;-        GIt.AddFilter_ObjectSet(MkSet(ePadObject));-        GIt.AddFilter_LayerSet(AllLayers);-        Pad := GIt.FirstPCBObject;-        While Pad <> Nil Do-        Begin-            If Pad.Name = Des Then-            Begin-                Pad.BeginModify;-                Pad.Net := Net;-                Pad.EndModify;-                Net.AddPCBObject(Pad);-            End;-            Pad := GIt.NextPCBObject;-        End;-        Inst.GroupIterator_Destroy(GIt);-    End;-    AL.Free;-End;--{ New PcbDoc: FR4 rectangle wMm x hMm (origin at 0,0), the footprint placed at-  the centre, pad 1 and pad 49 given nets with a stub track each.-  Args: footprint, wMm, hMm, pcb=<save path>, assigns=<des=net;...>, stubs=<des=x,y;...> (mm from centre) }-Function CmdTestBoard(Lines : TStringList) : String;-Var-    PLib  : IPCB_Library;-    LibFp : IPCB_LibComponent;-    Board : IPCB_Board;-    SD    : IServerDocument;-    Inst  : IPCB_Component;-    BO    : IPCB_BoardOutline;-    Seg   : TPolySegment;-    Trk   : IPCB_Track;-    Txt   : IPCB_Text;-    R     : TCoordRect;-    Net   : IPCB_Net;-    GIt   : IPCB_GroupIterator;-    Pad   : IPCB_Pad;-    SL    : TStringList;-    FpList : TStringList;-    BIt   : IPCB_BoardIterator;-    ECmp  : IPCB_Component;-    GIt2  : IPCB_GroupIterator;-    MPrim : IPCB_Primitive;-    I, K, WC, HC, CX, CY, PX, PY, Found : Integer;-    NExist, ExtR, Cur, FW, FL, FR, AnyF : Integer;-    FpName, PcbPath, Des, NetName, Item, Missing : String;-    Ok, Append : Boolean;-Begin-    FpName := Param(Lines, 'footprint');-    PcbPath := Param(Lines, 'pcb');-    { footprint may be a ';' list: every part lands on ONE board, spread evenly along X-      (John, 2026-09-19: three ToF sensors on one board for the demo). }-    FpList := SplitSemi(FpName);-    WC := MMsToCoord(AdomFloat(Param(Lines, 'wMm')));-    HC := MMsToCoord(AdomFloat(Param(Lines, 'hMm')));-    PLib := PCBServer.GetCurrentPCBLibrary;-    If PLib = Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the PcbLib first') + '}';-        Exit;-    End;-    Missing := '';-    For K := 0 To FpList.Count - 1 Do-    Begin-        Ok := False;-        For I := 0 To PLib.ComponentCount - 1 Do-        Begin-            LibFp := PLib.GetComponent(I);-            If UpperCase(LibFp.Name) = UpperCase(FpList[K]) Then Begin Ok := True; Break; End;-        End;-        If Not Ok Then Missing := Missing + FpList[K] + ' ';-    End;-    If (FpList.Count = 0) Or (Missing <> '') Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','footprint not found: ' + Trim(Missing + FpName)) + '}';-        FpList.Free;-        Exit;-    End;-    { Append mode (issue 11): pcb names an EXISTING .PcbDoc -> open it and add the parts,-      instead of a new board per call. The board keeps its own outline and layers. }-    SD := Nil;-    Append := (PcbPath <> '') And FileExists(PcbPath);-    If (PcbPath <> '') And (Not Append) Then-    Begin-        SD := Nil;-        Try-            SD := Client.GetDocumentByPath(PcbPath);-        Except-            SD := Nil;-        End;-        If SD <> Nil Then Append := True;-    End;-    If Append Then-    Begin-        If SD = Nil Then SD := Client.OpenDocument('PCB', PcbPath);-        If SD = Nil Then-        Begin-            Result := '{' + JRaw('ok','false') + ',' + JStr('error','could not open the existing board: ' + PcbPath) + '}';-            FpList.Free;-            Exit;-        End;-        Client.ShowDocument(SD);-    End-    Else-    Begin-        SD := CreateNewDocumentFromDocumentKind('PCB');-        If SD = Nil Then-        Begin-            Result := '{' + JRaw('ok','false') + ',' + JStr('error','could not create a PCB document') + '}';-            FpList.Free;-            Exit;-        End;-        Client.ShowDocument(SD);-    End;-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board after open') + '}';-        FpList.Free;-        Exit;-    End;-    { existing parts: how many (designators continue from there) and how far right they reach }-    NExist := 0; ExtR := 0; CY := 0;-    If Append Then-    Begin-        BIt := Board.BoardIterator_Create;-        BIt.AddFilter_ObjectSet(MkSet(eComponentObject));-        BIt.AddFilter_LayerSet(AllLayers);-        BIt.AddFilter_Method(eProcessAll);-        ECmp := BIt.FirstPCBObject;-        While ECmp <> Nil Do-        Begin-            NExist := NExist + 1;-            If (NExist = 1) Or (ECmp.x > ExtR) Then ExtR := ECmp.x;-            CY := CY + ECmp.y;-            ECmp := BIt.NextPCBObject;-        End;-        Board.BoardIterator_Destroy(BIt);-        If NExist > 0 Then CY := CY Div NExist Else CY := MMsToCoord(7);-    End;-    PCBServer.PreProcess;-    If Not Append Then-    Begin-    Board.LayerStack.LayerObject[eMechanical9].MechanicalLayerEnabled := True;-    Board.LayerStack.LayerObject[eMechanical9].Name := 'Adom.SolderBalls';-    Board.LayerIsDisplayed[eMechanical9] := False;-    Board.LayerStack.LayerObject[eMechanical13].MechanicalLayerEnabled := True;-    Board.LayerStack.LayerObject[eMechanical13].Name := 'Fab';-    Board.LayerStack.LayerObject[eMechanical15].MechanicalLayerEnabled := True;-    Board.LayerStack.LayerObject[eMechanical15].Name := 'Courtyard';-    { board shape }-    BO := Board.BoardOutline;-    BO.Invalidate;-    { no Clear: IPCB_BoardOutline has no Clear in this engine (halt, measured);-      PointCount + Segments[] + Rebuild is the working pattern }-    BO.PointCount := 4;-    Seg := BO.Segments[0]; Seg.vx := 0; Seg.vy := 0; Seg.Kind := ePolySegmentLine; BO.Segments[0] := Seg;-    Seg := BO.Segments[1]; Seg.vx := WC; Seg.vy := 0; Seg.Kind := ePolySegmentLine; BO.Segments[1] := Seg;-    Seg := BO.Segments[2]; Seg.vx := WC; Seg.vy := HC; Seg.Kind := ePolySegmentLine; BO.Segments[2] := Seg;-    Seg := BO.Segments[3]; Seg.vx := 0; Seg.vy := HC; Seg.Kind := ePolySegmentLine; BO.Segments[3] := Seg;-    BO.Rebuild;-    BO.Validate;-    End;-    CX := WC Div 2;-    If Append Then Cur := ExtR + MMsToCoord(6)-    Else CY := HC Div 2;-    { one part: centred; N parts: at W*(k+1)/(N+1). Inst keeps the LAST placed part so the-      single-part assigns/stubs behaviour is unchanged. }-    For K := 0 To FpList.Count - 1 Do-    Begin-        For I := 0 To PLib.ComponentCount - 1 Do-        Begin-            LibFp := PLib.GetComponent(I);-            If UpperCase(LibFp.Name) = UpperCase(FpList[K]) Then Break;-        End;-        If Append Then-        Begin-            { pad-extent width of the library footprint, measured before placing }-            AnyF := 0; FL := 0; FR := 0;-            GIt2 := LibFp.GroupIterator_Create;-            GIt2.AddFilter_ObjectSet(MkSet(ePadObject));-            GIt2.AddFilter_LayerSet(AllLayers);-            MPrim := GIt2.FirstPCBObject;-            While MPrim <> Nil Do-            Begin-                R := MPrim.BoundingRectangle;-                If (AnyF = 0) Or (R.Left < FL) Then FL := R.Left;-                If (AnyF = 0) Or (R.Right > FR) Then FR := R.Right;-                AnyF := 1;-                MPrim := GIt2.NextPCBObject;-            End;-            LibFp.GroupIterator_Destroy(GIt2);-            FW := FR - FL;-            If FW < MMsToCoord(2) Then FW := MMsToCoord(2);-            CX := Cur + ((FW * 3) Div 4) + MMsToCoord(1);-            Cur := CX + ((FW * 3) Div 4) + MMsToCoord(1);-        End-        Else-            CX := (WC * (K + 1)) Div (FpList.Count + 1);-        Inst := PlaceCopy(Board, LibFp, CX, CY);-        { U1..Un so the read verbs (component_pins, footprint_primitives) can address each part;-          the factory left every part named Designator1 (measured 2026-09-20). Name stays hidden. }-        Inst.Name.Text := 'U' + IntToStr(NExist + K + 1);-        { the footprint name: the primitives were copied one by one, so the component carries-          no Pattern unless it is set here, and BOM / STEP naming / footprint compare key on it }-        Inst.Pattern := LibFp.Name;-        { The part name is the component's own COMMENT, shown on the top overlay under the-          footprint, so it moves with the part (free overlay text stayed behind when parts were-          moved and piled up in the silkscreen Gerber). 35 mil stroke, its top 2.5 mm below the-          part centre, clear of a 3 mm part's courtyard; centred on its real extent. }-        Inst.Comment.Text := LibFp.Name;-        Inst.CommentOn := True;-        Inst.Comment.Size := MilsToCoord(35);-        Inst.Comment.Width := MilsToCoord(5);-        Inst.Comment.MoveToXY(CX, CY - MMsToCoord(3.4));-        R := Inst.Comment.BoundingRectangle;-        Inst.Comment.MoveByXY(CX - ((R.Left + R.Right) Div 2), 0);-    End;-    AssignPadNets(Board, Inst, Param(Lines, 'assigns'));-    { stub tracks: des=x,y (mm from centre) -> a track from that pad to the point, on its net }-    SL := SplitSemi(Param(Lines, 'stubs'));-    For I := 0 To SL.Count - 1 Do-    Begin-        Item := SL[I];-        If Pos('=', Item) = 0 Then Continue;-        Des := Copy(Item, 1, Pos('=', Item) - 1);-        PX := CX + MMsToCoord(AdomFloat(Field(Copy(Item, Pos('=', Item) + 1, Length(Item)), 1)));-        PY := CY + MMsToCoord(AdomFloat(Field(Copy(Item, Pos('=', Item) + 1, Length(Item)), 2)));-        GIt := Inst.GroupIterator_Create;-        GIt.AddFilter_ObjectSet(MkSet(ePadObject));-        GIt.AddFilter_LayerSet(AllLayers);-        Pad := GIt.FirstPCBObject;-        Found := 0;-        While (Pad <> Nil) And (Found = 0) Do-        Begin-            If (Pad.Name = Des) And (Pad.Layer <> eMultiLayer) Then Found := 1-            Else If (Pad.Name = Des) And (Found = 0) And (Pad.Layer = eMultiLayer) Then Found := 0;-            If Found = 1 Then Break;-            Pad := GIt.NextPCBObject;-        End;-        If Pad <> Nil Then-        Begin-            Trk := PCBServer.PCBObjectFactory(eTrackObject, eNoDimension, eCreate_Default);-            Trk.Layer := eTopLayer;-            Trk.X1 := Pad.x; Trk.Y1 := Pad.y; Trk.X2 := PX; Trk.Y2 := PY;-            Trk.Width := MMsToCoord(0.2);-            Trk.Net := Pad.Net;-            Board.AddPCBObject(Trk);-            PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Trk.I_ObjectAddress);-        End;-        Inst.GroupIterator_Destroy(GIt);-    End;-    SL.Free;-    PCBServer.PostProcess;-    Board.ViewManager_FullUpdate;-    If PcbPath <> '' Then-    Begin-        If Not Append Then Begin FreePath(PcbPath); SD.SetFileName(PcbPath); End;-        SD.DoFileSave('');-    End;-    Result := '{' + JRaw('ok','true') + ',' + JStr('pcb', PcbPath) + ',' + JStr('placed', FpName) + ',' +-        JRaw('appended', BoolStr(Append)) + ',' + JRaw('existingParts', IntToStr(NExist)) + ',' +-        JRaw('count', IntToStr(FpList.Count)) + ',' +-        JRaw('padsOnComp', IntToStr(GroupCount(Inst, ePadObject))) + ',' +-        JRaw('bodiesOnComp', IntToStr(GroupCount(Inst, eComponentBodyObject))) + '}';-    FpList.Free;-End;--{ Replace the first component on the current board with another library-  footprint at the same position, keeping the nets by pad designator. Tracks are-  untouched, so the wiring survives the swap. Args: footprint, assigns }-Function CmdSwapFootprint(Lines : TStringList) : String;-Var-    PLib  : IPCB_Library;-    LibFp : IPCB_LibComponent;-    Board : IPCB_Board;-    BIt   : IPCB_BoardIterator;-    Old, Inst : IPCB_Component;-    I, CX, CY : Integer;-    FpName, LibPath : String;-    Ok : Boolean;-Begin-    FpName := Param(Lines, 'footprint');-    LibPath := Param(Lines, 'libraryPath');-    Board := PCBServer.GetCurrentPCBBoard;-    PLib := Nil;-    If LibPath <> '' Then PLib := PCBServer.LoadPCBLibraryByPath(LibPath);-    If PLib = Nil Then PLib := PCBServer.GetCurrentPCBLibrary;-    If (Board = Nil) Or (PLib = Nil) Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','need the board focused and the PcbLib path') + '}';-        Exit;-    End;-    Ok := False;-    For I := 0 To PLib.ComponentCount - 1 Do-    Begin-        LibFp := PLib.GetComponent(I);-        If UpperCase(LibFp.Name) = UpperCase(FpName) Then Begin Ok := True; Break; End;-    End;-    If Not Ok Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','footprint not found: ' + FpName) + '}';-        Exit;-    End;-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eComponentObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Old := BIt.FirstPCBObject;-    Board.BoardIterator_Destroy(BIt);-    If Old = Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','no component on the board') + '}';-        Exit;-    End;-    CX := Old.x; CY := Old.y;-    PCBServer.PreProcess;-    Board.RemovePCBObject(Old);-    Inst := PlaceCopy(Board, LibFp, CX, CY);-    AssignPadNets(Board, Inst, Param(Lines, 'assigns'));-    PCBServer.PostProcess;-    Board.ViewManager_FullUpdate;-    Result := '{' + JRaw('ok','true') + ',' + JStr('placed', FpName) + ',' +-        JRaw('padsOnComp', IntToStr(GroupCount(Inst, ePadObject))) + ',' +-        JRaw('bodiesOnComp', IntToStr(GroupCount(Inst, eComponentBodyObject))) + '}';-End;---{ ------------------------------------------------------------ board_shape --- }--{ Redefine the CURRENT board shape as a wMm x hMm rectangle at (x0Mm,y0Mm).-  Until 0.2.31 this drew four temporary tracks and ran the editor process-  PCB:PlaceBoardOutline (Mode=BOARDOUTLINE_FROM_SEL_PRIMS), then read-  Board.BoardOutline.BoundingRectangle with NO nil guard. On AD 26.10.1 and on Altium Develop the-  process left no outline, the read raised on a nil object, and the script parked on a breakpoint-  with no error box (found by the verb battery on two builds, 2026-09-20). It now uses the pattern-  CmdTestBoard has always used and that renders correctly on every build: PointCount + Segments[]-  + Rebuild on the board's own outline, every access nil-guarded. }-Function CmdBoardShape(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    BO    : IPCB_BoardOutline;-    Seg   : TPolySegment;-    X0, Y0, X1, Y1, K, NTr : Integer;-    Trk : IPCB_Track;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','no board focused') + '}';-        Exit;-    End;-    X0 := MMsToCoord(AdomFloat(Param(Lines, 'x0Mm')));-    Y0 := MMsToCoord(AdomFloat(Param(Lines, 'y0Mm')));-    X1 := X0 + MMsToCoord(AdomFloat(Param(Lines, 'wMm')));-    Y1 := Y0 + MMsToCoord(AdomFloat(Param(Lines, 'hMm')));-    If (X1 <= X0) Or (Y1 <= Y0) Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','bad_size') + ',' + JStr('error','wMm and hMm must both be greater than 0') + '}';-        Exit;-    End;-    BO := Board.BoardOutline;-    If BO = Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_board_outline') + ',' +-            JStr('error','this document has no board outline object (a PcbLib internal board has none); open a .PcbDoc') + '}';-        Exit;-    End;-    PCBServer.PreProcess;-    BO.Invalidate;-    BO.PointCount := 4;-    Seg := BO.Segments[0]; Seg.vx := X0; Seg.vy := Y0; Seg.Kind := ePolySegmentLine; BO.Segments[0] := Seg;-    Seg := BO.Segments[1]; Seg.vx := X1; Seg.vy := Y0; Seg.Kind := ePolySegmentLine; BO.Segments[1] := Seg;-    Seg := BO.Segments[2]; Seg.vx := X1; Seg.vy := Y1; Seg.Kind := ePolySegmentLine; BO.Segments[2] := Seg;-    Seg := BO.Segments[3]; Seg.vx := X0; Seg.vy := Y1; Seg.Kind := ePolySegmentLine; BO.Segments[3] := Seg;-    BO.Rebuild;-    BO.Validate;-    { The board shape is not plotted into any Gerber: a fab outline needs real tracks. On-      request, draw the rectangle on Mechanical 1 (0.1 mm) so .GM1 carries it. Additive: a-      second call draws a second set, so callers reshaping repeatedly pass it once at the end. }-    NTr := 0;-    If Param(Lines, 'mechOutline') = '1' Then-    Begin-        For K := 0 To 3 Do-        Begin-            Trk := PCBServer.PCBObjectFactory(eTrackObject, eNoDimension, eCreate_Default);-            Trk.Layer := eMechanical1;-            Trk.Width := MMsToCoord(0.1);-            If K = 0 Then Begin Trk.X1 := X0; Trk.Y1 := Y0; Trk.X2 := X1; Trk.Y2 := Y0; End;-            If K = 1 Then Begin Trk.X1 := X1; Trk.Y1 := Y0; Trk.X2 := X1; Trk.Y2 := Y1; End;-            If K = 2 Then Begin Trk.X1 := X1; Trk.Y1 := Y1; Trk.X2 := X0; Trk.Y2 := Y1; End;-            If K = 3 Then Begin Trk.X1 := X0; Trk.Y1 := Y1; Trk.X2 := X0; Trk.Y2 := Y0; End;-            Board.AddPCBObject(Trk);-            PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Trk.I_ObjectAddress);-            NTr := NTr + 1;-        End;-    End;-    PCBServer.PostProcess;-    Board.ViewManager_FullUpdate;-    Result := '{' + JRaw('ok','true') + ',' + JRaw('outlineMil', OutlineMilFromVertices(BO)) + ',' +-        JStr('outlineSource', 'vertices') + ',' + JRaw('mechOutlineTracks', IntToStr(NTr)) + '}';-End;---{ ---------------------------------------------------------- remove_symbol --- }--Function CmdRemoveSymbol(Lines : TStringList) : String;-Var-    SLib : ISch_Lib;-    Iter : ISch_Iterator;-    Comp : ISch_Component;-    Name : String;-    Found : Boolean;-Begin-    Name := Param(Lines, 'name');-    SLib := SchServer.GetCurrentSchDocument;-    If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the SchLib first') + '}';-        Exit;-    End;-    Found := False;-    Iter := SLib.SchLibIterator_Create;-    Iter.AddFilter_ObjectSet(MkSet(eSchComponent));-    Comp := Iter.FirstSchObject;-    While Comp <> Nil Do-    Begin-        If UpperCase(Comp.LibReference) = UpperCase(Name) Then Begin Found := True; Break; End;-        Comp := Iter.NextSchObject;-    End;-    SLib.SchIterator_Destroy(Iter);-    If Not Found Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','component not found: ' + Name) + '}';-        Exit;-    End;-    SLib.RemoveSchComponent(Comp);-    SLib.GraphicallyInvalidate;-    Result := '{' + JRaw('ok','true') + ',' + JStr('removed', Name) + '}';-End;---{ -------------------------------------- Barrett-parity: library manipulation --- }--Function FindSchComp(SLib : ISch_Lib; Name : String) : ISch_Component;-Var-    Iter : ISch_Iterator;-    C    : ISch_Component;-Begin-    Result := Nil;-    Iter := SLib.SchLibIterator_Create;-    Iter.AddFilter_ObjectSet(MkSet(eSchComponent));-    C := Iter.FirstSchObject;-    While C <> Nil Do-    Begin-        If UpperCase(C.LibReference) = UpperCase(Name) Then Begin Result := C; Break; End;-        C := Iter.NextSchObject;-    End;-    SLib.SchIterator_Destroy(Iter);-End;--Function FindFp(PLib : IPCB_Library; Name : String) : IPCB_LibComponent;-Var-    I : Integer;-    F : IPCB_LibComponent;-Begin-    Result := Nil;-    For I := 0 To PLib.ComponentCount - 1 Do-    Begin-        F := PLib.GetComponent(I);-        If UpperCase(F.Name) = UpperCase(Name) Then Begin Result := F; Break; End;-    End;-End;--{ Clone a symbol inside the current SchLib under a new LibReference, all display-  modes included (per-primitive copy - group Replicate drops children). }-Function CmdCloneSymbol(Lines : TStringList) : String;-Var-    SLib : ISch_Lib;-    Src, Dst : ISch_Component;-    It  : ISch_Iterator;-    Prim, Cpy : ISch_GraphicalObject;-    NCopied : Integer;-Begin-    SLib := SchServer.GetCurrentSchDocument;-    If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the SchLib first') + '}';-        Exit;-    End;-    Src := FindSchComp(SLib, Param(Lines, 'from'));-    If Src = Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','component not found: ' + Param(Lines, 'from')) + '}';-        Exit;-    End;-    If FindSchComp(SLib, Param(Lines, 'to')) <> Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','target already exists: ' + Param(Lines, 'to')) + '}';-        Exit;-    End;-    Dst := SchServer.SchObjectFactory(eSchComponent, eCreate_Default);-    Dst.LibReference := Param(Lines, 'to');-    Dst.ComponentDescription := Src.ComponentDescription;-    Dst.CurrentPartID := 1;-    Dst.DisplayMode := 0;-    Dst.Designator.Text := Src.Designator.Text;-    Dst.Comment.Text := Src.Comment.Text;-    NCopied := 0;-    It := Src.SchIterator_Create;-    Prim := It.FirstSchObject;-    While Prim <> Nil Do-    Begin-        If Prim.ObjectID <> eParameter Then-        Begin-            Cpy := Prim.Replicate;-            Dst.AddSchObject(Cpy);-            NCopied := NCopied + 1;-        End;-        Prim := It.NextSchObject;-    End;-    Src.SchIterator_Destroy(It);-    Dst.DisplayModeCount := Src.DisplayModeCount;-    SLib.AddSchComponent(Dst);-    SchServer.RobotManager.SendMessage(SLib.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, Dst.I_ObjectAddress);-    SLib.GraphicallyInvalidate;-    Result := '{' + JRaw('ok','true') + ',' + JStr('from', Src.LibReference) + ',' + JStr('to', Dst.LibReference) + ',' +-        JRaw('primitives', IntToStr(NCopied)) + ',' + JRaw('displayModeCount', IntToStr(Dst.DisplayModeCount)) + '}';-End;--Function CmdRenameSymbol(Lines : TStringList) : String;-Var-    SLib : ISch_Lib;-    C    : ISch_Component;-Begin-    SLib := SchServer.GetCurrentSchDocument;-    If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the SchLib first') + '}';-        Exit;-    End;-    C := FindSchComp(SLib, Param(Lines, 'from'));-    If C = Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','component not found: ' + Param(Lines, 'from')) + '}';-        Exit;-    End;-    If Trim(Param(Lines, 'to')) = '' Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','name_required') + ',' + JStr('error','to (the new name) is required') + '}';-        Exit;-    End;-    If FindSchComp(SLib, Param(Lines, 'to')) <> Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','name_taken') + ',' + JStr('error','a component named ' + Param(Lines, 'to') + ' already exists in this library') + '}';-        Exit;-    End;-    C.LibReference := Param(Lines, 'to');-    SLib.GraphicallyInvalidate;-    Result := '{' + JRaw('ok','true') + ',' + JStr('renamed', Param(Lines, 'to')) + '}';-End;--{ Read a symbol back: pins (designator/name/mode), description, modes, params. }-Function CmdReadSymbol(Lines : TStringList) : String;-Var-    SLib : ISch_Lib;-    C    : ISch_Component;-    It   : ISch_Iterator;-    Prim : ISch_GraphicalObject;-    Pin  : ISch_Pin;-    Pm   : ISch_Parameter;-    Impl : ISch_Implementation;-    Pins, Params, Models : String;-    NPins : Integer;-Begin-    SLib := SchServer.GetCurrentSchDocument;-    If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the SchLib first') + '}';-        Exit;-    End;-    C := FindSchComp(SLib, Param(Lines, 'name'));-    If C = Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','component not found: ' + Param(Lines, 'name')) + '}';-        Exit;-    End;-    Pins := ''; Params := ''; NPins := 0;-    It := C.SchIterator_Create;-    Prim := It.FirstSchObject;-    While Prim <> Nil Do-    Begin-        If Prim.ObjectID = ePin Then-        Begin-            Pin := Prim;-            If Pin.OwnerPartDisplayMode = 0 Then-            Begin-                If Pins <> '' Then Pins := Pins + ',';-                Pins := Pins + '{' + JStr('des', Pin.Designator) + ',' + JStr('name', Pin.Name) + ',' + JRaw('electrical', IntToStr(Pin.Electrical)) + '}';-                NPins := NPins + 1;-            End;-        End-        Else If Prim.ObjectID = eParameter Then-        Begin-            Pm := Prim;-            If Params <> '' Then Params := Params + ',';-            Params := Params + '{' + JStr('name', Pm.Name) + ',' + JStr('value', Pm.Text) + ',' +-                JRaw('hidden', JBool(Pm.IsHidden)) + '}';-        End;-        Prim := It.NextSchObject;-    End;-    C.SchIterator_Destroy(It);-    { the symbol's model links (footprints, sim, 3D): the eImplementation child iterator, as in-      CmdSymbolPrims (ImplementationCount halts the script on some builds) (#24) }-    Models := '';-    It := C.SchIterator_Create;-    It.AddFilter_ObjectSet(MkSet(eImplementation));-    Impl := It.FirstSchObject;-    While Impl <> Nil Do-    Begin-        If Models <> '' Then Models := Models + ',';-        Models := Models + '{' + JStr('type', Impl.ModelType) + ',' + JStr('name', Impl.ModelName) + ',' +-            JRaw('isCurrent', JBool(Impl.IsCurrent)) + '}';-        Impl := It.NextSchObject;-    End;-    C.SchIterator_Destroy(It);-    Result := '{' + JRaw('ok','true') + ',' + JStr('libref', C.LibReference) + ',' +-        JStr('description', C.ComponentDescription) + ',' +-        JRaw('displayModeCount', IntToStr(C.DisplayModeCount)) + ',' +-        JRaw('pinsMode0', IntToStr(NPins)) + ',' +-        JRaw('pins', '[' + Pins + ']') + ',' + JRaw('params', '[' + Params + ']') + ',' + JRaw('models', '[' + Models + ']') + '}';-End;--{ Copy a footprint inside the current PcbLib under a new name (per-primitive). }-Function CmdCopyFootprint(Lines : TStringList) : String;-Var-    PLib : IPCB_Library;-    Src, Dst : IPCB_LibComponent;-    GIt  : IPCB_GroupIterator;-    Prim, Cpy : IPCB_Primitive;-    NCopied : Integer;-Begin-    PLib := PCBServer.GetCurrentPCBLibrary;-    If PLib = Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the PcbLib first') + '}';-        Exit;-    End;-    Src := FindFp(PLib, Param(Lines, 'from'));-    If Src = Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','footprint not found: ' + Param(Lines, 'from')) + '}';-        Exit;-    End;-    If FindFp(PLib, Param(Lines, 'to')) <> Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','target already exists: ' + Param(Lines, 'to')) + '}';-        Exit;-    End;-    PCBServer.PreProcess;-    Dst := PCBServer.CreatePCBLibComp;-    Dst.Name := Param(Lines, 'to');-    Dst.Description := Src.Description;-    NCopied := 0;-    GIt := Src.GroupIterator_Create;-    GIt.AddFilter_LayerSet(AllLayers);-    Prim := GIt.FirstPCBObject;-    While Prim <> Nil Do-    Begin-        Cpy := Prim.Replicate;-        Dst.AddPCBObject(Cpy);-        NCopied := NCopied + 1;-        Prim := GIt.NextPCBObject;-    End;-    Src.GroupIterator_Destroy(GIt);-    PLib.RegisterComponent(Dst);-    PCBServer.PostProcess;-    PLib.Board.ViewManager_FullUpdate;-    Result := '{' + JRaw('ok','true') + ',' + JStr('from', Src.Name) + ',' + JStr('to', Dst.Name) + ',' +-        JRaw('primitives', IntToStr(NCopied)) + '}';-End;--Function CmdRenameFootprint(Lines : TStringList) : String;-Var-    PLib : IPCB_Library;-    F    : IPCB_LibComponent;-Begin-    PLib := PCBServer.GetCurrentPCBLibrary;-    If PLib = Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the PcbLib first') + '}';-        Exit;-    End;-    F := FindFp(PLib, Param(Lines, 'from'));-    If F = Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','footprint not found: ' + Param(Lines, 'from')) + '}';-        Exit;-    End;-    If Trim(Param(Lines, 'to')) = '' Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','name_required') + ',' + JStr('error','to (the new name) is required') + '}';-        Exit;-    End;-    If FindFp(PLib, Param(Lines, 'to')) <> Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','name_taken') + ',' + JStr('error','a footprint named ' + Param(Lines, 'to') + ' already exists in this library') + '}';-        Exit;-    End;-    F.Name := Param(Lines, 'to');-    PLib.Board.ViewManager_FullUpdate;-    Result := '{' + JRaw('ok','true') + ',' + JStr('renamed', Param(Lines, 'to')) + '}';-End;--Function CmdRemoveFootprint(Lines : TStringList) : String;-Var-    PLib : IPCB_Library;-    F    : IPCB_LibComponent;-Begin-    PLib := PCBServer.GetCurrentPCBLibrary;-    If PLib = Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the PcbLib first') + '}';-        Exit;-    End;-    F := FindFp(PLib, Param(Lines, 'name'));-    If F = Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','footprint not found: ' + Param(Lines, 'name')) + '}';-        Exit;-    End;-    PLib.RemoveComponent(F);-    PLib.Board.ViewManager_FullUpdate;-    Result := '{' + JRaw('ok','true') + ',' + JStr('removed', Param(Lines, 'name')) + '}';-End;---{ ---------------------------------------- cleanup_libs / set_visibility / xcopy --- }--{ Remove factory-default empty components (Component_1 / PCBComponent_1) from the-  current SchLib or PcbLib - the strays every fresh library carries. }-Function CmdCleanupLibs(Lines : TStringList) : String;-Var-    SLib : ISch_Lib;-    PLib : IPCB_Library;-    C    : ISch_Component;-    F    : IPCB_LibComponent;-    PIt  : ISch_Iterator;-    Pin  : ISch_Pin;-    NPin, NRemoved, NKept : Integer;-Begin-    NRemoved := 0;-    NKept    := 0;-    SLib := SchServer.GetCurrentSchDocument;-    If (SLib <> Nil) And (SLib.ObjectID = eSchLib) Then-    Begin-        C := FindSchComp(SLib, 'Component_1');-        If C <> Nil Then-        Begin-            { Only the FACTORY stray, never a symbol someone drew into it. The PcbLib side has-              always checked this (GroupCount = 0); the SchLib side deleted whatever was called-              Component_1, pins and all. }-            NPin := 0;-            PIt := C.SchIterator_Create;-            PIt.AddFilter_ObjectSet(MkSet(ePin));-            Pin := PIt.FirstSchObject;-            While Pin <> Nil Do-            Begin-                NPin := NPin + 1;-                Pin := PIt.NextSchObject;-            End;-            C.SchIterator_Destroy(PIt);-            If NPin = 0 Then-            Begin-                SLib.RemoveSchComponent(C);-                NRemoved := NRemoved + 1;-            End-            Else NKept := NKept + 1;-        End;-        SLib.GraphicallyInvalidate;-    End;-    PLib := PCBServer.GetCurrentPCBLibrary;-    If PLib <> Nil Then-    Begin-        F := FindFp(PLib, 'PCBComponent_1');-        If F <> Nil Then-        Begin-            If GroupCount(F, ePadObject) = 0 Then-            Begin-                PLib.RemoveComponent(F);-                NRemoved := NRemoved + 1;-            End-            Else NKept := NKept + 1;-        End;-        PLib.Board.ViewManager_FullUpdate;-    End;-    Result := '{' + JRaw('ok','true') + ',' + JRaw('removed', IntToStr(NRemoved)) +-        ',' + JRaw('keptNotEmpty', IntToStr(NKept)) + '}';-End;--{ Show or hide a named parameter on a SchLib component. name, param, visible=0|1 }-Function CmdSetVisibility(Lines : TStringList) : String;-Var-    SLib : ISch_Lib;-    C    : ISch_Component;-    It   : ISch_Iterator;-    Pm   : ISch_Parameter;-    Target : String;-    Found : Boolean;-Begin-    SLib := SchServer.GetCurrentSchDocument;-    If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the SchLib first') + '}';-        Exit;-    End;-    C := FindSchComp(SLib, Param(Lines, 'name'));-    If C = Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','component not found: ' + Param(Lines, 'name')) + '}';-        Exit;-    End;-    Target := Param(Lines, 'param');-    Found := False;-    It := C.SchIterator_Create;-    It.AddFilter_ObjectSet(MkSet(eParameter));-    Pm := It.FirstSchObject;-    While Pm <> Nil Do-    Begin-        If UpperCase(Pm.Name) = UpperCase(Target) Then-        Begin-            Pm.IsHidden := Param(Lines, 'visible') <> '1';-            Found := True;-        End;-        Pm := It.NextSchObject;-    End;-    C.SchIterator_Destroy(It);-    SLib.GraphicallyInvalidate;-    If Not Found Then-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','parameter not found: ' + Target) + '}'-    Else-        Result := '{' + JRaw('ok','true') + ',' + JStr('param', Target) + ',' + JStr('visible', Param(Lines, 'visible')) + '}';-End;--{ Copy a footprint ACROSS libraries: fromLib(path)+from -> the CURRENT PcbLib as to. }-{ a footprint's pad by its name, Nil when it has none }-Function FpPad(Fp : IPCB_LibComponent; Name : String) : IPCB_Pad;-Var-    GIt : IPCB_GroupIterator;-    P   : IPCB_Pad;-Begin-    Result := Nil;-    GIt := Fp.GroupIterator_Create;-    GIt.AddFilter_ObjectSet(MkSet(ePadObject));-    GIt.AddFilter_LayerSet(AllLayers);-    P := GIt.FirstPCBObject;-    While P <> Nil Do-    Begin-        If P.Name = Name Then Begin Result := P; Break; End;-        P := GIt.NextPCBObject;-    End;-    Fp.GroupIterator_Destroy(GIt);-End;--{ the name of a footprint's first pad, '' when it has none }-Function FirstPadName(Fp : IPCB_LibComponent) : String;-Var-    GIt : IPCB_GroupIterator;-    P   : IPCB_Pad;-Begin-    Result := '';-    GIt := Fp.GroupIterator_Create;-    GIt.AddFilter_ObjectSet(MkSet(ePadObject));-    GIt.AddFilter_LayerSet(AllLayers);-    P := GIt.FirstPCBObject;-    If P <> Nil Then Result := P.Name;-    Fp.GroupIterator_Destroy(GIt);-End;--{ any primitive of a footprint, Nil when it is empty }-Function FpFirstAny(Fp : IPCB_LibComponent) : IPCB_Primitive;-Var-    GIt : IPCB_GroupIterator;-Begin-    GIt := Fp.GroupIterator_Create;-    GIt.AddFilter_LayerSet(AllLayers);-    Result := GIt.FirstPCBObject;-    Fp.GroupIterator_Destroy(GIt);-End;--Function CmdXCopyFootprint(Lines : TStringList) : String;-Var-    SrcLib, DstLib : IPCB_Library;-    Src, Dst : IPCB_LibComponent;-    GIt  : IPCB_GroupIterator;-    Prim, Cpy : IPCB_Primitive;-    SP, DP : IPCB_Pad;-    NCopied, SX, SY : Integer;-    Dflt : IPCB_LibComponent;-    Dropped, PN : String;-Begin-    DstLib := PCBServer.GetCurrentPCBLibrary;-    If DstLib = Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the destination PcbLib first') + '}';-        Exit;-    End;-    SrcLib := PCBServer.LoadPCBLibraryByPath(Param(Lines, 'fromLib'));-    If SrcLib = Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','source library not loadable: ' + Param(Lines, 'fromLib')) + '}';-        Exit;-    End;-    Src := FindFp(SrcLib, Param(Lines, 'from'));-    If Src = Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','footprint not found in source: ' + Param(Lines, 'from')) + '}';-        Exit;-    End;-    If FindFp(DstLib, Param(Lines, 'to')) <> Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','target already exists: ' + Param(Lines, 'to')) + '}';-        Exit;-    End;-    { the source's first pad, from its origin, read while the source is the footprint on display: only that one sits-      on the library's origin (lib_fp reads the same way) }-    If SrcLib.CurrentComponent <> Src Then Begin SrcLib.CurrentComponent := Src; SrcLib.Board.ViewManager_FullUpdate; End;-    { one pad, named once: the order Altium walks a footprint's pads in changes when the display moves }-    PN := FirstPadName(Src);-    SP := FpPad(Src, PN);-    If SP <> Nil Then Begin SX := SP.X - SrcLib.Board.XOrigin; SY := SP.Y - SrcLib.Board.YOrigin; End;-    PCBServer.PreProcess;-    Dst := PCBServer.CreatePCBLibComp;-    Dst.Name := Param(Lines, 'to');-    Dst.Description := Src.Description;-    NCopied := 0;-    GIt := Src.GroupIterator_Create;-    GIt.AddFilter_LayerSet(AllLayers);-    Prim := GIt.FirstPCBObject;-    While Prim <> Nil Do-    Begin-        Cpy := Prim.Replicate;-        { the source is on display, so its primitives sit on the library's origin; a footprint not on display keeps-          them from the origin itself, and Altium puts it back on the origin when it is shown }-        Cpy.MoveByXY(-SrcLib.Board.XOrigin, -SrcLib.Board.YOrigin);-        Dst.AddPCBObject(Cpy);-        NCopied := NCopied + 1;-        Prim := GIt.NextPCBObject;-    End;-    Src.GroupIterator_Destroy(GIt);-    DstLib.RegisterComponent(Dst);-    { Moving the copy while it is on display does not hold: Altium shifted it again when the display left it and came-      back (the copy landed one origin, 1270 mm, off, then 6 mm off: altium-bridge#29, 2026-10-30). The replicas are-      made from the origin instead, above, and the copy is checked once shown. }-    DstLib.CurrentComponent := Dst; DstLib.Board.ViewManager_FullUpdate;-    { a PcbLib Altium has just made holds an empty PCBComponent_1: it goes once a real footprint is in }-    Dropped := '';-    Dflt := FindFp(DstLib, 'PCBComponent_1');-    If (Dflt <> Nil) And (UpperCase(Dst.Name) <> 'PCBCOMPONENT_1') And (FirstPadName(Dflt) = '') And (FpFirstAny(Dflt) = Nil) Then-    Begin-        DstLib.RemoveComponent(Dflt);-        Dropped := 'PCBComponent_1';-    End;-    PCBServer.PostProcess;-    DstLib.Board.ViewManager_FullUpdate;-    { read back on display: the copy's first pad must sit where the source's does }-    DP := FpPad(Dst, PN);-    If (SP <> Nil) And (DP <> Nil) And (DstLib.CurrentComponent = Dst) Then-        If (DP.X - DstLib.Board.XOrigin <> SX) Or (DP.Y - DstLib.Board.YOrigin <> SY) Then-        Begin-            Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','copy_misplaced') + ',' + JStr('error', 'the copy of ' + Src.Name + ' landed off its origin: pad ' + DP.Name + ' at ' + IntToStr(DP.X - DstLib.Board.XOrigin) + ',' + IntToStr(DP.Y - DstLib.Board.YOrigin) + ' where the source has ' + IntToStr(SX) + ',' + IntToStr(SY) + ' (internal units)') + '}';-            Exit;-        End;-    Result := '{' + JRaw('ok','true') + ',' + JStr('to', Dst.Name) + ',' + JRaw('primitives', IntToStr(NCopied)) + ',' + JStr('removedDefault', Dropped) + '}';-End;----{ ---- Phase 1: board introspection, read-only (altium-bridge parity plan) ---- }-{ Every pad of a component as name=net pairs, all pads, capped. }-Function AllPadNets(Comp : IPCB_Component) : String;-Var-    GIt : IPCB_GroupIterator;-    Pad : IPCB_Pad;-    S, N : String;-    K : Integer;-Begin-    S := ''; K := 0;-    GIt := Comp.GroupIterator_Create;-    GIt.AddFilter_ObjectSet(MkSet(ePadObject));-    GIt.AddFilter_LayerSet(AllLayers);-    Pad := GIt.FirstPCBObject;-    While (Pad <> Nil) And (K < 400) Do-    Begin-        N := '';-        If Pad.Net <> Nil Then N := Pad.Net.Name;-        If S <> '' Then S := S + ';';-        S := S + Pad.Name + '=' + N;-        K := K + 1;-        Pad := GIt.NextPCBObject;-    End;-    Comp.GroupIterator_Destroy(GIt);-    Result := S;-End;--Function FindCompByDes(Board : IPCB_Board; Des : String) : IPCB_Component;-Var-    BIt  : IPCB_BoardIterator;-    Comp : IPCB_Component;-Begin-    Result := Nil;-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eComponentObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Comp := BIt.FirstPCBObject;-    While (Comp <> Nil) And (Result = Nil) Do-    Begin-        If UpperCase(Comp.Name.Text) = UpperCase(Des) Then Result := Comp;-        Comp := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-End;--Function CompJson(Comp : IPCB_Component) : String;-Var-    R : TCoordRect;-    Src : String;-Begin-    R := Comp.BoundingRectangle;-    { the link to the schematic symbol (its UniqueId path); empty for a part placed by hand }-    Src := '';-    Try Src := Comp.SourceUniqueId; Except End;-    Result := '{' + JStr('designator', Comp.Name.Text) + ',' + JStr('sourceUniqueId', Src) + ',' +-        JStr('pattern', Comp.Pattern) + ',' +-        JStr('comment', Comp.Comment.Text) + ',' +-        JRaw('layerId', IntToStr(Comp.Layer)) + ',' +-        JRaw('bottom', JBool(Comp.Layer = eBottomLayer)) + ',' +-        JRaw('xMil', IntToStr(Round(CoordToMils(Comp.x)))) + ',' +-        JRaw('yMil', IntToStr(Round(CoordToMils(Comp.y)))) + ',' +-        JRaw('rotation', IntToStr(Round(Comp.Rotation))) + ',' +-        JRaw('wMil', IntToStr(Round(CoordToMils(R.Right - R.Left)))) + ',' +-        JRaw('hMil', IntToStr(Round(CoordToMils(R.Top - R.Bottom)))) + ',' +-        JRaw('pads', IntToStr(GroupCount(Comp, ePadObject))) + ',' +-        JStr('nets', AllPadNets(Comp)) + '}';-End;--{ bom_placement: every component's footprint, side and reference point relative to the board origin, for a-  pick-and-place file. Lean on purpose (no pads, no nets): a large board's full read takes minutes.-  x/y are RAW coords from the origin (10000 per mil), rotation in thousandths of a degree (locale-free). }-{ import_step: a STEP placed with the board as free 3D geometry (an enclosure, a bracket) for fit checks: a 3D body of-  the board itself, on a mechanical layer, belonging to no part. path= the STEP; x= y= mm (the board model's frame) where-  the STEP's own origin lands; rx= ry= rz= degrees; standoff= mm; layer= the mechanical layer id; side=top|bottom.-  Answers the body's extent (raw, absolute) and its height. }-Function CmdImportStep(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    Body  : IPCB_ComponentBody;-    Mdl   : IPCB_Model;-    R     : TCoordRect;-    Path  : String;-    L     : Integer;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_board') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    Path := Param(Lines, 'path');-    If Not FileExists(Path) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','file_not_found') + ',' + JStr('error','no STEP at ' + Path) + '}'; Exit; End;-    L := StrToIntDef(Param(Lines, 'layer'), eMechanical1);-    PCBServer.PreProcess;-    Body := PCBServer.PCBObjectFactory(eComponentBodyObject, eNoDimension, eCreate_Default);-    { a body with no layer is dropped on add, and an invisible one reads as missing (see add_body) }-    Body.Layer := L;-    Board.LayerStack.LayerObject[L].MechanicalLayerEnabled := True;-    Board.LayerIsDisplayed[L] := True;-    Body.BodyOpacity3D := 1.0;-    Mdl := Body.ModelFactory_FromFilename(Path, False);-    If Mdl = Nil Then Begin PCBServer.PostProcess; Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','model_not_loaded') + ',' + JStr('error','Altium could not load ' + Path) + '}'; Exit; End;-    Try Mdl.SetState(AdomFloat(Param(Lines, 'rx')), AdomFloat(Param(Lines, 'ry')), AdomFloat(Param(Lines, 'rz')), MmC(Param(Lines, 'standoff'))); Except End;-    Body.SetState_FromModel;-    Body.Model := Mdl;-    If Param(Lines, 'side') = 'bottom' Then Body.BodyProjection := eBoardSide_Bottom Else Body.BodyProjection := eBoardSide_Top;-    Body.MoveByXY(MmC(Param(Lines, 'x')), MmC(Param(Lines, 'y')));-    Board.AddPCBObject(Body);-    PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Body.I_ObjectAddress);-    PCBServer.PostProcess;-    Board.ViewManager_FullUpdate;-    R := Body.BoundingRectangle;-    Result := '{' + JRaw('ok','true') + ',' + JRaw('box', '[' + IntToStr(R.Left) + ',' + IntToStr(R.Bottom) + ',' + IntToStr(R.Right) + ',' + IntToStr(R.Top) + ']') + ',' +-        JRaw('standoff', IntToStr(Body.StandoffHeight)) + ',' + JRaw('height', IntToStr(Body.OverallHeight)) + ',' + JRaw('layer', IntToStr(Body.Layer)) + ',' +-        JStr('id', IntToStr(Body.I_ObjectAddress)) + '}';-End;--Function CmdBomPlacement(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    BIt   : IPCB_BoardIterator;-    Comp  : IPCB_Component;-    GIt   : IPCB_GroupIterator;-    Pad   : IPCB_Pad;-    S     : String;-    N     : Integer;-    Tht   : Boolean;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    S := ''; N := 0;-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eComponentObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Comp := BIt.FirstPCBObject;-    While Comp <> Nil Do-    Begin-        { through-hole: any pad with a hole (export_pos's smdOnly leaves these out) }-        Tht := False;-        GIt := Comp.GroupIterator_Create;-        GIt.AddFilter_ObjectSet(MkSet(ePadObject));-        Pad := GIt.FirstPCBObject;-        While (Pad <> Nil) And (Not Tht) Do Begin If Pad.HoleSize > 0 Then Tht := True; Pad := GIt.NextPCBObject; End;-        Comp.GroupIterator_Destroy(GIt);-        If S <> '' Then S := S + ',';-        S := S + '[' + '"' + JsonEsc(Comp.Name.Text) + '","' + JsonEsc(Comp.Pattern) + '",' +-            IntToStr(Comp.x - Board.XOrigin) + ',' + IntToStr(Comp.y - Board.YOrigin) + ',' +-            IntToStr(Round(Comp.Rotation * 1000)) + ',' + JBool(Comp.Layer = eBottomLayer) + ',"' + JsonEsc(Comp.Comment.Text) + '",' + JBool(Tht) + ']';-        N := N + 1;-        Comp := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-    Result := '{' + JRaw('ok','true') + ',' + JRaw('count', IntToStr(N)) + ',' + JRaw('comps', '[' + S + ']') + '}';-End;--{ comp_rects: every component's extent the way Altium's Component Clearance rule sees it: its 3D bodies and pads-  when it has a body, else every primitive but text (pads, silkscreen and other lines, fills, regions; measured-  2026-10-22: a part with no body clashed by its silkscreen outline). Raw units relative to the board origin:-  [des, l, b, r, t, bottom, hasBody] }-Function CmdCompRects(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    BIt   : IPCB_BoardIterator;-    GIt   : IPCB_GroupIterator;-    Comp  : IPCB_Component;-    Prim  : IPCB_Primitive;-    R     : TCoordRect;-    S     : String;-    L, B, Rt, T, L2, B2, R2, T2 : Integer;-    AnyAll, AnyBody, HasBody : Boolean;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_board') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    S := '';-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eComponentObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Comp := BIt.FirstPCBObject;-    While Comp <> Nil Do-    Begin-        AnyAll := False; AnyBody := False; HasBody := False;-        L := 0; B := 0; Rt := 0; T := 0; L2 := 0; B2 := 0; R2 := 0; T2 := 0;-        GIt := Comp.GroupIterator_Create;-        GIt.AddFilter_ObjectSet(MkSet(ePadObject, eTrackObject, eArcObject, eFillObject, eRegionObject, eComponentBodyObject));-        Prim := GIt.FirstPCBObject;-        While Prim <> Nil Do-        Begin-            R := Prim.BoundingRectangle;-            If Not AnyAll Then Begin L := R.Left; B := R.Bottom; Rt := R.Right; T := R.Top; AnyAll := True; End-            Else Begin-                If R.Left < L Then L := R.Left; If R.Bottom < B Then B := R.Bottom;-                If R.Right > Rt Then Rt := R.Right; If R.Top > T Then T := R.Top;-            End;-            If (Prim.ObjectId = ePadObject) Or (Prim.ObjectId = eComponentBodyObject) Then-            Begin-                If Prim.ObjectId = eComponentBodyObject Then HasBody := True;-                If Not AnyBody Then Begin L2 := R.Left; B2 := R.Bottom; R2 := R.Right; T2 := R.Top; AnyBody := True; End-                Else Begin-                    If R.Left < L2 Then L2 := R.Left; If R.Bottom < B2 Then B2 := R.Bottom;-                    If R.Right > R2 Then R2 := R.Right; If R.Top > T2 Then T2 := R.Top;-                End;-            End;-            Prim := GIt.NextPCBObject;-        End;-        Comp.GroupIterator_Destroy(GIt);-        If HasBody Then Begin L := L2; B := B2; Rt := R2; T := T2; End;-        If AnyAll Then-        Begin-            If S <> '' Then S := S + ',';-            S := S + '["' + JsonEsc(Comp.Name.Text) + '",' + IntToStr(L - Board.XOrigin) + ',' + IntToStr(B - Board.YOrigin) + ',' +-                IntToStr(Rt - Board.XOrigin) + ',' + IntToStr(T - Board.YOrigin) + ',' + JBool(Comp.Layer = eBottomLayer) + ',' + JBool(HasBody) + ']';-        End;-        Comp := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-    Result := '{' + JRaw('ok','true') + ',' + JRaw('rects', '[' + S + ']') + '}';-End;--{ list_designators: every component designator on the current board }-Function CmdListDesignators(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    BIt   : IPCB_BoardIterator;-    Comp  : IPCB_Component;-    S     : String;-    N     : Integer;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    S := ''; N := 0;-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eComponentObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Comp := BIt.FirstPCBObject;-    While Comp <> Nil Do-    Begin-        If S <> '' Then S := S + ',';-        S := S + '"' + JsonEsc(Comp.Name.Text) + '"';-        N := N + 1;-        Comp := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-    Result := '{' + JRaw('ok','true') + ',' + JRaw('count', IntToStr(N)) + ',' + JRaw('designators', '[' + S + ']') + '}';-End;--{ component_data: designator=<one> or all (limit=N, default 500) }-Function CmdComponentData(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    BIt   : IPCB_BoardIterator;-    Comp  : IPCB_Component;-    Des, S : String;-    N, Limit : Integer;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    Des := Param(Lines, 'designator');-    Limit := StrToIntDef(Param(Lines, 'limit'), 500);-    If Des <> '' Then-    Begin-        Comp := FindCompByDes(Board, Des);-        If Comp = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','designator not found: ' + Des) + '}'; Exit; End;-        Result := '{' + JRaw('ok','true') + ',' + JRaw('component', CompJson(Comp)) + '}';-        Exit;-    End;-    S := ''; N := 0;-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eComponentObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Comp := BIt.FirstPCBObject;-    While (Comp <> Nil) And (N < Limit) Do-    Begin-        If S <> '' Then S := S + ',';-        S := S + CompJson(Comp);-        N := N + 1;-        Comp := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-    Result := '{' + JRaw('ok','true') + ',' + JRaw('count', IntToStr(N)) + ',' + JRaw('components', '[' + S + ']') + '}';-End;--{ component_pins: designator=<one> -> every pad with net, position, size, hole }-Function CmdComponentPins(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    Comp  : IPCB_Component;-    GIt   : IPCB_GroupIterator;-    Pad   : IPCB_Pad;-    Des, S, N : String;-    K : Integer;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    Des := Param(Lines, 'designator');-    If Des = '' Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','designator is required') + '}'; Exit; End;-    Comp := FindCompByDes(Board, Des);-    If Comp = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','designator not found: ' + Des) + '}'; Exit; End;-    S := ''; K := 0;-    GIt := Comp.GroupIterator_Create;-    GIt.AddFilter_ObjectSet(MkSet(ePadObject));-    GIt.AddFilter_LayerSet(AllLayers);-    Pad := GIt.FirstPCBObject;-    While (Pad <> Nil) And (K < 1000) Do-    Begin-        N := '';-        If Pad.Net <> Nil Then N := Pad.Net.Name;-        If S <> '' Then S := S + ',';-        S := S + '{' + JStr('name', Pad.Name) + ',' + JStr('net', N) + ',' +-            JRaw('xMil', IntToStr(Round(CoordToMils(Pad.x)))) + ',' +-            JRaw('yMil', IntToStr(Round(CoordToMils(Pad.y)))) + ',' +-            JRaw('layerId', IntToStr(Pad.Layer)) + ',' +-            JRaw('wMil', IntToStr(Round(CoordToMils(Pad.TopXSize)))) + ',' +-            JRaw('hMil', IntToStr(Round(CoordToMils(Pad.TopYSize)))) + ',' +-            JRaw('holeMil', IntToStr(Round(CoordToMils(Pad.HoleSize)))) + '}';-        K := K + 1;-        Pad := GIt.NextPCBObject;-    End;-    Comp.GroupIterator_Destroy(GIt);-    Result := '{' + JRaw('ok','true') + ',' + JStr('designator', Comp.Name.Text) + ',' + JRaw('count', IntToStr(K)) + ',' + JRaw('pins', '[' + S + ']') + '}';-End;--{ list_nets: every net on the board with its pad count (tallied over all pads) }-Function CmdListNets(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    BIt   : IPCB_BoardIterator;-    Pad   : IPCB_Pad;-    SL    : TStringList;-    S, N  : String;-    I, C  : Integer;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    SL := TStringList.Create;-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(ePadObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Pad := BIt.FirstPCBObject;-    While Pad <> Nil Do-    Begin-        If Pad.Net <> Nil Then-        Begin-            N := Pad.Net.Name;-            C := StrToIntDef(SL.Values[N], 0);-            SL.Values[N] := IntToStr(C + 1);-        End;-        Pad := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-    S := '';-    For I := 0 To SL.Count - 1 Do-    Begin-        If S <> '' Then S := S + ',';-        S := S + '{' + JStr('name', SL.Names[I]) + ',' + JRaw('pads', SL.Values[SL.Names[I]]) + '}';-    End;-    Result := '{' + JRaw('ok','true') + ',' + JRaw('count', IntToStr(SL.Count)) + ',' + JRaw('nets', '[' + S + ']') + '}';-    SL.Free;-End;--Function LayerJson(Board : IPCB_Board; L : Integer; Kind : String) : String;-Begin-    Result := '{' + JStr('name', Board.LayerStack.LayerObject[L].Name) + ',' +-        JRaw('id', IntToStr(L)) + ',' + JStr('kind', Kind) + ',' +-        JRaw('displayed', JBool(Board.LayerIsDisplayed[L])) + '}';-End;--{ layer_stack: the copper layers in stack order with their copper thickness and the dielectric under each-  (height, constant x1000, material). Classic stack API (FirstLayer / NextLayer / Dielectric). }-Function CmdLayerStack(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    LS    : IPCB_LayerStack_V7;-    L     : IPCB_LayerObject_V7;-    S     : String;-    N, Tot : Integer;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    S := ''; N := 0; Tot := 0;-    LS := Board.LayerStack_V7;-    L := LS.FirstLayer;-    While (L <> Nil) And (N < 64) Do-    Begin-        If S <> '' Then S := S + ',';-        S := S + '{' + JStr('name', L.Name) + ',' + JRaw('layerId', IntToStr(L.V6_LayerID)) + ',' + JRaw('copperUm', IntToStr(Round(CoordToMils(L.CopperThickness) * 25.4)));-        If (L.V6_LayerID >= eInternalPlane1) And (L.V6_LayerID <= eInternalPlane16) Then-            S := S + ',' + JStr('kind', 'plane') + ',' + JStr('net', L.NetName) + ',' + JRaw('plane', IntToStr(L.V6_LayerID - eInternalPlane1 + 1))-        Else If (L.V6_LayerID >= eMidLayer1) And (L.V6_LayerID <= eMidLayer30) Then-            S := S + ',' + JStr('kind', 'signal') + ',' + JRaw('mid', IntToStr(L.V6_LayerID - eMidLayer1 + 1))-        Else S := S + ',' + JStr('kind', 'signal');-        Tot := Tot + L.CopperThickness;-        { a layer's dielectric is the one below it: the last copper layer has none in the board }-        If (L.Dielectric <> Nil) And (LS.NextLayer(L) <> Nil) Then-        Begin-            S := S + ',' + JRaw('dielectricUm', IntToStr(Round(CoordToMils(L.Dielectric.DielectricHeight) * 25.4))) + ',' +-                JRaw('dk1000', IntToStr(Round(L.Dielectric.DielectricConstant * 1000))) + ',' + JStr('material', L.Dielectric.DielectricMaterial);-            Tot := Tot + L.Dielectric.DielectricHeight;-        End;-        S := S + '}';-        N := N + 1;-        L := LS.NextLayer(L);-    End;-    Result := '{' + JRaw('ok','true') + ',' + JRaw('copperLayers', IntToStr(N)) + ',' + JRaw('totalUm', IntToStr(Round(CoordToMils(Tot) * 25.4))) + ',' + JRaw('layers', '[' + S + ']') + '}';-End;--{ layer_stack_edit: op=insert (layer=<V6 id: plane N = eInternalPlane1+N-1, mid N = eMidLayer1+N-1>, net for a plane,-  name) | remove (layer). Through Board.LayerStack_V7 (the classic LayerStack halts on AD 26). Answers the stack after. }-Function CmdLayerStackEdit(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    LS    : IPCB_LayerStack_V7;-    L     : IPCB_LayerObject_V7;-    Op, Net, Nm : String;-    Id : Integer;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_board') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    Op := Param(Lines, 'op'); Net := Param(Lines, 'net'); Nm := Param(Lines, 'name');-    If Param(Lines, 'kind') = 'plane' Then Id := eInternalPlane1 + StrToIntDef(Param(Lines, 'n'), 1) - 1-    Else Id := eMidLayer1 + StrToIntDef(Param(Lines, 'n'), 1) - 1;-    LS := Board.LayerStack_V7;-    PCBServer.PreProcess;-    If Op = 'insert' Then-    Begin-        LS.InsertLayer(Id);-        L := LS.LayerObject(Id);-        If (L <> Nil) And (Nm <> '') Then L.Name := Nm;-        If (L <> Nil) And (Net <> '') Then L.NetName := Net;-    End-    Else If Op = 'remove' Then-    Begin-        L := LS.LayerObject(Id);-        If L <> Nil Then LS.RemoveFromStack(L);-    End-    Else If Op = 'set' Then-    Begin-        { by name: copper (um), the dielectric BELOW it (um, Dk x 1000, material), a plane's net and pullback (um) }-        L := LS.FirstLayer;-        While L <> Nil Do-        Begin-            If UpperCase(L.Name) = UpperCase(Param(Lines, 'target')) Then-            Begin-                If Param(Lines, 'copperUm') <> '' Then L.CopperThickness := MilsToCoord(StrToIntDef(Param(Lines, 'copperUm'), 35) / 25.4);-                If (L.Dielectric <> Nil) And (Param(Lines, 'dielectricUm') <> '') Then L.Dielectric.DielectricHeight := MilsToCoord(StrToIntDef(Param(Lines, 'dielectricUm'), 200) / 25.4);-                If (L.Dielectric <> Nil) And (Param(Lines, 'dk1000') <> '') Then L.Dielectric.DielectricConstant := StrToIntDef(Param(Lines, 'dk1000'), 4200) / 1000;-                If (L.Dielectric <> Nil) And (Param(Lines, 'material') <> '') Then L.Dielectric.DielectricMaterial := Param(Lines, 'material');-                If Nm <> '' Then L.Name := Nm;-                If (L.V6_LayerID >= eInternalPlane1) And (L.V6_LayerID <= eInternalPlane16) Then-                Begin-                    If Net <> '' Then L.NetName := Net;-                    If Param(Lines, 'pullbackUm') <> '' Then L.PullBackDistance := MilsToCoord(StrToIntDef(Param(Lines, 'pullbackUm'), 500) / 25.4);-                End;-            End;-            L := LS.NextLayer(L);-        End;-    End;-    PCBServer.PostProcess;-    Board.ViewManager_UpdateLayerTabs;-    Board.ViewManager_FullUpdate;-    Result := CmdLayerStack(Lines);-End;--{ layers: the fixed layer set with names and visibility (stackup detail is a later verb) }-Function CmdLayers(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    S : String;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    S := LayerJson(Board, eTopLayer, 'signal') + ',' + LayerJson(Board, eBottomLayer, 'signal') + ',' +-        LayerJson(Board, eTopOverlay, 'overlay') + ',' + LayerJson(Board, eBottomOverlay, 'overlay') + ',' +-        LayerJson(Board, eTopPaste, 'paste') + ',' + LayerJson(Board, eBottomPaste, 'paste') + ',' +-        LayerJson(Board, eTopSolder, 'solder') + ',' + LayerJson(Board, eBottomSolder, 'solder') + ',' +-        LayerJson(Board, eKeepOutLayer, 'keepout') + ',' + LayerJson(Board, eMultiLayer, 'multi') + ',' +-        LayerJson(Board, eDrillGuide, 'drill') + ',' + LayerJson(Board, eDrillDrawing, 'drill') + ',' +-        LayerJson(Board, eMechanical1, 'mechanical') + ',' + LayerJson(Board, eMechanical2, 'mechanical') + ',' +-        LayerJson(Board, eMechanical3, 'mechanical') + ',' + LayerJson(Board, eMechanical4, 'mechanical') + ',' +-        LayerJson(Board, eMechanical5, 'mechanical') + ',' + LayerJson(Board, eMechanical6, 'mechanical') + ',' +-        LayerJson(Board, eMechanical7, 'mechanical') + ',' + LayerJson(Board, eMechanical8, 'mechanical') + ',' +-        LayerJson(Board, eMechanical9, 'mechanical') + ',' + LayerJson(Board, eMechanical10, 'mechanical') + ',' +-        LayerJson(Board, eMechanical11, 'mechanical') + ',' + LayerJson(Board, eMechanical12, 'mechanical') + ',' +-        LayerJson(Board, eMechanical13, 'mechanical') + ',' + LayerJson(Board, eMechanical14, 'mechanical') + ',' +-        LayerJson(Board, eMechanical15, 'mechanical') + ',' + LayerJson(Board, eMechanical16, 'mechanical');-    Result := '{' + JRaw('ok','true') + ',' + JRaw('layers', '[' + S + ']') + '}';-End;---{ ---- Phase 1 batch 2 ---------------------------------------------------- }-{ net_connections: every pad on the board with its net and owner, plus per-net span.-  net=<name> filters to one net. limit caps pads (default 4000). }-Function CmdNetConnections(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    BIt   : IPCB_BoardIterator;-    Pad   : IPCB_Pad;-    Want, S, N, Own, Spans, V : String;-    K, Limit, X, Y, I : Integer;-    SL    : TStringList;-    P     : Integer;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    Want := Param(Lines, 'net');-    Limit := StrToIntDef(Param(Lines, 'limit'), 4000);-    SL := TStringList.Create;-    S := ''; K := 0;-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(ePadObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Pad := BIt.FirstPCBObject;-    While (Pad <> Nil) And (K < Limit) Do-    Begin-        If Pad.Net <> Nil Then-        Begin-            N := Pad.Net.Name;-            If (Want = '') Or (UpperCase(N) = UpperCase(Want)) Then-            Begin-                Own := '';-                If Pad.Component <> Nil Then Own := Pad.Component.Name.Text;-                X := Round(CoordToMils(Pad.x)); Y := Round(CoordToMils(Pad.y));-                If S <> '' Then S := S + ',';-                S := S + '{' + JStr('net', N) + ',' + JStr('pad', Own + '.' + Pad.Name) + ',' +-                    JRaw('xMil', IntToStr(X)) + ',' + JRaw('yMil', IntToStr(Y)) + '}';-                K := K + 1;-                V := SL.Values[N];-                If V = '' Then SL.Values[N] := IntToStr(X) + '|' + IntToStr(Y) + '|' + IntToStr(X) + '|' + IntToStr(Y) + '|1'-                Else-                Begin-                    P := Pos('|', V); Spans := Copy(V, 1, P - 1); V := Copy(V, P + 1, Length(V));-                    I := StrToIntDef(Spans, X); If X < I Then I := X; Spans := IntToStr(I);-                    P := Pos('|', V); N := Copy(V, 1, P - 1); V := Copy(V, P + 1, Length(V));-                    I := StrToIntDef(N, Y); If Y < I Then I := Y; Spans := Spans + '|' + IntToStr(I);-                    P := Pos('|', V); N := Copy(V, 1, P - 1); V := Copy(V, P + 1, Length(V));-                    I := StrToIntDef(N, X); If X > I Then I := X; Spans := Spans + '|' + IntToStr(I);-                    P := Pos('|', V); N := Copy(V, 1, P - 1); V := Copy(V, P + 1, Length(V));-                    I := StrToIntDef(N, Y); If Y > I Then I := Y; Spans := Spans + '|' + IntToStr(I);-                    I := StrToIntDef(V, 0) + 1; Spans := Spans + '|' + IntToStr(I);-                    SL.Values[Pad.Net.Name] := Spans;-                End;-            End;-        End;-        Pad := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-    Spans := '';-    For I := 0 To SL.Count - 1 Do-    Begin-        If Spans <> '' Then Spans := Spans + ',';-        Spans := Spans + '{' + JStr('net', SL.Names[I]) + ',' + JStr('minXminYmaxXmaxYpads', SL.Values[SL.Names[I]]) + '}';-    End;-    SL.Free;-    Result := '{' + JRaw('ok','true') + ',' + JRaw('count', IntToStr(K)) + ',' + JRaw('pads', '[' + S + ']') + ',' + JRaw('nets', '[' + Spans + ']') + '}';-End;--{ schematic_data: components on the CURRENT schematic sheet with pins.-  designator=<one> filters. pins=0 skips pin detail. limit caps components. }-Function CmdSchematicData(Lines : TStringList) : String;-Var-    Doc  : ISch_Document;-    Iter : ISch_Iterator;-    PIt  : ISch_Iterator;-    Comp : ISch_Component;-    Pin  : ISch_Pin;-    Prm  : ISch_Parameter;-    QIt  : ISch_Iterator;-    Want, S, PS, MS, PF, PV, Desc : String;-    N, Limit, PN, T0, Budget : Integer;-    WantPins, WantParams, Late : Boolean;-    Skipped : String;-Begin-    Doc := SchServer.GetCurrentSchDocument;-    If Doc = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_schematic') + ',' + JStr('error','no current schematic document (open a .SchDoc first)') + '}'; Exit; End;-    { a schematic LIBRARY is an ISch_Document too: read as a sheet it answered ok with 0 parts (#21) }-    If Doc.ObjectID = eSchLib Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_schematic') + ',' + JStr('error','the current document is a schematic library, not a sheet: ' + Doc.DocumentName) + '}'; Exit; End;-    Want := Param(Lines, 'designator');-    Limit := StrToIntDef(Param(Lines, 'limit'), 500);-    WantPins := Param(Lines, 'pins') <> '0';-    WantParams := Param(Lines, 'params') = '1';-    PF := UpperCase(Param(Lines, 'paramFilter'));-    { #28: a 6-part sheet's parameters ran out the whole 120 s on one machine and were never reproduced. A budget for-      parameters and descriptions: once spent, the remaining parts come back without them and are named in paramsSkipped,-      so the bridge fills them from the saved file instead of the call timing out }-    Budget := StrToIntDef(Param(Lines, 'paramBudgetMs'), 60000);-    T0 := GetTickCount;-    Skipped := '';-    S := ''; N := 0;-    Iter := Doc.SchIterator_Create;-    Iter.AddFilter_ObjectSet(MkSet(eSchComponent));-    Comp := Iter.FirstSchObject;-    While (Comp <> Nil) And (N < Limit) Do-    Begin-        If (Want = '') Or (UpperCase(Comp.Designator.Text) = UpperCase(Want)) Then-        Begin-            PS := '';-            If WantPins Then-            Begin-                PIt := Comp.SchIterator_Create;-                PIt.AddFilter_ObjectSet(MkSet(ePin));-                Pin := PIt.FirstSchObject;-                While Pin <> Nil Do-                Begin-                    { a placed part carries every part's and display mode's pins: list the ones it shows (its part,-                      the shared part 0, its display mode) }-                    If ((Pin.OwnerPartId = Comp.CurrentPartID) Or (Pin.OwnerPartId = 0)) And (Pin.OwnerPartDisplayMode = Comp.DisplayMode) Then-                    Begin-                        If PS <> '' Then PS := PS + ',';-                        PS := PS + '{' + JStr('number', Pin.Designator) + ',' + JStr('name', Pin.Name) + ',' +-                            JRaw('electrical', IntToStr(Pin.Electrical)) + ',' +-                            JRaw('xMil', IntToStr(Round(CoordToMils(Pin.Location.X)))) + ',' + JRaw('yMil', IntToStr(Round(CoordToMils(Pin.Location.Y)))) + ',' +-                            JRaw('orientation', IntToStr(Pin.Orientation)) + ',' + JRaw('lengthMil', IntToStr(Round(CoordToMils(Pin.PinLength)))) + ',' +-                            JRaw('part', IntToStr(Pin.OwnerPartId)) + ',' + JRaw('hidden', JBool(Pin.IsHidden)) + ',' + JStr('hiddenNet', Pin.HiddenNetName) + '}';-                    End;-                    Pin := PIt.NextSchObject;-                End;-                Comp.SchIterator_Destroy(PIt);-            End;-            MS := '';-            Late := GetTickCount - T0 > Budget;-            If WantParams And Late Then-            Begin-                If Skipped <> '' Then Skipped := Skipped + ',';-                Skipped := Skipped + '"' + JsonEsc(Comp.Designator.Text) + '"';-            End;-            If WantParams And (Not Late) Then-            Begin-                PN := 0;-                QIt := Comp.SchIterator_Create;-                QIt.AddFilter_ObjectSet(MkSet(eParameter));-                Prm := QIt.FirstSchObject;-                While (Prm <> Nil) And (PN < 40) And (GetTickCount - T0 <= Budget) Do-                Begin-                    If MS <> '' Then MS := MS + ',';-                    { paramFilter=|NAME|NAME| reads only those values: an example IC's three 'Available Preview Images'-                      parameters held so much that reading them took minutes (2026-10-21). Long values are cut. }-                    If (Param(Lines, 'paramNamesOnly') = '1') Or ((PF <> '') And (Pos('|' + UpperCase(Prm.Name) + '|', PF) = 0)) Then-                        MS := MS + '{' + JStr('name', Prm.Name) + '}'-                    Else-                    Begin-                        PV := Prm.Text;-                        If Length(PV) > 2000 Then-                            MS := MS + '{' + JStr('name', Prm.Name) + ',' + JStr('value', Copy(PV, 1, 2000)) + ',' + JRaw('truncatedFrom', IntToStr(Length(PV))) + '}'-                        Else-                            MS := MS + '{' + JStr('name', Prm.Name) + ',' + JStr('value', PV) + '}';-                    End;-                    PN := PN + 1;-                    Prm := QIt.NextSchObject;-                End;-                Comp.SchIterator_Destroy(QIt);-            End;-            { a workspace-managed part's description is fetched from its vault: on the Bluetooth Sentinel-              example's Altium Content Vault parts that call never returned (2026-10-21). Their item GUID says-              where they live; the description of an unmanaged part is read as before. }-            If (Comp.ItemGUID = '') And (Not Late) Then Desc := Comp.ComponentDescription Else Desc := '';-            If S <> '' Then S := S + ',';-            S := S + '{' + JRaw('params', '[' + MS + ']') + ',' + JStr('designator', Comp.Designator.Text) + ',' +-                JRaw('partId', IntToStr(Comp.CurrentPartID)) + ',' +-                JRaw('partCount', IntToStr(Comp.PartCount)) + ',' +-                JStr('libReference', Comp.LibReference) + ',' +-                JStr('comment', Comp.Comment.Text) + ',' +-                JStr('description', Desc) + ',' +-                JStr('uniqueId', Comp.UniqueId) + ',' +-                JStr('itemGuid', Comp.ItemGUID) + ',' +-                JStr('vaultGuid', Comp.VaultGUID) + ',' +-                JStr('sourceLibrary', Comp.SourceLibraryName) + ',' +-                JRaw('kind', IntToStr(Comp.ComponentKind)) + ',' +-                JRaw('xMil', IntToStr(Round(CoordToMils(Comp.Location.X)))) + ',' +-                JRaw('yMil', IntToStr(Round(CoordToMils(Comp.Location.Y)))) + ',' +-                JRaw('orientation', IntToStr(Comp.Orientation)) + ',' +-                JRaw('pins', '[' + PS + ']') + '}';-            N := N + 1;-        End;-        Comp := Iter.NextSchObject;-    End;-    Doc.SchIterator_Destroy(Iter);-    Result := '{' + JRaw('ok','true') + ',' + JStr('document', Doc.DocumentName) + ',' + JRaw('count', IntToStr(N)) + ',' +-        JRaw('paramsSkipped', '[' + Skipped + ']') + ',' + JRaw('elapsedMs', IntToStr(GetTickCount - T0)) + ',' + JRaw('components', '[' + S + ']') + '}';-End;--{ sheet_params: set parameters on PLACED components of the CURRENT schematic sheet (the BOM write-back).-  designators=R1,R2 (every part of a multi-part component)  params=Key=Value;Key=Value-  A missing parameter is added hidden; an existing one gets the value (PreProcess/PostProcess around the edit;-  c_BroadcastBroadcast robot messages are not declared in AD 26's script engine). Save with altium_save. }-Function CmdSheetParams(Lines : TStringList) : String;-Var-    Doc  : ISch_Document;-    Iter : ISch_Iterator;-    Comp : ISch_Component;-    Want, Items : TStringList;-    Spec, K, V, Found, Missing : String;-    I, P, NComp, NSet : Integer;-Begin-    Doc := SchServer.GetCurrentSchDocument;-    If (Doc = Nil) Or (Doc.ObjectID = eSchLib) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_schematic') + ',' + JStr('error','no current schematic sheet (open a .SchDoc first)') + '}'; Exit; End;-    Want := TStringList.Create;-    Want.Delimiter := ',';-    Want.StrictDelimiter := True;-    Want.DelimitedText := UpperCase(Param(Lines, 'designators'));-    Items := SplitSemi(Param(Lines, 'params'));-    If (Want.Count = 0) Or (Items.Count = 0) Then-    Begin-        Want.Free; Items.Free;-        Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','bad_args') + ',' + JStr('error','designators and params are required') + '}';-        Exit;-    End;-    Found := ''; NComp := 0; NSet := 0;-    SchServer.ProcessControl.PreProcess(Doc, '');-    Iter := Doc.SchIterator_Create;-    Iter.AddFilter_ObjectSet(MkSet(eSchComponent));-    Comp := Iter.FirstSchObject;-    While Comp <> Nil Do-    Begin-        If Want.IndexOf(UpperCase(Comp.Designator.Text)) >= 0 Then-        Begin-            SchServer.RobotManager.SendMessage(Comp.I_ObjectAddress, c_BroadCast, SCHM_BeginModify, c_NoEventData);-            For I := 0 To Items.Count - 1 Do-            Begin-                Spec := Items[I];-                P := Pos('=', Spec);-                If P > 0 Then-                Begin-                    K := Trim(Copy(Spec, 1, P - 1));-                    V := Trim(Copy(Spec, P + 1, Length(Spec)));-                    If K <> '' Then-                    Begin-                        SetSchParam(Comp, K, V); NSet := NSet + 1;-                        { the part's own description is what the BOM, pick and place and the board read: a-                          'Description' parameter alone left them on the library's text (2026-10-22) }-                        If UpperCase(K) = 'DESCRIPTION' Then Comp.ComponentDescription := V;-                    End;-                End;-            End;-            SchServer.RobotManager.SendMessage(Comp.I_ObjectAddress, c_BroadCast, SCHM_EndModify, c_NoEventData);-            If Pos('"' + Comp.Designator.Text + '"', Found) = 0 Then-            Begin-                If Found <> '' Then Found := Found + ',';-                Found := Found + '"' + JsonEsc(Comp.Designator.Text) + '"';-            End;-            NComp := NComp + 1;-        End;-        Comp := Iter.NextSchObject;-    End;-    Doc.SchIterator_Destroy(Iter);-    SchServer.ProcessControl.PostProcess(Doc, '');-    MarkSchModified(Doc);-    Doc.GraphicallyInvalidate;-    Missing := '';-    For I := 0 To Want.Count - 1 Do-        If Pos('"' + UpperCase(Want[I]) + '"', UpperCase(Found)) = 0 Then-        Begin-            If Missing <> '' Then Missing := Missing + ',';-            Missing := Missing + '"' + JsonEsc(Want[I]) + '"';-        End;-    Want.Free; Items.Free;-    Result := '{' + JRaw('ok','true') + ',' + JStr('document', Doc.DocumentName) + ',' + JRaw('parts', IntToStr(NComp)) + ',' +-        JRaw('paramsSet', IntToStr(NSet)) + ',' + JRaw('designators', '[' + Found + ']') + ',' + JRaw('notOnSheet', '[' + Missing + ']') + '}';-End;--{ copper_layers: the full copper set (top, mid 1-30, planes 1-16, bottom) with names and visibility }-Function CmdCopperLayers(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    S : String;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    S := LayerJson(Board, eTopLayer, 'signal');-    S := S + ',' + LayerJson(Board, eMidLayer1, 'signal');-    S := S + ',' + LayerJson(Board, eMidLayer2, 'signal');-    S := S + ',' + LayerJson(Board, eMidLayer3, 'signal');-    S := S + ',' + LayerJson(Board, eMidLayer4, 'signal');-    S := S + ',' + LayerJson(Board, eMidLayer5, 'signal');-    S := S + ',' + LayerJson(Board, eMidLayer6, 'signal');-    S := S + ',' + LayerJson(Board, eMidLayer7, 'signal');-    S := S + ',' + LayerJson(Board, eMidLayer8, 'signal');-    S := S + ',' + LayerJson(Board, eMidLayer9, 'signal');-    S := S + ',' + LayerJson(Board, eMidLayer10, 'signal');-    S := S + ',' + LayerJson(Board, eMidLayer11, 'signal');-    S := S + ',' + LayerJson(Board, eMidLayer12, 'signal');-    S := S + ',' + LayerJson(Board, eMidLayer13, 'signal');-    S := S + ',' + LayerJson(Board, eMidLayer14, 'signal');-    S := S + ',' + LayerJson(Board, eMidLayer15, 'signal');-    S := S + ',' + LayerJson(Board, eMidLayer16, 'signal');-    S := S + ',' + LayerJson(Board, eMidLayer17, 'signal');-    S := S + ',' + LayerJson(Board, eMidLayer18, 'signal');-    S := S + ',' + LayerJson(Board, eMidLayer19, 'signal');-    S := S + ',' + LayerJson(Board, eMidLayer20, 'signal');-    S := S + ',' + LayerJson(Board, eMidLayer21, 'signal');-    S := S + ',' + LayerJson(Board, eMidLayer22, 'signal');-    S := S + ',' + LayerJson(Board, eMidLayer23, 'signal');-    S := S + ',' + LayerJson(Board, eMidLayer24, 'signal');-    S := S + ',' + LayerJson(Board, eMidLayer25, 'signal');-    S := S + ',' + LayerJson(Board, eMidLayer26, 'signal');-    S := S + ',' + LayerJson(Board, eMidLayer27, 'signal');-    S := S + ',' + LayerJson(Board, eMidLayer28, 'signal');-    S := S + ',' + LayerJson(Board, eMidLayer29, 'signal');-    S := S + ',' + LayerJson(Board, eMidLayer30, 'signal');-    S := S + ',' + LayerJson(Board, eInternalPlane1, 'plane');-    S := S + ',' + LayerJson(Board, eInternalPlane2, 'plane');-    S := S + ',' + LayerJson(Board, eInternalPlane3, 'plane');-    S := S + ',' + LayerJson(Board, eInternalPlane4, 'plane');-    S := S + ',' + LayerJson(Board, eInternalPlane5, 'plane');-    S := S + ',' + LayerJson(Board, eInternalPlane6, 'plane');-    S := S + ',' + LayerJson(Board, eInternalPlane7, 'plane');-    S := S + ',' + LayerJson(Board, eInternalPlane8, 'plane');-    S := S + ',' + LayerJson(Board, eInternalPlane9, 'plane');-    S := S + ',' + LayerJson(Board, eInternalPlane10, 'plane');-    S := S + ',' + LayerJson(Board, eInternalPlane11, 'plane');-    S := S + ',' + LayerJson(Board, eInternalPlane12, 'plane');-    S := S + ',' + LayerJson(Board, eInternalPlane13, 'plane');-    S := S + ',' + LayerJson(Board, eInternalPlane14, 'plane');-    S := S + ',' + LayerJson(Board, eInternalPlane15, 'plane');-    S := S + ',' + LayerJson(Board, eInternalPlane16, 'plane');-    S := S + ',' + LayerJson(Board, eBottomLayer, 'signal');-    Result := '{' + JRaw('ok','true') + ',' + JRaw('layers', '[' + S + ']') + '}';-End;---{ ---- Phase 1 batch 3 ---------------------------------------------------- }-{ read_rules: every design rule on the board }-Function CmdReadRules(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    BIt   : IPCB_BoardIterator;-    Rule  : IPCB_Rule;-    S     : String;-    N     : Integer;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    S := ''; N := 0;-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eRuleObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Rule := BIt.FirstPCBObject;-    While Rule <> Nil Do-    Begin-        If S <> '' Then S := S + ',';-        S := S + '{' + JStr('name', Rule.Name) + ',' + JRaw('kind', IntToStr(Rule.RuleKind)) + ',' +-            JRaw('enabled', JBool(Rule.Enabled)) + ',' + JRaw('priority', IntToStr(Rule.Priority)) + ',' +-            JStr('scope1', Rule.Scope1Expression) + ',' + JStr('scope2', Rule.Scope2Expression) + ',' +-            JStr('comment', Rule.Comment) + ',' + JStr('descriptor', Rule.Descriptor) + '}';-        N := N + 1;-        Rule := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-    Result := '{' + JRaw('ok','true') + ',' + JRaw('count', IntToStr(N)) + ',' + JRaw('rules', '[' + S + ']') + '}';-End;--{ set_layer_visibility: layerId=<int> displayed=1|0 }-Function CmdSetLayerVisibility(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    L     : Integer;-    Show  : Boolean;-    LName : String;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    L := StrToIntDef(Param(Lines, 'layerId'), -1);-    If L < 0 Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','layerId (from altium_layers) is required') + '}'; Exit; End;-    Show := Param(Lines, 'displayed') <> '0';-    { an id that is not in THIS board's stack has a nil LayerObject: reading .Name off it halts the-      engine. Refuse by name instead (found by review, 2026-09-21). }-    If Board.LayerStack.LayerObject[L] = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','unknown_layer') + ',' + JStr('error','layerId ' + IntToStr(L) + ' is not in this board; altium_layers lists the valid ids') + '}'; Exit; End;-    LName := Board.LayerStack.LayerObject[L].Name;-    Board.LayerIsDisplayed[L] := Show;-    Board.ViewManager_UpdateLayerTabs;-    Board.ViewManager_FullUpdate;-    Result := '{' + JRaw('ok','true') + ',' + JRaw('layerId', IntToStr(L)) + ',' + JStr('name', LName) + ',' + JRaw('displayed', JBool(Board.LayerIsDisplayed[L])) + '}';-End;--{ project_variants: the variants Altium itself holds for an open project (path=<.PrjPcb>), each with its-  variations (designator, kind: 1 not fitted, 2 alternate). The bridge writes variants into the project-  file; this is the read-back that Altium took them. }-Function CmdProjectVariants(Lines : TStringList) : String;-Var-    WS   : IWorkspace;-    Prj  : IProject;-    V    : IProjectVariant;-    CV   : IComponentVariation;-    I, J : Integer;-    Want, S, M : String;-Begin-    Want := UpperCase(Param(Lines, 'path'));-    WS := GetWorkspace; Prj := Nil;-    If WS <> Nil Then-        For I := 0 To WS.DM_ProjectCount - 1 Do-            If UpperCase(WS.DM_Projects(I).DM_ProjectFullPath) = Want Then Prj := WS.DM_Projects(I);-    If Prj = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','project_not_open') + ',' + JStr('error','that project is not open in Altium') + '}'; Exit; End;-    S := '';-    For I := 0 To Prj.DM_ProjectVariantCount - 1 Do-    Begin-        V := Prj.DM_ProjectVariants(I);-        M := '';-        For J := 0 To V.DM_VariationCount - 1 Do-        Begin-            CV := V.DM_Variations(J);-            If M <> '' Then M := M + ',';-            M := M + '{' + JStr('designator', CV.DM_PhysicalDesignator) + ',' + JRaw('kind', IntToStr(CV.DM_VariationKind)) + '}';-        End;-        If S <> '' Then S := S + ',';-        S := S + '{' + JStr('name', V.DM_Description) + ',' + JRaw('variations', '[' + M + ']') + '}';-    End;-    Result := '{' + JRaw('ok','true') + ',' + JRaw('variants', '[' + S + ']') + '}';-End;--{ pcb_postprocess: close a board edit the script engine left open. A runtime error between PCBServer.PreProcess-  and PostProcess leaves Altium sure a command is still running: every save then asks 'A command is currently-  active and save cannot be completed' (measured 2026-10-22). The bridge sends this after such an error. }-Function CmdPcbPostProcess(Lines : TStringList) : String;-Begin-    PCBServer.PostProcess;-    Result := '{' + JRaw('ok','true') + '}';-End;--{ outline_tracks: the board shape drawn as tracks and arcs on a mechanical layer (mech=N, width raw), the-  outline a fab house reads from the Gerbers. A scripted board has its shape but nothing on any outline layer,-  so its GM1/GKO Gerbers came out empty (2026-10-22). op=check counts what the layer holds; op=draw draws-  unless the layer already has primitives (replace=1 draws anyway). }-Function CmdOutlineTracks(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    BO    : IPCB_BoardOutline;-    BIt   : IPCB_BoardIterator;-    Prim  : IPCB_Primitive;-    Seg, Nx : TPolySegment;-    Trk   : IPCB_Track;-    Arc   : IPCB_Arc;-    Lyr, W, K, Have, Drawn : Integer;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_board') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    Lyr := eMechanical1 + StrToIntDef(Param(Lines, 'mech'), 1) - 1;-    W := StrToIntDef(Param(Lines, 'width'), 39370);-    Have := 0;-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eTrackObject, eArcObject));-    BIt.AddFilter_LayerSet(MkSet(Lyr));-    BIt.AddFilter_Method(eProcessAll);-    Prim := BIt.FirstPCBObject;-    While Prim <> Nil Do Begin Have := Have + 1; Prim := BIt.NextPCBObject; End;-    Board.BoardIterator_Destroy(BIt);-    Drawn := 0;-    BO := Board.BoardOutline;-    If (Param(Lines, 'op') = 'draw') And (BO <> Nil) And ((Have = 0) Or (Param(Lines, 'replace') = '1')) Then-    Begin-        PCBServer.PreProcess;-        For K := 0 To BO.PointCount - 1 Do-        Begin-            Seg := BO.Segments[K];-            Nx := BO.Segments[(K + 1) Mod BO.PointCount];-            If Seg.Kind = ePolySegmentArc Then-            Begin-                Arc := PCBServer.PCBObjectFactory(eArcObject, eNoDimension, eCreate_Default);-                Arc.Layer := Lyr; Arc.XCenter := Seg.cx; Arc.YCenter := Seg.cy; Arc.Radius := Seg.Radius;-                Arc.LineWidth := W; Arc.StartAngle := Seg.Angle1; Arc.EndAngle := Seg.Angle2;-                Board.AddPCBObject(Arc);-                PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Arc.I_ObjectAddress);-            End-            Else-            Begin-                Trk := PCBServer.PCBObjectFactory(eTrackObject, eNoDimension, eCreate_Default);-                Trk.Layer := Lyr; Trk.X1 := Seg.vx; Trk.Y1 := Seg.vy; Trk.X2 := Nx.vx; Trk.Y2 := Nx.vy; Trk.Width := W;-                Board.AddPCBObject(Trk);-                PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Trk.I_ObjectAddress);-            End;-            Drawn := Drawn + 1;-        End;-        PCBServer.PostProcess;-        Board.ViewManager_FullUpdate;-    End;-    Result := '{' + JRaw('ok','true') + ',' + JStr('layer', Board.LayerStack.LayerObject[Lyr].Name) + ',' + JRaw('alreadyThere', IntToStr(Have)) + ',' +-        JRaw('drawn', IntToStr(Drawn)) + ',' + JRaw('outlineVertices', IntToStr(BO.PointCount)) + '}';-End;--{ track_width: set the width of every track and arc on the named nets (nets=A;B, width raw, layer=<V6 id> optional).-  Answers how many changed per net. }-Function CmdTrackWidth(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    BIt   : IPCB_BoardIterator;-    Prim  : IPCB_Primitive;-    Hits  : TInterfaceList;-    Want, Cnt : TStringList;-    W, Lyr, I : Integer;-    Nm, S : String;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_board') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    Want := TStringList.Create; Want.Delimiter := ';'; Want.StrictDelimiter := True; Want.DelimitedText := UpperCase(Param(Lines, 'nets'));-    Cnt := TStringList.Create;-    Hits := TInterfaceList.Create;-    W := StrToIntDef(Param(Lines, 'width'), 0);-    Lyr := StrToIntDef(Param(Lines, 'layer'), 0);-    { collect first, change after: a primitive that changes size moves in the board's spatial index, and changing it-      mid-iteration crashed Altium (2026-10-22) }-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eTrackObject, eArcObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Prim := BIt.FirstPCBObject;-    While Prim <> Nil Do-    Begin-        { routed copper only: a pour's own tracks (InPolygon) and a footprint's are left alone }-        If (Prim.Net <> Nil) And (Not Prim.InComponent) And (Not Prim.InPolygon) And ((Lyr = 0) Or (Prim.Layer = Lyr)) Then-            If Want.IndexOf(UpperCase(Prim.Net.Name)) >= 0 Then Hits.Add(Prim);-        Prim := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-    PCBServer.PreProcess;-    For I := 0 To Hits.Count - 1 Do-    Begin-        Prim := Hits[I];-        Nm := UpperCase(Prim.Net.Name);-        Prim.BeginModify;-        If Prim.ObjectId = eTrackObject Then Prim.Width := W Else Prim.LineWidth := W;-        Prim.EndModify;-        Cnt.Values[Nm] := IntToStr(StrToIntDef(Cnt.Values[Nm], 0) + 1);-    End;-    PCBServer.PostProcess;-    Board.ViewManager_FullUpdate;-    S := '';-    For I := 0 To Cnt.Count - 1 Do Begin If S <> '' Then S := S + ','; S := S + JRaw(Cnt.Names[I], Cnt.ValueFromIndex[I]); End;-    Result := '{' + JRaw('ok','true') + ',' + JRaw('changed', IntToStr(Hits.Count)) + ',' + JRaw('byNet', '{' + S + '}') + '}';-    Want.Free; Cnt.Free; Hits.Free;-End;--{ selection: what the user has selected in the CURRENT document. Board: components with position (raw, relative-  to the origin), rotation, side, footprint and extent. Sheet: components with location (mils) and orientation. }-Function CmdSelection(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    BIt   : IPCB_BoardIterator;-    Comp  : IPCB_Component;-    Doc   : ISch_Document;-    It    : ISch_Iterator;-    C     : ISch_Component;-    R     : TCoordRect;-    S, SetTo : String;-    Want  : TStringList;-Begin-    S := '';-    { set=A;B: select exactly these (everything else deselected) before reading }-    SetTo := UpperCase(Param(Lines, 'set'));-    Want := TStringList.Create; Want.Delimiter := ';'; Want.StrictDelimiter := True; Want.DelimitedText := SetTo;-    Board := PCBServer.GetCurrentPCBBoard;-    If Board <> Nil Then-    Begin-        BIt := Board.BoardIterator_Create;-        BIt.AddFilter_ObjectSet(MkSet(eComponentObject));-        BIt.AddFilter_LayerSet(AllLayers);-        BIt.AddFilter_Method(eProcessAll);-        Comp := BIt.FirstPCBObject;-        While Comp <> Nil Do-        Begin-            If SetTo <> '' Then Comp.Selected := Want.IndexOf(UpperCase(Comp.Name.Text)) >= 0;-            If Comp.Selected Then-            Begin-                R := Comp.BoundingRectangle;-                If S <> '' Then S := S + ',';-                S := S + '["' + JsonEsc(Comp.Name.Text) + '",' + IntToStr(Comp.x - Board.XOrigin) + ',' + IntToStr(Comp.y - Board.YOrigin) + ',' +-                    IntToStr(Round(Comp.Rotation * 1000)) + ',' + JBool(Comp.Layer = eBottomLayer) + ',"' + JsonEsc(Comp.Pattern) + '",' +-                    IntToStr(R.Right - R.Left) + ',' + IntToStr(R.Top - R.Bottom) + ']';-            End;-            Comp := BIt.NextPCBObject;-        End;-        Board.BoardIterator_Destroy(BIt);-        If SetTo <> '' Then Board.ViewManager_FullUpdate;-        Want.Free;-        Result := '{' + JRaw('ok','true') + ',' + JStr('kind','board') + ',' + JStr('document', Board.FileName) + ',' + JRaw('selected', '[' + S + ']') + '}';-        Exit;-    End;-    Doc := SchServer.GetCurrentSchDocument;-    If (Doc <> Nil) And (Doc.ObjectID <> eSchLib) Then-    Begin-        It := Doc.SchIterator_Create;-        It.AddFilter_ObjectSet(MkSet(eSchComponent));-        C := It.FirstSchObject;-        While C <> Nil Do-        Begin-            If SetTo <> '' Then C.Selection := Want.IndexOf(UpperCase(C.Designator.Text)) >= 0;-            If C.Selection Then-            Begin-                If S <> '' Then S := S + ',';-                S := S + '["' + JsonEsc(C.Designator.Text) + '",' + IntToStr(Round(CoordToMils(C.Location.X))) + ',' + IntToStr(Round(CoordToMils(C.Location.Y))) + ',' +-                    IntToStr(C.Orientation) + ',"' + JsonEsc(C.LibReference) + '","' + JsonEsc(C.Comment.Text) + '"]';-            End;-            C := It.NextSchObject;-        End;-        Doc.SchIterator_Destroy(It);-        If SetTo <> '' Then Doc.GraphicallyInvalidate;-        Want.Free;-        Result := '{' + JRaw('ok','true') + ',' + JStr('kind','sheet') + ',' + JStr('document', Doc.DocumentName) + ',' + JRaw('selected', '[' + S + ']') + '}';-        Exit;-    End;-    Want.Free;-    Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_document') + ',' + JStr('error','the current document is neither a board nor a schematic sheet') + '}';-End;--{ every copper layer of a width rule (top, mid 1-30, bottom): a rule set on top and bottom only left inner-  layers at Altium's default once a board had them }-Procedure SetWidthAllLayers(Rule : IPCB_Rule; A, B, C : Integer);-Var-    L : Integer;-Begin-    For L := eTopLayer To eBottomLayer Do-    Begin-        Rule.MinWidth[L] := A; Rule.FavoredWidth[L] := B; Rule.MaxWidth[L] := C;-    End;-End;--Function NetObjNamed(Board : IPCB_Board; Name : String) : IPCB_Net;-Var-    BIt : IPCB_BoardIterator;-    N   : IPCB_Net;-Begin-    Result := Nil;-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eNetObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    N := BIt.FirstPCBObject;-    While N <> Nil Do-    Begin-        If UpperCase(N.Name) = UpperCase(Name) Then Begin Result := N; Break; End;-        N := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-End;--{ net_classes: op=list | set (name, nets=A;B;C: replaces a class of that name) | remove (name).-  list answers every net class with its member nets; Altium's own 'All Nets' is flagged superClass. }-Function CmdNetClasses(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    BIt, NIt : IPCB_BoardIterator;-    Cls, Old : IPCB_ObjectClass;-    Net   : IPCB_Net;-    Op, Name, Nets, N, S, M, Missing : String;-    P, K  : Integer;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_board') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    Op := Param(Lines, 'op'); Name := Param(Lines, 'name');-    Old := Nil; S := '';-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eClassObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Cls := BIt.FirstPCBObject;-    While Cls <> Nil Do-    Begin-        If Cls.MemberKind = eClassMemberKind_Net Then-        Begin-            If (Name <> '') And (UpperCase(Cls.Name) = UpperCase(Name)) Then Old := Cls;-            If Op = 'list' Then-            Begin-                M := '';-                If Not Cls.SuperClass Then-                Begin-                    NIt := Board.BoardIterator_Create;-                    NIt.AddFilter_ObjectSet(MkSet(eNetObject));-                    NIt.AddFilter_LayerSet(AllLayers);-                    NIt.AddFilter_Method(eProcessAll);-                    Net := NIt.FirstPCBObject;-                    While Net <> Nil Do-                    Begin-                        If Cls.IsMember(Net) Then Begin If M <> '' Then M := M + ','; M := M + '"' + JsonEsc(Net.Name) + '"'; End;-                        Net := NIt.NextPCBObject;-                    End;-                    Board.BoardIterator_Destroy(NIt);-                End;-                If S <> '' Then S := S + ',';-                S := S + '{' + JStr('name', Cls.Name) + ',' + JRaw('superClass', JBool(Cls.SuperClass)) + ',' + JRaw('nets', '[' + M + ']') + '}';-            End;-        End;-        Cls := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-    If Op = 'list' Then Begin Result := '{' + JRaw('ok','true') + ',' + JRaw('classes', '[' + S + ']') + '}'; Exit; End;-    If Name = '' Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','bad_args') + ',' + JStr('error','name is required') + '}'; Exit; End;-    PCBServer.PreProcess;-    If Old <> Nil Then Board.RemovePCBObject(Old);-    If Op = 'remove' Then-    Begin-        PCBServer.PostProcess;-        Result := '{' + JRaw('ok','true') + ',' + JStr('class', Name) + ',' + JRaw('removed', JBool(Old <> Nil)) + '}';-        Exit;-    End;-    Cls := PCBServer.PCBClassFactoryByClassMember(eClassMemberKind_Net);-    Cls.Name := Name;-    Nets := Param(Lines, 'nets'); K := 0; Missing := '';-    While Nets <> '' Do-    Begin-        P := Pos(';', Nets);-        If P = 0 Then Begin N := Nets; Nets := ''; End-        Else Begin N := Copy(Nets, 1, P - 1); Nets := Copy(Nets, P + 1, Length(Nets)); End;-        If N <> '' Then-        Begin-            Net := NetObjNamed(Board, N);-            If Net = Nil Then Begin If Missing <> '' Then Missing := Missing + ','; Missing := Missing + '"' + JsonEsc(N) + '"'; End-            Else Begin Cls.AddMemberByName(Net.Name); K := K + 1; End;-        End;-    End;-    Board.AddPCBObject(Cls);-    PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Cls.I_ObjectAddress);-    PCBServer.PostProcess;-    Result := '{' + JRaw('ok','true') + ',' + JStr('class', Name) + ',' + JRaw('members', IntToStr(K)) + ',' + JRaw('replaced', JBool(Old <> Nil)) + ',' + JRaw('notOnBoard', '[' + Missing + ']') + '}';-End;--{ diff_pairs: op=list | set (name, pos, neg: board nets; replaces a pair of that name) | remove (name) }-Function CmdDiffPairs(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    BIt   : IPCB_BoardIterator;-    DP, Old : IPCB_DifferentialPair;-    PN, NN : IPCB_Net;-    Op, Name, S, PS, NS : String;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_board') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    Op := Param(Lines, 'op'); Name := Param(Lines, 'name');-    Old := Nil; S := '';-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eDifferentialPairObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    DP := BIt.FirstPCBObject;-    While DP <> Nil Do-    Begin-        If (Name <> '') And (UpperCase(DP.Name) = UpperCase(Name)) Then Old := DP;-        PS := ''; NS := '';-        If DP.PositiveNet <> Nil Then PS := DP.PositiveNet.Name;-        If DP.NegativeNet <> Nil Then NS := DP.NegativeNet.Name;-        If S <> '' Then S := S + ',';-        S := S + '{' + JStr('name', DP.Name) + ',' + JStr('pos', PS) + ',' + JStr('neg', NS) + '}';-        DP := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-    If Op = 'list' Then Begin Result := '{' + JRaw('ok','true') + ',' + JRaw('pairs', '[' + S + ']') + '}'; Exit; End;-    If Name = '' Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','bad_args') + ',' + JStr('error','name is required') + '}'; Exit; End;-    If Op = 'set' Then-    Begin-        PN := NetObjNamed(Board, Param(Lines, 'pos'));-        NN := NetObjNamed(Board, Param(Lines, 'neg'));-        If (PN = Nil) Or (NN = Nil) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','net_not_on_board') + ',' + JRaw('posFound', JBool(PN <> Nil)) + ',' + JRaw('negFound', JBool(NN <> Nil)) + '}'; Exit; End;-    End;-    PCBServer.PreProcess;-    If Old <> Nil Then Board.RemovePCBObject(Old);-    If Op = 'remove' Then-    Begin-        PCBServer.PostProcess;-        Result := '{' + JRaw('ok','true') + ',' + JStr('pair', Name) + ',' + JRaw('removed', JBool(Old <> Nil)) + '}';-        Exit;-    End;-    DP := PCBServer.PCBObjectFactory(eDifferentialPairObject, eNoDimension, eCreate_Default);-    DP.Name := Name;-    DP.PositiveNet := PN;-    DP.NegativeNet := NN;-    Board.AddPCBObject(DP);-    PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, DP.I_ObjectAddress);-    PCBServer.PostProcess;-    Result := '{' + JRaw('ok','true') + ',' + JStr('pair', Name) + ',' + JStr('pos', PN.Name) + ',' + JStr('neg', NN.Name) + ',' + JRaw('replaced', JBool(Old <> Nil)) + '}';-End;--{ footprint_primitives: designator=<board component> -> geometry dump (tracks, arcs, pads, texts, others counted) }-Function CmdFootprintPrimitives(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    Comp  : IPCB_Component;-    GIt   : IPCB_GroupIterator;-    Prim  : IPCB_Primitive;-    Trk   : IPCB_Track;-    Arc   : IPCB_Arc;-    Pad   : IPCB_Pad;-    Txt   : IPCB_Text;-    Des, S, Item : String;-    K, Other : Integer;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    Des := Param(Lines, 'designator');-    If Des = '' Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','designator is required') + '}'; Exit; End;-    Comp := FindCompByDes(Board, Des);-    If Comp = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','designator not found: ' + Des) + '}'; Exit; End;-    S := ''; K := 0; Other := 0;-    GIt := Comp.GroupIterator_Create;-    GIt.AddFilter_LayerSet(AllLayers);-    Prim := GIt.FirstPCBObject;-    While (Prim <> Nil) And (K < 2000) Do-    Begin-        Item := '';-        If Prim.ObjectId = eTrackObject Then-        Begin-            Trk := Prim;-            Item := '{' + JStr('kind','track') + ',' + JRaw('layerId', IntToStr(Trk.Layer)) + ',' +-                JRaw('x1', IntToStr(Round(CoordToMils(Trk.x1)))) + ',' + JRaw('y1', IntToStr(Round(CoordToMils(Trk.y1)))) + ',' +-                JRaw('x2', IntToStr(Round(CoordToMils(Trk.x2)))) + ',' + JRaw('y2', IntToStr(Round(CoordToMils(Trk.y2)))) + ',' +-                JRaw('wMil', IntToStr(Round(CoordToMils(Trk.Width)))) + '}';-        End-        Else If Prim.ObjectId = eArcObject Then-        Begin-            Arc := Prim;-            Item := '{' + JStr('kind','arc') + ',' + JRaw('layerId', IntToStr(Arc.Layer)) + ',' +-                JRaw('cx', IntToStr(Round(CoordToMils(Arc.XCenter)))) + ',' + JRaw('cy', IntToStr(Round(CoordToMils(Arc.YCenter)))) + ',' +-                JRaw('rMil', IntToStr(Round(CoordToMils(Arc.Radius)))) + ',' + JRaw('start', IntToStr(Round(Arc.StartAngle))) + ',' +-                JRaw('end', IntToStr(Round(Arc.EndAngle))) + ',' + JRaw('wMil', IntToStr(Round(CoordToMils(Arc.LineWidth)))) + '}';-        End-        Else If Prim.ObjectId = ePadObject Then-        Begin-            Pad := Prim;-            Item := '{' + JStr('kind','pad') + ',' + JStr('name', Pad.Name) + ',' + JRaw('layerId', IntToStr(Pad.Layer)) + ',' +-                JRaw('x', IntToStr(Round(CoordToMils(Pad.x)))) + ',' + JRaw('y', IntToStr(Round(CoordToMils(Pad.y)))) + ',' +-                JRaw('wMil', IntToStr(Round(CoordToMils(Pad.TopXSize)))) + ',' + JRaw('hMil', IntToStr(Round(CoordToMils(Pad.TopYSize)))) + ',' +-                JRaw('holeMil', IntToStr(Round(CoordToMils(Pad.HoleSize)))) + ',' +-                { micrometres as integers: FloatToStr writes a comma decimal on some Windows locales, which breaks JSON }-                JRaw('solderMaskExpUm', IntToStr(Round(CoordToMils(Pad.SolderMaskExpansion) * 25.4))) + ',' +-                JRaw('pasteMaskExpUm', IntToStr(Round(CoordToMils(Pad.PasteMaskExpansion) * 25.4))) + '}';-        End-        Else If Prim.ObjectId = eTextObject Then-        Begin-            Txt := Prim;-            Item := '{' + JStr('kind','text') + ',' + JRaw('layerId', IntToStr(Txt.Layer)) + ',' + JStr('text', Txt.Text) + ',' +-                JRaw('x', IntToStr(Round(CoordToMils(Txt.XLocation)))) + ',' + JRaw('y', IntToStr(Round(CoordToMils(Txt.YLocation)))) + '}';-        End-        Else Other := Other + 1;-        If Item <> '' Then-        Begin-            If S <> '' Then S := S + ',';-            S := S + Item;-            K := K + 1;-        End;-        Prim := GIt.NextPCBObject;-    End;-    Comp.GroupIterator_Destroy(GIt);-    Result := '{' + JRaw('ok','true') + ',' + JStr('designator', Comp.Name.Text) + ',' + JStr('pattern', Comp.Pattern) + ',' +-        JRaw('count', IntToStr(K)) + ',' + JRaw('otherPrimitives', IntToStr(Other)) + ',' + JRaw('primitives', '[' + S + ']') + '}';-End;--{ I/O bench: where does the ~300 ms per file write go? Times TStringList.SaveToFile against-  several paths (workspace / %TEMP% / Documents), overwrite vs new, and rename/delete. Uses only-  routines the agent already relies on, so it cannot introduce a new undeclared identifier. }-Function BenchOne(Path : String; Content : String) : String;-Var-    L : TStringList;-    T0, TNew, TOver, TDel : Integer;-Begin-    L := TStringList.Create;-    Try-        L.Text := Content;-        If FileExists(Path) Then DeleteFile(Path);-        T0 := GetTickCount; L.SaveToFile(Path); TNew := GetTickCount - T0;-        T0 := GetTickCount; L.SaveToFile(Path); TOver := GetTickCount - T0;-        T0 := GetTickCount; DeleteFile(Path); TDel := GetTickCount - T0;-    Finally-        L.Free;-    End;-    Result := '{' + JStr('path', Path) + ',' + JRaw('newMs', IntToStr(TNew)) + ',' + JRaw('overwriteMs', IntToStr(TOver)) + ',' + JRaw('deleteMs', IntToStr(TDel)) + '}';-End;--Function CmdIoBench(Lines : TStringList) : String;-Var-    Big, Small, TmpDir, Docs : String;-    K, T0, TRen, TSleep, TLoop, X : Integer;-    L : TStringList;-Begin-    Small := 'payload 0123456789 abcdefghij';-    Big := '';-    For K := 1 To 200 Do Big := Big + 'line ' + IntToStr(K) + ' 0123456789 abcdefghij 0123456789 abcdefghij' + #13#10;-    { derive %TEMP% and Documents from WORKSPACE (…\AppData\Local\Adom Bridge\…) - no new identifiers }-    TmpDir := Copy(WORKSPACE, 1, Pos('\Adom Bridge\', WORKSPACE)) + 'Temp';-    Docs := Copy(WORKSPACE, 1, Pos('\AppData\', WORKSPACE)) + 'Documents';-    { rename cost on an existing small file }-    L := TStringList.Create;-    Try-        L.Text := Small;-        L.SaveToFile(WORKSPACE + 'bench_ren.tmp');-    Finally-        L.Free;-    End;-    T0 := GetTickCount; RenameFile(WORKSPACE + 'bench_ren.tmp', WORKSPACE + 'bench_ren.txt'); TRen := GetTickCount - T0;-    DeleteFile(WORKSPACE + 'bench_ren.txt');-    T0 := GetTickCount; Sleep(1); TSleep := GetTickCount - T0;-    T0 := GetTickCount; For K := 1 To 1000 Do X := K * 2; TLoop := GetTickCount - T0;-    Result := '{' + JRaw('ok', 'true')-        + ',' + JRaw('wsSmall', BenchOne(WORKSPACE + 'bench_a.txt', Small))-        + ',' + JRaw('wsSmallTmpExt', BenchOne(WORKSPACE + 'bench_b.tmp', Small))-        + ',' + JRaw('wsBig', BenchOne(WORKSPACE + 'bench_c.json', Big))-        + ',' + JRaw('tempSmall', BenchOne(TmpDir + '\adom_bench_d.txt', Small))-        + ',' + JRaw('docsSmall', BenchOne(Docs + '\adom_bench_e.txt', Small))-        + ',' + JRaw('renameMs', IntToStr(TRen))-        + ',' + JRaw('sleep1Ms', IntToStr(TSleep))-        + ',' + JRaw('loop1000Ms', IntToStr(TLoop))-        + '}';-End;--{ Which copper layers actually CARRY copper. The layer stack's "is this layer in the stack" flag has-  no identifier we have proven on this Altium build, and guessing one halts the engine behind a modal-  on the user's screen — so instead of asking the stack, count primitives. ONE pass over pads, tracks-  and arcs across all layers, tallied by Prim.Layer; every identifier here is already used elsewhere-  in this unit. An empty inner plane reports zero, which is why the caller must read this as-  "layers with copper on them", never as "the board's layer count". }-Function CmdLayerOccupancy(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    BIt   : IPCB_BoardIterator;-    Prim  : IPCB_Primitive;-    SL    : TStringList;-    S, K  : String;-    I, C, N : Integer;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}';-        Exit;-    End;-    SL := TStringList.Create;-    N := 0;-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(ePadObject, eTrackObject, eArcObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Prim := BIt.FirstPCBObject;-    While Prim <> Nil Do-    Begin-        K := IntToStr(Prim.Layer);-        C := StrToIntDef(SL.Values[K], 0);-        SL.Values[K] := IntToStr(C + 1);-        N := N + 1;-        Prim := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-    S := '';-    For I := 0 To SL.Count - 1 Do-    Begin-        If S <> '' Then S := S + ',';-        K := SL.Names[I];-        S := S + '{' + JRaw('layerId', K) + ',' +-            JStr('name', Board.LayerStack.LayerObject[StrToIntDef(K, 1)].Name) + ',' +-            JRaw('primitives', SL.Values[K]) + '}';-    End;-    SL.Free;-    Result := '{' + JRaw('ok','true') + ',' + JRaw('primitives', IntToStr(N)) + ',' + JRaw('occupancy', '[' + S + ']') + '}';-End;--{ The schematic-side companion to read_board: ONE round trip for the current sheet. Composed from-  the existing reader (same rule — no new Altium API surface, so it cannot introduce an-  engine-halting identifier). A hierarchical PARENT sheet holds sheet symbols, not parts, so this-  reports what the CURRENT sheet contains; the caller activates a leaf sheet first. }-Function CmdReadSchematic(Lines : TStringList) : String;-Var-    Doc   : ISch_Document;-    Sects : String;-    S     : String;-Begin-    Doc := SchServer.GetCurrentSchDocument;-    If Doc = Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current schematic document (open a .SchDoc first)') + '}';-        Exit;-    End;-    Sects := Param(Lines, 'sections');-    If Sects = '' Then Sects := 'components';-    S := '';-    If Pos('components', Sects) > 0 Then S := S + ',' + JRaw('components', CmdSchematicData(Lines));-    Result := '{' + JRaw('ok','true') + ',' + JStr('document', Doc.DocumentName) + ',' + JStr('sections', Sects) + S + '}';-End;--{ The board's extent, for the checks that need to know where the edge is. Uses the SAME guarded-  pattern the library reader already relies on: a library's internal board has a NIL BoardOutline and-  touching BoundingRectangle on it halts the engine (measured 2026-08-30). Reports which source it-  used, because the outline's bounding box and the board's bounding box are not the same claim. }-Function CmdReadOutline(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    R     : TCoordRect;-    Src   : String;-    Seg   : TPolySegment;-    K, L, B, RR, T : Integer;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}';-        Exit;-    End;-    If Board.BoardOutline <> Nil Then-    Begin-        { vertices, not the cached BoundingRectangle (see OutlineMilFromVertices) }-        L := 0; B := 0; RR := 0; T := 0;-        For K := 0 To Board.BoardOutline.PointCount - 1 Do-        Begin-            Seg := Board.BoardOutline.Segments[K];-            If (K = 0) Or (Seg.vx < L) Then L := Seg.vx;-            If (K = 0) Or (Seg.vy < B) Then B := Seg.vy;-            If (K = 0) Or (Seg.vx > RR) Then RR := Seg.vx;-            If (K = 0) Or (Seg.vy > T) Then T := Seg.vy;-        End;-        Src := 'boardOutline';-    End-    Else-    Begin-        R := Board.BoundingRectangle;-        L := R.Left; B := R.Bottom; RR := R.Right; T := R.Top;-        Src := 'boardBounds';-    End;-    Result := '{' + JRaw('ok','true') + ',' + JStr('source', Src) + ',' +-        JRaw('leftMil', IntToStr(Round(CoordToMils(L)))) + ',' +-        JRaw('bottomMil', IntToStr(Round(CoordToMils(B)))) + ',' +-        JRaw('rightMil', IntToStr(Round(CoordToMils(RR)))) + ',' +-        JRaw('topMil', IntToStr(Round(CoordToMils(T)))) + ',' +-        JRaw('widthMil', IntToStr(Round(CoordToMils(RR - L)))) + ',' +-        JRaw('heightMil', IntToStr(Round(CoordToMils(T - B)))) + '}';-End;--{ ONE round trip to READ the whole board. Composed from the EXISTING readers on purpose: no new Altium-  API surface, so it cannot introduce an undeclared identifier (which halts the engine behind a-  modal). The bridge shapes these into typed views; recorded dumps are the unit-test fixtures. }-Function CmdReadBoard(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    Sects, S : String;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}';-        Exit;-    End;-    Sects := Param(Lines, 'sections');-    If Sects = '' Then Sects := 'components,nets,layers,copper,rules,occupancy,outline';-    S := '';-    If Pos('components', Sects) > 0 Then S := S + ',' + JRaw('components', CmdComponentData(Lines));-    If Pos('nets', Sects) > 0 Then S := S + ',' + JRaw('nets', CmdListNets(Lines));-    If Pos('layers', Sects) > 0 Then S := S + ',' + JRaw('layers', CmdLayers(Lines));-    If Pos('copper', Sects) > 0 Then S := S + ',' + JRaw('copper', CmdCopperLayers(Lines));-    If Pos('rules', Sects) > 0 Then S := S + ',' + JRaw('rules', CmdReadRules(Lines));-    If Pos('occupancy', Sects) > 0 Then S := S + ',' + JRaw('occupancy', CmdLayerOccupancy(Lines));-    If Pos('outline', Sects) > 0 Then S := S + ',' + JRaw('outline', CmdReadOutline(Lines));-    Result := '{' + JRaw('ok','true') + ',' + JStr('board', Board.FileName) + ',' + JStr('sections', Sects) + S + '}';-End;--{ routing_state: the whole live board as data for AI Flow's Altium lane. Every coordinate is the-  RAW internal unit (an integer; 10000 per mil), so nothing is rounded and no decimal is-  formatted in the user's locale. Rotations and angles are in thousandths of a degree. The-  bridge converts to mm and hashes the dump into a revision. Arrays, not objects, keep a board-  with thousands of tracks small:-    tracks   [x1,y1,x2,y2,width,layer,"net"]-    arcs     [cx,cy,radius,startMilliDeg,endMilliDeg,width,layer,"net"]-    vias     [x,y,size,hole,lowLayer,highLayer,"net"]-    pads     ["comp","name",x,y,xSize,ySize,rotMilliDeg,layer,hole,"net",shape]-    comps    ["designator",x,y,rotMilliDeg,layer,"pattern",bodies]-    polys    ["net",layer,[[x,y],...]]-    outline  [[x,y,kind,cx,cy,radius,angle1MilliDeg,angle2MilliDeg],...]  kind 1 = an arc to the next vertex-    unrouted the ratsnest connection count (connection objects on the board) }-{ The list as a JSON array. S := S + item copied the whole growing string for every item: quadratic, about 70 s for-  the Bluetooth Sentinel's 9381 tracks (2026-10-29). Each item takes its own comma and TStringList joins them in one-  pass (its line breaks are JSON whitespace). The list is spent: every caller frees it after. }-Function JoinList(L : TStringList) : String;-Var-    I : Integer;-Begin-    For I := 0 To L.Count - 2 Do L[I] := L[I] + ',';-    Result := '[' + L.Text + ']';-End;--Function NetOf(Prim : IPCB_Primitive) : String;-Begin-    Result := '""';-    If Prim.Net <> Nil Then Result := '"' + JsonEsc(Prim.Net.Name) + '"';-End;--{ A polygon's outline vertices for the board model: [x,y,0] for a line segment, [x,y,1,cx,cy,r,a1*1000,a2*1000] for an-  arc segment, as the board outline sends them. Only the vertices went before, so a round pour came back as 3 or 4-  corners (the Bluetooth Sentinel's GND pours, 2026-10-28). }-Function PolySegPts(Poly : IPCB_Polygon) : String;-Var-    Seg : TPolySegment;-    K   : Integer;-Begin-    Result := '';-    For K := 0 To Poly.PointCount - 1 Do-    Begin-        Seg := Poly.Segments[K];-        If K > 0 Then Result := Result + ',';-        If Seg.Kind = ePolySegmentArc Then-            Result := Result + '[' + IntToStr(Seg.vx) + ',' + IntToStr(Seg.vy) + ',1,' + IntToStr(Seg.cx) + ',' + IntToStr(Seg.cy) + ',' +-                IntToStr(Seg.Radius) + ',' + IntToStr(Round(Seg.Angle1 * 1000)) + ',' + IntToStr(Round(Seg.Angle2 * 1000)) + ']'-        Else-            Result := Result + '[' + IntToStr(Seg.vx) + ',' + IntToStr(Seg.vy) + ',0]';-    End;-End;--Function CmdRoutingState(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    BIt   : IPCB_BoardIterator;-    Prim  : IPCB_Primitive;-    Trk   : IPCB_Track;-    Arc   : IPCB_Arc;-    Via   : IPCB_Via;-    Pad   : IPCB_Pad;-    Comp  : IPCB_Component;-    Poly  : IPCB_Polygon;-    BO    : IPCB_BoardOutline;-    Seg   : TPolySegment;-    LT, LA, LV, LP, LC, LG, LO : TStringList;-    Own, Pts : String;-    K, Unrouted : Integer;-    PR    : TCoordRect;-    PX, PY, PW, PH, PRot, PS : Integer;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    LT := TStringList.Create; LA := TStringList.Create; LV := TStringList.Create; LP := TStringList.Create;-    LC := TStringList.Create; LG := TStringList.Create; LO := TStringList.Create;-    Unrouted := 0;-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eTrackObject, eArcObject, eViaObject, ePadObject, eComponentObject, ePolyObject, eConnectionObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Prim := BIt.FirstPCBObject;-    While Prim <> Nil Do-    Begin-        If Prim.ObjectId = eTrackObject Then-        Begin-            Trk := Prim;-            If Not Trk.InComponent Then-                LT.Add('[' + IntToStr(Trk.x1) + ',' + IntToStr(Trk.y1) + ',' + IntToStr(Trk.x2) + ',' + IntToStr(Trk.y2) + ',' +-                    IntToStr(Trk.Width) + ',' + IntToStr(Trk.Layer) + ',' + NetOf(Trk) + ']');-        End-        Else If Prim.ObjectId = eArcObject Then-        Begin-            Arc := Prim;-            If Not Arc.InComponent Then-                LA.Add('[' + IntToStr(Arc.XCenter) + ',' + IntToStr(Arc.YCenter) + ',' + IntToStr(Arc.Radius) + ',' +-                    IntToStr(Round(Arc.StartAngle * 1000)) + ',' + IntToStr(Round(Arc.EndAngle * 1000)) + ',' +-                    IntToStr(Arc.LineWidth) + ',' + IntToStr(Arc.Layer) + ',' + NetOf(Arc) + ']');-        End-        Else If Prim.ObjectId = eViaObject Then-        Begin-            Via := Prim;-            LV.Add('[' + IntToStr(Via.x) + ',' + IntToStr(Via.y) + ',' + IntToStr(Via.Size) + ',' + IntToStr(Via.HoleSize) + ',' +-                IntToStr(Via.LowLayer) + ',' + IntToStr(Via.HighLayer) + ',' + NetOf(Via) + ']');-        End-        Else If Prim.ObjectId = ePadObject Then-        Begin-            Pad := Prim;-            Own := '';-            If Pad.Component <> Nil Then Own := Pad.Component.Name.Text;-            PX := Pad.x; PY := Pad.y; PW := Pad.TopXSize; PH := Pad.TopYSize; PRot := Round(Pad.Rotation * 1000); PS := Pad.TopShape;-            If (PW = 0) Or (PH = 0) Then-            Begin-                { a custom-shape pad has no size: its copper is its bounding rectangle (the Bluetooth Sentinel coin cell-                  holder's 5 mm contacts read 0 x 0, and a via fence put a via on one, 2026-10-23) }-                PR := Pad.BoundingRectangle;-                PX := (PR.Left + PR.Right) Div 2; PY := (PR.Bottom + PR.Top) Div 2;-                PW := PR.Right - PR.Left; PH := PR.Top - PR.Bottom; PRot := 0; PS := eRectangular;-            End;-            LP.Add('["' + JsonEsc(Own) + '","' + JsonEsc(Pad.Name) + '",' + IntToStr(PX) + ',' + IntToStr(PY) + ',' +-                IntToStr(PW) + ',' + IntToStr(PH) + ',' + IntToStr(PRot) + ',' +-                IntToStr(Pad.Layer) + ',' + IntToStr(Pad.HoleSize) + ',' + NetOf(Pad) + ',' + IntToStr(PS) + ']');-        End-        Else If Prim.ObjectId = eComponentObject Then-        Begin-            Comp := Prim;-            Own := '';-            Try Own := Comp.SourceFootprintLibrary; Except Own := ''; End;-            LC.Add('["' + JsonEsc(Comp.Name.Text) + '",' + IntToStr(Comp.x) + ',' + IntToStr(Comp.y) + ',' +-                IntToStr(Round(Comp.Rotation * 1000)) + ',' + IntToStr(Comp.Layer) + ',"' + JsonEsc(Comp.Pattern) + '",' +-                IntToStr(GroupCount(Comp, eComponentBodyObject)) + ',"' + JsonEsc(Own) + '"]');-        End-        Else If Prim.ObjectId = ePolyObject Then-        Begin-            Poly := Prim;-            LG.Add('[' + NetOf(Poly) + ',' + IntToStr(Poly.Layer) + ',[' + PolySegPts(Poly) + ']]');-        End-        Else If Prim.ObjectId = eConnectionObject Then-            Unrouted := Unrouted + 1;-        Prim := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-    BO := Board.BoardOutline;-    If BO <> Nil Then-        For K := 0 To BO.PointCount - 1 Do-        Begin-            Seg := BO.Segments[K];-            If Seg.Kind = ePolySegmentArc Then-                LO.Add('[' + IntToStr(Seg.vx) + ',' + IntToStr(Seg.vy) + ',1,' + IntToStr(Seg.cx) + ',' + IntToStr(Seg.cy) + ',' +-                    IntToStr(Seg.Radius) + ',' + IntToStr(Round(Seg.Angle1 * 1000)) + ',' + IntToStr(Round(Seg.Angle2 * 1000)) + ']')-            Else-                LO.Add('[' + IntToStr(Seg.vx) + ',' + IntToStr(Seg.vy) + ',0]');-        End;-    Result := '{' + JRaw('ok','true') + ',' + JStr('file', Board.FileName) + ',' + JRaw('coordPerMil','10000') + ',' +-        JRaw('tracks', JoinList(LT)) + ',' + JRaw('arcs', JoinList(LA)) + ',' + JRaw('vias', JoinList(LV)) + ',' +-        JRaw('pads', JoinList(LP)) + ',' + JRaw('comps', JoinList(LC)) + ',' + JRaw('polys', JoinList(LG)) + ',' +-        JRaw('outline', JoinList(LO)) + ',' + JRaw('unrouted', IntToStr(Unrouted)) + '}';-    LT.Free; LA.Free; LV.Free; LP.Free; LC.Free; LG.Free; LO.Free;-End;--{ edit_board: a batch of copper and placement edits in ONE PreProcess/PostProcess (one undo-  step), for AI Flow's Altium lane. The payload is one op per line, '|'-separated, coordinates-  in RAW internal units (10000 per mil), rotation in thousandths of a degree:-    T|x1|y1|x2|y2|width|layer|net         add a track-    V|x|y|size|hole|lowLayer|highLayer|net add a via-    M|ref|x|y|rotMilliDeg                 move + rotate a placed component-    D|T|x1|y1|x2|y2|layer                 delete the free track with these ends (either direction)-    D|V|x|y                               delete the via at x,y-    N|net                                 rip up: delete every free track, arc and via on the net-  Each op answers in results[] in order: "ok", "ok:<n>" (deleted count) or "err:<why>". A-  failed op does not stop the batch; the bridge reads the board back and reports what landed. }-Function FindTrack(Board : IPCB_Board; X1, Y1, X2, Y2, Lay : Integer) : IPCB_Track;-Var-    BIt : IPCB_BoardIterator;-    Trk : IPCB_Track;-Begin-    Result := Nil;-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eTrackObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Trk := BIt.FirstPCBObject;-    While Trk <> Nil Do-    Begin-        { within 50 units (1.3 um): routing_state rounds to 0.1 um, about 39 units, so an off-grid track never matched-          exactly (a board outline on Mechanical 1 could not be removed, 2026-10-23) }-        If (Not Trk.InComponent) And (Trk.Layer = Lay) And-           (((Abs(Trk.x1 - X1) <= 50) And (Abs(Trk.y1 - Y1) <= 50) And (Abs(Trk.x2 - X2) <= 50) And (Abs(Trk.y2 - Y2) <= 50)) Or-            ((Abs(Trk.x1 - X2) <= 50) And (Abs(Trk.y1 - Y2) <= 50) And (Abs(Trk.x2 - X1) <= 50) And (Abs(Trk.y2 - Y1) <= 50))) Then-        Begin-            Result := Trk; Break;-        End;-        Trk := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-End;--Function FindViaAt(Board : IPCB_Board; X, Y : Integer) : IPCB_Via;-Var-    BIt : IPCB_BoardIterator;-    Via : IPCB_Via;-Begin-    Result := Nil;-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eViaObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Via := BIt.FirstPCBObject;-    While Via <> Nil Do-    Begin-        { within 50 units: routing_state's mm are rounded to 0.1 um (see FindTrack) }-        If (Abs(Via.x - X) <= 50) And (Abs(Via.y - Y) <= 50) Then Begin Result := Via; Break; End;-        Via := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-End;--{ The first free track, arc or via on the net, or Nil. }-Function FirstOnNet(Board : IPCB_Board; NetName : String) : IPCB_Primitive;-Var-    BIt  : IPCB_BoardIterator;-    Prim : IPCB_Primitive;-Begin-    Result := Nil;-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eTrackObject, eArcObject, eViaObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Prim := BIt.FirstPCBObject;-    While Prim <> Nil Do-    Begin-        If (Not Prim.InComponent) And (Prim.Net <> Nil) Then-            If Prim.Net.Name = NetName Then Begin Result := Prim; Break; End;-        Prim := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-End;--Procedure RegisterPrim(Board : IPCB_Board; Prim : IPCB_Primitive);-Begin-    Board.AddPCBObject(Prim);-    PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Prim.I_ObjectAddress);-End;--{ the nets a part's pads carry, ; separated }-Function PartNetList(Comp : IPCB_Component) : String;-Var-    GIt : IPCB_GroupIterator;-    Pd  : IPCB_Pad;-Begin-    Result := '';-    GIt := Comp.GroupIterator_Create;-    GIt.AddFilter_ObjectSet(MkSet(ePadObject));-    Pd := GIt.FirstPCBObject;-    While Pd <> Nil Do-    Begin-        If Pd.Net <> Nil Then-            If Pos(';' + Pd.Net.Name + ';', ';' + Result + ';') = 0 Then-            Begin-                If Result <> '' Then Result := Result + ';';-                Result := Result + Pd.Net.Name;-            End;-        Pd := GIt.NextPCBObject;-    End;-    Comp.GroupIterator_Destroy(GIt);-End;--{ A board graphic's key: kind, layer and raw geometry (Altium gives free primitives no stable id). Board-level-  graphics only: not in a part, not on a net. }-Function GraphicKey(Prim : IPCB_Primitive) : String;-Begin-    Result := '';-    If Prim.ObjectId = eTrackObject Then Result := 'line:' + IntToStr(Prim.Layer) + ':' + IntToStr(Prim.X1) + ',' + IntToStr(Prim.Y1) + ',' + IntToStr(Prim.X2) + ',' + IntToStr(Prim.Y2) + ',' + IntToStr(Prim.Width)-    Else If Prim.ObjectId = eArcObject Then Result := 'arc:' + IntToStr(Prim.Layer) + ':' + IntToStr(Prim.XCenter) + ',' + IntToStr(Prim.YCenter) + ',' + IntToStr(Prim.Radius) + ',' + IntToStr(Round(Prim.StartAngle * 1000)) + ',' + IntToStr(Round(Prim.EndAngle * 1000)) + ',' + IntToStr(Prim.LineWidth)-    Else If Prim.ObjectId = eTextObject Then Result := 'text:' + IntToStr(Prim.Layer) + ':' + IntToStr(Prim.XLocation) + ',' + IntToStr(Prim.YLocation) + ':' + Prim.Text-    Else If Prim.ObjectId = eFillObject Then Result := 'rect:' + IntToStr(Prim.Layer) + ':' + IntToStr(Prim.X1Location) + ',' + IntToStr(Prim.Y1Location) + ',' + IntToStr(Prim.X2Location) + ',' + IntToStr(Prim.Y2Location)-    Else If Prim.ObjectId = eRegionObject Then Result := 'polygon:' + IntToStr(Prim.Layer) + ':' + IntToStr(Prim.BoundingRectangle.Left) + ',' + IntToStr(Prim.BoundingRectangle.Bottom) + ',' + IntToStr(Prim.BoundingRectangle.Right) + ',' + IntToStr(Prim.BoundingRectangle.Top)-    Else If Prim.ObjectId = eDimensionObject Then Result := 'dimension:' + IntToStr(Prim.Layer) + ':' + IntToStr(Prim.BoundingRectangle.Left) + ',' + IntToStr(Prim.BoundingRectangle.Bottom) + ',' + IntToStr(Prim.BoundingRectangle.Right) + ',' + IntToStr(Prim.BoundingRectangle.Top);-End;--Function IsBoardGraphic(Prim : IPCB_Primitive) : Boolean;-Begin-    Result := (Not Prim.InComponent) And (Prim.InPolygon = False);-    If Result And (Prim.ObjectId <> eTextObject) And (Prim.ObjectId <> eDimensionObject) Then-        Result := (Prim.Net = Nil) And (Not ((Prim.Layer >= eTopLayer) And (Prim.Layer <= eBottomLayer)));-End;--Function GraphicByKey(Board : IPCB_Board; Key : String) : IPCB_Primitive;-Var-    BIt  : IPCB_BoardIterator;-    Prim : IPCB_Primitive;-Begin-    Result := Nil;-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eTrackObject, eArcObject, eTextObject, eFillObject, eRegionObject, eDimensionObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Prim := BIt.FirstPCBObject;-    While Prim <> Nil Do-    Begin-        If IsBoardGraphic(Prim) And (GraphicKey(Prim) = Key) Then Begin Result := Prim; Break; End;-        Prim := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-End;--{ board_graphics: every board-level graphic (lines, arcs, texts, rectangles, regions, dimensions not in a part and-  not on a net) with its key; raw units. }-Function CmdBoardGraphics(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    BIt   : IPCB_BoardIterator;-    Prim  : IPCB_Primitive;-    S     : String;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_board') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    S := '';-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eTrackObject, eArcObject, eTextObject, eFillObject, eRegionObject, eDimensionObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Prim := BIt.FirstPCBObject;-    While Prim <> Nil Do-    Begin-        If IsBoardGraphic(Prim) Then-        Begin-            If S <> '' Then S := S + ',';-            S := S + '"' + JsonEsc(GraphicKey(Prim)) + '"';-        End;-        Prim := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-    Result := '{' + JRaw('ok','true') + ',' + JRaw('k', IntToStr(MilsToCoord(1000))) + ',' + JRaw('ox', IntToStr(Board.XOrigin)) + ',' + JRaw('oy', IntToStr(Board.YOrigin)) + ',' + JRaw('graphics', '[' + S + ']') + '}';-End;---Function CmdEditBoard(Lines : TStringList; Payload : String) : String;-Var-    Board : IPCB_Board;-    Ops   : TStringList;-    Op, Kind, Res, Results : String;-    Trk   : IPCB_Track;-    Via   : IPCB_Via;-    Pad   : IPCB_Pad;-    PC    : TPadCache;-    Txt   : IPCB_Text;-    Fl    : IPCB_Fill;-    Comp  : IPCB_Component;-    Prim  : IPCB_Primitive;-    Net   : IPCB_Net;-    Reg   : IPCB_Region;-    Cont  : IPCB_Contour;-    PtList, Pt : String;-    I, N, Okay, Failed : Integer;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    Ops := TStringList.Create;-    Ops.Text := Payload;-    Results := ''; Okay := 0; Failed := 0;-    PCBServer.PreProcess;-    For I := 0 To Ops.Count - 1 Do-    Begin-        Op := Trim(Ops[I]);-        If Op = '' Then Continue;-        Kind := FieldP(Op, 1);-        Res := 'ok';-        gInputError := '';-        If Kind = 'T' Then-        Begin-            Net := FindNet(Board, FieldP(Op, 8));-            If (Net = Nil) And (FieldP(Op, 8) <> '') Then Res := 'err:no net ' + FieldP(Op, 8)-            Else-            Begin-                Trk := PCBServer.PCBObjectFactory(eTrackObject, eNoDimension, eCreate_Default);-                Trk.Layer := AdomInt(FieldP(Op, 7));-                Trk.X1 := AdomInt(FieldP(Op, 2)); Trk.Y1 := AdomInt(FieldP(Op, 3));-                Trk.X2 := AdomInt(FieldP(Op, 4)); Trk.Y2 := AdomInt(FieldP(Op, 5));-                Trk.Width := AdomInt(FieldP(Op, 6));-                If Net <> Nil Then Trk.Net := Net;-                If gInputError <> '' Then Res := 'err:bad number ' + gInputError Else RegisterPrim(Board, Trk);-            End;-        End-        Else If Kind = 'V' Then-        Begin-            Net := FindNet(Board, FieldP(Op, 8));-            If (Net = Nil) And (FieldP(Op, 8) <> '') Then Res := 'err:no net ' + FieldP(Op, 8)-            Else-            Begin-                Via := PCBServer.PCBObjectFactory(eViaObject, eNoDimension, eCreate_Default);-                Via.x := AdomInt(FieldP(Op, 2)); Via.y := AdomInt(FieldP(Op, 3));-                Via.Size := AdomInt(FieldP(Op, 4)); Via.HoleSize := AdomInt(FieldP(Op, 5));-                Via.LowLayer := AdomInt(FieldP(Op, 6)); Via.HighLayer := AdomInt(FieldP(Op, 7));-                If Net <> Nil Then Via.Net := Net;-                If gInputError <> '' Then Res := 'err:bad number ' + gInputError Else RegisterPrim(Board, Via);-            End;-        End-        Else If Kind = 'P' Then-        Begin-            { a free pad: P|x|y|size|hole|plated|layer|net|name|maskExpansion. Layer 74 = multi-layer (a mounting-              hole); a hole of 0 on top or bottom is a fiducial. }-            Net := FindNet(Board, FieldP(Op, 8));-            If (Net = Nil) And (FieldP(Op, 8) <> '') Then Res := 'err:no net ' + FieldP(Op, 8)-            Else-            Begin-                Pad := PCBServer.PCBObjectFactory(ePadObject, eNoDimension, eCreate_Default);-                Pad.X := AdomInt(FieldP(Op, 2)); Pad.Y := AdomInt(FieldP(Op, 3));-                Pad.TopXSize := AdomInt(FieldP(Op, 4)); Pad.TopYSize := AdomInt(FieldP(Op, 4));-                Pad.TopShape := eRounded;-                Pad.HoleSize := AdomInt(FieldP(Op, 5));-                If AdomInt(FieldP(Op, 7)) = 74 Then-                Begin-                    { a simple-mode pad: its top size is every layer's (MidXSize halts the engine) }-                    Pad.Layer := eMultiLayer;-                    Pad.Plated := FieldP(Op, 6) = '1';-                End-                Else If AdomInt(FieldP(Op, 7)) = 32 Then Pad.Layer := eBottomLayer-                Else Pad.Layer := eTopLayer;-                Pad.Name := FieldP(Op, 9);-                If FieldP(Op, 10) <> '' Then-                Begin-                    { a manual mask opening (a fiducial's bare ring) and no paste. Only through the pad cache: assigning-                      Pad.SolderMaskExpansion raised an access violation in ScriptingSystem.DLL (2026-10-23). }-                    PC := Pad.GetState_Cache;-                    PC.SolderMaskExpansion := AdomInt(FieldP(Op, 10));-                    PC.SolderMaskExpansionValid := eCacheManual;-                    PC.PasteMaskExpansion := -Pad.TopXSize;-                    PC.PasteMaskExpansionValid := eCacheManual;-                    Pad.SetState_Cache := PC;-                End;-                If Net <> Nil Then Pad.Net := Net;-                If gInputError <> '' Then Res := 'err:bad number ' + gInputError Else RegisterPrim(Board, Pad);-            End;-        End-        Else If Kind = 'X' Then-        Begin-            { a stroke text: X|x|y|height|stroke|layer|rotation|text (a panel's V-CUT marks on the outline layer;-              measured on Develop 2026-10-23) }-            Txt := PCBServer.PCBObjectFactory(eTextObject, eNoDimension, eCreate_Default);-            Txt.XLocation := AdomInt(FieldP(Op, 2)); Txt.YLocation := AdomInt(FieldP(Op, 3));-            Txt.Size := AdomInt(FieldP(Op, 4)); Txt.Width := AdomInt(FieldP(Op, 5));-            Txt.Layer := AdomInt(FieldP(Op, 6));-            Txt.Rotation := AdomInt(FieldP(Op, 7));-            Txt.Text := FieldP(Op, 8);-            If gInputError <> '' Then Res := 'err:bad number ' + gInputError Else RegisterPrim(Board, Txt);-        End-        Else If Kind = 'K' Then-        Begin-            { a keepout rectangle: K|x1|y1|x2|y2|layer. Layer 0 = the Keep-Out layer (every layer); a copper layer id-              = a keepout fill on that layer only (IsKeepout). Measured on Develop 2026-10-23. }-            Fl := PCBServer.PCBObjectFactory(eFillObject, eNoDimension, eCreate_Default);-            Fl.X1Location := AdomInt(FieldP(Op, 2)); Fl.Y1Location := AdomInt(FieldP(Op, 3));-            Fl.X2Location := AdomInt(FieldP(Op, 4)); Fl.Y2Location := AdomInt(FieldP(Op, 5));-            Fl.Rotation := 0;-            If AdomInt(FieldP(Op, 6)) = 0 Then Fl.Layer := eKeepOutLayer-            Else Begin Fl.Layer := AdomInt(FieldP(Op, 6)); Fl.IsKeepout := True; End;-            If gInputError <> '' Then Res := 'err:bad number ' + gInputError Else RegisterPrim(Board, Fl);-        End-        Else If Kind = 'M' Then-        Begin-            Comp := FindCompByDes(Board, FieldP(Op, 2));-            If Comp = Nil Then Res := 'err:no component ' + FieldP(Op, 2)-            Else-            Begin-                Comp.BeginModify;-                { M|ref|x|y|rot*1000|side: side T or B puts the part on that side first (Altium mirrors it as it-                  flips), then rotation and position are set; empty keeps the side }-                If (FieldP(Op, 6) = 'B') And (Comp.Layer <> eBottomLayer) Then Comp.Layer := eBottomLayer-                Else If (FieldP(Op, 6) = 'T') And (Comp.Layer <> eTopLayer) Then Comp.Layer := eTopLayer;-                Comp.Rotation := AdomInt(FieldP(Op, 5)) / 1000;-                Comp.MoveToXY(AdomInt(FieldP(Op, 3)), AdomInt(FieldP(Op, 4)));-                Comp.EndModify;-                If gInputError <> '' Then Res := 'err:bad number ' + gInputError;-            End;-        End-        Else If Kind = 'D' Then-        Begin-            Prim := Nil;-            If FieldP(Op, 2) = 'T' Then-                Prim := FindTrack(Board, AdomInt(FieldP(Op, 3)), AdomInt(FieldP(Op, 4)), AdomInt(FieldP(Op, 5)), AdomInt(FieldP(Op, 6)), AdomInt(FieldP(Op, 7)))-            Else If FieldP(Op, 2) = 'V' Then-                Prim := FindViaAt(Board, AdomInt(FieldP(Op, 3)), AdomInt(FieldP(Op, 4)));-            If Prim = Nil Then Res := 'err:not found'-            Else Board.RemovePCBObject(Prim);-        End-        Else If Kind = 'R' Then-        Begin-            { R|ref|allowLocked(1/0): remove a placed part; the nets its pads carried answer as ok:NET1;NET2 }-            Comp := FindCompByDes(Board, FieldP(Op, 2));-            If Comp = Nil Then Res := 'err:no component ' + FieldP(Op, 2)-            Else If (Not Comp.Moveable) And (FieldP(Op, 3) <> '1') Then Res := 'err:part_locked'-            Else-            Begin-                Res := 'ok:' + PartNetList(Comp);-                Board.RemovePCBObject(Comp);-            End;-        End-        Else If Kind = 'E' Then-        Begin-            { E|ref|field|value: designator, comment (the value), locked (1/0), kind (an Altium TComponentKind) }-            Comp := FindCompByDes(Board, FieldP(Op, 2));-            If Comp = Nil Then Res := 'err:no component ' + FieldP(Op, 2)-            Else-            Begin-                Comp.BeginModify;-                If FieldP(Op, 3) = 'designator' Then Comp.Name.Text := FieldP(Op, 4)-                Else If FieldP(Op, 3) = 'comment' Then Comp.Comment.Text := FieldP(Op, 4)-                Else If FieldP(Op, 3) = 'locked' Then Comp.Moveable := FieldP(Op, 4) <> '1'-                Else If FieldP(Op, 3) = 'kind' Then Comp.ComponentKind := AdomInt(FieldP(Op, 4))-                Else Res := 'err:unknown field ' + FieldP(Op, 3);-                Comp.EndModify;-                Comp.GraphicallyInvalidate;-                Res := Res + ':' + Comp.Name.Text + '|' + Comp.Comment.Text + '|' + JBool(Not Comp.Moveable) + '|' + IntToStr(Comp.ComponentKind);-            End;-        End-        Else If Kind = 'G' Then-        Begin-            { G|key: delete the board graphic with that key (board_graphics) }-            Prim := GraphicByKey(Board, FieldP(Op, 2));-            If Prim = Nil Then Res := 'err:not found: the graphic changed since it was read'-            Else Board.RemovePCBObject(Prim);-        End-        Else If Kind = 'Y' Then-        Begin-            { a filled region: Y|layer|net|x,y;x,y;... in internal units (import_svg). A hole is part of the one contour,-              reached by a zero-width slit: a hole contour (GeometricPolygon AddContourIsHole) printed filled on-              Develop, 2026-10-28, and the slit form printed with its hole open. }-            Net := FindNet(Board, FieldP(Op, 3));-            If (Net = Nil) And (FieldP(Op, 3) <> '') Then Res := 'err:no net ' + FieldP(Op, 3)-            Else-            Begin-                Cont := PCBServer.PCBContourFactory;-                PtList := FieldP(Op, 4) + ';';-                While Pos(';', PtList) > 0 Do-                Begin-                    Pt := Copy(PtList, 1, Pos(';', PtList) - 1);-                    PtList := Copy(PtList, Pos(';', PtList) + 1, Length(PtList));-                    If Pos(',', Pt) > 0 Then Cont.AddPoint(AdomInt(Copy(Pt, 1, Pos(',', Pt) - 1)), AdomInt(Copy(Pt, Pos(',', Pt) + 1, Length(Pt))));-                End;-                Reg := PCBServer.PCBObjectFactory(eRegionObject, eNoDimension, eCreate_Default);-                Reg.Layer := AdomInt(FieldP(Op, 2));-                Reg.SetOutlineContour(Cont);-                If Net <> Nil Then Reg.Net := Net;-                If gInputError <> '' Then Res := 'err:bad number ' + gInputError-                Else Begin RegisterPrim(Board, Reg); Res := 'ok:' + GraphicKey(Reg); End;-            End;-        End-        Else If Kind = 'N' Then-        Begin-            N := 0;-            Prim := FirstOnNet(Board, FieldP(Op, 2));-            While (Prim <> Nil) And (N < 20000) Do-            Begin-                Board.RemovePCBObject(Prim);-                N := N + 1;-                Prim := FirstOnNet(Board, FieldP(Op, 2));-            End;-            Res := 'ok:' + IntToStr(N);-        End-        Else Res := 'err:unknown op ' + Kind;-        If Copy(Res, 1, 3) = 'err' Then Failed := Failed + 1 Else Okay := Okay + 1;-        If Results <> '' Then Results := Results + ',';-        Results := Results + '"' + JsonEsc(Res) + '"';-    End;-    PCBServer.PostProcess;-    gInputError := '';-    Board.ViewManager_FullUpdate;-    Ops.Free;-    Result := '{' + JRaw('ok','true') + ',' + JRaw('applied', IntToStr(Okay)) + ',' + JRaw('failed', IntToStr(Failed)) + ',' +-        JRaw('results', '[' + Results + ']') + '}';-End;--{ apply_pours: solid polygon pours, one op per payload line, RAW internal units:-    P|net|layer|pourIndex|x,y;x,y;...|removeDead   a closed outline (the last point need not repeat the-                                                   first); removeDead 0 keeps islands Altium calls dead-  All in one PreProcess/PostProcess; each pour is rebuilt (poured) after it is added. Answers-  results[] per line: "ok" or "err:<why>". }-{ a pour's connect style: Altium keeps no connect style on a polygon; a PolygonConnect rule-  scoped IsNamedPolygon('<name>') sets it for that one pour. Altium gives a newly added rule-  priority 1 and moves the rest down (measured AD 26.11.1, 2026-10-15), so it outranks the board's-  own. Writing Rule.Priority from a script halted the engine with an empty Error box (same-  date): do not reorder rules here. }-Function FindRuleNamed(Board : IPCB_Board; Kind : Integer; Name : String) : IPCB_Rule;-Var-    BIt  : IPCB_BoardIterator;-    Rule : IPCB_Rule;-Begin-    Result := Nil;-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eRuleObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Rule := BIt.FirstPCBObject;-    While Rule <> Nil Do-    Begin-        If (Rule.RuleKind = Kind) And (Rule.Name = Name) Then Begin Result := Rule; Break; End;-        Rule := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-End;--Function PolyNamed(Board : IPCB_Board; Name : String) : Boolean;-Var-    BIt  : IPCB_BoardIterator;-    Poly : IPCB_Polygon;-Begin-    Result := False;-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(ePolyObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Poly := BIt.FirstPCBObject;-    While Poly <> Nil Do-    Begin-        If Poly.Name = Name Then Begin Result := True; Break; End;-        Poly := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-End;--{ Style: D direct (solid), R relief, N no connect. W/G raw units, E spokes (2 or 4). }-Procedure SetPourConnect(Board : IPCB_Board; PolyName, Style : String; W, G, E : Integer);-Var-    Rule : IPCB_Rule;-Begin-    Rule := FindRuleNamed(Board, eRule_PolygonConnectStyle, 'AdomPC ' + PolyName);-    If Rule <> Nil Then Board.RemovePCBObject(Rule);-    Rule := PCBServer.PCBRuleFactory(eRule_PolygonConnectStyle);-    Rule.Name := 'AdomPC ' + PolyName;-    Rule.Comment := PolyName;-    { IsNamedPolygon is the polygon itself; InNamedPolygon is 'belongs to' (its pour primitives) and-      never matched the polygon, so the catch-all relief stayed in force (measured 2026-10-15). The-      same OR on both sides: which of the pair (polygon, pad) Altium tests against Scope1 is not ours-      to know. }-    Rule.Scope1Expression := 'IsNamedPolygon(''' + PolyName + ''') Or IsPad Or IsVia';-    Rule.Scope2Expression := Rule.Scope1Expression;-    If Style = 'D' Then Rule.ConnectStyle := eDirectConnectToPlane-    Else If Style = 'N' Then Rule.ConnectStyle := eNoConnect-    Else-    Begin-        Rule.ConnectStyle := eReliefConnectToPlane;-        If W > 0 Then Rule.ReliefConductorWidth := W;-        { no air gap here: AD 26.11.1 keeps it per pad type (SetAirGapWidth(primitiveType, v));-          a plain AirGapWidth is 'Undeclared identifier' (measured 2026-10-15). Altium's default-          gap is 10 mil. }-        If E > 0 Then Rule.ReliefEntries := E;-    End;-    Board.AddPCBObject(Rule);-    PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Rule.I_ObjectAddress);-End;--{ solid joints inside a pour: pads completely inside each rect (the bridge widens a joint box to-  the pads it touches) connect to this pour full width-  (KiCad's small solid zone over a pad, which Altium cannot pour as a separate overlapping-  polygon: a same-net polygon inside another pours nothing, measured 2026-10-15). One direct rule-  per rect; both scopes are the same OR so the pair matches in either order. Rects: 'l,b,r,t'-  RAW, ';'-joined. The rule's Comment holds the polygon name for cleanup. }-Procedure SetPourJoints(Board : IPCB_Board; PolyName, Rects : String);-Var-    RL   : TStringList;-    Rule : IPCB_Rule;-    K    : Integer;-    Q, R : String;-Begin-    RL := SplitSemi(Rects);-    For K := 0 To RL.Count - 1 Do-    Begin-        R := RL[K];-        { whole mils, widened outward: the region test wants the pad completely inside }-        Q := 'IsNamedPolygon(''' + PolyName + ''') Or InRegionAbsolute(' +-            IntToStr(Trunc(CoordToMils(AdomInt(Field(R, 1)))) - 1) + ',' + IntToStr(Trunc(CoordToMils(AdomInt(Field(R, 2)))) - 1) + ',' +-            IntToStr(Trunc(CoordToMils(AdomInt(Field(R, 3)))) + 1) + ',' + IntToStr(Trunc(CoordToMils(AdomInt(Field(R, 4)))) + 1) + ')';-        Rule := FindRuleNamed(Board, eRule_PolygonConnectStyle, 'AdomPJ ' + PolyName + ' #' + IntToStr(K + 1));-        If Rule <> Nil Then Board.RemovePCBObject(Rule);-        Rule := PCBServer.PCBRuleFactory(eRule_PolygonConnectStyle);-        Rule.Name := 'AdomPJ ' + PolyName + ' #' + IntToStr(K + 1);-        Rule.Comment := PolyName;-        Rule.Scope1Expression := Q;-        Rule.Scope2Expression := Q;-        Rule.ConnectStyle := eDirectConnectToPlane;-        Board.AddPCBObject(Rule);-        PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Rule.I_ObjectAddress);-    End;-    RL.Free;-End;--{ a pour keepout: a polygon cutout region on one layer (pours stay out, tracks do not) }-Function AddCutout(Board : IPCB_Board; Lay : Integer; Pts : TStringList; Name : String) : String;-Var-    Reg  : IPCB_Region;-    Cont : IPCB_Contour;-    K    : Integer;-Begin-    Result := 'ok';-    Cont := PCBServer.PCBContourFactory;-    For K := 0 To Pts.Count - 1 Do Cont.AddPoint(AdomInt(Field(Pts[K], 1)), AdomInt(Field(Pts[K], 2)));-    If gInputError <> '' Then Begin Result := 'err:bad number ' + gInputError; Exit; End;-    Reg := PCBServer.PCBObjectFactory(eRegionObject, eNoDimension, eCreate_Default);-    Reg.BeginModify;-    Reg.Layer := Lay;-    Reg.Kind := eRegionKind_Cutout;-    Reg.Name := Name;-    Reg.SetOutlineContour(Cont);-    Reg.EndModify;-    RegisterPrim(Board, Reg);-End;--{ The polygon named Name, Nil when none }-Function PolyByName(Board : IPCB_Board; Name : String) : IPCB_Polygon;-Var-    BIt  : IPCB_BoardIterator;-    Poly : IPCB_Polygon;-Begin-    Result := Nil;-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(ePolyObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Poly := BIt.FirstPCBObject;-    While (Poly <> Nil) And (Result = Nil) Do-    Begin-        If Poly.Name = Name Then Result := Poly;-        Poly := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-End;--{ edit_pour: change existing polygons in place. Payload lines "name|key|value": net, layer (id), hatch (solid, 45, 90,-  none), track (raw width of hatch tracks), grid (raw), dead (0/1: remove dead copper), islands (0/1: remove small-  islands), neck (raw neck width; 0 off), over (same | none: pour over same-net objects), index (pour order),-  connect (D|R|N,width,gap,entries raw: the pour's PolygonConnect rule). Each answers ok:<key> or err:<why>, in order;-  every polygon touched is rebuilt. }-Function CmdEditPour(Lines : TStringList; Payload : String) : String;-Var-    Board : IPCB_Board;-    Poly  : IPCB_Polygon;-    Net   : IPCB_Net;-    Ops, Done : TStringList;-    Op, Nm, Key, V, Res, Results : String;-    I : Integer;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_board') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    Ops := TStringList.Create; Ops.Text := Payload;-    Done := TStringList.Create;-    Results := '';-    PCBServer.PreProcess;-    For I := 0 To Ops.Count - 1 Do-    Begin-        Op := Ops[I];-        If Trim(Op) = '' Then Continue;-        Nm := FieldP(Op, 1); Key := FieldP(Op, 2); V := FieldP(Op, 3);-        Poly := PolyByName(Board, Nm);-        Res := 'ok:' + Key;-        If Poly = Nil Then Res := 'err:no pour named ' + Nm-        Else If Key = 'connect' Then SetPourConnect(Board, Nm, Field(V, 1), AdomInt(Field(V, 2)), AdomInt(Field(V, 3)), AdomInt(Field(V, 4)))-        Else-        Begin-            Poly.BeginModify;-            If Key = 'net' Then-            Begin-                Net := FindNet(Board, V);-                If Net = Nil Then Res := 'err:no net ' + V Else Poly.Net := Net;-            End-            Else If Key = 'layer' Then Poly.Layer := AdomInt(V)-            Else If Key = 'hatch' Then-            Begin-                If V = 'solid' Then Poly.PolyHatchStyle := ePolySolid-                Else If V = '45' Then Poly.PolyHatchStyle := ePolyHatch45-                Else If V = '90' Then Poly.PolyHatchStyle := ePolyHatch90-                Else If V = 'none' Then Poly.PolyHatchStyle := ePolyNoHatch-                Else Res := 'err:hatch is solid, 45, 90 or none';-            End-            Else If Key = 'track' Then Poly.TrackSize := AdomInt(V)-            Else If Key = 'grid' Then Poly.Grid := AdomInt(V)-            Else If Key = 'dead' Then Poly.RemoveDead := V = '1'-            Else If Key = 'islands' Then Poly.RemoveIslandsByArea := V = '1'-            Else If Key = 'neck' Then-            Begin-                Poly.RemoveNarrowNecks := AdomInt(V) > 0;-                If AdomInt(V) > 0 Then Poly.NeckWidthThreshold := AdomInt(V);-            End-            Else If Key = 'over' Then-            Begin-                If V = 'same' Then Poly.PourOver := ePolygonPourOver_SameNet Else Poly.PourOver := ePolygonPourOver_None;-            End-            Else If Key = 'index' Then Poly.PourIndex := AdomInt(V)-            Else Res := 'err:unknown key ' + Key;-            Poly.EndModify;-        End;-        If (Poly <> Nil) And (Done.IndexOf(Nm) < 0) Then Done.Add(Nm);-        If Results <> '' Then Results := Results + ',';-        Results := Results + '"' + JsonEsc(Res) + '"';-    End;-    For I := 0 To Done.Count - 1 Do-    Begin-        Poly := PolyByName(Board, Done[I]);-        If Poly <> Nil Then Begin Poly.Rebuild; Poly.GraphicallyInvalidate; End;-    End;-    PCBServer.PostProcess;-    Board.ViewManager_FullUpdate;-    Result := '{' + JRaw('ok','true') + ',' + JRaw('results', '[' + Results + ']') + ',' + JRaw('pours', IntToStr(Done.Count)) + '}';-    Ops.Free; Done.Free;-End;--Function CmdApplyPours(Lines : TStringList; Payload : String) : String;-Var-    Board : IPCB_Board;-    Ops, Pts : TStringList;-    Op, Res, Results, P, PName : String;-    Poly  : IPCB_Polygon;-    Seg   : TPolySegment;-    Net   : IPCB_Net;-    I, K, C, Okay, Failed : Integer;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    Ops := TStringList.Create;-    Ops.Text := Payload;-    Results := ''; Okay := 0; Failed := 0;-    PCBServer.PreProcess;-    For I := 0 To Ops.Count - 1 Do-    Begin-        Op := Trim(Ops[I]);-        If Op = '' Then Continue;-        Res := 'ok';-        gInputError := '';-        If FieldP(Op, 1) = 'C' Then-        Begin-            Pts := SplitSemi(FieldP(Op, 3));-            If Pts.Count < 3 Then Res := 'err:a cutout needs 3 or more points'-            Else Res := AddCutout(Board, AdomInt(FieldP(Op, 2)), Pts, FieldP(Op, 4));-            Pts.Free;-        End-        Else If FieldP(Op, 1) <> 'P' Then Res := 'err:unknown op'-        Else-        Begin-            Net := FindNet(Board, FieldP(Op, 2));-            Pts := SplitSemi(FieldP(Op, 5));-            If (Net = Nil) And (FieldP(Op, 2) <> '') Then Res := 'err:no net ' + FieldP(Op, 2)-            Else If Pts.Count < 3 Then Res := 'err:an outline needs 3 or more points'-            Else-            Begin-                Poly := PCBServer.PCBObjectFactory(ePolyObject, eNoDimension, eCreate_Default);-                Poly.BeginModify;-                Poly.PolygonType := eSignalLayerPolygon;-                Poly.Layer := AdomInt(FieldP(Op, 3));-                Poly.PolyHatchStyle := ePolySolid;-                PName := FieldP(Op, 7);-                If PName = '' Then PName := 'Adom ' + FieldP(Op, 2) + ' L' + FieldP(Op, 3);-                If PolyNamed(Board, PName) Then-                Begin-                    K := 2;-                    While PolyNamed(Board, PName + ' ' + IntToStr(K)) Do K := K + 1;-                    PName := PName + ' ' + IntToStr(K);-                End;-                Poly.Name := PName;-                Poly.RemoveDead := FieldP(Op, 6) <> '0';-                { connected copper of any size stays, as in a KiCad zone: Altium's own default drops-                  islands under 2500 sq mil, which emptied every small joint pour (measured-                  2026-10-15: a 1.8 mm box around a pad poured 0 mm2). Dead copper still goes. }-                Poly.RemoveIslandsByArea := FieldP(Op, 12) = '1';-                If AdomInt(FieldP(Op, 13)) > 0 Then-                Begin-                    Poly.RemoveNarrowNecks := True;-                    Poly.NeckWidthThreshold := AdomInt(FieldP(Op, 13));-                End;-                { pour over every same-net object, as a KiCad zone does: by default Altium pours AROUND-                  same-net tracks, which leaves slivers that touch no pad and are removed as dead copper-                  (measured 2026-10-15: a buck's SW patch poured 0 mm2 beside its own SW track) }-                Poly.PourOver := ePolygonPourOver_SameNet;-                If Net <> Nil Then Poly.Net := Net;-                Poly.PointCount := Pts.Count;-                For K := 0 To Pts.Count - 1 Do-                Begin-                    P := Pts[K];-                    Seg := Poly.Segments[K];-                    Seg.Kind := ePolySegmentLine;-                    Seg.vx := AdomInt(Field(P, 1));-                    Seg.vy := AdomInt(Field(P, 2));-                    Poly.Segments[K] := Seg;-                End;-                C := AdomInt(FieldP(Op, 4));-                If C > 0 Then Poly.PourIndex := C;-                Poly.EndModify;-                If gInputError <> '' Then Res := 'err:bad number ' + gInputError-                Else-                Begin-                    RegisterPrim(Board, Poly);-                    If FieldP(Op, 8) <> '' Then-                        SetPourConnect(Board, PName, FieldP(Op, 8), AdomInt(FieldP(Op, 9)), AdomInt(FieldP(Op, 10)), AdomInt(FieldP(Op, 11)));-                    If FieldP(Op, 14) <> '' Then SetPourJoints(Board, PName, FieldP(Op, 14));-                    Res := 'ok:' + PName;-                    Poly.Rebuild;-                    Poly.GraphicallyInvalidate;-                End;-            End;-            Pts.Free;-        End;-        If Copy(Res, 1, 3) = 'err' Then Failed := Failed + 1 Else Okay := Okay + 1;-        If Results <> '' Then Results := Results + ',';-        Results := Results + '"' + JsonEsc(Res) + '"';-    End;-    PCBServer.PostProcess;-    gInputError := '';-    Board.ViewManager_FullUpdate;-    Ops.Free;-    Result := '{' + JRaw('ok','true') + ',' + JRaw('applied', IntToStr(Okay)) + ',' + JRaw('failed', IntToStr(Failed)) + ',' +-        JRaw('results', '[' + Results + ']') + '}';-End;--{ pour_state: every polygon with its outline AND the copper it actually poured: the region-  primitives inside it, each as a main contour plus holes, in RAW units. The bridge computes-  the areas (shoelace), so nothing here does floating point.-    polys [["net",layer,pourIndex,[[x,y]...outline],[ [[x,y]...main], [[[x,y]...hole]...] ]...]] }-Function ContourPts(Ct : IPCB_Contour) : String;-Var-    K : Integer;-    S : String;-Begin-    S := '';-    For K := 0 To Ct.Count - 1 Do-    Begin-        If K > 0 Then S := S + ',';-        S := S + '[' + IntToStr(Ct.x[K]) + ',' + IntToStr(Ct.y[K]) + ']';-    End;-    Result := '[' + S + ']';-End;--{ rebuild_pours: rebuild every polygon on the current board through the API. Altium's own Repour All (the helper's-  PCB:ChangeObject RepourAllPolygons) left the Bluetooth Sentinel's hatched GND pours as they were, shorting new stubs,-  where Rebuild cleared them (2026-10-28). Scoped pour rules match nothing in here, so altium_repour uses it only when the-  helper's repour changed no pour. }-Function CmdRebuildPours(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    BIt   : IPCB_BoardIterator;-    Poly  : IPCB_Polygon;-    N     : Integer;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    N := 0;-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(ePolyObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Poly := BIt.FirstPCBObject;-    While Poly <> Nil Do-    Begin-        PCBServer.PreProcess;-        Poly.Rebuild;-        Poly.GraphicallyInvalidate;-        PCBServer.PostProcess;-        N := N + 1;-        Poly := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-    Board.ViewManager_FullUpdate;-    Result := '{' + JRaw('ok','true') + ',' + JRaw('rebuilt', IntToStr(N)) + '}';-End;--Function CmdPourState(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    BIt   : IPCB_BoardIterator;-    GIt   : IPCB_GroupIterator;-    Poly  : IPCB_Polygon;-    Prim  : IPCB_Primitive;-    Reg   : IPCB_Region;-    Seg   : TPolySegment;-    LG    : TStringList;-    Outl, Fills, Holes : String;-    K, H, Pieces : Integer;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    LG := TStringList.Create;-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(ePolyObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Poly := BIt.FirstPCBObject;-    While Poly <> Nil Do-    Begin-        Outl := PolySegPts(Poly);-        Fills := '';-        GIt := Poly.GroupIterator_Create;-        GIt.AddFilter_ObjectSet(MkSet(eRegionObject));-        GIt.AddFilter_LayerSet(AllLayers);-        Prim := GIt.FirstPCBObject;-        While Prim <> Nil Do-        Begin-            Reg := Prim;-            Holes := '';-            For H := 0 To Reg.HoleCount - 1 Do-            Begin-                If H > 0 Then Holes := Holes + ',';-                Holes := Holes + ContourPts(Reg.Holes[H]);-            End;-            If Fills <> '' Then Fills := Fills + ',';-            Fills := Fills + '[' + ContourPts(Reg.MainContour) + ',[' + Holes + ']]';-            Prim := GIt.NextPCBObject;-        End;-        Poly.GroupIterator_Destroy(GIt);-        { a hatched pour is poured as tracks and arcs, not regions: count them, or it reads as unfilled }-        Pieces := 0;-        GIt := Poly.GroupIterator_Create;-        GIt.AddFilter_ObjectSet(MkSet(eTrackObject, eArcObject));-        GIt.AddFilter_LayerSet(AllLayers);-        Prim := GIt.FirstPCBObject;-        While Prim <> Nil Do Begin Pieces := Pieces + 1; Prim := GIt.NextPCBObject; End;-        Poly.GroupIterator_Destroy(GIt);-        LG.Add('[' + NetOf(Poly) + ',' + IntToStr(Poly.Layer) + ',' + IntToStr(Poly.PourIndex) + ',[' + Outl + '],[' + Fills + '],"' + JsonEsc(Poly.Name) + '",' + IntToStr(Pieces) + ']');-        Poly := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-    Result := '{' + JRaw('ok','true') + ',' + JStr('file', Board.FileName) + ',' + JRaw('polys', JoinList(LG)) + '}';-    LG.Free;-End;--{ remove_pours: delete polygons. Payload lines: "net|layer" (layer 0 = any layer), or "*" for all. }-Function FirstPoly(Board : IPCB_Board; NetName : String; Lay : Integer; All : Boolean) : IPCB_Polygon;-Var-    BIt  : IPCB_BoardIterator;-    Poly : IPCB_Polygon;-    N    : String;-Begin-    Result := Nil;-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(ePolyObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Poly := BIt.FirstPCBObject;-    While Poly <> Nil Do-    Begin-        N := '';-        If Poly.Net <> Nil Then N := Poly.Net.Name;-        { NetName '=<name>' picks a polygon by its name }-        If All Or ((N = NetName) And ((Lay = 0) Or (Poly.Layer = Lay))) Or ((Copy(NetName, 1, 1) = '=') And (Poly.Name = Copy(NetName, 2, Length(NetName)))) Then Begin Result := Poly; Break; End;-        Poly := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-End;--Function FirstAdomCutout(Board : IPCB_Board; Lay : Integer) : IPCB_Region;-Var-    BIt : IPCB_BoardIterator;-    Reg : IPCB_Region;-Begin-    Result := Nil;-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eRegionObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Reg := BIt.FirstPCBObject;-    While Reg <> Nil Do-    Begin-        If (Reg.Kind = eRegionKind_Cutout) And (Copy(Reg.Name, 1, 4) = 'Adom') And ((Lay = 0) Or (Reg.Layer = Lay)) Then Begin Result := Reg; Break; End;-        Reg := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-End;--{ the AdomPC/AdomPJ connect rule whose polygon (named in its Comment) is gone }-Function OrphanPourRule(Board : IPCB_Board) : IPCB_Rule;-Var-    BIt  : IPCB_BoardIterator;-    Rule : IPCB_Rule;-    PN   : String;-Begin-    Result := Nil;-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eRuleObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Rule := BIt.FirstPCBObject;-    While Rule <> Nil Do-    Begin-        If (Rule.RuleKind = eRule_PolygonConnectStyle) And ((Copy(Rule.Name, 1, 7) = 'AdomPC ') Or (Copy(Rule.Name, 1, 7) = 'AdomPJ ')) Then-        Begin-            PN := Rule.Comment;-            If PN = '' Then PN := Copy(Rule.Name, 8, Length(Rule.Name));-            If Not PolyNamed(Board, PN) Then Begin Result := Rule; Break; End;-        End;-        Rule := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-End;--Function CmdRemovePours(Lines : TStringList; Payload : String) : String;-Var-    Board : IPCB_Board;-    Ops   : TStringList;-    Op    : String;-    Poly  : IPCB_Polygon;-    Reg   : IPCB_Region;-    Rule  : IPCB_Rule;-    I, N, NC, NR : Integer;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    Ops := TStringList.Create;-    Ops.Text := Payload;-    N := 0; NC := 0; NR := 0;-    PCBServer.PreProcess;-    For I := 0 To Ops.Count - 1 Do-    Begin-        Op := Trim(Ops[I]);-        If Op = '' Then Continue;-        If FieldP(Op, 1) = 'C' Then-        Begin-            Reg := FirstAdomCutout(Board, AdomInt(FieldP(Op, 2)));-            While (Reg <> Nil) And (NC < 5000) Do-            Begin-                Board.RemovePCBObject(Reg);-                NC := NC + 1;-                Reg := FirstAdomCutout(Board, AdomInt(FieldP(Op, 2)));-            End;-            Continue;-        End;-        Poly := FirstPoly(Board, FieldP(Op, 1), AdomInt(FieldP(Op, 2)), Op = '*');-        While (Poly <> Nil) And (N < 5000) Do-        Begin-            Board.RemovePCBObject(Poly);-            N := N + 1;-            Poly := FirstPoly(Board, FieldP(Op, 1), AdomInt(FieldP(Op, 2)), Op = '*');-        End;-    End;-    Rule := OrphanPourRule(Board);-    While (Rule <> Nil) And (NR < 5000) Do-    Begin-        Board.RemovePCBObject(Rule);-        NR := NR + 1;-        Rule := OrphanPourRule(Board);-    End;-    PCBServer.PostProcess;-    gInputError := '';-    Board.ViewManager_FullUpdate;-    Ops.Free;-    Result := '{' + JRaw('ok','true') + ',' + JRaw('removed', IntToStr(N)) + ',' + JRaw('cutoutsRemoved', IntToStr(NC)) + ',' + JRaw('rulesRemoved', IntToStr(NR)) + '}';-End;--{ routing_drc: the violations on the board right now (online DRC keeps them current as edits-  land, a batch DRC refreshes them all) with each one's location, plus the ratsnest count.-    violations [["rule","description",left,bottom,right,top]]  RAW units }-Function CmdRoutingDrc(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    BIt   : IPCB_BoardIterator;-    Prim  : IPCB_Primitive;-    V     : IPCB_Violation;-    R     : TCoordRect;-    LV    : TStringList;-    Rule, O1, O2 : String;-    Unrouted, N : Integer;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    LV := TStringList.Create;-    Unrouted := 0; N := 0;-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eViolationObject, eConnectionObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Prim := BIt.FirstPCBObject;-    While Prim <> Nil Do-    Begin-        If Prim.ObjectId = eConnectionObject Then Unrouted := Unrouted + 1-        Else-        Begin-            V := Prim;-            N := N + 1;-            If N <= 500 Then-            Begin-                Rule := '';-                If V.Rule <> Nil Then Rule := V.Rule.Name;-                R := V.BoundingRectangle;-                { the two objects that clash: a description can be empty (a Clearance one was, 2026-10-22) }-                O1 := ''; O2 := '';-                If V.Primitive1 <> Nil Then O1 := V.Primitive1.Descriptor;-                If V.Primitive2 <> Nil Then O2 := V.Primitive2.Descriptor;-                LV.Add('["' + JsonEsc(Rule) + '","' + JsonEsc(V.Description) + '",' + IntToStr(R.Left) + ',' + IntToStr(R.Bottom) + ',' +-                    IntToStr(R.Right) + ',' + IntToStr(R.Top) + ',"' + JsonEsc(O1) + '","' + JsonEsc(O2) + '"]');-            End;-        End;-        Prim := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-    Result := '{' + JRaw('ok','true') + ',' + JStr('file', Board.FileName) + ',' + JRaw('violationCount', IntToStr(N)) + ',' +-        JRaw('unrouted', IntToStr(Unrouted)) + ',' + JRaw('violations', JoinList(LV)) + '}';-    LV.Free;-End;--{ Outline vertices from the payload's O line into the board outline, RAW units:-    O|x,y,kind,cx,cy,radius,angle1MilliDeg,angle2MilliDeg;...   kind 1 = an arc from this vertex to the next }-Function SetOutlineFrom(Board : IPCB_Board; Spec : String) : Integer;-Var-    BO  : IPCB_BoardOutline;-    Seg : TPolySegment;-    Pts : TStringList;-    K   : Integer;-    P   : String;-Begin-    Result := 0;-    BO := Board.BoardOutline;-    If BO = Nil Then Exit;-    Pts := SplitSemi(Spec);-    If Pts.Count < 3 Then Begin Pts.Free; Exit; End;-    BO.Invalidate;-    BO.PointCount := Pts.Count;-    For K := 0 To Pts.Count - 1 Do-    Begin-        P := Pts[K];-        Seg := BO.Segments[K];-        Seg.vx := AdomInt(Field(P, 1));-        Seg.vy := AdomInt(Field(P, 2));-        If Field(P, 3) = '1' Then-        Begin-            Seg.Kind := ePolySegmentArc;-            Seg.cx := AdomInt(Field(P, 4));-            Seg.cy := AdomInt(Field(P, 5));-            Seg.Radius := AdomInt(Field(P, 6));-            Seg.Angle1 := AdomInt(Field(P, 7)) / 1000;-            Seg.Angle2 := AdomInt(Field(P, 8)) / 1000;-        End-        Else Seg.Kind := ePolySegmentLine;-        BO.Segments[K] := Seg;-    End;-    BO.Rebuild;-    BO.Validate;-    Result := Pts.Count;-    Pts.Free;-End;--{ A library footprint placed by its OWN origin (the library's 0,0), then moved and rotated as a-  whole: the coordinates a layout tool or a reference design gives mean the same here. }-{ A library stores a footprint either around the library's canvas origin (Altium's own editor: about-  50000 mil out) or around 0,0 (bridge-built footprints once a body was aligned). Measured 2026-10-15:-  one PcbLib held both. The footprint's frame is whichever its pad cluster sits nearer to; the offset-  returned is what to take off every primitive. }-Function FpOffset(PLib : IPCB_Library; LibFp : IPCB_LibComponent; Var OX : Integer; Var OY : Integer) : Boolean;-Var-    GIt  : IPCB_GroupIterator;-    Prim : IPCB_Primitive;-    R    : TCoordRect;-    CL, CB, CR, CT, CX, CY, LX, LY : Integer;-    AnyP : Boolean;-Begin-    OX := 0; OY := 0; AnyP := False; CL := 0; CB := 0; CR := 0; CT := 0;-    GIt := LibFp.GroupIterator_Create;-    GIt.AddFilter_ObjectSet(MkSet(ePadObject));-    GIt.AddFilter_LayerSet(AllLayers);-    Prim := GIt.FirstPCBObject;-    While Prim <> Nil Do-    Begin-        R := Prim.BoundingRectangle;-        If Not AnyP Then Begin CL := R.Left; CB := R.Bottom; CR := R.Right; CT := R.Top; AnyP := True; End-        Else-        Begin-            If R.Left < CL Then CL := R.Left;-            If R.Bottom < CB Then CB := R.Bottom;-            If R.Right > CR Then CR := R.Right;-            If R.Top > CT Then CT := R.Top;-        End;-        Prim := GIt.NextPCBObject;-    End;-    LibFp.GroupIterator_Destroy(GIt);-    Result := False;-    If Not AnyP Then Exit;-    CX := (CL + CR) Div 2; CY := (CB + CT) Div 2;-    LX := PLib.Board.XOrigin; LY := PLib.Board.YOrigin;-    If (Abs(CX - LX) + Abs(CY - LY)) < (Abs(CX) + Abs(CY)) Then-    Begin-        OX := LX; OY := LY; Result := True;-    End;-End;--Function PlaceByOrigin(Board : IPCB_Board; PLib : IPCB_Library; LibFp : IPCB_LibComponent; X, Y, RotMilli : Integer) : IPCB_Component;-Var-    Inst : IPCB_Component;-    GIt  : IPCB_GroupIterator;-    Prim, Cpy : IPCB_Primitive;-    OX, OY : Integer;-Begin-    FpOffset(PLib, LibFp, OX, OY);-    Inst := PCBServer.PCBObjectFactory(eComponentObject, eNoDimension, eCreate_Default);-    Inst.Layer := eTopLayer;-    Inst.NameOn := False;-    Inst.CommentOn := False;-    GIt := LibFp.GroupIterator_Create;-    GIt.AddFilter_LayerSet(AllLayers);-    Prim := GIt.FirstPCBObject;-    While Prim <> Nil Do-    Begin-        Cpy := Prim.Replicate;-        If (OX <> 0) Or (OY <> 0) Then Cpy.MoveByXY(-OX, -OY);-        Inst.AddPCBObject(Cpy);-        Prim := GIt.NextPCBObject;-    End;-    LibFp.GroupIterator_Destroy(GIt);-    Board.AddPCBObject(Inst);-    PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Inst.I_ObjectAddress);-    Inst.BeginModify;-    Inst.MoveToXY(X, Y);-    Inst.Rotation := RotMilli / 1000;-    Inst.EndModify;-    Result := Inst;-End;--Function FindLibFp(PLib : IPCB_Library; Name : String) : IPCB_LibComponent;-Var-    I : Integer;-    F : IPCB_LibComponent;-Begin-    Result := Nil;-    For I := 0 To PLib.ComponentCount - 1 Do-    Begin-        F := PLib.GetComponent(I);-        If UpperCase(F.Name) = UpperCase(Name) Then Begin Result := F; Break; End;-    End;-End;--Function JoinSemi(L : TStringList) : String;-Var-    I : Integer;-Begin-    Result := '';-    For I := 0 To L.Count - 1 Do-    Begin-        If I > 0 Then Result := Result + ';';-        Result := Result + L[I];-    End;-End;--{ build_board: a NEW .PcbDoc from a parts list and a netlist, saved to pcb=<path>. The PcbLib with-  every footprint must be the CURRENT document. Payload lines, RAW units:-    O|<outline vertices, see SetOutlineFrom>-    C|ref|footprint|x|y|rotMilliDeg|comment-    N|ref|pad|net-  Answers placed, missing footprints, the nets made and pads wired. }-Function CmdBuildBoard(Lines : TStringList; Payload : String) : String;-Var-    PLib  : IPCB_Library;-    Board : IPCB_Board;-    SD    : IServerDocument;-    Ops, Fps, Assign : TStringList;-    Inst  : IPCB_Component;-    LibFp : IPCB_LibComponent;-    Op, PcbPath, Missing, Outline, Refs : String;-    I, K, Placed, Wired, OutN : Integer;-Begin-    PcbPath := Param(Lines, 'pcb');-    PLib := PCBServer.GetCurrentPCBLibrary;-    If PLib = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the PcbLib with every footprint first (it must be the current document)') + '}'; Exit; End;-    If PcbPath = '' Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','pcb (the .PcbDoc path to save) is required') + '}'; Exit; End;-    Ops := TStringList.Create;-    Ops.Text := Payload;-    { every footprint must be in the library before anything is made }-    Fps := TStringList.Create;-    Missing := ''; Outline := '';-    For I := 0 To Ops.Count - 1 Do-    Begin-        Op := Trim(Ops[I]);-        If FieldP(Op, 1) = 'O' Then Outline := FieldP(Op, 2);-        If FieldP(Op, 1) = 'C' Then-        Begin-            If FindLibFp(PLib, FieldP(Op, 3)) = Nil Then Missing := Missing + FieldP(Op, 3) + ' ';-        End;-    End;-    If Missing <> '' Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','footprint_not_found') + ',' + JStr('error','not in the current PcbLib: ' + Trim(Missing)) + '}';-        Ops.Free; Fps.Free; Exit;-    End;-    SD := CreateNewDocumentFromDocumentKind('PCB');-    If SD = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','could not create a PCB document') + '}'; Ops.Free; Fps.Free; Exit; End;-    Client.ShowDocument(SD);-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board after create') + '}'; Ops.Free; Fps.Free; Exit; End;-    Placed := 0; Wired := 0; OutN := 0; Refs := '';-    gInputError := '';-    PCBServer.PreProcess;-    If Outline <> '' Then OutN := SetOutlineFrom(Board, Outline);-    For I := 0 To Ops.Count - 1 Do-    Begin-        Op := Trim(Ops[I]);-        If FieldP(Op, 1) <> 'C' Then Continue;-        LibFp := FindLibFp(PLib, FieldP(Op, 3));-        Inst := PlaceByOrigin(Board, PLib, LibFp, AdomInt(FieldP(Op, 4)), AdomInt(FieldP(Op, 5)), AdomInt(FieldP(Op, 6)));-        Inst.Name.Text := FieldP(Op, 2);-        Inst.Pattern := LibFp.Name;-        Inst.Comment.Text := FieldP(Op, 7);-        { the pad assignments for this part, as AssignPadNets takes them }-        Assign := TStringList.Create;-        For K := 0 To Ops.Count - 1 Do-            If (FieldP(Trim(Ops[K]), 1) = 'N') And (FieldP(Trim(Ops[K]), 2) = FieldP(Op, 2)) Then-            Begin-                Assign.Add(FieldP(Trim(Ops[K]), 3) + '=' + FieldP(Trim(Ops[K]), 4));-                Wired := Wired + 1;-            End;-        If Assign.Count > 0 Then-        Begin-            AssignPadNets(Board, Inst, JoinSemi(Assign));-        End;-        Assign.Free;-        Placed := Placed + 1;-        If Refs <> '' Then Refs := Refs + ',';-        Refs := Refs + '"' + JsonEsc(FieldP(Op, 2)) + '"';-    End;-    PCBServer.PostProcess;-    Board.ViewManager_FullUpdate;-    FreePath(PcbPath);-    SD.SetFileName(PcbPath);-    SD.DoFileSave('');-    Ops.Free; Fps.Free;-    Result := '{' + JRaw('ok','true') + ',' + JStr('pcb', PcbPath) + ',' + JRaw('placed', IntToStr(Placed)) + ',' + JRaw('padsWired', IntToStr(Wired)) + ',' +-        JRaw('libOriginMil', '[' + IntToStr(Round(CoordToMils(PLib.Board.XOrigin))) + ',' + IntToStr(Round(CoordToMils(PLib.Board.YOrigin))) + ']') + ',' +-        JRaw('outlineVertices', IntToStr(OutN)) + ',' + JRaw('refs', '[' + Refs + ']') + ',' + JRaw('badNumber', JBool(gInputError <> '')) + '}';-    gInputError := '';-End;--{ set_outline: reshape the current board's outline (arcs allowed: rounded corners), one undo step.-  Payload: O|<vertices, see SetOutlineFrom> }-Function CmdSetOutline(Lines : TStringList; Payload : String) : String;-Var-    Board : IPCB_Board;-    N : Integer;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    PCBServer.PreProcess;-    N := SetOutlineFrom(Board, FieldP(Trim(Payload), 2));-    PCBServer.PostProcess;-    gInputError := '';-    Board.ViewManager_FullUpdate;-    Result := '{' + JRaw('ok', JBool(N >= 3)) + ',' + JRaw('vertices', IntToStr(N)) + '}';-End;--{ set_rule: write ONE design rule kind on the current board, every rule of that kind (each scope-  keeps its own scope; the values are the fab's floor). One kind per call, so a property this-  Altium build does not know fails alone. kind = clearance | width | via | hole | ring |-  holetohole | edge. Values are RAW units: a, b, c (min / preferred / max where they apply),-  d, e, f (via drill min / preferred / max). }-Function CmdSetRule(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    BIt   : IPCB_BoardIterator;-    Rule  : IPCB_Rule;-    Kind  : String;-    A, B, C, D, E, F, N, Want : Integer;-    Created : Boolean;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    Created := False;-    Kind := Param(Lines, 'kind');-    A := StrToIntDef(Param(Lines, 'a'), 0); B := StrToIntDef(Param(Lines, 'b'), 0); C := StrToIntDef(Param(Lines, 'c'), 0);-    D := StrToIntDef(Param(Lines, 'd'), 0); E := StrToIntDef(Param(Lines, 'e'), 0); F := StrToIntDef(Param(Lines, 'f'), 0);-    If Kind = 'clearance' Then Want := eRule_Clearance-    Else If Kind = 'width' Then Want := eRule_MaxMinWidth-    Else If Kind = 'via' Then Want := eRule_RoutingViaStyle-    Else If Kind = 'hole' Then Want := eRule_MaxMinHoleSize-    Else If Kind = 'ring' Then Want := eRule_MinimumAnnularRing-    Else If Kind = 'holetohole' Then Want := eRule_HoleToHoleClearance-    Else If Kind = 'edge' Then Want := eRule_BoardOutlineClearance-    Else If Kind = 'polyconnect' Then Want := eRule_PolygonConnectStyle-    Else Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','unknown rule kind: ' + Kind) + '}'; Exit; End;-    N := 0;-    PCBServer.PreProcess;-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eRuleObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Rule := BIt.FirstPCBObject;-    While Rule <> Nil Do-    Begin-        { polyconnect sets the board's catch-all (All / All) rule only: per-pour rules keep theirs }-        If (Rule.RuleKind = Want) And ((Kind <> 'polyconnect') Or ((Rule.Scope1Expression = 'All') And (Rule.Scope2Expression = 'All'))) Then-        Begin-            Rule.BeginModify;-            If Kind = 'clearance' Then Rule.Gap := A-            Else If Kind = 'width' Then-            Begin-                SetWidthAllLayers(Rule, A, B, C);-            End-            Else If Kind = 'via' Then-            Begin-                Rule.MinWidth := A; Rule.PreferedWidth := B; Rule.MaxWidth := C;-                Rule.MinHoleWidth := D; Rule.PreferedHoleWidth := E; Rule.MaxHoleWidth := F;-            End-            Else If Kind = 'hole' Then Begin Rule.MinLimit := A; Rule.MaxLimit := C; End-            Else If Kind = 'ring' Then Rule.Minimum := A-            Else If Kind = 'holetohole' Then Rule.Gap := A-            Else If Kind = 'edge' Then-            Begin-                Rule.Gap := A;-                { the fab's edge clearance is copper to edge (KiCad's edge_clearance): an Adom edge rule an-                  earlier bridge scoped to All also caught silkscreen (adom/altium-bridge#26) }-                If (Rule.Name = 'Adom edge') And (Rule.Scope1Expression = 'All') Then Rule.Scope1Expression := 'OnCopper';-            End-            Else If Kind = 'polyconnect' Then-            Begin-                { A: 1 direct, 2 none, else relief with B spoke width (raw) and C spokes }-                If A = 1 Then Rule.ConnectStyle := eDirectConnectToPlane-                Else If A = 2 Then Rule.ConnectStyle := eNoConnect-                Else-                Begin-                    Rule.ConnectStyle := eReliefConnectToPlane;-                    If B > 0 Then Rule.ReliefConductorWidth := B;-                    If C > 0 Then Rule.ReliefEntries := C;-                End;-            End;-            Rule.EndModify;-            N := N + 1;-        End;-        Rule := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-    { a board with no rule of this kind (a new board has no annular-ring or edge rule) gets one,-      scoped to everything }-    If N = 0 Then-    Begin-        Rule := PCBServer.PCBRuleFactory(Want);-        Rule.Name := 'Adom ' + Kind;-        Rule.Scope1Expression := 'All';-        { edge: copper to the board edge, as the fab pack means it; silkscreen keeps its own rules }-        If Kind = 'edge' Then Rule.Scope1Expression := 'OnCopper';-        Rule.Scope2Expression := 'All';-        If Kind = 'ring' Then Rule.Minimum := A-        Else If (Kind = 'edge') Or (Kind = 'clearance') Or (Kind = 'holetohole') Then Rule.Gap := A-        Else If Kind = 'hole' Then Begin Rule.MinLimit := A; Rule.MaxLimit := C; End;-        Board.AddPCBObject(Rule);-        PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Rule.I_ObjectAddress);-        N := 1;-        Created := True;-    End;-    PCBServer.PostProcess;-    Result := '{' + JRaw('ok','true') + ',' + JStr('kind', Kind) + ',' + JRaw('rulesWritten', IntToStr(N)) + ',' + JRaw('created', JBool(Created)) + '}';-End;--{ zoom_rect: frame the current board's view on a rectangle, RAW units (l b r t). }-Function CmdZoomRect(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    Board.GraphicalView_ZoomOnRect(StrToIntDef(Param(Lines, 'l'), 0), StrToIntDef(Param(Lines, 'b'), 0), StrToIntDef(Param(Lines, 'r'), 0), StrToIntDef(Param(Lines, 't'), 0));-    Result := '{' + JRaw('ok','true') + '}';-End;--{ add_rule: a SCOPED rule (net, net class, any query) the fab-wide set_rule cannot express.-  kind clearance (a = gap) | width (a min, b preferred, c max), raw units; name is 'Adom <name>'-  and a second add with the same name replaces it. Lands at priority 1 (Altium puts a new rule-  first), so it outranks the board's catch-all of its kind. }-Function CmdAddRule(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    Rule  : IPCB_Rule;-    Kind, Name : String;-    A, B, C, D, E, G, U, L, Want : Integer;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    Kind := Param(Lines, 'kind');-    Name := 'Adom ' + Param(Lines, 'name');-    A := StrToIntDef(Param(Lines, 'a'), 0); B := StrToIntDef(Param(Lines, 'b'), 0); C := StrToIntDef(Param(Lines, 'c'), 0);-    D := StrToIntDef(Param(Lines, 'd'), 0); E := StrToIntDef(Param(Lines, 'e'), 0); G := StrToIntDef(Param(Lines, 'g'), 0); U := StrToIntDef(Param(Lines, 'u'), 0);-    If Kind = 'clearance' Then Want := eRule_Clearance-    Else If Kind = 'width' Then Want := eRule_MaxMinWidth-    Else If Kind = 'length' Then Want := eRule_MaxMinLength-    Else If Kind = 'matched' Then Want := eRule_MatchedLengths-    Else If Kind = 'diffpair' Then Want := eRule_DifferentialPairsRouting-    Else If Kind = 'via' Then Want := eRule_RoutingViaStyle-    Else If Kind = 'polyconnect' Then Want := eRule_PolygonConnectStyle-    Else Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','unknown rule kind: ' + Kind) + '}'; Exit; End;-    PCBServer.PreProcess;-    Rule := FindRuleNamed(Board, Want, Name);-    If Rule <> Nil Then Board.RemovePCBObject(Rule);-    If Param(Lines, 'remove') = '1' Then-    Begin-        PCBServer.PostProcess;-        Result := '{' + JRaw('ok','true') + ',' + JStr('name', Name) + ',' + JRaw('removed', JBool(Rule <> Nil)) + '}';-        Exit;-    End;-    Rule := PCBServer.PCBRuleFactory(Want);-    Rule.Name := Name;-    Rule.Comment := Param(Lines, 'comment');-    Rule.Scope1Expression := Param(Lines, 'scope1');-    Rule.Scope2Expression := Param(Lines, 'scope2');-    { measured on Develop 2026-10-23: the length, matched-length, diff-pair and via-style properties below; a diff pair's-      widths and gaps are per layer (setting one layer moved only the descriptor's minimum) }-    If Kind = 'clearance' Then Rule.Gap := A-    Else If Kind = 'length' Then Begin Rule.MinLimit := A; Rule.MaxLimit := C; End-    Else If Kind = 'matched' Then Rule.Tolerance := A-    Else If Kind = 'diffpair' Then-    Begin-        For L := eTopLayer To eBottomLayer Do-        Begin-            Rule.MinWidth[L] := A; Rule.PreferedWidth[L] := B; Rule.MaxWidth[L] := C;-            Rule.MinGap[L] := D; Rule.PreferedGap[L] := E; Rule.MaxGap[L] := G;-        End;-        If U > 0 Then Rule.MaxUncoupledLength := U;-    End-    Else If Kind = 'polyconnect' Then-    Begin-        { a = style (0 relief, 1 direct, 2 none), b = relief conductor width, c = relief entries; the same properties-          set_design_rules' board-wide polygon connect uses }-        If A = 1 Then Rule.ConnectStyle := eDirectConnectToPlane-        Else If A = 2 Then Rule.ConnectStyle := eNoConnect-        Else-        Begin-            Rule.ConnectStyle := eReliefConnectToPlane;-            If B > 0 Then Rule.ReliefConductorWidth := B;-            If C > 0 Then Rule.ReliefEntries := C;-        End;-    End-    Else If Kind = 'via' Then-    Begin-        Rule.MinWidth := A; Rule.PreferedWidth := B; Rule.MaxWidth := C;-        Rule.MinHoleWidth := D; Rule.PreferedHoleWidth := E; Rule.MaxHoleWidth := G;-    End-    Else-    Begin-        SetWidthAllLayers(Rule, A, B, C);-    End;-    Board.AddPCBObject(Rule);-    PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Rule.I_ObjectAddress);-    PCBServer.PostProcess;-    Result := '{' + JRaw('ok','true') + ',' + JStr('name', Name) + ',' + JRaw('priority', IntToStr(Rule.Priority)) + ',' + JStr('descriptor', Rule.Descriptor) + '}';-End;--{ build_schematic: one sheet drawn from a netlist with the project library's symbols. The SchLib-  must be the current document. Each part is a fresh component with its symbol's display-mode-0-  primitives copied in (place_on_sch's measured lane: ISch_Component.Replicate does not carry the-  pins). Parts stand in one column per block, sized from the symbols; every pin on a net gets a-  short wire and a net label at its end, so the sheet compiles to the netlist.-    payload  C|ref|libref|block|comment|footprint      P|ref|pin|net      (param pcblib: the footprints' .PcbLib)-  Coordinates in mil. }-Function LibCmpOf(SLib : ISch_Lib; LibRef : String) : ISch_Component;-Var-    Iter : ISch_Iterator;-    C    : ISch_Component;-Begin-    Result := Nil;-    Iter := SLib.SchLibIterator_Create;-    Iter.AddFilter_ObjectSet(MkSet(eSchComponent));-    C := Iter.FirstSchObject;-    While C <> Nil Do-    Begin-        If UpperCase(C.LibReference) = UpperCase(LibRef) Then Begin Result := C; Break; End;-        C := Iter.NextSchObject;-    End;-    SLib.SchIterator_Destroy(Iter);-End;--Function IsDrawn(Prim : ISch_GraphicalObject) : Boolean;-Begin-    Result := ((Prim.ObjectID = ePin) Or (Prim.ObjectID = eRectangle) Or (Prim.ObjectID = eLine) Or-        (Prim.ObjectID = eArc) Or (Prim.ObjectID = eEllipse) Or (Prim.ObjectID = ePolygon) Or-        (Prim.ObjectID = ePolyline) Or (Prim.ObjectID = eLabel)) And (Prim.OwnerPartDisplayMode = 0);-End;--{ ------------------------------------------------------------ schematic editing (0.2.57) --- }--{ the placed parts of the current sheet with this designator (every part of a multi-part component) }-Function SheetCompsOf(Doc : ISch_Document; Des : String) : TInterfaceList;-Var-    It : ISch_Iterator;-    C  : ISch_Component;-Begin-    Result := TInterfaceList.Create;-    It := Doc.SchIterator_Create;-    It.AddFilter_ObjectSet(MkSet(eSchComponent));-    C := It.FirstSchObject;-    While C <> Nil Do-    Begin-        If UpperCase(C.Designator.Text) = UpperCase(Des) Then Result.Add(C);-        C := It.NextSchObject;-    End;-    Doc.SchIterator_Destroy(It);-End;--{ a GND-like net gets a ground symbol, anything else a bar }-Function PowerStyleOf(Net : String) : Integer;-Var-    U : String;-Begin-    U := UpperCase(Net);-    If (U = 'GND') Or (U = 'AGND') Or (U = 'DGND') Or (U = 'PGND') Or (U = 'GNDA') Or (U = 'GNDD') Or (U = 'VSS') Or (U = '0V') Then-        Result := ePowerGndPower-    Else If (U = 'EARTH') Or (U = 'PE') Or (U = 'CHASSIS') Then-        Result := ePowerGndEarth-    Else-        Result := ePowerBar;-End;--{ sch_connect: a wire stub and a net label (kind=label), power port (kind=power) or nothing (kind=wire)-  at the outer end of pins of placed parts on the current sheet.-  payload lines: designator|pin (number or name)|net|kind|stubMil-  One undo step. Answers per line: ok, or why not. }-{ sheet_symbol: a sheet symbol on the CURRENT sheet for a child sheet. Params x, y (mil, its top-left corner), w, h-  (mil), name, file. Payload: one entry per line, name|side(left/right)|io(input/output/bidirectional/passive)|net|-  stubMil. Altium never places an entry by itself (its Location stays 0,0 and it draws nowhere, measured on Develop-  2026-10-23): each one is put on its edge here, 100 mil apart from 100 mil down. A net gives the entry a wire stub out-  of the symbol and a net label on it. }-Function CmdSheetSymbol(Lines : TStringList; Payload : String) : String;-Var-    Doc   : ISch_Document;-    SS    : ISch_SheetSymbol;-    E     : ISch_SheetEntry;-    W     : ISch_Wire;-    NL    : ISch_NetLabel;-    Ops   : TStringList;-    Op, Nm, Side, IOS, Net, S : String;-    I, X, Y, SW, SH, NL0, NR, Dist, EX, EY, TX, Stub : Integer;-Begin-    Doc := SchServer.GetCurrentSchDocument;-    If (Doc = Nil) Or (Doc.ObjectID = eSchLib) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_schematic') + ',' + JStr('error','no current schematic sheet') + '}'; Exit; End;-    X := MilsToCoord(AdomInt(Param(Lines, 'x'))); Y := MilsToCoord(AdomInt(Param(Lines, 'y')));-    SW := MilsToCoord(AdomInt(Param(Lines, 'w'))); SH := MilsToCoord(AdomInt(Param(Lines, 'h')));-    Ops := TStringList.Create; Ops.Text := Payload;-    SchServer.ProcessControl.PreProcess(Doc, '');-    SS := SchServer.SchObjectFactory(eSheetSymbol, eCreate_Default);-    SS.Location := Point(X, Y);-    SS.XSize := SW; SS.YSize := SH;-    SS.SheetName.Text := Param(Lines, 'name');-    SS.SheetName.Location := Point(X, Y + MilsToCoord(100));-    SS.SheetFileName.Text := Param(Lines, 'file');-    SS.SheetFileName.Location := Point(X, Y - SH - MilsToCoord(150));-    S := ''; NL0 := 0; NR := 0;-    For I := 0 To Ops.Count - 1 Do-    Begin-        Op := Ops[I];-        If Trim(Op) = '' Then Continue;-        Nm := FieldP(Op, 1); Side := LowerCase(FieldP(Op, 2)); IOS := LowerCase(FieldP(Op, 3)); Net := FieldP(Op, 4);-        Stub := AdomInt(FieldP(Op, 5)); If Stub <= 0 Then Stub := 300;-        E := SchServer.SchObjectFactory(eSheetEntry, eCreate_Default);-        E.Name := Nm;-        If IOS = 'input' Then E.IOType := ePortInput-        Else If IOS = 'output' Then E.IOType := ePortOutput-        Else If (IOS = 'bidirectional') Or (IOS = 'bidir') Then E.IOType := ePortBidirectional-        Else E.IOType := ePortUnspecified;-        If Side = 'right' Then Begin NR := NR + 1; Dist := MilsToCoord(100 * NR); E.Side := eRightSide; EX := X + SW; End-        Else Begin NL0 := NL0 + 1; Dist := MilsToCoord(100 * NL0); E.Side := eLeftSide; EX := X; End;-        EY := Y - Dist;-        E.DistanceFromTop := Dist;-        E.Location := Point(EX, EY);-        SS.AddSchObject(E);-        If Net <> '' Then-        Begin-            If Side = 'right' Then TX := EX + MilsToCoord(Stub) Else TX := EX - MilsToCoord(Stub);-            W := SchServer.SchObjectFactory(eWire, eCreate_Default);-            W.Location := Point(EX, EY);-            W.InsertVertex := 1; W.SetState_Vertex(1, Point(EX, EY));-            W.InsertVertex := 2; W.SetState_Vertex(2, Point(TX, EY));-            W.LineWidth := eSmall;-            Doc.RegisterSchObjectInContainer(W);-            SchServer.RobotManager.SendMessage(Doc.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, W.I_ObjectAddress);-            NL := SchServer.SchObjectFactory(eNetLabel, eCreate_Default);-            NL.Text := Net;-            NL.Location := Point(TX, EY);-            If Side <> 'right' Then NL.Justification := eJustify_BottomRight;-            Doc.RegisterSchObjectInContainer(NL);-            SchServer.RobotManager.SendMessage(Doc.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, NL.I_ObjectAddress);-        End;-        If S <> '' Then S := S + ',';-        S := S + '{' + JStr('name', Nm) + ',' + JStr('side', Side) + ',' + JStr('io', IOS) + ',' + JStr('net', Net) + ',' +-            JRaw('atMil', '[' + IntToStr(Round(CoordToMils(EX))) + ',' + IntToStr(Round(CoordToMils(EY))) + ']') + '}';-    End;-    Doc.RegisterSchObjectInContainer(SS);-    SchServer.RobotManager.SendMessage(Doc.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, SS.I_ObjectAddress);-    SchServer.ProcessControl.PostProcess(Doc, '');-    MarkSchModified(Doc);-    Doc.GraphicallyInvalidate;-    Ops.Free;-    Result := '{' + JRaw('ok','true') + ',' + JStr('name', SS.SheetName.Text) + ',' + JStr('file', SS.SheetFileName.Text) + ',' +-        JRaw('entries', '[' + S + ']') + '}';-End;--{ sheet_ports: ports on the CURRENT sheet for the names it lacks. Payload name|io per line. Inputs (and unspecified)-  stack down the left margin, outputs down the right, 200 mil apart, each with a 300 mil wire stub ready to join;-  names that already have a port are reported, not doubled. Params: left, right, top (mil). }-Function CmdSheetPorts(Lines : TStringList; Payload : String) : String;-Var-    Doc   : ISch_Document;-    It    : ISch_Iterator;-    Prt, Old : ISch_Port;-    W     : ISch_Wire;-    Ops, Have : TStringList;-    Op, Nm, IOS, S, Skip : String;-    I, LX, RX, TY, NL0, NR, PX, PY, WX : Integer;-Begin-    Doc := SchServer.GetCurrentSchDocument;-    If (Doc = Nil) Or (Doc.ObjectID = eSchLib) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_schematic') + ',' + JStr('error','no current schematic sheet') + '}'; Exit; End;-    Have := TStringList.Create;-    It := Doc.SchIterator_Create; It.AddFilter_ObjectSet(MkSet(ePort));-    Old := It.FirstSchObject;-    While Old <> Nil Do Begin Have.Add(UpperCase(Old.Name)); Old := It.NextSchObject; End;-    Doc.SchIterator_Destroy(It);-    LX := MilsToCoord(AdomInt(Param(Lines, 'left'))); RX := MilsToCoord(AdomInt(Param(Lines, 'right'))); TY := MilsToCoord(AdomInt(Param(Lines, 'top')));-    Ops := TStringList.Create; Ops.Text := Payload;-    SchServer.ProcessControl.PreProcess(Doc, '');-    S := ''; Skip := ''; NL0 := 0; NR := 0;-    For I := 0 To Ops.Count - 1 Do-    Begin-        Op := Ops[I];-        If Trim(Op) = '' Then Continue;-        Nm := FieldP(Op, 1); IOS := LowerCase(FieldP(Op, 2));-        If Have.IndexOf(UpperCase(Nm)) >= 0 Then-        Begin-            If Skip <> '' Then Skip := Skip + ',';-            Skip := Skip + '"' + JsonEsc(Nm) + '"';-            Continue;-        End;-        Prt := SchServer.SchObjectFactory(ePort, eCreate_Default);-        Prt.Name := Nm;-        Prt.Width := MilsToCoord(600);-        If IOS = 'input' Then Prt.IOType := ePortInput-        Else If IOS = 'output' Then Prt.IOType := ePortOutput-        Else If (IOS = 'bidirectional') Or (IOS = 'bidir') Then Prt.IOType := ePortBidirectional-        Else Prt.IOType := ePortUnspecified;-        If IOS = 'output' Then-        Begin-            PY := TY - MilsToCoord(200 * NR); NR := NR + 1;-            PX := RX - Prt.Width; Prt.Style := ePortRight; WX := PX - MilsToCoord(300);-            W := SchServer.SchObjectFactory(eWire, eCreate_Default);-            W.Location := Point(WX, PY);-            W.InsertVertex := 1; W.SetState_Vertex(1, Point(WX, PY));-            W.InsertVertex := 2; W.SetState_Vertex(2, Point(PX, PY));-        End-        Else-        Begin-            PY := TY - MilsToCoord(200 * NL0); NL0 := NL0 + 1;-            PX := LX; Prt.Style := ePortRight; WX := PX + Prt.Width + MilsToCoord(300);-            W := SchServer.SchObjectFactory(eWire, eCreate_Default);-            W.Location := Point(PX + Prt.Width, PY);-            W.InsertVertex := 1; W.SetState_Vertex(1, Point(PX + Prt.Width, PY));-            W.InsertVertex := 2; W.SetState_Vertex(2, Point(WX, PY));-        End;-        Prt.Location := Point(PX, PY);-        W.LineWidth := eSmall;-        Doc.RegisterSchObjectInContainer(Prt);-        SchServer.RobotManager.SendMessage(Doc.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, Prt.I_ObjectAddress);-        Doc.RegisterSchObjectInContainer(W);-        SchServer.RobotManager.SendMessage(Doc.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, W.I_ObjectAddress);-        If S <> '' Then S := S + ',';-        S := S + '{' + JStr('name', Nm) + ',' + JStr('io', IOS) + ',' + JRaw('atMil', '[' + IntToStr(Round(CoordToMils(PX))) + ',' + IntToStr(Round(CoordToMils(PY))) + ']') +-            ',' + JRaw('stubEndMil', '[' + IntToStr(Round(CoordToMils(WX))) + ',' + IntToStr(Round(CoordToMils(PY))) + ']') + '}';-    End;-    SchServer.ProcessControl.PostProcess(Doc, '');-    MarkSchModified(Doc);-    Doc.GraphicallyInvalidate;-    Ops.Free; Have.Free;-    Result := '{' + JRaw('ok','true') + ',' + JRaw('placed', '[' + S + ']') + ',' + JRaw('alreadyThere', '[' + Skip + ']') + '}';-End;--{ comp_classes: component classes on the current board, with an optional room (a confinement rule named-  'Adom room <class>' scoped InComponentClass, measured on Develop 2026-10-23). op=list | set | remove; name; members-  (designators, ';'); room=1 with rx1, ry1, rx2, ry2 (internal units, absolute like routing_state and edit_board). }-Function CmdCompClasses(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    BIt, CIt : IPCB_BoardIterator;-    Cls, Old : IPCB_ObjectClass;-    Rule, OldRoom : IPCB_Rule;-    Comp  : IPCB_Component;-    Op, Name, Mem, D, S, M, Missing, Q : String;-    P, K  : Integer;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_board') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    Op := Param(Lines, 'op'); Name := Param(Lines, 'name');-    Old := Nil; OldRoom := Nil; S := '';-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eClassObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Cls := BIt.FirstPCBObject;-    While Cls <> Nil Do-    Begin-        If Cls.MemberKind = eClassMemberKind_Component Then-        Begin-            If (Name <> '') And (UpperCase(Cls.Name) = UpperCase(Name)) Then Old := Cls;-            If (Op = 'list') And (Not Cls.SuperClass) Then-            Begin-                M := '';-                CIt := Board.BoardIterator_Create;-                CIt.AddFilter_ObjectSet(MkSet(eComponentObject));-                CIt.AddFilter_LayerSet(AllLayers);-                CIt.AddFilter_Method(eProcessAll);-                Comp := CIt.FirstPCBObject;-                While Comp <> Nil Do-                Begin-                    If Cls.IsMember(Comp) Then Begin If M <> '' Then M := M + ','; M := M + '"' + JsonEsc(Comp.Name.Text) + '"'; End;-                    Comp := CIt.NextPCBObject;-                End;-                Board.BoardIterator_Destroy(CIt);-                If S <> '' Then S := S + ',';-                S := S + '{' + JStr('name', Cls.Name) + ',' + JRaw('members', '[' + M + ']') + '}';-            End;-        End;-        Cls := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-    If Op = 'list' Then Begin Result := '{' + JRaw('ok','true') + ',' + JRaw('classes', '[' + S + ']') + '}'; Exit; End;-    If Name = '' Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','bad_args') + ',' + JStr('error','name is required') + '}'; Exit; End;-    OldRoom := FindRuleNamed(Board, eRule_ConfinementConstraint, 'Adom room ' + Name);-    PCBServer.PreProcess;-    If Old <> Nil Then Board.RemovePCBObject(Old);-    If OldRoom <> Nil Then Board.RemovePCBObject(OldRoom);-    If Op = 'remove' Then-    Begin-        PCBServer.PostProcess;-        Result := '{' + JRaw('ok','true') + ',' + JStr('class', Name) + ',' + JRaw('removed', JBool(Old <> Nil)) + ',' + JRaw('roomRemoved', JBool(OldRoom <> Nil)) + '}';-        Exit;-    End;-    Cls := PCBServer.PCBClassFactoryByClassMember(eClassMemberKind_Component);-    Cls.Name := Name;-    Mem := Param(Lines, 'members'); K := 0; Missing := ''; M := '';-    While Mem <> '' Do-    Begin-        P := Pos(';', Mem);-        If P = 0 Then Begin D := Mem; Mem := ''; End Else Begin D := Copy(Mem, 1, P - 1); Mem := Copy(Mem, P + 1, Length(Mem)); End;-        If D <> '' Then-        Begin-            Comp := FindCompByDes(Board, D);-            If Comp = Nil Then Begin If Missing <> '' Then Missing := Missing + ','; Missing := Missing + '"' + JsonEsc(D) + '"'; End-            Else Begin Cls.AddMemberByName(Comp.Name.Text); K := K + 1; If M <> '' Then M := M + ','; M := M + '"' + JsonEsc(Comp.Name.Text) + '"'; End;-        End;-    End;-    Board.AddPCBObject(Cls);-    PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Cls.I_ObjectAddress);-    S := 'null';-    If Param(Lines, 'room') = '1' Then-    Begin-        Q := Chr(39);-        Rule := PCBServer.PCBRuleFactory(eRule_ConfinementConstraint);-        Rule.Name := 'Adom room ' + Name;-        Rule.Scope1Expression := 'InComponentClass(' + Q + Name + Q + ')';-        Rule.Kind := eConfineIn;-        Rule.Layer := eTopLayer;-        Rule.BoundingRect := RectToCoordRect(Rect(AdomInt(Param(Lines, 'rx1')), AdomInt(Param(Lines, 'ry2')), AdomInt(Param(Lines, 'rx2')), AdomInt(Param(Lines, 'ry1'))));-        Board.AddPCBObject(Rule);-        PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Rule.I_ObjectAddress);-        S := '"' + JsonEsc(Rule.Descriptor) + '"';-    End;-    PCBServer.PostProcess;-    Result := '{' + JRaw('ok','true') + ',' + JStr('class', Name) + ',' + JRaw('members', '[' + M + ']') + ',' + JRaw('missing', '[' + Missing + ']') + ',' + JRaw('room', S) + '}';-End;--{ panelize: a new board holding an embedded board array of the CURRENT board (IPCB_EmbeddedBoard, measured on Develop-  2026-10-23). Params: path (the source .PcbDoc), panel (the new .PcbDoc), rows, cols, colStep, rowStep (internal-  units, board size + gap), bx0, by0 (the source outline's lower-left, absolute), rail. The array's first board lands-  with its outline corner at (panel origin + rail). }-Function CmdPanelize(Lines : TStringList) : String;-Var-    Src, PB : IPCB_Board;-    SD      : IServerDocument;-    E       : IPCB_EmbeddedBoard;-    OffX, OffY, Rail : Integer;-Begin-    Src := PCBServer.GetCurrentPCBBoard;-    If Src = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','the source board is not the current document') + '}'; Exit; End;-    OffX := AdomInt(Param(Lines, 'bx0')) - Src.XOrigin;-    OffY := AdomInt(Param(Lines, 'by0')) - Src.YOrigin;-    Rail := AdomInt(Param(Lines, 'rail'));-    SD := CreateNewDocumentFromDocumentKind('PCB');-    PB := PCBServer.GetCurrentPCBBoard;-    If (PB = Nil) Or (PB = Src) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','the new panel board did not become current') + '}'; Exit; End;-    PCBServer.PreProcess;-    E := PCBServer.PCBObjectFactory(eEmbeddedBoardObject, eNoDimension, eCreate_Default);-    E.DocumentPath := Param(Lines, 'path');-    E.RowCount := AdomInt(Param(Lines, 'rows'));-    E.ColCount := AdomInt(Param(Lines, 'cols'));-    E.ColSpacing := AdomInt(Param(Lines, 'colStep'));-    E.RowSpacing := AdomInt(Param(Lines, 'rowStep'));-    E.XLocation := PB.XOrigin + Rail - OffX;-    E.YLocation := PB.YOrigin + Rail - OffY;-    PB.AddPCBObject(E);-    PCBServer.SendMessageToRobots(PB.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, E.I_ObjectAddress);-    PCBServer.PostProcess;-    PB.ViewManager_FullUpdate;-    FreePath(Param(Lines, 'panel'));-    SD.SetFileName(Param(Lines, 'panel'));-    SD.DoFileSave('');-    Result := '{' + JRaw('ok','true') + ',' + JStr('panel', Param(Lines, 'panel')) + ',' + JRaw('originX', IntToStr(PB.XOrigin)) + ',' + JRaw('originY', IntToStr(PB.YOrigin)) + ',' +-        JRaw('rows', IntToStr(E.RowCount)) + ',' + JRaw('cols', IntToStr(E.ColCount)) + '}';-End;--{ replace_fp: put a library's current footprint in place of a placed part's (update_footprints_from_library), keeping-  where it sits (position, rotation, side), its designator and comment and where they stand, its pad nets by pad name,-  its schematic link (Source* ids) and its lock. ref= libraryPath= footprint=. One undo step. The new copy is placed-  by its pads: the library pad matching a placed pad lands where that pad would (the library's origin moves with its-  current footprint, measured 2026-10-27), then the side and rotation are set as move_parts sets them. }-Function CmdReplaceFp(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    PLib  : IPCB_Library;-    LibFp : IPCB_LibComponent;-    Old, Inst : IPCB_Component;-    GIt   : IPCB_GroupIterator;-    Prim, Cpy : IPCB_Primitive;-    Pad, LP : IPCB_Pad;-    Net   : IPCB_Net;-    NetMap : TStringList;-    I, CX, CY, OX, OY, DX, DY, NX, NY, CmX, CmY, Nets : Integer;-    Rot : Double;-    Bottom, Locked, NOn, COn, Found : Boolean;-    Des, Cmt, SrcU, SrcD, SrcL, SrcC, SrcF, SrcH : String;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_board') + ',' + JStr('error','no current PCB board') + '}'; Exit; End;-    Old := FindCompByDes(Board, Param(Lines, 'ref'));-    If Old = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_such_part') + ',' + JStr('error','no part ' + Param(Lines, 'ref')) + '}'; Exit; End;-    PLib := PCBServer.LoadPCBLibraryByPath(Param(Lines, 'libraryPath'));-    If PLib = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','library_not_found') + ',' + JStr('error','could not load ' + Param(Lines, 'libraryPath')) + '}'; Exit; End;-    LibFp := Nil;-    For I := 0 To PLib.ComponentCount - 1 Do-        If UpperCase(PLib.GetComponent(I).Name) = UpperCase(Param(Lines, 'footprint')) Then LibFp := PLib.GetComponent(I);-    If LibFp = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','footprint_not_found') + ',' + JStr('error', Param(Lines, 'footprint') + ' is not in ' + Param(Lines, 'libraryPath')) + '}'; Exit; End;-    If PLib.CurrentComponent <> LibFp Then PLib.CurrentComponent := LibFp;-    OX := PLib.Board.XOrigin; OY := PLib.Board.YOrigin;-    { everything the part carries that the library does not }-    CX := Old.x; CY := Old.y; Rot := Old.Rotation; Bottom := Old.Layer = eBottomLayer; Locked := Not Old.Moveable;-    Des := Old.Name.Text; Cmt := Old.Comment.Text; NOn := Old.NameOn; COn := Old.CommentOn;-    NX := Old.Name.XLocation; NY := Old.Name.YLocation; CmX := Old.Comment.XLocation; CmY := Old.Comment.YLocation;-    SrcU := ''; SrcD := ''; SrcL := ''; SrcC := ''; SrcF := ''; SrcH := '';-    Try SrcU := Old.SourceUniqueId; SrcD := Old.SourceDesignator; SrcL := Old.SourceLibReference; SrcC := Old.SourceComponentLibrary; SrcF := Old.SourceFootprintLibrary; SrcH := Old.SourceHierarchicalPath; Except End;-    { the footprint now comes from this library: name it, as a part placed from it is named (the next plan tries it first) }-    SrcF := Param(Lines, 'libraryPath');-    I := Length(SrcF);-    While (I > 0) And (Copy(SrcF, I, 1) <> '\') Do I := I - 1;-    SrcF := Copy(SrcF, I + 1, Length(SrcF));-    NetMap := TStringList.Create;-    GIt := Old.GroupIterator_Create;-    GIt.AddFilter_ObjectSet(MkSet(ePadObject)); GIt.AddFilter_LayerSet(AllLayers);-    Pad := GIt.FirstPCBObject;-    While Pad <> Nil Do-    Begin-        If (Pad.Net <> Nil) And (NetMap.IndexOfName(Pad.Name) < 0) Then NetMap.Add(Pad.Name + '=' + Pad.Net.Name);-        Pad := GIt.NextPCBObject;-    End;-    Old.GroupIterator_Destroy(GIt);-    PCBServer.PreProcess;-    Inst := PCBServer.PCBObjectFactory(eComponentObject, eNoDimension, eCreate_Default);-    Inst.Layer := eTopLayer;-    GIt := LibFp.GroupIterator_Create;-    GIt.AddFilter_LayerSet(AllLayers);-    Prim := GIt.FirstPCBObject;-    While Prim <> Nil Do Begin Cpy := Prim.Replicate; Inst.AddPCBObject(Cpy); Prim := GIt.NextPCBObject; End;-    LibFp.GroupIterator_Destroy(GIt);-    Board.AddPCBObject(Inst);-    PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Inst.I_ObjectAddress);-    Inst.x := CX; Inst.y := CY;-    { put the library's origin on the part's: a pad of the copy goes where its library pad sits from the origin }-    Found := False; DX := 0; DY := 0;-    GIt := LibFp.GroupIterator_Create;-    GIt.AddFilter_ObjectSet(MkSet(ePadObject)); GIt.AddFilter_LayerSet(AllLayers);-    LP := GIt.FirstPCBObject;-    LibFp.GroupIterator_Destroy(GIt);-    If LP <> Nil Then-    Begin-        GIt := Inst.GroupIterator_Create;-        GIt.AddFilter_ObjectSet(MkSet(ePadObject)); GIt.AddFilter_LayerSet(AllLayers);-        Pad := GIt.FirstPCBObject;-        While (Pad <> Nil) And (Not Found) Do-        Begin-            If (Pad.Name = LP.Name) And (Pad.TopXSize = LP.TopXSize) Then Begin DX := (LP.x - OX + CX) - Pad.x; DY := (LP.y - OY + CY) - Pad.y; Found := True; End;-            Pad := GIt.NextPCBObject;-        End;-        Inst.GroupIterator_Destroy(GIt);-    End;-    If (DX <> 0) Or (DY <> 0) Then-    Begin-        GIt := Inst.GroupIterator_Create;-        GIt.AddFilter_LayerSet(AllLayers);-        Prim := GIt.FirstPCBObject;-        While Prim <> Nil Do Begin If Prim.ObjectId <> eTextObject Then Prim.MoveByXY(DX, DY); Prim := GIt.NextPCBObject; End;-        Inst.GroupIterator_Destroy(GIt);-    End;-    Inst.BeginModify;-    If Bottom Then Inst.Layer := eBottomLayer;-    Inst.Rotation := Rot;-    Inst.MoveToXY(CX, CY);-    Inst.Pattern := LibFp.Name;-    Inst.Name.Text := Des; Inst.Comment.Text := Cmt; Inst.NameOn := NOn; Inst.CommentOn := COn;-    Try Inst.SourceUniqueId := SrcU; Inst.SourceDesignator := SrcD; Inst.SourceLibReference := SrcL; Inst.SourceComponentLibrary := SrcC; Inst.SourceFootprintLibrary := SrcF; Inst.SourceHierarchicalPath := SrcH; Except End;-    Inst.EndModify;-    Inst.Name.MoveToXY(NX, NY); Inst.Comment.MoveToXY(CmX, CmY);-    { the nets, pad by pad name }-    Nets := 0;-    GIt := Inst.GroupIterator_Create;-    GIt.AddFilter_ObjectSet(MkSet(ePadObject)); GIt.AddFilter_LayerSet(AllLayers);-    Pad := GIt.FirstPCBObject;-    While Pad <> Nil Do-    Begin-        If NetMap.IndexOfName(Pad.Name) >= 0 Then-        Begin-            Net := FindNet(Board, NetMap.Values[Pad.Name]);-            If Net <> Nil Then Begin Pad.BeginModify; Pad.Net := Net; Pad.EndModify; Net.AddPCBObject(Pad); Nets := Nets + 1; End;-        End;-        Pad := GIt.NextPCBObject;-    End;-    Inst.GroupIterator_Destroy(GIt);-    Board.RemovePCBObject(Old);-    If Locked Then Inst.Moveable := False;-    PCBServer.PostProcess;-    Board.ViewManager_FullUpdate;-    Result := '{' + JRaw('ok','true') + ',' + JStr('ref', Des) + ',' + JStr('footprint', LibFp.Name) + ',' + JRaw('placedByPad', JBool(Found)) + ',' +-        JRaw('padsOnPart', IntToStr(GroupCount(Inst, ePadObject))) + ',' + JRaw('netsKept', IntToStr(Nets)) + ',' + JRaw('netsBefore', IntToStr(NetMap.Count)) + '}';-    NetMap.Free;-End;--{ bodies: every 3D body on the board as [component, componentLayer, overallHeight, standoffHeight, left, bottom, right,-  top, rotation*1000, outline] in internal units (the outline only for a part turned off the 90-degree grid). OverallHeight is the body's top above the board surface its part sits on and StandoffHeight-  its bottom (measured on the Bluetooth Sentinel example, Develop 2026-10-23); a part with several bodies has several-  rows. A body with no component (a free 3D body) is named '-'. }-Function CmdBodies(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    BIt   : IPCB_BoardIterator;-    Body  : IPCB_ComponentBody;-    R     : TCoordRect;-    S, Nm, Outl : String;-    Lyr, N, Rot : Integer;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_board') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    S := ''; N := 0;-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eComponentBodyObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Body := BIt.FirstPCBObject;-    While (Body <> Nil) And (N < 20000) Do-    Begin-        Nm := '-'; Lyr := 0; Rot := 0;-        If Body.Component <> Nil Then Begin Nm := Body.Component.Name.Text; Lyr := Body.Component.Layer; Rot := Round(Body.Component.Rotation * 1000); End;-        R := Body.BoundingRectangle;-        { the body's outline and its part's rotation: a part turned off the grid has a box far bigger than its body, and-          the standard compares turned bodies by their own rectangles (EDA Bridge Standard #26) }-        Outl := '[]';-        If (Rot Mod 90000) <> 0 Then-            Try Outl := ContourPts(Body.MainContour); Except Outl := '[]'; End;-        If N > 0 Then S := S + ',';-        S := S + '["' + JsonEsc(Nm) + '",' + IntToStr(Lyr) + ',' + IntToStr(Body.OverallHeight) + ',' + IntToStr(Body.StandoffHeight) + ',' +-             IntToStr(R.Left) + ',' + IntToStr(R.Bottom) + ',' + IntToStr(R.Right) + ',' + IntToStr(R.Top) + ',' + IntToStr(Rot) + ',' + Outl + ']';-        N := N + 1;-        Body := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-    Result := '{' + JRaw('ok','true') + ',' + JStr('file', Board.FileName) + ',' + JRaw('count', IntToStr(N)) + ',' + JRaw('bodies', '[' + S + ']') + '}';-End;--{ teardrop_count: how many primitives on the current board are teardrops (IPCB_Primitive.TearDrop, measured on-  Develop 2026-10-23). }-Function CmdTeardropCount(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    It    : IPCB_BoardIterator;-    P     : IPCB_Primitive;-    N     : Integer;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    N := 0;-    It := Board.BoardIterator_Create;-    It.AddFilter_ObjectSet(AllObjects); It.AddFilter_LayerSet(AllLayers); It.AddFilter_Method(eProcessAll);-    P := It.FirstPCBObject;-    While P <> Nil Do Begin If P.TearDrop Then N := N + 1; P := It.NextPCBObject; End;-    Board.BoardIterator_Destroy(It);-    Result := '{' + JRaw('ok','true') + ',' + JRaw('teardrops', IntToStr(N)) + '}';-End;--Function CmdSchConnect(Lines : TStringList; Payload : String) : String;-Var-    Doc   : ISch_Document;-    Ops, Comps : TStringList;-    CL    : TInterfaceList;-    C     : ISch_Component;-    PIt   : ISch_Iterator;-    Pin, Hit : ISch_Pin;-    W     : ISch_Wire;-    NL    : ISch_NetLabel;-    PO    : ISch_PowerObject;-    NE    : ISch_NoERC;-    Prt   : ISch_Port;-    IOS   : String;-    Op, Des, PinKey, Net, Kind, Res, S : String;-    I, J, Dir, HX, HY, EX, EY, Stub, NOk : Integer;-Begin-    Doc := SchServer.GetCurrentSchDocument;-    If (Doc = Nil) Or (Doc.ObjectID = eSchLib) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_schematic') + ',' + JStr('error','no current schematic sheet (open a .SchDoc first)') + '}'; Exit; End;-    Ops := TStringList.Create; Ops.Text := Payload;-    S := ''; NOk := 0;-    SchServer.ProcessControl.PreProcess(Doc, '');-    For I := 0 To Ops.Count - 1 Do-    Begin-        Op := Ops[I];-        If Trim(Op) = '' Then Continue;-        Des := FieldP(Op, 1); PinKey := FieldP(Op, 2); Net := FieldP(Op, 3); Kind := LowerCase(FieldP(Op, 4));-        Stub := StrToIntDef(FieldP(Op, 5), 200);-        If Kind = '' Then Kind := 'label';-        Res := '';-        Hit := Nil;-        CL := SheetCompsOf(Doc, Des);-        If CL.Count = 0 Then Res := 'designator not on this sheet'-        Else-        Begin-            For J := 0 To CL.Count - 1 Do-            Begin-                C := CL[J];-                PIt := C.SchIterator_Create;-                PIt.AddFilter_ObjectSet(MkSet(ePin));-                Pin := PIt.FirstSchObject;-                While Pin <> Nil Do-                Begin-                    If (Pin.OwnerPartId = C.CurrentPartID) Or (Pin.OwnerPartId = 0) Then-                        If (UpperCase(Pin.Designator) = UpperCase(PinKey)) Or ((Hit = Nil) And (UpperCase(Pin.Name) = UpperCase(PinKey))) Then-                            If (Hit = Nil) Or (UpperCase(Pin.Designator) = UpperCase(PinKey)) Then Hit := Pin;-                    Pin := PIt.NextSchObject;-                End;-                C.SchIterator_Destroy(PIt);-            End;-            If Hit = Nil Then Res := 'pin not found on ' + Des;-        End;-        CL.Free;-        If (Res = '') And (Net = '') And (Kind <> 'wire') And (Kind <> 'noerc') Then Res := 'no net given';-        If Res = '' Then-        Begin-            Dir := Hit.Orientation;-            HX := Hit.Location.X; HY := Hit.Location.Y;-            If Dir = 0 Then HX := HX + Hit.PinLength-            Else If Dir = 1 Then HY := HY + Hit.PinLength-            Else If Dir = 2 Then HX := HX - Hit.PinLength-            Else HY := HY - Hit.PinLength;-            EX := HX; EY := HY;-            If Kind = 'noerc' Then Stub := 0;-            If Dir = 0 Then EX := HX + MilsToCoord(Stub)-            Else If Dir = 1 Then EY := HY + MilsToCoord(Stub)-            Else If Dir = 2 Then EX := HX - MilsToCoord(Stub)-            Else EY := HY - MilsToCoord(Stub);-            If Kind = 'noerc' Then-            Begin-                { a No ERC marker on the pin's outer end: an intentionally unconnected pin, no wire, no net }-                NE := SchServer.SchObjectFactory(eNoERC, eCreate_Default);-                NE.Location := Point(HX, HY);-                Doc.RegisterSchObjectInContainer(NE);-                SchServer.RobotManager.SendMessage(Doc.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, NE.I_ObjectAddress);-            End-            Else-            Begin-                W := SchServer.SchObjectFactory(eWire, eCreate_Default);-                W.Location := Point(HX, HY);-                W.InsertVertex := 1;-                W.SetState_Vertex(1, Point(HX, HY));-                W.InsertVertex := 2;-                W.SetState_Vertex(2, Point(EX, EY));-                W.LineWidth := eSmall;-                Doc.RegisterSchObjectInContainer(W);-                SchServer.RobotManager.SendMessage(Doc.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, W.I_ObjectAddress);-            End;-            If Kind = 'power' Then-            Begin-                PO := SchServer.SchObjectFactory(ePowerObject, eCreate_Default);-                PO.Text := Net;-                PO.Location := Point(EX, EY);-                PO.Style := PowerStyleOf(Net);-                If Dir = 0 Then PO.Orientation := eRotate0-                Else If Dir = 1 Then PO.Orientation := eRotate90-                Else If Dir = 2 Then PO.Orientation := eRotate180-                Else PO.Orientation := eRotate270;-                PO.ShowNetName := True;-                Doc.RegisterSchObjectInContainer(PO);-                SchServer.RobotManager.SendMessage(Doc.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, PO.I_ObjectAddress);-            End-            Else If Kind = 'port' Then-            Begin-                { a port named after the net at the stub's end: the sheet's link to its sheet symbol's entry. Field 6-                  is the direction (input/output/bidirectional; default unspecified). It points away from the part. }-                Prt := SchServer.SchObjectFactory(ePort, eCreate_Default);-                Prt.Name := Net;-                Prt.Width := MilsToCoord(600);-                IOS := LowerCase(FieldP(Op, 6));-                If IOS = 'input' Then Prt.IOType := ePortInput-                Else If IOS = 'output' Then Prt.IOType := ePortOutput-                Else If (IOS = 'bidirectional') Or (IOS = 'bidir') Then Prt.IOType := ePortBidirectional-                Else Prt.IOType := ePortUnspecified;-                If Dir = 2 Then Begin Prt.Style := ePortLeft; Prt.Location := Point(EX - Prt.Width, EY); End-                Else Begin Prt.Style := ePortRight; Prt.Location := Point(EX, EY); End;-                Doc.RegisterSchObjectInContainer(Prt);-                SchServer.RobotManager.SendMessage(Doc.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, Prt.I_ObjectAddress);-            End-            Else If Kind = 'label' Then-            Begin-                NL := SchServer.SchObjectFactory(eNetLabel, eCreate_Default);-                NL.Text := Net;-                NL.Location := Point(EX, EY);-                If (Dir = 1) Or (Dir = 3) Then NL.Orientation := eRotate90;-                If (Dir = 2) Or (Dir = 3) Then NL.Justification := eJustify_BottomRight;-                Doc.RegisterSchObjectInContainer(NL);-                SchServer.RobotManager.SendMessage(Doc.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, NL.I_ObjectAddress);-            End;-            NOk := NOk + 1;-            Res := 'ok';-        End;-        If S <> '' Then S := S + ',';-        S := S + '{' + JStr('designator', Des) + ',' + JStr('pin', PinKey) + ',' + JStr('net', Net) + ',' + JStr('kind', Kind) + ',' + JStr('result', Res);-        If Res = 'ok' Then S := S + ',' + JRaw('endMil', '[' + IntToStr(Round(CoordToMils(EX))) + ',' + IntToStr(Round(CoordToMils(EY))) + ']');-        S := S + '}';-    End;-    SchServer.ProcessControl.PostProcess(Doc, '');-    MarkSchModified(Doc);-    Doc.GraphicallyInvalidate;-    Ops.Free;-    Result := '{' + JRaw('ok','true') + ',' + JStr('document', Doc.DocumentName) + ',' + JRaw('connected', IntToStr(NOk)) + ',' + JRaw('results', '[' + S + ']') + '}';-End;--{ sch_edit: change placed parts on the current sheet.-  payload lines: designator|action|a|b   actions: move x y (mil, the part's origin) | moveby dx dy |-  rotate 0/90/180/270 | designator new | comment text | delete-  move, moveby and rotate act on every part of a multi-part component (they are drawn apart; say which-  with a single-part designator). One undo step. }-Function CmdSchEdit(Lines : TStringList; Payload : String) : String;-Var-    Doc  : ISch_Document;-    Ops  : TStringList;-    CL, Dead : TInterfaceList;-    C    : ISch_Component;-    Op, Des, Act, A, B, Res, S : String;-    I, J, K, Want, Have, Steps, NOk : Integer;-Begin-    Doc := SchServer.GetCurrentSchDocument;-    If (Doc = Nil) Or (Doc.ObjectID = eSchLib) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_schematic') + ',' + JStr('error','no current schematic sheet (open a .SchDoc first)') + '}'; Exit; End;-    Ops := TStringList.Create; Ops.Text := Payload;-    Dead := TInterfaceList.Create;-    S := ''; NOk := 0;-    SchServer.ProcessControl.PreProcess(Doc, '');-    For I := 0 To Ops.Count - 1 Do-    Begin-        Op := Ops[I];-        If Trim(Op) = '' Then Continue;-        Des := FieldP(Op, 1); Act := LowerCase(FieldP(Op, 2)); A := FieldP(Op, 3); B := FieldP(Op, 4);-        Res := 'ok';-        CL := SheetCompsOf(Doc, Des);-        If CL.Count = 0 Then Res := 'designator not on this sheet'-        Else-            For J := 0 To CL.Count - 1 Do-            Begin-                C := CL[J];-                { Begin/EndModify tell the project's compiler the part changed: without them a renamed or-                  re-commented part reached the change order with its OLD values (U? on the board, and-                  comment edits found no differences at all; measured 2026-10-21, AD 26.11.1). }-                If Act <> 'delete' Then SchServer.RobotManager.SendMessage(C.I_ObjectAddress, c_BroadCast, SCHM_BeginModify, c_NoEventData);-                If Act = 'move' Then C.MoveToXY(MilsToCoord(StrToIntDef(A, 0)), MilsToCoord(StrToIntDef(B, 0)))-                Else If Act = 'moveby' Then C.MoveByXY(MilsToCoord(StrToIntDef(A, 0)), MilsToCoord(StrToIntDef(B, 0)))-                Else If Act = 'rotate' Then-                Begin-                    Want := (StrToIntDef(A, 0) Div 90) Mod 4;-                    Have := C.Orientation;-                    Steps := (Want - Have + 4) Mod 4;-                    For K := 1 To Steps Do C.RotateBy90(C.Location, eRotate90);-                End-                Else If Act = 'designator' Then C.Designator.Text := A-                Else If Act = 'comment' Then C.Comment.Text := A-                Else If Act = 'delete' Then Dead.Add(C)-                Else Res := 'unknown action ' + Act;-                If Act <> 'delete' Then SchServer.RobotManager.SendMessage(C.I_ObjectAddress, c_BroadCast, SCHM_EndModify, c_NoEventData);-                C.GraphicallyInvalidate;-            End;-        CL.Free;-        If Res = 'ok' Then NOk := NOk + 1;-        If S <> '' Then S := S + ',';-        S := S + '{' + JStr('designator', Des) + ',' + JStr('action', Act) + ',' + JStr('result', Res) + '}';-    End;-    For I := 0 To Dead.Count - 1 Do-    Begin-        C := Dead[I];-        Doc.RemoveSchObject(C);-        SchServer.RobotManager.SendMessage(Doc.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, C.I_ObjectAddress);-    End;-    SchServer.ProcessControl.PostProcess(Doc, '');-    MarkSchModified(Doc);-    Doc.GraphicallyInvalidate;-    Ops.Free; Dead.Free;-    Result := '{' + JRaw('ok','true') + ',' + JStr('document', Doc.DocumentName) + ',' + JRaw('applied', IntToStr(NOk)) + ',' + JRaw('results', '[' + S + ']') + '}';-End;--{ sch_place_part: place one component of the CURRENT SchLib on a sheet (opened and shown here).-  libref, sch (the sheet path), designator, x, y (mil, snapped to 100), rotation, comment, partId,-  footprint + pcblib (else the library part's own footprint models are copied by name). }-Function CmdSchPlacePart(Lines : TStringList) : String;-Var-    SLib   : ISch_Lib;-    LibCmp, Inst : ISch_Component;-    Prim, Cpy : ISch_GraphicalObject;-    PIt    : ISch_Iterator;-    Impl, NImpl : ISch_Implementation;-    Prm    : ISch_Parameter;-    SD     : IServerDocument;-    Sheet  : ISch_Document;-    SchPath, Fp, Models : String;-    DX, DY, Part, K, Rot, NPins : Integer;-Begin-    SLib := SchServer.GetCurrentSchDocument;-    If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the SchLib first (it must be the current document)') + '}'; Exit; End;-    LibCmp := LibCmpOf(SLib, Param(Lines, 'libref'));-    If LibCmp = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','libref_not_found') + ',' + JStr('error','no component ' + Param(Lines, 'libref') + ' in ' + SLib.DocumentName) + '}'; Exit; End;-    SchPath := Param(Lines, 'sch');-    SD := Nil;-    Try-        SD := Client.OpenDocument('SCH', SchPath);-    Except-        SD := Nil;-    End;-    If SD = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','could not open the sheet ' + SchPath) + '}'; Exit; End;-    Client.ShowDocument(SD);-    Sheet := SchServer.GetCurrentSchDocument;-    If (Sheet = Nil) Or (Sheet.ObjectID = eSchLib) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','the sheet did not become the current document') + '}'; Exit; End;-    Part := StrToIntDef(Param(Lines, 'partId'), 1);-    DX := (StrToIntDef(Param(Lines, 'x'), 1000) Div 100) * 100;-    DY := (StrToIntDef(Param(Lines, 'y'), 1000) Div 100) * 100;-    SchServer.ProcessControl.PreProcess(Sheet, '');-    Inst := SchServer.SchObjectFactory(eSchComponent, eCreate_Default);-    { a multi-part symbol needs its part count on the instance first, or the part is clamped to 1 and the sheet reads-      part 2 back as part 1 (2026-10-22, dual op-amp) }-    If LibCmp.PartCount > 1 Then Inst.PartCount := LibCmp.PartCount;-    If (Part < 1) Or (Part > Inst.PartCount) Then Part := 1;-    Inst.CurrentPartID := Part;-    Inst.DisplayMode := 0;-    Inst.LibReference := LibCmp.LibReference;-    Inst.ComponentDescription := LibCmp.ComponentDescription;-    Inst.SourceLibraryName := SLib.DocumentName;-    Inst.Location := Point(MilsToCoord(DX), MilsToCoord(DY));-    NPins := 0;-    PIt := LibCmp.SchIterator_Create;-    Prim := PIt.FirstSchObject;-    While Prim <> Nil Do-    Begin-        If ((Prim.ObjectID = ePin) Or (Prim.ObjectID = eRectangle) Or (Prim.ObjectID = eLine) Or (Prim.ObjectID = eArc) Or-            (Prim.ObjectID = eEllipse) Or (Prim.ObjectID = ePolygon) Or (Prim.ObjectID = ePolyline) Or (Prim.ObjectID = eLabel)) And-           (Prim.OwnerPartDisplayMode = 0) Then-        Begin-            { every part's objects keep their own part (0 = shared by all parts), as Altium's own placement does: the-              instance shows CurrentPartID. Copying only the chosen part and moving its shared power pins onto it gave-              each part its own copy of V+/V-, which the compiler reported as duplicate pins (2026-10-23). }-            Cpy := Prim.Replicate;-            Cpy.OwnerPartDisplayMode := 0;-            Cpy.MoveByXY(MilsToCoord(DX), MilsToCoord(DY));-            Inst.AddSchObject(Cpy);-            If (Prim.ObjectID = ePin) And ((Prim.OwnerPartId = Part) Or (Prim.OwnerPartId = 0)) Then NPins := NPins + 1;-        End;-        Prim := PIt.NextSchObject;-    End;-    LibCmp.SchIterator_Destroy(PIt);-    PIt := LibCmp.SchIterator_Create;-    PIt.AddFilter_ObjectSet(MkSet(eParameter));-    Prm := PIt.FirstSchObject;-    While Prm <> Nil Do-    Begin-        If (UpperCase(Prm.Name) <> 'COMMENT') And (UpperCase(Prm.Name) <> 'DESIGNATOR') Then-        Begin-            Cpy := Prm.Replicate;-            Cpy.OwnerPartId := Part;-            Cpy.MoveByXY(MilsToCoord(DX), MilsToCoord(DY));-            Inst.AddSchObject(Cpy);-        End;-        Prm := PIt.NextSchObject;-    End;-    LibCmp.SchIterator_Destroy(PIt);-    { the footprint: the one named, else the library part's own PCBLIB models by name }-    Fp := Param(Lines, 'footprint');-    Models := '';-    If Fp <> '' Then-    Begin-        NImpl := Inst.AddSchImplementation;-        NImpl.ClearAllDatafileLinks;-        NImpl.Description := 'Footprint';-        NImpl.ModelName := Fp;-        NImpl.ModelType := 'PCBLIB';-        NImpl.AddDataFileLink(Fp, Param(Lines, 'pcblib'), 'PCBLIB');-        NImpl.IsCurrent := True;-        Models := '"' + Fp + '"';-    End-    Else-    Begin-        PIt := LibCmp.SchIterator_Create;-        PIt.AddFilter_ObjectSet(MkSet(eImplementation));-        Impl := PIt.FirstSchObject;-        While Impl <> Nil Do-        Begin-            If UpperCase(Impl.ModelType) = 'PCBLIB' Then-            Begin-                NImpl := Inst.AddSchImplementation;-                NImpl.ClearAllDatafileLinks;-                NImpl.Description := Impl.Description;-                NImpl.ModelName := Impl.ModelName;-                NImpl.ModelType := 'PCBLIB';-                NImpl.AddDataFileLink(Impl.ModelName, Param(Lines, 'pcblib'), 'PCBLIB');-                NImpl.IsCurrent := Impl.IsCurrent;-                If Models <> '' Then Models := Models + ',';-                Models := Models + '"' + JsonEsc(Impl.ModelName) + '"';-            End;-            Impl := PIt.NextSchObject;-        End;-        LibCmp.SchIterator_Destroy(PIt);-    End;-    If Param(Lines, 'designator') <> '' Then Inst.Designator.Text := Param(Lines, 'designator')-    Else Inst.Designator.Text := LibCmp.Designator.Text;-    Inst.Designator.IsHidden := False;-    Inst.Designator.Location := Point(MilsToCoord(DX), MilsToCoord(DY + 200));-    If Param(Lines, 'comment') <> '' Then Inst.Comment.Text := Param(Lines, 'comment')-    Else Inst.Comment.Text := LibCmp.Comment.Text;-    Inst.Comment.IsHidden := False;-    Inst.Comment.Location := Point(MilsToCoord(DX), MilsToCoord(DY - 200));-    Sheet.RegisterSchObjectInContainer(Inst);-    SchServer.RobotManager.SendMessage(Sheet.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, Inst.I_ObjectAddress);-    Rot := (StrToIntDef(Param(Lines, 'rotation'), 0) Div 90) Mod 4;-    For K := 1 To Rot Do Inst.RotateBy90(Inst.Location, eRotate90);-    SchServer.ProcessControl.PostProcess(Sheet, '');-    MarkSchModified(Sheet);-    Sheet.GraphicallyInvalidate;-    Result := '{' + JRaw('ok','true') + ',' + JStr('document', Sheet.DocumentName) + ',' + JStr('designator', Inst.Designator.Text) + ',' +-        JStr('libref', LibCmp.LibReference) + ',' + JRaw('pins', IntToStr(NPins)) + ',' + JRaw('footprints', '[' + Models + ']') + ',' +-        JRaw('atMil', '[' + IntToStr(DX) + ',' + IntToStr(DY) + ']') + '}';-End;--{ doc_params: the current sheet's document parameters (title block: Title, DocumentNumber, Revision,-  DrawnBy, Author, CompanyName, ...). params=K=V;K=V sets them (added hidden when missing); always-  answers every document parameter. }-Function CmdDocParams(Lines : TStringList) : String;-Var-    Doc   : ISch_Document;-    It    : ISch_Iterator;-    Pm    : ISch_Parameter;-    Items : TStringList;-    Spec, K, V, S : String;-    I, P, NSet : Integer;-Begin-    Doc := SchServer.GetCurrentSchDocument;-    If (Doc = Nil) Or (Doc.ObjectID = eSchLib) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_schematic') + ',' + JStr('error','no current schematic sheet (open a .SchDoc first)') + '}'; Exit; End;-    NSet := 0;-    If Param(Lines, 'params') <> '' Then-    Begin-        SchServer.ProcessControl.PreProcess(Doc, '');-        Items := SplitSemi(Param(Lines, 'params'));-        For I := 0 To Items.Count - 1 Do-        Begin-            Spec := Items[I];-            P := Pos('=', Spec);-            If P > 0 Then-            Begin-                K := Trim(Copy(Spec, 1, P - 1));-                V := Trim(Copy(Spec, P + 1, Length(Spec)));-                If K <> '' Then Begin SetDocParam(Doc, K, V); NSet := NSet + 1; End;-            End;-        End;-        Items.Free;-        SchServer.ProcessControl.PostProcess(Doc, '');-        MarkSchModified(Doc);-        Doc.GraphicallyInvalidate;-    End;-    S := '';-    It := Doc.SchIterator_Create;-    It.AddFilter_ObjectSet(MkSet(eParameter));-    Pm := It.FirstSchObject;-    While Pm <> Nil Do-    Begin-        { the sheet's own parameters, not its parts' (the iterator walks into components) }-        If (Pm.Container <> Nil) And (Pm.Container.ObjectID <> eSchComponent) Then-        Begin-            If S <> '' Then S := S + ',';-            S := S + '{' + JStr('name', Pm.Name) + ',' + JStr('value', Pm.Text) + '}';-        End;-        Pm := It.NextSchObject;-    End;-    Doc.SchIterator_Destroy(It);-    Result := '{' + JRaw('ok','true') + ',' + JStr('document', Doc.DocumentName) + ',' + JRaw('set', IntToStr(NSet)) + ',' + JRaw('params', '[' + S + ']') + '}';-End;--{ sch_rename_at: rename placed parts named by designator AND position (several parts on a sheet can all-  read 'R?'). payload lines: from|to|xMil|yMil (the part's Location). One undo step. }-Function CmdSchRenameAt(Lines : TStringList; Payload : String) : String;-Var-    Doc  : ISch_Document;-    Ops  : TStringList;-    CL   : TInterfaceList;-    C, Hit : ISch_Component;-    Op, Res, S : String;-    I, J, X, Y : Integer;-Begin-    Doc := SchServer.GetCurrentSchDocument;-    If (Doc = Nil) Or (Doc.ObjectID = eSchLib) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_schematic') + ',' + JStr('error','no current schematic sheet (open a .SchDoc first)') + '}'; Exit; End;-    Ops := TStringList.Create; Ops.Text := Payload;-    S := '';-    SchServer.ProcessControl.PreProcess(Doc, '');-    For I := 0 To Ops.Count - 1 Do-    Begin-        Op := Ops[I];-        If Trim(Op) = '' Then Continue;-        X := StrToIntDef(FieldP(Op, 3), 0); Y := StrToIntDef(FieldP(Op, 4), 0);-        Hit := Nil;-        CL := SheetCompsOf(Doc, FieldP(Op, 1));-        For J := 0 To CL.Count - 1 Do-        Begin-            C := CL[J];-            If (Abs(Round(CoordToMils(C.Location.X)) - X) <= 1) And (Abs(Round(CoordToMils(C.Location.Y)) - Y) <= 1) Then Hit := C;-        End;-        CL.Free;-        If Hit = Nil Then Res := 'no part ' + FieldP(Op, 1) + ' at that position'-        Else Begin-            { Begin/EndModify: without them the change order still saw 'U?' (see sch_edit) }-            SchServer.RobotManager.SendMessage(Hit.I_ObjectAddress, c_BroadCast, SCHM_BeginModify, c_NoEventData);-            Hit.Designator.Text := FieldP(Op, 2);-            SchServer.RobotManager.SendMessage(Hit.I_ObjectAddress, c_BroadCast, SCHM_EndModify, c_NoEventData);-            Hit.GraphicallyInvalidate; Res := 'ok';-        End;-        If S <> '' Then S := S + ',';-        S := S + '{' + JStr('from', FieldP(Op, 1)) + ',' + JStr('to', FieldP(Op, 2)) + ',' + JStr('result', Res) + '}';-    End;-    SchServer.ProcessControl.PostProcess(Doc, '');-    MarkSchModified(Doc);-    Doc.GraphicallyInvalidate;-    Ops.Free;-    Result := '{' + JRaw('ok','true') + ',' + JRaw('results', '[' + S + ']') + '}';-End;--{ sch_wires: wires on the current sheet, one per payload line: x1|y1|x2|y2[|x3|y3...] in mils (a polyline). }-Function CmdSchWires(Lines : TStringList; Payload : String) : String;-Var-    Doc : ISch_Document;-    Ops : TStringList;-    W   : ISch_Wire;-    I, K, N, Made : Integer;-    Op  : String;-Begin-    Doc := SchServer.GetCurrentSchDocument;-    If (Doc = Nil) Or (Doc.ObjectID = eSchLib) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_schematic') + ',' + JStr('error','no current schematic sheet (open a .SchDoc first)') + '}'; Exit; End;-    Ops := TStringList.Create; Ops.Text := Payload;-    Made := 0;-    SchServer.ProcessControl.PreProcess(Doc, '');-    For I := 0 To Ops.Count - 1 Do-    Begin-        Op := Trim(Ops[I]);-        If Op = '' Then Continue;-        N := 0;-        While FieldP(Op, 2 * N + 1) <> '' Do N := N + 1;-        If N < 2 Then Continue;-        W := SchServer.SchObjectFactory(eWire, eCreate_Default);-        W.Location := Point(MilsToCoord(StrToIntDef(FieldP(Op, 1), 0)), MilsToCoord(StrToIntDef(FieldP(Op, 2), 0)));-        For K := 1 To N Do-        Begin-            W.InsertVertex := K;-            W.SetState_Vertex(K, Point(MilsToCoord(StrToIntDef(FieldP(Op, 2 * K - 1), 0)), MilsToCoord(StrToIntDef(FieldP(Op, 2 * K), 0))));-        End;-        W.LineWidth := eSmall;-        Doc.RegisterSchObjectInContainer(W);-        SchServer.RobotManager.SendMessage(Doc.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, W.I_ObjectAddress);-        Made := Made + 1;-    End;-    SchServer.ProcessControl.PostProcess(Doc, '');-    MarkSchModified(Doc);-    Doc.GraphicallyInvalidate;-    Ops.Free;-    Result := '{' + JRaw('ok','true') + ',' + JRaw('wires', IntToStr(Made)) + '}';-End;--{ sch_zoom: select the named parts on the current sheet (everything else deselected) and zoom the view-  to them. designators=R1,U2 }-Function CmdSchZoom(Lines : TStringList) : String;-Var-    Doc  : ISch_Document;-    It   : ISch_Iterator;-    C    : ISch_Component;-    Want : TStringList;-    Found, S : String;-    N : Integer;-Begin-    Doc := SchServer.GetCurrentSchDocument;-    If (Doc = Nil) Or (Doc.ObjectID = eSchLib) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_schematic') + ',' + JStr('error','no current schematic sheet (open a .SchDoc first)') + '}'; Exit; End;-    Want := TStringList.Create;-    Want.Delimiter := ',';-    Want.StrictDelimiter := True;-    Want.DelimitedText := UpperCase(Param(Lines, 'designators'));-    N := 0; S := '';-    It := Doc.SchIterator_Create;-    It.AddFilter_ObjectSet(MkSet(eSchComponent));-    C := It.FirstSchObject;-    While C <> Nil Do-    Begin-        If Want.IndexOf(UpperCase(C.Designator.Text)) >= 0 Then-        Begin-            C.Selection := True;-            N := N + 1;-            If S <> '' Then S := S + ',';-            S := S + '{' + JStr('designator', C.Designator.Text) + ',' + JRaw('xMil', IntToStr(Round(CoordToMils(C.Location.X)))) + ',' + JRaw('yMil', IntToStr(Round(CoordToMils(C.Location.Y)))) + '}';-        End-        Else C.Selection := False;-        C := It.NextSchObject;-    End;-    Doc.SchIterator_Destroy(It);-    Want.Free;-    Doc.GraphicallyInvalidate;-    If N > 0 Then Begin ResetParameters; AddStringParameter('Object', 'Selected'); RunProcess('Sch:Zoom'); End;-    Result := '{' + JRaw('ok','true') + ',' + JRaw('selected', IntToStr(N)) + ',' + JRaw('parts', '[' + S + ']') + '}';-End;--Function CmdBuildSchematic(Lines : TStringList; Payload : String) : String;-Var-    SLib   : ISch_Lib;-    LibCmp : ISch_Component;-    Inst   : ISch_Component;-    Prim, Cpy : ISch_GraphicalObject;-    PIt    : ISch_Iterator;-    Pin    : ISch_Pin;-    W      : ISch_Wire;-    Impl   : ISch_Implementation;-    Prm    : ISch_Parameter;-    NL     : ISch_NetLabel;-    Lb     : ISch_Label;-    SD     : IServerDocument;-    Sheet  : ISch_Document;-    R      : TCoordRect;-    Ops, Refs, LibRefs, Blocks, Comments, Fps, PinNet, BlockOrder, BL, BB, BR, BT, ColOf : TStringList;-    Op, SchPath, Missing, Net, NoNet, Blk : String;-    Cols, Idx : TStringList;-    I, K, N, NWires, NLabels, NPlaced, Col, NCols : Integer;-    BandW : Integer;-    L0, B0, R0, T0, ColW, ColH, ColX, YCur, SheetW, SheetH, DX, DY, HX, HY, EX, EY, Dir, PW, MaxH : Integer;-Begin-    SchPath := Param(Lines, 'sch');-    SLib := SchServer.GetCurrentSchDocument;-    If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the SchLib first (it must be the current document)') + '}';-        Exit;-    End;-    Ops := TStringList.Create; Ops.Text := Payload;-    Refs := TStringList.Create; LibRefs := TStringList.Create; Blocks := TStringList.Create; Comments := TStringList.Create; Fps := TStringList.Create;-    PinNet := TStringList.Create; BlockOrder := TStringList.Create;-    BL := TStringList.Create; BB := TStringList.Create; BR := TStringList.Create; BT := TStringList.Create; ColOf := TStringList.Create;-    Missing := '';-    For I := 0 To Ops.Count - 1 Do-    Begin-        Op := Trim(Ops[I]);-        If FieldP(Op, 1) = 'C' Then-        Begin-            Refs.Add(FieldP(Op, 2)); LibRefs.Add(FieldP(Op, 3)); Blocks.Add(FieldP(Op, 4)); Comments.Add(FieldP(Op, 5)); Fps.Add(FieldP(Op, 6));-            If BlockOrder.IndexOf(FieldP(Op, 4)) < 0 Then BlockOrder.Add(FieldP(Op, 4));-            If LibCmpOf(SLib, FieldP(Op, 3)) = Nil Then Missing := Missing + FieldP(Op, 3) + ' ';-        End-        Else If FieldP(Op, 1) = 'P' Then PinNet.Add(FieldP(Op, 2) + '.' + FieldP(Op, 3) + '=' + FieldP(Op, 4));-    End;-    If Missing <> '' Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('error','symbols not in the SchLib: ' + Trim(Missing)) + '}';-        Exit;-    End;-    { measure every part (mil, library frame) }-    For K := 0 To Refs.Count - 1 Do-    Begin-        LibCmp := LibCmpOf(SLib, LibRefs[K]);-        N := 0; L0 := 0; B0 := 0; R0 := 0; T0 := 0;-        PIt := LibCmp.SchIterator_Create;-        Prim := PIt.FirstSchObject;-        While Prim <> Nil Do-        Begin-            If IsDrawn(Prim) Then-            Begin-                R := Prim.BoundingRectangle;-                If (N = 0) Or (Round(CoordToMils(R.Left)) < L0) Then L0 := Round(CoordToMils(R.Left));-                If (N = 0) Or (Round(CoordToMils(R.Bottom)) < B0) Then B0 := Round(CoordToMils(R.Bottom));-                If (N = 0) Or (Round(CoordToMils(R.Right)) > R0) Then R0 := Round(CoordToMils(R.Right));-                If (N = 0) Or (Round(CoordToMils(R.Top)) > T0) Then T0 := Round(CoordToMils(R.Top));-                N := N + 1;-            End;-            Prim := PIt.NextSchObject;-        End;-        LibCmp.SchIterator_Destroy(PIt);-        BL.Add(IntToStr(L0)); BB.Add(IntToStr(B0)); BR.Add(IntToStr(R0)); BT.Add(IntToStr(T0));-    End;-    { columns: one per block, at most 6 parts tall (a taller block wraps into the next column),-      each wide enough for its widest part plus a wire stub and a net label on each side; the-      columns pack left to right into bands at most 22000 mil wide, so the sheet stays landscape-      and a zoom-all keeps the parts readable. ColOf: 'x,width,bandTop,block|i;i;...' }-    Cols := TStringList.Create;-    For Col := 0 To BlockOrder.Count - 1 Do-    Begin-        N := 0; Op := '';-        For K := 0 To Refs.Count - 1 Do-            If Blocks[K] = BlockOrder[Col] Then-            Begin-                If N = 6 Then Begin Cols.Add(BlockOrder[Col] + '|' + Op); Op := ''; N := 0; End;-                If Op <> '' Then Op := Op + ';';-                Op := Op + IntToStr(K);-                N := N + 1;-            End;-        If Op <> '' Then Cols.Add(BlockOrder[Col] + '|' + Op);-    End;-    NCols := Cols.Count;-    { the bands wrap inside the template's own width (its frame and title block stay true) }-    BandW := AdomInt(Param(Lines, 'tplW')) - 600;-    If BandW < 11000 Then BandW := 22600;-    SheetW := 0; MaxH := 0; ColX := 600; YCur := 0; ColH := 0; Blk := '';-    For Col := 0 To NCols - 1 Do-    Begin-        Idx := SplitSemi(FieldP(Cols[Col], 2));-        PW := 0; DY := 0;-        For I := 0 To Idx.Count - 1 Do-        Begin-            K := AdomInt(Idx[I]);-            If AdomInt(BR[K]) - AdomInt(BL[K]) > PW Then PW := AdomInt(BR[K]) - AdomInt(BL[K]);-            DY := DY + (AdomInt(BT[K]) - AdomInt(BB[K])) + 900;-        End;-        Idx.Free;-        ColW := PW + 2000;-        { a new band when this column would run past 22000 mil }-        If (ColX > 600) And (ColX + ColW > BandW) Then-        Begin-            YCur := YCur + ColH + 900;-            ColH := 0;-            ColX := 600;-        End;-        { the block's name heads its first column only }-        If FieldP(Cols[Col], 1) <> Blk Then Op := FieldP(Cols[Col], 1) Else Op := '';-        Blk := FieldP(Cols[Col], 1);-        ColOf.Add(IntToStr(ColX) + ',' + IntToStr(ColW) + ',' + IntToStr(YCur) + ',' + Op + '|' + FieldP(Cols[Col], 2));-        ColX := ColX + ColW;-        If ColX > SheetW Then SheetW := ColX;-        If DY > ColH Then ColH := DY;-    End;-    MaxH := YCur + ColH;-    SheetW := SheetW + 600;-    SheetH := MaxH + 3600;-    If SheetW < 11500 Then SheetW := 11500;-    If SheetH < 7600 Then SheetH := 7600;-    { the sheet is a FILE the bridge put in place (an Altium sheet template copied to sch) and opened-      inside its project: a new document from CreateNewDocumentFromDocumentKind joins whatever project-      is focused (another design's, or the agent's own), measured 2026-10-15 }-    SD := Nil;-    Try-        SD := Client.OpenDocument('SCH', SchPath);-    Except-        SD := Nil;-    End;-    If SD = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','could not open the sheet ' + SchPath) + '}'; Exit; End;-    Client.ShowDocument(SD);-    Sheet := SchServer.GetCurrentSchDocument;-    If (Sheet = Nil) Or (Sheet.ObjectID = eSchLib) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','the sheet did not become the current document') + '}'; Exit; End;-    { the template's own size (and title block) when the parts fit it, else a custom size }-    If (AdomInt(Param(Lines, 'tplW')) >= SheetW) And (AdomInt(Param(Lines, 'tplH')) >= SheetH) Then-    Begin-        SheetW := AdomInt(Param(Lines, 'tplW')); SheetH := AdomInt(Param(Lines, 'tplH'));-    End-    Else-    Begin-        Sheet.UseCustomSheet := True;-        Sheet.CustomX := MilsToCoord(SheetW);-        Sheet.CustomY := MilsToCoord(SheetH);-        Sheet.CustomXZones := (SheetW + 2499) Div 2500;-        Sheet.CustomYZones := (SheetH + 2499) Div 2500;-        Sheet.CustomMarginWidth := MilsToCoord(200);-    End;-    NWires := 0; NLabels := 0; NPlaced := 0; NoNet := '';-    For Col := 0 To NCols - 1 Do-    Begin-        ColX := AdomInt(Field(FieldP(ColOf[Col], 1), 1)); ColW := AdomInt(Field(FieldP(ColOf[Col], 1), 2));-        Blk := Field(FieldP(ColOf[Col], 1), 4);-        If Blk <> '' Then-        Begin-            Lb := SchServer.SchObjectFactory(eLabel, eCreate_Default);-            Lb.Location := Point(MilsToCoord(ColX + 300), MilsToCoord(SheetH - 1500 - AdomInt(Field(FieldP(ColOf[Col], 1), 3))));-            Lb.Text := UpperCase(Blk);-            Sheet.RegisterSchObjectInContainer(Lb);-            SchServer.RobotManager.SendMessage(Sheet.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, Lb.I_ObjectAddress);-        End;-        { below the template's header strip (its notes and revision block) }-        YCur := SheetH - 2100 - AdomInt(Field(FieldP(ColOf[Col], 1), 3));-        Idx := SplitSemi(FieldP(ColOf[Col], 2));-        For I := 0 To Idx.Count - 1 Do-        Begin-            K := AdomInt(Idx[I]);-            LibCmp := LibCmpOf(SLib, LibRefs[K]);-            L0 := AdomInt(BL[K]); B0 := AdomInt(BB[K]); R0 := AdomInt(BR[K]); T0 := AdomInt(BT[K]);-            { snap to the 100 mil grid so pins land on the grid wires snap to }-            DX := ((ColX + ColW Div 2 - (L0 + R0) Div 2) Div 100) * 100;-            DY := ((YCur - 250 - T0) Div 100) * 100;-            Inst := SchServer.SchObjectFactory(eSchComponent, eCreate_Default);-            Inst.CurrentPartID := 1;-            Inst.DisplayMode := 0;-            Inst.LibReference := LibCmp.LibReference;-            Inst.ComponentDescription := LibCmp.ComponentDescription;-            Inst.Location := Point(MilsToCoord(DX), MilsToCoord(DY));-            PIt := LibCmp.SchIterator_Create;-            Prim := PIt.FirstSchObject;-            While Prim <> Nil Do-            Begin-                If IsDrawn(Prim) Then-                Begin-                    Cpy := Prim.Replicate;-                    Cpy.OwnerPartId := 1;-                    Cpy.OwnerPartDisplayMode := 0;-                    Cpy.MoveByXY(MilsToCoord(DX), MilsToCoord(DY));-                    Inst.AddSchObject(Cpy);-                End;-                Prim := PIt.NextSchObject;-            End;-            LibCmp.SchIterator_Destroy(PIt);-            { the part's parameters (MPN, Manufacturer, distributor numbers): the BOM reads them from-              here, and copying only the drawing left every number column empty (measured 2026-10-15).-              Designator and Comment are the instance's own. }-            PIt := LibCmp.SchIterator_Create;-            PIt.AddFilter_ObjectSet(MkSet(eParameter));-            Prm := PIt.FirstSchObject;-            While Prm <> Nil Do-            Begin-                If (UpperCase(Prm.Name) <> 'COMMENT') And (UpperCase(Prm.Name) <> 'DESIGNATOR') Then-                Begin-                    Cpy := Prm.Replicate;-                    Cpy.OwnerPartId := 1;-                    Cpy.MoveByXY(MilsToCoord(DX), MilsToCoord(DY));-                    Inst.AddSchObject(Cpy);-                End;-                Prm := PIt.NextSchObject;-            End;-            LibCmp.SchIterator_Destroy(PIt);-            { the footprint link the copied primitives do not carry: without it every part compiles-              with 'Footprint ... cannot be found' }-            If Fps[K] <> '' Then-            Begin-                Impl := Inst.AddSchImplementation;-                Impl.ClearAllDatafileLinks;-                Impl.Description := 'Footprint';-                Impl.ModelName := Fps[K];-                Impl.ModelType := 'PCBLIB';-                Impl.AddDataFileLink(Fps[K], Param(Lines, 'pcblib'), 'PCBLIB');-                Impl.IsCurrent := True;-            End;-            Inst.Designator.Text := Refs[K];-            Inst.Designator.IsHidden := False;-            Inst.Designator.Justification := eJustify_BottomCenter;-            Inst.Designator.Location := Point(MilsToCoord(DX + (L0 + R0) Div 2), MilsToCoord(DY + T0 + 80));-            Inst.Comment.Text := Comments[K];-            Inst.Comment.IsHidden := False;-            Inst.Comment.Justification := eJustify_TopCenter;-            Inst.Comment.Location := Point(MilsToCoord(DX + (L0 + R0) Div 2), MilsToCoord(DY + B0 - 80));-            Sheet.RegisterSchObjectInContainer(Inst);-            SchServer.RobotManager.SendMessage(Sheet.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, Inst.I_ObjectAddress);-            NPlaced := NPlaced + 1;-            { a wire stub and a net label at every pin's hot spot (Location is the body end) }-            PIt := Inst.SchIterator_Create;-            PIt.AddFilter_ObjectSet(MkSet(ePin));-            Pin := PIt.FirstSchObject;-            While Pin <> Nil Do-            Begin-                Net := PinNet.Values[Refs[K] + '.' + Pin.Designator];-                Dir := Pin.Orientation;-                HX := Pin.Location.X; HY := Pin.Location.Y;-                If Dir = 0 Then HX := HX + Pin.PinLength-                Else If Dir = 1 Then HY := HY + Pin.PinLength-                Else If Dir = 2 Then HX := HX - Pin.PinLength-                Else HY := HY - Pin.PinLength;-                EX := HX; EY := HY;-                If Dir = 0 Then EX := HX + MilsToCoord(200)-                Else If Dir = 1 Then EY := HY + MilsToCoord(200)-                Else If Dir = 2 Then EX := HX - MilsToCoord(200)-                Else EY := HY - MilsToCoord(200);-                If Net = '' Then-                Begin-                    If NoNet <> '' Then NoNet := NoNet + ';';-                    NoNet := NoNet + Refs[K] + '.' + Pin.Designator;-                End-                Else-                Begin-                    W := SchServer.SchObjectFactory(eWire, eCreate_Default);-                    W.Location := Point(HX, HY);-                    W.InsertVertex := 1;-                    W.SetState_Vertex(1, Point(HX, HY));-                    W.InsertVertex := 2;-                    W.SetState_Vertex(2, Point(EX, EY));-                    W.LineWidth := eSmall;-                    Sheet.RegisterSchObjectInContainer(W);-                    SchServer.RobotManager.SendMessage(Sheet.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, W.I_ObjectAddress);-                    NWires := NWires + 1;-                    NL := SchServer.SchObjectFactory(eNetLabel, eCreate_Default);-                    NL.Text := Net;-                    NL.Location := Point(EX, EY);-                    { the label reads away from the part on every side }-                    If (Dir = 1) Or (Dir = 3) Then NL.Orientation := eRotate90;-                    If (Dir = 2) Or (Dir = 3) Then NL.Justification := eJustify_BottomRight;-                    Sheet.RegisterSchObjectInContainer(NL);-                    SchServer.RobotManager.SendMessage(Sheet.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, NL.I_ObjectAddress);-                    NLabels := NLabels + 1;-                End;-                Pin := PIt.NextSchObject;-            End;-            Inst.SchIterator_Destroy(PIt);-            YCur := DY + B0 - 650;-        End;-        Idx.Free;-    End;-    If Param(Lines, 'title') <> '' Then SetDocParam(Sheet, 'Title', Param(Lines, 'title'));-    If Param(Lines, 'number') <> '' Then SetDocParam(Sheet, 'DocumentNumber', Param(Lines, 'number'));-    If Param(Lines, 'revision') <> '' Then SetDocParam(Sheet, 'Revision', Param(Lines, 'revision'));-    If Param(Lines, 'drawnBy') <> '' Then SetDocParam(Sheet, 'DrawnBy', Param(Lines, 'drawnBy'));-    Sheet.GraphicallyInvalidate;-    ResetParameters; AddStringParameter('Action','All'); RunProcess('Sch:Zoom');-    SD.DoFileSave('');-    Result := '{' + JRaw('ok','true') + ',' + JStr('sch', SchPath) + ',' + JRaw('placed', IntToStr(NPlaced)) + ',' +-        JRaw('wires', IntToStr(NWires)) + ',' + JRaw('labels', IntToStr(NLabels)) + ',' + JStr('pinsWithoutNet', NoNet) + ',' +-        JRaw('sheetMil', '[' + IntToStr(SheetW) + ',' + IntToStr(SheetH) + ']') + ',' + JRaw('columns', IntToStr(NCols)) + '}';-    Ops.Free; Refs.Free; LibRefs.Free; Blocks.Free; Comments.Free; Fps.Free; PinNet.Free; BlockOrder.Free;-    BL.Free; BB.Free; BR.Free; BT.Free; ColOf.Free; Cols.Free;-End;--{ project_nets: the FOCUSED project's compiled netlist, flattened: every net with its pins as-  designator.pin. Compile first (compile verb). }-Function CmdProjectNets(Lines : TStringList) : String;-Var-    WS   : IWorkspace;-    Prj  : IProject;-    Doc  : IDocument;-    Net  : INet;-    Pin  : IPin;-    I, J : Integer;-    S, P : String;-Begin-    WS := GetWorkspace;-    Prj := Nil;-    If WS <> Nil Then Prj := WS.DM_FocusedProject;-    If Prj = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no focused project') + '}'; Exit; End;-    Doc := Prj.DM_DocumentFlattened;-    If Doc = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','the project has no compiled document: compile it first') + ',' + JStr('project', Prj.DM_ProjectFullPath) + '}'; Exit; End;-    S := '';-    For I := 0 To Doc.DM_NetCount - 1 Do-    Begin-        Net := Doc.DM_Nets(I);-        P := '';-        For J := 0 To Net.DM_PinCount - 1 Do-        Begin-            Pin := Net.DM_Pins(J);-            If P <> '' Then P := P + ',';-            P := P + '"' + JsonEsc(Pin.DM_LogicalPartDesignator + '.' + Pin.DM_PinNumber) + '"';-        End;-        If S <> '' Then S := S + ',';-        S := S + '"' + JsonEsc(Net.DM_NetName) + '":[' + P + ']';-    End;-    Result := '{' + JRaw('ok','true') + ',' + JStr('project', Prj.DM_ProjectFullPath) + ',' + JRaw('netCount', IntToStr(Doc.DM_NetCount)) + ',' + JRaw('nets', '{' + S + '}') + '}';-End;--{ select_net: select every primitive of one net (tracks, arcs, vias, pads, fills, regions, pours) and-  deselect everything else; net empty = clear the selection. Answers the counts by kind. }-{ place_parts: footprints of the CURRENT PcbLib onto a board that may not be current. Payload lines-  ref|footprint|x|y|assigns, raw units from the board's origin. All the footprints are checked first, so a missing one-  places nothing. The bridge sets rotation and side after with move_parts and reads them back. }-Function CmdPlaceParts(Lines : TStringList; Payload : String) : String;-Var-    PLib  : IPCB_Library;-    LibFp : IPCB_LibComponent;-    Board : IPCB_Board;-    SD    : IServerDocument;-    Inst  : IPCB_Component;-    Ops   : TStringList;-    Op, Missing, S : String;-    I, K, N : Integer;-    Hit   : Boolean;-Begin-    PLib := PCBServer.GetCurrentPCBLibrary;-    If PLib = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','the footprint library is not current') + '}'; Exit; End;-    Ops := TStringList.Create; Ops.Text := Payload;-    Missing := '';-    For K := 0 To Ops.Count - 1 Do-    Begin-        If Trim(Ops[K]) = '' Then Continue;-        Hit := False;-        For I := 0 To PLib.ComponentCount - 1 Do-        Begin-            LibFp := PLib.GetComponent(I);-            If UpperCase(LibFp.Name) = UpperCase(FieldP(Ops[K], 2)) Then Hit := True;-        End;-        If Not Hit Then Missing := Missing + FieldP(Ops[K], 2) + ' ';-    End;-    If Missing <> '' Then-    Begin-        Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','footprint_not_found') + ',' + JStr('error','not in this library: ' + Trim(Missing)) + '}';-        Ops.Free;-        Exit;-    End;-    SD := DocAt(Param(Lines, 'board'));-    If SD = Nil Then SD := Client.OpenDocument('PCB', Param(Lines, 'board'));-    If SD = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','could not open the board') + '}'; Ops.Free; Exit; End;-    Client.ShowDocument(SD);-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board after open') + '}'; Ops.Free; Exit; End;-    S := ''; N := 0;-    PCBServer.PreProcess;-    For K := 0 To Ops.Count - 1 Do-    Begin-        Op := Ops[K];-        If Trim(Op) = '' Then Continue;-        For I := 0 To PLib.ComponentCount - 1 Do-        Begin-            LibFp := PLib.GetComponent(I);-            If UpperCase(LibFp.Name) = UpperCase(FieldP(Op, 2)) Then Break;-        End;-        Inst := PlaceCopy(Board, LibFp, Board.XOrigin + AdomInt(FieldP(Op, 3)), Board.YOrigin + AdomInt(FieldP(Op, 4)));-        Inst.Name.Text := FieldP(Op, 1);-        Inst.Pattern := LibFp.Name;-        Inst.NameOn := True;-        If FieldP(Op, 5) <> '' Then AssignPadNets(Board, Inst, FieldP(Op, 5));-        If S <> '' Then S := S + ',';-        S := S + '"' + JsonEsc(Inst.Name.Text) + '"';-        N := N + 1;-    End;-    PCBServer.PostProcess;-    Board.ViewManager_FullUpdate;-    Ops.Free;-    Result := '{' + JRaw('ok','true') + ',' + JRaw('placed', IntToStr(N)) + ',' + JRaw('refs', '[' + S + ']') + '}';-End;--{ One body as ["file",overall,standoff,width,height]: file is the model's own name, empty for an extruded body. }-Function ModelBodyJson(Body : IPCB_ComponentBody) : String;-Var-    F : String;-    R : TCoordRect;-Begin-    F := '';-    Try If Body.Model <> Nil Then F := Body.Model.FileName; Except F := ''; End;-    R := Body.BoundingRectangle;-    Result := '["' + JsonEsc(F) + '",' + IntToStr(Body.OverallHeight) + ',' + IntToStr(Body.StandoffHeight) + ',' + IntToStr(R.Right - R.Left) + ',' + IntToStr(R.Top - R.Bottom) + ']';-End;--Function GroupModelBodies(Grp : IPCB_Group) : String;-Var-    GIt  : IPCB_GroupIterator;-    Prim : IPCB_Primitive;-    S    : String;-Begin-    S := '';-    GIt := Grp.GroupIterator_Create;-    GIt.AddFilter_ObjectSet(MkSet(eComponentBodyObject));-    GIt.AddFilter_LayerSet(AllLayers);-    Prim := GIt.FirstPCBObject;-    While Prim <> Nil Do-    Begin-        If S <> '' Then S := S + ',';-        S := S + ModelBodyJson(Prim);-        Prim := GIt.NextPCBObject;-    End;-    Grp.GroupIterator_Destroy(GIt);-    Result := '[' + S + ']';-End;--{ model_check: every part of the current board, or footprint of the current library, with its bodies' models. }-Function CmdModelCheck(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    BIt   : IPCB_BoardIterator;-    Comp  : IPCB_Component;-    PLib  : IPCB_Library;-    Fp    : IPCB_LibComponent;-    S     : String;-    I     : Integer;-Begin-    S := '';-    Board := PCBServer.GetCurrentPCBBoard;-    PLib := PCBServer.GetCurrentPCBLibrary;-    If PLib <> Nil Then-    Begin-        For I := 0 To PLib.ComponentCount - 1 Do-        Begin-            Fp := PLib.GetComponent(I);-            If S <> '' Then S := S + ',';-            S := S + '["' + JsonEsc(Fp.Name) + '","' + JsonEsc(Fp.Name) + '",' + GroupModelBodies(Fp) + ']';-        End;-        Result := '{' + JRaw('ok','true') + ',' + JStr('kind','library') + ',' + JStr('document', PLib.Board.FileName) + ',' + JRaw('parts', '[' + S + ']') + '}';-        Exit;-    End;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current board or footprint library') + '}'; Exit; End;-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eComponentObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Comp := BIt.FirstPCBObject;-    While Comp <> Nil Do-    Begin-        If S <> '' Then S := S + ',';-        S := S + '["' + JsonEsc(Comp.Name.Text) + '","' + JsonEsc(Comp.Pattern) + '",' + GroupModelBodies(Comp) + ']';-        Comp := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-    Result := '{' + JRaw('ok','true') + ',' + JStr('kind','board') + ',' + JStr('document', Board.FileName) + ',' + JRaw('parts', '[' + S + ']') + '}';-End;--Function CmdSelectNet(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    BIt   : IPCB_BoardIterator;-    Prim  : IPCB_Primitive;-    NetName, N : String;-    NT, NA, NV, NP, NPoly, NO : Integer;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    NetName := Param(Lines, 'net');-    NT := 0; NA := 0; NV := 0; NP := 0; NPoly := 0; NO := 0;-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eTrackObject, eArcObject, eViaObject, ePadObject, eFillObject, eRegionObject, ePolyObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Prim := BIt.FirstPCBObject;-    While Prim <> Nil Do-    Begin-        N := '';-        If Prim.Net <> Nil Then N := Prim.Net.Name;-        If (NetName <> '') And (N = NetName) And (Not Prim.InPolygon) Then-        Begin-            Prim.Selected := True;-            If Prim.ObjectId = eTrackObject Then NT := NT + 1-            Else If Prim.ObjectId = eArcObject Then NA := NA + 1-            Else If Prim.ObjectId = eViaObject Then NV := NV + 1-            Else If Prim.ObjectId = ePadObject Then NP := NP + 1-            Else If Prim.ObjectId = ePolyObject Then NPoly := NPoly + 1-            Else NO := NO + 1;-        End-        Else If Prim.Selected Then Prim.Selected := False;-        Prim := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-    Board.ViewManager_FullUpdate;-    Result := '{' + JRaw('ok','true') + ',' + JStr('net', NetName) + ',' + JRaw('selected', IntToStr(NT + NA + NV + NP + NPoly + NO)) + ',' +-        JRaw('byKind', '{' + JRaw('tracks', IntToStr(NT)) + ',' + JRaw('arcs', IntToStr(NA)) + ',' + JRaw('vias', IntToStr(NV)) + ',' +-        JRaw('pads', IntToStr(NP)) + ',' + JRaw('pours', IntToStr(NPoly)) + ',' + JRaw('other', IntToStr(NO)) + '}') + '}';-End;--{ ------------------------------------------------------------- silkscreen --- }--Function TextJson(T : IPCB_Text; Shown : Boolean) : String;-Var-    R : TCoordRect;-Begin-    R := T.BoundingRectangle;-    Result := '[' + '"' + JsonEsc(T.Text) + '",' + IntToStr(T.XLocation) + ',' + IntToStr(T.YLocation) + ',' + IntToStr(T.Size) + ',' +-        IntToStr(Round(T.Rotation)) + ',' + JBool(Shown) + ',[' + IntToStr(R.Left) + ',' + IntToStr(R.Bottom) + ',' + IntToStr(R.Right) + ',' + IntToStr(R.Top) + ']]';-End;--{ silk_state: what the silkscreen planner needs that routing_state does not carry, RAW units.-    comps [["ref","pattern",x,y,rot,bottom,[body l,b,r,t],name,comment]]   name/comment: [text,x,y,size,rot,shown,[box]]-    texts [[layer,text,x,y,size,rot,mirror,[box]]]   free overlay text (not a component's)-    lines [[layer,x1,y1,x2,y2,width]]                free overlay tracks }-Function CmdSilkState(Lines : TStringList) : String;-Var-    Board : IPCB_Board;-    BIt   : IPCB_BoardIterator;-    Comp  : IPCB_Component;-    Prim  : IPCB_Primitive;-    Txt   : IPCB_Text;-    Trk   : IPCB_Track;-    R     : TCoordRect;-    LC, LT, LL : TStringList;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    LC := TStringList.Create; LT := TStringList.Create; LL := TStringList.Create;-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eComponentObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Comp := BIt.FirstPCBObject;-    While Comp <> Nil Do-    Begin-        R := Comp.BoundingRectangleNoNameComment;-        LC.Add('["' + JsonEsc(Comp.Name.Text) + '","' + JsonEsc(Comp.Pattern) + '",' + IntToStr(Comp.x) + ',' + IntToStr(Comp.y) + ',' +-            IntToStr(Round(Comp.Rotation)) + ',' + JBool(Comp.Layer = eBottomLayer) + ',[' + IntToStr(R.Left) + ',' + IntToStr(R.Bottom) + ',' +-            IntToStr(R.Right) + ',' + IntToStr(R.Top) + '],' + TextJson(Comp.Name, Comp.NameOn) + ',' + TextJson(Comp.Comment, Comp.CommentOn) + ']');-        Comp := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eTextObject, eTrackObject));-    BIt.AddFilter_LayerSet(MkSet(eTopOverlay, eBottomOverlay));-    BIt.AddFilter_Method(eProcessAll);-    Prim := BIt.FirstPCBObject;-    While Prim <> Nil Do-    Begin-        If (Not Prim.InComponent) And ((Prim.Layer = eTopOverlay) Or (Prim.Layer = eBottomOverlay)) Then-        Begin-            If Prim.ObjectId = eTextObject Then-            Begin-                Txt := Prim; R := Txt.BoundingRectangle;-                LT.Add('[' + IntToStr(Txt.Layer) + ',"' + JsonEsc(Txt.Text) + '",' + IntToStr(Txt.XLocation) + ',' + IntToStr(Txt.YLocation) + ',' +-                    IntToStr(Txt.Size) + ',' + IntToStr(Round(Txt.Rotation)) + ',' + JBool(Txt.MirrorFlag) + ',[' + IntToStr(R.Left) + ',' +-                    IntToStr(R.Bottom) + ',' + IntToStr(R.Right) + ',' + IntToStr(R.Top) + ']]');-            End-            Else-            Begin-                Trk := Prim;-                LL.Add('[' + IntToStr(Trk.Layer) + ',' + IntToStr(Trk.x1) + ',' + IntToStr(Trk.y1) + ',' + IntToStr(Trk.x2) + ',' + IntToStr(Trk.y2) + ',' + IntToStr(Trk.Width) + ']');-            End;-        End;-        Prim := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-    Result := '{' + JRaw('ok','true') + ',' + JRaw('top', IntToStr(eTopOverlay)) + ',' + JRaw('bottom', IntToStr(eBottomOverlay)) + ',' +-        JRaw('comps', JoinList(LC)) + ',' + JRaw('texts', JoinList(LT)) + ',' + JRaw('lines', JoinList(LL)) + '}';-    LC.Free; LT.Free; LL.Free;-End;--Function CompByRef(Board : IPCB_Board; Ref : String) : IPCB_Component;-Var-    BIt  : IPCB_BoardIterator;-    Comp : IPCB_Component;-Begin-    Result := Nil;-    BIt := Board.BoardIterator_Create;-    BIt.AddFilter_ObjectSet(MkSet(eComponentObject));-    BIt.AddFilter_LayerSet(AllLayers);-    BIt.AddFilter_Method(eProcessAll);-    Comp := BIt.FirstPCBObject;-    While Comp <> Nil Do-    Begin-        If Comp.Name.Text = Ref Then Begin Result := Comp; Break; End;-        Comp := BIt.NextPCBObject;-    End;-    Board.BoardIterator_Destroy(BIt);-End;--{ apply_silk: payload lines, RAW units, one undo step.-    N|ref|x|y|size|rot|show       the designator (x,y = the text's own location)-    V|ref|x|y|size|rot|show       the comment (value)-    T|T or B|text|x|y|size|rot    a free overlay text (B = bottom overlay, mirrored)-    L|T or B|x1|y1|x2|y2|width    a free overlay line-    DT|layer|text|x|y             delete the free overlay text at exactly that spot-    DL|layer|x1|y1|x2|y2          delete the free overlay line with exactly those ends }-Procedure SetText(T : IPCB_Text; X, Y, Sz, Rot : Integer);-Begin-    T.BeginModify;-    T.UseTTFonts := False;-    T.Size := Sz;-    T.Width := Sz Div 7;-    T.Rotation := Rot;-    T.XLocation := X;-    T.YLocation := Y;-    T.EndModify;-    T.GraphicallyInvalidate;-End;--Function CmdApplySilk(Lines : TStringList; Payload : String) : String;-Var-    Board : IPCB_Board;-    Ops   : TStringList;-    Op, Kind, Res, Results : String;-    Comp  : IPCB_Component;-    Txt   : IPCB_Text;-    Trk   : IPCB_Track;-    BIt   : IPCB_BoardIterator;-    Prim  : IPCB_Primitive;-    Lay, I, Okay, Failed : Integer;-Begin-    Board := PCBServer.GetCurrentPCBBoard;-    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;-    Ops := TStringList.Create; Ops.Text := Payload;-    Results := ''; Okay := 0; Failed := 0;-    PCBServer.PreProcess;-    For I := 0 To Ops.Count - 1 Do-    Begin-        Op := Trim(Ops[I]);-        If Op = '' Then Continue;-        Kind := FieldP(Op, 1); Res := 'ok'; gInputError := '';-        If (Kind = 'N') Or (Kind = 'V') Then-        Begin-            Comp := CompByRef(Board, FieldP(Op, 2));-            If Comp = Nil Then Res := 'err:no part ' + FieldP(Op, 2)-            Else-            Begin-                Comp.BeginModify;-                If Kind = 'N' Then-                Begin-                    Comp.NameAutoPosition := eAutoPos_Manual;-                    Comp.NameOn := FieldP(Op, 7) <> '0';-                    SetText(Comp.Name, AdomInt(FieldP(Op, 3)), AdomInt(FieldP(Op, 4)), AdomInt(FieldP(Op, 5)), AdomInt(FieldP(Op, 6)));-                End-                Else-                Begin-                    Comp.CommentAutoPosition := eAutoPos_Manual;-                    Comp.CommentOn := FieldP(Op, 7) <> '0';-                    SetText(Comp.Comment, AdomInt(FieldP(Op, 3)), AdomInt(FieldP(Op, 4)), AdomInt(FieldP(Op, 5)), AdomInt(FieldP(Op, 6)));-                End;-                Comp.EndModify;-                Comp.GraphicallyInvalidate;-            End;-        End-        Else If (Kind = 'T') Or (Kind = 'L') Then-        Begin-            If FieldP(Op, 2) = 'B' Then Lay := eBottomOverlay Else Lay := eTopOverlay;-            If Kind = 'T' Then-            Begin-                Txt := PCBServer.PCBObjectFactory(eTextObject, eNoDimension, eCreate_Default);-                Txt.Layer := Lay;-                Txt.Text := FieldP(Op, 3);-                Txt.MirrorFlag := Lay = eBottomOverlay;-                SetText(Txt, AdomInt(FieldP(Op, 4)), AdomInt(FieldP(Op, 5)), AdomInt(FieldP(Op, 6)), AdomInt(FieldP(Op, 7)));-                If gInputError = '' Then RegisterPrim(Board, Txt);-            End-            Else-            Begin-                Trk := PCBServer.PCBObjectFactory(eTrackObject, eNoDimension, eCreate_Default);-                Trk.Layer := Lay;-                Trk.x1 := AdomInt(FieldP(Op, 3)); Trk.y1 := AdomInt(FieldP(Op, 4));-                Trk.x2 := AdomInt(FieldP(Op, 5)); Trk.y2 := AdomInt(FieldP(Op, 6));-                Trk.Width := AdomInt(FieldP(Op, 7));-                If gInputError = '' Then RegisterPrim(Board, Trk);-            End;-        End-        Else If (Kind = 'DT') Or (Kind = 'DL') Then-        Begin-            Res := 'err:not found';-            BIt := Board.BoardIterator_Create;-            If Kind = 'DT' Then BIt.AddFilter_ObjectSet(MkSet(eTextObject)) Else BIt.AddFilter_ObjectSet(MkSet(eTrackObject));-            BIt.AddFilter_LayerSet(MkSet(eTopOverlay, eBottomOverlay));-            BIt.AddFilter_Method(eProcessAll);-            Prim := BIt.FirstPCBObject;-            While Prim <> Nil Do-            Begin-                If (Not Prim.InComponent) And (IntToStr(Prim.Layer) = FieldP(Op, 2)) Then-                Begin-                    If Kind = 'DT' Then-                    Begin-                        Txt := Prim;-                        If (Txt.Text = FieldP(Op, 3)) And (IntToStr(Txt.XLocation) = FieldP(Op, 4)) And (IntToStr(Txt.YLocation) = FieldP(Op, 5)) Then Begin Res := 'ok'; Break; End;-                    End-                    Else-                    Begin-                        Trk := Prim;-                        If (IntToStr(Trk.x1) = FieldP(Op, 3)) And (IntToStr(Trk.y1) = FieldP(Op, 4)) And (IntToStr(Trk.x2) = FieldP(Op, 5)) And (IntToStr(Trk.y2) = FieldP(Op, 6)) Then Begin Res := 'ok'; Break; End;-                    End;-                End;-                Prim := BIt.NextPCBObject;-            End;-            Board.BoardIterator_Destroy(BIt);-            If Res = 'ok' Then Board.RemovePCBObject(Prim);-        End-        Else Res := 'err:unknown op ' + Kind;-        If gInputError <> '' Then Res := 'err:bad number ' + gInputError;-        If Copy(Res, 1, 3) = 'err' Then Failed := Failed + 1 Else Okay := Okay + 1;-        If Results <> '' Then Results := Results + ',';-        Results := Results + '"' + JsonEsc(Res) + '"';-    End;-    PCBServer.PostProcess;-    gInputError := '';-    Board.ViewManager_FullUpdate;-    Ops.Free;-    Result := '{' + JRaw('ok','true') + ',' + JRaw('applied', IntToStr(Okay)) + ',' + JRaw('failed', IntToStr(Failed)) + ',' + JRaw('results', '[' + Results + ']') + '}';-End;--{ close_old_agents: every earlier generation of this agent's script project still open in Altium's-  Projects panel (each start opens a new AdomAgent_<gen>.PrjScr; the old ones piled up, ten deep by-  2026-10-15). The running generation stays. Answers how many it closed. }-Function CloseOldAgents : Integer;-Var-    WS   : IWorkspace;-    Prj  : IProject;-    Doc  : IServerDocument;-    I    : Integer;-    N    : String;-Begin-    Result := 0;-    WS := GetWorkspace;-    If WS = Nil Then Exit;-    For I := WS.DM_ProjectCount - 1 DownTo 0 Do-    Begin-        Prj := WS.DM_Projects(I);-        If Prj = Nil Then Continue;-        N := Prj.DM_ProjectFullPath;-        If (Pos('\AdomAgent_', N) > 0) And (Pos('.PRJSCR', UpperCase(N)) > 0) And (Pos(AGENT_GEN, N) = 0) Then-        Begin-            Doc := Nil;-            Try-                Doc := Client.GetDocumentByPath(Prj.DM_ProjectFullPath);-            Except-                Doc := Nil;-            End;-            If Doc <> Nil Then-            Begin-                Try-                    Client.CloseDocument(Doc);-                    Result := Result + 1;-                Except-                End;-            End;-        End;-    End;-End;--Function CmdCloseOldAgents(Lines : TStringList) : String;-Begin-    { test hook for adom/altium-bridge#30, inert unless the bridge's tests drop this file: an agent that halts on its-      first command after starting }-    If FileExists(WORKSPACE + 'test_halt_after_start') Then AdomDeliberateUndeclaredIdentifierForSelfTest(30);-    Result := '{' + JRaw('ok','true') + ',' + JRaw('closed', IntToStr(CloseOldAgents)) + '}';-End;--{ ----------------------------------------------------------------------------------------------------------------  Schematic wiring as data (the EDA Bridge Standard's sch_* verbs). The bridge does the planning in Rust; the agent-  only dumps a sheet's wiring and applies a list of primitive steps as one undo step.-  Every item has a KEY made of its kind and raw coordinates (and text): Altium gives wires, labels and junctions no-  stable id, so the key is the id, valid until the item changes. }--Function I2S(N : Integer) : String;-Begin-    Result := IntToStr(N);-End;--Function SchKey(Obj : ISch_GraphicalObject) : String;-Var-    J : Integer;-    S : String;-Begin-    S := '';-    If (Obj.ObjectID = eWire) Or (Obj.ObjectID = eBus) Then-    Begin-        If Obj.ObjectID = eWire Then S := 'W:' Else S := 'B:';-        For J := 1 To Obj.VerticesCount Do-        Begin-            If J > 1 Then S := S + ',';-            S := S + I2S(Obj.Vertex[J].X) + ',' + I2S(Obj.Vertex[J].Y);-        End;-    End-    Else If Obj.ObjectID = eBusEntry Then S := 'E:' + I2S(Obj.Location.X) + ',' + I2S(Obj.Location.Y) + ',' + I2S(Obj.Corner.X) + ',' + I2S(Obj.Corner.Y)-    Else If Obj.ObjectID = eJunction Then S := 'J:' + I2S(Obj.Location.X) + ',' + I2S(Obj.Location.Y)-    Else If Obj.ObjectID = eNoERC Then S := 'X:' + I2S(Obj.Location.X) + ',' + I2S(Obj.Location.Y)-    Else If Obj.ObjectID = eNetLabel Then S := 'N:' + I2S(Obj.Location.X) + ',' + I2S(Obj.Location.Y) + ':' + Obj.Text-    Else If Obj.ObjectID = ePort Then S := 'P:' + I2S(Obj.Location.X) + ',' + I2S(Obj.Location.Y) + ':' + Obj.Name-    Else If Obj.ObjectID = ePowerObject Then S := 'O:' + I2S(Obj.Location.X) + ',' + I2S(Obj.Location.Y) + ':' + Obj.Text-    Else If Obj.ObjectID = eLabel Then S := 'T:' + I2S(Obj.Location.X) + ',' + I2S(Obj.Location.Y) + ':' + Obj.Text-    Else If Obj.ObjectID = eSheetSymbol Then S := 'S:' + I2S(Obj.Location.X) + ',' + I2S(Obj.Location.Y) + ':' + Obj.SheetName.Text;-    Result := S;-End;--{ sch_dump: every wiring item of the CURRENT sheet with its key and raw coordinates, every pin of every part (outer-  end = Location moved PinLength along Orientation, as sch_connect measured), and the sheet's page. k is-  MilsToCoord(1000): the bridge turns raw units into mm with it. }-Function CmdSchDump(Lines : TStringList) : String;-Var-    Doc  : ISch_Document;-    It, PIt, EIt : ISch_Iterator;-    Obj  : ISch_GraphicalObject;-    C    : ISch_Component;-    Pin  : ISch_Pin;-    En   : ISch_SheetEntry;-    S, P, V, En2 : String;-    J, Dir, HX, HY : Integer;-    BL, BB, BR, BT : Integer;-    HasB : Boolean;-    GIt  : ISch_Iterator;-    G    : ISch_GraphicalObject;-    R    : TCoordRect;-Begin-    Doc := SchServer.GetCurrentSchDocument;-    If (Doc = Nil) Or (Doc.ObjectID = eSchLib) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_schematic') + ',' + JStr('error','no current schematic sheet (open a .SchDoc first)') + '}'; Exit; End;-    S := ''; P := '';-    It := Doc.SchIterator_Create;-    It.AddFilter_ObjectSet(MkSet(eWire, eBus, eBusEntry, eJunction, eNoERC, eNetLabel, ePort, ePowerObject, eLabel, eSheetSymbol, eSchComponent));-    { top-level items only: a part's own texts and lines come with the part (they moved with it and then could not be-      found by key, measured 2026-10-27) }-    It.SetState_IterationDepth(eIterateFirstLevel);-    Obj := It.FirstSchObject;-    While Obj <> Nil Do-    Begin-        If Obj.ObjectID = eSchComponent Then-        Begin-            C := Obj;-            { the symbol's box (sheetDrawing): its pins and the body drawn for the part and display mode it shows,-              never its designator or comment }-            HasB := False; BL := 0; BB := 0; BR := 0; BT := 0;-            GIt := C.SchIterator_Create;-            GIt.AddFilter_ObjectSet(MkSet(eRectangle, eRoundRectangle, eLine, ePolyline, ePolygon, eArc, eEllipse, eEllipticalArc, ePie, eBezier));-            G := GIt.FirstSchObject;-            While G <> Nil Do-            Begin-                If ((G.OwnerPartId = C.CurrentPartID) Or (G.OwnerPartId = 0)) And (G.OwnerPartDisplayMode = C.DisplayMode) Then-                Begin-                    R := G.BoundingRectangle;-                    If Not HasB Then Begin BL := R.Left; BB := R.Bottom; BR := R.Right; BT := R.Top; HasB := True; End-                    Else Begin If R.Left < BL Then BL := R.Left; If R.Bottom < BB Then BB := R.Bottom; If R.Right > BR Then BR := R.Right; If R.Top > BT Then BT := R.Top; End;-                End;-                G := GIt.NextSchObject;-            End;-            C.SchIterator_Destroy(GIt);-            PIt := C.SchIterator_Create;-            PIt.AddFilter_ObjectSet(MkSet(ePin));-            Pin := PIt.FirstSchObject;-            While Pin <> Nil Do-            Begin-                If ((Pin.OwnerPartId = C.CurrentPartID) Or (Pin.OwnerPartId = 0)) And (Pin.OwnerPartDisplayMode = C.DisplayMode) Then-                Begin-                    Dir := Pin.Orientation;-                    HX := Pin.Location.X; HY := Pin.Location.Y;-                    If Dir = 0 Then HX := HX + Pin.PinLength-                    Else If Dir = 1 Then HY := HY + Pin.PinLength-                    Else If Dir = 2 Then HX := HX - Pin.PinLength-                    Else HY := HY - Pin.PinLength;-                    If Not HasB Then Begin BL := HX; BB := HY; BR := HX; BT := HY; HasB := True; End;-                    If HX < BL Then BL := HX; If HX > BR Then BR := HX; If HY < BB Then BB := HY; If HY > BT Then BT := HY;-                    If Pin.Location.X < BL Then BL := Pin.Location.X; If Pin.Location.X > BR Then BR := Pin.Location.X;-                    If Pin.Location.Y < BB Then BB := Pin.Location.Y; If Pin.Location.Y > BT Then BT := Pin.Location.Y;-                    If P <> '' Then P := P + ',';-                    P := P + '["' + JsonEsc(C.Designator.Text) + '","' + JsonEsc(Pin.Designator) + '","' + JsonEsc(Pin.Name) + '",' +-                        I2S(Pin.Electrical) + ',' + I2S(HX) + ',' + I2S(HY) + ',' + I2S(Dir) + ',' + JBool(Pin.IsHidden) + ',' +-                        I2S(Pin.Location.X) + ',' + I2S(Pin.Location.Y) + ']';-                End;-                Pin := PIt.NextSchObject;-            End;-            C.SchIterator_Destroy(PIt);-            If S <> '' Then S := S + ',';-            S := S + '{"t":"C","key":"","ref":"' + JsonEsc(C.Designator.Text) + '","x":' + I2S(C.Location.X) + ',"y":' + I2S(C.Location.Y) +-                ',"lib":"' + JsonEsc(C.LibReference) + '","libPath":"' + JsonEsc(C.LibraryPath) + '","part":' + I2S(C.CurrentPartID) + ',"rot":' + I2S(C.Orientation) + ',"mirror":' + JBool(C.IsMirrored) +-                ',"des":[' + I2S(C.Designator.Location.X) + ',' + I2S(C.Designator.Location.Y) + ',' + I2S(C.Designator.Orientation) + ',' + I2S(C.Designator.Justification) + ']' +-                ',"cmt":[' + I2S(C.Comment.Location.X) + ',' + I2S(C.Comment.Location.Y) + ',' + I2S(C.Comment.Orientation) + ',' + I2S(C.Comment.Justification) + ']' +-                ',"box":[' + I2S(BL) + ',' + I2S(BB) + ',' + I2S(BR) + ',' + I2S(BT) + ']}';-        End-        Else-        Begin-            V := '';-            If (Obj.ObjectID = eWire) Or (Obj.ObjectID = eBus) Then-            Begin-                For J := 1 To Obj.VerticesCount Do-                Begin-                    If J > 1 Then V := V + ',';-                    V := V + '[' + I2S(Obj.Vertex[J].X) + ',' + I2S(Obj.Vertex[J].Y) + ']';-                End;-                V := '"pts":[' + V + ']';-            End-            Else-            Begin-                V := '"x":' + I2S(Obj.Location.X) + ',"y":' + I2S(Obj.Location.Y);-                If Obj.ObjectID = eBusEntry Then V := V + ',"cx":' + I2S(Obj.Corner.X) + ',"cy":' + I2S(Obj.Corner.Y)-                Else If (Obj.ObjectID = eNetLabel) Or (Obj.ObjectID = eLabel) Then V := V + ',"text":"' + JsonEsc(Obj.Text) + '","rot":' + I2S(Obj.Orientation) + ',"just":' + I2S(Obj.Justification)-                Else If Obj.ObjectID = ePowerObject Then V := V + ',"text":"' + JsonEsc(Obj.Text) + '","rot":' + I2S(Obj.Orientation) + ',"style":' + I2S(Obj.Style)-                Else If Obj.ObjectID = ePort Then V := V + ',"text":"' + JsonEsc(Obj.Name) + '","w":' + I2S(Obj.Width) + ',"style":' + I2S(Obj.Style) + ',"io":' + I2S(Obj.IOType)-                Else If Obj.ObjectID = eSheetSymbol Then-                Begin-                    En2 := '';-                    EIt := Obj.SchIterator_Create;-                    EIt.AddFilter_ObjectSet(MkSet(eSheetEntry));-                    En := EIt.FirstSchObject;-                    While En <> Nil Do-                    Begin-                        If En2 <> '' Then En2 := En2 + ',';-                        En2 := En2 + '["' + JsonEsc(En.Name) + '",' + I2S(En.Side) + ',' + I2S(En.DistanceFromTop) + ',' + I2S(En.IOType) + ']';-                        En := EIt.NextSchObject;-                    End;-                    Obj.SchIterator_Destroy(EIt);-                    V := V + ',"text":"' + JsonEsc(Obj.SheetName.Text) + '","file":"' + JsonEsc(Obj.SheetFileName.Text) + '","w":' + I2S(Obj.XSize) + ',"h":' + I2S(Obj.YSize) + ',"entries":[' + En2 + ']';-                End;-            End;-            If S <> '' Then S := S + ',';-            S := S + '{"t":"' + Copy(SchKey(Obj), 1, 1) + '","key":"' + JsonEsc(SchKey(Obj)) + '",' + V + '}';-        End;-        Obj := It.NextSchObject;-    End;-    Doc.SchIterator_Destroy(It);-    Result := '{' + JRaw('ok','true') + ',' + JStr('document', Doc.DocumentName) + ',' + JRaw('k', I2S(MilsToCoord(1000))) + ',' +-        JRaw('page', '{"style":' + I2S(Doc.SheetStyle) + ',"custom":' + JBool(Doc.UseCustomSheet) + ',"cx":' + I2S(Doc.CustomX) + ',"cy":' + I2S(Doc.CustomY) + ',"orientation":' + I2S(Doc.WorkspaceOrientation) + '}') + ',' +-        JRaw('items', '[' + S + ']') + ',' + JRaw('pins', '[' + P + ']') + '}';-End;--Function SchFindByKey(Doc : ISch_Document; Key : String) : ISch_GraphicalObject;-Var-    It  : ISch_Iterator;-    Obj : ISch_GraphicalObject;-Begin-    Result := Nil;-    It := Doc.SchIterator_Create;-    It.AddFilter_ObjectSet(MkSet(eWire, eBus, eBusEntry, eJunction, eNoERC, eNetLabel, ePort, ePowerObject, eLabel, eSheetSymbol));-    It.SetState_IterationDepth(eIterateFirstLevel);-    Obj := It.FirstSchObject;-    While Obj <> Nil Do-    Begin-        If SchKey(Obj) = Key Then Begin Result := Obj; Break; End;-        Obj := It.NextSchObject;-    End;-    Doc.SchIterator_Destroy(It);-End;--Procedure SchRegister(Doc : ISch_Document; Obj : ISch_GraphicalObject);-Begin-    Doc.RegisterSchObjectInContainer(Obj);-    SchServer.RobotManager.SendMessage(Doc.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, Obj.I_ObjectAddress);-End;--{ points "x,y,x,y,..." (raw) onto a new or existing wire/bus }-Procedure SchSetVertices(Obj : ISch_GraphicalObject; Pts : String);-Var-    L : TStringList;-    J, N : Integer;-Begin-    L := TStringList.Create; L.Delimiter := ','; L.StrictDelimiter := True; L.DelimitedText := Pts;-    N := L.Count Div 2;-    Obj.Location := Point(AdomInt(L[0]), AdomInt(L[1]));-    Obj.VerticesCount := N;-    For J := 1 To N Do Obj.Vertex[J] := Point(AdomInt(L[2 * J - 2]), AdomInt(L[2 * J - 1]));-    L.Free;-End;--{ sch_ops: primitive steps, one undo step. Fields after the kind letter, | separated, raw coordinates:-    W|pts      a new wire through the points "x,y,x,y,..."      B|pts   a new bus-    E|x|y|cx|cy  a bus entry from (x,y) to the corner (cx,cy)      J|x|y   a junction      X|x|y  a No ERC marker-    N|text|x|y|rot|just  a net label                               T|text|x|y|rot|pt|bold|italic|just  free text-    D|key      delete the item with that key                       V|key|pts  give a wire/bus new points-    G|key|dx|dy  move an item by dx, dy                            L|key|text|x|y|rot  edit a net label-    C|ref|dx|dy  move a part by dx, dy-  Each step answers ok, or why not, and the key it made or touched. }-Function CmdSchOps(Lines : TStringList; Payload : String) : String;-Var-    Doc  : ISch_Document;-    Ops  : TStringList;-    CL   : TInterfaceList;-    Obj  : ISch_GraphicalObject;-    C    : ISch_Component;-    En   : ISch_SheetEntry;-    EIt  : ISch_Iterator;-    Op, K, Res, NewKey, S : String;-    I, J, NOk : Integer;-Begin-    Doc := SchServer.GetCurrentSchDocument;-    If (Doc = Nil) Or (Doc.ObjectID = eSchLib) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_schematic') + ',' + JStr('error','no current schematic sheet (open a .SchDoc first)') + '}'; Exit; End;-    Ops := TStringList.Create; Ops.Text := Payload;-    S := ''; NOk := 0;-    SchServer.ProcessControl.PreProcess(Doc, '');-    For I := 0 To Ops.Count - 1 Do-    Begin-        Op := Ops[I];-        If Trim(Op) = '' Then Continue;-        K := FieldP(Op, 1); Res := 'ok'; NewKey := ''; gInputError := '';-        If (K = 'W') Or (K = 'B') Then-        Begin-            If K = 'W' Then Obj := SchServer.SchObjectFactory(eWire, eCreate_Default) Else Obj := SchServer.SchObjectFactory(eBus, eCreate_Default);-            SchSetVertices(Obj, FieldP(Op, 2));-            Obj.LineWidth := eSmall;-            SchRegister(Doc, Obj); NewKey := SchKey(Obj);-        End-        Else If K = 'E' Then-        Begin-            Obj := SchServer.SchObjectFactory(eBusEntry, eCreate_Default);-            Obj.Location := Point(AdomInt(FieldP(Op, 2)), AdomInt(FieldP(Op, 3)));-            Obj.Corner := Point(AdomInt(FieldP(Op, 4)), AdomInt(FieldP(Op, 5)));-            SchRegister(Doc, Obj); NewKey := SchKey(Obj);-        End-        Else If (K = 'J') Or (K = 'X') Then-        Begin-            If K = 'J' Then Obj := SchServer.SchObjectFactory(eJunction, eCreate_Default) Else Obj := SchServer.SchObjectFactory(eNoERC, eCreate_Default);-            Obj.Location := Point(AdomInt(FieldP(Op, 2)), AdomInt(FieldP(Op, 3)));-            SchRegister(Doc, Obj); NewKey := SchKey(Obj);-        End-        Else If K = 'N' Then-        Begin-            Obj := SchServer.SchObjectFactory(eNetLabel, eCreate_Default);-            Obj.Text := FieldP(Op, 2);-            Obj.Location := Point(AdomInt(FieldP(Op, 3)), AdomInt(FieldP(Op, 4)));-            Obj.Orientation := AdomInt(FieldP(Op, 5));-            If FieldP(Op, 6) <> '' Then Obj.Justification := AdomInt(FieldP(Op, 6));-            SchRegister(Doc, Obj); NewKey := SchKey(Obj);-        End-        Else If K = 'O' Then-        Begin-            { O|net|x|y|orientation: a power port, Altium's global net label }-            Obj := SchServer.SchObjectFactory(ePowerObject, eCreate_Default);-            Obj.Text := FieldP(Op, 2);-            Obj.Location := Point(AdomInt(FieldP(Op, 3)), AdomInt(FieldP(Op, 4)));-            Obj.Style := PowerStyleOf(FieldP(Op, 2));-            Obj.Orientation := AdomInt(FieldP(Op, 5));-            Obj.ShowNetName := True;-            SchRegister(Doc, Obj); NewKey := SchKey(Obj);-        End-        Else If K = 'P' Then-        Begin-            { P|name|x|y|left(1/0): a port, Altium's hierarchical label, its connecting end at x,y }-            Obj := SchServer.SchObjectFactory(ePort, eCreate_Default);-            Obj.Name := FieldP(Op, 2);-            Obj.Width := MilsToCoord(600);-            Obj.IOType := ePortUnspecified;-            If FieldP(Op, 5) = '1' Then Begin Obj.Style := ePortLeft; Obj.Location := Point(AdomInt(FieldP(Op, 3)) - Obj.Width, AdomInt(FieldP(Op, 4))); End-            Else Begin Obj.Style := ePortRight; Obj.Location := Point(AdomInt(FieldP(Op, 3)), AdomInt(FieldP(Op, 4))); End;-            SchRegister(Doc, Obj); NewKey := SchKey(Obj);-        End-        Else If K = 'T' Then-        Begin-            Obj := SchServer.SchObjectFactory(eLabel, eCreate_Default);-            Obj.Text := FieldP(Op, 2);-            Obj.Location := Point(AdomInt(FieldP(Op, 3)), AdomInt(FieldP(Op, 4)));-            Obj.Orientation := AdomInt(FieldP(Op, 5));-            If AdomInt(FieldP(Op, 6)) > 0 Then-                Obj.FontId := SchServer.FontManager.GetFontID(AdomInt(FieldP(Op, 6)), 0, False, FieldP(Op, 8) = '1', FieldP(Op, 7) = '1', False, 'Times New Roman');-            If FieldP(Op, 9) <> '' Then Obj.Justification := AdomInt(FieldP(Op, 9));-            SchRegister(Doc, Obj); NewKey := SchKey(Obj);-        End-        Else If K = 'C' Then-        Begin-            CL := SheetCompsOf(Doc, FieldP(Op, 2));-            If CL.Count = 0 Then Res := 'designator not on this sheet';-            For J := 0 To CL.Count - 1 Do-            Begin-                C := CL[J];-                SchServer.RobotManager.SendMessage(C.I_ObjectAddress, c_BroadCast, SCHM_BeginModify, c_NoEventData);-                C.MoveByXY(AdomInt(FieldP(Op, 3)), AdomInt(FieldP(Op, 4)));-                SchServer.RobotManager.SendMessage(C.I_ObjectAddress, c_BroadCast, SCHM_EndModify, c_NoEventData);-                C.GraphicallyInvalidate;-            End;-            CL.Free;-            NewKey := FieldP(Op, 2);-        End-        Else If K = 'A' Then-        Begin-            { A|style|custom(1/0)|cx|cy|orientation: the page (empty keeps a field) }-            If FieldP(Op, 2) <> '' Then Doc.SheetStyle := AdomInt(FieldP(Op, 2));-            If FieldP(Op, 3) <> '' Then Doc.UseCustomSheet := FieldP(Op, 3) = '1';-            If FieldP(Op, 4) <> '' Then Doc.CustomX := AdomInt(FieldP(Op, 4));-            If FieldP(Op, 5) <> '' Then Doc.CustomY := AdomInt(FieldP(Op, 5));-            If FieldP(Op, 6) <> '' Then Doc.WorkspaceOrientation := AdomInt(FieldP(Op, 6));-        End-        Else If K = 'F' Then-        Begin-            { F|ref|designator or comment|x|y|orientation|justification: put a part's field text there }-            CL := SheetCompsOf(Doc, FieldP(Op, 2));-            If CL.Count = 0 Then Res := 'designator not on this sheet';-            For J := 0 To CL.Count - 1 Do-            Begin-                C := CL[J];-                SchServer.RobotManager.SendMessage(C.I_ObjectAddress, c_BroadCast, SCHM_BeginModify, c_NoEventData);-                If LowerCase(FieldP(Op, 3)) = 'comment' Then Obj := C.Comment Else Obj := C.Designator;-                Obj.Location := Point(AdomInt(FieldP(Op, 4)), AdomInt(FieldP(Op, 5)));-                Obj.Orientation := AdomInt(FieldP(Op, 6));-                Obj.Justification := AdomInt(FieldP(Op, 7));-                SchServer.RobotManager.SendMessage(C.I_ObjectAddress, c_BroadCast, SCHM_EndModify, c_NoEventData);-                C.GraphicallyInvalidate;-            End;-            CL.Free;-        End-        Else If (K = 'Y') Or (K = 'Z') Or (K = 'Q') Then-        Begin-            { sheet symbol pins. Y|symkey|name|side|dist|io adds one; Z|symkey|name deletes it;-              Q|symkey|name|newname|side|dist|io edits it (empty keeps a field) }-            Obj := SchFindByKey(Doc, FieldP(Op, 2));-            If Obj = Nil Then Res := 'not found: the sheet symbol changed since it was read'-            Else-            Begin-                SchServer.RobotManager.SendMessage(Obj.I_ObjectAddress, c_BroadCast, SCHM_BeginModify, c_NoEventData);-                If K = 'Y' Then-                Begin-                    En := SchServer.SchObjectFactory(eSheetEntry, eCreate_Default);-                    En.Name := FieldP(Op, 3);-                    En.Side := AdomInt(FieldP(Op, 4));-                    En.DistanceFromTop := AdomInt(FieldP(Op, 5));-                    En.IOType := AdomInt(FieldP(Op, 6));-                    Obj.AddSchObject(En);-                    SchServer.RobotManager.SendMessage(Obj.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, En.I_ObjectAddress);-                End-                Else-                Begin-                    EIt := Obj.SchIterator_Create;-                    EIt.AddFilter_ObjectSet(MkSet(eSheetEntry));-                    En := EIt.FirstSchObject;-                    While (En <> Nil) And (UpperCase(En.Name) <> UpperCase(FieldP(Op, 3))) Do En := EIt.NextSchObject;-                    Obj.SchIterator_Destroy(EIt);-                    If En = Nil Then Res := 'no pin ' + FieldP(Op, 3)-                    Else If K = 'Z' Then Obj.RemoveSchObject(En)-                    Else-                    Begin-                        If FieldP(Op, 4) <> '' Then En.Name := FieldP(Op, 4);-                        If FieldP(Op, 5) <> '' Then En.Side := AdomInt(FieldP(Op, 5));-                        If FieldP(Op, 6) <> '' Then En.DistanceFromTop := AdomInt(FieldP(Op, 6));-                        If FieldP(Op, 7) <> '' Then En.IOType := AdomInt(FieldP(Op, 7));-                    End;-                End;-                SchServer.RobotManager.SendMessage(Obj.I_ObjectAddress, c_BroadCast, SCHM_EndModify, c_NoEventData);-                Obj.GraphicallyInvalidate;-                NewKey := SchKey(Obj);-            End;-        End-        Else If (K = 'D') Or (K = 'V') Or (K = 'G') Or (K = 'L') Then-        Begin-            Obj := SchFindByKey(Doc, FieldP(Op, 2));-            If Obj = Nil Then Res := 'not found: the item changed since it was read'-            Else If K = 'D' Then-            Begin-                Doc.RemoveSchObject(Obj);-                SchServer.RobotManager.SendMessage(Doc.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, Obj.I_ObjectAddress);-            End-            Else-            Begin-                SchServer.RobotManager.SendMessage(Obj.I_ObjectAddress, c_BroadCast, SCHM_BeginModify, c_NoEventData);-                If K = 'V' Then SchSetVertices(Obj, FieldP(Op, 3))-                Else If K = 'G' Then Obj.MoveByXY(AdomInt(FieldP(Op, 3)), AdomInt(FieldP(Op, 4)))-                Else-                Begin-                    If FieldP(Op, 3) <> '' Then Obj.Text := FieldP(Op, 3);-                    If FieldP(Op, 4) <> '' Then Obj.Location := Point(AdomInt(FieldP(Op, 4)), AdomInt(FieldP(Op, 5)));-                    If FieldP(Op, 6) <> '' Then Obj.Orientation := AdomInt(FieldP(Op, 6));-                End;-                SchServer.RobotManager.SendMessage(Obj.I_ObjectAddress, c_BroadCast, SCHM_EndModify, c_NoEventData);-                Obj.GraphicallyInvalidate;-                NewKey := SchKey(Obj);-            End;-        End-        Else Res := 'unknown step ' + K;-        If gInputError <> '' Then Res := 'bad number ' + gInputError;-        If Res = 'ok' Then NOk := NOk + 1;-        If S <> '' Then S := S + ',';-        S := S + '{' + JStr('result', Res) + ',' + JStr('key', NewKey) + '}';-    End;-    SchServer.ProcessControl.PostProcess(Doc, '');-    K := MarkSchModified(Doc);-    Doc.GraphicallyInvalidate;-    Ops.Free;-    Result := '{' + JRaw('ok','true') + ',' + JStr('document', Doc.DocumentName) + ',' + JStr('marked', K) + ',' + JRaw('applied', I2S(NOk)) + ',' + JRaw('results', '[' + S + ']') + '}';-End;---{ docs: every document open in Altium (each server's documents): file, kind, unsaved changes, and which one is in-  front (the focused view's document). }-Function CmdDocs(Lines : TStringList) : String;-Var-    SM   : IServerModule;-    D    : IServerDocument;-    V    : IServerDocumentView;-    S, Cur, Kd : String;-    I, J : Integer;-    Md   : Boolean;-Begin-    S := ''; Cur := '';-    Try-        V := Client.GetCurrentView;-        If (V <> Nil) And (V.OwnerDocument <> Nil) Then Cur := V.OwnerDocument.FileName;-    Except-    End;-    For I := 0 To Client.Count - 1 Do-    Begin-        SM := Client.ServerModule[I];-        If SM = Nil Then Continue;-        For J := 0 To SM.DocumentCount - 1 Do-        Begin-            D := SM.Documents[J];-            If D = Nil Then Continue;-            Kd := ''; Md := False;-            Try Kd := D.Kind; Except End;-            Try If D.Modified Then Md := True; Except End;-            Try If D.GetModified Then Md := True; Except End;-            If S <> '' Then S := S + ',';-            S := S + '{' + JStr('file', D.FileName) + ',' + JStr('kind', Kd) + ',' + JRaw('modified', JBool(Md)) + '}';-        End;-    End;-    Result := '{' + JRaw('ok','true') + ',' + JStr('current', Cur) + ',' + JRaw('documents', '[' + S + ']') + '}';-End;--{ Make the PcbLib at Path the current document (inside Altium), so GetCurrentPCBLibrary is that library. '' when it is,-  else why not. An empty Path leaves the current library as it is. }-Function CurrentLibAt(Path : String) : String;-Var-    SD : IServerDocument;-    PL : IPCB_Library;-Begin-    Result := '';-    If Path = '' Then Exit;-    SD := DocAt(Path);-    If SD = Nil Then Begin Result := Path + ' is not open in Altium'; Exit; End;-    PL := PCBServer.GetCurrentPCBLibrary;-    If (PL <> Nil) And (SamePathText(PL.Board.FileName) = SamePathText(Path)) Then Exit;-    Client.ShowDocument(SD);-    PL := PCBServer.GetCurrentPCBLibrary;-    If (PL = Nil) Or (SamePathText(PL.Board.FileName) <> SamePathText(Path)) Then Result := Path + ' did not become the current library';-End;--{ edit_fp: edit one footprint of the CURRENT PcbLib in place. name= the footprint; the payload is one operation a line,-  positions in mm from the footprint's origin (the library's, as lib_fp reads them):-    PS|pad|size|w|h   PS|pad|at|x|y   PS|pad|shape|rect|round|roundrect   PS|pad|drill|d   PS|pad|rotation|deg-    PS|pad|layer|top|bottom|multi   PS|pad|type|smd|pth|npth   PS|pad|number|new-    PD|pad   PA|name|x|y|w|h|shape|drill|layer|rot|type   GD|layerId   GL|layerId|x1|y1|x2|y2|w   GA|layerId|cx|cy|r|w|a1|a2-    MD|description   BD   (every 3D body)-  Each answers ok:<what> or err:<why>, in order; one undo step. }--{ the first drawing primitive (not a pad, a text or a body) on a layer of the footprint; layer 0 = the first 3D body }-Function FpFirstOn(Fp : IPCB_LibComponent; Lay : Integer) : IPCB_Primitive;-Var-    GIt : IPCB_GroupIterator;-    P   : IPCB_Primitive;-Begin-    Result := Nil;-    GIt := Fp.GroupIterator_Create;-    If Lay = 0 Then GIt.AddFilter_ObjectSet(MkSet(eComponentBodyObject))-    Else GIt.AddFilter_ObjectSet(MkSet(eTrackObject, eArcObject, eFillObject, eRegionObject));-    GIt.AddFilter_LayerSet(AllLayers);-    P := GIt.FirstPCBObject;-    While P <> Nil Do-    Begin-        If (Lay = 0) Or (P.Layer = Lay) Then Begin Result := P; Break; End;-        P := GIt.NextPCBObject;-    End;-    Fp.GroupIterator_Destroy(GIt);-End;--Function FpShape(S : String) : Integer;-Begin-    If S = 'rect' Then Result := eRectangular-    Else If S = 'roundrect' Then Result := eRoundedRectangular-    Else Result := eRounded;-End;--Function FpLayer(S : String) : Integer;-Begin-    If S = 'multi' Then Result := eMultiLayer-    Else If S = 'bottom' Then Result := eBottomLayer-    Else Result := eTopLayer;-End;--Function CmdEditFp(Lines : TStringList; Payload : String) : String;-Var-    LibErr : String;-    PLib  : IPCB_Library;-    Fp    : IPCB_LibComponent;-    Ops   : TStringList;-    Op, Kind, Key, Res, Results : String;-    Pad   : IPCB_Pad;-    Trk   : IPCB_Track;-    Arc   : IPCB_Arc;-    Prim, Cpy : IPCB_Primitive;-    Old   : IPCB_LibComponent;-    GIt   : IPCB_GroupIterator;-    I, K, OX, OY, Lay, Okay, Failed : Integer;-Begin-    { the library the bridge names, as the open document: a library loaded by path is a separate copy (pads added or-      deleted on it never reached the document, 2026-10-28), and open shows a document without making it current }-    LibErr := CurrentLibAt(Param(Lines, 'path'));-    If LibErr <> '' Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','library_not_current') + ',' + JStr('error', LibErr) + '}'; Exit; End;-    PLib := PCBServer.GetCurrentPCBLibrary;-    If PLib = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','not_a_pcblib') + ',' + JStr('error','the current document is not a PCB library') + '}'; Exit; End;-    Fp := FindFp(PLib, Param(Lines, 'name'));-    If Fp = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','footprint_not_found') + ',' + JStr('error','no footprint ' + Param(Lines, 'name') + ' in this library') + '}'; Exit; End;-    { the footprint on display sits on the library's origin (see lib_fp): show it before editing }-    If PLib.CurrentComponent <> Fp Then Begin PLib.CurrentComponent := Fp; PLib.Board.ViewManager_FullUpdate; End;-    OX := PLib.Board.XOrigin; OY := PLib.Board.YOrigin;-    Ops := TStringList.Create;-    Ops.Text := Payload;-    Results := ''; Okay := 0; Failed := 0;-    PCBServer.PreProcess;-    { the edit is made on a copy that is then registered in the footprint's place: on a registered footprint, pads-      and lines added or removed did not reach it (only changes to existing pads did, Develop 2026-10-28), while a-      footprint built before it is registered takes them all (build_footprint, xcopy) }-    Old := Fp;-    Fp := PCBServer.CreatePCBLibComp;-    Fp.Name := Old.Name;-    Fp.Description := Old.Description;-    Fp.Height := Old.Height;-    GIt := Old.GroupIterator_Create;-    GIt.AddFilter_LayerSet(AllLayers);-    Prim := GIt.FirstPCBObject;-    While Prim <> Nil Do-    Begin-        { the footprint on display reads from the library's origin; one not yet registered keeps its own frame,-          which Altium puts on the origin when it shows it (the copy came out 1270 mm off without this) }-        Cpy := Prim.Replicate;-        Cpy.MoveByXY(-OX, -OY);-        Fp.AddPCBObject(Cpy);-        Prim := GIt.NextPCBObject;-    End;-    Old.GroupIterator_Destroy(GIt);-    OX := 0; OY := 0;-    For I := 0 To Ops.Count - 1 Do-    Begin-        Op := Trim(Ops[I]);-        If Op = '' Then Continue;-        Kind := FieldP(Op, 1);-        Res := 'ok';-        gInputError := '';-        If (Kind = 'PS') Or (Kind = 'PD') Then-        Begin-            Pad := FpPad(Fp, FieldP(Op, 2));-            If Pad = Nil Then Res := 'err:no pad ' + FieldP(Op, 2)-            Else If Kind = 'PD' Then-            Begin-                Fp.RemovePCBObject(Pad);-                Res := 'ok:deleted pad ' + FieldP(Op, 2);-            End-            Else-            Begin-                Key := FieldP(Op, 3);-                Pad.BeginModify;-                If Key = 'size' Then Begin Pad.TopXSize := MmC(FieldP(Op, 4)); Pad.TopYSize := MmC(FieldP(Op, 5)); End-                Else If Key = 'at' Then Begin Pad.X := OX + MmC(FieldP(Op, 4)); Pad.Y := OY + MmC(FieldP(Op, 5)); End-                Else If Key = 'shape' Then Pad.TopShape := FpShape(FieldP(Op, 4))-                Else If Key = 'drill' Then Pad.HoleSize := MmC(FieldP(Op, 4))-                Else If Key = 'rotation' Then Pad.Rotation := AdomFloat(FieldP(Op, 4))-                Else If Key = 'layer' Then Pad.Layer := FpLayer(FieldP(Op, 4))-                Else If Key = 'type' Then-                Begin-                    If FieldP(Op, 4) = 'smd' Then Begin Pad.Layer := eTopLayer; Pad.HoleSize := 0; End-                    Else Begin Pad.Layer := eMultiLayer; Pad.Plated := FieldP(Op, 4) = 'pth'; End;-                End-                Else If Key = 'number' Then Pad.Name := FieldP(Op, 4)-                Else Res := 'err:unknown pad field ' + Key;-                Pad.EndModify;-                If Res = 'ok' Then Res := 'ok:pad ' + FieldP(Op, 2) + ' ' + Key;-            End;-        End-        Else If Kind = 'PA' Then-        Begin-            If FpPad(Fp, FieldP(Op, 2)) <> Nil Then Res := 'err:pad ' + FieldP(Op, 2) + ' exists'-            Else-            Begin-                Pad := PCBServer.PCBObjectFactory(ePadObject, eNoDimension, eCreate_Default);-                Pad.Name := FieldP(Op, 2);-                Pad.X := OX + MmC(FieldP(Op, 3)); Pad.Y := OY + MmC(FieldP(Op, 4));-                Pad.TopXSize := MmC(FieldP(Op, 5)); Pad.TopYSize := MmC(FieldP(Op, 6));-                Pad.TopShape := FpShape(FieldP(Op, 7));-                Pad.HoleSize := MmC(FieldP(Op, 8));-                Pad.Layer := FpLayer(FieldP(Op, 9));-                Pad.Rotation := AdomFloat(FieldP(Op, 10));-                If FieldP(Op, 11) = 'npth' Then Pad.Plated := False;-                Fp.AddPCBObject(Pad);-                Res := 'ok:added pad ' + FieldP(Op, 2);-            End;-        End-        Else If (Kind = 'GD') Or (Kind = 'BD') Then-        Begin-            If Kind = 'BD' Then Lay := 0 Else Lay := AdomInt(FieldP(Op, 2));-            K := 0;-            Prim := FpFirstOn(Fp, Lay);-            While (Prim <> Nil) And (K < 5000) Do-            Begin-                Fp.RemovePCBObject(Prim);-                K := K + 1;-                Prim := FpFirstOn(Fp, Lay);-            End;-            If Kind = 'BD' Then Res := 'ok:deleted ' + IntToStr(K) + ' 3D bodies'-            Else Res := 'ok:deleted ' + IntToStr(K) + ' shapes on layer ' + FieldP(Op, 2);-        End-        Else If Kind = 'GL' Then-        Begin-            Trk := PCBServer.PCBObjectFactory(eTrackObject, eNoDimension, eCreate_Default);-            Trk.Layer := AdomInt(FieldP(Op, 2));-            Trk.X1 := OX + MmC(FieldP(Op, 3)); Trk.Y1 := OY + MmC(FieldP(Op, 4));-            Trk.X2 := OX + MmC(FieldP(Op, 5)); Trk.Y2 := OY + MmC(FieldP(Op, 6));-            Trk.Width := MmC(FieldP(Op, 7));-            Fp.AddPCBObject(Trk);-            Res := 'ok:line on layer ' + FieldP(Op, 2);-        End-        Else If Kind = 'GA' Then-        Begin-            Arc := PCBServer.PCBObjectFactory(eArcObject, eNoDimension, eCreate_Default);-            Arc.Layer := AdomInt(FieldP(Op, 2));-            Arc.XCenter := OX + MmC(FieldP(Op, 3)); Arc.YCenter := OY + MmC(FieldP(Op, 4));-            Arc.Radius := MmC(FieldP(Op, 5));-            Arc.LineWidth := MmC(FieldP(Op, 6));-            Arc.StartAngle := AdomFloat(FieldP(Op, 7)); Arc.EndAngle := AdomFloat(FieldP(Op, 8));-            Fp.AddPCBObject(Arc);-            Res := 'ok:arc on layer ' + FieldP(Op, 2);-        End-        Else If Kind = 'MD' Then-        Begin-            Fp.Description := FieldP(Op, 2);-            Res := 'ok:description';-        End-        Else Res := 'err:unknown op ' + Kind;-        If (gInputError <> '') And (Copy(Res, 1, 3) <> 'err') Then Res := 'err:bad number ' + gInputError;-        If Copy(Res, 1, 3) = 'err' Then Failed := Failed + 1 Else Okay := Okay + 1;-        If Results <> '' Then Results := Results + ',';-        Results := Results + '"' + JsonEsc(Res) + '"';-    End;-    PLib.RemoveComponent(Old);-    PLib.RegisterComponent(Fp);-    PCBServer.PostProcess;-    gInputError := '';-    Ops.Free;-    PLib.CurrentComponent := Fp;-    PLib.Board.ViewManager_FullUpdate;-    PLib.RefreshView;-    Result := '{' + JRaw('ok','true') + ',' + JStr('footprint', Fp.Name) + ',' + JRaw('applied', IntToStr(Okay)) + ',' + JRaw('failed', IntToStr(Failed)) + ',' +-        JRaw('results', '[' + Results + ']') + '}';-End;---{ lib_fp: one footprint of the CURRENT PcbLib in full: name=<footprint>. Pads (mil, hole, layer, shape, rotation,-  plated), other primitives counted per layer, the bounding box, description, height and 3D bodies. }-Function CmdLibFootprint(Lines : TStringList) : String;-Var-    LibErr : String;-    PLib  : IPCB_Library;-    Fp    : IPCB_LibComponent;-    GIt   : IPCB_GroupIterator;-    Prim  : IPCB_Primitive;-    Pad   : IPCB_Pad;-    S, B, L, Nm, Mf : String;-    I, OX, OY : Integer;-    R : TCoordRect;-Begin-    { the library the bridge names, as the open document: a library loaded by path is a separate copy (pads added or-      deleted on it never reached the document, 2026-10-28), and open shows a document without making it current }-    LibErr := CurrentLibAt(Param(Lines, 'path'));-    If LibErr <> '' Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','library_not_current') + ',' + JStr('error', LibErr) + '}'; Exit; End;-    PLib := PCBServer.GetCurrentPCBLibrary;-    If PLib = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','not_a_pcblib') + ',' + JStr('error','the current document is not a PCB library') + '}'; Exit; End;-    Nm := Param(Lines, 'name'); Fp := Nil;-    For I := 0 To PLib.ComponentCount - 1 Do-        If UpperCase(PLib.GetComponent(I).Name) = UpperCase(Nm) Then Begin Fp := PLib.GetComponent(I); Break; End;-    If Fp = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','footprint_not_found') + ',' + JStr('error','no footprint ' + Nm + ' in this library') + '}'; Exit; End;-    S := ''; B := ''; L := '';-    { positions from the footprint's origin. Only the footprint on display sits on the library's origin; another-      keeps raw coordinates near 0 (one library read +1270 mm, another -1272.7 mm, 2026-10-28), so it is shown first }-    If PLib.CurrentComponent <> Fp Then Begin PLib.CurrentComponent := Fp; PLib.Board.ViewManager_FullUpdate; End;-    OX := PLib.Board.XOrigin; OY := PLib.Board.YOrigin;-    GIt := Fp.GroupIterator_Create;-    GIt.AddFilter_LayerSet(AllLayers);-    Prim := GIt.FirstPCBObject;-    While Prim <> Nil Do-    Begin-        If Prim.ObjectId = ePadObject Then-        Begin-            Pad := Prim;-            If S <> '' Then S := S + ',';-            S := S + '[' + '"' + JsonEsc(Pad.Name) + '",' + IntToStr(Pad.x - OX) + ',' + IntToStr(Pad.y - OY) + ',' + IntToStr(Pad.TopXSize) + ',' + IntToStr(Pad.TopYSize) + ',' +-                IntToStr(Pad.HoleSize) + ',' + IntToStr(Pad.Layer) + ',' + IntToStr(Pad.TopShape) + ',' + IntToStr(Round(Pad.Rotation * 1000)) + ',' + JBool(Pad.Plated) + ']';-        End-        Else If Prim.ObjectId = eComponentBodyObject Then-        Begin-            Mf := '';-            Try If Prim.Model <> Nil Then Mf := Prim.Model.FileName; Except End;-            If B <> '' Then B := B + ',';-            B := B + '{' + JStr('model', Mf) + ',' + JRaw('overallHeight', IntToStr(Prim.OverallHeight)) + ',' + JRaw('standoff', IntToStr(Prim.StandoffHeight)) + '}';-        End-        Else-        Begin-            R := Prim.BoundingRectangle;-            If L <> '' Then L := L + ',';-            L := L + '[' + IntToStr(Prim.ObjectId) + ',' + IntToStr(Prim.Layer) + ',' + IntToStr(R.Left - OX) + ',' + IntToStr(R.Bottom - OY) + ',' + IntToStr(R.Right - OX) + ',' + IntToStr(R.Top - OY) + ']';-        End;-        Prim := GIt.NextPCBObject;-    End;-    Fp.GroupIterator_Destroy(GIt);-    R := Fp.BoundingRectangle;-    Result := '{' + JRaw('ok','true') + ',' + JStr('name', Fp.Name) + ',' + JStr('description', Fp.Description) + ',' + JRaw('height', IntToStr(Fp.Height)) + ',' +-        JRaw('k', IntToStr(MilsToCoord(1000))) + ',' + JRaw('origin', '[' + IntToStr(OX) + ',' + IntToStr(OY) + ']') + ',' + JStr('libraryFile', PLib.Board.FileName) + ',' + JRaw('box', '[' + IntToStr(R.Left - OX) + ',' + IntToStr(R.Bottom - OY) + ',' + IntToStr(R.Right - OX) + ',' + IntToStr(R.Top - OY) + ']') + ',' +-        JRaw('pads', '[' + S + ']') + ',' + JRaw('bodies', '[' + B + ']') + ',' + JRaw('other', '[' + L + ']') + '}';-End;---{ new_pcb: an empty board saved at path= (Altium's default new board). Refuses an existing file. }-Function CmdNewPcb(Lines : TStringList) : String;-Var-    Path : String;-    Doc  : IServerDocument;-Begin-    Path := Param(Lines, 'path');-    If FileExists(Path) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','exists') + ',' + JStr('error', Path + ' exists') + '}'; Exit; End;-    Doc := CreateNewDocumentFromDocumentKind('PCB');-    If Doc = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','Altium made no new PCB document') + '}'; Exit; End;-        Client.ShowDocument(Doc);-    { as build_board saves its new board: name it, then save (DoSafeChangeFileNameAndSave left no file for a PCB) }-    FreePath(Path);-    Doc.SetFileName(Path);-    Doc.DoFileSave('');-    If Not FileExists(Path) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','not_saved') + ',' + JStr('error','the new board did not save to ' + Path) + '}'; Exit; End;-    Result := '{' + JRaw('ok','true') + ',' + JStr('path', Path) + ',' + JRaw('saved', 'true') + '}';-End;---{ save_doc: save the document at path= itself. The save process saves the FOCUSED document, and open shows a document-  without focusing it, so a save by path through it saved whichever document had focus (a source library, measured-  2026-10-27). }-{ The open SimData (results) documents, every server's. }-Function SimDataDocs : TInterfaceList;-Var-    SM : IServerModule;-    D  : IServerDocument;-    I, J : Integer;-Begin-    Result := TInterfaceList.Create;-    For I := 0 To Client.Count - 1 Do-    Begin-        SM := Client.ServerModule[I];-        If SM = Nil Then Continue;-        For J := 0 To SM.DocumentCount - 1 Do-        Begin-            D := SM.Documents[J];-            If (D <> Nil) And (UpperCase(D.Kind) = 'SIMDATA') Then Result.Add(D);-        End;-    End;-    { the one in front, when the server walk misses it }-    D := Nil;-    Try D := Client.CurrentView.OwnerDocument; Except D := Nil; End;-    If (D <> Nil) And (UpperCase(D.Kind) = 'SIMDATA') And (Result.IndexOf(D) < 0) Then Result.Add(D);-End;--{ sim_run: start Altium's mixed-signal simulator on one schematic sheet (path=<SchDoc>). Earlier results documents are-  closed first (each run makes its own) and the sheet is made the CURRENT document: the run netlists whatever is in-  front, and with the last run's results in front it netlisted nothing (2026-10-29). Then WorkspaceManager:-  GenerateReport ObjectKind=Simulator|Index=1|Action=Run (the "Mixed Sim" output: <project>.nsx, then the run). The-  results document comes up after this returns: sim_collect saves and closes it. }-Function CmdSimRun(Lines : TStringList) : String;-Var-    Doc : IServerDocument;-    L   : TInterfaceList;-    Path : String;-    I : Integer;-Begin-    Path := Param(Lines, 'path');-    L := SimDataDocs;-    For I := 0 To L.Count - 1 Do Try Client.CloseDocument(L[I]); Except End;-    Doc := Nil;-    Try Doc := Client.OpenDocument('SCH', Path); Except Doc := Nil; End;-    If Doc = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','not_open') + ',' + JStr('error', Path + ' could not be opened') + '}'; Exit; End;-    Client.ShowDocument(Doc);-    ResetParameters;-    AddStringParameter('ObjectKind', 'Simulator');-    AddStringParameter('Index', '1');-    AddStringParameter('Action', 'Run');-    RunProcess('WorkspaceManager:GenerateReport');-    Result := '{' + JRaw('ok','true') + ',' + JStr('sheet', Path) + ',' + JStr('current', Client.CurrentView.OwnerDocument.FileName) + '}';-End;--{ sim_collect: the run's results document, saved (so its .sdf is on disk) and closed; sdf is empty while none is up. }-Function CmdSimCollect(Lines : TStringList) : String;-Var-    L : TInterfaceList;-    D : IServerDocument;-    Sdf : String;-Begin-    L := SimDataDocs;-    Sdf := '';-    If L.Count > 0 Then-    Begin-        D := L[L.Count - 1];-        D.DoFileSave('');-        Sdf := D.FileName;-        Try Client.CloseDocument(D); Except End;-    End;-    Result := '{' + JRaw('ok','true') + ',' + JStr('sdf', Sdf) + ',' + JRaw('open', IntToStr(L.Count)) + '}';-End;--Function CmdSaveDoc(Lines : TStringList) : String;-Var-    Doc : IServerDocument;-Begin-    Doc := Nil;-    Try Doc := Client.GetDocumentByPath(Param(Lines, 'path')); Except Doc := Nil; End;-    If Doc = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','not_open') + ',' + JStr('error', Param(Lines, 'path') + ' is not open in Altium') + '}'; Exit; End;-    Doc.DoFileSave('');-    Result := '{' + JRaw('ok','true') + ',' + JStr('path', Doc.FileName) + '}';-End;---{ ensure_lib: open, or create and save, a library: kind=PCBLIB|SCHLIB path=<file>. It becomes the current document. }-Function CmdEnsureLib(Lines : TStringList) : String;-Var-    St : String;-Begin-    St := EnsureLibrary(Param(Lines, 'kind'), Param(Lines, 'path'));-    If Copy(St, 1, 5) = 'error' Then Result := '{' + JRaw('ok','false') + ',' + JStr('error', St) + '}'-    Else Result := '{' + JRaw('ok','true') + ',' + JStr('state', St) + ',' + JStr('path', Param(Lines, 'path')) + '}';-End;--{ how many pins a symbol holds (the empty default symbol holds none) }-Function SchChildCount(C : ISch_Component) : Integer;-Var-    It : ISch_Iterator;-    P  : ISch_GraphicalObject;-Begin-    Result := 0;-    It := C.SchIterator_Create;-    P := It.FirstSchObject;-    While P <> Nil Do-    Begin-        If P.ObjectID = ePin Then Result := Result + 1;-        P := It.NextSchObject;-    End;-    C.SchIterator_Destroy(It);-End;--{ xcopy_symbol: copy one symbol from fromLib (a .SchLib, opened if need be) into the CURRENT SchLib, under its own-  name: its graphics, pins, parameters (but the Comment, which the new symbol carries already) and model links. }-Function CmdXCopySymbol(Lines : TStringList) : String;-Var-    DstLib, SrcLib : ISch_Lib;-    Src, Dst : ISch_Component;-    It  : ISch_Iterator;-    Prim, Cpy : ISch_GraphicalObject;-    SD  : IServerDocument;-    Dflt : ISch_Component;-    NCopied : Integer;-    Dropped : String;-Begin-    DstLib := SchServer.GetCurrentSchDocument;-    If (DstLib = Nil) Or (DstLib.ObjectID <> eSchLib) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the destination SchLib first') + '}'; Exit; End;-    SrcLib := SchServer.GetSchDocumentByPath(Param(Lines, 'fromLib'));-    If SrcLib = Nil Then-    Begin-        Try SD := Client.OpenDocument('SCHLIB', Param(Lines, 'fromLib')); Except SD := Nil; End;-        SrcLib := SchServer.GetSchDocumentByPath(Param(Lines, 'fromLib'));-    End;-    If SrcLib = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','source library not loadable: ' + Param(Lines, 'fromLib')) + '}'; Exit; End;-    Src := FindSchComp(SrcLib, Param(Lines, 'from'));-    If Src = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','symbol_not_found') + ',' + JStr('error','symbol not found in source: ' + Param(Lines, 'from')) + '}'; Exit; End;-    If FindSchComp(DstLib, Src.LibReference) <> Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','target already has ' + Src.LibReference) + '}'; Exit; End;-    SchServer.ProcessControl.PreProcess(DstLib, '');-    Dst := SchServer.SchObjectFactory(eSchComponent, eCreate_Default);-    Dst.LibReference := Src.LibReference;-    Dst.ComponentDescription := Src.ComponentDescription;-    Dst.CurrentPartID := 1;-    Dst.DisplayMode := 0;-    Dst.Designator.Text := Src.Designator.Text;-    Dst.Comment.Text := Src.Comment.Text;-    NCopied := 0;-    It := Src.SchIterator_Create;-    Prim := It.FirstSchObject;-    While Prim <> Nil Do-    Begin-        If (Prim.ObjectID <> eParameter) Or (UpperCase(Prim.Name) <> 'COMMENT') Then-        Begin-            Cpy := Prim.Replicate;-            Dst.AddSchObject(Cpy);-            NCopied := NCopied + 1;-        End;-        Prim := It.NextSchObject;-    End;-    Src.SchIterator_Destroy(It);-    Dst.DisplayModeCount := Src.DisplayModeCount;-    DstLib.AddSchComponent(Dst);-    SchServer.RobotManager.SendMessage(DstLib.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, Dst.I_ObjectAddress);-    { a SchLib Altium has just made holds an empty Component_1: it goes once a real symbol is in }-    Dropped := '';-    Dflt := FindSchComp(DstLib, 'Component_1');-    If (Dflt <> Nil) And (UpperCase(Src.LibReference) <> 'COMPONENT_1') And (SchChildCount(Dflt) = 0) Then-    Begin-        DstLib.RemoveSchComponent(Dflt);-        Dropped := 'Component_1';-    End;-    SchServer.ProcessControl.PostProcess(DstLib, '');-    DstLib.GraphicallyInvalidate;-    Result := '{' + JRaw('ok','true') + ',' + JStr('symbol', Dst.LibReference) + ',' + JRaw('primitives', IntToStr(NCopied)) + ',' + JStr('removedDefault', Dropped) + '}';-End;---Function Dispatch(Cmd : String; Lines : TStringList; Payload : String) : String;-Begin-    If Cmd = 'ping' Then Result := CmdPing(Lines)-    Else If Cmd = 'state' Then Result := CmdState(Lines)-    Else If Cmd = 'run_process' Then Result := CmdRunProcess(Lines)-    Else If Cmd = 'open' Then Result := CmdOpen(Lines)-    Else If Cmd = 'save_doc' Then Result := CmdSaveDoc(Lines)-    Else If Cmd = 'sim_run' Then Result := CmdSimRun(Lines)-    Else If Cmd = 'sim_collect' Then Result := CmdSimCollect(Lines)-    Else If Cmd = 'edit_fp' Then Result := CmdEditFp(Lines, Payload)-    Else If Cmd = 'close_document' Then Result := CmdCloseDocument(Lines)-    Else If Cmd = 'messages' Then Result := CmdMessages(Lines)-    Else If Cmd = 'import_step' Then Result := CmdImportStep(Lines)-    Else If Cmd = 'eval' Then Result := CmdEval(Lines, Payload)-    Else If Cmd = 'io_bench' Then Result := CmdIoBench(Lines)-    Else If Cmd = 'read_board' Then Result := CmdReadBoard(Lines)-    Else If Cmd = 'layer_occupancy' Then Result := CmdLayerOccupancy(Lines)-    Else If Cmd = 'read_schematic' Then Result := CmdReadSchematic(Lines)-    Else If Cmd = 'read_outline' Then Result := CmdReadOutline(Lines)-    Else If Cmd = 'lib_list' Then Result := CmdLibList(Lines)-    Else If Cmd = 'lib_select' Then Result := CmdLibSelect(Lines)-    Else If Cmd = 'compile' Then Result := CmdCompile(Lines)-    Else If Cmd = 'violations' Then Result := CmdViolations(Lines)-    Else If Cmd = 'outjob' Then Result := CmdOutJob(Lines)-    Else If Cmd = 'create_footprint' Then Result := CmdCreateFootprint(Lines)-    Else If Cmd = 'create_symbol' Then Result := CmdCreateSymbol(Lines)-    Else If Cmd = 'install_library' Then Result := CmdInstallLibrary(Lines)-    Else If Cmd = 'list_installed_libraries' Then Result := CmdListInstalledLibs(Lines)-    Else If Cmd = 'uninstall_library' Then Result := CmdUninstallLibrary(Lines)-    Else If Cmd = 'compile_intlib' Then Result := CmdCompileIntLib(Lines)-    Else If Cmd = 'extract_intlib' Then Result := CmdExtractIntLib(Lines)-    Else If Cmd = 'set_params' Then Result := CmdSetParams(Lines)-    Else If Cmd = 'add_body' Then Result := CmdAddBody(Lines)-    Else If Cmd = 'place_on_sch' Then Result := CmdPlaceOnSch(Lines)-    Else If Cmd = 'add_symbol_art' Then Result := CmdAddSymbolArt(Lines, Payload)-    Else If Cmd = 'selftest_halt' Then Result := CmdSelfTestHalt(Lines)-    Else If Cmd = 'symbol_prims' Then Result := CmdSymbolPrims(Lines)-    Else If Cmd = 'set_models' Then Result := CmdSetModels(Lines)-    Else If Cmd = 'place_on_pcb' Then Result := CmdPlaceOnPcb(Lines)-    Else If Cmd = 'pcb_report' Then Result := CmdPcbReport(Lines)-    Else If Cmd = 'list_designators' Then Result := CmdListDesignators(Lines)-    Else If Cmd = 'component_data' Then Result := CmdComponentData(Lines)-    Else If Cmd = 'layer_stack' Then Result := CmdLayerStack(Lines)-    Else If Cmd = 'bom_placement' Then Result := CmdBomPlacement(Lines)-    Else If Cmd = 'component_pins' Then Result := CmdComponentPins(Lines)-    Else If Cmd = 'list_nets' Then Result := CmdListNets(Lines)-    Else If Cmd = 'layers' Then Result := CmdLayers(Lines)-    Else If Cmd = 'net_connections' Then Result := CmdNetConnections(Lines)-    Else If Cmd = 'schematic_data' Then Result := CmdSchematicData(Lines)-    Else If Cmd = 'sheet_params' Then Result := CmdSheetParams(Lines)-    Else If Cmd = 'sch_connect' Then Result := CmdSchConnect(Lines, Payload)-    Else If Cmd = 'sch_dump' Then Result := CmdSchDump(Lines)-    Else If Cmd = 'docs' Then Result := CmdDocs(Lines)-    Else If Cmd = 'ensure_lib' Then Result := CmdEnsureLib(Lines)-    Else If Cmd = 'xcopy_symbol' Then Result := CmdXCopySymbol(Lines)-    Else If Cmd = 'new_pcb' Then Result := CmdNewPcb(Lines)-    Else If Cmd = 'lib_fp' Then Result := CmdLibFootprint(Lines)-    Else If Cmd = 'board_graphics' Then Result := CmdBoardGraphics(Lines)-    Else If Cmd = 'sch_ops' Then Result := CmdSchOps(Lines, Payload)-    Else If Cmd = 'sheet_symbol' Then Result := CmdSheetSymbol(Lines, Payload)-    Else If Cmd = 'sheet_ports' Then Result := CmdSheetPorts(Lines, Payload)-    Else If Cmd = 'teardrop_count' Then Result := CmdTeardropCount(Lines)-    Else If Cmd = 'comp_classes' Then Result := CmdCompClasses(Lines)-    Else If Cmd = 'panelize' Then Result := CmdPanelize(Lines)-    Else If Cmd = 'bodies' Then Result := CmdBodies(Lines)-    Else If Cmd = 'sch_edit' Then Result := CmdSchEdit(Lines, Payload)-    Else If Cmd = 'sch_place_part' Then Result := CmdSchPlacePart(Lines)-    Else If Cmd = 'doc_params' Then Result := CmdDocParams(Lines)-    Else If Cmd = 'sch_rename_at' Then Result := CmdSchRenameAt(Lines, Payload)-    Else If Cmd = 'sch_wires' Then Result := CmdSchWires(Lines, Payload)-    Else If Cmd = 'net_classes' Then Result := CmdNetClasses(Lines)-    Else If Cmd = 'selection' Then Result := CmdSelection(Lines)-    Else If Cmd = 'comp_rects' Then Result := CmdCompRects(Lines)-    Else If Cmd = 'track_width' Then Result := CmdTrackWidth(Lines)-    Else If Cmd = 'outline_tracks' Then Result := CmdOutlineTracks(Lines)-    Else If Cmd = 'layer_stack_edit' Then Result := CmdLayerStackEdit(Lines)-    Else If Cmd = 'pcb_postprocess' Then Result := CmdPcbPostProcess(Lines)-    Else If Cmd = 'project_variants' Then Result := CmdProjectVariants(Lines)-    Else If Cmd = 'diff_pairs' Then Result := CmdDiffPairs(Lines)-    Else If Cmd = 'sch_zoom' Then Result := CmdSchZoom(Lines)-    Else If Cmd = 'copper_layers' Then Result := CmdCopperLayers(Lines)-    Else If Cmd = 'read_rules' Then Result := CmdReadRules(Lines)-    Else If Cmd = 'set_layer_visibility' Then Result := CmdSetLayerVisibility(Lines)-    Else If Cmd = 'footprint_primitives' Then Result := CmdFootprintPrimitives(Lines)-    Else If Cmd = 'routing_state' Then Result := CmdRoutingState(Lines)-    Else If Cmd = 'edit_board' Then Result := CmdEditBoard(Lines, Payload)-    Else If Cmd = 'apply_pours' Then Result := CmdApplyPours(Lines, Payload)-    Else If Cmd = 'edit_pour' Then Result := CmdEditPour(Lines, Payload)-    Else If Cmd = 'pour_state' Then Result := CmdPourState(Lines)-    Else If Cmd = 'rebuild_pours' Then Result := CmdRebuildPours(Lines)-    Else If Cmd = 'remove_pours' Then Result := CmdRemovePours(Lines, Payload)-    Else If Cmd = 'routing_drc' Then Result := CmdRoutingDrc(Lines)-    Else If Cmd = 'build_board' Then Result := CmdBuildBoard(Lines, Payload)-    Else If Cmd = 'set_outline' Then Result := CmdSetOutline(Lines, Payload)-    Else If Cmd = 'set_rule' Then Result := CmdSetRule(Lines)-    Else If Cmd = 'zoom_rect' Then Result := CmdZoomRect(Lines)-    Else If Cmd = 'add_rule' Then Result := CmdAddRule(Lines)-    Else If Cmd = 'build_schematic' Then Result := CmdBuildSchematic(Lines, Payload)-    Else If Cmd = 'project_nets' Then Result := CmdProjectNets(Lines)-    Else If Cmd = 'select_net' Then Result := CmdSelectNet(Lines)-    Else If Cmd = 'place_parts' Then Result := CmdPlaceParts(Lines, Payload)-    Else If Cmd = 'model_check' Then Result := CmdModelCheck(Lines)-    Else If Cmd = 'close_old_agents' Then Result := CmdCloseOldAgents(Lines)-    Else If Cmd = 'silk_state' Then Result := CmdSilkState(Lines)-    Else If Cmd = 'apply_silk' Then Result := CmdApplySilk(Lines, Payload)-    Else If Cmd = 'build_footprint' Then Result := CmdBuildFootprint(Lines)-    Else If Cmd = 'build_symbol_modes' Then Result := CmdBuildSymbolModes(Lines)-    Else If Cmd = 'symbol_mode' Then Result := CmdSymbolMode(Lines)-    Else If Cmd = 'add_models' Then Result := CmdAddModels(Lines)-    Else If Cmd = 'remove_symbol' Then Result := CmdRemoveSymbol(Lines)-    Else If Cmd = 'clone_symbol' Then Result := CmdCloneSymbol(Lines)-    Else If Cmd = 'rename_symbol' Then Result := CmdRenameSymbol(Lines)-    Else If Cmd = 'read_symbol' Then Result := CmdReadSymbol(Lines)-    Else If Cmd = 'copy_footprint' Then Result := CmdCopyFootprint(Lines)-    Else If Cmd = 'rename_footprint' Then Result := CmdRenameFootprint(Lines)-    Else If Cmd = 'remove_footprint' Then Result := CmdRemoveFootprint(Lines)-    Else If Cmd = 'cleanup_libs' Then Result := CmdCleanupLibs(Lines)-    Else If Cmd = 'set_visibility' Then Result := CmdSetVisibility(Lines)-    Else If Cmd = 'xcopy_footprint' Then Result := CmdXCopyFootprint(Lines)-    Else If Cmd = 'normalize_bodies' Then Result := CmdNormalizeBodies(Lines)-    Else If Cmd = 'plate_vias' Then Result := CmdPlateVias(Lines)-    Else If Cmd = 'test_board' Then Result := CmdTestBoard(Lines)-    Else If Cmd = 'board_shape' Then Result := CmdBoardShape(Lines)-    Else If Cmd = 'swap_footprint' Then Result := CmdSwapFootprint(Lines)-    Else If Cmd = 'replace_fp' Then Result := CmdReplaceFp(Lines)-    Else If Cmd = 'tx_clear' Then Begin TxClear; Result := '{' + JRaw('ok','true') + ',' + JStr('note','PCBServer transaction cleared') + '}'; End-    Else If Cmd = 'stop' Then-    Begin-        gRunning := False;-        Result := '{' + JRaw('ok', 'true') + ',' + JStr('note', 'agent loop stopping') + '}';-    End-    Else Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'unknown command: ' + Cmd) + ',' + JStr('known', 'ping,state,run_process,open,eval,lib_list,lib_select,compile,violations,outjob,create_footprint,create_symbol,install_library,compile_intlib,extract_intlib,set_params,add_body,stop,list_designators,component_data,component_pins,list_nets,layers,net_connections,schematic_data,copper_layers,read_rules,set_layer_visibility,footprint_primitives,io_bench,read_board,layer_occupancy,read_schematic,read_outline') + '}';-End;--{ ------------------------------------------------------------------ loop --- }--{ ONE inbox file, request.txt, written atomically by the bridge (tmp + rename) and-  serialized on the bridge side. A wildcard FindFirst('request_*.txt') never matched-  in this engine (measured 2026-08-29: heartbeat fine, requests stuck on disk), so-  the inbox is a fixed name and the id rides inside. }-Function ServeOne : Boolean;-Var-    Id, ReqPath, PayPath, Cmd, Body, Payload : String;-    Lines   : TStringList;-    T0, TDetect, TRead, TDone : Integer;-Begin-    Result := False;-    ReqPath := WORKSPACE + 'request.txt';-    If Not FileExists(ReqPath) Then Exit;-    PayPath := WORKSPACE + 'request.payload';-    Lines := TStringList.Create;-    Try-        TDetect := GetTickCount;-        Lines.Text := ReadAll(ReqPath);-        DeleteFile(ReqPath);-        TRead := GetTickCount;-        Payload := '';-        If FileExists(PayPath) Then-        Begin-            Payload := ReadAll(PayPath);-            DeleteFile(PayPath);-        End;-        Id := Param(Lines, 'id');-        Cmd := Param(Lines, 'command');-        gRequests := gRequests + 1;-        { a status read does not keep the loop resident: an AI polling altium_state held Altium (and the user's-          clicks) for as long as it polled (adom/altium-bridge#27) }-        If (Cmd <> 'state') And (Cmd <> 'ping') Then gLastReqTick := GetTickCount;-        T0 := GetTickCount;-        gInputError := '';-        Try-            Body := Dispatch(Cmd, Lines, Payload);-            If gInputError <> '' Then-                Body := '{' + JRaw('ok', 'false') + ',' + JStr('errorCode', 'bad_number') + ',' +-                    JStr('error', 'a value that must be a number was not: ' + gInputError) + ',' + JStr('command', Cmd) + '}';-        Except-            Body := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'exception in agent while handling ' + Cmd) + '}';-        End;-        TDone := GetTickCount;-        { wrap: id + timing + the handler body as "data"; agentPhases = read/dispatch ms + the loop tick that saw the request }-        WriteDirect(WORKSPACE + 'response_' + Id + '.json',-            '{' + JStr('id', Id) + ',' + JStr('command', Cmd) + ',' + JRaw('durationMs', IntToStr(TDone - T0)) + ',' +-            JRaw('agentPhases', '{' + JRaw('readMs', IntToStr(TRead - TDetect)) + ',' + JRaw('dispatchMs', IntToStr(TDone - T0)) + ',' + JRaw('detectTick', IntToStr(TDetect)) + ',' + JRaw('sleepMs', IntToStr(gLastSleepMs)) + ',' + JRaw('hbSaveMs', IntToStr(gLastSaveMs)) + ',' + JRaw('hbSaveTries', IntToStr(gLastSaveTries)) + ',' + JRaw('hbSaveExcept', IntToStr(gLastSaveExcept)) + ',' + JRaw('hbDeleteMs', IntToStr(gLastDeleteMs)) + ',' + JRaw('hbRenameMs', IntToStr(gLastRenameMs)) + ',' + JRaw('directMs', IntToStr(gDirectMs)) + ',' + JRaw('directTries', IntToStr(gDirectTries)) + ',' + JRaw('directExcept', IntToStr(gDirectExcept)) + '}') + ',' +-            JRaw('data', Body) + '}');-    Finally-        Lines.Free;-    End;-    Result := True;-End;--{ Register the PCB and SCH scripting APIs before the first PCBServer/SchServer-  reference. On a fresh session where no PCB document was ever opened, the PCB-  API unit is not registered and the first PCBServer call (TxClear, line ~140)-  halts the engine with an "Undeclared identifier: PostProcess" modal on the-  user's screen (altium-bridge#3, ADDevelop fresh session). Client.StartServer-  loads the editor server, which registers the API — background, no UI. }-Procedure ServersUp;-Begin-    Try-        Client.StartServer('PCB');-    Except-    End;-    Try-        Client.StartServer('SCH');-    Except-    End;-End;--Procedure StartAgent;-Var-    IdleTicks : Integer;-    Had       : Boolean;-    SleepMs   : Integer;-Begin-    ServersUp;-    TxClear;-    gRunning := True;-    gRequests := 0;-    gLastReqTick := GetTickCount;-    gLastHbTick := 0;-    IdleTicks := 0;-    gTicks := 0;-    { a stale stop file must not kill a fresh loop }-    If FileExists(WORKSPACE + 'stop') Then DeleteFile(WORKSPACE + 'stop');-    WriteAtomic(WORKSPACE + 'agent.json', '{' + JStr('version', AGENT_VERSION) + ',' + JRaw('startedTick', IntToStr(GetTickCount)) + ',' + JStr('workspace', WORKSPACE) + '}');-    Heartbeat(True);-    While gRunning Do-    Begin-        Try-            { two shutdown signals, both cheap reads: IsQuitting demonstrably never fired-              in time during the 2026-09-02 freezes, so Terminated rides beside it }-            If Client.IsQuitting Then gRunning := False;-            If Application.Terminated Then gRunning := False;-        Except-        End;-        If FileExists(WORKSPACE + 'stop') Then-        Begin-            DeleteFile(WORKSPACE + 'stop');-            gRunning := False;-        End;-        If Not gRunning Then Break;-        Had := False;-        Try-            Had := ServeOne;-        Except-        End;-        If Had Then-        Begin-            IdleTicks := 0;-            SleepMs := POLL_ACTIVE_MS;-        End-        Else-        Begin-            IdleTicks := IdleTicks + 1;-            If IdleTicks > IDLE_AFTER_TICKS Then SleepMs := POLL_IDLE_MS Else SleepMs := POLL_ACTIVE_MS;-        End;-        If (GetTickCount - gLastReqTick) > AUTO_STOP_MS Then gRunning := False;-        Heartbeat(False);-        Application.ProcessMessages;-        gLastSleepMs := SleepMs;-        gTicks := gTicks + 1;-        Sleep(SleepMs);-    End;-    Try-        WriteAtomic(WORKSPACE + 'agent.json', '{' + JStr('version', AGENT_VERSION) + ',' + JRaw('stoppedTick', IntToStr(GetTickCount)) + ',' + JRaw('requests', IntToStr(gRequests)) + '}');-        If FileExists(WORKSPACE + 'hb.txt') Then DeleteFile(WORKSPACE + 'hb.txt');-    Except-    End;-End;--{ Convenience entry points for File > Run Script users. }-Procedure StopAgent;-Begin-    WriteAtomic(WORKSPACE + 'stop', '1');-End;+    T, C : String;+    I, N, D : Integer;+    Sign, Frac : Double;+    InFrac : Boolean;+Begin+    Result := 0;+    T := Trim(S);+    If AdomIsNumber(T, True) Then+    Begin+        Sign := 1.0; Frac := 0.1; InFrac := False;+        N := Length(T);+        For I := 1 To N Do+        Begin+            C := Copy(T, I, 1);+            If (C = '-') And (I = 1) Then Sign := -1.0+            Else If (C = '+') And (I = 1) Then Sign := 1.0+            Else If C = '.' Then InFrac := True+            Else If (C >= '0') And (C <= '9') Then+            Begin+                D := Ord(C) - 48;+                If InFrac Then Begin Result := Result + D * Frac; Frac := Frac * 0.1; End+                Else Result := Result * 10.0 + D;+            End;+        End;+        Result := Result * Sign;+    End+    Else If gInputError = '' Then gInputError := T;+End;++Function AdomInt(S : String) : Integer;+Var+    T : String;+Begin+    Result := 0;+    T := Trim(S);+    If AdomIsNumber(T, False) Then Result := StrToInt(T)+    Else If gInputError = '' Then gInputError := T;+End;++{ ---------------------------------------------------------------- files ---- }++Function ReadAll(Path : String) : String;+Var+    L : TStringList;+    K, H : Integer;+Begin+    Result := '';+    { NEVER let LoadFromFile raise: the script debugger breaks on ANY exception+      even inside Try/Except and draws an Error modal (EFOpenError "being used+      by another process" when the bridge is mid-write, measured 2026-08-30).+      Probe with FileOpen, which returns -1 instead of raising, until the file+      is readable, then load. }+    For K := 1 To 100 Do+    Begin+        H := FileOpen(Path, fmOpenRead Or fmShareDenyNone);+        If H >= 0 Then+        Begin+            FileClose(H);+            Break;+        End;+        Sleep(20);+    End;+    If H < 0 Then Exit;+    L := TStringList.Create;+    Try+        L.LoadFromFile(Path);+        Result := L.Text;+    Finally+        L.Free;+    End;+End;++Procedure WriteAtomic(Path : String; Content : String);+Var+    L    : TStringList;+    Tmp  : String;+    Done : Boolean;+    K, TA, TB, TC : Integer;+Begin+    Tmp := Path + '.tmp';+    L := TStringList.Create;+    Try+        L.Text := Content;+        TA := GetTickCount;+        gLastSaveTries := 0;+        gLastSaveExcept := 0;+        Done := False;+        K := 0;+        { NOTE: never `Break` out of a Try..Except here - this engine implements Break as an+          exception, the Except swallows it, and the loop runs all 20 iterations (~220 ms). }+        While (Not Done) And (K < 20) Do+        Begin+            K := K + 1;+            gLastSaveTries := K;+            Try+                L.SaveToFile(Tmp);+                Done := True;+            Except+                gLastSaveExcept := gLastSaveExcept + 1;+                Sleep(10);+            End;+        End;+        TB := GetTickCount;+    Finally+        L.Free;+    End;+    If FileExists(Path) Then DeleteFile(Path);+    TC := GetTickCount;+    RenameFile(Tmp, Path);+    gLastSaveMs := TB - TA;+    gLastDeleteMs := TC - TB;+    gLastRenameMs := GetTickCount - TC;+End;++{ Direct write, no tmp+rename (A/B against WriteAtomic for the response path). Readers tolerate a+  partially written file by retrying the JSON parse. }+Procedure WriteDirect(Path : String; Content : String);+Var+    L    : TStringList;+    Done : Boolean;+    K    : Integer;+    T0   : Integer;+Begin+    L := TStringList.Create;+    Try+        L.Text := Content;+        T0 := GetTickCount;+        gDirectTries := 0;+        gDirectExcept := 0;+        Done := False;+        K := 0;+        While (Not Done) And (K < 20) Do+        Begin+            K := K + 1;+            gDirectTries := K;+            Try+                L.SaveToFile(Path);+                Done := True;+            Except+                gDirectExcept := gDirectExcept + 1;+                Sleep(10);+            End;+        End;+        gDirectMs := GetTickCount - T0;+    Finally+        L.Free;+    End;+End;++{ Runs of plain characters are copied whole: appending one character at a time is quadratic in+  DelphiScript, and one part's long parameter (a Mini PC example IC) took minutes to escape (2026-10-21). }+Function JsonEsc(S : String) : String;+Var+    I, Start, N : Integer;+    C : Char;+    E : String;+Begin+    Result := '';+    N := Length(S);+    Start := 1;+    For I := 1 To N Do+    Begin+        C := S[I];+        E := '';+        If C = '"' Then E := '\"'+        Else If C = '\' Then E := '\\'+        Else If C = #13 Then E := '\r'+        Else If C = #10 Then E := '\n'+        Else If C = #9 Then E := '\t'+        Else If Ord(C) < 32 Then E := ' ';+        If E <> '' Then+        Begin+            If I > Start Then Result := Result + Copy(S, Start, I - Start);+            Result := Result + E;+            Start := I + 1;+        End;+    End;+    If Start <= N Then Result := Result + Copy(S, Start, N - Start + 1);+End;++Function JStr(Key : String; Val : String) : String;+Begin+    Result := '"' + Key + '":"' + JsonEsc(Val) + '"';+End;++Function JRaw(Key : String; Val : String) : String;+Begin+    Result := '"' + Key + '":' + Val;+End;++Function JBool(B : Boolean) : String;+Begin+    If B Then Result := 'true' Else Result := 'false';+End;++{ An edit made through the scripting API does not mark a schematic document as changed (no * in the title, 2026-10-27):+  a save:false edit then looked unchanged and was lost when the document closed. Mark it after every edit. }+Function BaseNameOf(S : String) : String;+Var+    I : Integer;+Begin+    Result := S;+    For I := Length(S) DownTo 1 Do+        If (S[I] = '\') Or (S[I] = '/') Then Begin Result := Copy(S, I + 1, Length(S) - I); Exit; End;+End;++Function MarkSchModified(Doc : ISch_Document) : String;+Var+    SM : IServerModule;+    D  : IServerDocument;+    I, J : Integer;+    Nm : String;+Begin+    { by file name: a sheet's DocumentName and its server document's FileName are not always the same form, and the+      agent's own script view is the "current view" while it runs }+    Result := '';+    Try+        Nm := UpperCase(BaseNameOf(Doc.DocumentName));+        Result := Nm + ':';+        For I := 0 To Client.Count - 1 Do+        Begin+            SM := Client.ServerModule[I];+            If SM = Nil Then Continue;+            For J := 0 To SM.DocumentCount - 1 Do+            Begin+                D := SM.Documents[J];+                If (D <> Nil) And (UpperCase(BaseNameOf(D.FileName)) = Nm) Then Begin D.Modified := True; Result := Result + 'set;'; End;+            End;+        End;+    Except+        Result := Result + 'raised';+    End;+End;++++{ Close a dangling PCBServer transaction. A command that halted between+  PreProcess and PostProcess leaves Altium in "a command is currently active",+  and the next save draws the save-a-copy Warning. Measured 2026-08-30: an+  unbalanced PostProcess is harmless and clears it. }+Procedure TxClear;+Begin+    Try+        PCBServer.PostProcess;+    Except+    End;+End;++{ hb.txt MUST be written tmp+rename: readers (the bridge's is_alive poll) hold the final path+  open, so a direct SaveToFile onto it raises EFCreateError and halts the engine with a modal.+  Response files use WriteDirect - a fresh unique name nobody has open yet. }+Procedure Heartbeat(Force : Boolean);+Var+    T : Integer;+Begin+    T := GetTickCount;+    If Force Or ((T - gLastHbTick) >= HEARTBEAT_MS) Then+    Begin+        Try+            WriteAtomic(WORKSPACE + 'hb.txt', IntToStr(T) + #13#10 + 'requests=' + IntToStr(gRequests) + #13#10 + 'ticks=' + IntToStr(gTicks) + #13#10 + 'version=' + AGENT_VERSION + #13#10 + 'gen=' + AGENT_GEN);+        Except+        End;+        gLastHbTick := T;+    End;+End;++{ --------------------------------------------------------- request parse --- }++{ Requests are "key=value" lines.  Param(Lines, 'command') etc. }+Function Param(Lines : TStringList; Key : String) : String;+Var+    I : Integer;+    L : String;+Begin+    Result := '';+    For I := 0 To Lines.Count - 1 Do+    Begin+        L := Lines[I];+        If Pos(Key + '=', L) = 1 Then+        Begin+            Result := Copy(L, Length(Key) + 2, Length(L));+            Break;+        End;+    End;+End;++{ ------------------------------------------------------------- handlers --- }++Function CmdPing(Lines : TStringList) : String;+Begin+    Result := '{' + JRaw('ok', 'true') + ',' + JStr('agent', AGENT_VERSION) + ',' + JRaw('requests', IntToStr(gRequests)) + ',' + JRaw('tick', IntToStr(GetTickCount)) + '}';+End;++{ Every open document in the workspace + which one has focus. }+Function CmdState(Lines : TStringList) : String;+Var+    WS   : IWorkspace;+    Prj  : IProject;+    Doc  : IDocument;+    I, J : Integer;+    Docs : String;+    Focused : String;+    PrjName : String;+Begin+    Docs := '';+    Focused := '';+    PrjName := '';+    WS := GetWorkspace;+    If WS <> Nil Then+    Begin+        Try+            Doc := WS.DM_FocusedDocument;+            If Doc <> Nil Then Focused := Doc.DM_FullPath;+        Except+        End;+        Try+            Prj := WS.DM_FocusedProject;+            If Prj <> Nil Then PrjName := Prj.DM_ProjectFullPath;+        Except+        End;+        For I := 0 To WS.DM_ProjectCount - 1 Do+        Begin+            Prj := WS.DM_Projects(I);+            For J := 0 To Prj.DM_LogicalDocumentCount - 1 Do+            Begin+                Doc := Prj.DM_LogicalDocuments(J);+                If Docs <> '' Then Docs := Docs + ',';+                Docs := Docs + '{' + JStr('path', Doc.DM_FullPath) + ',' + JStr('kind', Doc.DM_DocumentKind) + ',' + JStr('project', Prj.DM_ProjectFileName) + ',' + JRaw('open', JBool(Doc.DM_DocumentIsLoaded)) + '}';+            End;+        End;+    End;+    Result := '{' + JRaw('ok', 'true') + ',' + JStr('focusedDocument', Focused) + ',' + JStr('focusedProject', PrjName) + ',' + JRaw('documents', '[' + Docs + ']') + '}';+End;++{ Run any server process from the 1,109-entry catalog: command=PCB:Zoom  params=Action=All|... }+Function CmdRunProcess(Lines : TStringList) : String;+Var+    Cmd, P, Rest, Pair : String;+    K : Integer;+Begin+    Cmd := Param(Lines, 'process');+    P := Param(Lines, 'params');+    If Cmd = '' Then+    Begin+        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'process is required, e.g. PCB:Zoom') + '}';+        Exit;+    End;+    { RunProcessWithParameters halted the debugger at this line on AD 26.9.1+      (2026-08-29) — the supported form is ResetParameters + one+      AddStringParameter per Name=Value pair, then RunProcess(Command). }+    ResetParameters;+    Rest := P;+    While Rest <> '' Do+    Begin+        K := Pos('|', Rest);+        If K > 0 Then+        Begin+            Pair := Copy(Rest, 1, K - 1);+            Rest := Copy(Rest, K + 1, Length(Rest));+        End+        Else+        Begin+            Pair := Rest;+            Rest := '';+        End;+        K := Pos('=', Pair);+        If K > 0 Then AddStringParameter(Copy(Pair, 1, K - 1), Copy(Pair, K + 1, Length(Pair)));+    End;+    RunProcess(Cmd);+    Result := '{' + JRaw('ok', 'true') + ',' + JStr('process', Cmd) + ',' + JStr('params', P) + '}';+End;++{ a path in one spelling, to compare two: backslashes, upper case }+Function SamePathText(P : String) : String;+Var+    K : Integer;+Begin+    Result := '';+    For K := 1 To Length(P) Do+        If Copy(P, K, 1) = '/' Then Result := Result + '\' Else Result := Result + Copy(P, K, 1);+    Result := UpperCase(Result);+End;++{ The document open at Path under any spelling of it (C:/x/a.PcbLib is C:\x\a.PcbLib), Nil when none. GetDocumentByPath+  matches the spelling exactly, so a second open made Altium hold a second copy of the file, and an edit on one copy+  was never seen by a read of the other (three copies of Battery.PcbLib, 2026-10-30). }+Function DocAt(Path : String) : IServerDocument;+Var+    SM   : IServerModule;+    D    : IServerDocument;+    I, J : Integer;+Begin+    Result := Nil;+    Try Result := Client.GetDocumentByPath(Path); Except Result := Nil; End;+    If Result <> Nil Then Exit;+    For I := 0 To Client.Count - 1 Do+    Begin+        SM := Client.ServerModule[I];+        If SM = Nil Then Continue;+        For J := 0 To SM.DocumentCount - 1 Do+        Begin+            D := SM.Documents[J];+            If (D <> Nil) And (Result = Nil) Then+                If SamePathText(D.FileName) = SamePathText(Path) Then Result := D;+        End;+    End;+End;++{ Open a document or project by path. Client.OpenDocument('', path) returns Nil on+  AD 26.9.1 (measured), so go through the WorkspaceManager process launcher, then+  look the document up and show it. .PrjPcb/.PrjScr/.PrjMbd open as a Project. }+Function CmdOpen(Lines : TStringList) : String;+Var+    Path, Ext, Kind : String;+    Doc  : IServerDocument;+    K    : Integer;+Begin+    Path := Param(Lines, 'path');+    If Not FileExists(Path) Then+    Begin+        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'file not found: ' + Path) + '}';+        Exit;+    End;+    Ext := '';+    K := Length(Path);+    While (K > 0) And (Path[K] <> '.') Do K := K - 1;+    If K > 0 Then Ext := UpperCase(Copy(Path, K + 1, Length(Path)));+    { Altium wants the DOCUMENT KIND, not a generic word. Passing ObjectKind='Document' makes it+      answer "File type not recognised." with a modal on the user's screen (measured twice on+      2026-09-01) even for a perfectly valid .PcbDoc. Map the extension to the kind Altium uses,+      and open through Client.OpenDocument — the sibling of ShowDocument, which this unit already+      relies on — falling back to the OpenObject process only if that raises. }+    Kind := '';+    If (Ext = 'PRJPCB') Or (Ext = 'PRJSCR') Or (Ext = 'PRJMBD') Or (Ext = 'PRJHAR') Or (Ext = 'LIBPKG') Then+        Kind := 'Project'+    Else If Ext = 'PCBDOC' Then Kind := 'PCB'+    Else If Ext = 'SCHDOC' Then Kind := 'SCH'+    Else If Ext = 'PCBLIB' Then Kind := 'PCBLIB'+    Else If Ext = 'SCHLIB' Then Kind := 'SCHLIB'+    Else If Ext = 'OUTJOB' Then Kind := 'OUTPUTJOB'+    Else If Ext = 'PCB3DLIB' Then Kind := 'PCB3DLIB'+    Else If (Ext = 'TXT') Or (Ext = 'PAS') Or (Ext = 'REP') Then Kind := 'TEXT';+    If Kind = '' Then+    Begin+        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'unsupported document type: .' + Ext) + ',' ++            JStr('supported', 'PcbDoc, SchDoc, PcbLib, SchLib, OutJob, PrjPcb, PrjScr, txt/pas/rep') + '}';+        Exit;+    End;+    If Kind <> 'Project' Then+    Begin+        { open under another spelling of the path, it is this file: open it by ITS spelling, and Altium answers that+          document rather than a second copy. OpenDocument still runs: showing the found document without it left no+          current board, and DRC on it crashed in ADVPCB.DLL (two batteries, 2026-10-30) }+        Doc := DocAt(Path);+        If Doc <> Nil Then Path := Doc.FileName;+        Doc := Nil;+        Try+            Doc := Client.OpenDocument(Kind, Path);+        Except+            Doc := Nil;+        End;+        If Doc <> Nil Then+        Begin+            Try+                Client.ShowDocumentDontFocus(Doc);+            Except+                Client.ShowDocument(Doc);+            End;+            Result := '{' + JRaw('ok', 'true') + ',' + JStr('path', Path) + ',' + JStr('objectKind', Kind) + ',' ++                JStr('kind', Doc.Kind) + ',' + JRaw('shown', 'true') + ',' + JStr('lane', 'OpenDocument') + '}';+            Exit;+        End;+    End;+    { fallback: the process lane, with the mapped kind rather than a generic one }+    ResetParameters;+    AddStringParameter('ObjectKind', Kind);+    AddStringParameter('FileName', Path);+    RunProcess('WorkspaceManager:OpenObject');+    Doc := Nil;+    Try+        Doc := Client.GetDocumentByPath(Path);+    Except+    End;+    If Doc <> Nil Then+    Begin+        Try+            Client.ShowDocumentDontFocus(Doc);+        Except+            Client.ShowDocument(Doc);+        End;+        Result := '{' + JRaw('ok', 'true') + ',' + JStr('path', Path) + ',' + JStr('objectKind', Kind) + ',' + JStr('kind', Doc.Kind) + ',' + JRaw('shown', 'true') + '}';+    End+    Else+        Result := '{' + JRaw('ok', 'true') + ',' + JStr('path', Path) + ',' + JStr('objectKind', Kind) + ',' + JRaw('shown', 'false') + ',' + JStr('note', 'OpenObject ran; document handle not resolved (projects have no document view)') + '}';+End;++{ Close ONE open document by path, never saving (a caller who wants a save saves first). Goes+  through the document HANDLE: the menu processes are broken for this on ADDevelop - RunProcess+  Client:CloseDocument no-ops, and Client:CloseAllOpenDocuments unregisters documents while+  their views stay open (both measured 2026-09-02, adom/altium-bridge issue 7). }+{ Before a NEW document is named onto Path: close any document already open at that path, unless it has changes.+  Altium holds a second document at the same path otherwise, and AD 26 (which reopens its last session) then crashed+  in ADVPCB.DLL when DRC ran on the battery's board (measured 2026-10-27, twice). }+Procedure FreePath(Path : String);+Var+    SM : IServerModule;+    D  : IServerDocument;+    I, J : Integer;+    Changed : Boolean;+Begin+    If Path = '' Then Exit;+    For I := 0 To Client.Count - 1 Do+    Begin+        SM := Client.ServerModule[I];+        If SM = Nil Then Continue;+        For J := SM.DocumentCount - 1 DownTo 0 Do+        Begin+            D := SM.Documents[J];+            If (D = Nil) Or (SamePathText(D.FileName) <> SamePathText(Path)) Then Continue;+            Changed := True;+            Try Changed := D.Modified; Except End;+            If Not Changed Then Try Client.CloseDocument(D); Except End;+        End;+    End;+End;++Function CmdCloseDocument(Lines : TStringList) : String;+Var+    Path : String;+    Doc, D : IServerDocument;+    SM   : IServerModule;+    I, J : Integer;+Begin+    Path := Param(Lines, 'path');+    Doc := Nil;+    Try+        Doc := Client.GetDocumentByPath(Path);+    Except+    End;+    { a document made in memory and saved under a name afterwards (altium_panelize's panel) is not found by path on+      Develop (2026-10-26): walk every server's documents by file name (the bridge sends backslashes) }+    If Doc = Nil Then+    Begin+        For I := 0 To Client.Count - 1 Do+        Begin+            SM := Client.ServerModule[I];+            If SM = Nil Then Continue;+            For J := 0 To SM.DocumentCount - 1 Do+            Begin+                D := SM.Documents[J];+                If (D <> Nil) And (SamePathText(D.FileName) = SamePathText(Path)) Then Doc := D;+            End;+        End;+    End;+    If Doc = Nil Then+    Begin+        Result := '{' + JRaw('ok', 'true') + ',' + JRaw('closed', 'false') + ',' + JStr('note', 'not open: ' + Path) + '}';+        Exit;+    End;+    Try+        Client.CloseDocument(Doc);+        Result := '{' + JRaw('ok', 'true') + ',' + JRaw('closed', 'true') + ',' + JStr('path', Path) + '}';+    Except+        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'CloseDocument raised for ' + Path) + '}';+    End;+End;++{ Inline statements: RunScriptText from INSIDE the agent (measured separately;+  if the engine refuses nested scripts this returns the error in-band). }+Function CmdEval(Lines : TStringList; Payload : String) : String;+Var+    Text : String;+Begin+    Text := Payload;+    If Text = '' Then Text := Param(Lines, 'text');+    If Text = '' Then+    Begin+        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'text (statements only, Begin ... End;) is required') + '}';+        Exit;+    End;+    ResetParameters;+    AddStringParameter('Text', Text);+    RunProcess('ScriptingSystem:RunScriptText');+    Result := '{' + JRaw('ok', 'true') + ',' + JStr('note', 'RunScriptText dispatched; an optional Var block, then Begin ... End;; capture output by writing a file') + '}';+End;+++{ ------------------------------------------------------------ libraries --- }++{ List components of the CURRENT library document (open it first). kind=sch|pcb }+Function CmdLibList(Lines : TStringList) : String;+Var+    Kind  : String;+    SLib  : ISch_Lib;+    Iter  : ISch_Iterator;+    Comp  : ISch_Component;+    PLib  : IPCB_Library;+    I, N  : Integer;+    Names : String;+    Limit : Integer;+Begin+    Kind := Param(Lines, 'kind');+    Limit := StrToIntDef(Param(Lines, 'limit'), 200);+    Names := '';+    N := 0;+    If Kind = 'sch' Then+    Begin+        SLib := SchServer.GetCurrentSchDocument;+        If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then+        Begin+            Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'current document is not a schematic library') + '}';+            Exit;+        End;+        Iter := SLib.SchLibIterator_Create;+        Iter.AddFilter_ObjectSet(MkSet(eSchComponent));+        Comp := Iter.FirstSchObject;+        While Comp <> Nil Do+        Begin+            If N < Limit Then+            Begin+                If Names <> '' Then Names := Names + ',';+                Names := Names + '"' + JsonEsc(Comp.LibReference) + '"';+            End;+            N := N + 1;+            Comp := Iter.NextSchObject;+        End;+        SLib.SchIterator_Destroy(Iter);+    End+    Else+    Begin+        PLib := PCBServer.GetCurrentPCBLibrary;+        If PLib = Nil Then+        Begin+            Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'current document is not a PCB library') + '}';+            Exit;+        End;+        For I := 0 To PLib.ComponentCount - 1 Do+        Begin+            If N < Limit Then+            Begin+                If Names <> '' Then Names := Names + ',';+                Names := Names + '"' + JsonEsc(PLib.GetComponent(I).Name) + '"';+            End;+            N := N + 1;+        End;+    End;+    Result := '{' + JRaw('ok', 'true') + ',' + JStr('kind', Kind) + ',' + JRaw('count', IntToStr(N)) + ',' + JRaw('names', '[' + Names + ']') + '}';+End;++{ Make one component current in the open library editor. kind=sch|pcb name=<LibReference|footprint>+  (name '' = the first one), then zoom all. }+Function CmdLibSelect(Lines : TStringList) : String;+Var+    Kind, Name, Found : String;+    SLib  : ISch_Lib;+    Iter  : ISch_Iterator;+    Comp  : ISch_Component;+    PLib  : IPCB_Library;+    PComp : IPCB_LibComponent;+    I     : Integer;+Begin+    Kind := Param(Lines, 'kind');+    Name := Param(Lines, 'name');+    Found := '';+    If Kind = 'sch' Then+    Begin+        SLib := SchServer.GetCurrentSchDocument;+        If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then+        Begin+            Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'current document is not a schematic library') + '}';+            Exit;+        End;+        Iter := SLib.SchLibIterator_Create;+        Iter.AddFilter_ObjectSet(MkSet(eSchComponent));+        Comp := Iter.FirstSchObject;+        While Comp <> Nil Do+        Begin+            If (Name = '') Or (UpperCase(Comp.LibReference) = UpperCase(Name)) Then+            Begin+                SLib.CurrentSchComponent := Comp;+                Found := Comp.LibReference;+                Break;+            End;+            Comp := Iter.NextSchObject;+        End;+        SLib.SchIterator_Destroy(Iter);+        If Found <> '' Then+        Begin+            SLib.GraphicallyInvalidate;+            ResetParameters;+            AddStringParameter('Action', 'All');+            RunProcess('Sch:Zoom');+        End;+    End+    Else+    Begin+        PLib := PCBServer.GetCurrentPCBLibrary;+        If PLib = Nil Then+        Begin+            Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'current document is not a PCB library') + '}';+            Exit;+        End;+        For I := 0 To PLib.ComponentCount - 1 Do+        Begin+            PComp := PLib.GetComponent(I);+            If (Name = '') Or (UpperCase(PComp.Name) = UpperCase(Name)) Then+            Begin+                PLib.CurrentComponent := PComp;+                Found := PComp.Name;+                Break;+            End;+        End;+        If Found <> '' Then+        Begin+            PLib.RefreshView;+            ResetParameters;+            AddStringParameter('Action', 'All');+            RunProcess('PCB:Zoom');+        End;+    End;+    If Found = '' Then+        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'component not found: ' + Name) + ',' + JStr('hint', 'lib_list to see names') + '}'+    Else+        Result := '{' + JRaw('ok', 'true') + ',' + JStr('kind', Kind) + ',' + JStr('selected', Found) + '}';+End;++{ ------------------------------------------------------------- outputs --- }++{ Compile the focused project (= ERC for schematics) and return the Messages panel.+  IMessagesManager (Workspace Manager API): MessagesCount, Messages(i) -> IMessageItem+  with MsgClass, Text, Source, Document. DM_MessageCount does not exist (measured).+  NEVER nest braces inside a brace comment - the inner one ends the comment early+  and the rest becomes code ("Error in declaration block"). }+Function CmdCompile(Lines : TStringList) : String;+Var+    WS   : IWorkspace;+    MM   : IDMMessagesManager;+    I, N : Integer;+    Msgs : String;+    Kind : String;+    Limit: Integer;+Begin+    Kind := Param(Lines, 'objectKind');+    If Kind = '' Then Kind := 'Project';+    Limit := StrToIntDef(Param(Lines, 'limit'), 200);+    ResetParameters;+    AddStringParameter('Action', 'Compile');+    AddStringParameter('ObjectKind', Kind);+    RunProcess('WorkspaceManager:Compile');+    Msgs := '';+    N := 0;+    WS := GetWorkspace;+    If WS <> Nil Then+    Begin+        MM := WS.DM_MessagesManager;+        If MM <> Nil Then+        Begin+            N := MM.MessagesCount;+            For I := 0 To N - 1 Do+            Begin+                If I >= Limit Then Break;+                If Msgs <> '' Then Msgs := Msgs + ',';+                Msgs := Msgs + '{' + JStr('class', MM.Messages(I).MsgClass) + ',' + JStr('source', MM.Messages(I).Source) + ',' + JStr('document', MM.Messages(I).Document) + ',' + JStr('text', MM.Messages(I).Text) + '}';+            End;+        End;+    End;+    Result := '{' + JRaw('ok', 'true') + ',' + JStr('objectKind', Kind) + ',' + JRaw('messageCount', IntToStr(N)) + ',' + JRaw('messages', '[' + Msgs + ']') + '}';+End;++{ messages: the Messages panel as it stands, compiling nothing (log_tail reads it). limit= caps the count. }+Function CmdMessages(Lines : TStringList) : String;+Var+    WS   : IWorkspace;+    MM   : IDMMessagesManager;+    I, N, Limit : Integer;+    Msgs : String;+Begin+    Limit := StrToIntDef(Param(Lines, 'limit'), 2000);+    Msgs := ''; N := 0;+    WS := GetWorkspace;+    If WS <> Nil Then+    Begin+        MM := WS.DM_MessagesManager;+        If MM <> Nil Then+        Begin+            N := MM.MessagesCount;+            For I := 0 To N - 1 Do+            Begin+                If I >= Limit Then Break;+                If Msgs <> '' Then Msgs := Msgs + ',';+                Msgs := Msgs + '{' + JStr('class', MM.Messages(I).MsgClass) + ',' + JStr('source', MM.Messages(I).Source) + ',' + JStr('document', MM.Messages(I).Document) + ',' + JStr('text', MM.Messages(I).Text) + '}';+            End;+        End;+    End;+    Result := '{' + JRaw('ok', 'true') + ',' + JRaw('messageCount', IntToStr(N)) + ',' + JRaw('messages', '[' + Msgs + ']') + '}';+End;++{ DRC violations currently on the focused board (run PCB:DesignRuleCheck first). }+Function CmdViolations(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    Iter  : IPCB_BoardIterator;+    V     : IPCB_Violation;+    N     : Integer;+    Items : String;+    Limit : Integer;+Begin+    Limit := StrToIntDef(Param(Lines, 'limit'), 200);+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then+    Begin+        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'no PCB document is current') + '}';+        Exit;+    End;+    Items := '';+    N := 0;+    Iter := Board.BoardIterator_Create;+    Iter.AddFilter_ObjectSet(MkSet(eViolationObject));+    Iter.AddFilter_LayerSet(AllLayers);+    Iter.AddFilter_Method(eProcessAll);+    V := Iter.FirstPCBObject;+    While V <> Nil Do+    Begin+        If N < Limit Then+        Begin+            If Items <> '' Then Items := Items + ',';+            Items := Items + '{' + JStr('rule', V.Rule.Name) + ',' + JStr('description', V.Description) + '}';+        End;+        N := N + 1;+        V := Iter.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(Iter);+    Result := '{' + JRaw('ok', 'true') + ',' + JStr('board', Board.FileName) + ',' + JRaw('violationCount', IntToStr(N)) + ',' + JRaw('violations', '[' + Items + ']') + '}';+End;++{ Run an OutJob medium. path=<.OutJob>  medium=<OutputMedium name, e.g. Folder Structure> }+Function CmdOutJob(Lines : TStringList) : String;+Var+    Path, Medium, Kind : String;+    Doc : IServerDocument;+Begin+    Path := Param(Lines, 'path');+    Medium := Param(Lines, 'medium');+    Kind := Param(Lines, 'objectKind');+    If Kind = '' Then Kind := 'OutputBatch';+    If Not FileExists(Path) Then+    Begin+        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'OutJob not found: ' + Path) + '}';+        Exit;+    End;+    { the OutJob's own document kind: ObjectKind 'Document' is the generic word Altium answers with+      "File type not recognised" (see CmdOpen) }+    Doc := Nil;+    Try+        Doc := Client.OpenDocument('OUTPUTJOB', Path);+    Except+        Doc := Nil;+    End;+    If Doc = Nil Then+    Begin+        ResetParameters;+        AddStringParameter('ObjectKind', 'OUTPUTJOB');+        AddStringParameter('FileName', Path);+        RunProcess('WorkspaceManager:OpenObject');+        Doc := Client.GetDocumentByPath(Path);+    End;+    If Doc <> Nil Then Client.ShowDocument(Doc);+    ResetParameters;+    AddStringParameter('Action', 'Run');+    AddStringParameter('ObjectKind', Kind);+    If Medium <> '' Then AddStringParameter('OutputMedium', Medium);+    AddStringParameter('DisableDialog', 'True');+    RunProcess('WorkspaceManager:GenerateReport');+    Result := '{' + JRaw('ok', 'true') + ',' + JStr('outjob', Path) + ',' + JStr('medium', Medium) + ',' + JStr('objectKind', Kind) + '}';+End;++{ ------------------------------------------------------- library authoring --- }++{ Split "a;b;c" into a TStringList (one item per ';'). }+Function SplitSemi(S : String) : TStringList;+Var+    K : Integer;+    Part : String;+Begin+    Result := TStringList.Create;+    While S <> '' Do+    Begin+        K := Pos(';', S);+        If K > 0 Then+        Begin+            Part := Copy(S, 1, K - 1);+            S := Copy(S, K + 1, Length(S));+        End+        Else+        Begin+            Part := S;+            S := '';+        End;+        If Trim(Part) <> '' Then Result.Add(Trim(Part));+    End;+End;++{ Field N (1-based) of a comma list. }+Function Field(S : String; N : Integer) : String;+Var+    I, K : Integer;+Begin+    Result := '';+    For I := 1 To N Do+    Begin+        K := Pos(',', S);+        If K > 0 Then+        Begin+            Result := Copy(S, 1, K - 1);+            S := Copy(S, K + 1, Length(S));+        End+        Else+        Begin+            Result := S;+            S := '';+        End;+    End;+    Result := Trim(Result);+End;++{ Open an existing library or create + save a new one. kind=PCBLIB|SCHLIB  path=<file> }+Function EnsureLibrary(Kind : String; Path : String) : String;+Var+    Doc : IServerDocument;+Begin+    If FileExists(Path) Then+    Begin+        ResetParameters;+        AddStringParameter('ObjectKind', 'Document');+        AddStringParameter('FileName', Path);+        RunProcess('WorkspaceManager:OpenObject');+        Doc := Client.GetDocumentByPath(Path);+        If Doc <> Nil Then Client.ShowDocument(Doc);+        Result := 'opened';+    End+    Else+    Begin+        Doc := CreateNewDocumentFromDocumentKind(Kind);+        If Doc = Nil Then+        Begin+            Result := 'error: could not create ' + Kind;+            Exit;+        End;+        Client.ShowDocument(Doc);+        Doc.DoSafeChangeFileNameAndSave(Path, Kind);+        Result := 'created';+    End;+End;++{ create_footprint: name, libraryPath, description,+  pads = des,x_mil,y_mil,w_mil,h_mil,shape(rect|round),hole_mil ; ...   (hole 0 = SMD) }+Function CmdCreateFootprint(Lines : TStringList) : String;+Var+    PLib  : IPCB_Library;+    Comp  : IPCB_LibComponent;+    Pad   : IPCB_Pad;+    Pads  : TStringList;+    I     : Integer;+    Name, LibPath, Desc, Spec, Shape, LibState : String;+    Hole  : Integer;+Begin+    Name := Param(Lines, 'name');+    LibPath := Param(Lines, 'libraryPath');+    Desc := Param(Lines, 'description');+    If (Name = '') Or (LibPath = '') Then+    Begin+        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'name and libraryPath are required') + '}';+        Exit;+    End;+    LibState := EnsureLibrary('PCBLIB', LibPath);+    PLib := PCBServer.GetCurrentPCBLibrary;+    If PLib = Nil Then+    Begin+        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'no PCB library is current (' + LibState + ')') + '}';+        Exit;+    End;+    PCBServer.PreProcess;+    Comp := PCBServer.CreatePCBLibComp;+    Comp.Name := Name;+    Comp.Description := Desc;+    Pads := SplitSemi(Param(Lines, 'pads'));+    For I := 0 To Pads.Count - 1 Do+    Begin+        Spec := Pads[I];+        Pad := PCBServer.PCBObjectFactory(ePadObject, eNoDimension, eCreate_Default);+        Pad.Name := Field(Spec, 1);+        Pad.X := MilsToCoord(AdomFloat(Field(Spec, 2)));+        Pad.Y := MilsToCoord(AdomFloat(Field(Spec, 3)));+        Pad.TopXSize := MilsToCoord(AdomFloat(Field(Spec, 4)));+        Pad.TopYSize := MilsToCoord(AdomFloat(Field(Spec, 5)));+        Shape := LowerCase(Field(Spec, 6));+        If Shape = 'round' Then Pad.TopShape := eRounded Else Pad.TopShape := eRectangular;+        Hole := 0;+        If Field(Spec, 7) <> '' Then Hole := AdomInt(Field(Spec, 7));+        If Hole > 0 Then+        Begin+            Pad.HoleSize := MilsToCoord(Hole);+            Pad.Layer := eMultiLayer;+        End+        Else+        Begin+            Pad.HoleSize := 0;+            Pad.Layer := eTopLayer;+        End;+        Comp.AddPCBObject(Pad);+    End;+    Pads.Free;+    PLib.RegisterComponent(Comp);+    PLib.CurrentComponent := Comp;+    PCBServer.PostProcess;+    PLib.Board.ViewManager_FullUpdate;+    PLib.RefreshView;+    ResetParameters;+    AddStringParameter('Action', 'All');+    RunProcess('PCB:Zoom');+    Result := '{' + JRaw('ok', 'true') + ',' + JStr('footprint', Name) + ',' + JStr('library', LibPath) + ',' + JStr('libraryState', LibState) + ',' + JRaw('pads', IntToStr(Comp.GetPrimitiveCount(MkSet(ePadObject)))) + '}';+End;++{ create_symbol: name, libraryPath, designator(U), description, body_w_mil, body_h_mil,+  pins = des,name,side(left|right|top|bottom),offset_mil ; ...  }+Function CmdCreateSymbol(Lines : TStringList) : String;+Var+    SLib  : ISch_Lib;+    Comp  : ISch_Component;+    Rect  : ISch_Rectangle;+    Pin   : ISch_Pin;+    Pins  : TStringList;+    I, W, H, Off, Parts, P : Integer;+    Name, LibPath, Desc, Des, Spec, Side, LibState : String;+Begin+    Name := Param(Lines, 'name');+    LibPath := Param(Lines, 'libraryPath');+    Desc := Param(Lines, 'description');+    Des := Param(Lines, 'designator');+    If Des = '' Then Des := 'U';+    W := StrToIntDef(Param(Lines, 'body_w_mil'), 800);+    H := StrToIntDef(Param(Lines, 'body_h_mil'), 600);+    If (Name = '') Or (LibPath = '') Then+    Begin+        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'name and libraryPath are required') + '}';+        Exit;+    End;+    LibState := EnsureLibrary('SCHLIB', LibPath);+    SLib := SchServer.GetCurrentSchDocument;+    If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then+    Begin+        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'no schematic library is current (' + LibState + ')') + '}';+        Exit;+    End;+    Comp := SchServer.SchObjectFactory(eSchComponent, eCreate_Default);+    Comp.CurrentPartID := 1;+    Comp.DisplayMode := 0;+    Comp.LibReference := Name;+    Comp.ComponentDescription := Desc;+    Comp.Designator.Text := Des;+    Pins := SplitSemi(Param(Lines, 'pins'));+    { multi-part: a pin's 5th field is its part (1..N; 0 = on every part, e.g. power); one body per part }+    Parts := 1;+    For I := 0 To Pins.Count - 1 Do+        If StrToIntDef(Field(Pins[I], 5), 1) > Parts Then Parts := StrToIntDef(Field(Pins[I], 5), 1);+    If Parts > 1 Then Comp.PartCount := Parts;+    For P := 1 To Parts Do+    Begin+        Rect := SchServer.SchObjectFactory(eRectangle, eCreate_Default);+        Rect.OwnerPartId := P;+        Rect.OwnerPartDisplayMode := 0;+        Rect.Location := Point(MilsToCoord(0), MilsToCoord(0));+        Rect.Corner := Point(MilsToCoord(W), MilsToCoord(H));+        Rect.IsSolid := True;+        Comp.AddSchObject(Rect);+    End;+    For I := 0 To Pins.Count - 1 Do+    Begin+        Spec := Pins[I];+        Pin := SchServer.SchObjectFactory(ePin, eCreate_Default);+        Pin.OwnerPartId := StrToIntDef(Field(Spec, 5), 1);+        Pin.OwnerPartDisplayMode := 0;+        Pin.Designator := Field(Spec, 1);+        Pin.Name := Field(Spec, 2);+        Side := LowerCase(Field(Spec, 3));+        Off := StrToIntDef(Field(Spec, 4), 100);+        Pin.PinLength := MilsToCoord(300);+        If Side = 'right' Then+        Begin+            Pin.Orientation := eRotate0;+            Pin.Location := Point(MilsToCoord(W), MilsToCoord(H - Off));+        End+        Else If Side = 'top' Then+        Begin+            Pin.Orientation := eRotate90;+            Pin.Location := Point(MilsToCoord(Off), MilsToCoord(H));+        End+        Else If Side = 'bottom' Then+        Begin+            Pin.Orientation := eRotate270;+            Pin.Location := Point(MilsToCoord(Off), MilsToCoord(0));+        End+        Else+        Begin+            Pin.Orientation := eRotate180;+            Pin.Location := Point(MilsToCoord(0), MilsToCoord(H - Off));+        End;+        { a 6th field hides the pin on that net: shared power pins of a multi-part symbol (part 0) are hidden and+          joined to their net by name, or every part shows them and the compiler reports duplicate pins (measured+          2026-10-23: Altium's own placement of visible part-0 pins gave the same warnings) }+        If Trim(Field(Spec, 6)) <> '' Then+        Begin+            Pin.IsHidden := True;+            Pin.HiddenNetName := Trim(Field(Spec, 6));+        End;+        Comp.AddSchObject(Pin);+    End;+    I := Pins.Count;+    Pins.Free;+    SLib.AddSchComponent(Comp);+    SchServer.RobotManager.SendMessage(SLib.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, Comp.I_ObjectAddress);+    SLib.CurrentSchComponent := Comp;+    SLib.GraphicallyInvalidate;+    ResetParameters;+    AddStringParameter('Action', 'All');+    RunProcess('Sch:Zoom');+    Result := '{' + JRaw('ok', 'true') + ',' + JStr('symbol', Name) + ',' + JStr('library', LibPath) + ',' + JStr('libraryState', LibState) + ',' + JRaw('pins', IntToStr(I)) + ',' + JRaw('parts', IntToStr(Parts)) + '}';+End;++{ --------------------------------------------- library management (Barrett port) --- }++{ Register a library in Altium's installed-libraries list (IntLib, SchLib, PcbLib, DbLib). }+{ Every library in Altium's Installed Libraries list, as a JSON array of paths. }+Function CmdListInstalledLibs(Lines : TStringList) : String;+Var+    I, N : Integer;+    S : String;+Begin+    N := IntegratedLibraryManager.InstalledLibraryCount;+    S := '';+    For I := 0 To N - 1 Do+    Begin+        If S <> '' Then S := S + ',';+        S := S + '"' + JsonEsc(IntegratedLibraryManager.InstalledLibraryPath(I)) + '"';+    End;+    Result := '{' + JRaw('ok','true') + ',' + JRaw('count', IntToStr(N)) + ',' + JRaw('libraries', '[' + S + ']') + '}';+End;++{ Unregister installed libraries by exact path (paths=a;b). The files are never touched. A+  library that is open as a document is closed first: an installed SchLib loads as a document+  that comes up modified, and a close that discards it drops the unregistration (measured by+  John on Altium Develop, 2026-09-23). Removal takes effect live. }+Function CmdUninstallLibrary(Lines : TStringList) : String;+Var+    PL : TStringList;+    K : Integer;+    P, Removed, Closed : String;+    Doc : IServerDocument;+Begin+    PL := SplitSemi(Param(Lines, 'paths'));+    Removed := ''; Closed := '';+    For K := 0 To PL.Count - 1 Do+    Begin+        P := PL[K];+        Doc := Nil;+        Try+            Doc := Client.GetDocumentByPath(P);+        Except+            Doc := Nil;+        End;+        If Doc <> Nil Then+        Begin+            Try+                Client.CloseDocument(Doc);+                If Closed <> '' Then Closed := Closed + ',';+                Closed := Closed + '"' + JsonEsc(P) + '"';+            Except+            End;+        End;+        IntegratedLibraryManager.UninstallLibrary(P);+        If Removed <> '' Then Removed := Removed + ',';+        Removed := Removed + '"' + JsonEsc(P) + '"';+    End;+    PL.Free;+    Result := '{' + JRaw('ok','true') + ',' + JRaw('removed', '[' + Removed + ']') + ',' ++        JRaw('closedDocuments', '[' + Closed + ']') + ',' ++        JRaw('remaining', IntToStr(IntegratedLibraryManager.InstalledLibraryCount)) + '}';+End;++Function CmdInstallLibrary(Lines : TStringList) : String;+Var+    Path : String;+Begin+    Path := Param(Lines, 'path');+    If Not FileExists(Path) Then+    Begin+        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'library not found: ' + Path) + '}';+        Exit;+    End;+    IntegratedLibraryManager.InstallLibrary(Path);+    Result := '{' + JRaw('ok', 'true') + ',' + JStr('installed', Path) + '}';+End;++{ Compile a .LibPkg into a .IntLib: path=<LibPkg> output=<dir> }+Function CmdCompileIntLib(Lines : TStringList) : String;+Var+    Path, OutDir : String;+    Proj : IProject;+Begin+    Path := Param(Lines, 'path');+    OutDir := Param(Lines, 'output');+    If Not FileExists(Path) Then+    Begin+        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'LibPkg not found: ' + Path) + '}';+        Exit;+    End;+    Proj := GetWorkspace.DM_OpenProject(Path, True);+    If Proj = Nil Then+    Begin+        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'could not open LibPkg') + '}';+        Exit;+    End;+    Proj.DM_Compile;+    IntegratedLibraryManager.CreateIntegratedLibrary(Proj, OutDir, True);+    Result := '{' + JRaw('ok', 'true') + ',' + JStr('libpkg', Path) + ',' + JStr('outputDir', OutDir) + '}';+End;++{ The folder part of a path, ending in '\'. Built from Copy/Length only: SysUtils names that+  this engine does not expose raise an Error box and park the script on a breakpoint, so the agent+  never calls one that is not already proven elsewhere in this file. }+Function AdomDirOf(P : String) : String;+Var+    I : Integer;+Begin+    Result := '';+    For I := Length(P) DownTo 1 Do+    Begin+        If (Copy(P, I, 1) = '\') Or (Copy(P, I, 1) = '/') Then+        Begin+            Result := Copy(P, 1, I);+            Break;+        End;+    End;+End;++{ Extract editable SchLib/PcbLib sources from a compiled .IntLib: path=<IntLib> }+Function CmdExtractIntLib(Lines : TStringList) : String;+Var+    Path, P, Acc, Dir, UP : String;+    WS : IWorkspace;+    Prj : IProject;+    Doc : IDocument;+    I, J : Integer;+Begin+    Path := Param(Lines, 'path');+    If Not FileExists(Path) Then+    Begin+        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'IntLib not found: ' + Path) + '}';+        Exit;+    End;+    IntegratedLibraryManager.ExtractSources(Path);+    { ExtractSources writes the editable libraries BESIDE the .IntLib. This used to report every+      .SchLib/.PcbLib open anywhere in the workspace, so a caller with other libraries open was+      handed paths that had nothing to do with this IntLib. Scope it to the IntLib's own folder. }+    Dir := UpperCase(AdomDirOf(Path));+    Acc := '';+    WS := GetWorkspace;+    If WS <> Nil Then+        For I := 0 To WS.DM_ProjectCount - 1 Do+        Begin+            Prj := WS.DM_Projects(I);+            For J := 0 To Prj.DM_LogicalDocumentCount - 1 Do+            Begin+                Doc := Prj.DM_LogicalDocuments(J);+                P := Doc.DM_FullPath;+                UP := UpperCase(P);+                If ((Pos('.SCHLIB', UP) > 0) Or (Pos('.PCBLIB', UP) > 0)) And+                   ((Dir = '') Or (Pos(Dir, UP) = 1)) Then+                Begin+                    If Acc <> '' Then Acc := Acc + ',';+                    Acc := Acc + '"' + JsonEsc(P) + '"';+                End;+            End;+        End;+    Result := '{' + JRaw('ok', 'true') + ',' + JStr('intlib', Path) + ',' ++        JStr('searchedUnder', AdomDirOf(Path)) + ',' + JRaw('sources', '[' + Acc + ']') + '}';+End;++{ Set/replace parameters on a component of the CURRENT SchLib and optionally link a footprint.+  name=<LibReference>  params=Key=Value;Key=Value  footprint=<PcbLib footprint name>  comment=<text> }+Procedure SetSchParam(Cmp : ISch_Component; PName : String; PVal : String);+Var+    It : ISch_Iterator;+    Pm, Existing : ISch_Parameter;+Begin+    Existing := Nil;+    It := Cmp.SchIterator_Create;+    It.AddFilter_ObjectSet(MkSet(eParameter));+    Pm := It.FirstSchObject;+    While Pm <> Nil Do+    Begin+        If Pm.Name = PName Then+        Begin+            Existing := Pm;+            Break;+        End;+        Pm := It.NextSchObject;+    End;+    Cmp.SchIterator_Destroy(It);+    If Existing <> Nil Then+        Existing.Text := PVal+    Else+    Begin+        Pm := SchServer.SchObjectFactory(eParameter, eCreate_GlobalCopy);+        Pm.Name := PName;+        Pm.Text := PVal;+        { library-part parameters are metadata; visible ones pile up over the symbol (measured 2026-08-30) }+        Pm.IsHidden := True;+        Cmp.AddSchObject(Pm);+    End;+End;++Function CmdSetParams(Lines : TStringList) : String;+Var+    SLib : ISch_Lib;+    Iter : ISch_Iterator;+    Comp : ISch_Component;+    Impl : ISch_Implementation;+    C2   : ISch_Component;+    Name, Fp, Cmt, Spec, K, V : String;+    Items : TStringList;+    I, P, N : Integer;+Begin+    Name := Param(Lines, 'name');+    Fp := Param(Lines, 'footprint');+    Cmt := Param(Lines, 'comment');+    SLib := SchServer.GetCurrentSchDocument;+    If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then+    Begin+        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'current document is not a schematic library') + '}';+        Exit;+    End;+    { With no name this used to take whichever component the iterator yielded FIRST, so a+      set_params against a multi-symbol library silently wrote the footprint and comment onto+      an arbitrary part. An omitted name may only mean "the one component in here". }+    Comp := Nil;+    N := 0;+    Iter := SLib.SchLibIterator_Create;+    Iter.AddFilter_ObjectSet(MkSet(eSchComponent));+    C2 := Iter.FirstSchObject;+    While C2 <> Nil Do+    Begin+        N := N + 1;+        If Name = '' Then+        Begin+            If Comp = Nil Then Comp := C2;+        End+        Else If UpperCase(C2.LibReference) = UpperCase(Name) Then Comp := C2;+        C2 := Iter.NextSchObject;+    End;+    SLib.SchIterator_Destroy(Iter);+    If (Name = '') And (N > 1) Then+    Begin+        Result := '{' + JRaw('ok', 'false') + ',' + JStr('errorCode', 'name_required') + ',' ++            JStr('error', 'this library holds ' + IntToStr(N) + ' components: name the one to set') + '}';+        Exit;+    End;+    If Comp = Nil Then+    Begin+        Result := '{' + JRaw('ok', 'false') + ',' + JStr('errorCode', 'name_not_found') + ',' ++            JStr('error', 'component not found: ' + Name) + '}';+        Exit;+    End;+    N := 0;+    Items := SplitSemi(Param(Lines, 'params'));+    For I := 0 To Items.Count - 1 Do+    Begin+        Spec := Items[I];+        P := Pos('=', Spec);+        If P > 0 Then+        Begin+            K := Trim(Copy(Spec, 1, P - 1));+            V := Trim(Copy(Spec, P + 1, Length(Spec)));+            SetSchParam(Comp, K, V);+            N := N + 1;+        End;+    End;+    Items.Free;+    If Cmt <> '' Then Comp.Comment.Text := Cmt;+    If Fp <> '' Then+    Begin+        Impl := Comp.AddSchImplementation;+        Impl.ClearAllDatafileLinks;+        Impl.ModelName := Fp;+        Impl.ModelType := 'PCBLIB';+        Impl.AddDataFileLink(Fp, '', 'PCBLIB');+        Impl.IsCurrent := True;+    End;+    SLib.GraphicallyInvalidate;+    Result := '{' + JRaw('ok', 'true') + ',' + JStr('component', Comp.LibReference) + ',' + JRaw('paramsSet', IntToStr(N)) + ',' + JStr('footprint', Fp) + '}';+End;++{ Add a 3D body to a footprint of the CURRENT PcbLib: name=<footprint>+  step=<path.step>  OR  extruded box: bw_mil,bh_mil,h_mil,so_mil }+{ ------------------------------------------------- diagnostics: pcb_report --- }++Function BodyJson(Prim : IPCB_Primitive) : String;+Var+    R : TCoordRect;+    HasModel : String;+Begin+    R := Prim.BoundingRectangle;+    HasModel := 'false';+    Try+        If Prim.Model <> Nil Then HasModel := 'true';+    Except+    End;+    Result := '[' + IntToStr(Prim.Layer) + ',' ++        IntToStr(Round(CoordToMils(R.Right - R.Left))) + ',' ++        IntToStr(Round(CoordToMils(R.Top - R.Bottom))) + ',' ++        IntToStr(Round(CoordToMils(R.Left))) + ',' ++        IntToStr(Round(CoordToMils(R.Bottom))) + ',' ++        IntToStr(Round(CoordToMils(Prim.StandoffHeight))) + ',' ++        IntToStr(Round(CoordToMils(Prim.OverallHeight))) + ',' ++        HasModel + ']';+End;++Function GroupBodies(Grp : IPCB_Group) : String;+Var+    GIt  : IPCB_GroupIterator;+    Prim : IPCB_Primitive;+    S    : String;+Begin+    S := '';+    GIt := Grp.GroupIterator_Create;+    GIt.AddFilter_ObjectSet(MkSet(eComponentBodyObject));+    GIt.AddFilter_LayerSet(AllLayers);+    Prim := GIt.FirstPCBObject;+    While Prim <> Nil Do+    Begin+        If S <> '' Then S := S + ',';+        S := S + BodyJson(Prim);+        Prim := GIt.NextPCBObject;+    End;+    Grp.GroupIterator_Destroy(GIt);+    Result := '[' + S + ']';+End;++Function GroupCensus(Grp : IPCB_Group) : String;+Var+    GIt  : IPCB_GroupIterator;+    Prim : IPCB_Primitive;+    N    : Integer;+    S    : String;+Begin+    N := 0;+    S := '';+    GIt := Grp.GroupIterator_Create;+    GIt.AddFilter_LayerSet(AllLayers);+    Prim := GIt.FirstPCBObject;+    While Prim <> Nil Do+    Begin+        N := N + 1;+        If S <> '' Then S := S + ';';+        S := S + IntToStr(Prim.ObjectId);+        Prim := GIt.NextPCBObject;+    End;+    Grp.GroupIterator_Destroy(GIt);+    Result := IntToStr(N) + ':' + S;+End;++Function GroupCount(Grp : IPCB_Group; Kind : Integer) : Integer;+Var+    GIt  : IPCB_GroupIterator;+    Prim : IPCB_Primitive;+    N    : Integer;+Begin+    N := 0;+    GIt := Grp.GroupIterator_Create;+    GIt.AddFilter_ObjectSet(MkSet(Kind));+    GIt.AddFilter_LayerSet(AllLayers);+    Prim := GIt.FirstPCBObject;+    While Prim <> Nil Do+    Begin+        N := N + 1;+        Prim := GIt.NextPCBObject;+    End;+    Grp.GroupIterator_Destroy(GIt);+    Result := N;+End;+++Function BoardBodyCount(Board : IPCB_Board) : Integer;+Var+    BIt  : IPCB_BoardIterator;+    Prim : IPCB_Primitive;+    N    : Integer;+Begin+    N := 0;+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eComponentBodyObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Prim := BIt.FirstPCBObject;+    While Prim <> Nil Do+    Begin+        N := N + 1;+        Prim := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+    Result := N;+End;++Function CmdAddBody(Lines : TStringList) : String;+Var+    PLib : IPCB_Library;+    Fp   : IPCB_LibComponent;+    Fp2  : IPCB_LibComponent;+    Body : IPCB_ComponentBody;+    Mdl  : IPCB_Model;+    Cont : IPCB_Contour;+    BIt  : IPCB_BoardIterator;+    Old  : IPCB_Primitive;+    Name, StepPath, Note, KeepS : String;+    I, BW, BH, HG, SO : Integer;+    NRep, NBefore, NSame : Integer;+    Found, Keep : Boolean;+Begin+    Name := Param(Lines, 'name');+    StepPath := Param(Lines, 'step');+    { keep=1 is the deliberate-second-body opt-out; anything else REPLACES the footprint's bodies }+    Keep := (Param(Lines, 'keep') = '1');+    If Keep Then KeepS := 'true' Else KeepS := 'false';+    PLib := PCBServer.GetCurrentPCBLibrary;+    If PLib = Nil Then+    Begin+        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'current document is not a PCB library') + '}';+        Exit;+    End;+    { An omitted name may only mean "the one footprint in here": adding a 3D body to whichever+      footprint happened to be first is a silent edit of the wrong part. }+    If (Name = '') And (PLib.ComponentCount > 1) Then+    Begin+        Result := '{' + JRaw('ok', 'false') + ',' + JStr('errorCode', 'name_required') + ',' ++            JStr('error', 'this library holds ' + IntToStr(PLib.ComponentCount) + ' footprints: name the one to add the body to') + '}';+        Exit;+    End;+    Found := False;+    For I := 0 To PLib.ComponentCount - 1 Do+    Begin+        Fp := PLib.GetComponent(I);+        If (Name = '') Or (UpperCase(Fp.Name) = UpperCase(Name)) Then+        Begin+            Found := True;+            Break;+        End;+    End;+    If Not Found Then+    Begin+        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'footprint not found: ' + Name) + '}';+        Exit;+    End;+    { footprints sharing this name: more than one means the body went to the FIRST of them only }+    NSame := 0;+    For I := 0 To PLib.ComponentCount - 1 Do+    Begin+        Fp2 := PLib.GetComponent(I);+        If UpperCase(Fp2.Name) = UpperCase(Fp.Name) Then NSame := NSame + 1;+    End;+    PCBServer.PreProcess;+    Body := PCBServer.PCBObjectFactory(eComponentBodyObject, eNoDimension, eCreate_Default);+    { A primitive without a layer is silently dropped on AddPCBObject (measured+      2026-08-30: add returned ok, census showed 16 pads and 0 bodies). Component+      bodies live on a mechanical layer. }+    Body.Layer := eMechanical1;+    { An invisible body is indistinguishable from a missing one: opacity defaults+      to 0 in this engine. Make sure the mech layer is enabled and the body opaque. }+    PLib.Board.LayerStack.LayerObject[eMechanical1].MechanicalLayerEnabled := True;+    PLib.Board.LayerIsDisplayed[eMechanical1] := True;+    Body.BodyOpacity3D := 1.0;+    If StepPath <> '' Then+    Begin+        Mdl := Body.ModelFactory_FromFilename(StepPath, False);+        If Mdl = Nil Then+        Begin+            PCBServer.PostProcess;+            Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'could not load model: ' + StepPath) + '}';+            Exit;+        End;+        Body.SetState_FromModel;+        Body.Model := Mdl;+        Note := 'step';+    End+    Else+    Begin+        BW := StrToIntDef(Param(Lines, 'bw_mil'), 100);+        BH := StrToIntDef(Param(Lines, 'bh_mil'), 100);+        HG := StrToIntDef(Param(Lines, 'h_mil'), 40);+        SO := StrToIntDef(Param(Lines, 'so_mil'), 0);+        Body.BodyProjection := eBoardSide_Top;+        Body.StandoffHeight := MilsToCoord(SO);+        Body.OverallHeight := MilsToCoord(SO + HG);+        Body.BodyColor3D := $00202020;+        Cont := PCBServer.PCBContourFactory;+        Cont.AddPoint(MilsToCoord(-(BW Div 2)), MilsToCoord(-(BH Div 2)));+        Cont.AddPoint(MilsToCoord((BW Div 2)), MilsToCoord(-(BH Div 2)));+        Cont.AddPoint(MilsToCoord((BW Div 2)), MilsToCoord((BH Div 2)));+        Cont.AddPoint(MilsToCoord(-(BW Div 2)), MilsToCoord((BH Div 2)));+        Body.SetOutlineContour(Cont);+        Note := 'extruded';+    End;+    { The stored component is NOT the editing surface: a direct Fp.AddPCBObject+      counted 1 body immediately but the editor's canvas copy overwrote it on+      save/reopen - libBodies read back 0 (measured 2026-08-30). Make Fp current+      FIRST, then add to the CANVAS (PLib.Board); the editor owns the canvas and+      syncs it into the stored component on save. }+    PLib.CurrentComponent := Fp;+    NBefore := GroupCount(Fp, eComponentBodyObject);+    { a part imported again REPLACES its 3D body, the way build_symbol_modes replaces its symbol:+      adding on top left the old model and the fixed one inside each other on the board+      (seen 2026-10-16 on AMPLH5030S). Only now that the new model has loaded, so a bad step+      path keeps the old body. }+    NRep := 0;+    If Not Keep Then+    Begin+        { delete them the way the editor does: RemovePCBObject on the canvas or on the footprint+          left them in place after save and reopen (measured 2026-10-20: the stored footprint kept+          every body). Select exactly the canvas's bodies, end this command, run Edit>Delete+          (PCB:Clear) on the selection, then start a new command for the add. Nothing else+          may be selected: Clear deletes the whole selection. }+        BIt := PLib.Board.BoardIterator_Create;+        BIt.AddFilter_LayerSet(AllLayers);+        BIt.AddFilter_Method(eProcessAll);+        Old := BIt.FirstPCBObject;+        While Old <> Nil Do+        Begin+            If Old.Selected Then Old.Selected := False;+            Old := BIt.NextPCBObject;+        End;+        PLib.Board.BoardIterator_Destroy(BIt);+        BIt := PLib.Board.BoardIterator_Create;+        BIt.AddFilter_ObjectSet(MkSet(eComponentBodyObject));+        BIt.AddFilter_LayerSet(AllLayers);+        BIt.AddFilter_Method(eProcessAll);+        Old := BIt.FirstPCBObject;+        While Old <> Nil Do+        Begin+            Old.Selected := True;+            NRep := NRep + 1;+            Old := BIt.NextPCBObject;+        End;+        PLib.Board.BoardIterator_Destroy(BIt);+        If NRep > 0 Then+        Begin+            PCBServer.PostProcess;+            ResetParameters;+            RunProcess('PCB:Clear');+            PCBServer.PreProcess;+        End;+    End;+    PLib.Board.AddPCBObject(Body);+    PCBServer.SendMessageToRobots(PLib.Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Body.I_ObjectAddress);+    Note := Note + ':canvas';+    PCBServer.PostProcess;+    PLib.Board.ViewManager_FullUpdate;+    PLib.RefreshView;+    Result := '{' + JRaw('ok', 'true') + ',' + JStr('footprint', Fp.Name) + ',' + JStr('body', Note) + ',' ++        JRaw('replacedBodies', IntToStr(NRep)) + ',' + JRaw('keptExisting', KeepS) + ',' ++        JRaw('bodiesBefore', IntToStr(NBefore)) + ',' + JRaw('sameNameFootprints', IntToStr(NSame)) + ',' ++        JRaw('bodiesNow', IntToStr(GroupCount(Fp, eComponentBodyObject))) + ',' ++        JRaw('bodiesOnCanvas', IntToStr(BoardBodyCount(PLib.Board))) + '}';+End;++{ --------------------------------------------------- demo: place on sch + pcb --- }++{ Create a new schematic sheet and place the CH340G symbol (Replicated from the+  currently-open SchLib) at the centre. sch=<save path> libref=<component> }+{ true/false for JSON }+Function BoolStr(B : Boolean) : String;+Begin+    If B Then Result := 'true' Else Result := 'false';+End;++{ Set (or add) a DOCUMENT parameter on a sheet: Title / DocumentNumber / Revision / DrawnBy feed the+  standard title block through its special strings. }+Procedure SetDocParam(Sheet : ISch_Document; PName : String; PVal : String);+Var+    It : ISch_Iterator;+    Pm, Existing : ISch_Parameter;+Begin+    Existing := Nil;+    It := Sheet.SchIterator_Create;+    It.AddFilter_ObjectSet(MkSet(eParameter));+    Pm := It.FirstSchObject;+    While Pm <> Nil Do+    Begin+        If Pm.Name = PName Then+        Begin+            Existing := Pm;+            Break;+        End;+        Pm := It.NextSchObject;+    End;+    Sheet.SchIterator_Destroy(It);+    If Existing <> Nil Then+        Existing.Text := PVal+    Else+    Begin+        Pm := SchServer.SchObjectFactory(eParameter, eCreate_Default);+        Pm.Name := PName;+        Pm.Text := PVal;+        Pm.IsHidden := True;+        Sheet.AddSchObject(Pm);+        SchServer.RobotManager.SendMessage(Sheet.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, Pm.I_ObjectAddress);+    End;+End;++{ Split on single spaces }+Function SplitSpace(S : String) : TStringList;+Var+    K : Integer;+    Part : String;+Begin+    Result := TStringList.Create;+    S := Trim(S);+    While S <> '' Do+    Begin+        K := Pos(' ', S);+        If K > 0 Then+        Begin+            Part := Copy(S, 1, K - 1);+            S := Trim(Copy(S, K + 1, Length(S)));+        End+        Else+        Begin+            Part := S;+            S := '';+        End;+        If Part <> '' Then Result.Add(Part);+    End;+End;++{ Draw the chip-outline artwork (chip-thumbnailer polylines, as baked into the KiCad symbol) into a+  SchLib component. Args: libref, kicadRect=x0,y0,x1,y1 (mils, the KiCad body rectangle). Payload:+  one polyline per line, 'x,y x,y ...' in mils of the KiCad frame. The KiCad body rectangle is mapped+  onto the component's own rectangle, so the outline lands between the pin-name columns exactly as+  it does in KiCad and Fusion (John, 2026-09-19). }+Function CmdAddSymbolArt(Lines : TStringList; Payload : String) : String;+Var+    SLib   : ISch_Lib;+    Iter   : ISch_Iterator;+    Comp   : ISch_Component;+    Rect   : ISch_Rectangle;+    Poly   : ISch_Polyline;+    Seg    : ISch_Line;+    Prim   : ISch_GraphicalObject;+    Victims : TInterfaceList;+    PL, Pts : TStringList;+    I, J, N, NPoly, NPts, NCleared : Integer;+    Name, KR, Ln, Tok, Mode : String;+    KX0, KY0, KX1, KY1, AX0, AY0, AX1, AY1, SX, SY, MX, MY, PX, PY, Tmp : Double;+    UseLines : Boolean;+Begin+    Name := Param(Lines, 'libref');+    KR := Param(Lines, 'kicadRect');+    Mode := Param(Lines, 'mode');+    { lines (default): every polyline becomes N-1 eLine segments, the primitive BuildSymbolModes has+      always drawn with; polyline: ISch_Polyline objects (measured 2026-09-19: they render on one+      part and stay invisible on another with identical geometry). }+    UseLines := (Mode <> 'polyline');+    SLib := SchServer.GetCurrentSchDocument;+    If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then+    Begin+        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'current document is not a schematic library') + '}';+        Exit;+    End;+    Comp := Nil;+    Iter := SLib.SchLibIterator_Create;+    Iter.AddFilter_ObjectSet(MkSet(eSchComponent));+    Comp := Iter.FirstSchObject;+    While Comp <> Nil Do+    Begin+        If UpperCase(Comp.LibReference) = UpperCase(Name) Then Break;+        Comp := Iter.NextSchObject;+    End;+    SLib.SchIterator_Destroy(Iter);+    If Comp = Nil Then+    Begin+        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'component not found: ' + Name) + '}';+        Exit;+    End;+    Rect := Nil;+    Iter := Comp.SchIterator_Create;+    Iter.AddFilter_ObjectSet(MkSet(eRectangle));+    Rect := Iter.FirstSchObject;+    If Rect <> Nil Then+    Begin+        AX0 := CoordToMils(Rect.Location.X); AY0 := CoordToMils(Rect.Location.Y);+        AX1 := CoordToMils(Rect.Corner.X); AY1 := CoordToMils(Rect.Corner.Y);+    End;+    Comp.SchIterator_Destroy(Iter);+    If Rect = Nil Then+    Begin+        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'component has no body rectangle to fit the artwork into') + '}';+        Exit;+    End;+    KX0 := AdomFloat(Field(KR, 1)); KY0 := AdomFloat(Field(KR, 2));+    KX1 := AdomFloat(Field(KR, 3)); KY1 := AdomFloat(Field(KR, 4));+    If KX1 < KX0 Then Begin Tmp := KX0; KX0 := KX1; KX1 := Tmp; End;+    If KY1 < KY0 Then Begin Tmp := KY0; KY0 := KY1; KY1 := Tmp; End;+    If AX1 < AX0 Then Begin Tmp := AX0; AX0 := AX1; AX1 := Tmp; End;+    If AY1 < AY0 Then Begin Tmp := AY0; AY0 := AY1; AY1 := Tmp; End;+    If (KX1 - KX0 < 1) Or (KY1 - KY0 < 1) Then+    Begin+        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'kicadRect is degenerate: ' + KR) + '}';+        Exit;+    End;+    SX := (AX1 - AX0) / (KX1 - KX0);+    SY := (AY1 - AY0) / (KY1 - KY0);+    SchServer.ProcessControl.PreProcess(SLib, '');+    { replace, never stack: every earlier polyline and every line that is not part of the pins+      or the body outline is art from a previous run. Collect first, remove after the walk. }+    NCleared := 0;+    If Param(Lines, 'clear') <> '0' Then+    Begin+        Victims := TInterfaceList.Create;+        Iter := Comp.SchIterator_Create;+        Iter.AddFilter_ObjectSet(MkSet(ePolyline, eLine));+        Prim := Iter.FirstSchObject;+        While Prim <> Nil Do+        Begin+            Victims.Add(Prim);+            Prim := Iter.NextSchObject;+        End;+        Comp.SchIterator_Destroy(Iter);+        For I := 0 To Victims.Count - 1 Do+        Begin+            Prim := Victims[I];+            Comp.RemoveSchObject(Prim);+            SchServer.DestroySchObject(Prim);+            NCleared := NCleared + 1;+        End;+        Victims.Free;+    End;+    PL := TStringList.Create;+    PL.Text := Payload;+    NPoly := 0; NPts := 0;+    For I := 0 To PL.Count - 1 Do+    Begin+        Ln := Trim(PL[I]);+        If Ln = '' Then Continue;+        Pts := SplitSpace(Ln);+        N := Pts.Count;+        If N >= 2 Then+        Begin+            If UseLines Then+            Begin+                PX := 0; PY := 0;+                For J := 1 To N Do+                Begin+                    Tok := Pts[J - 1];+                    MX := AX0 + (AdomFloat(Field(Tok, 1)) - KX0) * SX;+                    MY := AY0 + (AdomFloat(Field(Tok, 2)) - KY0) * SY;+                    If J > 1 Then+                    Begin+                        Seg := SchServer.SchObjectFactory(eLine, eCreate_Default);+                        Seg.OwnerPartId := 1;+                        Seg.OwnerPartDisplayMode := 0;+                        Seg.LineWidth := eSmall;+                        Seg.Color := $8A8A8A;+                        Seg.Location := Point(MilsToCoord(PX), MilsToCoord(PY));+                        Seg.Corner := Point(MilsToCoord(MX), MilsToCoord(MY));+                        Comp.AddSchObject(Seg);+                        SchServer.RobotManager.SendMessage(Comp.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, Seg.I_ObjectAddress);+                    End;+                    PX := MX; PY := MY;+                End;+            End+            Else+            Begin+                Poly := SchServer.SchObjectFactory(ePolyline, eCreate_Default);+                Poly.OwnerPartId := 1;+                Poly.OwnerPartDisplayMode := 0;+                Poly.LineWidth := eSmall;+                Poly.Color := $8A8A8A;+                Poly.VerticesCount := N;+                For J := 1 To N Do+                Begin+                    Tok := Pts[J - 1];+                    MX := AX0 + (AdomFloat(Field(Tok, 1)) - KX0) * SX;+                    MY := AY0 + (AdomFloat(Field(Tok, 2)) - KY0) * SY;+                    Poly.Vertex[J] := Point(MilsToCoord(MX), MilsToCoord(MY));+                End;+                Comp.AddSchObject(Poly);+                SchServer.RobotManager.SendMessage(Comp.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, Poly.I_ObjectAddress);+            End;+            NPoly := NPoly + 1;+            NPts := NPts + N;+        End;+        Pts.Free;+    End;+    SchServer.ProcessControl.PostProcess(SLib, '');+    MarkSchModified(SLib);+    PL.Free;+    SLib.GraphicallyInvalidate;+    Result := '{' + JRaw('ok', 'true') + ',' + JStr('libref', Comp.LibReference) + ',' + JRaw('polylines', IntToStr(NPoly)) + ',' ++        JRaw('points', IntToStr(NPts)) + ',' + JRaw('cleared', IntToStr(NCleared)) + ',' + JStr('mode', Mode) + ',' ++        JRaw('bodyRectMil', '[' + IntToStr(Round(AX0)) + ',' + IntToStr(Round(AY0)) + ',' + IntToStr(Round(AX1)) + ',' + IntToStr(Round(AY1)) + ']') + '}';+End;++{ Read a SchLib component drawing: primitive counts by kind, the body rectangle, the polyline+  bounding box and the footprint models. Args: libref }+{ Regression test for the script-error safety net: raise a REAL runtime error inside the agent's+  own code, on purpose. The bridge must photograph the Error box, stop the halted script itself+  and answer agent_script_error; a timeout here means the net is broken. Never call this except+  through altium_selftest_script_error. }+Function CmdSelfTestHalt(Lines : TStringList) : String;+Begin+    AdomDeliberateUndeclaredIdentifierForSelfTest(1);+    Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'the engine did not halt: this Altium build tolerated an undeclared identifier') + '}';+End;++Function CmdSymbolPrims(Lines : TStringList) : String;+Var+    AnyR0 : Boolean;+    SLib  : ISch_Lib;+    Iter  : ISch_Iterator;+    Comp  : ISch_Component;+    Prim  : ISch_GraphicalObject;+    Impl  : ISch_Implementation;+    Name, Models : String;+    NPin, NRect, NLine, NPoly, NOther, I : Integer;+    R : TCoordRect;+    PX0, PY0, PX1, PY1, RX0, RY0, RX1, RY1 : Integer;+    AnyP, AnyR : Boolean;+Begin+    Name := Param(Lines, 'libref');+    SLib := SchServer.GetCurrentSchDocument;+    If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then+    Begin+        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'current document is not a schematic library') + '}';+        Exit;+    End;+    Comp := Nil;+    Iter := SLib.SchLibIterator_Create;+    Iter.AddFilter_ObjectSet(MkSet(eSchComponent));+    Comp := Iter.FirstSchObject;+    While Comp <> Nil Do+    Begin+        If UpperCase(Comp.LibReference) = UpperCase(Name) Then Break;+        Comp := Iter.NextSchObject;+    End;+    SLib.SchIterator_Destroy(Iter);+    If Comp = Nil Then+    Begin+        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'component not found: ' + Name) + '}';+        Exit;+    End;+    NPin := 0; NRect := 0; NLine := 0; NPoly := 0; NOther := 0;+    AnyP := False; AnyR := False; AnyR0 := False;+    PX0 := 0; PY0 := 0; PX1 := 0; PY1 := 0; RX0 := 0; RY0 := 0; RX1 := 0; RY1 := 0;+    Iter := Comp.SchIterator_Create;+    Prim := Iter.FirstSchObject;+    While Prim <> Nil Do+    Begin+        If Prim.ObjectID = ePin Then NPin := NPin + 1+        Else If Prim.ObjectID = eRectangle Then+        Begin+            NRect := NRect + 1;+            { the body of the mode the symbol shows (the sheet draws mode 0): the union of that+              mode's rectangles (a capacitor's two plates are two). The first rectangle found+              could be any mode's: a 10-mode symbol answered a wider mode's body. }+            R := Prim.BoundingRectangle;+            If Prim.OwnerPartDisplayMode = Comp.DisplayMode Then+            Begin+                If Not AnyR0 Then+                Begin+                    RX0 := Round(CoordToMils(R.Left)); RY0 := Round(CoordToMils(R.Bottom));+                    RX1 := Round(CoordToMils(R.Right)); RY1 := Round(CoordToMils(R.Top));+                End+                Else+                Begin+                    If Round(CoordToMils(R.Left)) < RX0 Then RX0 := Round(CoordToMils(R.Left));+                    If Round(CoordToMils(R.Bottom)) < RY0 Then RY0 := Round(CoordToMils(R.Bottom));+                    If Round(CoordToMils(R.Right)) > RX1 Then RX1 := Round(CoordToMils(R.Right));+                    If Round(CoordToMils(R.Top)) > RY1 Then RY1 := Round(CoordToMils(R.Top));+                End;+                AnyR0 := True; AnyR := True;+            End+            Else If Not AnyR Then+            Begin+                RX0 := Round(CoordToMils(R.Left)); RY0 := Round(CoordToMils(R.Bottom));+                RX1 := Round(CoordToMils(R.Right)); RY1 := Round(CoordToMils(R.Top));+                AnyR := True;+            End;+        End+        Else If Prim.ObjectID = eLine Then NLine := NLine + 1+        Else If Prim.ObjectID = ePolyline Then+        Begin+            NPoly := NPoly + 1;+            R := Prim.BoundingRectangle;+            If Not AnyP Then+            Begin+                PX0 := Round(CoordToMils(R.Left)); PY0 := Round(CoordToMils(R.Bottom));+                PX1 := Round(CoordToMils(R.Right)); PY1 := Round(CoordToMils(R.Top));+                AnyP := True;+            End+            Else+            Begin+                If Round(CoordToMils(R.Left)) < PX0 Then PX0 := Round(CoordToMils(R.Left));+                If Round(CoordToMils(R.Bottom)) < PY0 Then PY0 := Round(CoordToMils(R.Bottom));+                If Round(CoordToMils(R.Right)) > PX1 Then PX1 := Round(CoordToMils(R.Right));+                If Round(CoordToMils(R.Top)) > PY1 Then PY1 := Round(CoordToMils(R.Top));+            End;+        End+        Else NOther := NOther + 1;+        Prim := Iter.NextSchObject;+    End;+    Comp.SchIterator_Destroy(Iter);+    Models := '';+    If Param(Lines, 'models') = '1' Then+    Begin+        { ISch_Component has no ImplementationCount/Implementation[] in DelphiScript on every AD+          build (26.9.1 halts the script there); the eImplementation child iterator works on all. }+        Iter := Comp.SchIterator_Create;+        Iter.AddFilter_ObjectSet(MkSet(eImplementation));+        Impl := Iter.FirstSchObject;+        While Impl <> Nil Do+        Begin+            If Models <> '' Then Models := Models + ',';+            Models := Models + '"' + Impl.ModelType + ':' + Impl.ModelName + '"';+            Impl := Iter.NextSchObject;+        End;+        Comp.SchIterator_Destroy(Iter);+    End;+    Result := '{' + JRaw('ok', 'true') + ',' + JStr('libref', Comp.LibReference) + ',' ++        JRaw('pins', IntToStr(NPin)) + ',' + JRaw('rectangles', IntToStr(NRect)) + ',' + JRaw('lines', IntToStr(NLine)) + ',' ++        JRaw('polylines', IntToStr(NPoly)) + ',' + JRaw('other', IntToStr(NOther)) + ',' ++        JRaw('displayMode', IntToStr(Comp.DisplayMode)) + ',' + JRaw('displayModeCount', IntToStr(Comp.DisplayModeCount)) + ',' ++        JRaw('currentPartId', IntToStr(Comp.CurrentPartID)) + ',' ++        JRaw('bodyRectMil', '[' + IntToStr(RX0) + ',' + IntToStr(RY0) + ',' + IntToStr(RX1) + ',' + IntToStr(RY1) + ']') + ',' ++        JRaw('polyBboxMil', '[' + IntToStr(PX0) + ',' + IntToStr(PY0) + ',' + IntToStr(PX1) + ',' + IntToStr(PY1) + ']') + ',' ++        JRaw('models', '[' + Models + ']') + '}';+End;++{ Replace every footprint model on a SchLib component with ONE PCBLIB model (set_params+  footprint APPENDS an implementation per call, which is how a symbol ends up with two).+  Args: libref, footprint }+Function CmdSetModels(Lines : TStringList) : String;+Var+    SLib  : ISch_Lib;+    Iter  : ISch_Iterator;+    Comp  : ISch_Component;+    Impl  : ISch_Implementation;+    Name, Fp : String;+    Removed : Integer;+Begin+    Name := Param(Lines, 'libref');+    Fp := Param(Lines, 'footprint');+    SLib := SchServer.GetCurrentSchDocument;+    If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then+    Begin+        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'current document is not a schematic library') + '}';+        Exit;+    End;+    Comp := Nil;+    Iter := SLib.SchLibIterator_Create;+    Iter.AddFilter_ObjectSet(MkSet(eSchComponent));+    Comp := Iter.FirstSchObject;+    While Comp <> Nil Do+    Begin+        If UpperCase(Comp.LibReference) = UpperCase(Name) Then Break;+        Comp := Iter.NextSchObject;+    End;+    SLib.SchIterator_Destroy(Iter);+    If Comp = Nil Then+    Begin+        Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'component not found: ' + Name) + '}';+        Exit;+    End;+    Removed := 0;+    { Same rule as CmdSymbolPrims: walk implementations with the eImplementation iterator. One+      iterator per removal, because removing under a live iterator invalidates it. }+    While Removed < 64 Do+    Begin+        Iter := Comp.SchIterator_Create;+        Iter.AddFilter_ObjectSet(MkSet(eImplementation));+        Impl := Iter.FirstSchObject;+        Comp.SchIterator_Destroy(Iter);+        If Impl = Nil Then Break;+        Comp.RemoveSchImplementation(Impl);+        Removed := Removed + 1;+    End;+    If Fp <> '' Then+    Begin+        Impl := Comp.AddSchImplementation;+        Impl.ClearAllDatafileLinks;+        Impl.ModelName := Fp;+        Impl.ModelType := 'PCBLIB';+        Impl.AddDataFileLink(Fp, '', 'PCBLIB');+        Impl.IsCurrent := True;+    End;+    SLib.GraphicallyInvalidate;+    Result := '{' + JRaw('ok', 'true') + ',' + JStr('libref', Comp.LibReference) + ',' + JRaw('removed', IntToStr(Removed)) + ',' + JStr('footprint', Fp) + '}';+End;++Function CmdPlaceOnSch(Lines : TStringList) : String;+Var+    SLib   : ISch_Lib;+    Iter   : ISch_Iterator;+    PIt    : ISch_Iterator;+    LibCmp : ISch_Component;+    Inst   : ISch_Component;+    Prim   : ISch_GraphicalObject;+    Cpy    : ISch_GraphicalObject;+    SD     : IServerDocument;+    Sheet  : ISch_Document;+    SchPath, LibRef, Missing, Placed, Title, DocNum, Rev, Drawn, Desc : String;+    NCopied, K, W, H, XK, YK, BCX, BCY, BH : Integer;+    PWs, CXs, CYs, TPs, BTs : TStringList;+    R : TCoordRect;+    TotalW, MaxH, Gap, Margin, GapA, MarginA, Cur, DX, DY, BBL, BBB, BBR, BBT, BTop, BBot : Integer;+    Append : Boolean;+    EIt    : ISch_Iterator;+    ECmp   : ISch_Component;+    NExist, ExtX, SumY, GW : Integer;+    Grown  : Boolean;+    RefList : TStringList;+    RIt    : ISch_Iterator;+    LRect  : ISch_Rectangle;+    Pm     : ISch_Parameter;+    TF     : ISch_TextFrame;+Begin+    SchPath := Param(Lines, 'sch');+    LibRef := Param(Lines, 'libref');+    Title := Param(Lines, 'title');+    DocNum := Param(Lines, 'number');+    Rev := Param(Lines, 'revision');+    Drawn := Param(Lines, 'drawnBy');+    Desc := Param(Lines, 'description');+    { libref may be a ';' list: every symbol lands on ONE new sheet as U1..Un, 5000 mil apart+      (John, 2026-09-19: three ToF sensors on one schematic for the demo). }+    RefList := SplitSemi(LibRef);+    SLib := SchServer.GetCurrentSchDocument;+    If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the SchLib first (it must be the current document)') + '}';+        Exit;+    End;+    Missing := '';+    For K := 0 To RefList.Count - 1 Do+    Begin+        LibCmp := Nil;+        Iter := SLib.SchLibIterator_Create;+        Iter.AddFilter_ObjectSet(MkSet(eSchComponent));+        LibCmp := Iter.FirstSchObject;+        While LibCmp <> Nil Do+        Begin+            If UpperCase(LibCmp.LibReference) = UpperCase(RefList[K]) Then Break;+            LibCmp := Iter.NextSchObject;+        End;+        SLib.SchIterator_Destroy(Iter);+        If LibCmp = Nil Then Missing := Missing + RefList[K] + ' ';+    End;+    If (RefList.Count = 0) Or (Missing <> '') Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','component not found in SchLib: ' + Trim(Missing + LibRef)) + '}';+        RefList.Free;+        Exit;+    End;+    { Append mode (issue 11): sch names an EXISTING .SchDoc -> open it and add the parts to+      the right of what is there; the sheet keeps its size and title block. }+    SD := Nil;+    Append := (SchPath <> '') And FileExists(SchPath);+    If (SchPath <> '') And (Not Append) Then+    Begin+        SD := Nil;+        Try+            SD := Client.GetDocumentByPath(SchPath);+        Except+            SD := Nil;+        End;+        If SD <> Nil Then Append := True;+    End;+    If Append Then+    Begin+        If SD = Nil Then SD := Client.OpenDocument('SCH', SchPath);+        If SD = Nil Then+        Begin+            Result := '{' + JRaw('ok','false') + ',' + JStr('error','could not open the existing schematic: ' + SchPath) + '}';+            RefList.Free;+            Exit;+        End;+        Client.ShowDocument(SD);+    End+    Else+    Begin+        SD := CreateNewDocumentFromDocumentKind('SCH');+        If SD = Nil Then+        Begin+            Result := '{' + JRaw('ok','false') + ',' + JStr('error','could not create a schematic sheet') + '}';+            RefList.Free;+            Exit;+        End;+        Client.ShowDocument(SD);+    End;+    Sheet := SchServer.GetCurrentSchDocument;+    If Sheet = Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current schematic after open') + '}';+        RefList.Free;+        Exit;+    End;+    { existing symbols: count (designators continue) and the rightmost anchor + height band }+    NExist := 0; ExtX := 0; SumY := 0; Grown := False;+    If Append Then+    Begin+        EIt := Sheet.SchIterator_Create;+        EIt.AddFilter_ObjectSet(MkSet(eSchComponent));+        ECmp := EIt.FirstSchObject;+        While ECmp <> Nil Do+        Begin+            NExist := NExist + 1;+            If (NExist = 1) Or (Round(CoordToMils(ECmp.Location.X)) > ExtX) Then ExtX := Round(CoordToMils(ECmp.Location.X));+            SumY := SumY + Round(CoordToMils(ECmp.Location.Y));+            ECmp := EIt.NextSchObject;+        End;+        Sheet.SchIterator_Destroy(EIt);+    End;+    { Pass 1: MEASURE every part (union of its primitives' bounding rectangles, pins+      included, +250 mil a side for the pin-number text that hangs outside the stub).+      John, 2026-09-19, reviewing three 5000 mil bodies squeezed onto one sheet:+      "you are smooshing these symbols together such that their pin names overlap; no+      real EE would ever do that, they'd space them out". So the sheet is sized FROM the+      parts, never the parts from the sheet. }+    PWs := TStringList.Create; CXs := TStringList.Create; CYs := TStringList.Create;+    TPs := TStringList.Create; BTs := TStringList.Create;+    TotalW := 0; MaxH := 0;+    For K := 0 To RefList.Count - 1 Do+    Begin+        LibCmp := Nil;+        Iter := SLib.SchLibIterator_Create;+        Iter.AddFilter_ObjectSet(MkSet(eSchComponent));+        LibCmp := Iter.FirstSchObject;+        While LibCmp <> Nil Do+        Begin+            If UpperCase(LibCmp.LibReference) = UpperCase(RefList[K]) Then Break;+            LibCmp := Iter.NextSchObject;+        End;+        SLib.SchIterator_Destroy(Iter);+        BBL := 0; BBB := 0; BBR := 0; BBT := 0; BTop := 0; BBot := 0; NCopied := 0;+        PIt := LibCmp.SchIterator_Create;+        Prim := PIt.FirstSchObject;+        While Prim <> Nil Do+        Begin+            If (Prim.ObjectID = ePin) Or (Prim.ObjectID = eRectangle) Or+               (Prim.ObjectID = eLine) Or (Prim.ObjectID = eArc) Or+               (Prim.ObjectID = eEllipse) Or (Prim.ObjectID = ePolygon) Or+               (Prim.ObjectID = ePolyline) Or (Prim.ObjectID = eLabel) Then+            Begin+                R := Prim.BoundingRectangle;+                If (NCopied = 0) Or (Round(CoordToMils(R.Left)) < BBL) Then BBL := Round(CoordToMils(R.Left));+                If (NCopied = 0) Or (Round(CoordToMils(R.Bottom)) < BBB) Then BBB := Round(CoordToMils(R.Bottom));+                If (NCopied = 0) Or (Round(CoordToMils(R.Right)) > BBR) Then BBR := Round(CoordToMils(R.Right));+                If (NCopied = 0) Or (Round(CoordToMils(R.Top)) > BBT) Then BBT := Round(CoordToMils(R.Top));+                If Prim.ObjectID = eRectangle Then+                Begin+                    BTop := Round(CoordToMils(R.Top)); BBot := Round(CoordToMils(R.Bottom));+                End;+                NCopied := NCopied + 1;+            End;+            Prim := PIt.NextSchObject;+        End;+        LibCmp.SchIterator_Destroy(PIt);+        If BTop = 0 Then Begin BTop := BBT; BBot := BBB; End;+        PWs.Add(IntToStr((BBR - BBL) + 500));+        CXs.Add(IntToStr((BBL + BBR) Div 2));+        CYs.Add(IntToStr((BBB + BBT) Div 2));+        TPs.Add(IntToStr(BTop)); BTs.Add(IntToStr(BBot));+        TotalW := TotalW + (BBR - BBL) + 500;+        If (BBT - BBB) > MaxH Then MaxH := BBT - BBB;+    End;+    { Sheet from the parts: 1000 mil margins, at least 1500 mil of air between parts, and+      room above (1700 mil description strip) and below (title block) the row. Sheet.SheetStyle+      does NOT take on ADDevelop (measured 2026-09-19: the sheet stayed the 9500 x 7500 mil+      default), so the sheet is a CUSTOM size, never smaller than A4 (11500 x 7600). }+    { John, 2026-09-19 (after the 1500 mil cut): "put those symbols a bit closer together so+      we get a better screenshot" - 500 mil of air between parts, 600 mil margins, and the+      sheet hugs the row so a zoom-all fills the frame. }+    Gap := 500; Margin := 600;+    W := TotalW + Gap * (RefList.Count - 1) + 2 * Margin;+    If W < 11500 Then W := 11500;+    H := MaxH + 3600;+    If H < 7600 Then H := 7600;+    If H * 2 < W Then H := W Div 2;+    If Not Append Then+    Begin+    Sheet.UseCustomSheet := True;+    Sheet.CustomX := MilsToCoord(W);+    Sheet.CustomY := MilsToCoord(H);+    Sheet.CustomXZones := (W + 2499) Div 2500;+    Sheet.CustomYZones := (H + 2499) Div 2500;+    Sheet.CustomMarginWidth := MilsToCoord(200);+    End;+    { Do NOT read SheetSizeX/Y back here: on ADDevelop they still report the 9500 x 7500 mil+      default after the custom size is set (measured 2026-09-19: the read-back shrank W to+      9500 and squeezed three 5800 mil parts into it, pin numbers on top of each other,+      while the sheet itself drew at the custom size). W and H ARE the sheet. }+    { spread the slack evenly: the row is centred and every gap grows by the same amount }+    GapA := Gap; MarginA := Margin;+    If W > TotalW + Gap * (RefList.Count - 1) + 2 * Margin Then+    Begin+        GapA := Gap + (W - (TotalW + Gap * (RefList.Count - 1) + 2 * Margin)) Div (RefList.Count + 1);+        MarginA := (W - TotalW - GapA * (RefList.Count - 1)) Div 2;+    End;+    Placed := '';+    NCopied := 0;+    Cur := MarginA;+    If Append Then Cur := ExtX + 4000;+    For K := 0 To RefList.Count - 1 Do+    Begin+    XK := Cur + AdomInt(PWs[K]) Div 2;+    Cur := Cur + AdomInt(PWs[K]) + GapA;+    { the row sits a little below centre: the description strip takes the top 1700 mil;+      appending, it sits in line with the existing symbols instead }+    YK := (H Div 2) - 300;+    If Append Then+    Begin+        If NExist > 0 Then YK := SumY Div NExist Else YK := 3500;+    End;+    DX := XK - AdomInt(CXs[K]);+    DY := YK - AdomInt(CYs[K]);+    LibCmp := Nil;+    Iter := SLib.SchLibIterator_Create;+    Iter.AddFilter_ObjectSet(MkSet(eSchComponent));+    LibCmp := Iter.FirstSchObject;+    While LibCmp <> Nil Do+    Begin+        If UpperCase(LibCmp.LibReference) = UpperCase(RefList[K]) Then Break;+        LibCmp := Iter.NextSchObject;+    End;+    SLib.SchIterator_Destroy(Iter);+    { Build a fresh component instance and copy the library part's primitives into it.+      ISch_Component.Replicate does NOT re-parent the pins/rectangle onto a normal+      sheet (measured 2026-08-30: only the designator/comment text placed), so copy+      each child primitive with primitive-level Replicate + AddSchObject. }+    Inst := SchServer.SchObjectFactory(eSchComponent, eCreate_Default);+    Inst.CurrentPartID := 1;+    Inst.DisplayMode := 0;+    Inst.LibReference := LibCmp.LibReference;+    Inst.ComponentDescription := LibCmp.ComponentDescription;+    Inst.Location := Point(MilsToCoord(XK), MilsToCoord(YK));+    { body rectangle centre (mil, library frame) for the designator / comment columns }+    BCX := AdomInt(CXs[K]); BCY := AdomInt(CYs[K]);+    RIt := LibCmp.SchIterator_Create;+    RIt.AddFilter_ObjectSet(MkSet(eRectangle));+    LRect := RIt.FirstSchObject;+    If LRect <> Nil Then+        BCX := Round(CoordToMils(LRect.Location.X + LRect.Corner.X) / 2);+    LibCmp.SchIterator_Destroy(RIt);+    PIt := LibCmp.SchIterator_Create;+    Prim := PIt.FirstSchObject;+    While Prim <> Nil Do+    Begin+        If (Prim.ObjectID = ePin) Or (Prim.ObjectID = eRectangle) Or+           (Prim.ObjectID = eLine) Or (Prim.ObjectID = eArc) Or+           (Prim.ObjectID = eEllipse) Or (Prim.ObjectID = ePolygon) Or+           (Prim.ObjectID = ePolyline) Or (Prim.ObjectID = eLabel) Then+        Begin+            Cpy := Prim.Replicate;+            Cpy.OwnerPartId := 1;+            Cpy.OwnerPartDisplayMode := 0;+            { copied primitives keep the library coordinates, Inst.Location does not carry them+              (measured 2026-09-19: three parts drew on top of each other), so shift each child }+            Cpy.MoveByXY(MilsToCoord(DX), MilsToCoord(DY));+            Inst.AddSchObject(Cpy);+            NCopied := NCopied + 1;+        End;+        Prim := PIt.NextSchObject;+    End;+    LibCmp.SchIterator_Destroy(PIt);+    { Designator centred ABOVE the body and the comment centred BELOW it, whatever the+      library part did with them (measured 2026-09-19: +200 mil put U1 on top of the chip+      outline inside a 3700 mil body). }+    Inst.Designator.Text := 'U' + IntToStr(NExist + K + 1);+    Inst.Designator.IsHidden := False;+    Inst.Designator.Justification := eJustify_BottomCenter;+    Inst.Designator.Location := Point(MilsToCoord(BCX + DX), MilsToCoord(AdomInt(TPs[K]) + DY + 100));+    { never leave the star placeholder: the comment is the part number }+    Inst.Comment.Text := LibCmp.LibReference;+    Inst.Comment.IsHidden := False;+    Inst.Comment.Justification := eJustify_TopCenter;+    Inst.Comment.Location := Point(MilsToCoord(BCX + DX), MilsToCoord(AdomInt(BTs[K]) + DY - 100));+    Sheet.RegisterSchObjectInContainer(Inst);+    SchServer.RobotManager.SendMessage(Sheet.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, Inst.I_ObjectAddress);+    If Placed <> '' Then Placed := Placed + ';';+    Placed := Placed + RefList[K];+    End;+    { Appended parts extend the row to the right; grow the sheet so they stay inside the+      drawing frame (a fixed sheet left the appended part out in the gray, measured+      2026-10-15). Never shrinks a sheet. }+    If Append Then+    Begin+        GW := Cur + 600;+        If Sheet.UseCustomSheet Then+        Begin+            If GW > Round(CoordToMils(Sheet.CustomX)) Then+            Begin+                Sheet.CustomX := MilsToCoord(GW);+                Sheet.CustomXZones := (GW + 2499) Div 2500;+                Grown := True;+            End;+        End+        Else+        Begin+            Sheet.UseCustomSheet := True;+            Sheet.CustomX := MilsToCoord(GW);+            Sheet.CustomY := MilsToCoord(H);+            Sheet.CustomXZones := (GW + 2499) Div 2500;+            Sheet.CustomYZones := (H + 2499) Div 2500;+            Sheet.CustomMarginWidth := MilsToCoord(200);+            Grown := True;+        End;+        If Grown Then W := GW;+    End;+    { Title block: the standard sheet renders the document parameters Title, DocumentNumber,+      Revision and DrawnBy through its special strings; an empty block is a demo defect. }+    If Append Then+    Begin+        Title := ''; DocNum := ''; Rev := ''; Drawn := ''; Desc := '';+    End;+    If Title <> '' Then SetDocParam(Sheet, 'Title', Title);+    If DocNum <> '' Then SetDocParam(Sheet, 'DocumentNumber', DocNum);+    If Rev <> '' Then SetDocParam(Sheet, 'Revision', Rev);+    If Drawn <> '' Then SetDocParam(Sheet, 'DrawnBy', Drawn);+    If Desc <> '' Then+    Begin+        SetDocParam(Sheet, 'Description', Desc);+        TF := SchServer.SchObjectFactory(eTextFrame, eCreate_Default);+        { inside the drawing frame: the border + zone strip take ~400 mil, so the frame+          runs from 700 mil below the top edge down 1000 mil, and stops 700 mil short of+          the right edge }+        TF.Location := Point(MilsToCoord(1000), MilsToCoord(H - 1700));+        TF.Corner := Point(MilsToCoord(W - 700), MilsToCoord(H - 700));+        TF.Text := Desc;+        TF.WordWrap := True;+        TF.ShowBorder := False;+        Sheet.RegisterSchObjectInContainer(TF);+        SchServer.RobotManager.SendMessage(Sheet.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, TF.I_ObjectAddress);+    End;+    Sheet.GraphicallyInvalidate;+    ResetParameters; AddStringParameter('Action','All'); RunProcess('Sch:Zoom');+    If SchPath <> '' Then+    Begin+        If Not Append Then Begin FreePath(SchPath); SD.SetFileName(SchPath); End;+        SD.DoFileSave('');+    End;+    PWs.Free; CXs.Free; CYs.Free; TPs.Free; BTs.Free;+    Result := '{' + JRaw('ok','true') + ',' + JStr('sch', SchPath) + ',' + JRaw('sheetMil', '[' + IntToStr(W) + ',' + IntToStr(H) + ']') + ',' ++        JRaw('titleBlock', '{' + JRaw('title', BoolStr(Title <> '')) + ',' + JRaw('number', BoolStr(DocNum <> '')) + ',' + JRaw('revision', BoolStr(Rev <> '')) + ',' + JRaw('drawnBy', BoolStr(Drawn <> '')) + ',' + JRaw('description', BoolStr(Desc <> '')) + '}') + ',' ++        JStr('placed', Placed) + ',' + JStr('designator','U' + IntToStr(NExist + 1)) + ',' + JRaw('appended', BoolStr(Append)) + ',' + JRaw('sheetGrown', BoolStr(Grown)) + ',' + JRaw('existingParts', IntToStr(NExist)) + ',' + JRaw('count', IntToStr(RefList.Count)) + ',' + JRaw('primitives', IntToStr(NCopied)) + '}';+    RefList.Free;+End;++Function CmdPlaceOnPcb(Lines : TStringList) : String;+Var+    PLib   : IPCB_Library;+    LibFp  : IPCB_LibComponent;+    Inst   : IPCB_Component;+    Board  : IPCB_Board;+    SD     : IServerDocument;+    GIt    : IPCB_GroupIterator;+    Prim   : IPCB_Primitive;+    Cpy    : IPCB_Primitive;+    PcbPath, FpName : String;+    I, NCopied : Integer;+    Found  : Boolean;+    R      : TCoordRect;+    DX, DY, CL, CB, CR, CT : Integer;+    AnyP   : Boolean;+Begin+    PcbPath := Param(Lines, 'pcb');+    FpName := Param(Lines, 'footprint');+    PLib := PCBServer.GetCurrentPCBLibrary;+    If PLib = Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the PcbLib first (it must be the current document)') + '}';+        Exit;+    End;+    Found := False;+    For I := 0 To PLib.ComponentCount - 1 Do+    Begin+        LibFp := PLib.GetComponent(I);+        If UpperCase(LibFp.Name) = UpperCase(FpName) Then Begin Found := True; Break; End;+    End;+    If Not Found Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','footprint not found in PcbLib: ' + FpName) + '}';+        Exit;+    End;+    { ALWAYS create a fresh PcbDoc: with a PcbLib focused, GetCurrentPCBBoard+      returns the LIBRARY internal board - a reuse path put the part into the lib+      canvas and renamed the lib doc, measured 2026-08-30. }+    SD := CreateNewDocumentFromDocumentKind('PCB');+    If SD = Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','could not create a PCB document') + '}';+        Exit;+    End;+    Client.ShowDocument(SD);+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board') + '}';+        Exit;+    End;+    Board.LayerStack.LayerObject[eMechanical1].MechanicalLayerEnabled := True;+    Board.LayerIsDisplayed[eMechanical1] := True;+    WriteAtomic(WORKSPACE + '\stage.txt', 'board_ready');+    { Group Replicate of an IPCB_LibComponent DISSOLVES on a board: census showed+      16 free pads, no eComponentObject, no body - measured 2026-08-30. Build a+      fresh board component and copy each child primitive, the same cure the+      schematic placement needed. }+    PCBServer.PreProcess;+    Inst := PCBServer.PCBObjectFactory(eComponentObject, eNoDimension, eCreate_Default);+    Inst.Layer := eTopLayer;+    { The factory component's auto Designator1/Comment text is enormous and+      hijacks Zoom All - hide both. }+    Inst.NameOn := False;+    Inst.CommentOn := False;+    { ORDER MATTERS: fill the group BEFORE the board add. Children added to an+      already-registered component exist in data but never draw, whatever address+      they are robot-registered to (measured 2026-08-30). This mirrors+      create_footprint, where pads go in before RegisterComponent. }+    NCopied := 0;+    GIt := LibFp.GroupIterator_Create;+    GIt.AddFilter_LayerSet(AllLayers);+    Prim := GIt.FirstPCBObject;+    While Prim <> Nil Do+    Begin+        Cpy := Prim.Replicate;+        Inst.AddPCBObject(Cpy);+        NCopied := NCopied + 1;+        Prim := GIt.NextPCBObject;+    End;+    LibFp.GroupIterator_Destroy(GIt);+    Board.AddPCBObject(Inst);+    PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Inst.I_ObjectAddress);+    WriteAtomic(WORKSPACE + '\stage.txt', 'component_registered');+    WriteAtomic(WORKSPACE + '\stage.txt', 'children_copied');+    { Copied children keep their LIBRARY coordinates (lib origin ~50000,50000 mil);+      anchor the fresh component onto them BEFORE the move so MoveToXY translates+      the whole group to the target (bbox was 50146 mil wide without this). }+    { Anchor the component BEFORE normalizing children: assigning Inst.x/y+      afterwards re-translates the whole group by the same delta (measured+      2026-08-30: everything landed at 8000,6000 instead of 4000,3000). }+    Inst.x := MilsToCoord(4000);+    Inst.y := MilsToCoord(3000);+    { Normalize positions: the lib stores pads around the canvas origin+      (~50000,50000 mil) but an authored body contour around 0,0 - one group move+      cannot land both (measured 2026-08-30: body at 4000, pads at 54000). Move+      the pad cluster and each body to the target independently. }+    AnyP := False;+    CL := 0; CB := 0; CR := 0; CT := 0;+    GIt := Inst.GroupIterator_Create;+    GIt.AddFilter_ObjectSet(MkSet(ePadObject));+    GIt.AddFilter_LayerSet(AllLayers);+    Prim := GIt.FirstPCBObject;+    While Prim <> Nil Do+    Begin+        R := Prim.BoundingRectangle;+        If Not AnyP Then+        Begin+            CL := R.Left; CB := R.Bottom; CR := R.Right; CT := R.Top; AnyP := True;+        End+        Else+        Begin+            If R.Left < CL Then CL := R.Left;+            If R.Bottom < CB Then CB := R.Bottom;+            If R.Right > CR Then CR := R.Right;+            If R.Top > CT Then CT := R.Top;+        End;+        Prim := GIt.NextPCBObject;+    End;+    Inst.GroupIterator_Destroy(GIt);+    DX := 0; DY := 0;+    If AnyP Then+    Begin+        DX := MilsToCoord(4000) - ((CL + CR) Div 2);+        DY := MilsToCoord(3000) - ((CB + CT) Div 2);+    End;+    GIt := Inst.GroupIterator_Create;+    GIt.AddFilter_LayerSet(AllLayers);+    Prim := GIt.FirstPCBObject;+    While Prim <> Nil Do+    Begin+        If Prim.ObjectId = eComponentBodyObject Then+        Begin+            R := Prim.BoundingRectangle;+            Prim.MoveByXY(MilsToCoord(4000) - ((R.Left + R.Right) Div 2),+                          MilsToCoord(3000) - ((R.Bottom + R.Top) Div 2));+        End+        Else If Prim.ObjectId <> eTextObject Then+            Prim.MoveByXY(DX, DY);+        Prim := GIt.NextPCBObject;+    End;+    Inst.GroupIterator_Destroy(GIt);+    { Hidden auto Name/Comment texts otherwise bloat the bbox by ~50000 mil. }+    Inst.Name.MoveToXY(MilsToCoord(4000), MilsToCoord(3250));+    Inst.Comment.MoveToXY(MilsToCoord(4000), MilsToCoord(2750));+    PCBServer.PostProcess;+    WriteAtomic(WORKSPACE + '\stage.txt', 'moved');+    Board.ViewManager_FullUpdate;+    ResetParameters; AddStringParameter('Action','All'); RunProcess('PCB:Zoom');+    If PcbPath <> '' Then+    Begin+        FreePath(PcbPath);+        SD.SetFileName(PcbPath);+        SD.DoFileSave('');+    End;+    WriteAtomic(WORKSPACE + '\stage.txt', 'saved');+    Result := '{' + JRaw('ok','true') + ',' + JStr('pcb', PcbPath) + ',' + JStr('placed', FpName) + ',' ++        JStr('libFile', PLib.Board.FileName) + ',' ++        JRaw('libBodies', IntToStr(GroupCount(LibFp, eComponentBodyObject))) + ',' ++        JRaw('primitives', IntToStr(NCopied)) + ',' ++        JRaw('padsOnComp', IntToStr(GroupCount(Inst, ePadObject))) + ',' ++        JRaw('bodiesOnComp', IntToStr(GroupCount(Inst, eComponentBodyObject))) + '}';+End;++Function BoardCensus(Board : IPCB_Board) : String;+Var+    BIt  : IPCB_BoardIterator;+    Prim : IPCB_Primitive;+    N    : Integer;+    S    : String;+Begin+    N := 0;+    S := '';+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(AllObjects);+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Prim := BIt.FirstPCBObject;+    While (Prim <> Nil) And (N < 200) Do+    Begin+        N := N + 1;+        If S <> '' Then S := S + ';';+        S := S + IntToStr(Prim.ObjectId);+        Prim := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+    Result := IntToStr(N) + ':' + S;+End;+++Function GroupPadBbox(Grp : IPCB_Group) : String;+Var+    GIt  : IPCB_GroupIterator;+    Prim : IPCB_Primitive;+    R    : TCoordRect;+    L, B, Rt, T : Integer;+    Any  : Boolean;+Begin+    Any := False;+    L := 0; B := 0; Rt := 0; T := 0;+    GIt := Grp.GroupIterator_Create;+    GIt.AddFilter_ObjectSet(MkSet(ePadObject));+    GIt.AddFilter_LayerSet(AllLayers);+    Prim := GIt.FirstPCBObject;+    While Prim <> Nil Do+    Begin+        R := Prim.BoundingRectangle;+        If Not Any Then+        Begin+            L := R.Left; B := R.Bottom; Rt := R.Right; T := R.Top; Any := True;+        End+        Else+        Begin+            If R.Left < L Then L := R.Left;+            If R.Bottom < B Then B := R.Bottom;+            If R.Right > Rt Then Rt := R.Right;+            If R.Top > T Then T := R.Top;+        End;+        Prim := GIt.NextPCBObject;+    End;+    Grp.GroupIterator_Destroy(GIt);+    If Not Any Then+    Begin+        Result := '[]';+        Exit;+    End;+    Result := '[' + IntToStr(Round(CoordToMils(L))) + ',' + IntToStr(Round(CoordToMils(B))) + ',' ++        IntToStr(Round(CoordToMils(Rt))) + ',' + IntToStr(Round(CoordToMils(T))) + ']';+End;+++{ Net names on the pads that matter for the swap proof: "des=net;..." for pads+  1 and 49 (every pad named 49 listed), plus the track count and their nets. }+Function CompNets(Comp : IPCB_Component) : String;+Var+    PadIt : IPCB_GroupIterator;+    Pad : IPCB_Pad;+    S, N : String;+Begin+    S := '';+    PadIt := Comp.GroupIterator_Create;+    PadIt.AddFilter_ObjectSet(MkSet(ePadObject));+    PadIt.AddFilter_LayerSet(AllLayers);+    Pad := PadIt.FirstPCBObject;+    While Pad <> Nil Do+    Begin+        If (Pad.Name = '1') Or (Pad.Name = '49') Then+        Begin+            N := '';+            If Pad.Net <> Nil Then N := Pad.Net.Name;+            If S <> '' Then S := S + ';';+            S := S + Pad.Name + '=' + N;+        End;+        Pad := PadIt.NextPCBObject;+    End;+    Comp.GroupIterator_Destroy(PadIt);+    Result := S;+End;++Function BoardTracks(Board : IPCB_Board) : String;+Var+    BIt : IPCB_BoardIterator;+    T   : IPCB_Track;+    S, N : String;+Begin+    S := '';+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eTrackObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    T := BIt.FirstPCBObject;+    While T <> Nil Do+    Begin+        N := '';+        If T.Net <> Nil Then N := T.Net.Name;+        If S <> '' Then S := S + ';';+        S := S + N + '@' + IntToStr(Round(CoordToMils(T.X1))) + ',' + IntToStr(Round(CoordToMils(T.Y1)));+        T := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+    Result := S;+End;++{ The board outline's extent in mil as '[left,bottom,right,top]', read from the outline's own+  vertices. BoardOutline.BoundingRectangle is a cached rectangle that does not follow a reshape+  in the same session on AD 26.9.1 or Altium Develop (it kept the 6000 x 4000 mil default while+  the view showed the new shape, measured 2026-10-15); the vertices are what the view draws. }+Function OutlineMilFromVertices(BO : IPCB_BoardOutline) : String;+Var+    Seg : TPolySegment;+    K, L, B, R, T : Integer;+Begin+    L := 0; B := 0; R := 0; T := 0;+    For K := 0 To BO.PointCount - 1 Do+    Begin+        Seg := BO.Segments[K];+        If (K = 0) Or (Seg.vx < L) Then L := Seg.vx;+        If (K = 0) Or (Seg.vy < B) Then B := Seg.vy;+        If (K = 0) Or (Seg.vx > R) Then R := Seg.vx;+        If (K = 0) Or (Seg.vy > T) Then T := Seg.vy;+    End;+    Result := '[' + IntToStr(Round(CoordToMils(L))) + ',' + IntToStr(Round(CoordToMils(B))) + ',' ++        IntToStr(Round(CoordToMils(R))) + ',' + IntToStr(Round(CoordToMils(T))) + ']';+End;++Function CmdPcbReport(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    PLib  : IPCB_Library;+    Fp    : IPCB_LibComponent;+    Comp  : IPCB_Component;+    BIt   : IPCB_BoardIterator;+    R     : TCoordRect;+    Name, S, OM : String;+    I     : Integer;+Begin+    PLib := PCBServer.GetCurrentPCBLibrary;+    Name := Param(Lines, 'name');+    { GetCurrentPCBLibrary answers even when a BOARD is focused; want=board skips+      the library branch so the report describes the focused board. }+    If Param(Lines, 'want') = 'board' Then PLib := Nil;+    If PLib <> Nil Then+    Begin+        S := '';+        For I := 0 To PLib.ComponentCount - 1 Do+        Begin+            Fp := PLib.GetComponent(I);+            If (Name = '') Or (UpperCase(Fp.Name) = UpperCase(Name)) Then+            Begin+                If S <> '' Then S := S + ',';+                S := S + '{' + JStr('name', Fp.Name) + ',' ++                    JRaw('pads', IntToStr(GroupCount(Fp, ePadObject))) + ',' ++                    JRaw('padsBboxMil', GroupPadBbox(Fp)) + ',' ++                    JStr('census', GroupCensus(Fp)) + ',' ++                    JRaw('bodies', GroupBodies(Fp)) + '}';+            End;+        End;+        Result := '{' + JRaw('ok','true') + ',' + JStr('kind','library') + ',' ++            JRaw('components', '[' + S + ']') + ',' ++            JStr('bodyCols','layerId,wMil,hMil,leftMil,bottomMil,standoffMil,overallMil,hasModel') + '}';+        Exit;+    End;+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','current document is neither a PCB library nor a board') + '}';+        Exit;+    End;+    S := '';+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eComponentObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Comp := BIt.FirstPCBObject;+    While Comp <> Nil Do+    Begin+        If S <> '' Then S := S + ',';+        R := Comp.BoundingRectangle;+        S := S + '{' + JStr('pattern', Comp.Pattern) + ',' ++            JRaw('xMil', IntToStr(Round(CoordToMils(Comp.x)))) + ',' ++            JRaw('yMil', IntToStr(Round(CoordToMils(Comp.y)))) + ',' ++            JRaw('wMil', IntToStr(Round(CoordToMils(R.Right - R.Left)))) + ',' ++            JRaw('pads', IntToStr(GroupCount(Comp, ePadObject))) + ',' ++            JRaw('padsBboxMil', GroupPadBbox(Comp)) + ',' ++            JStr('nets', CompNets(Comp)) + ',' ++            JRaw('bodies', GroupBodies(Comp)) + '}';+        Comp := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+    { A library's internal board has no outline; touching BoundingRectangle on the+      nil BoardOutline halts the engine (measured 2026-08-30). }+    If Board.BoardOutline <> Nil Then+        OM := OutlineMilFromVertices(Board.BoardOutline)+    Else+    Begin+        R := Board.BoundingRectangle;+        OM := '[' + IntToStr(Round(CoordToMils(R.Left))) + ',' + IntToStr(Round(CoordToMils(R.Bottom))) + ',' ++             IntToStr(Round(CoordToMils(R.Right))) + ',' + IntToStr(Round(CoordToMils(R.Top))) + ']';+    End;+    Result := '{' + JRaw('ok','true') + ',' + JStr('kind','board') + ',' ++        JRaw('outlineMil', OM) + ',' ++        JRaw('components', '[' + S + ']') + ',' ++        JStr('census', BoardCensus(Board)) + ',' ++        JStr('tracks', BoardTracks(Board)) + ',' ++        JStr('bodyCols','layerId,wMil,hMil,leftMil,bottomMil,standoffMil,overallMil,hasModel') + '}';+End;++{ ------------------------------------------------ build_footprint (spec file) --- }++{ Pipe-separated field N (1-based) of S. }+Function FieldP(S : String; N : Integer) : String;+Var+    I, Cnt, Start : Integer;+Begin+    Cnt := 1; Start := 1; Result := '';+    For I := 1 To Length(S) Do+    Begin+        If S[I] = '|' Then+        Begin+            If Cnt = N Then Begin Result := Copy(S, Start, I - Start); Exit; End;+            Cnt := Cnt + 1; Start := I + 1;+        End;+    End;+    If Cnt = N Then Result := Copy(S, Start, Length(S) - Start + 1);+End;++Function MmC(S : String) : Integer;+Begin+    Result := MMsToCoord(AdomFloat(S));+End;++Function SpecLayer(Tag : String) : Integer;+Begin+    If Tag = 'overlay' Then Result := eTopOverlay+    Else If Tag = 'fab' Then Result := eMechanical13+    Else If Tag = 'courtyard' Then Result := eMechanical15+    Else If Tag = 'balls' Then Result := eMechanical9+    Else If Tag = 'comment' Then Result := eMechanical1+    Else If Tag = 'multi' Then Result := eMultiLayer+    Else Result := eTopLayer;+End;++{ Build a footprint from a spec file (tools/kicad2altium.py output):+  META|name|desc  PAD|des|x|y|w|h|shape|hole|layer|crpct  LINE|layer|x1|y1|x2|y2|w+  ARC|layer|cx|cy|r|w|a0|a1  DISC|layer|cx|cy|r.  Units mm, Y up. Args:+  libraryPath, spec=<windows path>. Pads with a repeated designator are legal in+  Altium and all map to that pin (the heatsink-via convention). }+Function CmdBuildFootprint(Lines : TStringList) : String;+Var+    PLib  : IPCB_Library;+    Comp  : IPCB_LibComponent;+    Pad   : IPCB_Pad;+    Trk   : IPCB_Track;+    Arc   : IPCB_Arc;+    Spec  : TStringList;+    I, NPads, NTh, NPrims : Integer;+    L, Kind, Name, Desc, LibState, LibPath, SpecPath, Shape : String;+    R : Integer;+Begin+    LibPath := Param(Lines, 'libraryPath');+    SpecPath := Param(Lines, 'spec');+    If (LibPath = '') Or (SpecPath = '') Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','libraryPath and spec are required') + '}';+        Exit;+    End;+    LibState := EnsureLibrary('PCBLIB', LibPath);+    PLib := PCBServer.GetCurrentPCBLibrary;+    If PLib = Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','no PCB library is current (' + LibState + ')') + '}';+        Exit;+    End;+    Spec := TStringList.Create;+    Spec.LoadFromFile(SpecPath);+    Name := ''; Desc := '';+    NPads := 0; NTh := 0; NPrims := 0;+    { mech layers this spec may use: enable + name them once per library }+    PLib.Board.LayerStack.LayerObject[eMechanical1].MechanicalLayerEnabled := True;+    PLib.Board.LayerStack.LayerObject[eMechanical9].MechanicalLayerEnabled := True;+    PLib.Board.LayerStack.LayerObject[eMechanical9].Name := 'Adom.SolderBalls';+    PLib.Board.LayerStack.LayerObject[eMechanical13].MechanicalLayerEnabled := True;+    PLib.Board.LayerStack.LayerObject[eMechanical13].Name := 'Fab';+    PLib.Board.LayerStack.LayerObject[eMechanical15].MechanicalLayerEnabled := True;+    PLib.Board.LayerStack.LayerObject[eMechanical15].Name := 'Courtyard';+    PLib.Board.LayerIsDisplayed[eMechanical9] := False;+    PLib.Board.LayerIsDisplayed[eMechanical13] := True;+    PLib.Board.LayerIsDisplayed[eMechanical15] := True;+    PCBServer.PreProcess;+    Comp := PCBServer.CreatePCBLibComp;+    For I := 0 To Spec.Count - 1 Do+    Begin+        L := Spec[I];+        Kind := FieldP(L, 1);+        If Kind = 'META' Then+        Begin+            Name := FieldP(L, 2); Desc := FieldP(L, 3);+            Comp.Name := Name; Comp.Description := Desc;+        End+        Else If Kind = 'PAD' Then+        Begin+            Pad := PCBServer.PCBObjectFactory(ePadObject, eNoDimension, eCreate_Default);+            Pad.Name := FieldP(L, 2);+            Pad.X := MmC(FieldP(L, 3));+            Pad.Y := MmC(FieldP(L, 4));+            Pad.TopXSize := MmC(FieldP(L, 5));+            Pad.TopYSize := MmC(FieldP(L, 6));+            Shape := FieldP(L, 7);+            If Shape = 'rect' Then Pad.TopShape := eRectangular+            Else If Shape = 'roundrect' Then Pad.TopShape := eRoundedRectangular+            Else Pad.TopShape := eRounded;+            Pad.HoleSize := MmC(FieldP(L, 8));+            If FieldP(L, 9) = 'multi' Then+            Begin+                Pad.Layer := eMultiLayer;+                NTh := NTh + 1;+            End+            Else Pad.Layer := eTopLayer;+            R := StrToIntDef(FieldP(L, 10), 0);+            If (Shape = 'roundrect') And (R > 0) Then+                Pad.SetState_StackCRPctOnLayer(eTopLayer, R);+            Comp.AddPCBObject(Pad);+            NPads := NPads + 1;+        End+        Else If Kind = 'LINE' Then+        Begin+            Trk := PCBServer.PCBObjectFactory(eTrackObject, eNoDimension, eCreate_Default);+            Trk.Layer := SpecLayer(FieldP(L, 2));+            Trk.X1 := MmC(FieldP(L, 3)); Trk.Y1 := MmC(FieldP(L, 4));+            Trk.X2 := MmC(FieldP(L, 5)); Trk.Y2 := MmC(FieldP(L, 6));+            Trk.Width := MmC(FieldP(L, 7));+            Comp.AddPCBObject(Trk);+            NPrims := NPrims + 1;+        End+        Else If Kind = 'ARC' Then+        Begin+            Arc := PCBServer.PCBObjectFactory(eArcObject, eNoDimension, eCreate_Default);+            Arc.Layer := SpecLayer(FieldP(L, 2));+            Arc.XCenter := MmC(FieldP(L, 3)); Arc.YCenter := MmC(FieldP(L, 4));+            Arc.Radius := MmC(FieldP(L, 5));+            Arc.LineWidth := MmC(FieldP(L, 6));+            Arc.StartAngle := AdomFloat(FieldP(L, 7)); Arc.EndAngle := AdomFloat(FieldP(L, 8));+            Comp.AddPCBObject(Arc);+            NPrims := NPrims + 1;+        End+        Else If Kind = 'DISC' Then+        Begin+            { a filled disc of radius r = a full arc at r/2 with stroke r }+            Arc := PCBServer.PCBObjectFactory(eArcObject, eNoDimension, eCreate_Default);+            Arc.Layer := SpecLayer(FieldP(L, 2));+            Arc.XCenter := MmC(FieldP(L, 3)); Arc.YCenter := MmC(FieldP(L, 4));+            Arc.Radius := MmC(FieldP(L, 5)) Div 2;+            Arc.LineWidth := MmC(FieldP(L, 5));+            Arc.StartAngle := 0; Arc.EndAngle := 360;+            Comp.AddPCBObject(Arc);+            NPrims := NPrims + 1;+        End;+    End;+    Spec.Free;+    PLib.RegisterComponent(Comp);+    PLib.CurrentComponent := Comp;+    PCBServer.PostProcess;+    PLib.Board.ViewManager_FullUpdate;+    PLib.RefreshView;+    { no PCB:Zoom here: an interactive process left 'a command active' and the+      next save drew Altium's save-a-copy Warning (measured 2026-08-30). }+    Result := '{' + JRaw('ok', 'true') + ',' + JStr('footprint', Name) + ',' + JStr('description', Desc) + ',' ++        JStr('library', LibPath) + ',' + JStr('libraryState', LibState) + ',' ++        JRaw('pads', IntToStr(NPads)) + ',' + JRaw('throughHolePads', IntToStr(NTh)) + ',' ++        JRaw('drawingPrimitives', IntToStr(NPrims)) + '}';+End;+++{ ------------------------------------------- build_symbol_modes (spec file) --- }++{ ONE component with N display modes (the Altium alternate-symbol mechanism:+  Normal + Alternate 1..N-1). Spec (tools/symbol_modes.py), mils, Y up:+  META|libref|desc|designator  MODE|i|name  RECT|mode|x1|y1|x2|y2+  PIN|mode|number|name|x|y|orient|len  LINE|mode|x1|y1|x2|y2+  Args: libraryPath, spec=<windows path>. Every primitive carries+  OwnerPartId 1 and OwnerPartDisplayMode = its mode. }+Function CmdBuildSymbolModes(Lines : TStringList) : String;+Var+    EType : String;+    SLib  : ISch_Lib;+    Comp  : ISch_Component;+    Rect  : ISch_Rectangle;+    Pin   : ISch_Pin;+    Ln    : ISch_Line;+    Ar    : ISch_Arc;+    Spec  : TStringList;+    Old   : ISch_Component;+    OIt   : ISch_Iterator;+    NOld  : Integer;+    I, NModes, NPins, NLines, Md : Integer;+    L, Kind, LibRef, Desc, Des, LibPath, SpecPath, LibState, Orient : String;+Begin+    LibPath := Param(Lines, 'libraryPath');+    SpecPath := Param(Lines, 'spec');+    If (LibPath = '') Or (SpecPath = '') Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','libraryPath and spec are required') + '}';+        Exit;+    End;+    LibState := EnsureLibrary('SCHLIB', LibPath);+    SLib := SchServer.GetCurrentSchDocument;+    If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','no schematic library is current (' + LibState + ')') + '}';+        Exit;+    End;+    Spec := TStringList.Create;+    Spec.LoadFromFile(SpecPath);+    NModes := 0; NPins := 0; NLines := 0;+    LibRef := ''; Desc := ''; Des := 'U';+    Comp := SchServer.SchObjectFactory(eSchComponent, eCreate_Default);+    Comp.CurrentPartID := 1;+    Comp.DisplayMode := 0;+    For I := 0 To Spec.Count - 1 Do+    Begin+        L := Spec[I];+        Kind := FieldP(L, 1);+        If Kind = 'META' Then+        Begin+            LibRef := FieldP(L, 2); Desc := FieldP(L, 3); Des := FieldP(L, 4);+            Comp.LibReference := LibRef;+            Comp.ComponentDescription := Desc;+            Comp.Designator.Text := Des + '?';+            Comp.Comment.Text := LibRef;+        End+        Else If Kind = 'MODE' Then+        Begin+            Md := AdomInt(FieldP(L, 2));+            If Md + 1 > NModes Then NModes := Md + 1;+        End+        Else If Kind = 'RECT' Then+        Begin+            Md := AdomInt(FieldP(L, 2));+            Rect := SchServer.SchObjectFactory(eRectangle, eCreate_Default);+            Rect.OwnerPartId := 1;+            Rect.OwnerPartDisplayMode := Md;+            Rect.Location := Point(MilsToCoord(AdomInt(FieldP(L, 3))), MilsToCoord(AdomInt(FieldP(L, 4))));+            Rect.Corner := Point(MilsToCoord(AdomInt(FieldP(L, 5))), MilsToCoord(AdomInt(FieldP(L, 6))));+            Rect.IsSolid := True;+            Comp.AddSchObject(Rect);+        End+        Else If Kind = 'PIN' Then+        Begin+            Md := AdomInt(FieldP(L, 2));+            Pin := SchServer.SchObjectFactory(ePin, eCreate_Default);+            Pin.OwnerPartId := 1;+            Pin.OwnerPartDisplayMode := Md;+            Pin.Designator := FieldP(L, 3);+            Pin.Name := FieldP(L, 4);+            Pin.PinLength := MilsToCoord(AdomInt(FieldP(L, 8)));+            Orient := FieldP(L, 7);+            { orientation = the direction the pin points AWAY from the body }+            If Orient = 'left' Then Pin.Orientation := eRotate180+            Else If Orient = 'right' Then Pin.Orientation := eRotate0+            Else If Orient = 'up' Then Pin.Orientation := eRotate90+            Else Pin.Orientation := eRotate270;+            Pin.Location := Point(MilsToCoord(AdomInt(FieldP(L, 5))), MilsToCoord(AdomInt(FieldP(L, 6))));+            { the KiCad pin type (field 10): until 0.2.52 every pin was Passive, so Altium's ERC+              and AI Flow's pin-type check could not see an output, an input or a power pin }+            EType := FieldP(L, 10);+            If EType = 'input' Then Pin.Electrical := eElectricInput+            Else If EType = 'output' Then Pin.Electrical := eElectricOutput+            Else If EType = 'bidirectional' Then Pin.Electrical := eElectricIO+            Else If EType = 'tri_state' Then Pin.Electrical := eElectricHiZ+            Else If EType = 'open_collector' Then Pin.Electrical := eElectricOpenCollector+            Else If EType = 'open_emitter' Then Pin.Electrical := eElectricOpenEmitter+            Else If (EType = 'power_in') Or (EType = 'power_out') Then Pin.Electrical := eElectricPower+            Else Pin.Electrical := eElectricPassive;+            { a small part's pins carry no visible name or number, as a library resistor's do }+            If FieldP(L, 9) = 'hide' Then+            Begin+                Pin.ShowName := False;+                Pin.ShowDesignator := False;+            End;+            Comp.AddSchObject(Pin);+            NPins := NPins + 1;+        End+        Else If Kind = 'ARC' Then+        Begin+            { ARC|mode|cx|cy|r|startDeg|endDeg, counterclockwise }+            Md := AdomInt(FieldP(L, 2));+            Ar := SchServer.SchObjectFactory(eArc, eCreate_Default);+            Ar.OwnerPartId := 1;+            Ar.OwnerPartDisplayMode := Md;+            Ar.Location := Point(MilsToCoord(AdomInt(FieldP(L, 3))), MilsToCoord(AdomInt(FieldP(L, 4))));+            Ar.Radius := MilsToCoord(AdomInt(FieldP(L, 5)));+            Ar.StartAngle := AdomInt(FieldP(L, 6));+            Ar.EndAngle := AdomInt(FieldP(L, 7));+            Comp.AddSchObject(Ar);+            NLines := NLines + 1;+        End+        Else If Kind = 'LINE' Then+        Begin+            Md := AdomInt(FieldP(L, 2));+            Ln := SchServer.SchObjectFactory(eLine, eCreate_Default);+            Ln.OwnerPartId := 1;+            Ln.OwnerPartDisplayMode := Md;+            Ln.Location := Point(MilsToCoord(AdomInt(FieldP(L, 3))), MilsToCoord(AdomInt(FieldP(L, 4))));+            Ln.Corner := Point(MilsToCoord(AdomInt(FieldP(L, 5))), MilsToCoord(AdomInt(FieldP(L, 6))));+            Comp.AddSchObject(Ln);+            NLines := NLines + 1;+        End;+    End;+    Spec.Free;+    Comp.DisplayModeCount := NModes;+    { a part imported again REPLACES its symbol: a second component with the same LibReference+      left the old one first in the library, so every reader kept drawing the old pins+      (measured 2026-10-15: a fixed 2-pin capacitor still placed with 1 pin) }+    NOld := 0;+    Repeat+        Old := Nil;+        OIt := SLib.SchLibIterator_Create;+        OIt.AddFilter_ObjectSet(MkSet(eSchComponent));+        Old := OIt.FirstSchObject;+        While Old <> Nil Do+        Begin+            If (Old <> Comp) And (UpperCase(Old.LibReference) = UpperCase(LibRef)) Then Break;+            Old := OIt.NextSchObject;+        End;+        SLib.SchIterator_Destroy(OIt);+        If Old <> Nil Then Begin SLib.RemoveSchComponent(Old); NOld := NOld + 1; End;+    Until (Old = Nil) Or (NOld > 20);+    SLib.AddSchComponent(Comp);+    SchServer.RobotManager.SendMessage(SLib.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, Comp.I_ObjectAddress);+    SLib.CurrentSchComponent := Comp;+    SLib.GraphicallyInvalidate;+    Result := '{' + JRaw('ok', 'true') + ',' + JStr('libref', LibRef) + ',' + JStr('library', LibPath) + ',' + JRaw('replaced', IntToStr(NOld)) + ',' ++        JStr('libraryState', LibState) + ',' + JRaw('modes', IntToStr(NModes)) + ',' ++        JRaw('pins', IntToStr(NPins)) + ',' + JRaw('lines', IntToStr(NLines)) + ',' ++        JRaw('displayModeCount', IntToStr(Comp.DisplayModeCount)) + '}';+End;++{ Switch the display mode of the current library component (for frames). mode=<int> }+Function CmdSymbolMode(Lines : TStringList) : String;+Var+    SLib : ISch_Lib;+    Comp : ISch_Component;+    Md   : Integer;+Begin+    SLib := SchServer.GetCurrentSchDocument;+    If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','no schematic library is current') + '}';+        Exit;+    End;+    Comp := SLib.CurrentSchComponent;+    If Comp = Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current component') + '}';+        Exit;+    End;+    Md := StrToIntDef(Param(Lines, 'mode'), 0);+    Comp.DisplayMode := Md;+    SLib.GraphicallyInvalidate;+    Result := '{' + JRaw('ok','true') + ',' + JRaw('mode', IntToStr(Comp.DisplayMode)) + ',' ++        JRaw('displayModeCount', IntToStr(Comp.DisplayModeCount)) + ',' + JStr('libref', Comp.LibReference) + '}';+End;+++{ ------------------------------------------------------------- add_models --- }++{ Attach PCB footprint models (name|pcblib|description;...) and parameters+  (k=v;...) to a SchLib component; set comment + description. Altium keeps+  several footprints on one component and the user picks the current one on+  the placed part (Properties > Footprint) - the CHANGE PACKAGE equivalent. }+Function CmdAddModels(Lines : TStringList) : String;+Var+    SLib   : ISch_Lib;+    Iter   : ISch_Iterator;+    Cmp    : ISch_Component;+    Impl   : ISch_Implementation;+    Models, Params, Name, Item, K, V, Cmt, Desc : String;+    ML, PL : TStringList;+    I, NModels, NParams : Integer;+Begin+    Name := Param(Lines, 'name');+    Models := Param(Lines, 'models');+    Params := Param(Lines, 'params');+    Cmt := Param(Lines, 'comment');+    Desc := Param(Lines, 'description');+    SLib := SchServer.GetCurrentSchDocument;+    If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the SchLib first') + '}';+        Exit;+    End;+    Cmp := Nil;+    Iter := SLib.SchLibIterator_Create;+    Iter.AddFilter_ObjectSet(MkSet(eSchComponent));+    Cmp := Iter.FirstSchObject;+    While Cmp <> Nil Do+    Begin+        If UpperCase(Cmp.LibReference) = UpperCase(Name) Then Break;+        Cmp := Iter.NextSchObject;+    End;+    SLib.SchIterator_Destroy(Iter);+    If Cmp = Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','component not found: ' + Name) + '}';+        Exit;+    End;+    SchServer.ProcessControl.PreProcess(SLib, '');+    If Cmt <> '' Then Cmp.Comment.Text := Cmt;+    If Desc <> '' Then Cmp.ComponentDescription := Desc;+    NModels := 0;+    ML := SplitSemi(Models);+    For I := 0 To ML.Count - 1 Do+    Begin+        Item := ML[I];+        If FieldP(Item, 1) = '' Then Continue;+        Impl := Cmp.AddSchImplementation;+        Impl.ModelName := FieldP(Item, 1);+        Impl.ModelType := 'PCBLIB';+        Impl.Description := FieldP(Item, 3);+        Impl.IsCurrent := (NModels = 0);+        If FieldP(Item, 2) <> '' Then Impl.AddDataFileLink(FieldP(Item, 1), FieldP(Item, 2), 'PCBLIB');+        NModels := NModels + 1;+    End;+    ML.Free;+    NParams := 0;+    PL := SplitSemi(Params);+    For I := 0 To PL.Count - 1 Do+    Begin+        Item := PL[I];+        If Pos('=', Item) = 0 Then Continue;+        K := Copy(Item, 1, Pos('=', Item) - 1);+        V := Copy(Item, Pos('=', Item) + 1, Length(Item));+        SetSchParam(Cmp, K, V);+        NParams := NParams + 1;+    End;+    PL.Free;+    SchServer.ProcessControl.PostProcess(SLib, '');+    MarkSchModified(SLib);+    SLib.GraphicallyInvalidate;+    Result := '{' + JRaw('ok','true') + ',' + JStr('libref', Cmp.LibReference) + ',' ++        JRaw('models', IntToStr(NModels)) + ',' + JRaw('params', IntToStr(NParams)) + '}';+End;+++{ ------------------------------------------------------ plate_vias --- }++{ Every drilled pad in the current PcbLib (all footprints, or name=<filter>) becomes a+  plated MULTI-LAYER pad. adom-lbr writes heatsink vias as pads with a hole, and Altium+  loaded them on the Top Layer (John, 2026-09-20: "did Altium support bringing in those+  heatsink vias?" - the holes came, the layer did not). Saving the library afterwards+  also yields a file whose bytes show the encoding Altium wants. }+Function CmdPlateVias(Lines : TStringList) : String;+Var+    PLib : IPCB_Library;+    Fp   : IPCB_LibComponent;+    GIt  : IPCB_GroupIterator;+    Pad  : IPCB_Pad;+    I, NFixed, NHoles : Integer;+    Name, S : String;+Begin+    PLib := PCBServer.GetCurrentPCBLibrary;+    Name := Param(Lines, 'name');+    If PLib = Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','no PCB library is current') + '}';+        Exit;+    End;+    NFixed := 0; NHoles := 0; S := '';+    PCBServer.PreProcess;+    For I := 0 To PLib.ComponentCount - 1 Do+    Begin+        Fp := PLib.GetComponent(I);+        If (Name <> '') And (UpperCase(Fp.Name) <> UpperCase(Name)) Then Continue;+        GIt := Fp.GroupIterator_Create;+        GIt.AddFilter_ObjectSet(MkSet(ePadObject));+        GIt.AddFilter_LayerSet(AllLayers);+        Pad := GIt.FirstPCBObject;+        While Pad <> Nil Do+        Begin+            If Pad.HoleSize > 0 Then+            Begin+                NHoles := NHoles + 1;+                If Pad.Layer <> eMultiLayer Then+                Begin+                    Pad.BeginModify;+                    Pad.Layer := eMultiLayer;+                    Pad.Plated := True;+                    Pad.EndModify;+                    NFixed := NFixed + 1;+                    If S <> '' Then S := S + ';';+                    S := S + Fp.Name + ':' + Pad.Name;+                End;+            End;+            Pad := GIt.NextPCBObject;+        End;+        Fp.GroupIterator_Destroy(GIt);+    End;+    PCBServer.PostProcess;+    PLib.Board.ViewManager_FullUpdate;+    PLib.RefreshView;+    Result := '{' + JRaw('ok','true') + ',' + JRaw('holes', IntToStr(NHoles)) + ',' + JRaw('plated', IntToStr(NFixed)) + ',' + JStr('fixed', S) + '}';+End;+++{ ------------------------------------------------------ normalize_bodies --- }++{ The PcbLib canvas origin sits 50000 mil from the stored component origin, so a+  body added through the canvas lands 50000 mil away from the pads in the stored+  frame (measured: body left -50138 vs pads +/-157) - Zoom All then frames 50+  inches of nothing and the editor paints an empty canvas. Align every body+  centre onto the pad-cluster centre of its footprint. name=<optional filter> }+Function CmdNormalizeBodies(Lines : TStringList) : String;+Var+    PLib : IPCB_Library;+    Fp   : IPCB_LibComponent;+    GIt  : IPCB_GroupIterator;+    Prim : IPCB_Primitive;+    R    : TCoordRect;+    I, NMoved : Integer;+    PL, PB, PR, PT, DX, DY : Integer;+    AnyP : Boolean;+    Name, S : String;+Begin+    PLib := PCBServer.GetCurrentPCBLibrary;+    Name := Param(Lines, 'name');+    If PLib = Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','no PCB library is current') + '}';+        Exit;+    End;+    NMoved := 0; S := '';+    PCBServer.PreProcess;+    For I := 0 To PLib.ComponentCount - 1 Do+    Begin+        Fp := PLib.GetComponent(I);+        If (Name <> '') And (UpperCase(Fp.Name) <> UpperCase(Name)) Then Continue;+        AnyP := False; PL := 0; PB := 0; PR := 0; PT := 0;+        GIt := Fp.GroupIterator_Create;+        GIt.AddFilter_ObjectSet(MkSet(ePadObject));+        GIt.AddFilter_LayerSet(AllLayers);+        Prim := GIt.FirstPCBObject;+        While Prim <> Nil Do+        Begin+            R := Prim.BoundingRectangle;+            If Not AnyP Then Begin PL := R.Left; PB := R.Bottom; PR := R.Right; PT := R.Top; AnyP := True; End+            Else+            Begin+                If R.Left < PL Then PL := R.Left;+                If R.Bottom < PB Then PB := R.Bottom;+                If R.Right > PR Then PR := R.Right;+                If R.Top > PT Then PT := R.Top;+            End;+            Prim := GIt.NextPCBObject;+        End;+        Fp.GroupIterator_Destroy(GIt);+        If Not AnyP Then Continue;+        GIt := Fp.GroupIterator_Create;+        GIt.AddFilter_ObjectSet(MkSet(eComponentBodyObject));+        GIt.AddFilter_LayerSet(AllLayers);+        Prim := GIt.FirstPCBObject;+        While Prim <> Nil Do+        Begin+            R := Prim.BoundingRectangle;+            DX := ((PL + PR) Div 2) - ((R.Left + R.Right) Div 2);+            DY := ((PB + PT) Div 2) - ((R.Bottom + R.Top) Div 2);+            If (Abs(DX) > MilsToCoord(5)) Or (Abs(DY) > MilsToCoord(5)) Then+            Begin+                Prim.MoveByXY(DX, DY);+                NMoved := NMoved + 1;+                If S <> '' Then S := S + ';';+                S := S + Fp.Name + ':' + IntToStr(Round(CoordToMils(DX))) + ',' + IntToStr(Round(CoordToMils(DY)));+            End;+            Prim := GIt.NextPCBObject;+        End;+        Fp.GroupIterator_Destroy(GIt);+    End;+    PCBServer.PostProcess;+    PLib.Board.ViewManager_FullUpdate;+    PLib.RefreshView;+    Result := '{' + JRaw('ok','true') + ',' + JRaw('moved', IntToStr(NMoved)) + ',' + JStr('shifts', S) + '}';+End;+++{ ------------------------------------------ test_board / swap_footprint --- }++{ Copy a library footprint onto Board as a fresh component centred at (CX,CY)+  coords, per-primitive (group Replicate dissolves), children BEFORE the board+  add, pad cluster and bodies normalised to the target independently. }+Function PlaceCopy(Board : IPCB_Board; LibFp : IPCB_LibComponent; CX, CY : Integer) : IPCB_Component;+Var+    Inst : IPCB_Component;+    GIt  : IPCB_GroupIterator;+    Prim, Cpy : IPCB_Primitive;+    R    : TCoordRect;+    DX, DY, CL, CB, CR, CT : Integer;+    AnyP : Boolean;+Begin+    Inst := PCBServer.PCBObjectFactory(eComponentObject, eNoDimension, eCreate_Default);+    Inst.Layer := eTopLayer;+    Inst.NameOn := False;+    Inst.CommentOn := False;+    GIt := LibFp.GroupIterator_Create;+    GIt.AddFilter_LayerSet(AllLayers);+    Prim := GIt.FirstPCBObject;+    While Prim <> Nil Do+    Begin+        Cpy := Prim.Replicate;+        Inst.AddPCBObject(Cpy);+        Prim := GIt.NextPCBObject;+    End;+    LibFp.GroupIterator_Destroy(GIt);+    Board.AddPCBObject(Inst);+    PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Inst.I_ObjectAddress);+    Inst.x := CX; Inst.y := CY;+    AnyP := False; CL := 0; CB := 0; CR := 0; CT := 0;+    GIt := Inst.GroupIterator_Create;+    GIt.AddFilter_ObjectSet(MkSet(ePadObject));+    GIt.AddFilter_LayerSet(AllLayers);+    Prim := GIt.FirstPCBObject;+    While Prim <> Nil Do+    Begin+        R := Prim.BoundingRectangle;+        If Not AnyP Then Begin CL := R.Left; CB := R.Bottom; CR := R.Right; CT := R.Top; AnyP := True; End+        Else+        Begin+            If R.Left < CL Then CL := R.Left;+            If R.Bottom < CB Then CB := R.Bottom;+            If R.Right > CR Then CR := R.Right;+            If R.Top > CT Then CT := R.Top;+        End;+        Prim := GIt.NextPCBObject;+    End;+    Inst.GroupIterator_Destroy(GIt);+    DX := 0; DY := 0;+    If AnyP Then Begin DX := CX - ((CL + CR) Div 2); DY := CY - ((CB + CT) Div 2); End;+    GIt := Inst.GroupIterator_Create;+    GIt.AddFilter_LayerSet(AllLayers);+    Prim := GIt.FirstPCBObject;+    While Prim <> Nil Do+    Begin+        If Prim.ObjectId = eComponentBodyObject Then+        Begin+            R := Prim.BoundingRectangle;+            Prim.MoveByXY(CX - ((R.Left + R.Right) Div 2), CY - ((R.Bottom + R.Top) Div 2));+        End+        Else If Prim.ObjectId <> eTextObject Then+            Prim.MoveByXY(DX, DY);+        Prim := GIt.NextPCBObject;+    End;+    Inst.GroupIterator_Destroy(GIt);+    Inst.Name.MoveToXY(CX, CY + MilsToCoord(250));+    Inst.Comment.MoveToXY(CX, CY - MilsToCoord(250));+    Result := Inst;+End;++Function FindNet(Board : IPCB_Board; NetName : String) : IPCB_Net;+Var+    BIt : IPCB_BoardIterator;+    N   : IPCB_Net;+Begin+    Result := Nil;+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eNetObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    N := BIt.FirstPCBObject;+    While N <> Nil Do+    Begin+        If N.Name = NetName Then Begin Result := N; Break; End;+        N := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+End;++{ Assign nets to pads by designator: assigns=<des>=<net>;... (all pads sharing a+  designator get the net - the heatsink-via convention). }+Procedure AssignPadNets(Board : IPCB_Board; Inst : IPCB_Component; Assigns : String);+Var+    GIt  : IPCB_GroupIterator;+    Pad  : IPCB_Pad;+    AL   : TStringList;+    I    : Integer;+    Des, NetName : String;+    Net  : IPCB_Net;+Begin+    AL := SplitSemi(Assigns);+    For I := 0 To AL.Count - 1 Do+    Begin+        If Pos('=', AL[I]) = 0 Then Continue;+        Des := Copy(AL[I], 1, Pos('=', AL[I]) - 1);+        NetName := Copy(AL[I], Pos('=', AL[I]) + 1, Length(AL[I]));+        Net := FindNet(Board, NetName);+        If Net = Nil Then+        Begin+            { a net that is only added (not registered) lives in memory: the save drops it and+              Altium builds no ratsnest for it (measured 2026-10-15: reopened board, 0 nets) }+            Net := PCBServer.PCBObjectFactory(eNetObject, eNoDimension, eCreate_Default);+            Net.Name := NetName;+            Board.AddPCBObject(Net);+            PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Net.I_ObjectAddress);+        End;+        GIt := Inst.GroupIterator_Create;+        GIt.AddFilter_ObjectSet(MkSet(ePadObject));+        GIt.AddFilter_LayerSet(AllLayers);+        Pad := GIt.FirstPCBObject;+        While Pad <> Nil Do+        Begin+            If Pad.Name = Des Then+            Begin+                Pad.BeginModify;+                Pad.Net := Net;+                Pad.EndModify;+                Net.AddPCBObject(Pad);+            End;+            Pad := GIt.NextPCBObject;+        End;+        Inst.GroupIterator_Destroy(GIt);+    End;+    AL.Free;+End;++{ New PcbDoc: FR4 rectangle wMm x hMm (origin at 0,0), the footprint placed at+  the centre, pad 1 and pad 49 given nets with a stub track each.+  Args: footprint, wMm, hMm, pcb=<save path>, assigns=<des=net;...>, stubs=<des=x,y;...> (mm from centre) }+Function CmdTestBoard(Lines : TStringList) : String;+Var+    PLib  : IPCB_Library;+    LibFp : IPCB_LibComponent;+    Board : IPCB_Board;+    SD    : IServerDocument;+    Inst  : IPCB_Component;+    BO    : IPCB_BoardOutline;+    Seg   : TPolySegment;+    Trk   : IPCB_Track;+    Txt   : IPCB_Text;+    R     : TCoordRect;+    Net   : IPCB_Net;+    GIt   : IPCB_GroupIterator;+    Pad   : IPCB_Pad;+    SL    : TStringList;+    FpList : TStringList;+    BIt   : IPCB_BoardIterator;+    ECmp  : IPCB_Component;+    GIt2  : IPCB_GroupIterator;+    MPrim : IPCB_Primitive;+    I, K, WC, HC, CX, CY, PX, PY, Found : Integer;+    NExist, ExtR, Cur, FW, FL, FR, AnyF : Integer;+    FpName, PcbPath, Des, NetName, Item, Missing : String;+    Ok, Append : Boolean;+Begin+    FpName := Param(Lines, 'footprint');+    PcbPath := Param(Lines, 'pcb');+    { footprint may be a ';' list: every part lands on ONE board, spread evenly along X+      (John, 2026-09-19: three ToF sensors on one board for the demo). }+    FpList := SplitSemi(FpName);+    WC := MMsToCoord(AdomFloat(Param(Lines, 'wMm')));+    HC := MMsToCoord(AdomFloat(Param(Lines, 'hMm')));+    PLib := PCBServer.GetCurrentPCBLibrary;+    If PLib = Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the PcbLib first') + '}';+        Exit;+    End;+    Missing := '';+    For K := 0 To FpList.Count - 1 Do+    Begin+        Ok := False;+        For I := 0 To PLib.ComponentCount - 1 Do+        Begin+            LibFp := PLib.GetComponent(I);+            If UpperCase(LibFp.Name) = UpperCase(FpList[K]) Then Begin Ok := True; Break; End;+        End;+        If Not Ok Then Missing := Missing + FpList[K] + ' ';+    End;+    If (FpList.Count = 0) Or (Missing <> '') Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','footprint not found: ' + Trim(Missing + FpName)) + '}';+        FpList.Free;+        Exit;+    End;+    { Append mode (issue 11): pcb names an EXISTING .PcbDoc -> open it and add the parts,+      instead of a new board per call. The board keeps its own outline and layers. }+    SD := Nil;+    Append := (PcbPath <> '') And FileExists(PcbPath);+    If (PcbPath <> '') And (Not Append) Then+    Begin+        SD := Nil;+        Try+            SD := Client.GetDocumentByPath(PcbPath);+        Except+            SD := Nil;+        End;+        If SD <> Nil Then Append := True;+    End;+    If Append Then+    Begin+        If SD = Nil Then SD := Client.OpenDocument('PCB', PcbPath);+        If SD = Nil Then+        Begin+            Result := '{' + JRaw('ok','false') + ',' + JStr('error','could not open the existing board: ' + PcbPath) + '}';+            FpList.Free;+            Exit;+        End;+        Client.ShowDocument(SD);+    End+    Else+    Begin+        SD := CreateNewDocumentFromDocumentKind('PCB');+        If SD = Nil Then+        Begin+            Result := '{' + JRaw('ok','false') + ',' + JStr('error','could not create a PCB document') + '}';+            FpList.Free;+            Exit;+        End;+        Client.ShowDocument(SD);+    End;+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board after open') + '}';+        FpList.Free;+        Exit;+    End;+    { existing parts: how many (designators continue from there) and how far right they reach }+    NExist := 0; ExtR := 0; CY := 0;+    If Append Then+    Begin+        BIt := Board.BoardIterator_Create;+        BIt.AddFilter_ObjectSet(MkSet(eComponentObject));+        BIt.AddFilter_LayerSet(AllLayers);+        BIt.AddFilter_Method(eProcessAll);+        ECmp := BIt.FirstPCBObject;+        While ECmp <> Nil Do+        Begin+            NExist := NExist + 1;+            If (NExist = 1) Or (ECmp.x > ExtR) Then ExtR := ECmp.x;+            CY := CY + ECmp.y;+            ECmp := BIt.NextPCBObject;+        End;+        Board.BoardIterator_Destroy(BIt);+        If NExist > 0 Then CY := CY Div NExist Else CY := MMsToCoord(7);+    End;+    PCBServer.PreProcess;+    If Not Append Then+    Begin+    Board.LayerStack.LayerObject[eMechanical9].MechanicalLayerEnabled := True;+    Board.LayerStack.LayerObject[eMechanical9].Name := 'Adom.SolderBalls';+    Board.LayerIsDisplayed[eMechanical9] := False;+    Board.LayerStack.LayerObject[eMechanical13].MechanicalLayerEnabled := True;+    Board.LayerStack.LayerObject[eMechanical13].Name := 'Fab';+    Board.LayerStack.LayerObject[eMechanical15].MechanicalLayerEnabled := True;+    Board.LayerStack.LayerObject[eMechanical15].Name := 'Courtyard';+    { board shape }+    BO := Board.BoardOutline;+    BO.Invalidate;+    { no Clear: IPCB_BoardOutline has no Clear in this engine (halt, measured);+      PointCount + Segments[] + Rebuild is the working pattern }+    BO.PointCount := 4;+    Seg := BO.Segments[0]; Seg.vx := 0; Seg.vy := 0; Seg.Kind := ePolySegmentLine; BO.Segments[0] := Seg;+    Seg := BO.Segments[1]; Seg.vx := WC; Seg.vy := 0; Seg.Kind := ePolySegmentLine; BO.Segments[1] := Seg;+    Seg := BO.Segments[2]; Seg.vx := WC; Seg.vy := HC; Seg.Kind := ePolySegmentLine; BO.Segments[2] := Seg;+    Seg := BO.Segments[3]; Seg.vx := 0; Seg.vy := HC; Seg.Kind := ePolySegmentLine; BO.Segments[3] := Seg;+    BO.Rebuild;+    BO.Validate;+    End;+    CX := WC Div 2;+    If Append Then Cur := ExtR + MMsToCoord(6)+    Else CY := HC Div 2;+    { one part: centred; N parts: at W*(k+1)/(N+1). Inst keeps the LAST placed part so the+      single-part assigns/stubs behaviour is unchanged. }+    For K := 0 To FpList.Count - 1 Do+    Begin+        For I := 0 To PLib.ComponentCount - 1 Do+        Begin+            LibFp := PLib.GetComponent(I);+            If UpperCase(LibFp.Name) = UpperCase(FpList[K]) Then Break;+        End;+        If Append Then+        Begin+            { pad-extent width of the library footprint, measured before placing }+            AnyF := 0; FL := 0; FR := 0;+            GIt2 := LibFp.GroupIterator_Create;+            GIt2.AddFilter_ObjectSet(MkSet(ePadObject));+            GIt2.AddFilter_LayerSet(AllLayers);+            MPrim := GIt2.FirstPCBObject;+            While MPrim <> Nil Do+            Begin+                R := MPrim.BoundingRectangle;+                If (AnyF = 0) Or (R.Left < FL) Then FL := R.Left;+                If (AnyF = 0) Or (R.Right > FR) Then FR := R.Right;+                AnyF := 1;+                MPrim := GIt2.NextPCBObject;+            End;+            LibFp.GroupIterator_Destroy(GIt2);+            FW := FR - FL;+            If FW < MMsToCoord(2) Then FW := MMsToCoord(2);+            CX := Cur + ((FW * 3) Div 4) + MMsToCoord(1);+            Cur := CX + ((FW * 3) Div 4) + MMsToCoord(1);+        End+        Else+            CX := (WC * (K + 1)) Div (FpList.Count + 1);+        Inst := PlaceCopy(Board, LibFp, CX, CY);+        { U1..Un so the read verbs (component_pins, footprint_primitives) can address each part;+          the factory left every part named Designator1 (measured 2026-09-20). Name stays hidden. }+        Inst.Name.Text := 'U' + IntToStr(NExist + K + 1);+        { the footprint name: the primitives were copied one by one, so the component carries+          no Pattern unless it is set here, and BOM / STEP naming / footprint compare key on it }+        Inst.Pattern := LibFp.Name;+        { The part name is the component's own COMMENT, shown on the top overlay under the+          footprint, so it moves with the part (free overlay text stayed behind when parts were+          moved and piled up in the silkscreen Gerber). 35 mil stroke, its top 2.5 mm below the+          part centre, clear of a 3 mm part's courtyard; centred on its real extent. }+        Inst.Comment.Text := LibFp.Name;+        Inst.CommentOn := True;+        Inst.Comment.Size := MilsToCoord(35);+        Inst.Comment.Width := MilsToCoord(5);+        Inst.Comment.MoveToXY(CX, CY - MMsToCoord(3.4));+        R := Inst.Comment.BoundingRectangle;+        Inst.Comment.MoveByXY(CX - ((R.Left + R.Right) Div 2), 0);+    End;+    AssignPadNets(Board, Inst, Param(Lines, 'assigns'));+    { stub tracks: des=x,y (mm from centre) -> a track from that pad to the point, on its net }+    SL := SplitSemi(Param(Lines, 'stubs'));+    For I := 0 To SL.Count - 1 Do+    Begin+        Item := SL[I];+        If Pos('=', Item) = 0 Then Continue;+        Des := Copy(Item, 1, Pos('=', Item) - 1);+        PX := CX + MMsToCoord(AdomFloat(Field(Copy(Item, Pos('=', Item) + 1, Length(Item)), 1)));+        PY := CY + MMsToCoord(AdomFloat(Field(Copy(Item, Pos('=', Item) + 1, Length(Item)), 2)));+        GIt := Inst.GroupIterator_Create;+        GIt.AddFilter_ObjectSet(MkSet(ePadObject));+        GIt.AddFilter_LayerSet(AllLayers);+        Pad := GIt.FirstPCBObject;+        Found := 0;+        While (Pad <> Nil) And (Found = 0) Do+        Begin+            If (Pad.Name = Des) And (Pad.Layer <> eMultiLayer) Then Found := 1+            Else If (Pad.Name = Des) And (Found = 0) And (Pad.Layer = eMultiLayer) Then Found := 0;+            If Found = 1 Then Break;+            Pad := GIt.NextPCBObject;+        End;+        If Pad <> Nil Then+        Begin+            Trk := PCBServer.PCBObjectFactory(eTrackObject, eNoDimension, eCreate_Default);+            Trk.Layer := eTopLayer;+            Trk.X1 := Pad.x; Trk.Y1 := Pad.y; Trk.X2 := PX; Trk.Y2 := PY;+            Trk.Width := MMsToCoord(0.2);+            Trk.Net := Pad.Net;+            Board.AddPCBObject(Trk);+            PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Trk.I_ObjectAddress);+        End;+        Inst.GroupIterator_Destroy(GIt);+    End;+    SL.Free;+    PCBServer.PostProcess;+    Board.ViewManager_FullUpdate;+    If PcbPath <> '' Then+    Begin+        If Not Append Then Begin FreePath(PcbPath); SD.SetFileName(PcbPath); End;+        SD.DoFileSave('');+    End;+    Result := '{' + JRaw('ok','true') + ',' + JStr('pcb', PcbPath) + ',' + JStr('placed', FpName) + ',' ++        JRaw('appended', BoolStr(Append)) + ',' + JRaw('existingParts', IntToStr(NExist)) + ',' ++        JRaw('count', IntToStr(FpList.Count)) + ',' ++        JRaw('padsOnComp', IntToStr(GroupCount(Inst, ePadObject))) + ',' ++        JRaw('bodiesOnComp', IntToStr(GroupCount(Inst, eComponentBodyObject))) + '}';+    FpList.Free;+End;++{ Replace the first component on the current board with another library+  footprint at the same position, keeping the nets by pad designator. Tracks are+  untouched, so the wiring survives the swap. Args: footprint, assigns }+Function CmdSwapFootprint(Lines : TStringList) : String;+Var+    PLib  : IPCB_Library;+    LibFp : IPCB_LibComponent;+    Board : IPCB_Board;+    BIt   : IPCB_BoardIterator;+    Old, Inst : IPCB_Component;+    I, CX, CY : Integer;+    FpName, LibPath : String;+    Ok : Boolean;+Begin+    FpName := Param(Lines, 'footprint');+    LibPath := Param(Lines, 'libraryPath');+    Board := PCBServer.GetCurrentPCBBoard;+    PLib := Nil;+    If LibPath <> '' Then PLib := PCBServer.LoadPCBLibraryByPath(LibPath);+    If PLib = Nil Then PLib := PCBServer.GetCurrentPCBLibrary;+    If (Board = Nil) Or (PLib = Nil) Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','need the board focused and the PcbLib path') + '}';+        Exit;+    End;+    Ok := False;+    For I := 0 To PLib.ComponentCount - 1 Do+    Begin+        LibFp := PLib.GetComponent(I);+        If UpperCase(LibFp.Name) = UpperCase(FpName) Then Begin Ok := True; Break; End;+    End;+    If Not Ok Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','footprint not found: ' + FpName) + '}';+        Exit;+    End;+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eComponentObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Old := BIt.FirstPCBObject;+    Board.BoardIterator_Destroy(BIt);+    If Old = Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','no component on the board') + '}';+        Exit;+    End;+    CX := Old.x; CY := Old.y;+    PCBServer.PreProcess;+    Board.RemovePCBObject(Old);+    Inst := PlaceCopy(Board, LibFp, CX, CY);+    AssignPadNets(Board, Inst, Param(Lines, 'assigns'));+    PCBServer.PostProcess;+    Board.ViewManager_FullUpdate;+    Result := '{' + JRaw('ok','true') + ',' + JStr('placed', FpName) + ',' ++        JRaw('padsOnComp', IntToStr(GroupCount(Inst, ePadObject))) + ',' ++        JRaw('bodiesOnComp', IntToStr(GroupCount(Inst, eComponentBodyObject))) + '}';+End;+++{ ------------------------------------------------------------ board_shape --- }++{ Redefine the CURRENT board shape as a wMm x hMm rectangle at (x0Mm,y0Mm).+  Until 0.2.31 this drew four temporary tracks and ran the editor process+  PCB:PlaceBoardOutline (Mode=BOARDOUTLINE_FROM_SEL_PRIMS), then read+  Board.BoardOutline.BoundingRectangle with NO nil guard. On AD 26.10.1 and on Altium Develop the+  process left no outline, the read raised on a nil object, and the script parked on a breakpoint+  with no error box (found by the verb battery on two builds, 2026-09-20). It now uses the pattern+  CmdTestBoard has always used and that renders correctly on every build: PointCount + Segments[]+  + Rebuild on the board's own outline, every access nil-guarded. }+Function CmdBoardShape(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    BO    : IPCB_BoardOutline;+    Seg   : TPolySegment;+    X0, Y0, X1, Y1, K, NTr : Integer;+    Trk : IPCB_Track;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','no board focused') + '}';+        Exit;+    End;+    X0 := MMsToCoord(AdomFloat(Param(Lines, 'x0Mm')));+    Y0 := MMsToCoord(AdomFloat(Param(Lines, 'y0Mm')));+    X1 := X0 + MMsToCoord(AdomFloat(Param(Lines, 'wMm')));+    Y1 := Y0 + MMsToCoord(AdomFloat(Param(Lines, 'hMm')));+    If (X1 <= X0) Or (Y1 <= Y0) Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','bad_size') + ',' + JStr('error','wMm and hMm must both be greater than 0') + '}';+        Exit;+    End;+    BO := Board.BoardOutline;+    If BO = Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_board_outline') + ',' ++            JStr('error','this document has no board outline object (a PcbLib internal board has none); open a .PcbDoc') + '}';+        Exit;+    End;+    PCBServer.PreProcess;+    BO.Invalidate;+    BO.PointCount := 4;+    Seg := BO.Segments[0]; Seg.vx := X0; Seg.vy := Y0; Seg.Kind := ePolySegmentLine; BO.Segments[0] := Seg;+    Seg := BO.Segments[1]; Seg.vx := X1; Seg.vy := Y0; Seg.Kind := ePolySegmentLine; BO.Segments[1] := Seg;+    Seg := BO.Segments[2]; Seg.vx := X1; Seg.vy := Y1; Seg.Kind := ePolySegmentLine; BO.Segments[2] := Seg;+    Seg := BO.Segments[3]; Seg.vx := X0; Seg.vy := Y1; Seg.Kind := ePolySegmentLine; BO.Segments[3] := Seg;+    BO.Rebuild;+    BO.Validate;+    { The board shape is not plotted into any Gerber: a fab outline needs real tracks. On+      request, draw the rectangle on Mechanical 1 (0.1 mm) so .GM1 carries it. Additive: a+      second call draws a second set, so callers reshaping repeatedly pass it once at the end. }+    NTr := 0;+    If Param(Lines, 'mechOutline') = '1' Then+    Begin+        For K := 0 To 3 Do+        Begin+            Trk := PCBServer.PCBObjectFactory(eTrackObject, eNoDimension, eCreate_Default);+            Trk.Layer := eMechanical1;+            Trk.Width := MMsToCoord(0.1);+            If K = 0 Then Begin Trk.X1 := X0; Trk.Y1 := Y0; Trk.X2 := X1; Trk.Y2 := Y0; End;+            If K = 1 Then Begin Trk.X1 := X1; Trk.Y1 := Y0; Trk.X2 := X1; Trk.Y2 := Y1; End;+            If K = 2 Then Begin Trk.X1 := X1; Trk.Y1 := Y1; Trk.X2 := X0; Trk.Y2 := Y1; End;+            If K = 3 Then Begin Trk.X1 := X0; Trk.Y1 := Y1; Trk.X2 := X0; Trk.Y2 := Y0; End;+            Board.AddPCBObject(Trk);+            PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Trk.I_ObjectAddress);+            NTr := NTr + 1;+        End;+    End;+    PCBServer.PostProcess;+    Board.ViewManager_FullUpdate;+    Result := '{' + JRaw('ok','true') + ',' + JRaw('outlineMil', OutlineMilFromVertices(BO)) + ',' ++        JStr('outlineSource', 'vertices') + ',' + JRaw('mechOutlineTracks', IntToStr(NTr)) + '}';+End;+++{ ---------------------------------------------------------- remove_symbol --- }++Function CmdRemoveSymbol(Lines : TStringList) : String;+Var+    SLib : ISch_Lib;+    Iter : ISch_Iterator;+    Comp : ISch_Component;+    Name : String;+    Found : Boolean;+Begin+    Name := Param(Lines, 'name');+    SLib := SchServer.GetCurrentSchDocument;+    If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the SchLib first') + '}';+        Exit;+    End;+    Found := False;+    Iter := SLib.SchLibIterator_Create;+    Iter.AddFilter_ObjectSet(MkSet(eSchComponent));+    Comp := Iter.FirstSchObject;+    While Comp <> Nil Do+    Begin+        If UpperCase(Comp.LibReference) = UpperCase(Name) Then Begin Found := True; Break; End;+        Comp := Iter.NextSchObject;+    End;+    SLib.SchIterator_Destroy(Iter);+    If Not Found Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','component not found: ' + Name) + '}';+        Exit;+    End;+    SLib.RemoveSchComponent(Comp);+    SLib.GraphicallyInvalidate;+    Result := '{' + JRaw('ok','true') + ',' + JStr('removed', Name) + '}';+End;+++{ -------------------------------------- Barrett-parity: library manipulation --- }++Function FindSchComp(SLib : ISch_Lib; Name : String) : ISch_Component;+Var+    Iter : ISch_Iterator;+    C    : ISch_Component;+Begin+    Result := Nil;+    Iter := SLib.SchLibIterator_Create;+    Iter.AddFilter_ObjectSet(MkSet(eSchComponent));+    C := Iter.FirstSchObject;+    While C <> Nil Do+    Begin+        If UpperCase(C.LibReference) = UpperCase(Name) Then Begin Result := C; Break; End;+        C := Iter.NextSchObject;+    End;+    SLib.SchIterator_Destroy(Iter);+End;++Function FindFp(PLib : IPCB_Library; Name : String) : IPCB_LibComponent;+Var+    I : Integer;+    F : IPCB_LibComponent;+Begin+    Result := Nil;+    For I := 0 To PLib.ComponentCount - 1 Do+    Begin+        F := PLib.GetComponent(I);+        If UpperCase(F.Name) = UpperCase(Name) Then Begin Result := F; Break; End;+    End;+End;++{ Clone a symbol inside the current SchLib under a new LibReference, all display+  modes included (per-primitive copy - group Replicate drops children). }+Function CmdCloneSymbol(Lines : TStringList) : String;+Var+    SLib : ISch_Lib;+    Src, Dst : ISch_Component;+    It  : ISch_Iterator;+    Prim, Cpy : ISch_GraphicalObject;+    NCopied : Integer;+Begin+    SLib := SchServer.GetCurrentSchDocument;+    If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the SchLib first') + '}';+        Exit;+    End;+    Src := FindSchComp(SLib, Param(Lines, 'from'));+    If Src = Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','component not found: ' + Param(Lines, 'from')) + '}';+        Exit;+    End;+    If FindSchComp(SLib, Param(Lines, 'to')) <> Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','target already exists: ' + Param(Lines, 'to')) + '}';+        Exit;+    End;+    Dst := SchServer.SchObjectFactory(eSchComponent, eCreate_Default);+    Dst.LibReference := Param(Lines, 'to');+    Dst.ComponentDescription := Src.ComponentDescription;+    Dst.CurrentPartID := 1;+    Dst.DisplayMode := 0;+    Dst.Designator.Text := Src.Designator.Text;+    Dst.Comment.Text := Src.Comment.Text;+    NCopied := 0;+    It := Src.SchIterator_Create;+    Prim := It.FirstSchObject;+    While Prim <> Nil Do+    Begin+        If Prim.ObjectID <> eParameter Then+        Begin+            Cpy := Prim.Replicate;+            Dst.AddSchObject(Cpy);+            NCopied := NCopied + 1;+        End;+        Prim := It.NextSchObject;+    End;+    Src.SchIterator_Destroy(It);+    Dst.DisplayModeCount := Src.DisplayModeCount;+    SLib.AddSchComponent(Dst);+    SchServer.RobotManager.SendMessage(SLib.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, Dst.I_ObjectAddress);+    SLib.GraphicallyInvalidate;+    Result := '{' + JRaw('ok','true') + ',' + JStr('from', Src.LibReference) + ',' + JStr('to', Dst.LibReference) + ',' ++        JRaw('primitives', IntToStr(NCopied)) + ',' + JRaw('displayModeCount', IntToStr(Dst.DisplayModeCount)) + '}';+End;++Function CmdRenameSymbol(Lines : TStringList) : String;+Var+    SLib : ISch_Lib;+    C    : ISch_Component;+Begin+    SLib := SchServer.GetCurrentSchDocument;+    If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the SchLib first') + '}';+        Exit;+    End;+    C := FindSchComp(SLib, Param(Lines, 'from'));+    If C = Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','component not found: ' + Param(Lines, 'from')) + '}';+        Exit;+    End;+    If Trim(Param(Lines, 'to')) = '' Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','name_required') + ',' + JStr('error','to (the new name) is required') + '}';+        Exit;+    End;+    If FindSchComp(SLib, Param(Lines, 'to')) <> Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','name_taken') + ',' + JStr('error','a component named ' + Param(Lines, 'to') + ' already exists in this library') + '}';+        Exit;+    End;+    C.LibReference := Param(Lines, 'to');+    SLib.GraphicallyInvalidate;+    Result := '{' + JRaw('ok','true') + ',' + JStr('renamed', Param(Lines, 'to')) + '}';+End;++{ Read a symbol back: pins (designator/name/mode), description, modes, params. }+Function CmdReadSymbol(Lines : TStringList) : String;+Var+    SLib : ISch_Lib;+    C    : ISch_Component;+    It   : ISch_Iterator;+    Prim : ISch_GraphicalObject;+    Pin  : ISch_Pin;+    Pm   : ISch_Parameter;+    Impl : ISch_Implementation;+    Pins, Params, Models : String;+    NPins : Integer;+Begin+    SLib := SchServer.GetCurrentSchDocument;+    If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the SchLib first') + '}';+        Exit;+    End;+    C := FindSchComp(SLib, Param(Lines, 'name'));+    If C = Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','component not found: ' + Param(Lines, 'name')) + '}';+        Exit;+    End;+    Pins := ''; Params := ''; NPins := 0;+    It := C.SchIterator_Create;+    Prim := It.FirstSchObject;+    While Prim <> Nil Do+    Begin+        If Prim.ObjectID = ePin Then+        Begin+            Pin := Prim;+            If Pin.OwnerPartDisplayMode = 0 Then+            Begin+                If Pins <> '' Then Pins := Pins + ',';+                Pins := Pins + '{' + JStr('des', Pin.Designator) + ',' + JStr('name', Pin.Name) + ',' + JRaw('electrical', IntToStr(Pin.Electrical)) + '}';+                NPins := NPins + 1;+            End;+        End+        Else If Prim.ObjectID = eParameter Then+        Begin+            Pm := Prim;+            If Params <> '' Then Params := Params + ',';+            Params := Params + '{' + JStr('name', Pm.Name) + ',' + JStr('value', Pm.Text) + ',' ++                JRaw('hidden', JBool(Pm.IsHidden)) + '}';+        End;+        Prim := It.NextSchObject;+    End;+    C.SchIterator_Destroy(It);+    { the symbol's model links (footprints, sim, 3D): the eImplementation child iterator, as in+      CmdSymbolPrims (ImplementationCount halts the script on some builds) (#24) }+    Models := '';+    It := C.SchIterator_Create;+    It.AddFilter_ObjectSet(MkSet(eImplementation));+    Impl := It.FirstSchObject;+    While Impl <> Nil Do+    Begin+        If Models <> '' Then Models := Models + ',';+        Models := Models + '{' + JStr('type', Impl.ModelType) + ',' + JStr('name', Impl.ModelName) + ',' ++            JRaw('isCurrent', JBool(Impl.IsCurrent)) + '}';+        Impl := It.NextSchObject;+    End;+    C.SchIterator_Destroy(It);+    Result := '{' + JRaw('ok','true') + ',' + JStr('libref', C.LibReference) + ',' ++        JStr('description', C.ComponentDescription) + ',' ++        JRaw('displayModeCount', IntToStr(C.DisplayModeCount)) + ',' ++        JRaw('pinsMode0', IntToStr(NPins)) + ',' ++        JRaw('pins', '[' + Pins + ']') + ',' + JRaw('params', '[' + Params + ']') + ',' + JRaw('models', '[' + Models + ']') + '}';+End;++{ Copy a footprint inside the current PcbLib under a new name (per-primitive). }+Function CmdCopyFootprint(Lines : TStringList) : String;+Var+    PLib : IPCB_Library;+    Src, Dst : IPCB_LibComponent;+    GIt  : IPCB_GroupIterator;+    Prim, Cpy : IPCB_Primitive;+    NCopied : Integer;+Begin+    PLib := PCBServer.GetCurrentPCBLibrary;+    If PLib = Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the PcbLib first') + '}';+        Exit;+    End;+    Src := FindFp(PLib, Param(Lines, 'from'));+    If Src = Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','footprint not found: ' + Param(Lines, 'from')) + '}';+        Exit;+    End;+    If FindFp(PLib, Param(Lines, 'to')) <> Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','target already exists: ' + Param(Lines, 'to')) + '}';+        Exit;+    End;+    PCBServer.PreProcess;+    Dst := PCBServer.CreatePCBLibComp;+    Dst.Name := Param(Lines, 'to');+    Dst.Description := Src.Description;+    NCopied := 0;+    GIt := Src.GroupIterator_Create;+    GIt.AddFilter_LayerSet(AllLayers);+    Prim := GIt.FirstPCBObject;+    While Prim <> Nil Do+    Begin+        Cpy := Prim.Replicate;+        Dst.AddPCBObject(Cpy);+        NCopied := NCopied + 1;+        Prim := GIt.NextPCBObject;+    End;+    Src.GroupIterator_Destroy(GIt);+    PLib.RegisterComponent(Dst);+    PCBServer.PostProcess;+    PLib.Board.ViewManager_FullUpdate;+    Result := '{' + JRaw('ok','true') + ',' + JStr('from', Src.Name) + ',' + JStr('to', Dst.Name) + ',' ++        JRaw('primitives', IntToStr(NCopied)) + '}';+End;++Function CmdRenameFootprint(Lines : TStringList) : String;+Var+    PLib : IPCB_Library;+    F    : IPCB_LibComponent;+Begin+    PLib := PCBServer.GetCurrentPCBLibrary;+    If PLib = Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the PcbLib first') + '}';+        Exit;+    End;+    F := FindFp(PLib, Param(Lines, 'from'));+    If F = Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','footprint not found: ' + Param(Lines, 'from')) + '}';+        Exit;+    End;+    If Trim(Param(Lines, 'to')) = '' Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','name_required') + ',' + JStr('error','to (the new name) is required') + '}';+        Exit;+    End;+    If FindFp(PLib, Param(Lines, 'to')) <> Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','name_taken') + ',' + JStr('error','a footprint named ' + Param(Lines, 'to') + ' already exists in this library') + '}';+        Exit;+    End;+    F.Name := Param(Lines, 'to');+    PLib.Board.ViewManager_FullUpdate;+    Result := '{' + JRaw('ok','true') + ',' + JStr('renamed', Param(Lines, 'to')) + '}';+End;++Function CmdRemoveFootprint(Lines : TStringList) : String;+Var+    PLib : IPCB_Library;+    F    : IPCB_LibComponent;+Begin+    PLib := PCBServer.GetCurrentPCBLibrary;+    If PLib = Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the PcbLib first') + '}';+        Exit;+    End;+    F := FindFp(PLib, Param(Lines, 'name'));+    If F = Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','footprint not found: ' + Param(Lines, 'name')) + '}';+        Exit;+    End;+    PLib.RemoveComponent(F);+    PLib.Board.ViewManager_FullUpdate;+    Result := '{' + JRaw('ok','true') + ',' + JStr('removed', Param(Lines, 'name')) + '}';+End;+++{ ---------------------------------------- cleanup_libs / set_visibility / xcopy --- }++{ Remove factory-default empty components (Component_1 / PCBComponent_1) from the+  current SchLib or PcbLib - the strays every fresh library carries. }+Function CmdCleanupLibs(Lines : TStringList) : String;+Var+    SLib : ISch_Lib;+    PLib : IPCB_Library;+    C    : ISch_Component;+    F    : IPCB_LibComponent;+    PIt  : ISch_Iterator;+    Pin  : ISch_Pin;+    NPin, NRemoved, NKept : Integer;+Begin+    NRemoved := 0;+    NKept    := 0;+    SLib := SchServer.GetCurrentSchDocument;+    If (SLib <> Nil) And (SLib.ObjectID = eSchLib) Then+    Begin+        C := FindSchComp(SLib, 'Component_1');+        If C <> Nil Then+        Begin+            { Only the FACTORY stray, never a symbol someone drew into it. The PcbLib side has+              always checked this (GroupCount = 0); the SchLib side deleted whatever was called+              Component_1, pins and all. }+            NPin := 0;+            PIt := C.SchIterator_Create;+            PIt.AddFilter_ObjectSet(MkSet(ePin));+            Pin := PIt.FirstSchObject;+            While Pin <> Nil Do+            Begin+                NPin := NPin + 1;+                Pin := PIt.NextSchObject;+            End;+            C.SchIterator_Destroy(PIt);+            If NPin = 0 Then+            Begin+                SLib.RemoveSchComponent(C);+                NRemoved := NRemoved + 1;+            End+            Else NKept := NKept + 1;+        End;+        SLib.GraphicallyInvalidate;+    End;+    PLib := PCBServer.GetCurrentPCBLibrary;+    If PLib <> Nil Then+    Begin+        F := FindFp(PLib, 'PCBComponent_1');+        If F <> Nil Then+        Begin+            If GroupCount(F, ePadObject) = 0 Then+            Begin+                PLib.RemoveComponent(F);+                NRemoved := NRemoved + 1;+            End+            Else NKept := NKept + 1;+        End;+        PLib.Board.ViewManager_FullUpdate;+    End;+    Result := '{' + JRaw('ok','true') + ',' + JRaw('removed', IntToStr(NRemoved)) ++        ',' + JRaw('keptNotEmpty', IntToStr(NKept)) + '}';+End;++{ Show or hide a named parameter on a SchLib component. name, param, visible=0|1 }+Function CmdSetVisibility(Lines : TStringList) : String;+Var+    SLib : ISch_Lib;+    C    : ISch_Component;+    It   : ISch_Iterator;+    Pm   : ISch_Parameter;+    Target : String;+    Found : Boolean;+Begin+    SLib := SchServer.GetCurrentSchDocument;+    If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the SchLib first') + '}';+        Exit;+    End;+    C := FindSchComp(SLib, Param(Lines, 'name'));+    If C = Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','component not found: ' + Param(Lines, 'name')) + '}';+        Exit;+    End;+    Target := Param(Lines, 'param');+    Found := False;+    It := C.SchIterator_Create;+    It.AddFilter_ObjectSet(MkSet(eParameter));+    Pm := It.FirstSchObject;+    While Pm <> Nil Do+    Begin+        If UpperCase(Pm.Name) = UpperCase(Target) Then+        Begin+            Pm.IsHidden := Param(Lines, 'visible') <> '1';+            Found := True;+        End;+        Pm := It.NextSchObject;+    End;+    C.SchIterator_Destroy(It);+    SLib.GraphicallyInvalidate;+    If Not Found Then+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','parameter not found: ' + Target) + '}'+    Else+        Result := '{' + JRaw('ok','true') + ',' + JStr('param', Target) + ',' + JStr('visible', Param(Lines, 'visible')) + '}';+End;++{ Copy a footprint ACROSS libraries: fromLib(path)+from -> the CURRENT PcbLib as to. }+{ a footprint's pad by its name, Nil when it has none }+Function FpPad(Fp : IPCB_LibComponent; Name : String) : IPCB_Pad;+Var+    GIt : IPCB_GroupIterator;+    P   : IPCB_Pad;+Begin+    Result := Nil;+    GIt := Fp.GroupIterator_Create;+    GIt.AddFilter_ObjectSet(MkSet(ePadObject));+    GIt.AddFilter_LayerSet(AllLayers);+    P := GIt.FirstPCBObject;+    While P <> Nil Do+    Begin+        If P.Name = Name Then Begin Result := P; Break; End;+        P := GIt.NextPCBObject;+    End;+    Fp.GroupIterator_Destroy(GIt);+End;++{ the name of a footprint's first pad, '' when it has none }+Function FirstPadName(Fp : IPCB_LibComponent) : String;+Var+    GIt : IPCB_GroupIterator;+    P   : IPCB_Pad;+Begin+    Result := '';+    GIt := Fp.GroupIterator_Create;+    GIt.AddFilter_ObjectSet(MkSet(ePadObject));+    GIt.AddFilter_LayerSet(AllLayers);+    P := GIt.FirstPCBObject;+    If P <> Nil Then Result := P.Name;+    Fp.GroupIterator_Destroy(GIt);+End;++{ any primitive of a footprint, Nil when it is empty }+Function FpFirstAny(Fp : IPCB_LibComponent) : IPCB_Primitive;+Var+    GIt : IPCB_GroupIterator;+Begin+    GIt := Fp.GroupIterator_Create;+    GIt.AddFilter_LayerSet(AllLayers);+    Result := GIt.FirstPCBObject;+    Fp.GroupIterator_Destroy(GIt);+End;++Function CmdXCopyFootprint(Lines : TStringList) : String;+Var+    SrcLib, DstLib : IPCB_Library;+    Src, Dst : IPCB_LibComponent;+    GIt  : IPCB_GroupIterator;+    Prim, Cpy : IPCB_Primitive;+    SP, DP : IPCB_Pad;+    NCopied, SX, SY : Integer;+    Dflt : IPCB_LibComponent;+    Dropped, PN : String;+Begin+    DstLib := PCBServer.GetCurrentPCBLibrary;+    If DstLib = Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the destination PcbLib first') + '}';+        Exit;+    End;+    SrcLib := PCBServer.LoadPCBLibraryByPath(Param(Lines, 'fromLib'));+    If SrcLib = Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','source library not loadable: ' + Param(Lines, 'fromLib')) + '}';+        Exit;+    End;+    Src := FindFp(SrcLib, Param(Lines, 'from'));+    If Src = Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','footprint not found in source: ' + Param(Lines, 'from')) + '}';+        Exit;+    End;+    If FindFp(DstLib, Param(Lines, 'to')) <> Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','target already exists: ' + Param(Lines, 'to')) + '}';+        Exit;+    End;+    { the source's first pad, from its origin, read while the source is the footprint on display: only that one sits+      on the library's origin (lib_fp reads the same way) }+    If SrcLib.CurrentComponent <> Src Then Begin SrcLib.CurrentComponent := Src; SrcLib.Board.ViewManager_FullUpdate; End;+    { one pad, named once: the order Altium walks a footprint's pads in changes when the display moves }+    PN := FirstPadName(Src);+    SP := FpPad(Src, PN);+    If SP <> Nil Then Begin SX := SP.X - SrcLib.Board.XOrigin; SY := SP.Y - SrcLib.Board.YOrigin; End;+    PCBServer.PreProcess;+    Dst := PCBServer.CreatePCBLibComp;+    Dst.Name := Param(Lines, 'to');+    Dst.Description := Src.Description;+    NCopied := 0;+    GIt := Src.GroupIterator_Create;+    GIt.AddFilter_LayerSet(AllLayers);+    Prim := GIt.FirstPCBObject;+    While Prim <> Nil Do+    Begin+        Cpy := Prim.Replicate;+        { the source is on display, so its primitives sit on the library's origin; a footprint not on display keeps+          them from the origin itself, and Altium puts it back on the origin when it is shown }+        Cpy.MoveByXY(-SrcLib.Board.XOrigin, -SrcLib.Board.YOrigin);+        Dst.AddPCBObject(Cpy);+        NCopied := NCopied + 1;+        Prim := GIt.NextPCBObject;+    End;+    Src.GroupIterator_Destroy(GIt);+    DstLib.RegisterComponent(Dst);+    { Moving the copy while it is on display does not hold: Altium shifted it again when the display left it and came+      back (the copy landed one origin, 1270 mm, off, then 6 mm off: altium-bridge#29, 2026-10-30). The replicas are+      made from the origin instead, above, and the copy is checked once shown. }+    DstLib.CurrentComponent := Dst; DstLib.Board.ViewManager_FullUpdate;+    { a PcbLib Altium has just made holds an empty PCBComponent_1: it goes once a real footprint is in }+    Dropped := '';+    Dflt := FindFp(DstLib, 'PCBComponent_1');+    If (Dflt <> Nil) And (UpperCase(Dst.Name) <> 'PCBCOMPONENT_1') And (FirstPadName(Dflt) = '') And (FpFirstAny(Dflt) = Nil) Then+    Begin+        DstLib.RemoveComponent(Dflt);+        Dropped := 'PCBComponent_1';+    End;+    PCBServer.PostProcess;+    DstLib.Board.ViewManager_FullUpdate;+    { read back on display: the copy's first pad must sit where the source's does }+    DP := FpPad(Dst, PN);+    If (SP <> Nil) And (DP <> Nil) And (DstLib.CurrentComponent = Dst) Then+        If (DP.X - DstLib.Board.XOrigin <> SX) Or (DP.Y - DstLib.Board.YOrigin <> SY) Then+        Begin+            Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','copy_misplaced') + ',' + JStr('error', 'the copy of ' + Src.Name + ' landed off its origin: pad ' + DP.Name + ' at ' + IntToStr(DP.X - DstLib.Board.XOrigin) + ',' + IntToStr(DP.Y - DstLib.Board.YOrigin) + ' where the source has ' + IntToStr(SX) + ',' + IntToStr(SY) + ' (internal units)') + '}';+            Exit;+        End;+    Result := '{' + JRaw('ok','true') + ',' + JStr('to', Dst.Name) + ',' + JRaw('primitives', IntToStr(NCopied)) + ',' + JStr('removedDefault', Dropped) + '}';+End;++++{ ---- Phase 1: board introspection, read-only (altium-bridge parity plan) ---- }+{ Every pad of a component as name=net pairs, all pads, capped. }+Function AllPadNets(Comp : IPCB_Component) : String;+Var+    GIt : IPCB_GroupIterator;+    Pad : IPCB_Pad;+    S, N : String;+    K : Integer;+Begin+    S := ''; K := 0;+    GIt := Comp.GroupIterator_Create;+    GIt.AddFilter_ObjectSet(MkSet(ePadObject));+    GIt.AddFilter_LayerSet(AllLayers);+    Pad := GIt.FirstPCBObject;+    While (Pad <> Nil) And (K < 400) Do+    Begin+        N := '';+        If Pad.Net <> Nil Then N := Pad.Net.Name;+        If S <> '' Then S := S + ';';+        S := S + Pad.Name + '=' + N;+        K := K + 1;+        Pad := GIt.NextPCBObject;+    End;+    Comp.GroupIterator_Destroy(GIt);+    Result := S;+End;++Function FindCompByDes(Board : IPCB_Board; Des : String) : IPCB_Component;+Var+    BIt  : IPCB_BoardIterator;+    Comp : IPCB_Component;+Begin+    Result := Nil;+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eComponentObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Comp := BIt.FirstPCBObject;+    While (Comp <> Nil) And (Result = Nil) Do+    Begin+        If UpperCase(Comp.Name.Text) = UpperCase(Des) Then Result := Comp;+        Comp := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+End;++Function CompJson(Comp : IPCB_Component) : String;+Var+    R : TCoordRect;+    Src : String;+Begin+    R := Comp.BoundingRectangle;+    { the link to the schematic symbol (its UniqueId path); empty for a part placed by hand }+    Src := '';+    Try Src := Comp.SourceUniqueId; Except End;+    Result := '{' + JStr('designator', Comp.Name.Text) + ',' + JStr('sourceUniqueId', Src) + ',' ++        JStr('pattern', Comp.Pattern) + ',' ++        JStr('comment', Comp.Comment.Text) + ',' ++        JRaw('layerId', IntToStr(Comp.Layer)) + ',' ++        JRaw('bottom', JBool(Comp.Layer = eBottomLayer)) + ',' ++        JRaw('xMil', IntToStr(Round(CoordToMils(Comp.x)))) + ',' ++        JRaw('yMil', IntToStr(Round(CoordToMils(Comp.y)))) + ',' ++        JRaw('rotation', IntToStr(Round(Comp.Rotation))) + ',' ++        JRaw('wMil', IntToStr(Round(CoordToMils(R.Right - R.Left)))) + ',' ++        JRaw('hMil', IntToStr(Round(CoordToMils(R.Top - R.Bottom)))) + ',' ++        JRaw('pads', IntToStr(GroupCount(Comp, ePadObject))) + ',' ++        JStr('nets', AllPadNets(Comp)) + '}';+End;++{ bom_placement: every component's footprint, side and reference point relative to the board origin, for a+  pick-and-place file. Lean on purpose (no pads, no nets): a large board's full read takes minutes.+  x/y are RAW coords from the origin (10000 per mil), rotation in thousandths of a degree (locale-free). }+{ import_step: a STEP placed with the board as free 3D geometry (an enclosure, a bracket) for fit checks: a 3D body of+  the board itself, on a mechanical layer, belonging to no part. path= the STEP; x= y= mm (the board model's frame) where+  the STEP's own origin lands; rx= ry= rz= degrees; standoff= mm; layer= the mechanical layer id; side=top|bottom.+  Answers the body's extent (raw, absolute) and its height. }+Function CmdImportStep(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    Body  : IPCB_ComponentBody;+    Mdl   : IPCB_Model;+    R     : TCoordRect;+    Path  : String;+    L     : Integer;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_board') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    Path := Param(Lines, 'path');+    If Not FileExists(Path) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','file_not_found') + ',' + JStr('error','no STEP at ' + Path) + '}'; Exit; End;+    L := StrToIntDef(Param(Lines, 'layer'), eMechanical1);+    PCBServer.PreProcess;+    Body := PCBServer.PCBObjectFactory(eComponentBodyObject, eNoDimension, eCreate_Default);+    { a body with no layer is dropped on add, and an invisible one reads as missing (see add_body) }+    Body.Layer := L;+    Board.LayerStack.LayerObject[L].MechanicalLayerEnabled := True;+    Board.LayerIsDisplayed[L] := True;+    Body.BodyOpacity3D := 1.0;+    Mdl := Body.ModelFactory_FromFilename(Path, False);+    If Mdl = Nil Then Begin PCBServer.PostProcess; Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','model_not_loaded') + ',' + JStr('error','Altium could not load ' + Path) + '}'; Exit; End;+    Try Mdl.SetState(AdomFloat(Param(Lines, 'rx')), AdomFloat(Param(Lines, 'ry')), AdomFloat(Param(Lines, 'rz')), MmC(Param(Lines, 'standoff'))); Except End;+    Body.SetState_FromModel;+    Body.Model := Mdl;+    If Param(Lines, 'side') = 'bottom' Then Body.BodyProjection := eBoardSide_Bottom Else Body.BodyProjection := eBoardSide_Top;+    Body.MoveByXY(MmC(Param(Lines, 'x')), MmC(Param(Lines, 'y')));+    Board.AddPCBObject(Body);+    PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Body.I_ObjectAddress);+    PCBServer.PostProcess;+    Board.ViewManager_FullUpdate;+    R := Body.BoundingRectangle;+    Result := '{' + JRaw('ok','true') + ',' + JRaw('box', '[' + IntToStr(R.Left) + ',' + IntToStr(R.Bottom) + ',' + IntToStr(R.Right) + ',' + IntToStr(R.Top) + ']') + ',' ++        JRaw('standoff', IntToStr(Body.StandoffHeight)) + ',' + JRaw('height', IntToStr(Body.OverallHeight)) + ',' + JRaw('layer', IntToStr(Body.Layer)) + ',' ++        JStr('id', IntToStr(Body.I_ObjectAddress)) + '}';+End;++Function CmdBomPlacement(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    BIt   : IPCB_BoardIterator;+    Comp  : IPCB_Component;+    GIt   : IPCB_GroupIterator;+    Pad   : IPCB_Pad;+    S     : String;+    N     : Integer;+    Tht   : Boolean;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    S := ''; N := 0;+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eComponentObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Comp := BIt.FirstPCBObject;+    While Comp <> Nil Do+    Begin+        { through-hole: any pad with a hole (export_pos's smdOnly leaves these out) }+        Tht := False;+        GIt := Comp.GroupIterator_Create;+        GIt.AddFilter_ObjectSet(MkSet(ePadObject));+        Pad := GIt.FirstPCBObject;+        While (Pad <> Nil) And (Not Tht) Do Begin If Pad.HoleSize > 0 Then Tht := True; Pad := GIt.NextPCBObject; End;+        Comp.GroupIterator_Destroy(GIt);+        If S <> '' Then S := S + ',';+        S := S + '[' + '"' + JsonEsc(Comp.Name.Text) + '","' + JsonEsc(Comp.Pattern) + '",' ++            IntToStr(Comp.x - Board.XOrigin) + ',' + IntToStr(Comp.y - Board.YOrigin) + ',' ++            IntToStr(Round(Comp.Rotation * 1000)) + ',' + JBool(Comp.Layer = eBottomLayer) + ',"' + JsonEsc(Comp.Comment.Text) + '",' + JBool(Tht) + ']';+        N := N + 1;+        Comp := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+    Result := '{' + JRaw('ok','true') + ',' + JRaw('count', IntToStr(N)) + ',' + JRaw('comps', '[' + S + ']') + '}';+End;++{ comp_rects: every component's extent the way Altium's Component Clearance rule sees it: its 3D bodies and pads+  when it has a body, else every primitive but text (pads, silkscreen and other lines, fills, regions; measured+  2026-10-22: a part with no body clashed by its silkscreen outline). Raw units relative to the board origin:+  [des, l, b, r, t, bottom, hasBody] }+Function CmdCompRects(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    BIt   : IPCB_BoardIterator;+    GIt   : IPCB_GroupIterator;+    Comp  : IPCB_Component;+    Prim  : IPCB_Primitive;+    R     : TCoordRect;+    S     : String;+    L, B, Rt, T, L2, B2, R2, T2 : Integer;+    AnyAll, AnyBody, HasBody : Boolean;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_board') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    S := '';+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eComponentObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Comp := BIt.FirstPCBObject;+    While Comp <> Nil Do+    Begin+        AnyAll := False; AnyBody := False; HasBody := False;+        L := 0; B := 0; Rt := 0; T := 0; L2 := 0; B2 := 0; R2 := 0; T2 := 0;+        GIt := Comp.GroupIterator_Create;+        GIt.AddFilter_ObjectSet(MkSet(ePadObject, eTrackObject, eArcObject, eFillObject, eRegionObject, eComponentBodyObject));+        Prim := GIt.FirstPCBObject;+        While Prim <> Nil Do+        Begin+            R := Prim.BoundingRectangle;+            If Not AnyAll Then Begin L := R.Left; B := R.Bottom; Rt := R.Right; T := R.Top; AnyAll := True; End+            Else Begin+                If R.Left < L Then L := R.Left; If R.Bottom < B Then B := R.Bottom;+                If R.Right > Rt Then Rt := R.Right; If R.Top > T Then T := R.Top;+            End;+            If (Prim.ObjectId = ePadObject) Or (Prim.ObjectId = eComponentBodyObject) Then+            Begin+                If Prim.ObjectId = eComponentBodyObject Then HasBody := True;+                If Not AnyBody Then Begin L2 := R.Left; B2 := R.Bottom; R2 := R.Right; T2 := R.Top; AnyBody := True; End+                Else Begin+                    If R.Left < L2 Then L2 := R.Left; If R.Bottom < B2 Then B2 := R.Bottom;+                    If R.Right > R2 Then R2 := R.Right; If R.Top > T2 Then T2 := R.Top;+                End;+            End;+            Prim := GIt.NextPCBObject;+        End;+        Comp.GroupIterator_Destroy(GIt);+        If HasBody Then Begin L := L2; B := B2; Rt := R2; T := T2; End;+        If AnyAll Then+        Begin+            If S <> '' Then S := S + ',';+            S := S + '["' + JsonEsc(Comp.Name.Text) + '",' + IntToStr(L - Board.XOrigin) + ',' + IntToStr(B - Board.YOrigin) + ',' ++                IntToStr(Rt - Board.XOrigin) + ',' + IntToStr(T - Board.YOrigin) + ',' + JBool(Comp.Layer = eBottomLayer) + ',' + JBool(HasBody) + ']';+        End;+        Comp := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+    Result := '{' + JRaw('ok','true') + ',' + JRaw('rects', '[' + S + ']') + '}';+End;++{ list_designators: every component designator on the current board }+Function CmdListDesignators(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    BIt   : IPCB_BoardIterator;+    Comp  : IPCB_Component;+    S     : String;+    N     : Integer;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    S := ''; N := 0;+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eComponentObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Comp := BIt.FirstPCBObject;+    While Comp <> Nil Do+    Begin+        If S <> '' Then S := S + ',';+        S := S + '"' + JsonEsc(Comp.Name.Text) + '"';+        N := N + 1;+        Comp := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+    Result := '{' + JRaw('ok','true') + ',' + JRaw('count', IntToStr(N)) + ',' + JRaw('designators', '[' + S + ']') + '}';+End;++{ component_data: designator=<one> or all (limit=N, default 500) }+Function CmdComponentData(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    BIt   : IPCB_BoardIterator;+    Comp  : IPCB_Component;+    Des, S : String;+    N, Limit : Integer;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    Des := Param(Lines, 'designator');+    Limit := StrToIntDef(Param(Lines, 'limit'), 500);+    If Des <> '' Then+    Begin+        Comp := FindCompByDes(Board, Des);+        If Comp = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','designator not found: ' + Des) + '}'; Exit; End;+        Result := '{' + JRaw('ok','true') + ',' + JRaw('component', CompJson(Comp)) + '}';+        Exit;+    End;+    S := ''; N := 0;+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eComponentObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Comp := BIt.FirstPCBObject;+    While (Comp <> Nil) And (N < Limit) Do+    Begin+        If S <> '' Then S := S + ',';+        S := S + CompJson(Comp);+        N := N + 1;+        Comp := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+    Result := '{' + JRaw('ok','true') + ',' + JRaw('count', IntToStr(N)) + ',' + JRaw('components', '[' + S + ']') + '}';+End;++{ component_pins: designator=<one> -> every pad with net, position, size, hole }+Function CmdComponentPins(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    Comp  : IPCB_Component;+    GIt   : IPCB_GroupIterator;+    Pad   : IPCB_Pad;+    Des, S, N : String;+    K : Integer;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    Des := Param(Lines, 'designator');+    If Des = '' Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','designator is required') + '}'; Exit; End;+    Comp := FindCompByDes(Board, Des);+    If Comp = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','designator not found: ' + Des) + '}'; Exit; End;+    S := ''; K := 0;+    GIt := Comp.GroupIterator_Create;+    GIt.AddFilter_ObjectSet(MkSet(ePadObject));+    GIt.AddFilter_LayerSet(AllLayers);+    Pad := GIt.FirstPCBObject;+    While (Pad <> Nil) And (K < 1000) Do+    Begin+        N := '';+        If Pad.Net <> Nil Then N := Pad.Net.Name;+        If S <> '' Then S := S + ',';+        S := S + '{' + JStr('name', Pad.Name) + ',' + JStr('net', N) + ',' ++            JRaw('xMil', IntToStr(Round(CoordToMils(Pad.x)))) + ',' ++            JRaw('yMil', IntToStr(Round(CoordToMils(Pad.y)))) + ',' ++            JRaw('layerId', IntToStr(Pad.Layer)) + ',' ++            JRaw('wMil', IntToStr(Round(CoordToMils(Pad.TopXSize)))) + ',' ++            JRaw('hMil', IntToStr(Round(CoordToMils(Pad.TopYSize)))) + ',' ++            JRaw('holeMil', IntToStr(Round(CoordToMils(Pad.HoleSize)))) + '}';+        K := K + 1;+        Pad := GIt.NextPCBObject;+    End;+    Comp.GroupIterator_Destroy(GIt);+    Result := '{' + JRaw('ok','true') + ',' + JStr('designator', Comp.Name.Text) + ',' + JRaw('count', IntToStr(K)) + ',' + JRaw('pins', '[' + S + ']') + '}';+End;++{ list_nets: every net on the board with its pad count (tallied over all pads) }+Function CmdListNets(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    BIt   : IPCB_BoardIterator;+    Pad   : IPCB_Pad;+    SL    : TStringList;+    S, N  : String;+    I, C  : Integer;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    SL := TStringList.Create;+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(ePadObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Pad := BIt.FirstPCBObject;+    While Pad <> Nil Do+    Begin+        If Pad.Net <> Nil Then+        Begin+            N := Pad.Net.Name;+            C := StrToIntDef(SL.Values[N], 0);+            SL.Values[N] := IntToStr(C + 1);+        End;+        Pad := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+    S := '';+    For I := 0 To SL.Count - 1 Do+    Begin+        If S <> '' Then S := S + ',';+        S := S + '{' + JStr('name', SL.Names[I]) + ',' + JRaw('pads', SL.Values[SL.Names[I]]) + '}';+    End;+    Result := '{' + JRaw('ok','true') + ',' + JRaw('count', IntToStr(SL.Count)) + ',' + JRaw('nets', '[' + S + ']') + '}';+    SL.Free;+End;++Function LayerJson(Board : IPCB_Board; L : Integer; Kind : String) : String;+Begin+    Result := '{' + JStr('name', Board.LayerStack.LayerObject[L].Name) + ',' ++        JRaw('id', IntToStr(L)) + ',' + JStr('kind', Kind) + ',' ++        JRaw('displayed', JBool(Board.LayerIsDisplayed[L])) + '}';+End;++{ layer_stack: the copper layers in stack order with their copper thickness and the dielectric under each+  (height, constant x1000, material). Classic stack API (FirstLayer / NextLayer / Dielectric). }+Function CmdLayerStack(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    LS    : IPCB_LayerStack_V7;+    L     : IPCB_LayerObject_V7;+    S     : String;+    N, Tot : Integer;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    S := ''; N := 0; Tot := 0;+    LS := Board.LayerStack_V7;+    L := LS.FirstLayer;+    While (L <> Nil) And (N < 64) Do+    Begin+        If S <> '' Then S := S + ',';+        S := S + '{' + JStr('name', L.Name) + ',' + JRaw('layerId', IntToStr(L.V6_LayerID)) + ',' + JRaw('copperUm', IntToStr(Round(CoordToMils(L.CopperThickness) * 25.4)));+        If (L.V6_LayerID >= eInternalPlane1) And (L.V6_LayerID <= eInternalPlane16) Then+            S := S + ',' + JStr('kind', 'plane') + ',' + JStr('net', L.NetName) + ',' + JRaw('plane', IntToStr(L.V6_LayerID - eInternalPlane1 + 1))+        Else If (L.V6_LayerID >= eMidLayer1) And (L.V6_LayerID <= eMidLayer30) Then+            S := S + ',' + JStr('kind', 'signal') + ',' + JRaw('mid', IntToStr(L.V6_LayerID - eMidLayer1 + 1))+        Else S := S + ',' + JStr('kind', 'signal');+        Tot := Tot + L.CopperThickness;+        { a layer's dielectric is the one below it: the last copper layer has none in the board }+        If (L.Dielectric <> Nil) And (LS.NextLayer(L) <> Nil) Then+        Begin+            S := S + ',' + JRaw('dielectricUm', IntToStr(Round(CoordToMils(L.Dielectric.DielectricHeight) * 25.4))) + ',' ++                JRaw('dk1000', IntToStr(Round(L.Dielectric.DielectricConstant * 1000))) + ',' + JStr('material', L.Dielectric.DielectricMaterial);+            Tot := Tot + L.Dielectric.DielectricHeight;+        End;+        S := S + '}';+        N := N + 1;+        L := LS.NextLayer(L);+    End;+    Result := '{' + JRaw('ok','true') + ',' + JRaw('copperLayers', IntToStr(N)) + ',' + JRaw('totalUm', IntToStr(Round(CoordToMils(Tot) * 25.4))) + ',' + JRaw('layers', '[' + S + ']') + '}';+End;++{ layer_stack_edit: op=insert (layer=<V6 id: plane N = eInternalPlane1+N-1, mid N = eMidLayer1+N-1>, net for a plane,+  name) | remove (layer). Through Board.LayerStack_V7 (the classic LayerStack halts on AD 26). Answers the stack after. }+Function CmdLayerStackEdit(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    LS    : IPCB_LayerStack_V7;+    L     : IPCB_LayerObject_V7;+    Op, Net, Nm : String;+    Id : Integer;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_board') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    Op := Param(Lines, 'op'); Net := Param(Lines, 'net'); Nm := Param(Lines, 'name');+    If Param(Lines, 'kind') = 'plane' Then Id := eInternalPlane1 + StrToIntDef(Param(Lines, 'n'), 1) - 1+    Else Id := eMidLayer1 + StrToIntDef(Param(Lines, 'n'), 1) - 1;+    LS := Board.LayerStack_V7;+    PCBServer.PreProcess;+    If Op = 'insert' Then+    Begin+        LS.InsertLayer(Id);+        L := LS.LayerObject(Id);+        If (L <> Nil) And (Nm <> '') Then L.Name := Nm;+        If (L <> Nil) And (Net <> '') Then L.NetName := Net;+    End+    Else If Op = 'remove' Then+    Begin+        L := LS.LayerObject(Id);+        If L <> Nil Then LS.RemoveFromStack(L);+    End+    Else If Op = 'set' Then+    Begin+        { by name: copper (um), the dielectric BELOW it (um, Dk x 1000, material), a plane's net and pullback (um) }+        L := LS.FirstLayer;+        While L <> Nil Do+        Begin+            If UpperCase(L.Name) = UpperCase(Param(Lines, 'target')) Then+            Begin+                If Param(Lines, 'copperUm') <> '' Then L.CopperThickness := MilsToCoord(StrToIntDef(Param(Lines, 'copperUm'), 35) / 25.4);+                If (L.Dielectric <> Nil) And (Param(Lines, 'dielectricUm') <> '') Then L.Dielectric.DielectricHeight := MilsToCoord(StrToIntDef(Param(Lines, 'dielectricUm'), 200) / 25.4);+                If (L.Dielectric <> Nil) And (Param(Lines, 'dk1000') <> '') Then L.Dielectric.DielectricConstant := StrToIntDef(Param(Lines, 'dk1000'), 4200) / 1000;+                If (L.Dielectric <> Nil) And (Param(Lines, 'material') <> '') Then L.Dielectric.DielectricMaterial := Param(Lines, 'material');+                If Nm <> '' Then L.Name := Nm;+                If (L.V6_LayerID >= eInternalPlane1) And (L.V6_LayerID <= eInternalPlane16) Then+                Begin+                    If Net <> '' Then L.NetName := Net;+                    If Param(Lines, 'pullbackUm') <> '' Then L.PullBackDistance := MilsToCoord(StrToIntDef(Param(Lines, 'pullbackUm'), 500) / 25.4);+                End;+            End;+            L := LS.NextLayer(L);+        End;+    End;+    PCBServer.PostProcess;+    Board.ViewManager_UpdateLayerTabs;+    Board.ViewManager_FullUpdate;+    Result := CmdLayerStack(Lines);+End;++{ layers: the fixed layer set with names and visibility (stackup detail is a later verb) }+Function CmdLayers(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    S : String;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    S := LayerJson(Board, eTopLayer, 'signal') + ',' + LayerJson(Board, eBottomLayer, 'signal') + ',' ++        LayerJson(Board, eTopOverlay, 'overlay') + ',' + LayerJson(Board, eBottomOverlay, 'overlay') + ',' ++        LayerJson(Board, eTopPaste, 'paste') + ',' + LayerJson(Board, eBottomPaste, 'paste') + ',' ++        LayerJson(Board, eTopSolder, 'solder') + ',' + LayerJson(Board, eBottomSolder, 'solder') + ',' ++        LayerJson(Board, eKeepOutLayer, 'keepout') + ',' + LayerJson(Board, eMultiLayer, 'multi') + ',' ++        LayerJson(Board, eDrillGuide, 'drill') + ',' + LayerJson(Board, eDrillDrawing, 'drill') + ',' ++        LayerJson(Board, eMechanical1, 'mechanical') + ',' + LayerJson(Board, eMechanical2, 'mechanical') + ',' ++        LayerJson(Board, eMechanical3, 'mechanical') + ',' + LayerJson(Board, eMechanical4, 'mechanical') + ',' ++        LayerJson(Board, eMechanical5, 'mechanical') + ',' + LayerJson(Board, eMechanical6, 'mechanical') + ',' ++        LayerJson(Board, eMechanical7, 'mechanical') + ',' + LayerJson(Board, eMechanical8, 'mechanical') + ',' ++        LayerJson(Board, eMechanical9, 'mechanical') + ',' + LayerJson(Board, eMechanical10, 'mechanical') + ',' ++        LayerJson(Board, eMechanical11, 'mechanical') + ',' + LayerJson(Board, eMechanical12, 'mechanical') + ',' ++        LayerJson(Board, eMechanical13, 'mechanical') + ',' + LayerJson(Board, eMechanical14, 'mechanical') + ',' ++        LayerJson(Board, eMechanical15, 'mechanical') + ',' + LayerJson(Board, eMechanical16, 'mechanical');+    Result := '{' + JRaw('ok','true') + ',' + JRaw('layers', '[' + S + ']') + '}';+End;+++{ ---- Phase 1 batch 2 ---------------------------------------------------- }+{ net_connections: every pad on the board with its net and owner, plus per-net span.+  net=<name> filters to one net. limit caps pads (default 4000). }+Function CmdNetConnections(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    BIt   : IPCB_BoardIterator;+    Pad   : IPCB_Pad;+    Want, S, N, Own, Spans, V : String;+    K, Limit, X, Y, I : Integer;+    SL    : TStringList;+    P     : Integer;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    Want := Param(Lines, 'net');+    Limit := StrToIntDef(Param(Lines, 'limit'), 4000);+    SL := TStringList.Create;+    S := ''; K := 0;+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(ePadObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Pad := BIt.FirstPCBObject;+    While (Pad <> Nil) And (K < Limit) Do+    Begin+        If Pad.Net <> Nil Then+        Begin+            N := Pad.Net.Name;+            If (Want = '') Or (UpperCase(N) = UpperCase(Want)) Then+            Begin+                Own := '';+                If Pad.Component <> Nil Then Own := Pad.Component.Name.Text;+                X := Round(CoordToMils(Pad.x)); Y := Round(CoordToMils(Pad.y));+                If S <> '' Then S := S + ',';+                S := S + '{' + JStr('net', N) + ',' + JStr('pad', Own + '.' + Pad.Name) + ',' ++                    JRaw('xMil', IntToStr(X)) + ',' + JRaw('yMil', IntToStr(Y)) + '}';+                K := K + 1;+                V := SL.Values[N];+                If V = '' Then SL.Values[N] := IntToStr(X) + '|' + IntToStr(Y) + '|' + IntToStr(X) + '|' + IntToStr(Y) + '|1'+                Else+                Begin+                    P := Pos('|', V); Spans := Copy(V, 1, P - 1); V := Copy(V, P + 1, Length(V));+                    I := StrToIntDef(Spans, X); If X < I Then I := X; Spans := IntToStr(I);+                    P := Pos('|', V); N := Copy(V, 1, P - 1); V := Copy(V, P + 1, Length(V));+                    I := StrToIntDef(N, Y); If Y < I Then I := Y; Spans := Spans + '|' + IntToStr(I);+                    P := Pos('|', V); N := Copy(V, 1, P - 1); V := Copy(V, P + 1, Length(V));+                    I := StrToIntDef(N, X); If X > I Then I := X; Spans := Spans + '|' + IntToStr(I);+                    P := Pos('|', V); N := Copy(V, 1, P - 1); V := Copy(V, P + 1, Length(V));+                    I := StrToIntDef(N, Y); If Y > I Then I := Y; Spans := Spans + '|' + IntToStr(I);+                    I := StrToIntDef(V, 0) + 1; Spans := Spans + '|' + IntToStr(I);+                    SL.Values[Pad.Net.Name] := Spans;+                End;+            End;+        End;+        Pad := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+    Spans := '';+    For I := 0 To SL.Count - 1 Do+    Begin+        If Spans <> '' Then Spans := Spans + ',';+        Spans := Spans + '{' + JStr('net', SL.Names[I]) + ',' + JStr('minXminYmaxXmaxYpads', SL.Values[SL.Names[I]]) + '}';+    End;+    SL.Free;+    Result := '{' + JRaw('ok','true') + ',' + JRaw('count', IntToStr(K)) + ',' + JRaw('pads', '[' + S + ']') + ',' + JRaw('nets', '[' + Spans + ']') + '}';+End;++{ schematic_data: components on the CURRENT schematic sheet with pins.+  designator=<one> filters. pins=0 skips pin detail. limit caps components. }+Function CmdSchematicData(Lines : TStringList) : String;+Var+    Doc  : ISch_Document;+    Iter : ISch_Iterator;+    PIt  : ISch_Iterator;+    Comp : ISch_Component;+    Pin  : ISch_Pin;+    Prm  : ISch_Parameter;+    QIt  : ISch_Iterator;+    Want, S, PS, MS, PF, PV, Desc : String;+    N, Limit, PN, T0, Budget : Integer;+    WantPins, WantParams, Late : Boolean;+    Skipped : String;+Begin+    Doc := SchServer.GetCurrentSchDocument;+    If Doc = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_schematic') + ',' + JStr('error','no current schematic document (open a .SchDoc first)') + '}'; Exit; End;+    { a schematic LIBRARY is an ISch_Document too: read as a sheet it answered ok with 0 parts (#21) }+    If Doc.ObjectID = eSchLib Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_schematic') + ',' + JStr('error','the current document is a schematic library, not a sheet: ' + Doc.DocumentName) + '}'; Exit; End;+    Want := Param(Lines, 'designator');+    Limit := StrToIntDef(Param(Lines, 'limit'), 500);+    WantPins := Param(Lines, 'pins') <> '0';+    WantParams := Param(Lines, 'params') = '1';+    PF := UpperCase(Param(Lines, 'paramFilter'));+    { #28: a 6-part sheet's parameters ran out the whole 120 s on one machine and were never reproduced. A budget for+      parameters and descriptions: once spent, the remaining parts come back without them and are named in paramsSkipped,+      so the bridge fills them from the saved file instead of the call timing out }+    Budget := StrToIntDef(Param(Lines, 'paramBudgetMs'), 60000);+    T0 := GetTickCount;+    Skipped := '';+    S := ''; N := 0;+    Iter := Doc.SchIterator_Create;+    Iter.AddFilter_ObjectSet(MkSet(eSchComponent));+    Comp := Iter.FirstSchObject;+    While (Comp <> Nil) And (N < Limit) Do+    Begin+        If (Want = '') Or (UpperCase(Comp.Designator.Text) = UpperCase(Want)) Then+        Begin+            PS := '';+            If WantPins Then+            Begin+                PIt := Comp.SchIterator_Create;+                PIt.AddFilter_ObjectSet(MkSet(ePin));+                Pin := PIt.FirstSchObject;+                While Pin <> Nil Do+                Begin+                    { a placed part carries every part's and display mode's pins: list the ones it shows (its part,+                      the shared part 0, its display mode) }+                    If ((Pin.OwnerPartId = Comp.CurrentPartID) Or (Pin.OwnerPartId = 0)) And (Pin.OwnerPartDisplayMode = Comp.DisplayMode) Then+                    Begin+                        If PS <> '' Then PS := PS + ',';+                        PS := PS + '{' + JStr('number', Pin.Designator) + ',' + JStr('name', Pin.Name) + ',' ++                            JRaw('electrical', IntToStr(Pin.Electrical)) + ',' ++                            JRaw('xMil', IntToStr(Round(CoordToMils(Pin.Location.X)))) + ',' + JRaw('yMil', IntToStr(Round(CoordToMils(Pin.Location.Y)))) + ',' ++                            JRaw('orientation', IntToStr(Pin.Orientation)) + ',' + JRaw('lengthMil', IntToStr(Round(CoordToMils(Pin.PinLength)))) + ',' ++                            JRaw('part', IntToStr(Pin.OwnerPartId)) + ',' + JRaw('hidden', JBool(Pin.IsHidden)) + ',' + JStr('hiddenNet', Pin.HiddenNetName) + '}';+                    End;+                    Pin := PIt.NextSchObject;+                End;+                Comp.SchIterator_Destroy(PIt);+            End;+            MS := '';+            Late := GetTickCount - T0 > Budget;+            If WantParams And Late Then+            Begin+                If Skipped <> '' Then Skipped := Skipped + ',';+                Skipped := Skipped + '"' + JsonEsc(Comp.Designator.Text) + '"';+            End;+            If WantParams And (Not Late) Then+            Begin+                PN := 0;+                QIt := Comp.SchIterator_Create;+                QIt.AddFilter_ObjectSet(MkSet(eParameter));+                Prm := QIt.FirstSchObject;+                While (Prm <> Nil) And (PN < 40) And (GetTickCount - T0 <= Budget) Do+                Begin+                    If MS <> '' Then MS := MS + ',';+                    { paramFilter=|NAME|NAME| reads only those values: an example IC's three 'Available Preview Images'+                      parameters held so much that reading them took minutes (2026-10-21). Long values are cut. }+                    If (Param(Lines, 'paramNamesOnly') = '1') Or ((PF <> '') And (Pos('|' + UpperCase(Prm.Name) + '|', PF) = 0)) Then+                        MS := MS + '{' + JStr('name', Prm.Name) + '}'+                    Else+                    Begin+                        PV := Prm.Text;+                        If Length(PV) > 2000 Then+                            MS := MS + '{' + JStr('name', Prm.Name) + ',' + JStr('value', Copy(PV, 1, 2000)) + ',' + JRaw('truncatedFrom', IntToStr(Length(PV))) + '}'+                        Else+                            MS := MS + '{' + JStr('name', Prm.Name) + ',' + JStr('value', PV) + '}';+                    End;+                    PN := PN + 1;+                    Prm := QIt.NextSchObject;+                End;+                Comp.SchIterator_Destroy(QIt);+            End;+            { a workspace-managed part's description is fetched from its vault: on the Bluetooth Sentinel+              example's Altium Content Vault parts that call never returned (2026-10-21). Their item GUID says+              where they live; the description of an unmanaged part is read as before. }+            If (Comp.ItemGUID = '') And (Not Late) Then Desc := Comp.ComponentDescription Else Desc := '';+            If S <> '' Then S := S + ',';+            S := S + '{' + JRaw('params', '[' + MS + ']') + ',' + JStr('designator', Comp.Designator.Text) + ',' ++                JRaw('partId', IntToStr(Comp.CurrentPartID)) + ',' ++                JRaw('partCount', IntToStr(Comp.PartCount)) + ',' ++                JStr('libReference', Comp.LibReference) + ',' ++                JStr('comment', Comp.Comment.Text) + ',' ++                JStr('description', Desc) + ',' ++                JStr('uniqueId', Comp.UniqueId) + ',' ++                JStr('itemGuid', Comp.ItemGUID) + ',' ++                JStr('vaultGuid', Comp.VaultGUID) + ',' ++                JStr('sourceLibrary', Comp.SourceLibraryName) + ',' ++                JRaw('kind', IntToStr(Comp.ComponentKind)) + ',' ++                JRaw('xMil', IntToStr(Round(CoordToMils(Comp.Location.X)))) + ',' ++                JRaw('yMil', IntToStr(Round(CoordToMils(Comp.Location.Y)))) + ',' ++                JRaw('orientation', IntToStr(Comp.Orientation)) + ',' ++                JRaw('pins', '[' + PS + ']') + '}';+            N := N + 1;+        End;+        Comp := Iter.NextSchObject;+    End;+    Doc.SchIterator_Destroy(Iter);+    Result := '{' + JRaw('ok','true') + ',' + JStr('document', Doc.DocumentName) + ',' + JRaw('count', IntToStr(N)) + ',' ++        JRaw('paramsSkipped', '[' + Skipped + ']') + ',' + JRaw('elapsedMs', IntToStr(GetTickCount - T0)) + ',' + JRaw('components', '[' + S + ']') + '}';+End;++{ sheet_params: set parameters on PLACED components of the CURRENT schematic sheet (the BOM write-back).+  designators=R1,R2 (every part of a multi-part component)  params=Key=Value;Key=Value+  A missing parameter is added hidden; an existing one gets the value (PreProcess/PostProcess around the edit;+  c_BroadcastBroadcast robot messages are not declared in AD 26's script engine). Save with altium_save. }+Function CmdSheetParams(Lines : TStringList) : String;+Var+    Doc  : ISch_Document;+    Iter : ISch_Iterator;+    Comp : ISch_Component;+    Want, Items : TStringList;+    Spec, K, V, Found, Missing : String;+    I, P, NComp, NSet : Integer;+Begin+    Doc := SchServer.GetCurrentSchDocument;+    If (Doc = Nil) Or (Doc.ObjectID = eSchLib) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_schematic') + ',' + JStr('error','no current schematic sheet (open a .SchDoc first)') + '}'; Exit; End;+    Want := TStringList.Create;+    Want.Delimiter := ',';+    Want.StrictDelimiter := True;+    Want.DelimitedText := UpperCase(Param(Lines, 'designators'));+    Items := SplitSemi(Param(Lines, 'params'));+    If (Want.Count = 0) Or (Items.Count = 0) Then+    Begin+        Want.Free; Items.Free;+        Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','bad_args') + ',' + JStr('error','designators and params are required') + '}';+        Exit;+    End;+    Found := ''; NComp := 0; NSet := 0;+    SchServer.ProcessControl.PreProcess(Doc, '');+    Iter := Doc.SchIterator_Create;+    Iter.AddFilter_ObjectSet(MkSet(eSchComponent));+    Comp := Iter.FirstSchObject;+    While Comp <> Nil Do+    Begin+        If Want.IndexOf(UpperCase(Comp.Designator.Text)) >= 0 Then+        Begin+            SchServer.RobotManager.SendMessage(Comp.I_ObjectAddress, c_BroadCast, SCHM_BeginModify, c_NoEventData);+            For I := 0 To Items.Count - 1 Do+            Begin+                Spec := Items[I];+                P := Pos('=', Spec);+                If P > 0 Then+                Begin+                    K := Trim(Copy(Spec, 1, P - 1));+                    V := Trim(Copy(Spec, P + 1, Length(Spec)));+                    If K <> '' Then+                    Begin+                        SetSchParam(Comp, K, V); NSet := NSet + 1;+                        { the part's own description is what the BOM, pick and place and the board read: a+                          'Description' parameter alone left them on the library's text (2026-10-22) }+                        If UpperCase(K) = 'DESCRIPTION' Then Comp.ComponentDescription := V;+                    End;+                End;+            End;+            SchServer.RobotManager.SendMessage(Comp.I_ObjectAddress, c_BroadCast, SCHM_EndModify, c_NoEventData);+            If Pos('"' + Comp.Designator.Text + '"', Found) = 0 Then+            Begin+                If Found <> '' Then Found := Found + ',';+                Found := Found + '"' + JsonEsc(Comp.Designator.Text) + '"';+            End;+            NComp := NComp + 1;+        End;+        Comp := Iter.NextSchObject;+    End;+    Doc.SchIterator_Destroy(Iter);+    SchServer.ProcessControl.PostProcess(Doc, '');+    MarkSchModified(Doc);+    Doc.GraphicallyInvalidate;+    Missing := '';+    For I := 0 To Want.Count - 1 Do+        If Pos('"' + UpperCase(Want[I]) + '"', UpperCase(Found)) = 0 Then+        Begin+            If Missing <> '' Then Missing := Missing + ',';+            Missing := Missing + '"' + JsonEsc(Want[I]) + '"';+        End;+    Want.Free; Items.Free;+    Result := '{' + JRaw('ok','true') + ',' + JStr('document', Doc.DocumentName) + ',' + JRaw('parts', IntToStr(NComp)) + ',' ++        JRaw('paramsSet', IntToStr(NSet)) + ',' + JRaw('designators', '[' + Found + ']') + ',' + JRaw('notOnSheet', '[' + Missing + ']') + '}';+End;++{ copper_layers: the full copper set (top, mid 1-30, planes 1-16, bottom) with names and visibility }+Function CmdCopperLayers(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    S : String;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    S := LayerJson(Board, eTopLayer, 'signal');+    S := S + ',' + LayerJson(Board, eMidLayer1, 'signal');+    S := S + ',' + LayerJson(Board, eMidLayer2, 'signal');+    S := S + ',' + LayerJson(Board, eMidLayer3, 'signal');+    S := S + ',' + LayerJson(Board, eMidLayer4, 'signal');+    S := S + ',' + LayerJson(Board, eMidLayer5, 'signal');+    S := S + ',' + LayerJson(Board, eMidLayer6, 'signal');+    S := S + ',' + LayerJson(Board, eMidLayer7, 'signal');+    S := S + ',' + LayerJson(Board, eMidLayer8, 'signal');+    S := S + ',' + LayerJson(Board, eMidLayer9, 'signal');+    S := S + ',' + LayerJson(Board, eMidLayer10, 'signal');+    S := S + ',' + LayerJson(Board, eMidLayer11, 'signal');+    S := S + ',' + LayerJson(Board, eMidLayer12, 'signal');+    S := S + ',' + LayerJson(Board, eMidLayer13, 'signal');+    S := S + ',' + LayerJson(Board, eMidLayer14, 'signal');+    S := S + ',' + LayerJson(Board, eMidLayer15, 'signal');+    S := S + ',' + LayerJson(Board, eMidLayer16, 'signal');+    S := S + ',' + LayerJson(Board, eMidLayer17, 'signal');+    S := S + ',' + LayerJson(Board, eMidLayer18, 'signal');+    S := S + ',' + LayerJson(Board, eMidLayer19, 'signal');+    S := S + ',' + LayerJson(Board, eMidLayer20, 'signal');+    S := S + ',' + LayerJson(Board, eMidLayer21, 'signal');+    S := S + ',' + LayerJson(Board, eMidLayer22, 'signal');+    S := S + ',' + LayerJson(Board, eMidLayer23, 'signal');+    S := S + ',' + LayerJson(Board, eMidLayer24, 'signal');+    S := S + ',' + LayerJson(Board, eMidLayer25, 'signal');+    S := S + ',' + LayerJson(Board, eMidLayer26, 'signal');+    S := S + ',' + LayerJson(Board, eMidLayer27, 'signal');+    S := S + ',' + LayerJson(Board, eMidLayer28, 'signal');+    S := S + ',' + LayerJson(Board, eMidLayer29, 'signal');+    S := S + ',' + LayerJson(Board, eMidLayer30, 'signal');+    S := S + ',' + LayerJson(Board, eInternalPlane1, 'plane');+    S := S + ',' + LayerJson(Board, eInternalPlane2, 'plane');+    S := S + ',' + LayerJson(Board, eInternalPlane3, 'plane');+    S := S + ',' + LayerJson(Board, eInternalPlane4, 'plane');+    S := S + ',' + LayerJson(Board, eInternalPlane5, 'plane');+    S := S + ',' + LayerJson(Board, eInternalPlane6, 'plane');+    S := S + ',' + LayerJson(Board, eInternalPlane7, 'plane');+    S := S + ',' + LayerJson(Board, eInternalPlane8, 'plane');+    S := S + ',' + LayerJson(Board, eInternalPlane9, 'plane');+    S := S + ',' + LayerJson(Board, eInternalPlane10, 'plane');+    S := S + ',' + LayerJson(Board, eInternalPlane11, 'plane');+    S := S + ',' + LayerJson(Board, eInternalPlane12, 'plane');+    S := S + ',' + LayerJson(Board, eInternalPlane13, 'plane');+    S := S + ',' + LayerJson(Board, eInternalPlane14, 'plane');+    S := S + ',' + LayerJson(Board, eInternalPlane15, 'plane');+    S := S + ',' + LayerJson(Board, eInternalPlane16, 'plane');+    S := S + ',' + LayerJson(Board, eBottomLayer, 'signal');+    Result := '{' + JRaw('ok','true') + ',' + JRaw('layers', '[' + S + ']') + '}';+End;+++{ ---- Phase 1 batch 3 ---------------------------------------------------- }+{ read_rules: every design rule on the board }+Function CmdReadRules(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    BIt   : IPCB_BoardIterator;+    Rule  : IPCB_Rule;+    S     : String;+    N     : Integer;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    S := ''; N := 0;+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eRuleObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Rule := BIt.FirstPCBObject;+    While Rule <> Nil Do+    Begin+        If S <> '' Then S := S + ',';+        S := S + '{' + JStr('name', Rule.Name) + ',' + JRaw('kind', IntToStr(Rule.RuleKind)) + ',' ++            JRaw('enabled', JBool(Rule.Enabled)) + ',' + JRaw('priority', IntToStr(Rule.Priority)) + ',' ++            JStr('scope1', Rule.Scope1Expression) + ',' + JStr('scope2', Rule.Scope2Expression) + ',' ++            JStr('comment', Rule.Comment) + ',' + JStr('descriptor', Rule.Descriptor) + '}';+        N := N + 1;+        Rule := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+    Result := '{' + JRaw('ok','true') + ',' + JRaw('count', IntToStr(N)) + ',' + JRaw('rules', '[' + S + ']') + '}';+End;++{ set_layer_visibility: layerId=<int> displayed=1|0 }+Function CmdSetLayerVisibility(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    L     : Integer;+    Show  : Boolean;+    LName : String;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    L := StrToIntDef(Param(Lines, 'layerId'), -1);+    If L < 0 Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','layerId (from altium_layers) is required') + '}'; Exit; End;+    Show := Param(Lines, 'displayed') <> '0';+    { an id that is not in THIS board's stack has a nil LayerObject: reading .Name off it halts the+      engine. Refuse by name instead (found by review, 2026-09-21). }+    If Board.LayerStack.LayerObject[L] = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','unknown_layer') + ',' + JStr('error','layerId ' + IntToStr(L) + ' is not in this board; altium_layers lists the valid ids') + '}'; Exit; End;+    LName := Board.LayerStack.LayerObject[L].Name;+    Board.LayerIsDisplayed[L] := Show;+    Board.ViewManager_UpdateLayerTabs;+    Board.ViewManager_FullUpdate;+    Result := '{' + JRaw('ok','true') + ',' + JRaw('layerId', IntToStr(L)) + ',' + JStr('name', LName) + ',' + JRaw('displayed', JBool(Board.LayerIsDisplayed[L])) + '}';+End;++{ project_variants: the variants Altium itself holds for an open project (path=<.PrjPcb>), each with its+  variations (designator, kind: 1 not fitted, 2 alternate). The bridge writes variants into the project+  file; this is the read-back that Altium took them. }+Function CmdProjectVariants(Lines : TStringList) : String;+Var+    WS   : IWorkspace;+    Prj  : IProject;+    V    : IProjectVariant;+    CV   : IComponentVariation;+    I, J : Integer;+    Want, S, M : String;+Begin+    Want := UpperCase(Param(Lines, 'path'));+    WS := GetWorkspace; Prj := Nil;+    If WS <> Nil Then+        For I := 0 To WS.DM_ProjectCount - 1 Do+            If UpperCase(WS.DM_Projects(I).DM_ProjectFullPath) = Want Then Prj := WS.DM_Projects(I);+    If Prj = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','project_not_open') + ',' + JStr('error','that project is not open in Altium') + '}'; Exit; End;+    S := '';+    For I := 0 To Prj.DM_ProjectVariantCount - 1 Do+    Begin+        V := Prj.DM_ProjectVariants(I);+        M := '';+        For J := 0 To V.DM_VariationCount - 1 Do+        Begin+            CV := V.DM_Variations(J);+            If M <> '' Then M := M + ',';+            M := M + '{' + JStr('designator', CV.DM_PhysicalDesignator) + ',' + JRaw('kind', IntToStr(CV.DM_VariationKind)) + '}';+        End;+        If S <> '' Then S := S + ',';+        S := S + '{' + JStr('name', V.DM_Description) + ',' + JRaw('variations', '[' + M + ']') + '}';+    End;+    Result := '{' + JRaw('ok','true') + ',' + JRaw('variants', '[' + S + ']') + '}';+End;++{ pcb_postprocess: close a board edit the script engine left open. A runtime error between PCBServer.PreProcess+  and PostProcess leaves Altium sure a command is still running: every save then asks 'A command is currently+  active and save cannot be completed' (measured 2026-10-22). The bridge sends this after such an error. }+Function CmdPcbPostProcess(Lines : TStringList) : String;+Begin+    PCBServer.PostProcess;+    Result := '{' + JRaw('ok','true') + '}';+End;++{ outline_tracks: the board shape drawn as tracks and arcs on a mechanical layer (mech=N, width raw), the+  outline a fab house reads from the Gerbers. A scripted board has its shape but nothing on any outline layer,+  so its GM1/GKO Gerbers came out empty (2026-10-22). op=check counts what the layer holds; op=draw draws+  unless the layer already has primitives (replace=1 draws anyway). }+Function CmdOutlineTracks(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    BO    : IPCB_BoardOutline;+    BIt   : IPCB_BoardIterator;+    Prim  : IPCB_Primitive;+    Seg, Nx : TPolySegment;+    Trk   : IPCB_Track;+    Arc   : IPCB_Arc;+    Lyr, W, K, Have, Drawn : Integer;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_board') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    Lyr := eMechanical1 + StrToIntDef(Param(Lines, 'mech'), 1) - 1;+    W := StrToIntDef(Param(Lines, 'width'), 39370);+    Have := 0;+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eTrackObject, eArcObject));+    BIt.AddFilter_LayerSet(MkSet(Lyr));+    BIt.AddFilter_Method(eProcessAll);+    Prim := BIt.FirstPCBObject;+    While Prim <> Nil Do Begin Have := Have + 1; Prim := BIt.NextPCBObject; End;+    Board.BoardIterator_Destroy(BIt);+    Drawn := 0;+    BO := Board.BoardOutline;+    If (Param(Lines, 'op') = 'draw') And (BO <> Nil) And ((Have = 0) Or (Param(Lines, 'replace') = '1')) Then+    Begin+        PCBServer.PreProcess;+        For K := 0 To BO.PointCount - 1 Do+        Begin+            Seg := BO.Segments[K];+            Nx := BO.Segments[(K + 1) Mod BO.PointCount];+            If Seg.Kind = ePolySegmentArc Then+            Begin+                Arc := PCBServer.PCBObjectFactory(eArcObject, eNoDimension, eCreate_Default);+                Arc.Layer := Lyr; Arc.XCenter := Seg.cx; Arc.YCenter := Seg.cy; Arc.Radius := Seg.Radius;+                Arc.LineWidth := W; Arc.StartAngle := Seg.Angle1; Arc.EndAngle := Seg.Angle2;+                Board.AddPCBObject(Arc);+                PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Arc.I_ObjectAddress);+            End+            Else+            Begin+                Trk := PCBServer.PCBObjectFactory(eTrackObject, eNoDimension, eCreate_Default);+                Trk.Layer := Lyr; Trk.X1 := Seg.vx; Trk.Y1 := Seg.vy; Trk.X2 := Nx.vx; Trk.Y2 := Nx.vy; Trk.Width := W;+                Board.AddPCBObject(Trk);+                PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Trk.I_ObjectAddress);+            End;+            Drawn := Drawn + 1;+        End;+        PCBServer.PostProcess;+        Board.ViewManager_FullUpdate;+    End;+    Result := '{' + JRaw('ok','true') + ',' + JStr('layer', Board.LayerStack.LayerObject[Lyr].Name) + ',' + JRaw('alreadyThere', IntToStr(Have)) + ',' ++        JRaw('drawn', IntToStr(Drawn)) + ',' + JRaw('outlineVertices', IntToStr(BO.PointCount)) + '}';+End;++{ track_width: set the width of every track and arc on the named nets (nets=A;B, width raw, layer=<V6 id> optional).+  Answers how many changed per net. }+Function CmdTrackWidth(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    BIt   : IPCB_BoardIterator;+    Prim  : IPCB_Primitive;+    Hits  : TInterfaceList;+    Want, Cnt : TStringList;+    W, Lyr, I : Integer;+    Nm, S : String;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_board') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    Want := TStringList.Create; Want.Delimiter := ';'; Want.StrictDelimiter := True; Want.DelimitedText := UpperCase(Param(Lines, 'nets'));+    Cnt := TStringList.Create;+    Hits := TInterfaceList.Create;+    W := StrToIntDef(Param(Lines, 'width'), 0);+    Lyr := StrToIntDef(Param(Lines, 'layer'), 0);+    { collect first, change after: a primitive that changes size moves in the board's spatial index, and changing it+      mid-iteration crashed Altium (2026-10-22) }+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eTrackObject, eArcObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Prim := BIt.FirstPCBObject;+    While Prim <> Nil Do+    Begin+        { routed copper only: a pour's own tracks (InPolygon) and a footprint's are left alone }+        If (Prim.Net <> Nil) And (Not Prim.InComponent) And (Not Prim.InPolygon) And ((Lyr = 0) Or (Prim.Layer = Lyr)) Then+            If Want.IndexOf(UpperCase(Prim.Net.Name)) >= 0 Then Hits.Add(Prim);+        Prim := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+    PCBServer.PreProcess;+    For I := 0 To Hits.Count - 1 Do+    Begin+        Prim := Hits[I];+        Nm := UpperCase(Prim.Net.Name);+        Prim.BeginModify;+        If Prim.ObjectId = eTrackObject Then Prim.Width := W Else Prim.LineWidth := W;+        Prim.EndModify;+        Cnt.Values[Nm] := IntToStr(StrToIntDef(Cnt.Values[Nm], 0) + 1);+    End;+    PCBServer.PostProcess;+    Board.ViewManager_FullUpdate;+    S := '';+    For I := 0 To Cnt.Count - 1 Do Begin If S <> '' Then S := S + ','; S := S + JRaw(Cnt.Names[I], Cnt.ValueFromIndex[I]); End;+    Result := '{' + JRaw('ok','true') + ',' + JRaw('changed', IntToStr(Hits.Count)) + ',' + JRaw('byNet', '{' + S + '}') + '}';+    Want.Free; Cnt.Free; Hits.Free;+End;++{ selection: what the user has selected in the CURRENT document. Board: components with position (raw, relative+  to the origin), rotation, side, footprint and extent. Sheet: components with location (mils) and orientation. }+Function CmdSelection(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    BIt   : IPCB_BoardIterator;+    Comp  : IPCB_Component;+    Doc   : ISch_Document;+    It    : ISch_Iterator;+    C     : ISch_Component;+    R     : TCoordRect;+    S, SetTo : String;+    Want  : TStringList;+Begin+    S := '';+    { set=A;B: select exactly these (everything else deselected) before reading }+    SetTo := UpperCase(Param(Lines, 'set'));+    Want := TStringList.Create; Want.Delimiter := ';'; Want.StrictDelimiter := True; Want.DelimitedText := SetTo;+    Board := PCBServer.GetCurrentPCBBoard;+    If Board <> Nil Then+    Begin+        BIt := Board.BoardIterator_Create;+        BIt.AddFilter_ObjectSet(MkSet(eComponentObject));+        BIt.AddFilter_LayerSet(AllLayers);+        BIt.AddFilter_Method(eProcessAll);+        Comp := BIt.FirstPCBObject;+        While Comp <> Nil Do+        Begin+            If SetTo <> '' Then Comp.Selected := Want.IndexOf(UpperCase(Comp.Name.Text)) >= 0;+            If Comp.Selected Then+            Begin+                R := Comp.BoundingRectangle;+                If S <> '' Then S := S + ',';+                S := S + '["' + JsonEsc(Comp.Name.Text) + '",' + IntToStr(Comp.x - Board.XOrigin) + ',' + IntToStr(Comp.y - Board.YOrigin) + ',' ++                    IntToStr(Round(Comp.Rotation * 1000)) + ',' + JBool(Comp.Layer = eBottomLayer) + ',"' + JsonEsc(Comp.Pattern) + '",' ++                    IntToStr(R.Right - R.Left) + ',' + IntToStr(R.Top - R.Bottom) + ']';+            End;+            Comp := BIt.NextPCBObject;+        End;+        Board.BoardIterator_Destroy(BIt);+        If SetTo <> '' Then Board.ViewManager_FullUpdate;+        Want.Free;+        Result := '{' + JRaw('ok','true') + ',' + JStr('kind','board') + ',' + JStr('document', Board.FileName) + ',' + JRaw('selected', '[' + S + ']') + '}';+        Exit;+    End;+    Doc := SchServer.GetCurrentSchDocument;+    If (Doc <> Nil) And (Doc.ObjectID <> eSchLib) Then+    Begin+        It := Doc.SchIterator_Create;+        It.AddFilter_ObjectSet(MkSet(eSchComponent));+        C := It.FirstSchObject;+        While C <> Nil Do+        Begin+            If SetTo <> '' Then C.Selection := Want.IndexOf(UpperCase(C.Designator.Text)) >= 0;+            If C.Selection Then+            Begin+                If S <> '' Then S := S + ',';+                S := S + '["' + JsonEsc(C.Designator.Text) + '",' + IntToStr(Round(CoordToMils(C.Location.X))) + ',' + IntToStr(Round(CoordToMils(C.Location.Y))) + ',' ++                    IntToStr(C.Orientation) + ',"' + JsonEsc(C.LibReference) + '","' + JsonEsc(C.Comment.Text) + '"]';+            End;+            C := It.NextSchObject;+        End;+        Doc.SchIterator_Destroy(It);+        If SetTo <> '' Then Doc.GraphicallyInvalidate;+        Want.Free;+        Result := '{' + JRaw('ok','true') + ',' + JStr('kind','sheet') + ',' + JStr('document', Doc.DocumentName) + ',' + JRaw('selected', '[' + S + ']') + '}';+        Exit;+    End;+    Want.Free;+    Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_document') + ',' + JStr('error','the current document is neither a board nor a schematic sheet') + '}';+End;++{ every copper layer of a width rule (top, mid 1-30, bottom): a rule set on top and bottom only left inner+  layers at Altium's default once a board had them }+Procedure SetWidthAllLayers(Rule : IPCB_Rule; A, B, C : Integer);+Var+    L : Integer;+Begin+    For L := eTopLayer To eBottomLayer Do+    Begin+        Rule.MinWidth[L] := A; Rule.FavoredWidth[L] := B; Rule.MaxWidth[L] := C;+    End;+End;++Function NetObjNamed(Board : IPCB_Board; Name : String) : IPCB_Net;+Var+    BIt : IPCB_BoardIterator;+    N   : IPCB_Net;+Begin+    Result := Nil;+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eNetObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    N := BIt.FirstPCBObject;+    While N <> Nil Do+    Begin+        If UpperCase(N.Name) = UpperCase(Name) Then Begin Result := N; Break; End;+        N := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+End;++{ net_classes: op=list | set (name, nets=A;B;C: replaces a class of that name) | remove (name).+  list answers every net class with its member nets; Altium's own 'All Nets' is flagged superClass. }+Function CmdNetClasses(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    BIt, NIt : IPCB_BoardIterator;+    Cls, Old : IPCB_ObjectClass;+    Net   : IPCB_Net;+    Op, Name, Nets, N, S, M, Missing : String;+    P, K  : Integer;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_board') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    Op := Param(Lines, 'op'); Name := Param(Lines, 'name');+    Old := Nil; S := '';+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eClassObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Cls := BIt.FirstPCBObject;+    While Cls <> Nil Do+    Begin+        If Cls.MemberKind = eClassMemberKind_Net Then+        Begin+            If (Name <> '') And (UpperCase(Cls.Name) = UpperCase(Name)) Then Old := Cls;+            If Op = 'list' Then+            Begin+                M := '';+                If Not Cls.SuperClass Then+                Begin+                    NIt := Board.BoardIterator_Create;+                    NIt.AddFilter_ObjectSet(MkSet(eNetObject));+                    NIt.AddFilter_LayerSet(AllLayers);+                    NIt.AddFilter_Method(eProcessAll);+                    Net := NIt.FirstPCBObject;+                    While Net <> Nil Do+                    Begin+                        If Cls.IsMember(Net) Then Begin If M <> '' Then M := M + ','; M := M + '"' + JsonEsc(Net.Name) + '"'; End;+                        Net := NIt.NextPCBObject;+                    End;+                    Board.BoardIterator_Destroy(NIt);+                End;+                If S <> '' Then S := S + ',';+                S := S + '{' + JStr('name', Cls.Name) + ',' + JRaw('superClass', JBool(Cls.SuperClass)) + ',' + JRaw('nets', '[' + M + ']') + '}';+            End;+        End;+        Cls := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+    If Op = 'list' Then Begin Result := '{' + JRaw('ok','true') + ',' + JRaw('classes', '[' + S + ']') + '}'; Exit; End;+    If Name = '' Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','bad_args') + ',' + JStr('error','name is required') + '}'; Exit; End;+    PCBServer.PreProcess;+    If Old <> Nil Then Board.RemovePCBObject(Old);+    If Op = 'remove' Then+    Begin+        PCBServer.PostProcess;+        Result := '{' + JRaw('ok','true') + ',' + JStr('class', Name) + ',' + JRaw('removed', JBool(Old <> Nil)) + '}';+        Exit;+    End;+    Cls := PCBServer.PCBClassFactoryByClassMember(eClassMemberKind_Net);+    Cls.Name := Name;+    Nets := Param(Lines, 'nets'); K := 0; Missing := '';+    While Nets <> '' Do+    Begin+        P := Pos(';', Nets);+        If P = 0 Then Begin N := Nets; Nets := ''; End+        Else Begin N := Copy(Nets, 1, P - 1); Nets := Copy(Nets, P + 1, Length(Nets)); End;+        If N <> '' Then+        Begin+            Net := NetObjNamed(Board, N);+            If Net = Nil Then Begin If Missing <> '' Then Missing := Missing + ','; Missing := Missing + '"' + JsonEsc(N) + '"'; End+            Else Begin Cls.AddMemberByName(Net.Name); K := K + 1; End;+        End;+    End;+    Board.AddPCBObject(Cls);+    PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Cls.I_ObjectAddress);+    PCBServer.PostProcess;+    Result := '{' + JRaw('ok','true') + ',' + JStr('class', Name) + ',' + JRaw('members', IntToStr(K)) + ',' + JRaw('replaced', JBool(Old <> Nil)) + ',' + JRaw('notOnBoard', '[' + Missing + ']') + '}';+End;++{ diff_pairs: op=list | set (name, pos, neg: board nets; replaces a pair of that name) | remove (name) }+Function CmdDiffPairs(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    BIt   : IPCB_BoardIterator;+    DP, Old : IPCB_DifferentialPair;+    PN, NN : IPCB_Net;+    Op, Name, S, PS, NS : String;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_board') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    Op := Param(Lines, 'op'); Name := Param(Lines, 'name');+    Old := Nil; S := '';+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eDifferentialPairObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    DP := BIt.FirstPCBObject;+    While DP <> Nil Do+    Begin+        If (Name <> '') And (UpperCase(DP.Name) = UpperCase(Name)) Then Old := DP;+        PS := ''; NS := '';+        If DP.PositiveNet <> Nil Then PS := DP.PositiveNet.Name;+        If DP.NegativeNet <> Nil Then NS := DP.NegativeNet.Name;+        If S <> '' Then S := S + ',';+        S := S + '{' + JStr('name', DP.Name) + ',' + JStr('pos', PS) + ',' + JStr('neg', NS) + '}';+        DP := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+    If Op = 'list' Then Begin Result := '{' + JRaw('ok','true') + ',' + JRaw('pairs', '[' + S + ']') + '}'; Exit; End;+    If Name = '' Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','bad_args') + ',' + JStr('error','name is required') + '}'; Exit; End;+    If Op = 'set' Then+    Begin+        PN := NetObjNamed(Board, Param(Lines, 'pos'));+        NN := NetObjNamed(Board, Param(Lines, 'neg'));+        If (PN = Nil) Or (NN = Nil) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','net_not_on_board') + ',' + JRaw('posFound', JBool(PN <> Nil)) + ',' + JRaw('negFound', JBool(NN <> Nil)) + '}'; Exit; End;+    End;+    PCBServer.PreProcess;+    If Old <> Nil Then Board.RemovePCBObject(Old);+    If Op = 'remove' Then+    Begin+        PCBServer.PostProcess;+        Result := '{' + JRaw('ok','true') + ',' + JStr('pair', Name) + ',' + JRaw('removed', JBool(Old <> Nil)) + '}';+        Exit;+    End;+    DP := PCBServer.PCBObjectFactory(eDifferentialPairObject, eNoDimension, eCreate_Default);+    DP.Name := Name;+    DP.PositiveNet := PN;+    DP.NegativeNet := NN;+    Board.AddPCBObject(DP);+    PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, DP.I_ObjectAddress);+    PCBServer.PostProcess;+    Result := '{' + JRaw('ok','true') + ',' + JStr('pair', Name) + ',' + JStr('pos', PN.Name) + ',' + JStr('neg', NN.Name) + ',' + JRaw('replaced', JBool(Old <> Nil)) + '}';+End;++{ footprint_primitives: designator=<board component> -> geometry dump (tracks, arcs, pads, texts, others counted) }+Function CmdFootprintPrimitives(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    Comp  : IPCB_Component;+    GIt   : IPCB_GroupIterator;+    Prim  : IPCB_Primitive;+    Trk   : IPCB_Track;+    Arc   : IPCB_Arc;+    Pad   : IPCB_Pad;+    Txt   : IPCB_Text;+    Des, S, Item : String;+    K, Other : Integer;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    Des := Param(Lines, 'designator');+    If Des = '' Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','designator is required') + '}'; Exit; End;+    Comp := FindCompByDes(Board, Des);+    If Comp = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','designator not found: ' + Des) + '}'; Exit; End;+    S := ''; K := 0; Other := 0;+    GIt := Comp.GroupIterator_Create;+    GIt.AddFilter_LayerSet(AllLayers);+    Prim := GIt.FirstPCBObject;+    While (Prim <> Nil) And (K < 2000) Do+    Begin+        Item := '';+        If Prim.ObjectId = eTrackObject Then+        Begin+            Trk := Prim;+            Item := '{' + JStr('kind','track') + ',' + JRaw('layerId', IntToStr(Trk.Layer)) + ',' ++                JRaw('x1', IntToStr(Round(CoordToMils(Trk.x1)))) + ',' + JRaw('y1', IntToStr(Round(CoordToMils(Trk.y1)))) + ',' ++                JRaw('x2', IntToStr(Round(CoordToMils(Trk.x2)))) + ',' + JRaw('y2', IntToStr(Round(CoordToMils(Trk.y2)))) + ',' ++                JRaw('wMil', IntToStr(Round(CoordToMils(Trk.Width)))) + '}';+        End+        Else If Prim.ObjectId = eArcObject Then+        Begin+            Arc := Prim;+            Item := '{' + JStr('kind','arc') + ',' + JRaw('layerId', IntToStr(Arc.Layer)) + ',' ++                JRaw('cx', IntToStr(Round(CoordToMils(Arc.XCenter)))) + ',' + JRaw('cy', IntToStr(Round(CoordToMils(Arc.YCenter)))) + ',' ++                JRaw('rMil', IntToStr(Round(CoordToMils(Arc.Radius)))) + ',' + JRaw('start', IntToStr(Round(Arc.StartAngle))) + ',' ++                JRaw('end', IntToStr(Round(Arc.EndAngle))) + ',' + JRaw('wMil', IntToStr(Round(CoordToMils(Arc.LineWidth)))) + '}';+        End+        Else If Prim.ObjectId = ePadObject Then+        Begin+            Pad := Prim;+            Item := '{' + JStr('kind','pad') + ',' + JStr('name', Pad.Name) + ',' + JRaw('layerId', IntToStr(Pad.Layer)) + ',' ++                JRaw('x', IntToStr(Round(CoordToMils(Pad.x)))) + ',' + JRaw('y', IntToStr(Round(CoordToMils(Pad.y)))) + ',' ++                JRaw('wMil', IntToStr(Round(CoordToMils(Pad.TopXSize)))) + ',' + JRaw('hMil', IntToStr(Round(CoordToMils(Pad.TopYSize)))) + ',' ++                JRaw('holeMil', IntToStr(Round(CoordToMils(Pad.HoleSize)))) + ',' ++                { micrometres as integers: FloatToStr writes a comma decimal on some Windows locales, which breaks JSON }+                JRaw('solderMaskExpUm', IntToStr(Round(CoordToMils(Pad.SolderMaskExpansion) * 25.4))) + ',' ++                JRaw('pasteMaskExpUm', IntToStr(Round(CoordToMils(Pad.PasteMaskExpansion) * 25.4))) + '}';+        End+        Else If Prim.ObjectId = eTextObject Then+        Begin+            Txt := Prim;+            Item := '{' + JStr('kind','text') + ',' + JRaw('layerId', IntToStr(Txt.Layer)) + ',' + JStr('text', Txt.Text) + ',' ++                JRaw('x', IntToStr(Round(CoordToMils(Txt.XLocation)))) + ',' + JRaw('y', IntToStr(Round(CoordToMils(Txt.YLocation)))) + '}';+        End+        Else Other := Other + 1;+        If Item <> '' Then+        Begin+            If S <> '' Then S := S + ',';+            S := S + Item;+            K := K + 1;+        End;+        Prim := GIt.NextPCBObject;+    End;+    Comp.GroupIterator_Destroy(GIt);+    Result := '{' + JRaw('ok','true') + ',' + JStr('designator', Comp.Name.Text) + ',' + JStr('pattern', Comp.Pattern) + ',' ++        JRaw('count', IntToStr(K)) + ',' + JRaw('otherPrimitives', IntToStr(Other)) + ',' + JRaw('primitives', '[' + S + ']') + '}';+End;++{ I/O bench: where does the ~300 ms per file write go? Times TStringList.SaveToFile against+  several paths (workspace / %TEMP% / Documents), overwrite vs new, and rename/delete. Uses only+  routines the agent already relies on, so it cannot introduce a new undeclared identifier. }+Function BenchOne(Path : String; Content : String) : String;+Var+    L : TStringList;+    T0, TNew, TOver, TDel : Integer;+Begin+    L := TStringList.Create;+    Try+        L.Text := Content;+        If FileExists(Path) Then DeleteFile(Path);+        T0 := GetTickCount; L.SaveToFile(Path); TNew := GetTickCount - T0;+        T0 := GetTickCount; L.SaveToFile(Path); TOver := GetTickCount - T0;+        T0 := GetTickCount; DeleteFile(Path); TDel := GetTickCount - T0;+    Finally+        L.Free;+    End;+    Result := '{' + JStr('path', Path) + ',' + JRaw('newMs', IntToStr(TNew)) + ',' + JRaw('overwriteMs', IntToStr(TOver)) + ',' + JRaw('deleteMs', IntToStr(TDel)) + '}';+End;++Function CmdIoBench(Lines : TStringList) : String;+Var+    Big, Small, TmpDir, Docs : String;+    K, T0, TRen, TSleep, TLoop, X : Integer;+    L : TStringList;+Begin+    Small := 'payload 0123456789 abcdefghij';+    Big := '';+    For K := 1 To 200 Do Big := Big + 'line ' + IntToStr(K) + ' 0123456789 abcdefghij 0123456789 abcdefghij' + #13#10;+    { derive %TEMP% and Documents from WORKSPACE (…\AppData\Local\Adom Bridge\…) - no new identifiers }+    TmpDir := Copy(WORKSPACE, 1, Pos('\Adom Bridge\', WORKSPACE)) + 'Temp';+    Docs := Copy(WORKSPACE, 1, Pos('\AppData\', WORKSPACE)) + 'Documents';+    { rename cost on an existing small file }+    L := TStringList.Create;+    Try+        L.Text := Small;+        L.SaveToFile(WORKSPACE + 'bench_ren.tmp');+    Finally+        L.Free;+    End;+    T0 := GetTickCount; RenameFile(WORKSPACE + 'bench_ren.tmp', WORKSPACE + 'bench_ren.txt'); TRen := GetTickCount - T0;+    DeleteFile(WORKSPACE + 'bench_ren.txt');+    T0 := GetTickCount; Sleep(1); TSleep := GetTickCount - T0;+    T0 := GetTickCount; For K := 1 To 1000 Do X := K * 2; TLoop := GetTickCount - T0;+    Result := '{' + JRaw('ok', 'true')+        + ',' + JRaw('wsSmall', BenchOne(WORKSPACE + 'bench_a.txt', Small))+        + ',' + JRaw('wsSmallTmpExt', BenchOne(WORKSPACE + 'bench_b.tmp', Small))+        + ',' + JRaw('wsBig', BenchOne(WORKSPACE + 'bench_c.json', Big))+        + ',' + JRaw('tempSmall', BenchOne(TmpDir + '\adom_bench_d.txt', Small))+        + ',' + JRaw('docsSmall', BenchOne(Docs + '\adom_bench_e.txt', Small))+        + ',' + JRaw('renameMs', IntToStr(TRen))+        + ',' + JRaw('sleep1Ms', IntToStr(TSleep))+        + ',' + JRaw('loop1000Ms', IntToStr(TLoop))+        + '}';+End;++{ Which copper layers actually CARRY copper. The layer stack's "is this layer in the stack" flag has+  no identifier we have proven on this Altium build, and guessing one halts the engine behind a modal+  on the user's screen — so instead of asking the stack, count primitives. ONE pass over pads, tracks+  and arcs across all layers, tallied by Prim.Layer; every identifier here is already used elsewhere+  in this unit. An empty inner plane reports zero, which is why the caller must read this as+  "layers with copper on them", never as "the board's layer count". }+Function CmdLayerOccupancy(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    BIt   : IPCB_BoardIterator;+    Prim  : IPCB_Primitive;+    SL    : TStringList;+    S, K  : String;+    I, C, N : Integer;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}';+        Exit;+    End;+    SL := TStringList.Create;+    N := 0;+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(ePadObject, eTrackObject, eArcObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Prim := BIt.FirstPCBObject;+    While Prim <> Nil Do+    Begin+        K := IntToStr(Prim.Layer);+        C := StrToIntDef(SL.Values[K], 0);+        SL.Values[K] := IntToStr(C + 1);+        N := N + 1;+        Prim := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+    S := '';+    For I := 0 To SL.Count - 1 Do+    Begin+        If S <> '' Then S := S + ',';+        K := SL.Names[I];+        S := S + '{' + JRaw('layerId', K) + ',' ++            JStr('name', Board.LayerStack.LayerObject[StrToIntDef(K, 1)].Name) + ',' ++            JRaw('primitives', SL.Values[K]) + '}';+    End;+    SL.Free;+    Result := '{' + JRaw('ok','true') + ',' + JRaw('primitives', IntToStr(N)) + ',' + JRaw('occupancy', '[' + S + ']') + '}';+End;++{ The schematic-side companion to read_board: ONE round trip for the current sheet. Composed from+  the existing reader (same rule — no new Altium API surface, so it cannot introduce an+  engine-halting identifier). A hierarchical PARENT sheet holds sheet symbols, not parts, so this+  reports what the CURRENT sheet contains; the caller activates a leaf sheet first. }+Function CmdReadSchematic(Lines : TStringList) : String;+Var+    Doc   : ISch_Document;+    Sects : String;+    S     : String;+Begin+    Doc := SchServer.GetCurrentSchDocument;+    If Doc = Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current schematic document (open a .SchDoc first)') + '}';+        Exit;+    End;+    Sects := Param(Lines, 'sections');+    If Sects = '' Then Sects := 'components';+    S := '';+    If Pos('components', Sects) > 0 Then S := S + ',' + JRaw('components', CmdSchematicData(Lines));+    Result := '{' + JRaw('ok','true') + ',' + JStr('document', Doc.DocumentName) + ',' + JStr('sections', Sects) + S + '}';+End;++{ The board's extent, for the checks that need to know where the edge is. Uses the SAME guarded+  pattern the library reader already relies on: a library's internal board has a NIL BoardOutline and+  touching BoundingRectangle on it halts the engine (measured 2026-08-30). Reports which source it+  used, because the outline's bounding box and the board's bounding box are not the same claim. }+Function CmdReadOutline(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    R     : TCoordRect;+    Src   : String;+    Seg   : TPolySegment;+    K, L, B, RR, T : Integer;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}';+        Exit;+    End;+    If Board.BoardOutline <> Nil Then+    Begin+        { vertices, not the cached BoundingRectangle (see OutlineMilFromVertices) }+        L := 0; B := 0; RR := 0; T := 0;+        For K := 0 To Board.BoardOutline.PointCount - 1 Do+        Begin+            Seg := Board.BoardOutline.Segments[K];+            If (K = 0) Or (Seg.vx < L) Then L := Seg.vx;+            If (K = 0) Or (Seg.vy < B) Then B := Seg.vy;+            If (K = 0) Or (Seg.vx > RR) Then RR := Seg.vx;+            If (K = 0) Or (Seg.vy > T) Then T := Seg.vy;+        End;+        Src := 'boardOutline';+    End+    Else+    Begin+        R := Board.BoundingRectangle;+        L := R.Left; B := R.Bottom; RR := R.Right; T := R.Top;+        Src := 'boardBounds';+    End;+    Result := '{' + JRaw('ok','true') + ',' + JStr('source', Src) + ',' ++        JRaw('leftMil', IntToStr(Round(CoordToMils(L)))) + ',' ++        JRaw('bottomMil', IntToStr(Round(CoordToMils(B)))) + ',' ++        JRaw('rightMil', IntToStr(Round(CoordToMils(RR)))) + ',' ++        JRaw('topMil', IntToStr(Round(CoordToMils(T)))) + ',' ++        JRaw('widthMil', IntToStr(Round(CoordToMils(RR - L)))) + ',' ++        JRaw('heightMil', IntToStr(Round(CoordToMils(T - B)))) + '}';+End;++{ ONE round trip to READ the whole board. Composed from the EXISTING readers on purpose: no new Altium+  API surface, so it cannot introduce an undeclared identifier (which halts the engine behind a+  modal). The bridge shapes these into typed views; recorded dumps are the unit-test fixtures. }+Function CmdReadBoard(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    Sects, S : String;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}';+        Exit;+    End;+    Sects := Param(Lines, 'sections');+    If Sects = '' Then Sects := 'components,nets,layers,copper,rules,occupancy,outline';+    S := '';+    If Pos('components', Sects) > 0 Then S := S + ',' + JRaw('components', CmdComponentData(Lines));+    If Pos('nets', Sects) > 0 Then S := S + ',' + JRaw('nets', CmdListNets(Lines));+    If Pos('layers', Sects) > 0 Then S := S + ',' + JRaw('layers', CmdLayers(Lines));+    If Pos('copper', Sects) > 0 Then S := S + ',' + JRaw('copper', CmdCopperLayers(Lines));+    If Pos('rules', Sects) > 0 Then S := S + ',' + JRaw('rules', CmdReadRules(Lines));+    If Pos('occupancy', Sects) > 0 Then S := S + ',' + JRaw('occupancy', CmdLayerOccupancy(Lines));+    If Pos('outline', Sects) > 0 Then S := S + ',' + JRaw('outline', CmdReadOutline(Lines));+    Result := '{' + JRaw('ok','true') + ',' + JStr('board', Board.FileName) + ',' + JStr('sections', Sects) + S + '}';+End;++{ routing_state: the whole live board as data for AI Flow's Altium lane. Every coordinate is the+  RAW internal unit (an integer; 10000 per mil), so nothing is rounded and no decimal is+  formatted in the user's locale. Rotations and angles are in thousandths of a degree. The+  bridge converts to mm and hashes the dump into a revision. Arrays, not objects, keep a board+  with thousands of tracks small:+    tracks   [x1,y1,x2,y2,width,layer,"net"]+    arcs     [cx,cy,radius,startMilliDeg,endMilliDeg,width,layer,"net"]+    vias     [x,y,size,hole,lowLayer,highLayer,"net"]+    pads     ["comp","name",x,y,xSize,ySize,rotMilliDeg,layer,hole,"net",shape]+    comps    ["designator",x,y,rotMilliDeg,layer,"pattern",bodies]+    polys    ["net",layer,[[x,y],...]]+    outline  [[x,y,kind,cx,cy,radius,angle1MilliDeg,angle2MilliDeg],...]  kind 1 = an arc to the next vertex+    unrouted the ratsnest connection count (connection objects on the board) }+{ The list as a JSON array. S := S + item copied the whole growing string for every item: quadratic, about 70 s for+  the Bluetooth Sentinel's 9381 tracks (2026-10-29). Each item takes its own comma and TStringList joins them in one+  pass (its line breaks are JSON whitespace). The list is spent: every caller frees it after. }+Function JoinList(L : TStringList) : String;+Var+    I : Integer;+Begin+    For I := 0 To L.Count - 2 Do L[I] := L[I] + ',';+    Result := '[' + L.Text + ']';+End;++Function NetOf(Prim : IPCB_Primitive) : String;+Begin+    Result := '""';+    If Prim.Net <> Nil Then Result := '"' + JsonEsc(Prim.Net.Name) + '"';+End;++{ A polygon's outline vertices for the board model: [x,y,0] for a line segment, [x,y,1,cx,cy,r,a1*1000,a2*1000] for an+  arc segment, as the board outline sends them. Only the vertices went before, so a round pour came back as 3 or 4+  corners (the Bluetooth Sentinel's GND pours, 2026-10-28). }+Function PolySegPts(Poly : IPCB_Polygon) : String;+Var+    Seg : TPolySegment;+    K   : Integer;+Begin+    Result := '';+    For K := 0 To Poly.PointCount - 1 Do+    Begin+        Seg := Poly.Segments[K];+        If K > 0 Then Result := Result + ',';+        If Seg.Kind = ePolySegmentArc Then+            Result := Result + '[' + IntToStr(Seg.vx) + ',' + IntToStr(Seg.vy) + ',1,' + IntToStr(Seg.cx) + ',' + IntToStr(Seg.cy) + ',' ++                IntToStr(Seg.Radius) + ',' + IntToStr(Round(Seg.Angle1 * 1000)) + ',' + IntToStr(Round(Seg.Angle2 * 1000)) + ']'+        Else+            Result := Result + '[' + IntToStr(Seg.vx) + ',' + IntToStr(Seg.vy) + ',0]';+    End;+End;++Function CmdRoutingState(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    BIt   : IPCB_BoardIterator;+    Prim  : IPCB_Primitive;+    Trk   : IPCB_Track;+    Arc   : IPCB_Arc;+    Via   : IPCB_Via;+    Pad   : IPCB_Pad;+    Comp  : IPCB_Component;+    Poly  : IPCB_Polygon;+    BO    : IPCB_BoardOutline;+    Seg   : TPolySegment;+    LT, LA, LV, LP, LC, LG, LO : TStringList;+    Own, Pts : String;+    K, Unrouted : Integer;+    PR    : TCoordRect;+    PX, PY, PW, PH, PRot, PS : Integer;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    LT := TStringList.Create; LA := TStringList.Create; LV := TStringList.Create; LP := TStringList.Create;+    LC := TStringList.Create; LG := TStringList.Create; LO := TStringList.Create;+    Unrouted := 0;+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eTrackObject, eArcObject, eViaObject, ePadObject, eComponentObject, ePolyObject, eConnectionObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Prim := BIt.FirstPCBObject;+    While Prim <> Nil Do+    Begin+        If Prim.ObjectId = eTrackObject Then+        Begin+            Trk := Prim;+            If Not Trk.InComponent Then+                LT.Add('[' + IntToStr(Trk.x1) + ',' + IntToStr(Trk.y1) + ',' + IntToStr(Trk.x2) + ',' + IntToStr(Trk.y2) + ',' ++                    IntToStr(Trk.Width) + ',' + IntToStr(Trk.Layer) + ',' + NetOf(Trk) + ']');+        End+        Else If Prim.ObjectId = eArcObject Then+        Begin+            Arc := Prim;+            If Not Arc.InComponent Then+                LA.Add('[' + IntToStr(Arc.XCenter) + ',' + IntToStr(Arc.YCenter) + ',' + IntToStr(Arc.Radius) + ',' ++                    IntToStr(Round(Arc.StartAngle * 1000)) + ',' + IntToStr(Round(Arc.EndAngle * 1000)) + ',' ++                    IntToStr(Arc.LineWidth) + ',' + IntToStr(Arc.Layer) + ',' + NetOf(Arc) + ']');+        End+        Else If Prim.ObjectId = eViaObject Then+        Begin+            Via := Prim;+            LV.Add('[' + IntToStr(Via.x) + ',' + IntToStr(Via.y) + ',' + IntToStr(Via.Size) + ',' + IntToStr(Via.HoleSize) + ',' ++                IntToStr(Via.LowLayer) + ',' + IntToStr(Via.HighLayer) + ',' + NetOf(Via) + ']');+        End+        Else If Prim.ObjectId = ePadObject Then+        Begin+            Pad := Prim;+            Own := '';+            If Pad.Component <> Nil Then Own := Pad.Component.Name.Text;+            PX := Pad.x; PY := Pad.y; PW := Pad.TopXSize; PH := Pad.TopYSize; PRot := Round(Pad.Rotation * 1000); PS := Pad.TopShape;+            If (PW = 0) Or (PH = 0) Then+            Begin+                { a custom-shape pad has no size: its copper is its bounding rectangle (the Bluetooth Sentinel coin cell+                  holder's 5 mm contacts read 0 x 0, and a via fence put a via on one, 2026-10-23) }+                PR := Pad.BoundingRectangle;+                PX := (PR.Left + PR.Right) Div 2; PY := (PR.Bottom + PR.Top) Div 2;+                PW := PR.Right - PR.Left; PH := PR.Top - PR.Bottom; PRot := 0; PS := eRectangular;+            End;+            LP.Add('["' + JsonEsc(Own) + '","' + JsonEsc(Pad.Name) + '",' + IntToStr(PX) + ',' + IntToStr(PY) + ',' ++                IntToStr(PW) + ',' + IntToStr(PH) + ',' + IntToStr(PRot) + ',' ++                IntToStr(Pad.Layer) + ',' + IntToStr(Pad.HoleSize) + ',' + NetOf(Pad) + ',' + IntToStr(PS) + ']');+        End+        Else If Prim.ObjectId = eComponentObject Then+        Begin+            Comp := Prim;+            Own := '';+            Try Own := Comp.SourceFootprintLibrary; Except Own := ''; End;+            LC.Add('["' + JsonEsc(Comp.Name.Text) + '",' + IntToStr(Comp.x) + ',' + IntToStr(Comp.y) + ',' ++                IntToStr(Round(Comp.Rotation * 1000)) + ',' + IntToStr(Comp.Layer) + ',"' + JsonEsc(Comp.Pattern) + '",' ++                IntToStr(GroupCount(Comp, eComponentBodyObject)) + ',"' + JsonEsc(Own) + '"]');+        End+        Else If Prim.ObjectId = ePolyObject Then+        Begin+            Poly := Prim;+            LG.Add('[' + NetOf(Poly) + ',' + IntToStr(Poly.Layer) + ',[' + PolySegPts(Poly) + ']]');+        End+        Else If Prim.ObjectId = eConnectionObject Then+            Unrouted := Unrouted + 1;+        Prim := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+    BO := Board.BoardOutline;+    If BO <> Nil Then+        For K := 0 To BO.PointCount - 1 Do+        Begin+            Seg := BO.Segments[K];+            If Seg.Kind = ePolySegmentArc Then+                LO.Add('[' + IntToStr(Seg.vx) + ',' + IntToStr(Seg.vy) + ',1,' + IntToStr(Seg.cx) + ',' + IntToStr(Seg.cy) + ',' ++                    IntToStr(Seg.Radius) + ',' + IntToStr(Round(Seg.Angle1 * 1000)) + ',' + IntToStr(Round(Seg.Angle2 * 1000)) + ']')+            Else+                LO.Add('[' + IntToStr(Seg.vx) + ',' + IntToStr(Seg.vy) + ',0]');+        End;+    Result := '{' + JRaw('ok','true') + ',' + JStr('file', Board.FileName) + ',' + JRaw('coordPerMil','10000') + ',' ++        JRaw('tracks', JoinList(LT)) + ',' + JRaw('arcs', JoinList(LA)) + ',' + JRaw('vias', JoinList(LV)) + ',' ++        JRaw('pads', JoinList(LP)) + ',' + JRaw('comps', JoinList(LC)) + ',' + JRaw('polys', JoinList(LG)) + ',' ++        JRaw('outline', JoinList(LO)) + ',' + JRaw('unrouted', IntToStr(Unrouted)) + '}';+    LT.Free; LA.Free; LV.Free; LP.Free; LC.Free; LG.Free; LO.Free;+End;++{ edit_board: a batch of copper and placement edits in ONE PreProcess/PostProcess (one undo+  step), for AI Flow's Altium lane. The payload is one op per line, '|'-separated, coordinates+  in RAW internal units (10000 per mil), rotation in thousandths of a degree:+    T|x1|y1|x2|y2|width|layer|net         add a track+    V|x|y|size|hole|lowLayer|highLayer|net add a via+    M|ref|x|y|rotMilliDeg                 move + rotate a placed component+    D|T|x1|y1|x2|y2|layer                 delete the free track with these ends (either direction)+    D|V|x|y                               delete the via at x,y+    N|net                                 rip up: delete every free track, arc and via on the net+  Each op answers in results[] in order: "ok", "ok:<n>" (deleted count) or "err:<why>". A+  failed op does not stop the batch; the bridge reads the board back and reports what landed. }+Function FindTrack(Board : IPCB_Board; X1, Y1, X2, Y2, Lay : Integer) : IPCB_Track;+Var+    BIt : IPCB_BoardIterator;+    Trk : IPCB_Track;+Begin+    Result := Nil;+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eTrackObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Trk := BIt.FirstPCBObject;+    While Trk <> Nil Do+    Begin+        { within 50 units (1.3 um): routing_state rounds to 0.1 um, about 39 units, so an off-grid track never matched+          exactly (a board outline on Mechanical 1 could not be removed, 2026-10-23) }+        If (Not Trk.InComponent) And (Trk.Layer = Lay) And+           (((Abs(Trk.x1 - X1) <= 50) And (Abs(Trk.y1 - Y1) <= 50) And (Abs(Trk.x2 - X2) <= 50) And (Abs(Trk.y2 - Y2) <= 50)) Or+            ((Abs(Trk.x1 - X2) <= 50) And (Abs(Trk.y1 - Y2) <= 50) And (Abs(Trk.x2 - X1) <= 50) And (Abs(Trk.y2 - Y1) <= 50))) Then+        Begin+            Result := Trk; Break;+        End;+        Trk := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+End;++Function FindViaAt(Board : IPCB_Board; X, Y : Integer) : IPCB_Via;+Var+    BIt : IPCB_BoardIterator;+    Via : IPCB_Via;+Begin+    Result := Nil;+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eViaObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Via := BIt.FirstPCBObject;+    While Via <> Nil Do+    Begin+        { within 50 units: routing_state's mm are rounded to 0.1 um (see FindTrack) }+        If (Abs(Via.x - X) <= 50) And (Abs(Via.y - Y) <= 50) Then Begin Result := Via; Break; End;+        Via := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+End;++{ The first free track, arc or via on the net, or Nil. }+Function FirstOnNet(Board : IPCB_Board; NetName : String) : IPCB_Primitive;+Var+    BIt  : IPCB_BoardIterator;+    Prim : IPCB_Primitive;+Begin+    Result := Nil;+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eTrackObject, eArcObject, eViaObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Prim := BIt.FirstPCBObject;+    While Prim <> Nil Do+    Begin+        If (Not Prim.InComponent) And (Prim.Net <> Nil) Then+            If Prim.Net.Name = NetName Then Begin Result := Prim; Break; End;+        Prim := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+End;++Procedure RegisterPrim(Board : IPCB_Board; Prim : IPCB_Primitive);+Begin+    Board.AddPCBObject(Prim);+    PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Prim.I_ObjectAddress);+End;++{ the nets a part's pads carry, ; separated }+Function PartNetList(Comp : IPCB_Component) : String;+Var+    GIt : IPCB_GroupIterator;+    Pd  : IPCB_Pad;+Begin+    Result := '';+    GIt := Comp.GroupIterator_Create;+    GIt.AddFilter_ObjectSet(MkSet(ePadObject));+    Pd := GIt.FirstPCBObject;+    While Pd <> Nil Do+    Begin+        If Pd.Net <> Nil Then+            If Pos(';' + Pd.Net.Name + ';', ';' + Result + ';') = 0 Then+            Begin+                If Result <> '' Then Result := Result + ';';+                Result := Result + Pd.Net.Name;+            End;+        Pd := GIt.NextPCBObject;+    End;+    Comp.GroupIterator_Destroy(GIt);+End;++{ A board graphic's key: kind, layer and raw geometry (Altium gives free primitives no stable id). Board-level+  graphics only: not in a part, not on a net. }+Function GraphicKey(Prim : IPCB_Primitive) : String;+Begin+    Result := '';+    If Prim.ObjectId = eTrackObject Then Result := 'line:' + IntToStr(Prim.Layer) + ':' + IntToStr(Prim.X1) + ',' + IntToStr(Prim.Y1) + ',' + IntToStr(Prim.X2) + ',' + IntToStr(Prim.Y2) + ',' + IntToStr(Prim.Width)+    Else If Prim.ObjectId = eArcObject Then Result := 'arc:' + IntToStr(Prim.Layer) + ':' + IntToStr(Prim.XCenter) + ',' + IntToStr(Prim.YCenter) + ',' + IntToStr(Prim.Radius) + ',' + IntToStr(Round(Prim.StartAngle * 1000)) + ',' + IntToStr(Round(Prim.EndAngle * 1000)) + ',' + IntToStr(Prim.LineWidth)+    Else If Prim.ObjectId = eTextObject Then Result := 'text:' + IntToStr(Prim.Layer) + ':' + IntToStr(Prim.XLocation) + ',' + IntToStr(Prim.YLocation) + ':' + Prim.Text+    Else If Prim.ObjectId = eFillObject Then Result := 'rect:' + IntToStr(Prim.Layer) + ':' + IntToStr(Prim.X1Location) + ',' + IntToStr(Prim.Y1Location) + ',' + IntToStr(Prim.X2Location) + ',' + IntToStr(Prim.Y2Location)+    Else If Prim.ObjectId = eRegionObject Then Result := 'polygon:' + IntToStr(Prim.Layer) + ':' + IntToStr(Prim.BoundingRectangle.Left) + ',' + IntToStr(Prim.BoundingRectangle.Bottom) + ',' + IntToStr(Prim.BoundingRectangle.Right) + ',' + IntToStr(Prim.BoundingRectangle.Top)+    Else If Prim.ObjectId = eDimensionObject Then Result := 'dimension:' + IntToStr(Prim.Layer) + ':' + IntToStr(Prim.BoundingRectangle.Left) + ',' + IntToStr(Prim.BoundingRectangle.Bottom) + ',' + IntToStr(Prim.BoundingRectangle.Right) + ',' + IntToStr(Prim.BoundingRectangle.Top);+End;++Function IsBoardGraphic(Prim : IPCB_Primitive) : Boolean;+Begin+    Result := (Not Prim.InComponent) And (Prim.InPolygon = False);+    If Result And (Prim.ObjectId <> eTextObject) And (Prim.ObjectId <> eDimensionObject) Then+        Result := (Prim.Net = Nil) And (Not ((Prim.Layer >= eTopLayer) And (Prim.Layer <= eBottomLayer)));+End;++Function GraphicByKey(Board : IPCB_Board; Key : String) : IPCB_Primitive;+Var+    BIt  : IPCB_BoardIterator;+    Prim : IPCB_Primitive;+Begin+    Result := Nil;+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eTrackObject, eArcObject, eTextObject, eFillObject, eRegionObject, eDimensionObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Prim := BIt.FirstPCBObject;+    While Prim <> Nil Do+    Begin+        If IsBoardGraphic(Prim) And (GraphicKey(Prim) = Key) Then Begin Result := Prim; Break; End;+        Prim := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+End;++{ board_graphics: every board-level graphic (lines, arcs, texts, rectangles, regions, dimensions not in a part and+  not on a net) with its key; raw units. }+Function CmdBoardGraphics(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    BIt   : IPCB_BoardIterator;+    Prim  : IPCB_Primitive;+    S     : String;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_board') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    S := '';+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eTrackObject, eArcObject, eTextObject, eFillObject, eRegionObject, eDimensionObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Prim := BIt.FirstPCBObject;+    While Prim <> Nil Do+    Begin+        If IsBoardGraphic(Prim) Then+        Begin+            If S <> '' Then S := S + ',';+            S := S + '"' + JsonEsc(GraphicKey(Prim)) + '"';+        End;+        Prim := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+    Result := '{' + JRaw('ok','true') + ',' + JRaw('k', IntToStr(MilsToCoord(1000))) + ',' + JRaw('ox', IntToStr(Board.XOrigin)) + ',' + JRaw('oy', IntToStr(Board.YOrigin)) + ',' + JRaw('graphics', '[' + S + ']') + '}';+End;+++Function CmdEditBoard(Lines : TStringList; Payload : String) : String;+Var+    Board : IPCB_Board;+    Ops   : TStringList;+    Op, Kind, Res, Results : String;+    Trk   : IPCB_Track;+    Via   : IPCB_Via;+    Pad   : IPCB_Pad;+    PC    : TPadCache;+    Txt   : IPCB_Text;+    Fl    : IPCB_Fill;+    Comp  : IPCB_Component;+    Prim  : IPCB_Primitive;+    Net   : IPCB_Net;+    Reg   : IPCB_Region;+    Cont  : IPCB_Contour;+    PtList, Pt : String;+    I, N, Okay, Failed : Integer;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    Ops := TStringList.Create;+    Ops.Text := Payload;+    Results := ''; Okay := 0; Failed := 0;+    PCBServer.PreProcess;+    For I := 0 To Ops.Count - 1 Do+    Begin+        Op := Trim(Ops[I]);+        If Op = '' Then Continue;+        Kind := FieldP(Op, 1);+        Res := 'ok';+        gInputError := '';+        If Kind = 'T' Then+        Begin+            Net := FindNet(Board, FieldP(Op, 8));+            If (Net = Nil) And (FieldP(Op, 8) <> '') Then Res := 'err:no net ' + FieldP(Op, 8)+            Else+            Begin+                Trk := PCBServer.PCBObjectFactory(eTrackObject, eNoDimension, eCreate_Default);+                Trk.Layer := AdomInt(FieldP(Op, 7));+                Trk.X1 := AdomInt(FieldP(Op, 2)); Trk.Y1 := AdomInt(FieldP(Op, 3));+                Trk.X2 := AdomInt(FieldP(Op, 4)); Trk.Y2 := AdomInt(FieldP(Op, 5));+                Trk.Width := AdomInt(FieldP(Op, 6));+                If Net <> Nil Then Trk.Net := Net;+                If gInputError <> '' Then Res := 'err:bad number ' + gInputError Else RegisterPrim(Board, Trk);+            End;+        End+        Else If Kind = 'V' Then+        Begin+            Net := FindNet(Board, FieldP(Op, 8));+            If (Net = Nil) And (FieldP(Op, 8) <> '') Then Res := 'err:no net ' + FieldP(Op, 8)+            Else+            Begin+                Via := PCBServer.PCBObjectFactory(eViaObject, eNoDimension, eCreate_Default);+                Via.x := AdomInt(FieldP(Op, 2)); Via.y := AdomInt(FieldP(Op, 3));+                Via.Size := AdomInt(FieldP(Op, 4)); Via.HoleSize := AdomInt(FieldP(Op, 5));+                Via.LowLayer := AdomInt(FieldP(Op, 6)); Via.HighLayer := AdomInt(FieldP(Op, 7));+                If Net <> Nil Then Via.Net := Net;+                If gInputError <> '' Then Res := 'err:bad number ' + gInputError Else RegisterPrim(Board, Via);+            End;+        End+        Else If Kind = 'P' Then+        Begin+            { a free pad: P|x|y|size|hole|plated|layer|net|name|maskExpansion. Layer 74 = multi-layer (a mounting+              hole); a hole of 0 on top or bottom is a fiducial. }+            Net := FindNet(Board, FieldP(Op, 8));+            If (Net = Nil) And (FieldP(Op, 8) <> '') Then Res := 'err:no net ' + FieldP(Op, 8)+            Else+            Begin+                Pad := PCBServer.PCBObjectFactory(ePadObject, eNoDimension, eCreate_Default);+                Pad.X := AdomInt(FieldP(Op, 2)); Pad.Y := AdomInt(FieldP(Op, 3));+                Pad.TopXSize := AdomInt(FieldP(Op, 4)); Pad.TopYSize := AdomInt(FieldP(Op, 4));+                Pad.TopShape := eRounded;+                Pad.HoleSize := AdomInt(FieldP(Op, 5));+                If AdomInt(FieldP(Op, 7)) = 74 Then+                Begin+                    { a simple-mode pad: its top size is every layer's (MidXSize halts the engine) }+                    Pad.Layer := eMultiLayer;+                    Pad.Plated := FieldP(Op, 6) = '1';+                End+                Else If AdomInt(FieldP(Op, 7)) = 32 Then Pad.Layer := eBottomLayer+                Else Pad.Layer := eTopLayer;+                Pad.Name := FieldP(Op, 9);+                If FieldP(Op, 10) <> '' Then+                Begin+                    { a manual mask opening (a fiducial's bare ring) and no paste. Only through the pad cache: assigning+                      Pad.SolderMaskExpansion raised an access violation in ScriptingSystem.DLL (2026-10-23). }+                    PC := Pad.GetState_Cache;+                    PC.SolderMaskExpansion := AdomInt(FieldP(Op, 10));+                    PC.SolderMaskExpansionValid := eCacheManual;+                    PC.PasteMaskExpansion := -Pad.TopXSize;+                    PC.PasteMaskExpansionValid := eCacheManual;+                    Pad.SetState_Cache := PC;+                End;+                If Net <> Nil Then Pad.Net := Net;+                If gInputError <> '' Then Res := 'err:bad number ' + gInputError Else RegisterPrim(Board, Pad);+            End;+        End+        Else If Kind = 'X' Then+        Begin+            { a stroke text: X|x|y|height|stroke|layer|rotation|text (a panel's V-CUT marks on the outline layer;+              measured on Develop 2026-10-23) }+            Txt := PCBServer.PCBObjectFactory(eTextObject, eNoDimension, eCreate_Default);+            Txt.XLocation := AdomInt(FieldP(Op, 2)); Txt.YLocation := AdomInt(FieldP(Op, 3));+            Txt.Size := AdomInt(FieldP(Op, 4)); Txt.Width := AdomInt(FieldP(Op, 5));+            Txt.Layer := AdomInt(FieldP(Op, 6));+            Txt.Rotation := AdomInt(FieldP(Op, 7));+            Txt.Text := FieldP(Op, 8);+            If gInputError <> '' Then Res := 'err:bad number ' + gInputError Else RegisterPrim(Board, Txt);+        End+        Else If Kind = 'K' Then+        Begin+            { a keepout rectangle: K|x1|y1|x2|y2|layer. Layer 0 = the Keep-Out layer (every layer); a copper layer id+              = a keepout fill on that layer only (IsKeepout). Measured on Develop 2026-10-23. }+            Fl := PCBServer.PCBObjectFactory(eFillObject, eNoDimension, eCreate_Default);+            Fl.X1Location := AdomInt(FieldP(Op, 2)); Fl.Y1Location := AdomInt(FieldP(Op, 3));+            Fl.X2Location := AdomInt(FieldP(Op, 4)); Fl.Y2Location := AdomInt(FieldP(Op, 5));+            Fl.Rotation := 0;+            If AdomInt(FieldP(Op, 6)) = 0 Then Fl.Layer := eKeepOutLayer+            Else Begin Fl.Layer := AdomInt(FieldP(Op, 6)); Fl.IsKeepout := True; End;+            If gInputError <> '' Then Res := 'err:bad number ' + gInputError Else RegisterPrim(Board, Fl);+        End+        Else If Kind = 'M' Then+        Begin+            Comp := FindCompByDes(Board, FieldP(Op, 2));+            If Comp = Nil Then Res := 'err:no component ' + FieldP(Op, 2)+            Else+            Begin+                Comp.BeginModify;+                { M|ref|x|y|rot*1000|side: side T or B puts the part on that side first (Altium mirrors it as it+                  flips), then rotation and position are set; empty keeps the side }+                If (FieldP(Op, 6) = 'B') And (Comp.Layer <> eBottomLayer) Then Comp.Layer := eBottomLayer+                Else If (FieldP(Op, 6) = 'T') And (Comp.Layer <> eTopLayer) Then Comp.Layer := eTopLayer;+                Comp.Rotation := AdomInt(FieldP(Op, 5)) / 1000;+                Comp.MoveToXY(AdomInt(FieldP(Op, 3)), AdomInt(FieldP(Op, 4)));+                Comp.EndModify;+                If gInputError <> '' Then Res := 'err:bad number ' + gInputError;+            End;+        End+        Else If Kind = 'D' Then+        Begin+            Prim := Nil;+            If FieldP(Op, 2) = 'T' Then+                Prim := FindTrack(Board, AdomInt(FieldP(Op, 3)), AdomInt(FieldP(Op, 4)), AdomInt(FieldP(Op, 5)), AdomInt(FieldP(Op, 6)), AdomInt(FieldP(Op, 7)))+            Else If FieldP(Op, 2) = 'V' Then+                Prim := FindViaAt(Board, AdomInt(FieldP(Op, 3)), AdomInt(FieldP(Op, 4)));+            If Prim = Nil Then Res := 'err:not found'+            Else Board.RemovePCBObject(Prim);+        End+        Else If Kind = 'R' Then+        Begin+            { R|ref|allowLocked(1/0): remove a placed part; the nets its pads carried answer as ok:NET1;NET2 }+            Comp := FindCompByDes(Board, FieldP(Op, 2));+            If Comp = Nil Then Res := 'err:no component ' + FieldP(Op, 2)+            Else If (Not Comp.Moveable) And (FieldP(Op, 3) <> '1') Then Res := 'err:part_locked'+            Else+            Begin+                Res := 'ok:' + PartNetList(Comp);+                Board.RemovePCBObject(Comp);+            End;+        End+        Else If Kind = 'E' Then+        Begin+            { E|ref|field|value: designator, comment (the value), locked (1/0), kind (an Altium TComponentKind) }+            Comp := FindCompByDes(Board, FieldP(Op, 2));+            If Comp = Nil Then Res := 'err:no component ' + FieldP(Op, 2)+            Else+            Begin+                Comp.BeginModify;+                If FieldP(Op, 3) = 'designator' Then Comp.Name.Text := FieldP(Op, 4)+                Else If FieldP(Op, 3) = 'comment' Then Comp.Comment.Text := FieldP(Op, 4)+                Else If FieldP(Op, 3) = 'locked' Then Comp.Moveable := FieldP(Op, 4) <> '1'+                Else If FieldP(Op, 3) = 'kind' Then Comp.ComponentKind := AdomInt(FieldP(Op, 4))+                Else Res := 'err:unknown field ' + FieldP(Op, 3);+                Comp.EndModify;+                Comp.GraphicallyInvalidate;+                Res := Res + ':' + Comp.Name.Text + '|' + Comp.Comment.Text + '|' + JBool(Not Comp.Moveable) + '|' + IntToStr(Comp.ComponentKind);+            End;+        End+        Else If Kind = 'G' Then+        Begin+            { G|key: delete the board graphic with that key (board_graphics) }+            Prim := GraphicByKey(Board, FieldP(Op, 2));+            If Prim = Nil Then Res := 'err:not found: the graphic changed since it was read'+            Else Board.RemovePCBObject(Prim);+        End+        Else If Kind = 'Y' Then+        Begin+            { a filled region: Y|layer|net|x,y;x,y;... in internal units (import_svg). A hole is part of the one contour,+              reached by a zero-width slit: a hole contour (GeometricPolygon AddContourIsHole) printed filled on+              Develop, 2026-10-28, and the slit form printed with its hole open. }+            Net := FindNet(Board, FieldP(Op, 3));+            If (Net = Nil) And (FieldP(Op, 3) <> '') Then Res := 'err:no net ' + FieldP(Op, 3)+            Else+            Begin+                Cont := PCBServer.PCBContourFactory;+                PtList := FieldP(Op, 4) + ';';+                While Pos(';', PtList) > 0 Do+                Begin+                    Pt := Copy(PtList, 1, Pos(';', PtList) - 1);+                    PtList := Copy(PtList, Pos(';', PtList) + 1, Length(PtList));+                    If Pos(',', Pt) > 0 Then Cont.AddPoint(AdomInt(Copy(Pt, 1, Pos(',', Pt) - 1)), AdomInt(Copy(Pt, Pos(',', Pt) + 1, Length(Pt))));+                End;+                Reg := PCBServer.PCBObjectFactory(eRegionObject, eNoDimension, eCreate_Default);+                Reg.Layer := AdomInt(FieldP(Op, 2));+                Reg.SetOutlineContour(Cont);+                If Net <> Nil Then Reg.Net := Net;+                If gInputError <> '' Then Res := 'err:bad number ' + gInputError+                Else Begin RegisterPrim(Board, Reg); Res := 'ok:' + GraphicKey(Reg); End;+            End;+        End+        Else If Kind = 'N' Then+        Begin+            N := 0;+            Prim := FirstOnNet(Board, FieldP(Op, 2));+            While (Prim <> Nil) And (N < 20000) Do+            Begin+                Board.RemovePCBObject(Prim);+                N := N + 1;+                Prim := FirstOnNet(Board, FieldP(Op, 2));+            End;+            Res := 'ok:' + IntToStr(N);+        End+        Else Res := 'err:unknown op ' + Kind;+        If Copy(Res, 1, 3) = 'err' Then Failed := Failed + 1 Else Okay := Okay + 1;+        If Results <> '' Then Results := Results + ',';+        Results := Results + '"' + JsonEsc(Res) + '"';+    End;+    PCBServer.PostProcess;+    gInputError := '';+    Board.ViewManager_FullUpdate;+    Ops.Free;+    Result := '{' + JRaw('ok','true') + ',' + JRaw('applied', IntToStr(Okay)) + ',' + JRaw('failed', IntToStr(Failed)) + ',' ++        JRaw('results', '[' + Results + ']') + '}';+End;++{ apply_pours: solid polygon pours, one op per payload line, RAW internal units:+    P|net|layer|pourIndex|x,y;x,y;...|removeDead   a closed outline (the last point need not repeat the+                                                   first); removeDead 0 keeps islands Altium calls dead+  All in one PreProcess/PostProcess; each pour is rebuilt (poured) after it is added. Answers+  results[] per line: "ok" or "err:<why>". }+{ a pour's connect style: Altium keeps no connect style on a polygon; a PolygonConnect rule+  scoped IsNamedPolygon('<name>') sets it for that one pour. Altium gives a newly added rule+  priority 1 and moves the rest down (measured AD 26.11.1, 2026-10-15), so it outranks the board's+  own. Writing Rule.Priority from a script halted the engine with an empty Error box (same+  date): do not reorder rules here. }+Function FindRuleNamed(Board : IPCB_Board; Kind : Integer; Name : String) : IPCB_Rule;+Var+    BIt  : IPCB_BoardIterator;+    Rule : IPCB_Rule;+Begin+    Result := Nil;+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eRuleObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Rule := BIt.FirstPCBObject;+    While Rule <> Nil Do+    Begin+        If (Rule.RuleKind = Kind) And (Rule.Name = Name) Then Begin Result := Rule; Break; End;+        Rule := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+End;++Function PolyNamed(Board : IPCB_Board; Name : String) : Boolean;+Var+    BIt  : IPCB_BoardIterator;+    Poly : IPCB_Polygon;+Begin+    Result := False;+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(ePolyObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Poly := BIt.FirstPCBObject;+    While Poly <> Nil Do+    Begin+        If Poly.Name = Name Then Begin Result := True; Break; End;+        Poly := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+End;++{ Style: D direct (solid), R relief, N no connect. W/G raw units, E spokes (2 or 4). }+Procedure SetPourConnect(Board : IPCB_Board; PolyName, Style : String; W, G, E : Integer);+Var+    Rule : IPCB_Rule;+Begin+    Rule := FindRuleNamed(Board, eRule_PolygonConnectStyle, 'AdomPC ' + PolyName);+    If Rule <> Nil Then Board.RemovePCBObject(Rule);+    Rule := PCBServer.PCBRuleFactory(eRule_PolygonConnectStyle);+    Rule.Name := 'AdomPC ' + PolyName;+    Rule.Comment := PolyName;+    { IsNamedPolygon is the polygon itself; InNamedPolygon is 'belongs to' (its pour primitives) and+      never matched the polygon, so the catch-all relief stayed in force (measured 2026-10-15). The+      same OR on both sides: which of the pair (polygon, pad) Altium tests against Scope1 is not ours+      to know. }+    Rule.Scope1Expression := 'IsNamedPolygon(''' + PolyName + ''') Or IsPad Or IsVia';+    Rule.Scope2Expression := Rule.Scope1Expression;+    If Style = 'D' Then Rule.ConnectStyle := eDirectConnectToPlane+    Else If Style = 'N' Then Rule.ConnectStyle := eNoConnect+    Else+    Begin+        Rule.ConnectStyle := eReliefConnectToPlane;+        If W > 0 Then Rule.ReliefConductorWidth := W;+        { no air gap here: AD 26.11.1 keeps it per pad type (SetAirGapWidth(primitiveType, v));+          a plain AirGapWidth is 'Undeclared identifier' (measured 2026-10-15). Altium's default+          gap is 10 mil. }+        If E > 0 Then Rule.ReliefEntries := E;+    End;+    Board.AddPCBObject(Rule);+    PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Rule.I_ObjectAddress);+End;++{ solid joints inside a pour: pads completely inside each rect (the bridge widens a joint box to+  the pads it touches) connect to this pour full width+  (KiCad's small solid zone over a pad, which Altium cannot pour as a separate overlapping+  polygon: a same-net polygon inside another pours nothing, measured 2026-10-15). One direct rule+  per rect; both scopes are the same OR so the pair matches in either order. Rects: 'l,b,r,t'+  RAW, ';'-joined. The rule's Comment holds the polygon name for cleanup. }+Procedure SetPourJoints(Board : IPCB_Board; PolyName, Rects : String);+Var+    RL   : TStringList;+    Rule : IPCB_Rule;+    K    : Integer;+    Q, R : String;+Begin+    RL := SplitSemi(Rects);+    For K := 0 To RL.Count - 1 Do+    Begin+        R := RL[K];+        { whole mils, widened outward: the region test wants the pad completely inside }+        Q := 'IsNamedPolygon(''' + PolyName + ''') Or InRegionAbsolute(' ++            IntToStr(Trunc(CoordToMils(AdomInt(Field(R, 1)))) - 1) + ',' + IntToStr(Trunc(CoordToMils(AdomInt(Field(R, 2)))) - 1) + ',' ++            IntToStr(Trunc(CoordToMils(AdomInt(Field(R, 3)))) + 1) + ',' + IntToStr(Trunc(CoordToMils(AdomInt(Field(R, 4)))) + 1) + ')';+        Rule := FindRuleNamed(Board, eRule_PolygonConnectStyle, 'AdomPJ ' + PolyName + ' #' + IntToStr(K + 1));+        If Rule <> Nil Then Board.RemovePCBObject(Rule);+        Rule := PCBServer.PCBRuleFactory(eRule_PolygonConnectStyle);+        Rule.Name := 'AdomPJ ' + PolyName + ' #' + IntToStr(K + 1);+        Rule.Comment := PolyName;+        Rule.Scope1Expression := Q;+        Rule.Scope2Expression := Q;+        Rule.ConnectStyle := eDirectConnectToPlane;+        Board.AddPCBObject(Rule);+        PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Rule.I_ObjectAddress);+    End;+    RL.Free;+End;++{ a pour keepout: a polygon cutout region on one layer (pours stay out, tracks do not) }+Function AddCutout(Board : IPCB_Board; Lay : Integer; Pts : TStringList; Name : String) : String;+Var+    Reg  : IPCB_Region;+    Cont : IPCB_Contour;+    K    : Integer;+Begin+    Result := 'ok';+    Cont := PCBServer.PCBContourFactory;+    For K := 0 To Pts.Count - 1 Do Cont.AddPoint(AdomInt(Field(Pts[K], 1)), AdomInt(Field(Pts[K], 2)));+    If gInputError <> '' Then Begin Result := 'err:bad number ' + gInputError; Exit; End;+    Reg := PCBServer.PCBObjectFactory(eRegionObject, eNoDimension, eCreate_Default);+    Reg.BeginModify;+    Reg.Layer := Lay;+    Reg.Kind := eRegionKind_Cutout;+    Reg.Name := Name;+    Reg.SetOutlineContour(Cont);+    Reg.EndModify;+    RegisterPrim(Board, Reg);+End;++{ The polygon named Name, Nil when none }+Function PolyByName(Board : IPCB_Board; Name : String) : IPCB_Polygon;+Var+    BIt  : IPCB_BoardIterator;+    Poly : IPCB_Polygon;+Begin+    Result := Nil;+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(ePolyObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Poly := BIt.FirstPCBObject;+    While (Poly <> Nil) And (Result = Nil) Do+    Begin+        If Poly.Name = Name Then Result := Poly;+        Poly := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+End;++{ edit_pour: change existing polygons in place. Payload lines "name|key|value": net, layer (id), hatch (solid, 45, 90,+  none), track (raw width of hatch tracks), grid (raw), dead (0/1: remove dead copper), islands (0/1: remove small+  islands), neck (raw neck width; 0 off), over (same | none: pour over same-net objects), index (pour order),+  connect (D|R|N,width,gap,entries raw: the pour's PolygonConnect rule). Each answers ok:<key> or err:<why>, in order;+  every polygon touched is rebuilt. }+Function CmdEditPour(Lines : TStringList; Payload : String) : String;+Var+    Board : IPCB_Board;+    Poly  : IPCB_Polygon;+    Net   : IPCB_Net;+    Ops, Done : TStringList;+    Op, Nm, Key, V, Res, Results : String;+    I : Integer;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_board') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    Ops := TStringList.Create; Ops.Text := Payload;+    Done := TStringList.Create;+    Results := '';+    PCBServer.PreProcess;+    For I := 0 To Ops.Count - 1 Do+    Begin+        Op := Ops[I];+        If Trim(Op) = '' Then Continue;+        Nm := FieldP(Op, 1); Key := FieldP(Op, 2); V := FieldP(Op, 3);+        Poly := PolyByName(Board, Nm);+        Res := 'ok:' + Key;+        If Poly = Nil Then Res := 'err:no pour named ' + Nm+        Else If Key = 'connect' Then SetPourConnect(Board, Nm, Field(V, 1), AdomInt(Field(V, 2)), AdomInt(Field(V, 3)), AdomInt(Field(V, 4)))+        Else+        Begin+            Poly.BeginModify;+            If Key = 'net' Then+            Begin+                Net := FindNet(Board, V);+                If Net = Nil Then Res := 'err:no net ' + V Else Poly.Net := Net;+            End+            Else If Key = 'layer' Then Poly.Layer := AdomInt(V)+            Else If Key = 'hatch' Then+            Begin+                If V = 'solid' Then Poly.PolyHatchStyle := ePolySolid+                Else If V = '45' Then Poly.PolyHatchStyle := ePolyHatch45+                Else If V = '90' Then Poly.PolyHatchStyle := ePolyHatch90+                Else If V = 'none' Then Poly.PolyHatchStyle := ePolyNoHatch+                Else Res := 'err:hatch is solid, 45, 90 or none';+            End+            Else If Key = 'track' Then Poly.TrackSize := AdomInt(V)+            Else If Key = 'grid' Then Poly.Grid := AdomInt(V)+            Else If Key = 'dead' Then Poly.RemoveDead := V = '1'+            Else If Key = 'islands' Then Poly.RemoveIslandsByArea := V = '1'+            Else If Key = 'neck' Then+            Begin+                Poly.RemoveNarrowNecks := AdomInt(V) > 0;+                If AdomInt(V) > 0 Then Poly.NeckWidthThreshold := AdomInt(V);+            End+            Else If Key = 'over' Then+            Begin+                If V = 'same' Then Poly.PourOver := ePolygonPourOver_SameNet Else Poly.PourOver := ePolygonPourOver_None;+            End+            Else If Key = 'index' Then Poly.PourIndex := AdomInt(V)+            Else Res := 'err:unknown key ' + Key;+            Poly.EndModify;+        End;+        If (Poly <> Nil) And (Done.IndexOf(Nm) < 0) Then Done.Add(Nm);+        If Results <> '' Then Results := Results + ',';+        Results := Results + '"' + JsonEsc(Res) + '"';+    End;+    For I := 0 To Done.Count - 1 Do+    Begin+        Poly := PolyByName(Board, Done[I]);+        If Poly <> Nil Then Begin Poly.Rebuild; Poly.GraphicallyInvalidate; End;+    End;+    PCBServer.PostProcess;+    Board.ViewManager_FullUpdate;+    Result := '{' + JRaw('ok','true') + ',' + JRaw('results', '[' + Results + ']') + ',' + JRaw('pours', IntToStr(Done.Count)) + '}';+    Ops.Free; Done.Free;+End;++Function CmdApplyPours(Lines : TStringList; Payload : String) : String;+Var+    Board : IPCB_Board;+    Ops, Pts : TStringList;+    Op, Res, Results, P, PName : String;+    Poly  : IPCB_Polygon;+    Seg   : TPolySegment;+    Net   : IPCB_Net;+    I, K, C, Okay, Failed : Integer;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    Ops := TStringList.Create;+    Ops.Text := Payload;+    Results := ''; Okay := 0; Failed := 0;+    PCBServer.PreProcess;+    For I := 0 To Ops.Count - 1 Do+    Begin+        Op := Trim(Ops[I]);+        If Op = '' Then Continue;+        Res := 'ok';+        gInputError := '';+        If FieldP(Op, 1) = 'C' Then+        Begin+            Pts := SplitSemi(FieldP(Op, 3));+            If Pts.Count < 3 Then Res := 'err:a cutout needs 3 or more points'+            Else Res := AddCutout(Board, AdomInt(FieldP(Op, 2)), Pts, FieldP(Op, 4));+            Pts.Free;+        End+        Else If FieldP(Op, 1) <> 'P' Then Res := 'err:unknown op'+        Else+        Begin+            Net := FindNet(Board, FieldP(Op, 2));+            Pts := SplitSemi(FieldP(Op, 5));+            If (Net = Nil) And (FieldP(Op, 2) <> '') Then Res := 'err:no net ' + FieldP(Op, 2)+            Else If Pts.Count < 3 Then Res := 'err:an outline needs 3 or more points'+            Else+            Begin+                Poly := PCBServer.PCBObjectFactory(ePolyObject, eNoDimension, eCreate_Default);+                Poly.BeginModify;+                Poly.PolygonType := eSignalLayerPolygon;+                Poly.Layer := AdomInt(FieldP(Op, 3));+                Poly.PolyHatchStyle := ePolySolid;+                PName := FieldP(Op, 7);+                If PName = '' Then PName := 'Adom ' + FieldP(Op, 2) + ' L' + FieldP(Op, 3);+                If PolyNamed(Board, PName) Then+                Begin+                    K := 2;+                    While PolyNamed(Board, PName + ' ' + IntToStr(K)) Do K := K + 1;+                    PName := PName + ' ' + IntToStr(K);+                End;+                Poly.Name := PName;+                Poly.RemoveDead := FieldP(Op, 6) <> '0';+                { connected copper of any size stays, as in a KiCad zone: Altium's own default drops+                  islands under 2500 sq mil, which emptied every small joint pour (measured+                  2026-10-15: a 1.8 mm box around a pad poured 0 mm2). Dead copper still goes. }+                Poly.RemoveIslandsByArea := FieldP(Op, 12) = '1';+                If AdomInt(FieldP(Op, 13)) > 0 Then+                Begin+                    Poly.RemoveNarrowNecks := True;+                    Poly.NeckWidthThreshold := AdomInt(FieldP(Op, 13));+                End;+                { pour over every same-net object, as a KiCad zone does: by default Altium pours AROUND+                  same-net tracks, which leaves slivers that touch no pad and are removed as dead copper+                  (measured 2026-10-15: a buck's SW patch poured 0 mm2 beside its own SW track) }+                Poly.PourOver := ePolygonPourOver_SameNet;+                If Net <> Nil Then Poly.Net := Net;+                Poly.PointCount := Pts.Count;+                For K := 0 To Pts.Count - 1 Do+                Begin+                    P := Pts[K];+                    Seg := Poly.Segments[K];+                    Seg.Kind := ePolySegmentLine;+                    Seg.vx := AdomInt(Field(P, 1));+                    Seg.vy := AdomInt(Field(P, 2));+                    Poly.Segments[K] := Seg;+                End;+                C := AdomInt(FieldP(Op, 4));+                If C > 0 Then Poly.PourIndex := C;+                Poly.EndModify;+                If gInputError <> '' Then Res := 'err:bad number ' + gInputError+                Else+                Begin+                    RegisterPrim(Board, Poly);+                    If FieldP(Op, 8) <> '' Then+                        SetPourConnect(Board, PName, FieldP(Op, 8), AdomInt(FieldP(Op, 9)), AdomInt(FieldP(Op, 10)), AdomInt(FieldP(Op, 11)));+                    If FieldP(Op, 14) <> '' Then SetPourJoints(Board, PName, FieldP(Op, 14));+                    Res := 'ok:' + PName;+                    Poly.Rebuild;+                    Poly.GraphicallyInvalidate;+                End;+            End;+            Pts.Free;+        End;+        If Copy(Res, 1, 3) = 'err' Then Failed := Failed + 1 Else Okay := Okay + 1;+        If Results <> '' Then Results := Results + ',';+        Results := Results + '"' + JsonEsc(Res) + '"';+    End;+    PCBServer.PostProcess;+    gInputError := '';+    Board.ViewManager_FullUpdate;+    Ops.Free;+    Result := '{' + JRaw('ok','true') + ',' + JRaw('applied', IntToStr(Okay)) + ',' + JRaw('failed', IntToStr(Failed)) + ',' ++        JRaw('results', '[' + Results + ']') + '}';+End;++{ pour_state: every polygon with its outline AND the copper it actually poured: the region+  primitives inside it, each as a main contour plus holes, in RAW units. The bridge computes+  the areas (shoelace), so nothing here does floating point.+    polys [["net",layer,pourIndex,[[x,y]...outline],[ [[x,y]...main], [[[x,y]...hole]...] ]...]] }+Function ContourPts(Ct : IPCB_Contour) : String;+Var+    K : Integer;+    S : String;+Begin+    S := '';+    For K := 0 To Ct.Count - 1 Do+    Begin+        If K > 0 Then S := S + ',';+        S := S + '[' + IntToStr(Ct.x[K]) + ',' + IntToStr(Ct.y[K]) + ']';+    End;+    Result := '[' + S + ']';+End;++{ rebuild_pours: rebuild every polygon on the current board through the API. Altium's own Repour All (the helper's+  PCB:ChangeObject RepourAllPolygons) left the Bluetooth Sentinel's hatched GND pours as they were, shorting new stubs,+  where Rebuild cleared them (2026-10-28). Scoped pour rules match nothing in here, so altium_repour uses it only when the+  helper's repour changed no pour. }+Function CmdRebuildPours(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    BIt   : IPCB_BoardIterator;+    Poly  : IPCB_Polygon;+    N     : Integer;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    N := 0;+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(ePolyObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Poly := BIt.FirstPCBObject;+    While Poly <> Nil Do+    Begin+        PCBServer.PreProcess;+        Poly.Rebuild;+        Poly.GraphicallyInvalidate;+        PCBServer.PostProcess;+        N := N + 1;+        Poly := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+    Board.ViewManager_FullUpdate;+    Result := '{' + JRaw('ok','true') + ',' + JRaw('rebuilt', IntToStr(N)) + '}';+End;++Function CmdPourState(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    BIt   : IPCB_BoardIterator;+    GIt   : IPCB_GroupIterator;+    Poly  : IPCB_Polygon;+    Prim  : IPCB_Primitive;+    Reg   : IPCB_Region;+    Seg   : TPolySegment;+    LG    : TStringList;+    Outl, Fills, Holes : String;+    K, H, Pieces : Integer;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    LG := TStringList.Create;+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(ePolyObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Poly := BIt.FirstPCBObject;+    While Poly <> Nil Do+    Begin+        Outl := PolySegPts(Poly);+        Fills := '';+        GIt := Poly.GroupIterator_Create;+        GIt.AddFilter_ObjectSet(MkSet(eRegionObject));+        GIt.AddFilter_LayerSet(AllLayers);+        Prim := GIt.FirstPCBObject;+        While Prim <> Nil Do+        Begin+            Reg := Prim;+            Holes := '';+            For H := 0 To Reg.HoleCount - 1 Do+            Begin+                If H > 0 Then Holes := Holes + ',';+                Holes := Holes + ContourPts(Reg.Holes[H]);+            End;+            If Fills <> '' Then Fills := Fills + ',';+            Fills := Fills + '[' + ContourPts(Reg.MainContour) + ',[' + Holes + ']]';+            Prim := GIt.NextPCBObject;+        End;+        Poly.GroupIterator_Destroy(GIt);+        { a hatched pour is poured as tracks and arcs, not regions: count them, or it reads as unfilled }+        Pieces := 0;+        GIt := Poly.GroupIterator_Create;+        GIt.AddFilter_ObjectSet(MkSet(eTrackObject, eArcObject));+        GIt.AddFilter_LayerSet(AllLayers);+        Prim := GIt.FirstPCBObject;+        While Prim <> Nil Do Begin Pieces := Pieces + 1; Prim := GIt.NextPCBObject; End;+        Poly.GroupIterator_Destroy(GIt);+        LG.Add('[' + NetOf(Poly) + ',' + IntToStr(Poly.Layer) + ',' + IntToStr(Poly.PourIndex) + ',[' + Outl + '],[' + Fills + '],"' + JsonEsc(Poly.Name) + '",' + IntToStr(Pieces) + ']');+        Poly := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+    Result := '{' + JRaw('ok','true') + ',' + JStr('file', Board.FileName) + ',' + JRaw('polys', JoinList(LG)) + '}';+    LG.Free;+End;++{ remove_pours: delete polygons. Payload lines: "net|layer" (layer 0 = any layer), or "*" for all. }+Function FirstPoly(Board : IPCB_Board; NetName : String; Lay : Integer; All : Boolean) : IPCB_Polygon;+Var+    BIt  : IPCB_BoardIterator;+    Poly : IPCB_Polygon;+    N    : String;+Begin+    Result := Nil;+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(ePolyObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Poly := BIt.FirstPCBObject;+    While Poly <> Nil Do+    Begin+        N := '';+        If Poly.Net <> Nil Then N := Poly.Net.Name;+        { NetName '=<name>' picks a polygon by its name }+        If All Or ((N = NetName) And ((Lay = 0) Or (Poly.Layer = Lay))) Or ((Copy(NetName, 1, 1) = '=') And (Poly.Name = Copy(NetName, 2, Length(NetName)))) Then Begin Result := Poly; Break; End;+        Poly := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+End;++Function FirstAdomCutout(Board : IPCB_Board; Lay : Integer) : IPCB_Region;+Var+    BIt : IPCB_BoardIterator;+    Reg : IPCB_Region;+Begin+    Result := Nil;+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eRegionObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Reg := BIt.FirstPCBObject;+    While Reg <> Nil Do+    Begin+        If (Reg.Kind = eRegionKind_Cutout) And (Copy(Reg.Name, 1, 4) = 'Adom') And ((Lay = 0) Or (Reg.Layer = Lay)) Then Begin Result := Reg; Break; End;+        Reg := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+End;++{ the AdomPC/AdomPJ connect rule whose polygon (named in its Comment) is gone }+Function OrphanPourRule(Board : IPCB_Board) : IPCB_Rule;+Var+    BIt  : IPCB_BoardIterator;+    Rule : IPCB_Rule;+    PN   : String;+Begin+    Result := Nil;+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eRuleObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Rule := BIt.FirstPCBObject;+    While Rule <> Nil Do+    Begin+        If (Rule.RuleKind = eRule_PolygonConnectStyle) And ((Copy(Rule.Name, 1, 7) = 'AdomPC ') Or (Copy(Rule.Name, 1, 7) = 'AdomPJ ')) Then+        Begin+            PN := Rule.Comment;+            If PN = '' Then PN := Copy(Rule.Name, 8, Length(Rule.Name));+            If Not PolyNamed(Board, PN) Then Begin Result := Rule; Break; End;+        End;+        Rule := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+End;++Function CmdRemovePours(Lines : TStringList; Payload : String) : String;+Var+    Board : IPCB_Board;+    Ops   : TStringList;+    Op    : String;+    Poly  : IPCB_Polygon;+    Reg   : IPCB_Region;+    Rule  : IPCB_Rule;+    I, N, NC, NR : Integer;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    Ops := TStringList.Create;+    Ops.Text := Payload;+    N := 0; NC := 0; NR := 0;+    PCBServer.PreProcess;+    For I := 0 To Ops.Count - 1 Do+    Begin+        Op := Trim(Ops[I]);+        If Op = '' Then Continue;+        If FieldP(Op, 1) = 'C' Then+        Begin+            Reg := FirstAdomCutout(Board, AdomInt(FieldP(Op, 2)));+            While (Reg <> Nil) And (NC < 5000) Do+            Begin+                Board.RemovePCBObject(Reg);+                NC := NC + 1;+                Reg := FirstAdomCutout(Board, AdomInt(FieldP(Op, 2)));+            End;+            Continue;+        End;+        Poly := FirstPoly(Board, FieldP(Op, 1), AdomInt(FieldP(Op, 2)), Op = '*');+        While (Poly <> Nil) And (N < 5000) Do+        Begin+            Board.RemovePCBObject(Poly);+            N := N + 1;+            Poly := FirstPoly(Board, FieldP(Op, 1), AdomInt(FieldP(Op, 2)), Op = '*');+        End;+    End;+    Rule := OrphanPourRule(Board);+    While (Rule <> Nil) And (NR < 5000) Do+    Begin+        Board.RemovePCBObject(Rule);+        NR := NR + 1;+        Rule := OrphanPourRule(Board);+    End;+    PCBServer.PostProcess;+    gInputError := '';+    Board.ViewManager_FullUpdate;+    Ops.Free;+    Result := '{' + JRaw('ok','true') + ',' + JRaw('removed', IntToStr(N)) + ',' + JRaw('cutoutsRemoved', IntToStr(NC)) + ',' + JRaw('rulesRemoved', IntToStr(NR)) + '}';+End;++{ routing_drc: the violations on the board right now (online DRC keeps them current as edits+  land, a batch DRC refreshes them all) with each one's location, plus the ratsnest count.+    violations [["rule","description",left,bottom,right,top]]  RAW units }+Function CmdRoutingDrc(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    BIt   : IPCB_BoardIterator;+    Prim  : IPCB_Primitive;+    V     : IPCB_Violation;+    R     : TCoordRect;+    LV    : TStringList;+    Rule, O1, O2 : String;+    Unrouted, N : Integer;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    LV := TStringList.Create;+    Unrouted := 0; N := 0;+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eViolationObject, eConnectionObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Prim := BIt.FirstPCBObject;+    While Prim <> Nil Do+    Begin+        If Prim.ObjectId = eConnectionObject Then Unrouted := Unrouted + 1+        Else+        Begin+            V := Prim;+            N := N + 1;+            If N <= 500 Then+            Begin+                Rule := '';+                If V.Rule <> Nil Then Rule := V.Rule.Name;+                R := V.BoundingRectangle;+                { the two objects that clash: a description can be empty (a Clearance one was, 2026-10-22) }+                O1 := ''; O2 := '';+                If V.Primitive1 <> Nil Then O1 := V.Primitive1.Descriptor;+                If V.Primitive2 <> Nil Then O2 := V.Primitive2.Descriptor;+                LV.Add('["' + JsonEsc(Rule) + '","' + JsonEsc(V.Description) + '",' + IntToStr(R.Left) + ',' + IntToStr(R.Bottom) + ',' ++                    IntToStr(R.Right) + ',' + IntToStr(R.Top) + ',"' + JsonEsc(O1) + '","' + JsonEsc(O2) + '"]');+            End;+        End;+        Prim := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+    Result := '{' + JRaw('ok','true') + ',' + JStr('file', Board.FileName) + ',' + JRaw('violationCount', IntToStr(N)) + ',' ++        JRaw('unrouted', IntToStr(Unrouted)) + ',' + JRaw('violations', JoinList(LV)) + '}';+    LV.Free;+End;++{ Outline vertices from the payload's O line into the board outline, RAW units:+    O|x,y,kind,cx,cy,radius,angle1MilliDeg,angle2MilliDeg;...   kind 1 = an arc from this vertex to the next }+Function SetOutlineFrom(Board : IPCB_Board; Spec : String) : Integer;+Var+    BO  : IPCB_BoardOutline;+    Seg : TPolySegment;+    Pts : TStringList;+    K   : Integer;+    P   : String;+Begin+    Result := 0;+    BO := Board.BoardOutline;+    If BO = Nil Then Exit;+    Pts := SplitSemi(Spec);+    If Pts.Count < 3 Then Begin Pts.Free; Exit; End;+    BO.Invalidate;+    BO.PointCount := Pts.Count;+    For K := 0 To Pts.Count - 1 Do+    Begin+        P := Pts[K];+        Seg := BO.Segments[K];+        Seg.vx := AdomInt(Field(P, 1));+        Seg.vy := AdomInt(Field(P, 2));+        If Field(P, 3) = '1' Then+        Begin+            Seg.Kind := ePolySegmentArc;+            Seg.cx := AdomInt(Field(P, 4));+            Seg.cy := AdomInt(Field(P, 5));+            Seg.Radius := AdomInt(Field(P, 6));+            Seg.Angle1 := AdomInt(Field(P, 7)) / 1000;+            Seg.Angle2 := AdomInt(Field(P, 8)) / 1000;+        End+        Else Seg.Kind := ePolySegmentLine;+        BO.Segments[K] := Seg;+    End;+    BO.Rebuild;+    BO.Validate;+    Result := Pts.Count;+    Pts.Free;+End;++{ A library footprint placed by its OWN origin (the library's 0,0), then moved and rotated as a+  whole: the coordinates a layout tool or a reference design gives mean the same here. }+{ A library stores a footprint either around the library's canvas origin (Altium's own editor: about+  50000 mil out) or around 0,0 (bridge-built footprints once a body was aligned). Measured 2026-10-15:+  one PcbLib held both. The footprint's frame is whichever its pad cluster sits nearer to; the offset+  returned is what to take off every primitive. }+Function FpOffset(PLib : IPCB_Library; LibFp : IPCB_LibComponent; Var OX : Integer; Var OY : Integer) : Boolean;+Var+    GIt  : IPCB_GroupIterator;+    Prim : IPCB_Primitive;+    R    : TCoordRect;+    CL, CB, CR, CT, CX, CY, LX, LY : Integer;+    AnyP : Boolean;+Begin+    OX := 0; OY := 0; AnyP := False; CL := 0; CB := 0; CR := 0; CT := 0;+    GIt := LibFp.GroupIterator_Create;+    GIt.AddFilter_ObjectSet(MkSet(ePadObject));+    GIt.AddFilter_LayerSet(AllLayers);+    Prim := GIt.FirstPCBObject;+    While Prim <> Nil Do+    Begin+        R := Prim.BoundingRectangle;+        If Not AnyP Then Begin CL := R.Left; CB := R.Bottom; CR := R.Right; CT := R.Top; AnyP := True; End+        Else+        Begin+            If R.Left < CL Then CL := R.Left;+            If R.Bottom < CB Then CB := R.Bottom;+            If R.Right > CR Then CR := R.Right;+            If R.Top > CT Then CT := R.Top;+        End;+        Prim := GIt.NextPCBObject;+    End;+    LibFp.GroupIterator_Destroy(GIt);+    Result := False;+    If Not AnyP Then Exit;+    CX := (CL + CR) Div 2; CY := (CB + CT) Div 2;+    LX := PLib.Board.XOrigin; LY := PLib.Board.YOrigin;+    If (Abs(CX - LX) + Abs(CY - LY)) < (Abs(CX) + Abs(CY)) Then+    Begin+        OX := LX; OY := LY; Result := True;+    End;+End;++Function PlaceByOrigin(Board : IPCB_Board; PLib : IPCB_Library; LibFp : IPCB_LibComponent; X, Y, RotMilli : Integer) : IPCB_Component;+Var+    Inst : IPCB_Component;+    GIt  : IPCB_GroupIterator;+    Prim, Cpy : IPCB_Primitive;+    OX, OY : Integer;+Begin+    FpOffset(PLib, LibFp, OX, OY);+    Inst := PCBServer.PCBObjectFactory(eComponentObject, eNoDimension, eCreate_Default);+    Inst.Layer := eTopLayer;+    Inst.NameOn := False;+    Inst.CommentOn := False;+    GIt := LibFp.GroupIterator_Create;+    GIt.AddFilter_LayerSet(AllLayers);+    Prim := GIt.FirstPCBObject;+    While Prim <> Nil Do+    Begin+        Cpy := Prim.Replicate;+        If (OX <> 0) Or (OY <> 0) Then Cpy.MoveByXY(-OX, -OY);+        Inst.AddPCBObject(Cpy);+        Prim := GIt.NextPCBObject;+    End;+    LibFp.GroupIterator_Destroy(GIt);+    Board.AddPCBObject(Inst);+    PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Inst.I_ObjectAddress);+    Inst.BeginModify;+    Inst.MoveToXY(X, Y);+    Inst.Rotation := RotMilli / 1000;+    Inst.EndModify;+    Result := Inst;+End;++Function FindLibFp(PLib : IPCB_Library; Name : String) : IPCB_LibComponent;+Var+    I : Integer;+    F : IPCB_LibComponent;+Begin+    Result := Nil;+    For I := 0 To PLib.ComponentCount - 1 Do+    Begin+        F := PLib.GetComponent(I);+        If UpperCase(F.Name) = UpperCase(Name) Then Begin Result := F; Break; End;+    End;+End;++Function JoinSemi(L : TStringList) : String;+Var+    I : Integer;+Begin+    Result := '';+    For I := 0 To L.Count - 1 Do+    Begin+        If I > 0 Then Result := Result + ';';+        Result := Result + L[I];+    End;+End;++{ build_board: a NEW .PcbDoc from a parts list and a netlist, saved to pcb=<path>. The PcbLib with+  every footprint must be the CURRENT document. Payload lines, RAW units:+    O|<outline vertices, see SetOutlineFrom>+    C|ref|footprint|x|y|rotMilliDeg|comment+    N|ref|pad|net+  Answers placed, missing footprints, the nets made and pads wired. }+Function CmdBuildBoard(Lines : TStringList; Payload : String) : String;+Var+    PLib  : IPCB_Library;+    Board : IPCB_Board;+    SD    : IServerDocument;+    Ops, Fps, Assign : TStringList;+    Inst  : IPCB_Component;+    LibFp : IPCB_LibComponent;+    Op, PcbPath, Missing, Outline, Refs : String;+    I, K, Placed, Wired, OutN : Integer;+Begin+    PcbPath := Param(Lines, 'pcb');+    PLib := PCBServer.GetCurrentPCBLibrary;+    If PLib = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the PcbLib with every footprint first (it must be the current document)') + '}'; Exit; End;+    If PcbPath = '' Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','pcb (the .PcbDoc path to save) is required') + '}'; Exit; End;+    Ops := TStringList.Create;+    Ops.Text := Payload;+    { every footprint must be in the library before anything is made }+    Fps := TStringList.Create;+    Missing := ''; Outline := '';+    For I := 0 To Ops.Count - 1 Do+    Begin+        Op := Trim(Ops[I]);+        If FieldP(Op, 1) = 'O' Then Outline := FieldP(Op, 2);+        If FieldP(Op, 1) = 'C' Then+        Begin+            If FindLibFp(PLib, FieldP(Op, 3)) = Nil Then Missing := Missing + FieldP(Op, 3) + ' ';+        End;+    End;+    If Missing <> '' Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','footprint_not_found') + ',' + JStr('error','not in the current PcbLib: ' + Trim(Missing)) + '}';+        Ops.Free; Fps.Free; Exit;+    End;+    SD := CreateNewDocumentFromDocumentKind('PCB');+    If SD = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','could not create a PCB document') + '}'; Ops.Free; Fps.Free; Exit; End;+    Client.ShowDocument(SD);+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board after create') + '}'; Ops.Free; Fps.Free; Exit; End;+    Placed := 0; Wired := 0; OutN := 0; Refs := '';+    gInputError := '';+    PCBServer.PreProcess;+    If Outline <> '' Then OutN := SetOutlineFrom(Board, Outline);+    For I := 0 To Ops.Count - 1 Do+    Begin+        Op := Trim(Ops[I]);+        If FieldP(Op, 1) <> 'C' Then Continue;+        LibFp := FindLibFp(PLib, FieldP(Op, 3));+        Inst := PlaceByOrigin(Board, PLib, LibFp, AdomInt(FieldP(Op, 4)), AdomInt(FieldP(Op, 5)), AdomInt(FieldP(Op, 6)));+        Inst.Name.Text := FieldP(Op, 2);+        Inst.Pattern := LibFp.Name;+        Inst.Comment.Text := FieldP(Op, 7);+        { the pad assignments for this part, as AssignPadNets takes them }+        Assign := TStringList.Create;+        For K := 0 To Ops.Count - 1 Do+            If (FieldP(Trim(Ops[K]), 1) = 'N') And (FieldP(Trim(Ops[K]), 2) = FieldP(Op, 2)) Then+            Begin+                Assign.Add(FieldP(Trim(Ops[K]), 3) + '=' + FieldP(Trim(Ops[K]), 4));+                Wired := Wired + 1;+            End;+        If Assign.Count > 0 Then+        Begin+            AssignPadNets(Board, Inst, JoinSemi(Assign));+        End;+        Assign.Free;+        Placed := Placed + 1;+        If Refs <> '' Then Refs := Refs + ',';+        Refs := Refs + '"' + JsonEsc(FieldP(Op, 2)) + '"';+    End;+    PCBServer.PostProcess;+    Board.ViewManager_FullUpdate;+    FreePath(PcbPath);+    SD.SetFileName(PcbPath);+    SD.DoFileSave('');+    Ops.Free; Fps.Free;+    Result := '{' + JRaw('ok','true') + ',' + JStr('pcb', PcbPath) + ',' + JRaw('placed', IntToStr(Placed)) + ',' + JRaw('padsWired', IntToStr(Wired)) + ',' ++        JRaw('libOriginMil', '[' + IntToStr(Round(CoordToMils(PLib.Board.XOrigin))) + ',' + IntToStr(Round(CoordToMils(PLib.Board.YOrigin))) + ']') + ',' ++        JRaw('outlineVertices', IntToStr(OutN)) + ',' + JRaw('refs', '[' + Refs + ']') + ',' + JRaw('badNumber', JBool(gInputError <> '')) + '}';+    gInputError := '';+End;++{ set_outline: reshape the current board's outline (arcs allowed: rounded corners), one undo step.+  Payload: O|<vertices, see SetOutlineFrom> }+Function CmdSetOutline(Lines : TStringList; Payload : String) : String;+Var+    Board : IPCB_Board;+    N : Integer;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    PCBServer.PreProcess;+    N := SetOutlineFrom(Board, FieldP(Trim(Payload), 2));+    PCBServer.PostProcess;+    gInputError := '';+    Board.ViewManager_FullUpdate;+    Result := '{' + JRaw('ok', JBool(N >= 3)) + ',' + JRaw('vertices', IntToStr(N)) + '}';+End;++{ set_rule: write ONE design rule kind on the current board, every rule of that kind (each scope+  keeps its own scope; the values are the fab's floor). One kind per call, so a property this+  Altium build does not know fails alone. kind = clearance | width | via | hole | ring |+  holetohole | edge. Values are RAW units: a, b, c (min / preferred / max where they apply),+  d, e, f (via drill min / preferred / max). }+Function CmdSetRule(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    BIt   : IPCB_BoardIterator;+    Rule  : IPCB_Rule;+    Kind  : String;+    A, B, C, D, E, F, N, Want : Integer;+    Created : Boolean;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    Created := False;+    Kind := Param(Lines, 'kind');+    A := StrToIntDef(Param(Lines, 'a'), 0); B := StrToIntDef(Param(Lines, 'b'), 0); C := StrToIntDef(Param(Lines, 'c'), 0);+    D := StrToIntDef(Param(Lines, 'd'), 0); E := StrToIntDef(Param(Lines, 'e'), 0); F := StrToIntDef(Param(Lines, 'f'), 0);+    If Kind = 'clearance' Then Want := eRule_Clearance+    Else If Kind = 'width' Then Want := eRule_MaxMinWidth+    Else If Kind = 'via' Then Want := eRule_RoutingViaStyle+    Else If Kind = 'hole' Then Want := eRule_MaxMinHoleSize+    Else If Kind = 'ring' Then Want := eRule_MinimumAnnularRing+    Else If Kind = 'holetohole' Then Want := eRule_HoleToHoleClearance+    Else If Kind = 'edge' Then Want := eRule_BoardOutlineClearance+    Else If Kind = 'polyconnect' Then Want := eRule_PolygonConnectStyle+    Else Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','unknown rule kind: ' + Kind) + '}'; Exit; End;+    N := 0;+    PCBServer.PreProcess;+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eRuleObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Rule := BIt.FirstPCBObject;+    While Rule <> Nil Do+    Begin+        { polyconnect sets the board's catch-all (All / All) rule only: per-pour rules keep theirs }+        If (Rule.RuleKind = Want) And ((Kind <> 'polyconnect') Or ((Rule.Scope1Expression = 'All') And (Rule.Scope2Expression = 'All'))) Then+        Begin+            Rule.BeginModify;+            If Kind = 'clearance' Then Rule.Gap := A+            Else If Kind = 'width' Then+            Begin+                SetWidthAllLayers(Rule, A, B, C);+            End+            Else If Kind = 'via' Then+            Begin+                Rule.MinWidth := A; Rule.PreferedWidth := B; Rule.MaxWidth := C;+                Rule.MinHoleWidth := D; Rule.PreferedHoleWidth := E; Rule.MaxHoleWidth := F;+            End+            Else If Kind = 'hole' Then Begin Rule.MinLimit := A; Rule.MaxLimit := C; End+            Else If Kind = 'ring' Then Rule.Minimum := A+            Else If Kind = 'holetohole' Then Rule.Gap := A+            Else If Kind = 'edge' Then+            Begin+                Rule.Gap := A;+                { the fab's edge clearance is copper to edge (KiCad's edge_clearance): an Adom edge rule an+                  earlier bridge scoped to All also caught silkscreen (adom/altium-bridge#26) }+                If (Rule.Name = 'Adom edge') And (Rule.Scope1Expression = 'All') Then Rule.Scope1Expression := 'OnCopper';+            End+            Else If Kind = 'polyconnect' Then+            Begin+                { A: 1 direct, 2 none, else relief with B spoke width (raw) and C spokes }+                If A = 1 Then Rule.ConnectStyle := eDirectConnectToPlane+                Else If A = 2 Then Rule.ConnectStyle := eNoConnect+                Else+                Begin+                    Rule.ConnectStyle := eReliefConnectToPlane;+                    If B > 0 Then Rule.ReliefConductorWidth := B;+                    If C > 0 Then Rule.ReliefEntries := C;+                End;+            End;+            Rule.EndModify;+            N := N + 1;+        End;+        Rule := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+    { a board with no rule of this kind (a new board has no annular-ring or edge rule) gets one,+      scoped to everything }+    If N = 0 Then+    Begin+        Rule := PCBServer.PCBRuleFactory(Want);+        Rule.Name := 'Adom ' + Kind;+        Rule.Scope1Expression := 'All';+        { edge: copper to the board edge, as the fab pack means it; silkscreen keeps its own rules }+        If Kind = 'edge' Then Rule.Scope1Expression := 'OnCopper';+        Rule.Scope2Expression := 'All';+        If Kind = 'ring' Then Rule.Minimum := A+        Else If (Kind = 'edge') Or (Kind = 'clearance') Or (Kind = 'holetohole') Then Rule.Gap := A+        Else If Kind = 'hole' Then Begin Rule.MinLimit := A; Rule.MaxLimit := C; End;+        Board.AddPCBObject(Rule);+        PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Rule.I_ObjectAddress);+        N := 1;+        Created := True;+    End;+    PCBServer.PostProcess;+    Result := '{' + JRaw('ok','true') + ',' + JStr('kind', Kind) + ',' + JRaw('rulesWritten', IntToStr(N)) + ',' + JRaw('created', JBool(Created)) + '}';+End;++{ zoom_rect: frame the current board's view on a rectangle, RAW units (l b r t). }+Function CmdZoomRect(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    Board.GraphicalView_ZoomOnRect(StrToIntDef(Param(Lines, 'l'), 0), StrToIntDef(Param(Lines, 'b'), 0), StrToIntDef(Param(Lines, 'r'), 0), StrToIntDef(Param(Lines, 't'), 0));+    Result := '{' + JRaw('ok','true') + '}';+End;++{ add_rule: a SCOPED rule (net, net class, any query) the fab-wide set_rule cannot express.+  kind clearance (a = gap) | width (a min, b preferred, c max), raw units; name is 'Adom <name>'+  and a second add with the same name replaces it. Lands at priority 1 (Altium puts a new rule+  first), so it outranks the board's catch-all of its kind. }+Function CmdAddRule(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    Rule  : IPCB_Rule;+    Kind, Name : String;+    A, B, C, D, E, G, U, L, Want : Integer;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    Kind := Param(Lines, 'kind');+    Name := 'Adom ' + Param(Lines, 'name');+    A := StrToIntDef(Param(Lines, 'a'), 0); B := StrToIntDef(Param(Lines, 'b'), 0); C := StrToIntDef(Param(Lines, 'c'), 0);+    D := StrToIntDef(Param(Lines, 'd'), 0); E := StrToIntDef(Param(Lines, 'e'), 0); G := StrToIntDef(Param(Lines, 'g'), 0); U := StrToIntDef(Param(Lines, 'u'), 0);+    If Kind = 'clearance' Then Want := eRule_Clearance+    Else If Kind = 'width' Then Want := eRule_MaxMinWidth+    Else If Kind = 'length' Then Want := eRule_MaxMinLength+    Else If Kind = 'matched' Then Want := eRule_MatchedLengths+    Else If Kind = 'diffpair' Then Want := eRule_DifferentialPairsRouting+    Else If Kind = 'via' Then Want := eRule_RoutingViaStyle+    Else If Kind = 'polyconnect' Then Want := eRule_PolygonConnectStyle+    Else Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','unknown rule kind: ' + Kind) + '}'; Exit; End;+    PCBServer.PreProcess;+    Rule := FindRuleNamed(Board, Want, Name);+    If Rule <> Nil Then Board.RemovePCBObject(Rule);+    If Param(Lines, 'remove') = '1' Then+    Begin+        PCBServer.PostProcess;+        Result := '{' + JRaw('ok','true') + ',' + JStr('name', Name) + ',' + JRaw('removed', JBool(Rule <> Nil)) + '}';+        Exit;+    End;+    Rule := PCBServer.PCBRuleFactory(Want);+    Rule.Name := Name;+    Rule.Comment := Param(Lines, 'comment');+    Rule.Scope1Expression := Param(Lines, 'scope1');+    Rule.Scope2Expression := Param(Lines, 'scope2');+    { measured on Develop 2026-10-23: the length, matched-length, diff-pair and via-style properties below; a diff pair's+      widths and gaps are per layer (setting one layer moved only the descriptor's minimum) }+    If Kind = 'clearance' Then Rule.Gap := A+    Else If Kind = 'length' Then Begin Rule.MinLimit := A; Rule.MaxLimit := C; End+    Else If Kind = 'matched' Then Rule.Tolerance := A+    Else If Kind = 'diffpair' Then+    Begin+        For L := eTopLayer To eBottomLayer Do+        Begin+            Rule.MinWidth[L] := A; Rule.PreferedWidth[L] := B; Rule.MaxWidth[L] := C;+            Rule.MinGap[L] := D; Rule.PreferedGap[L] := E; Rule.MaxGap[L] := G;+        End;+        If U > 0 Then Rule.MaxUncoupledLength := U;+    End+    Else If Kind = 'polyconnect' Then+    Begin+        { a = style (0 relief, 1 direct, 2 none), b = relief conductor width, c = relief entries; the same properties+          set_design_rules' board-wide polygon connect uses }+        If A = 1 Then Rule.ConnectStyle := eDirectConnectToPlane+        Else If A = 2 Then Rule.ConnectStyle := eNoConnect+        Else+        Begin+            Rule.ConnectStyle := eReliefConnectToPlane;+            If B > 0 Then Rule.ReliefConductorWidth := B;+            If C > 0 Then Rule.ReliefEntries := C;+        End;+    End+    Else If Kind = 'via' Then+    Begin+        Rule.MinWidth := A; Rule.PreferedWidth := B; Rule.MaxWidth := C;+        Rule.MinHoleWidth := D; Rule.PreferedHoleWidth := E; Rule.MaxHoleWidth := G;+    End+    Else+    Begin+        SetWidthAllLayers(Rule, A, B, C);+    End;+    Board.AddPCBObject(Rule);+    PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Rule.I_ObjectAddress);+    PCBServer.PostProcess;+    Result := '{' + JRaw('ok','true') + ',' + JStr('name', Name) + ',' + JRaw('priority', IntToStr(Rule.Priority)) + ',' + JStr('descriptor', Rule.Descriptor) + '}';+End;++{ build_schematic: one sheet drawn from a netlist with the project library's symbols. The SchLib+  must be the current document. Each part is a fresh component with its symbol's display-mode-0+  primitives copied in (place_on_sch's measured lane: ISch_Component.Replicate does not carry the+  pins). Parts stand in one column per block, sized from the symbols; every pin on a net gets a+  short wire and a net label at its end, so the sheet compiles to the netlist.+    payload  C|ref|libref|block|comment|footprint      P|ref|pin|net      (param pcblib: the footprints' .PcbLib)+  Coordinates in mil. }+Function LibCmpOf(SLib : ISch_Lib; LibRef : String) : ISch_Component;+Var+    Iter : ISch_Iterator;+    C    : ISch_Component;+Begin+    Result := Nil;+    Iter := SLib.SchLibIterator_Create;+    Iter.AddFilter_ObjectSet(MkSet(eSchComponent));+    C := Iter.FirstSchObject;+    While C <> Nil Do+    Begin+        If UpperCase(C.LibReference) = UpperCase(LibRef) Then Begin Result := C; Break; End;+        C := Iter.NextSchObject;+    End;+    SLib.SchIterator_Destroy(Iter);+End;++Function IsDrawn(Prim : ISch_GraphicalObject) : Boolean;+Begin+    Result := ((Prim.ObjectID = ePin) Or (Prim.ObjectID = eRectangle) Or (Prim.ObjectID = eLine) Or+        (Prim.ObjectID = eArc) Or (Prim.ObjectID = eEllipse) Or (Prim.ObjectID = ePolygon) Or+        (Prim.ObjectID = ePolyline) Or (Prim.ObjectID = eLabel)) And (Prim.OwnerPartDisplayMode = 0);+End;++{ ------------------------------------------------------------ schematic editing (0.2.57) --- }++{ the placed parts of the current sheet with this designator (every part of a multi-part component) }+Function SheetCompsOf(Doc : ISch_Document; Des : String) : TInterfaceList;+Var+    It : ISch_Iterator;+    C  : ISch_Component;+Begin+    Result := TInterfaceList.Create;+    It := Doc.SchIterator_Create;+    It.AddFilter_ObjectSet(MkSet(eSchComponent));+    C := It.FirstSchObject;+    While C <> Nil Do+    Begin+        If UpperCase(C.Designator.Text) = UpperCase(Des) Then Result.Add(C);+        C := It.NextSchObject;+    End;+    Doc.SchIterator_Destroy(It);+End;++{ a GND-like net gets a ground symbol, anything else a bar }+Function PowerStyleOf(Net : String) : Integer;+Var+    U : String;+Begin+    U := UpperCase(Net);+    If (U = 'GND') Or (U = 'AGND') Or (U = 'DGND') Or (U = 'PGND') Or (U = 'GNDA') Or (U = 'GNDD') Or (U = 'VSS') Or (U = '0V') Then+        Result := ePowerGndPower+    Else If (U = 'EARTH') Or (U = 'PE') Or (U = 'CHASSIS') Then+        Result := ePowerGndEarth+    Else+        Result := ePowerBar;+End;++{ sch_connect: a wire stub and a net label (kind=label), power port (kind=power) or nothing (kind=wire)+  at the outer end of pins of placed parts on the current sheet.+  payload lines: designator|pin (number or name)|net|kind|stubMil+  One undo step. Answers per line: ok, or why not. }+{ sheet_symbol: a sheet symbol on the CURRENT sheet for a child sheet. Params x, y (mil, its top-left corner), w, h+  (mil), name, file. Payload: one entry per line, name|side(left/right)|io(input/output/bidirectional/passive)|net|+  stubMil. Altium never places an entry by itself (its Location stays 0,0 and it draws nowhere, measured on Develop+  2026-10-23): each one is put on its edge here, 100 mil apart from 100 mil down. A net gives the entry a wire stub out+  of the symbol and a net label on it. }+Function CmdSheetSymbol(Lines : TStringList; Payload : String) : String;+Var+    Doc   : ISch_Document;+    SS    : ISch_SheetSymbol;+    E     : ISch_SheetEntry;+    W     : ISch_Wire;+    NL    : ISch_NetLabel;+    Ops   : TStringList;+    Op, Nm, Side, IOS, Net, S : String;+    I, X, Y, SW, SH, NL0, NR, Dist, EX, EY, TX, Stub : Integer;+Begin+    Doc := SchServer.GetCurrentSchDocument;+    If (Doc = Nil) Or (Doc.ObjectID = eSchLib) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_schematic') + ',' + JStr('error','no current schematic sheet') + '}'; Exit; End;+    X := MilsToCoord(AdomInt(Param(Lines, 'x'))); Y := MilsToCoord(AdomInt(Param(Lines, 'y')));+    SW := MilsToCoord(AdomInt(Param(Lines, 'w'))); SH := MilsToCoord(AdomInt(Param(Lines, 'h')));+    Ops := TStringList.Create; Ops.Text := Payload;+    SchServer.ProcessControl.PreProcess(Doc, '');+    SS := SchServer.SchObjectFactory(eSheetSymbol, eCreate_Default);+    SS.Location := Point(X, Y);+    SS.XSize := SW; SS.YSize := SH;+    SS.SheetName.Text := Param(Lines, 'name');+    SS.SheetName.Location := Point(X, Y + MilsToCoord(100));+    SS.SheetFileName.Text := Param(Lines, 'file');+    SS.SheetFileName.Location := Point(X, Y - SH - MilsToCoord(150));+    S := ''; NL0 := 0; NR := 0;+    For I := 0 To Ops.Count - 1 Do+    Begin+        Op := Ops[I];+        If Trim(Op) = '' Then Continue;+        Nm := FieldP(Op, 1); Side := LowerCase(FieldP(Op, 2)); IOS := LowerCase(FieldP(Op, 3)); Net := FieldP(Op, 4);+        Stub := AdomInt(FieldP(Op, 5)); If Stub <= 0 Then Stub := 300;+        E := SchServer.SchObjectFactory(eSheetEntry, eCreate_Default);+        E.Name := Nm;+        If IOS = 'input' Then E.IOType := ePortInput+        Else If IOS = 'output' Then E.IOType := ePortOutput+        Else If (IOS = 'bidirectional') Or (IOS = 'bidir') Then E.IOType := ePortBidirectional+        Else E.IOType := ePortUnspecified;+        If Side = 'right' Then Begin NR := NR + 1; Dist := MilsToCoord(100 * NR); E.Side := eRightSide; EX := X + SW; End+        Else Begin NL0 := NL0 + 1; Dist := MilsToCoord(100 * NL0); E.Side := eLeftSide; EX := X; End;+        EY := Y - Dist;+        E.DistanceFromTop := Dist;+        E.Location := Point(EX, EY);+        SS.AddSchObject(E);+        If Net <> '' Then+        Begin+            If Side = 'right' Then TX := EX + MilsToCoord(Stub) Else TX := EX - MilsToCoord(Stub);+            W := SchServer.SchObjectFactory(eWire, eCreate_Default);+            W.Location := Point(EX, EY);+            W.InsertVertex := 1; W.SetState_Vertex(1, Point(EX, EY));+            W.InsertVertex := 2; W.SetState_Vertex(2, Point(TX, EY));+            W.LineWidth := eSmall;+            Doc.RegisterSchObjectInContainer(W);+            SchServer.RobotManager.SendMessage(Doc.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, W.I_ObjectAddress);+            NL := SchServer.SchObjectFactory(eNetLabel, eCreate_Default);+            NL.Text := Net;+            NL.Location := Point(TX, EY);+            If Side <> 'right' Then NL.Justification := eJustify_BottomRight;+            Doc.RegisterSchObjectInContainer(NL);+            SchServer.RobotManager.SendMessage(Doc.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, NL.I_ObjectAddress);+        End;+        If S <> '' Then S := S + ',';+        S := S + '{' + JStr('name', Nm) + ',' + JStr('side', Side) + ',' + JStr('io', IOS) + ',' + JStr('net', Net) + ',' ++            JRaw('atMil', '[' + IntToStr(Round(CoordToMils(EX))) + ',' + IntToStr(Round(CoordToMils(EY))) + ']') + '}';+    End;+    Doc.RegisterSchObjectInContainer(SS);+    SchServer.RobotManager.SendMessage(Doc.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, SS.I_ObjectAddress);+    SchServer.ProcessControl.PostProcess(Doc, '');+    MarkSchModified(Doc);+    Doc.GraphicallyInvalidate;+    Ops.Free;+    Result := '{' + JRaw('ok','true') + ',' + JStr('name', SS.SheetName.Text) + ',' + JStr('file', SS.SheetFileName.Text) + ',' ++        JRaw('entries', '[' + S + ']') + '}';+End;++{ sheet_ports: ports on the CURRENT sheet for the names it lacks. Payload name|io per line. Inputs (and unspecified)+  stack down the left margin, outputs down the right, 200 mil apart, each with a 300 mil wire stub ready to join;+  names that already have a port are reported, not doubled. Params: left, right, top (mil). }+Function CmdSheetPorts(Lines : TStringList; Payload : String) : String;+Var+    Doc   : ISch_Document;+    It    : ISch_Iterator;+    Prt, Old : ISch_Port;+    W     : ISch_Wire;+    Ops, Have : TStringList;+    Op, Nm, IOS, S, Skip : String;+    I, LX, RX, TY, NL0, NR, PX, PY, WX : Integer;+Begin+    Doc := SchServer.GetCurrentSchDocument;+    If (Doc = Nil) Or (Doc.ObjectID = eSchLib) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_schematic') + ',' + JStr('error','no current schematic sheet') + '}'; Exit; End;+    Have := TStringList.Create;+    It := Doc.SchIterator_Create; It.AddFilter_ObjectSet(MkSet(ePort));+    Old := It.FirstSchObject;+    While Old <> Nil Do Begin Have.Add(UpperCase(Old.Name)); Old := It.NextSchObject; End;+    Doc.SchIterator_Destroy(It);+    LX := MilsToCoord(AdomInt(Param(Lines, 'left'))); RX := MilsToCoord(AdomInt(Param(Lines, 'right'))); TY := MilsToCoord(AdomInt(Param(Lines, 'top')));+    Ops := TStringList.Create; Ops.Text := Payload;+    SchServer.ProcessControl.PreProcess(Doc, '');+    S := ''; Skip := ''; NL0 := 0; NR := 0;+    For I := 0 To Ops.Count - 1 Do+    Begin+        Op := Ops[I];+        If Trim(Op) = '' Then Continue;+        Nm := FieldP(Op, 1); IOS := LowerCase(FieldP(Op, 2));+        If Have.IndexOf(UpperCase(Nm)) >= 0 Then+        Begin+            If Skip <> '' Then Skip := Skip + ',';+            Skip := Skip + '"' + JsonEsc(Nm) + '"';+            Continue;+        End;+        Prt := SchServer.SchObjectFactory(ePort, eCreate_Default);+        Prt.Name := Nm;+        Prt.Width := MilsToCoord(600);+        If IOS = 'input' Then Prt.IOType := ePortInput+        Else If IOS = 'output' Then Prt.IOType := ePortOutput+        Else If (IOS = 'bidirectional') Or (IOS = 'bidir') Then Prt.IOType := ePortBidirectional+        Else Prt.IOType := ePortUnspecified;+        If IOS = 'output' Then+        Begin+            PY := TY - MilsToCoord(200 * NR); NR := NR + 1;+            PX := RX - Prt.Width; Prt.Style := ePortRight; WX := PX - MilsToCoord(300);+            W := SchServer.SchObjectFactory(eWire, eCreate_Default);+            W.Location := Point(WX, PY);+            W.InsertVertex := 1; W.SetState_Vertex(1, Point(WX, PY));+            W.InsertVertex := 2; W.SetState_Vertex(2, Point(PX, PY));+        End+        Else+        Begin+            PY := TY - MilsToCoord(200 * NL0); NL0 := NL0 + 1;+            PX := LX; Prt.Style := ePortRight; WX := PX + Prt.Width + MilsToCoord(300);+            W := SchServer.SchObjectFactory(eWire, eCreate_Default);+            W.Location := Point(PX + Prt.Width, PY);+            W.InsertVertex := 1; W.SetState_Vertex(1, Point(PX + Prt.Width, PY));+            W.InsertVertex := 2; W.SetState_Vertex(2, Point(WX, PY));+        End;+        Prt.Location := Point(PX, PY);+        W.LineWidth := eSmall;+        Doc.RegisterSchObjectInContainer(Prt);+        SchServer.RobotManager.SendMessage(Doc.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, Prt.I_ObjectAddress);+        Doc.RegisterSchObjectInContainer(W);+        SchServer.RobotManager.SendMessage(Doc.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, W.I_ObjectAddress);+        If S <> '' Then S := S + ',';+        S := S + '{' + JStr('name', Nm) + ',' + JStr('io', IOS) + ',' + JRaw('atMil', '[' + IntToStr(Round(CoordToMils(PX))) + ',' + IntToStr(Round(CoordToMils(PY))) + ']') ++            ',' + JRaw('stubEndMil', '[' + IntToStr(Round(CoordToMils(WX))) + ',' + IntToStr(Round(CoordToMils(PY))) + ']') + '}';+    End;+    SchServer.ProcessControl.PostProcess(Doc, '');+    MarkSchModified(Doc);+    Doc.GraphicallyInvalidate;+    Ops.Free; Have.Free;+    Result := '{' + JRaw('ok','true') + ',' + JRaw('placed', '[' + S + ']') + ',' + JRaw('alreadyThere', '[' + Skip + ']') + '}';+End;++{ comp_classes: component classes on the current board, with an optional room (a confinement rule named+  'Adom room <class>' scoped InComponentClass, measured on Develop 2026-10-23). op=list | set | remove; name; members+  (designators, ';'); room=1 with rx1, ry1, rx2, ry2 (internal units, absolute like routing_state and edit_board). }+Function CmdCompClasses(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    BIt, CIt : IPCB_BoardIterator;+    Cls, Old : IPCB_ObjectClass;+    Rule, OldRoom : IPCB_Rule;+    Comp  : IPCB_Component;+    Op, Name, Mem, D, S, M, Missing, Q : String;+    P, K  : Integer;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_board') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    Op := Param(Lines, 'op'); Name := Param(Lines, 'name');+    Old := Nil; OldRoom := Nil; S := '';+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eClassObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Cls := BIt.FirstPCBObject;+    While Cls <> Nil Do+    Begin+        If Cls.MemberKind = eClassMemberKind_Component Then+        Begin+            If (Name <> '') And (UpperCase(Cls.Name) = UpperCase(Name)) Then Old := Cls;+            If (Op = 'list') And (Not Cls.SuperClass) Then+            Begin+                M := '';+                CIt := Board.BoardIterator_Create;+                CIt.AddFilter_ObjectSet(MkSet(eComponentObject));+                CIt.AddFilter_LayerSet(AllLayers);+                CIt.AddFilter_Method(eProcessAll);+                Comp := CIt.FirstPCBObject;+                While Comp <> Nil Do+                Begin+                    If Cls.IsMember(Comp) Then Begin If M <> '' Then M := M + ','; M := M + '"' + JsonEsc(Comp.Name.Text) + '"'; End;+                    Comp := CIt.NextPCBObject;+                End;+                Board.BoardIterator_Destroy(CIt);+                If S <> '' Then S := S + ',';+                S := S + '{' + JStr('name', Cls.Name) + ',' + JRaw('members', '[' + M + ']') + '}';+            End;+        End;+        Cls := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+    If Op = 'list' Then Begin Result := '{' + JRaw('ok','true') + ',' + JRaw('classes', '[' + S + ']') + '}'; Exit; End;+    If Name = '' Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','bad_args') + ',' + JStr('error','name is required') + '}'; Exit; End;+    OldRoom := FindRuleNamed(Board, eRule_ConfinementConstraint, 'Adom room ' + Name);+    PCBServer.PreProcess;+    If Old <> Nil Then Board.RemovePCBObject(Old);+    If OldRoom <> Nil Then Board.RemovePCBObject(OldRoom);+    If Op = 'remove' Then+    Begin+        PCBServer.PostProcess;+        Result := '{' + JRaw('ok','true') + ',' + JStr('class', Name) + ',' + JRaw('removed', JBool(Old <> Nil)) + ',' + JRaw('roomRemoved', JBool(OldRoom <> Nil)) + '}';+        Exit;+    End;+    Cls := PCBServer.PCBClassFactoryByClassMember(eClassMemberKind_Component);+    Cls.Name := Name;+    Mem := Param(Lines, 'members'); K := 0; Missing := ''; M := '';+    While Mem <> '' Do+    Begin+        P := Pos(';', Mem);+        If P = 0 Then Begin D := Mem; Mem := ''; End Else Begin D := Copy(Mem, 1, P - 1); Mem := Copy(Mem, P + 1, Length(Mem)); End;+        If D <> '' Then+        Begin+            Comp := FindCompByDes(Board, D);+            If Comp = Nil Then Begin If Missing <> '' Then Missing := Missing + ','; Missing := Missing + '"' + JsonEsc(D) + '"'; End+            Else Begin Cls.AddMemberByName(Comp.Name.Text); K := K + 1; If M <> '' Then M := M + ','; M := M + '"' + JsonEsc(Comp.Name.Text) + '"'; End;+        End;+    End;+    Board.AddPCBObject(Cls);+    PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Cls.I_ObjectAddress);+    S := 'null';+    If Param(Lines, 'room') = '1' Then+    Begin+        Q := Chr(39);+        Rule := PCBServer.PCBRuleFactory(eRule_ConfinementConstraint);+        Rule.Name := 'Adom room ' + Name;+        Rule.Scope1Expression := 'InComponentClass(' + Q + Name + Q + ')';+        Rule.Kind := eConfineIn;+        Rule.Layer := eTopLayer;+        Rule.BoundingRect := RectToCoordRect(Rect(AdomInt(Param(Lines, 'rx1')), AdomInt(Param(Lines, 'ry2')), AdomInt(Param(Lines, 'rx2')), AdomInt(Param(Lines, 'ry1'))));+        Board.AddPCBObject(Rule);+        PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Rule.I_ObjectAddress);+        S := '"' + JsonEsc(Rule.Descriptor) + '"';+    End;+    PCBServer.PostProcess;+    Result := '{' + JRaw('ok','true') + ',' + JStr('class', Name) + ',' + JRaw('members', '[' + M + ']') + ',' + JRaw('missing', '[' + Missing + ']') + ',' + JRaw('room', S) + '}';+End;++{ panelize: a new board holding an embedded board array of the CURRENT board (IPCB_EmbeddedBoard, measured on Develop+  2026-10-23). Params: path (the source .PcbDoc), panel (the new .PcbDoc), rows, cols, colStep, rowStep (internal+  units, board size + gap), bx0, by0 (the source outline's lower-left, absolute), rail. The array's first board lands+  with its outline corner at (panel origin + rail). }+Function CmdPanelize(Lines : TStringList) : String;+Var+    Src, PB : IPCB_Board;+    SD      : IServerDocument;+    E       : IPCB_EmbeddedBoard;+    OffX, OffY, Rail : Integer;+Begin+    Src := PCBServer.GetCurrentPCBBoard;+    If Src = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','the source board is not the current document') + '}'; Exit; End;+    OffX := AdomInt(Param(Lines, 'bx0')) - Src.XOrigin;+    OffY := AdomInt(Param(Lines, 'by0')) - Src.YOrigin;+    Rail := AdomInt(Param(Lines, 'rail'));+    SD := CreateNewDocumentFromDocumentKind('PCB');+    PB := PCBServer.GetCurrentPCBBoard;+    If (PB = Nil) Or (PB = Src) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','the new panel board did not become current') + '}'; Exit; End;+    PCBServer.PreProcess;+    E := PCBServer.PCBObjectFactory(eEmbeddedBoardObject, eNoDimension, eCreate_Default);+    E.DocumentPath := Param(Lines, 'path');+    E.RowCount := AdomInt(Param(Lines, 'rows'));+    E.ColCount := AdomInt(Param(Lines, 'cols'));+    E.ColSpacing := AdomInt(Param(Lines, 'colStep'));+    E.RowSpacing := AdomInt(Param(Lines, 'rowStep'));+    E.XLocation := PB.XOrigin + Rail - OffX;+    E.YLocation := PB.YOrigin + Rail - OffY;+    PB.AddPCBObject(E);+    PCBServer.SendMessageToRobots(PB.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, E.I_ObjectAddress);+    PCBServer.PostProcess;+    PB.ViewManager_FullUpdate;+    FreePath(Param(Lines, 'panel'));+    SD.SetFileName(Param(Lines, 'panel'));+    SD.DoFileSave('');+    Result := '{' + JRaw('ok','true') + ',' + JStr('panel', Param(Lines, 'panel')) + ',' + JRaw('originX', IntToStr(PB.XOrigin)) + ',' + JRaw('originY', IntToStr(PB.YOrigin)) + ',' ++        JRaw('rows', IntToStr(E.RowCount)) + ',' + JRaw('cols', IntToStr(E.ColCount)) + '}';+End;++{ replace_fp: put a library's current footprint in place of a placed part's (update_footprints_from_library), keeping+  where it sits (position, rotation, side), its designator and comment and where they stand, its pad nets by pad name,+  its schematic link (Source* ids) and its lock. ref= libraryPath= footprint=. One undo step. The new copy is placed+  by its pads: the library pad matching a placed pad lands where that pad would (the library's origin moves with its+  current footprint, measured 2026-10-27), then the side and rotation are set as move_parts sets them. }+Function CmdReplaceFp(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    PLib  : IPCB_Library;+    LibFp : IPCB_LibComponent;+    Old, Inst : IPCB_Component;+    GIt   : IPCB_GroupIterator;+    Prim, Cpy : IPCB_Primitive;+    Pad, LP : IPCB_Pad;+    Net   : IPCB_Net;+    NetMap : TStringList;+    I, CX, CY, OX, OY, DX, DY, NX, NY, CmX, CmY, Nets : Integer;+    Rot : Double;+    Bottom, Locked, NOn, COn, Found : Boolean;+    Des, Cmt, SrcU, SrcD, SrcL, SrcC, SrcF, SrcH : String;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_board') + ',' + JStr('error','no current PCB board') + '}'; Exit; End;+    Old := FindCompByDes(Board, Param(Lines, 'ref'));+    If Old = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_such_part') + ',' + JStr('error','no part ' + Param(Lines, 'ref')) + '}'; Exit; End;+    PLib := PCBServer.LoadPCBLibraryByPath(Param(Lines, 'libraryPath'));+    If PLib = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','library_not_found') + ',' + JStr('error','could not load ' + Param(Lines, 'libraryPath')) + '}'; Exit; End;+    LibFp := Nil;+    For I := 0 To PLib.ComponentCount - 1 Do+        If UpperCase(PLib.GetComponent(I).Name) = UpperCase(Param(Lines, 'footprint')) Then LibFp := PLib.GetComponent(I);+    If LibFp = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','footprint_not_found') + ',' + JStr('error', Param(Lines, 'footprint') + ' is not in ' + Param(Lines, 'libraryPath')) + '}'; Exit; End;+    If PLib.CurrentComponent <> LibFp Then PLib.CurrentComponent := LibFp;+    OX := PLib.Board.XOrigin; OY := PLib.Board.YOrigin;+    { everything the part carries that the library does not }+    CX := Old.x; CY := Old.y; Rot := Old.Rotation; Bottom := Old.Layer = eBottomLayer; Locked := Not Old.Moveable;+    Des := Old.Name.Text; Cmt := Old.Comment.Text; NOn := Old.NameOn; COn := Old.CommentOn;+    NX := Old.Name.XLocation; NY := Old.Name.YLocation; CmX := Old.Comment.XLocation; CmY := Old.Comment.YLocation;+    SrcU := ''; SrcD := ''; SrcL := ''; SrcC := ''; SrcF := ''; SrcH := '';+    Try SrcU := Old.SourceUniqueId; SrcD := Old.SourceDesignator; SrcL := Old.SourceLibReference; SrcC := Old.SourceComponentLibrary; SrcF := Old.SourceFootprintLibrary; SrcH := Old.SourceHierarchicalPath; Except End;+    { the footprint now comes from this library: name it, as a part placed from it is named (the next plan tries it first) }+    SrcF := Param(Lines, 'libraryPath');+    I := Length(SrcF);+    While (I > 0) And (Copy(SrcF, I, 1) <> '\') Do I := I - 1;+    SrcF := Copy(SrcF, I + 1, Length(SrcF));+    NetMap := TStringList.Create;+    GIt := Old.GroupIterator_Create;+    GIt.AddFilter_ObjectSet(MkSet(ePadObject)); GIt.AddFilter_LayerSet(AllLayers);+    Pad := GIt.FirstPCBObject;+    While Pad <> Nil Do+    Begin+        If (Pad.Net <> Nil) And (NetMap.IndexOfName(Pad.Name) < 0) Then NetMap.Add(Pad.Name + '=' + Pad.Net.Name);+        Pad := GIt.NextPCBObject;+    End;+    Old.GroupIterator_Destroy(GIt);+    PCBServer.PreProcess;+    Inst := PCBServer.PCBObjectFactory(eComponentObject, eNoDimension, eCreate_Default);+    Inst.Layer := eTopLayer;+    GIt := LibFp.GroupIterator_Create;+    GIt.AddFilter_LayerSet(AllLayers);+    Prim := GIt.FirstPCBObject;+    While Prim <> Nil Do Begin Cpy := Prim.Replicate; Inst.AddPCBObject(Cpy); Prim := GIt.NextPCBObject; End;+    LibFp.GroupIterator_Destroy(GIt);+    Board.AddPCBObject(Inst);+    PCBServer.SendMessageToRobots(Board.I_ObjectAddress, c_Broadcast, PCBM_BoardRegisteration, Inst.I_ObjectAddress);+    Inst.x := CX; Inst.y := CY;+    { put the library's origin on the part's: a pad of the copy goes where its library pad sits from the origin }+    Found := False; DX := 0; DY := 0;+    GIt := LibFp.GroupIterator_Create;+    GIt.AddFilter_ObjectSet(MkSet(ePadObject)); GIt.AddFilter_LayerSet(AllLayers);+    LP := GIt.FirstPCBObject;+    LibFp.GroupIterator_Destroy(GIt);+    If LP <> Nil Then+    Begin+        GIt := Inst.GroupIterator_Create;+        GIt.AddFilter_ObjectSet(MkSet(ePadObject)); GIt.AddFilter_LayerSet(AllLayers);+        Pad := GIt.FirstPCBObject;+        While (Pad <> Nil) And (Not Found) Do+        Begin+            If (Pad.Name = LP.Name) And (Pad.TopXSize = LP.TopXSize) Then Begin DX := (LP.x - OX + CX) - Pad.x; DY := (LP.y - OY + CY) - Pad.y; Found := True; End;+            Pad := GIt.NextPCBObject;+        End;+        Inst.GroupIterator_Destroy(GIt);+    End;+    If (DX <> 0) Or (DY <> 0) Then+    Begin+        GIt := Inst.GroupIterator_Create;+        GIt.AddFilter_LayerSet(AllLayers);+        Prim := GIt.FirstPCBObject;+        While Prim <> Nil Do Begin If Prim.ObjectId <> eTextObject Then Prim.MoveByXY(DX, DY); Prim := GIt.NextPCBObject; End;+        Inst.GroupIterator_Destroy(GIt);+    End;+    Inst.BeginModify;+    If Bottom Then Inst.Layer := eBottomLayer;+    Inst.Rotation := Rot;+    Inst.MoveToXY(CX, CY);+    Inst.Pattern := LibFp.Name;+    Inst.Name.Text := Des; Inst.Comment.Text := Cmt; Inst.NameOn := NOn; Inst.CommentOn := COn;+    Try Inst.SourceUniqueId := SrcU; Inst.SourceDesignator := SrcD; Inst.SourceLibReference := SrcL; Inst.SourceComponentLibrary := SrcC; Inst.SourceFootprintLibrary := SrcF; Inst.SourceHierarchicalPath := SrcH; Except End;+    Inst.EndModify;+    Inst.Name.MoveToXY(NX, NY); Inst.Comment.MoveToXY(CmX, CmY);+    { the nets, pad by pad name }+    Nets := 0;+    GIt := Inst.GroupIterator_Create;+    GIt.AddFilter_ObjectSet(MkSet(ePadObject)); GIt.AddFilter_LayerSet(AllLayers);+    Pad := GIt.FirstPCBObject;+    While Pad <> Nil Do+    Begin+        If NetMap.IndexOfName(Pad.Name) >= 0 Then+        Begin+            Net := FindNet(Board, NetMap.Values[Pad.Name]);+            If Net <> Nil Then Begin Pad.BeginModify; Pad.Net := Net; Pad.EndModify; Net.AddPCBObject(Pad); Nets := Nets + 1; End;+        End;+        Pad := GIt.NextPCBObject;+    End;+    Inst.GroupIterator_Destroy(GIt);+    Board.RemovePCBObject(Old);+    If Locked Then Inst.Moveable := False;+    PCBServer.PostProcess;+    Board.ViewManager_FullUpdate;+    Result := '{' + JRaw('ok','true') + ',' + JStr('ref', Des) + ',' + JStr('footprint', LibFp.Name) + ',' + JRaw('placedByPad', JBool(Found)) + ',' ++        JRaw('padsOnPart', IntToStr(GroupCount(Inst, ePadObject))) + ',' + JRaw('netsKept', IntToStr(Nets)) + ',' + JRaw('netsBefore', IntToStr(NetMap.Count)) + '}';+    NetMap.Free;+End;++{ bodies: every 3D body on the board as [component, componentLayer, overallHeight, standoffHeight, left, bottom, right,+  top, rotation*1000, outline] in internal units (the outline only for a part turned off the 90-degree grid). OverallHeight is the body's top above the board surface its part sits on and StandoffHeight+  its bottom (measured on the Bluetooth Sentinel example, Develop 2026-10-23); a part with several bodies has several+  rows. A body with no component (a free 3D body) is named '-'. }+Function CmdBodies(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    BIt   : IPCB_BoardIterator;+    Body  : IPCB_ComponentBody;+    R     : TCoordRect;+    S, Nm, Outl : String;+    Lyr, N, Rot : Integer;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_board') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    S := ''; N := 0;+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eComponentBodyObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Body := BIt.FirstPCBObject;+    While (Body <> Nil) And (N < 20000) Do+    Begin+        Nm := '-'; Lyr := 0; Rot := 0;+        If Body.Component <> Nil Then Begin Nm := Body.Component.Name.Text; Lyr := Body.Component.Layer; Rot := Round(Body.Component.Rotation * 1000); End;+        R := Body.BoundingRectangle;+        { the body's outline and its part's rotation: a part turned off the grid has a box far bigger than its body, and+          the standard compares turned bodies by their own rectangles (EDA Bridge Standard #26) }+        Outl := '[]';+        If (Rot Mod 90000) <> 0 Then+            Try Outl := ContourPts(Body.MainContour); Except Outl := '[]'; End;+        If N > 0 Then S := S + ',';+        S := S + '["' + JsonEsc(Nm) + '",' + IntToStr(Lyr) + ',' + IntToStr(Body.OverallHeight) + ',' + IntToStr(Body.StandoffHeight) + ',' ++             IntToStr(R.Left) + ',' + IntToStr(R.Bottom) + ',' + IntToStr(R.Right) + ',' + IntToStr(R.Top) + ',' + IntToStr(Rot) + ',' + Outl + ']';+        N := N + 1;+        Body := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+    Result := '{' + JRaw('ok','true') + ',' + JStr('file', Board.FileName) + ',' + JRaw('count', IntToStr(N)) + ',' + JRaw('bodies', '[' + S + ']') + '}';+End;++{ teardrop_count: how many primitives on the current board are teardrops (IPCB_Primitive.TearDrop, measured on+  Develop 2026-10-23). }+Function CmdTeardropCount(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    It    : IPCB_BoardIterator;+    P     : IPCB_Primitive;+    N     : Integer;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    N := 0;+    It := Board.BoardIterator_Create;+    It.AddFilter_ObjectSet(AllObjects); It.AddFilter_LayerSet(AllLayers); It.AddFilter_Method(eProcessAll);+    P := It.FirstPCBObject;+    While P <> Nil Do Begin If P.TearDrop Then N := N + 1; P := It.NextPCBObject; End;+    Board.BoardIterator_Destroy(It);+    Result := '{' + JRaw('ok','true') + ',' + JRaw('teardrops', IntToStr(N)) + '}';+End;++Function CmdSchConnect(Lines : TStringList; Payload : String) : String;+Var+    Doc   : ISch_Document;+    Ops, Comps : TStringList;+    CL    : TInterfaceList;+    C     : ISch_Component;+    PIt   : ISch_Iterator;+    Pin, Hit : ISch_Pin;+    W     : ISch_Wire;+    NL    : ISch_NetLabel;+    PO    : ISch_PowerObject;+    NE    : ISch_NoERC;+    Prt   : ISch_Port;+    IOS   : String;+    Op, Des, PinKey, Net, Kind, Res, S : String;+    I, J, Dir, HX, HY, EX, EY, Stub, NOk : Integer;+Begin+    Doc := SchServer.GetCurrentSchDocument;+    If (Doc = Nil) Or (Doc.ObjectID = eSchLib) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_schematic') + ',' + JStr('error','no current schematic sheet (open a .SchDoc first)') + '}'; Exit; End;+    Ops := TStringList.Create; Ops.Text := Payload;+    S := ''; NOk := 0;+    SchServer.ProcessControl.PreProcess(Doc, '');+    For I := 0 To Ops.Count - 1 Do+    Begin+        Op := Ops[I];+        If Trim(Op) = '' Then Continue;+        Des := FieldP(Op, 1); PinKey := FieldP(Op, 2); Net := FieldP(Op, 3); Kind := LowerCase(FieldP(Op, 4));+        Stub := StrToIntDef(FieldP(Op, 5), 200);+        If Kind = '' Then Kind := 'label';+        Res := '';+        Hit := Nil;+        CL := SheetCompsOf(Doc, Des);+        If CL.Count = 0 Then Res := 'designator not on this sheet'+        Else+        Begin+            For J := 0 To CL.Count - 1 Do+            Begin+                C := CL[J];+                PIt := C.SchIterator_Create;+                PIt.AddFilter_ObjectSet(MkSet(ePin));+                Pin := PIt.FirstSchObject;+                While Pin <> Nil Do+                Begin+                    If (Pin.OwnerPartId = C.CurrentPartID) Or (Pin.OwnerPartId = 0) Then+                        If (UpperCase(Pin.Designator) = UpperCase(PinKey)) Or ((Hit = Nil) And (UpperCase(Pin.Name) = UpperCase(PinKey))) Then+                            If (Hit = Nil) Or (UpperCase(Pin.Designator) = UpperCase(PinKey)) Then Hit := Pin;+                    Pin := PIt.NextSchObject;+                End;+                C.SchIterator_Destroy(PIt);+            End;+            If Hit = Nil Then Res := 'pin not found on ' + Des;+        End;+        CL.Free;+        If (Res = '') And (Net = '') And (Kind <> 'wire') And (Kind <> 'noerc') Then Res := 'no net given';+        If Res = '' Then+        Begin+            Dir := Hit.Orientation;+            HX := Hit.Location.X; HY := Hit.Location.Y;+            If Dir = 0 Then HX := HX + Hit.PinLength+            Else If Dir = 1 Then HY := HY + Hit.PinLength+            Else If Dir = 2 Then HX := HX - Hit.PinLength+            Else HY := HY - Hit.PinLength;+            EX := HX; EY := HY;+            If Kind = 'noerc' Then Stub := 0;+            If Dir = 0 Then EX := HX + MilsToCoord(Stub)+            Else If Dir = 1 Then EY := HY + MilsToCoord(Stub)+            Else If Dir = 2 Then EX := HX - MilsToCoord(Stub)+            Else EY := HY - MilsToCoord(Stub);+            If Kind = 'noerc' Then+            Begin+                { a No ERC marker on the pin's outer end: an intentionally unconnected pin, no wire, no net }+                NE := SchServer.SchObjectFactory(eNoERC, eCreate_Default);+                NE.Location := Point(HX, HY);+                Doc.RegisterSchObjectInContainer(NE);+                SchServer.RobotManager.SendMessage(Doc.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, NE.I_ObjectAddress);+            End+            Else+            Begin+                W := SchServer.SchObjectFactory(eWire, eCreate_Default);+                W.Location := Point(HX, HY);+                W.InsertVertex := 1;+                W.SetState_Vertex(1, Point(HX, HY));+                W.InsertVertex := 2;+                W.SetState_Vertex(2, Point(EX, EY));+                W.LineWidth := eSmall;+                Doc.RegisterSchObjectInContainer(W);+                SchServer.RobotManager.SendMessage(Doc.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, W.I_ObjectAddress);+            End;+            If Kind = 'power' Then+            Begin+                PO := SchServer.SchObjectFactory(ePowerObject, eCreate_Default);+                PO.Text := Net;+                PO.Location := Point(EX, EY);+                PO.Style := PowerStyleOf(Net);+                If Dir = 0 Then PO.Orientation := eRotate0+                Else If Dir = 1 Then PO.Orientation := eRotate90+                Else If Dir = 2 Then PO.Orientation := eRotate180+                Else PO.Orientation := eRotate270;+                PO.ShowNetName := True;+                Doc.RegisterSchObjectInContainer(PO);+                SchServer.RobotManager.SendMessage(Doc.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, PO.I_ObjectAddress);+            End+            Else If Kind = 'port' Then+            Begin+                { a port named after the net at the stub's end: the sheet's link to its sheet symbol's entry. Field 6+                  is the direction (input/output/bidirectional; default unspecified). It points away from the part. }+                Prt := SchServer.SchObjectFactory(ePort, eCreate_Default);+                Prt.Name := Net;+                Prt.Width := MilsToCoord(600);+                IOS := LowerCase(FieldP(Op, 6));+                If IOS = 'input' Then Prt.IOType := ePortInput+                Else If IOS = 'output' Then Prt.IOType := ePortOutput+                Else If (IOS = 'bidirectional') Or (IOS = 'bidir') Then Prt.IOType := ePortBidirectional+                Else Prt.IOType := ePortUnspecified;+                If Dir = 2 Then Begin Prt.Style := ePortLeft; Prt.Location := Point(EX - Prt.Width, EY); End+                Else Begin Prt.Style := ePortRight; Prt.Location := Point(EX, EY); End;+                Doc.RegisterSchObjectInContainer(Prt);+                SchServer.RobotManager.SendMessage(Doc.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, Prt.I_ObjectAddress);+            End+            Else If Kind = 'label' Then+            Begin+                NL := SchServer.SchObjectFactory(eNetLabel, eCreate_Default);+                NL.Text := Net;+                NL.Location := Point(EX, EY);+                If (Dir = 1) Or (Dir = 3) Then NL.Orientation := eRotate90;+                If (Dir = 2) Or (Dir = 3) Then NL.Justification := eJustify_BottomRight;+                Doc.RegisterSchObjectInContainer(NL);+                SchServer.RobotManager.SendMessage(Doc.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, NL.I_ObjectAddress);+            End;+            NOk := NOk + 1;+            Res := 'ok';+        End;+        If S <> '' Then S := S + ',';+        S := S + '{' + JStr('designator', Des) + ',' + JStr('pin', PinKey) + ',' + JStr('net', Net) + ',' + JStr('kind', Kind) + ',' + JStr('result', Res);+        If Res = 'ok' Then S := S + ',' + JRaw('endMil', '[' + IntToStr(Round(CoordToMils(EX))) + ',' + IntToStr(Round(CoordToMils(EY))) + ']');+        S := S + '}';+    End;+    SchServer.ProcessControl.PostProcess(Doc, '');+    MarkSchModified(Doc);+    Doc.GraphicallyInvalidate;+    Ops.Free;+    Result := '{' + JRaw('ok','true') + ',' + JStr('document', Doc.DocumentName) + ',' + JRaw('connected', IntToStr(NOk)) + ',' + JRaw('results', '[' + S + ']') + '}';+End;++{ sch_edit: change placed parts on the current sheet.+  payload lines: designator|action|a|b   actions: move x y (mil, the part's origin) | moveby dx dy |+  rotate 0/90/180/270 | designator new | comment text | delete+  move, moveby and rotate act on every part of a multi-part component (they are drawn apart; say which+  with a single-part designator). One undo step. }+Function CmdSchEdit(Lines : TStringList; Payload : String) : String;+Var+    Doc  : ISch_Document;+    Ops  : TStringList;+    CL, Dead : TInterfaceList;+    C    : ISch_Component;+    Op, Des, Act, A, B, Res, S : String;+    I, J, K, Want, Have, Steps, NOk : Integer;+Begin+    Doc := SchServer.GetCurrentSchDocument;+    If (Doc = Nil) Or (Doc.ObjectID = eSchLib) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_schematic') + ',' + JStr('error','no current schematic sheet (open a .SchDoc first)') + '}'; Exit; End;+    Ops := TStringList.Create; Ops.Text := Payload;+    Dead := TInterfaceList.Create;+    S := ''; NOk := 0;+    SchServer.ProcessControl.PreProcess(Doc, '');+    For I := 0 To Ops.Count - 1 Do+    Begin+        Op := Ops[I];+        If Trim(Op) = '' Then Continue;+        Des := FieldP(Op, 1); Act := LowerCase(FieldP(Op, 2)); A := FieldP(Op, 3); B := FieldP(Op, 4);+        Res := 'ok';+        CL := SheetCompsOf(Doc, Des);+        If CL.Count = 0 Then Res := 'designator not on this sheet'+        Else+            For J := 0 To CL.Count - 1 Do+            Begin+                C := CL[J];+                { Begin/EndModify tell the project's compiler the part changed: without them a renamed or+                  re-commented part reached the change order with its OLD values (U? on the board, and+                  comment edits found no differences at all; measured 2026-10-21, AD 26.11.1). }+                If Act <> 'delete' Then SchServer.RobotManager.SendMessage(C.I_ObjectAddress, c_BroadCast, SCHM_BeginModify, c_NoEventData);+                If Act = 'move' Then C.MoveToXY(MilsToCoord(StrToIntDef(A, 0)), MilsToCoord(StrToIntDef(B, 0)))+                Else If Act = 'moveby' Then C.MoveByXY(MilsToCoord(StrToIntDef(A, 0)), MilsToCoord(StrToIntDef(B, 0)))+                Else If Act = 'rotate' Then+                Begin+                    Want := (StrToIntDef(A, 0) Div 90) Mod 4;+                    Have := C.Orientation;+                    Steps := (Want - Have + 4) Mod 4;+                    For K := 1 To Steps Do C.RotateBy90(C.Location, eRotate90);+                End+                Else If Act = 'designator' Then C.Designator.Text := A+                Else If Act = 'comment' Then C.Comment.Text := A+                Else If Act = 'delete' Then Dead.Add(C)+                Else Res := 'unknown action ' + Act;+                If Act <> 'delete' Then SchServer.RobotManager.SendMessage(C.I_ObjectAddress, c_BroadCast, SCHM_EndModify, c_NoEventData);+                C.GraphicallyInvalidate;+            End;+        CL.Free;+        If Res = 'ok' Then NOk := NOk + 1;+        If S <> '' Then S := S + ',';+        S := S + '{' + JStr('designator', Des) + ',' + JStr('action', Act) + ',' + JStr('result', Res) + '}';+    End;+    For I := 0 To Dead.Count - 1 Do+    Begin+        C := Dead[I];+        Doc.RemoveSchObject(C);+        SchServer.RobotManager.SendMessage(Doc.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, C.I_ObjectAddress);+    End;+    SchServer.ProcessControl.PostProcess(Doc, '');+    MarkSchModified(Doc);+    Doc.GraphicallyInvalidate;+    Ops.Free; Dead.Free;+    Result := '{' + JRaw('ok','true') + ',' + JStr('document', Doc.DocumentName) + ',' + JRaw('applied', IntToStr(NOk)) + ',' + JRaw('results', '[' + S + ']') + '}';+End;++{ sch_place_part: place one component of the CURRENT SchLib on a sheet (opened and shown here).+  libref, sch (the sheet path), designator, x, y (mil, snapped to 100), rotation, comment, partId,+  footprint + pcblib (else the library part's own footprint models are copied by name). }+Function CmdSchPlacePart(Lines : TStringList) : String;+Var+    SLib   : ISch_Lib;+    LibCmp, Inst : ISch_Component;+    Prim, Cpy : ISch_GraphicalObject;+    PIt    : ISch_Iterator;+    Impl, NImpl : ISch_Implementation;+    Prm    : ISch_Parameter;+    SD     : IServerDocument;+    Sheet  : ISch_Document;+    SchPath, Fp, Models : String;+    DX, DY, Part, K, Rot, NPins : Integer;+Begin+    SLib := SchServer.GetCurrentSchDocument;+    If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the SchLib first (it must be the current document)') + '}'; Exit; End;+    LibCmp := LibCmpOf(SLib, Param(Lines, 'libref'));+    If LibCmp = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','libref_not_found') + ',' + JStr('error','no component ' + Param(Lines, 'libref') + ' in ' + SLib.DocumentName) + '}'; Exit; End;+    SchPath := Param(Lines, 'sch');+    SD := Nil;+    Try+        SD := Client.OpenDocument('SCH', SchPath);+    Except+        SD := Nil;+    End;+    If SD = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','could not open the sheet ' + SchPath) + '}'; Exit; End;+    Client.ShowDocument(SD);+    Sheet := SchServer.GetCurrentSchDocument;+    If (Sheet = Nil) Or (Sheet.ObjectID = eSchLib) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','the sheet did not become the current document') + '}'; Exit; End;+    Part := StrToIntDef(Param(Lines, 'partId'), 1);+    DX := (StrToIntDef(Param(Lines, 'x'), 1000) Div 100) * 100;+    DY := (StrToIntDef(Param(Lines, 'y'), 1000) Div 100) * 100;+    SchServer.ProcessControl.PreProcess(Sheet, '');+    Inst := SchServer.SchObjectFactory(eSchComponent, eCreate_Default);+    { a multi-part symbol needs its part count on the instance first, or the part is clamped to 1 and the sheet reads+      part 2 back as part 1 (2026-10-22, dual op-amp) }+    If LibCmp.PartCount > 1 Then Inst.PartCount := LibCmp.PartCount;+    If (Part < 1) Or (Part > Inst.PartCount) Then Part := 1;+    Inst.CurrentPartID := Part;+    Inst.DisplayMode := 0;+    Inst.LibReference := LibCmp.LibReference;+    Inst.ComponentDescription := LibCmp.ComponentDescription;+    Inst.SourceLibraryName := SLib.DocumentName;+    Inst.Location := Point(MilsToCoord(DX), MilsToCoord(DY));+    NPins := 0;+    PIt := LibCmp.SchIterator_Create;+    Prim := PIt.FirstSchObject;+    While Prim <> Nil Do+    Begin+        If ((Prim.ObjectID = ePin) Or (Prim.ObjectID = eRectangle) Or (Prim.ObjectID = eLine) Or (Prim.ObjectID = eArc) Or+            (Prim.ObjectID = eEllipse) Or (Prim.ObjectID = ePolygon) Or (Prim.ObjectID = ePolyline) Or (Prim.ObjectID = eLabel)) And+           (Prim.OwnerPartDisplayMode = 0) Then+        Begin+            { every part's objects keep their own part (0 = shared by all parts), as Altium's own placement does: the+              instance shows CurrentPartID. Copying only the chosen part and moving its shared power pins onto it gave+              each part its own copy of V+/V-, which the compiler reported as duplicate pins (2026-10-23). }+            Cpy := Prim.Replicate;+            Cpy.OwnerPartDisplayMode := 0;+            Cpy.MoveByXY(MilsToCoord(DX), MilsToCoord(DY));+            Inst.AddSchObject(Cpy);+            If (Prim.ObjectID = ePin) And ((Prim.OwnerPartId = Part) Or (Prim.OwnerPartId = 0)) Then NPins := NPins + 1;+        End;+        Prim := PIt.NextSchObject;+    End;+    LibCmp.SchIterator_Destroy(PIt);+    PIt := LibCmp.SchIterator_Create;+    PIt.AddFilter_ObjectSet(MkSet(eParameter));+    Prm := PIt.FirstSchObject;+    While Prm <> Nil Do+    Begin+        If (UpperCase(Prm.Name) <> 'COMMENT') And (UpperCase(Prm.Name) <> 'DESIGNATOR') Then+        Begin+            Cpy := Prm.Replicate;+            Cpy.OwnerPartId := Part;+            Cpy.MoveByXY(MilsToCoord(DX), MilsToCoord(DY));+            Inst.AddSchObject(Cpy);+        End;+        Prm := PIt.NextSchObject;+    End;+    LibCmp.SchIterator_Destroy(PIt);+    { the footprint: the one named, else the library part's own PCBLIB models by name }+    Fp := Param(Lines, 'footprint');+    Models := '';+    If Fp <> '' Then+    Begin+        NImpl := Inst.AddSchImplementation;+        NImpl.ClearAllDatafileLinks;+        NImpl.Description := 'Footprint';+        NImpl.ModelName := Fp;+        NImpl.ModelType := 'PCBLIB';+        NImpl.AddDataFileLink(Fp, Param(Lines, 'pcblib'), 'PCBLIB');+        NImpl.IsCurrent := True;+        Models := '"' + Fp + '"';+    End+    Else+    Begin+        PIt := LibCmp.SchIterator_Create;+        PIt.AddFilter_ObjectSet(MkSet(eImplementation));+        Impl := PIt.FirstSchObject;+        While Impl <> Nil Do+        Begin+            If UpperCase(Impl.ModelType) = 'PCBLIB' Then+            Begin+                NImpl := Inst.AddSchImplementation;+                NImpl.ClearAllDatafileLinks;+                NImpl.Description := Impl.Description;+                NImpl.ModelName := Impl.ModelName;+                NImpl.ModelType := 'PCBLIB';+                NImpl.AddDataFileLink(Impl.ModelName, Param(Lines, 'pcblib'), 'PCBLIB');+                NImpl.IsCurrent := Impl.IsCurrent;+                If Models <> '' Then Models := Models + ',';+                Models := Models + '"' + JsonEsc(Impl.ModelName) + '"';+            End;+            Impl := PIt.NextSchObject;+        End;+        LibCmp.SchIterator_Destroy(PIt);+    End;+    If Param(Lines, 'designator') <> '' Then Inst.Designator.Text := Param(Lines, 'designator')+    Else Inst.Designator.Text := LibCmp.Designator.Text;+    Inst.Designator.IsHidden := False;+    Inst.Designator.Location := Point(MilsToCoord(DX), MilsToCoord(DY + 200));+    If Param(Lines, 'comment') <> '' Then Inst.Comment.Text := Param(Lines, 'comment')+    Else Inst.Comment.Text := LibCmp.Comment.Text;+    Inst.Comment.IsHidden := False;+    Inst.Comment.Location := Point(MilsToCoord(DX), MilsToCoord(DY - 200));+    Sheet.RegisterSchObjectInContainer(Inst);+    SchServer.RobotManager.SendMessage(Sheet.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, Inst.I_ObjectAddress);+    Rot := (StrToIntDef(Param(Lines, 'rotation'), 0) Div 90) Mod 4;+    For K := 1 To Rot Do Inst.RotateBy90(Inst.Location, eRotate90);+    SchServer.ProcessControl.PostProcess(Sheet, '');+    MarkSchModified(Sheet);+    Sheet.GraphicallyInvalidate;+    Result := '{' + JRaw('ok','true') + ',' + JStr('document', Sheet.DocumentName) + ',' + JStr('designator', Inst.Designator.Text) + ',' ++        JStr('libref', LibCmp.LibReference) + ',' + JRaw('pins', IntToStr(NPins)) + ',' + JRaw('footprints', '[' + Models + ']') + ',' ++        JRaw('atMil', '[' + IntToStr(DX) + ',' + IntToStr(DY) + ']') + '}';+End;++{ doc_params: the current sheet's document parameters (title block: Title, DocumentNumber, Revision,+  DrawnBy, Author, CompanyName, ...). params=K=V;K=V sets them (added hidden when missing); always+  answers every document parameter. }+Function CmdDocParams(Lines : TStringList) : String;+Var+    Doc   : ISch_Document;+    It    : ISch_Iterator;+    Pm    : ISch_Parameter;+    Items : TStringList;+    Spec, K, V, S : String;+    I, P, NSet : Integer;+Begin+    Doc := SchServer.GetCurrentSchDocument;+    If (Doc = Nil) Or (Doc.ObjectID = eSchLib) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_schematic') + ',' + JStr('error','no current schematic sheet (open a .SchDoc first)') + '}'; Exit; End;+    NSet := 0;+    If Param(Lines, 'params') <> '' Then+    Begin+        SchServer.ProcessControl.PreProcess(Doc, '');+        Items := SplitSemi(Param(Lines, 'params'));+        For I := 0 To Items.Count - 1 Do+        Begin+            Spec := Items[I];+            P := Pos('=', Spec);+            If P > 0 Then+            Begin+                K := Trim(Copy(Spec, 1, P - 1));+                V := Trim(Copy(Spec, P + 1, Length(Spec)));+                If K <> '' Then Begin SetDocParam(Doc, K, V); NSet := NSet + 1; End;+            End;+        End;+        Items.Free;+        SchServer.ProcessControl.PostProcess(Doc, '');+        MarkSchModified(Doc);+        Doc.GraphicallyInvalidate;+    End;+    S := '';+    It := Doc.SchIterator_Create;+    It.AddFilter_ObjectSet(MkSet(eParameter));+    Pm := It.FirstSchObject;+    While Pm <> Nil Do+    Begin+        { the sheet's own parameters, not its parts' (the iterator walks into components) }+        If (Pm.Container <> Nil) And (Pm.Container.ObjectID <> eSchComponent) Then+        Begin+            If S <> '' Then S := S + ',';+            S := S + '{' + JStr('name', Pm.Name) + ',' + JStr('value', Pm.Text) + '}';+        End;+        Pm := It.NextSchObject;+    End;+    Doc.SchIterator_Destroy(It);+    Result := '{' + JRaw('ok','true') + ',' + JStr('document', Doc.DocumentName) + ',' + JRaw('set', IntToStr(NSet)) + ',' + JRaw('params', '[' + S + ']') + '}';+End;++{ sch_rename_at: rename placed parts named by designator AND position (several parts on a sheet can all+  read 'R?'). payload lines: from|to|xMil|yMil (the part's Location). One undo step. }+Function CmdSchRenameAt(Lines : TStringList; Payload : String) : String;+Var+    Doc  : ISch_Document;+    Ops  : TStringList;+    CL   : TInterfaceList;+    C, Hit : ISch_Component;+    Op, Res, S : String;+    I, J, X, Y : Integer;+Begin+    Doc := SchServer.GetCurrentSchDocument;+    If (Doc = Nil) Or (Doc.ObjectID = eSchLib) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_schematic') + ',' + JStr('error','no current schematic sheet (open a .SchDoc first)') + '}'; Exit; End;+    Ops := TStringList.Create; Ops.Text := Payload;+    S := '';+    SchServer.ProcessControl.PreProcess(Doc, '');+    For I := 0 To Ops.Count - 1 Do+    Begin+        Op := Ops[I];+        If Trim(Op) = '' Then Continue;+        X := StrToIntDef(FieldP(Op, 3), 0); Y := StrToIntDef(FieldP(Op, 4), 0);+        Hit := Nil;+        CL := SheetCompsOf(Doc, FieldP(Op, 1));+        For J := 0 To CL.Count - 1 Do+        Begin+            C := CL[J];+            If (Abs(Round(CoordToMils(C.Location.X)) - X) <= 1) And (Abs(Round(CoordToMils(C.Location.Y)) - Y) <= 1) Then Hit := C;+        End;+        CL.Free;+        If Hit = Nil Then Res := 'no part ' + FieldP(Op, 1) + ' at that position'+        Else Begin+            { Begin/EndModify: without them the change order still saw 'U?' (see sch_edit) }+            SchServer.RobotManager.SendMessage(Hit.I_ObjectAddress, c_BroadCast, SCHM_BeginModify, c_NoEventData);+            Hit.Designator.Text := FieldP(Op, 2);+            SchServer.RobotManager.SendMessage(Hit.I_ObjectAddress, c_BroadCast, SCHM_EndModify, c_NoEventData);+            Hit.GraphicallyInvalidate; Res := 'ok';+        End;+        If S <> '' Then S := S + ',';+        S := S + '{' + JStr('from', FieldP(Op, 1)) + ',' + JStr('to', FieldP(Op, 2)) + ',' + JStr('result', Res) + '}';+    End;+    SchServer.ProcessControl.PostProcess(Doc, '');+    MarkSchModified(Doc);+    Doc.GraphicallyInvalidate;+    Ops.Free;+    Result := '{' + JRaw('ok','true') + ',' + JRaw('results', '[' + S + ']') + '}';+End;++{ sch_wires: wires on the current sheet, one per payload line: x1|y1|x2|y2[|x3|y3...] in mils (a polyline). }+Function CmdSchWires(Lines : TStringList; Payload : String) : String;+Var+    Doc : ISch_Document;+    Ops : TStringList;+    W   : ISch_Wire;+    I, K, N, Made : Integer;+    Op  : String;+Begin+    Doc := SchServer.GetCurrentSchDocument;+    If (Doc = Nil) Or (Doc.ObjectID = eSchLib) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_schematic') + ',' + JStr('error','no current schematic sheet (open a .SchDoc first)') + '}'; Exit; End;+    Ops := TStringList.Create; Ops.Text := Payload;+    Made := 0;+    SchServer.ProcessControl.PreProcess(Doc, '');+    For I := 0 To Ops.Count - 1 Do+    Begin+        Op := Trim(Ops[I]);+        If Op = '' Then Continue;+        N := 0;+        While FieldP(Op, 2 * N + 1) <> '' Do N := N + 1;+        If N < 2 Then Continue;+        W := SchServer.SchObjectFactory(eWire, eCreate_Default);+        W.Location := Point(MilsToCoord(StrToIntDef(FieldP(Op, 1), 0)), MilsToCoord(StrToIntDef(FieldP(Op, 2), 0)));+        For K := 1 To N Do+        Begin+            W.InsertVertex := K;+            W.SetState_Vertex(K, Point(MilsToCoord(StrToIntDef(FieldP(Op, 2 * K - 1), 0)), MilsToCoord(StrToIntDef(FieldP(Op, 2 * K), 0))));+        End;+        W.LineWidth := eSmall;+        Doc.RegisterSchObjectInContainer(W);+        SchServer.RobotManager.SendMessage(Doc.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, W.I_ObjectAddress);+        Made := Made + 1;+    End;+    SchServer.ProcessControl.PostProcess(Doc, '');+    MarkSchModified(Doc);+    Doc.GraphicallyInvalidate;+    Ops.Free;+    Result := '{' + JRaw('ok','true') + ',' + JRaw('wires', IntToStr(Made)) + '}';+End;++{ sch_zoom: select the named parts on the current sheet (everything else deselected) and zoom the view+  to them. designators=R1,U2 }+Function CmdSchZoom(Lines : TStringList) : String;+Var+    Doc  : ISch_Document;+    It   : ISch_Iterator;+    C    : ISch_Component;+    Want : TStringList;+    Found, S : String;+    N : Integer;+Begin+    Doc := SchServer.GetCurrentSchDocument;+    If (Doc = Nil) Or (Doc.ObjectID = eSchLib) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_schematic') + ',' + JStr('error','no current schematic sheet (open a .SchDoc first)') + '}'; Exit; End;+    Want := TStringList.Create;+    Want.Delimiter := ',';+    Want.StrictDelimiter := True;+    Want.DelimitedText := UpperCase(Param(Lines, 'designators'));+    N := 0; S := '';+    It := Doc.SchIterator_Create;+    It.AddFilter_ObjectSet(MkSet(eSchComponent));+    C := It.FirstSchObject;+    While C <> Nil Do+    Begin+        If Want.IndexOf(UpperCase(C.Designator.Text)) >= 0 Then+        Begin+            C.Selection := True;+            N := N + 1;+            If S <> '' Then S := S + ',';+            S := S + '{' + JStr('designator', C.Designator.Text) + ',' + JRaw('xMil', IntToStr(Round(CoordToMils(C.Location.X)))) + ',' + JRaw('yMil', IntToStr(Round(CoordToMils(C.Location.Y)))) + '}';+        End+        Else C.Selection := False;+        C := It.NextSchObject;+    End;+    Doc.SchIterator_Destroy(It);+    Want.Free;+    Doc.GraphicallyInvalidate;+    If N > 0 Then Begin ResetParameters; AddStringParameter('Object', 'Selected'); RunProcess('Sch:Zoom'); End;+    Result := '{' + JRaw('ok','true') + ',' + JRaw('selected', IntToStr(N)) + ',' + JRaw('parts', '[' + S + ']') + '}';+End;++Function CmdBuildSchematic(Lines : TStringList; Payload : String) : String;+Var+    SLib   : ISch_Lib;+    LibCmp : ISch_Component;+    Inst   : ISch_Component;+    Prim, Cpy : ISch_GraphicalObject;+    PIt    : ISch_Iterator;+    Pin    : ISch_Pin;+    W      : ISch_Wire;+    Impl   : ISch_Implementation;+    Prm    : ISch_Parameter;+    NL     : ISch_NetLabel;+    Lb     : ISch_Label;+    SD     : IServerDocument;+    Sheet  : ISch_Document;+    R      : TCoordRect;+    Ops, Refs, LibRefs, Blocks, Comments, Fps, PinNet, BlockOrder, BL, BB, BR, BT, ColOf : TStringList;+    Op, SchPath, Missing, Net, NoNet, Blk : String;+    Cols, Idx : TStringList;+    I, K, N, NWires, NLabels, NPlaced, Col, NCols : Integer;+    BandW : Integer;+    L0, B0, R0, T0, ColW, ColH, ColX, YCur, SheetW, SheetH, DX, DY, HX, HY, EX, EY, Dir, PW, MaxH : Integer;+Begin+    SchPath := Param(Lines, 'sch');+    SLib := SchServer.GetCurrentSchDocument;+    If (SLib = Nil) Or (SLib.ObjectID <> eSchLib) Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the SchLib first (it must be the current document)') + '}';+        Exit;+    End;+    Ops := TStringList.Create; Ops.Text := Payload;+    Refs := TStringList.Create; LibRefs := TStringList.Create; Blocks := TStringList.Create; Comments := TStringList.Create; Fps := TStringList.Create;+    PinNet := TStringList.Create; BlockOrder := TStringList.Create;+    BL := TStringList.Create; BB := TStringList.Create; BR := TStringList.Create; BT := TStringList.Create; ColOf := TStringList.Create;+    Missing := '';+    For I := 0 To Ops.Count - 1 Do+    Begin+        Op := Trim(Ops[I]);+        If FieldP(Op, 1) = 'C' Then+        Begin+            Refs.Add(FieldP(Op, 2)); LibRefs.Add(FieldP(Op, 3)); Blocks.Add(FieldP(Op, 4)); Comments.Add(FieldP(Op, 5)); Fps.Add(FieldP(Op, 6));+            If BlockOrder.IndexOf(FieldP(Op, 4)) < 0 Then BlockOrder.Add(FieldP(Op, 4));+            If LibCmpOf(SLib, FieldP(Op, 3)) = Nil Then Missing := Missing + FieldP(Op, 3) + ' ';+        End+        Else If FieldP(Op, 1) = 'P' Then PinNet.Add(FieldP(Op, 2) + '.' + FieldP(Op, 3) + '=' + FieldP(Op, 4));+    End;+    If Missing <> '' Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('error','symbols not in the SchLib: ' + Trim(Missing)) + '}';+        Exit;+    End;+    { measure every part (mil, library frame) }+    For K := 0 To Refs.Count - 1 Do+    Begin+        LibCmp := LibCmpOf(SLib, LibRefs[K]);+        N := 0; L0 := 0; B0 := 0; R0 := 0; T0 := 0;+        PIt := LibCmp.SchIterator_Create;+        Prim := PIt.FirstSchObject;+        While Prim <> Nil Do+        Begin+            If IsDrawn(Prim) Then+            Begin+                R := Prim.BoundingRectangle;+                If (N = 0) Or (Round(CoordToMils(R.Left)) < L0) Then L0 := Round(CoordToMils(R.Left));+                If (N = 0) Or (Round(CoordToMils(R.Bottom)) < B0) Then B0 := Round(CoordToMils(R.Bottom));+                If (N = 0) Or (Round(CoordToMils(R.Right)) > R0) Then R0 := Round(CoordToMils(R.Right));+                If (N = 0) Or (Round(CoordToMils(R.Top)) > T0) Then T0 := Round(CoordToMils(R.Top));+                N := N + 1;+            End;+            Prim := PIt.NextSchObject;+        End;+        LibCmp.SchIterator_Destroy(PIt);+        BL.Add(IntToStr(L0)); BB.Add(IntToStr(B0)); BR.Add(IntToStr(R0)); BT.Add(IntToStr(T0));+    End;+    { columns: one per block, at most 6 parts tall (a taller block wraps into the next column),+      each wide enough for its widest part plus a wire stub and a net label on each side; the+      columns pack left to right into bands at most 22000 mil wide, so the sheet stays landscape+      and a zoom-all keeps the parts readable. ColOf: 'x,width,bandTop,block|i;i;...' }+    Cols := TStringList.Create;+    For Col := 0 To BlockOrder.Count - 1 Do+    Begin+        N := 0; Op := '';+        For K := 0 To Refs.Count - 1 Do+            If Blocks[K] = BlockOrder[Col] Then+            Begin+                If N = 6 Then Begin Cols.Add(BlockOrder[Col] + '|' + Op); Op := ''; N := 0; End;+                If Op <> '' Then Op := Op + ';';+                Op := Op + IntToStr(K);+                N := N + 1;+            End;+        If Op <> '' Then Cols.Add(BlockOrder[Col] + '|' + Op);+    End;+    NCols := Cols.Count;+    { the bands wrap inside the template's own width (its frame and title block stay true) }+    BandW := AdomInt(Param(Lines, 'tplW')) - 600;+    If BandW < 11000 Then BandW := 22600;+    SheetW := 0; MaxH := 0; ColX := 600; YCur := 0; ColH := 0; Blk := '';+    For Col := 0 To NCols - 1 Do+    Begin+        Idx := SplitSemi(FieldP(Cols[Col], 2));+        PW := 0; DY := 0;+        For I := 0 To Idx.Count - 1 Do+        Begin+            K := AdomInt(Idx[I]);+            If AdomInt(BR[K]) - AdomInt(BL[K]) > PW Then PW := AdomInt(BR[K]) - AdomInt(BL[K]);+            DY := DY + (AdomInt(BT[K]) - AdomInt(BB[K])) + 900;+        End;+        Idx.Free;+        ColW := PW + 2000;+        { a new band when this column would run past 22000 mil }+        If (ColX > 600) And (ColX + ColW > BandW) Then+        Begin+            YCur := YCur + ColH + 900;+            ColH := 0;+            ColX := 600;+        End;+        { the block's name heads its first column only }+        If FieldP(Cols[Col], 1) <> Blk Then Op := FieldP(Cols[Col], 1) Else Op := '';+        Blk := FieldP(Cols[Col], 1);+        ColOf.Add(IntToStr(ColX) + ',' + IntToStr(ColW) + ',' + IntToStr(YCur) + ',' + Op + '|' + FieldP(Cols[Col], 2));+        ColX := ColX + ColW;+        If ColX > SheetW Then SheetW := ColX;+        If DY > ColH Then ColH := DY;+    End;+    MaxH := YCur + ColH;+    SheetW := SheetW + 600;+    SheetH := MaxH + 3600;+    If SheetW < 11500 Then SheetW := 11500;+    If SheetH < 7600 Then SheetH := 7600;+    { the sheet is a FILE the bridge put in place (an Altium sheet template copied to sch) and opened+      inside its project: a new document from CreateNewDocumentFromDocumentKind joins whatever project+      is focused (another design's, or the agent's own), measured 2026-10-15 }+    SD := Nil;+    Try+        SD := Client.OpenDocument('SCH', SchPath);+    Except+        SD := Nil;+    End;+    If SD = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','could not open the sheet ' + SchPath) + '}'; Exit; End;+    Client.ShowDocument(SD);+    Sheet := SchServer.GetCurrentSchDocument;+    If (Sheet = Nil) Or (Sheet.ObjectID = eSchLib) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','the sheet did not become the current document') + '}'; Exit; End;+    { the template's own size (and title block) when the parts fit it, else a custom size }+    If (AdomInt(Param(Lines, 'tplW')) >= SheetW) And (AdomInt(Param(Lines, 'tplH')) >= SheetH) Then+    Begin+        SheetW := AdomInt(Param(Lines, 'tplW')); SheetH := AdomInt(Param(Lines, 'tplH'));+    End+    Else+    Begin+        Sheet.UseCustomSheet := True;+        Sheet.CustomX := MilsToCoord(SheetW);+        Sheet.CustomY := MilsToCoord(SheetH);+        Sheet.CustomXZones := (SheetW + 2499) Div 2500;+        Sheet.CustomYZones := (SheetH + 2499) Div 2500;+        Sheet.CustomMarginWidth := MilsToCoord(200);+    End;+    NWires := 0; NLabels := 0; NPlaced := 0; NoNet := '';+    For Col := 0 To NCols - 1 Do+    Begin+        ColX := AdomInt(Field(FieldP(ColOf[Col], 1), 1)); ColW := AdomInt(Field(FieldP(ColOf[Col], 1), 2));+        Blk := Field(FieldP(ColOf[Col], 1), 4);+        If Blk <> '' Then+        Begin+            Lb := SchServer.SchObjectFactory(eLabel, eCreate_Default);+            Lb.Location := Point(MilsToCoord(ColX + 300), MilsToCoord(SheetH - 1500 - AdomInt(Field(FieldP(ColOf[Col], 1), 3))));+            Lb.Text := UpperCase(Blk);+            Sheet.RegisterSchObjectInContainer(Lb);+            SchServer.RobotManager.SendMessage(Sheet.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, Lb.I_ObjectAddress);+        End;+        { below the template's header strip (its notes and revision block) }+        YCur := SheetH - 2100 - AdomInt(Field(FieldP(ColOf[Col], 1), 3));+        Idx := SplitSemi(FieldP(ColOf[Col], 2));+        For I := 0 To Idx.Count - 1 Do+        Begin+            K := AdomInt(Idx[I]);+            LibCmp := LibCmpOf(SLib, LibRefs[K]);+            L0 := AdomInt(BL[K]); B0 := AdomInt(BB[K]); R0 := AdomInt(BR[K]); T0 := AdomInt(BT[K]);+            { snap to the 100 mil grid so pins land on the grid wires snap to }+            DX := ((ColX + ColW Div 2 - (L0 + R0) Div 2) Div 100) * 100;+            DY := ((YCur - 250 - T0) Div 100) * 100;+            Inst := SchServer.SchObjectFactory(eSchComponent, eCreate_Default);+            Inst.CurrentPartID := 1;+            Inst.DisplayMode := 0;+            Inst.LibReference := LibCmp.LibReference;+            Inst.ComponentDescription := LibCmp.ComponentDescription;+            Inst.Location := Point(MilsToCoord(DX), MilsToCoord(DY));+            PIt := LibCmp.SchIterator_Create;+            Prim := PIt.FirstSchObject;+            While Prim <> Nil Do+            Begin+                If IsDrawn(Prim) Then+                Begin+                    Cpy := Prim.Replicate;+                    Cpy.OwnerPartId := 1;+                    Cpy.OwnerPartDisplayMode := 0;+                    Cpy.MoveByXY(MilsToCoord(DX), MilsToCoord(DY));+                    Inst.AddSchObject(Cpy);+                End;+                Prim := PIt.NextSchObject;+            End;+            LibCmp.SchIterator_Destroy(PIt);+            { the part's parameters (MPN, Manufacturer, distributor numbers): the BOM reads them from+              here, and copying only the drawing left every number column empty (measured 2026-10-15).+              Designator and Comment are the instance's own. }+            PIt := LibCmp.SchIterator_Create;+            PIt.AddFilter_ObjectSet(MkSet(eParameter));+            Prm := PIt.FirstSchObject;+            While Prm <> Nil Do+            Begin+                If (UpperCase(Prm.Name) <> 'COMMENT') And (UpperCase(Prm.Name) <> 'DESIGNATOR') Then+                Begin+                    Cpy := Prm.Replicate;+                    Cpy.OwnerPartId := 1;+                    Cpy.MoveByXY(MilsToCoord(DX), MilsToCoord(DY));+                    Inst.AddSchObject(Cpy);+                End;+                Prm := PIt.NextSchObject;+            End;+            LibCmp.SchIterator_Destroy(PIt);+            { the footprint link the copied primitives do not carry: without it every part compiles+              with 'Footprint ... cannot be found' }+            If Fps[K] <> '' Then+            Begin+                Impl := Inst.AddSchImplementation;+                Impl.ClearAllDatafileLinks;+                Impl.Description := 'Footprint';+                Impl.ModelName := Fps[K];+                Impl.ModelType := 'PCBLIB';+                Impl.AddDataFileLink(Fps[K], Param(Lines, 'pcblib'), 'PCBLIB');+                Impl.IsCurrent := True;+            End;+            Inst.Designator.Text := Refs[K];+            Inst.Designator.IsHidden := False;+            Inst.Designator.Justification := eJustify_BottomCenter;+            Inst.Designator.Location := Point(MilsToCoord(DX + (L0 + R0) Div 2), MilsToCoord(DY + T0 + 80));+            Inst.Comment.Text := Comments[K];+            Inst.Comment.IsHidden := False;+            Inst.Comment.Justification := eJustify_TopCenter;+            Inst.Comment.Location := Point(MilsToCoord(DX + (L0 + R0) Div 2), MilsToCoord(DY + B0 - 80));+            Sheet.RegisterSchObjectInContainer(Inst);+            SchServer.RobotManager.SendMessage(Sheet.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, Inst.I_ObjectAddress);+            NPlaced := NPlaced + 1;+            { a wire stub and a net label at every pin's hot spot (Location is the body end) }+            PIt := Inst.SchIterator_Create;+            PIt.AddFilter_ObjectSet(MkSet(ePin));+            Pin := PIt.FirstSchObject;+            While Pin <> Nil Do+            Begin+                Net := PinNet.Values[Refs[K] + '.' + Pin.Designator];+                Dir := Pin.Orientation;+                HX := Pin.Location.X; HY := Pin.Location.Y;+                If Dir = 0 Then HX := HX + Pin.PinLength+                Else If Dir = 1 Then HY := HY + Pin.PinLength+                Else If Dir = 2 Then HX := HX - Pin.PinLength+                Else HY := HY - Pin.PinLength;+                EX := HX; EY := HY;+                If Dir = 0 Then EX := HX + MilsToCoord(200)+                Else If Dir = 1 Then EY := HY + MilsToCoord(200)+                Else If Dir = 2 Then EX := HX - MilsToCoord(200)+                Else EY := HY - MilsToCoord(200);+                If Net = '' Then+                Begin+                    If NoNet <> '' Then NoNet := NoNet + ';';+                    NoNet := NoNet + Refs[K] + '.' + Pin.Designator;+                End+                Else+                Begin+                    W := SchServer.SchObjectFactory(eWire, eCreate_Default);+                    W.Location := Point(HX, HY);+                    W.InsertVertex := 1;+                    W.SetState_Vertex(1, Point(HX, HY));+                    W.InsertVertex := 2;+                    W.SetState_Vertex(2, Point(EX, EY));+                    W.LineWidth := eSmall;+                    Sheet.RegisterSchObjectInContainer(W);+                    SchServer.RobotManager.SendMessage(Sheet.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, W.I_ObjectAddress);+                    NWires := NWires + 1;+                    NL := SchServer.SchObjectFactory(eNetLabel, eCreate_Default);+                    NL.Text := Net;+                    NL.Location := Point(EX, EY);+                    { the label reads away from the part on every side }+                    If (Dir = 1) Or (Dir = 3) Then NL.Orientation := eRotate90;+                    If (Dir = 2) Or (Dir = 3) Then NL.Justification := eJustify_BottomRight;+                    Sheet.RegisterSchObjectInContainer(NL);+                    SchServer.RobotManager.SendMessage(Sheet.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, NL.I_ObjectAddress);+                    NLabels := NLabels + 1;+                End;+                Pin := PIt.NextSchObject;+            End;+            Inst.SchIterator_Destroy(PIt);+            YCur := DY + B0 - 650;+        End;+        Idx.Free;+    End;+    If Param(Lines, 'title') <> '' Then SetDocParam(Sheet, 'Title', Param(Lines, 'title'));+    If Param(Lines, 'number') <> '' Then SetDocParam(Sheet, 'DocumentNumber', Param(Lines, 'number'));+    If Param(Lines, 'revision') <> '' Then SetDocParam(Sheet, 'Revision', Param(Lines, 'revision'));+    If Param(Lines, 'drawnBy') <> '' Then SetDocParam(Sheet, 'DrawnBy', Param(Lines, 'drawnBy'));+    Sheet.GraphicallyInvalidate;+    ResetParameters; AddStringParameter('Action','All'); RunProcess('Sch:Zoom');+    SD.DoFileSave('');+    Result := '{' + JRaw('ok','true') + ',' + JStr('sch', SchPath) + ',' + JRaw('placed', IntToStr(NPlaced)) + ',' ++        JRaw('wires', IntToStr(NWires)) + ',' + JRaw('labels', IntToStr(NLabels)) + ',' + JStr('pinsWithoutNet', NoNet) + ',' ++        JRaw('sheetMil', '[' + IntToStr(SheetW) + ',' + IntToStr(SheetH) + ']') + ',' + JRaw('columns', IntToStr(NCols)) + '}';+    Ops.Free; Refs.Free; LibRefs.Free; Blocks.Free; Comments.Free; Fps.Free; PinNet.Free; BlockOrder.Free;+    BL.Free; BB.Free; BR.Free; BT.Free; ColOf.Free; Cols.Free;+End;++{ project_nets: the FOCUSED project's compiled netlist, flattened: every net with its pins as+  designator.pin. Compile first (compile verb). }+Function CmdProjectNets(Lines : TStringList) : String;+Var+    WS   : IWorkspace;+    Prj  : IProject;+    Doc  : IDocument;+    Net  : INet;+    Pin  : IPin;+    I, J : Integer;+    S, P : String;+Begin+    WS := GetWorkspace;+    Prj := Nil;+    If WS <> Nil Then Prj := WS.DM_FocusedProject;+    If Prj = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no focused project') + '}'; Exit; End;+    Doc := Prj.DM_DocumentFlattened;+    If Doc = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','the project has no compiled document: compile it first') + ',' + JStr('project', Prj.DM_ProjectFullPath) + '}'; Exit; End;+    S := '';+    For I := 0 To Doc.DM_NetCount - 1 Do+    Begin+        Net := Doc.DM_Nets(I);+        P := '';+        For J := 0 To Net.DM_PinCount - 1 Do+        Begin+            Pin := Net.DM_Pins(J);+            If P <> '' Then P := P + ',';+            P := P + '"' + JsonEsc(Pin.DM_LogicalPartDesignator + '.' + Pin.DM_PinNumber) + '"';+        End;+        If S <> '' Then S := S + ',';+        S := S + '"' + JsonEsc(Net.DM_NetName) + '":[' + P + ']';+    End;+    Result := '{' + JRaw('ok','true') + ',' + JStr('project', Prj.DM_ProjectFullPath) + ',' + JRaw('netCount', IntToStr(Doc.DM_NetCount)) + ',' + JRaw('nets', '{' + S + '}') + '}';+End;++{ select_net: select every primitive of one net (tracks, arcs, vias, pads, fills, regions, pours) and+  deselect everything else; net empty = clear the selection. Answers the counts by kind. }+{ place_parts: footprints of the CURRENT PcbLib onto a board that may not be current. Payload lines+  ref|footprint|x|y|assigns, raw units from the board's origin. All the footprints are checked first, so a missing one+  places nothing. The bridge sets rotation and side after with move_parts and reads them back. }+Function CmdPlaceParts(Lines : TStringList; Payload : String) : String;+Var+    PLib  : IPCB_Library;+    LibFp : IPCB_LibComponent;+    Board : IPCB_Board;+    SD    : IServerDocument;+    Inst  : IPCB_Component;+    Ops   : TStringList;+    Op, Missing, S : String;+    I, K, N : Integer;+    Hit   : Boolean;+Begin+    PLib := PCBServer.GetCurrentPCBLibrary;+    If PLib = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','the footprint library is not current') + '}'; Exit; End;+    Ops := TStringList.Create; Ops.Text := Payload;+    Missing := '';+    For K := 0 To Ops.Count - 1 Do+    Begin+        If Trim(Ops[K]) = '' Then Continue;+        Hit := False;+        For I := 0 To PLib.ComponentCount - 1 Do+        Begin+            LibFp := PLib.GetComponent(I);+            If UpperCase(LibFp.Name) = UpperCase(FieldP(Ops[K], 2)) Then Hit := True;+        End;+        If Not Hit Then Missing := Missing + FieldP(Ops[K], 2) + ' ';+    End;+    If Missing <> '' Then+    Begin+        Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','footprint_not_found') + ',' + JStr('error','not in this library: ' + Trim(Missing)) + '}';+        Ops.Free;+        Exit;+    End;+    SD := DocAt(Param(Lines, 'board'));+    If SD = Nil Then SD := Client.OpenDocument('PCB', Param(Lines, 'board'));+    If SD = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','could not open the board') + '}'; Ops.Free; Exit; End;+    Client.ShowDocument(SD);+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board after open') + '}'; Ops.Free; Exit; End;+    S := ''; N := 0;+    PCBServer.PreProcess;+    For K := 0 To Ops.Count - 1 Do+    Begin+        Op := Ops[K];+        If Trim(Op) = '' Then Continue;+        For I := 0 To PLib.ComponentCount - 1 Do+        Begin+            LibFp := PLib.GetComponent(I);+            If UpperCase(LibFp.Name) = UpperCase(FieldP(Op, 2)) Then Break;+        End;+        Inst := PlaceCopy(Board, LibFp, Board.XOrigin + AdomInt(FieldP(Op, 3)), Board.YOrigin + AdomInt(FieldP(Op, 4)));+        Inst.Name.Text := FieldP(Op, 1);+        Inst.Pattern := LibFp.Name;+        Inst.NameOn := True;+        If FieldP(Op, 5) <> '' Then AssignPadNets(Board, Inst, FieldP(Op, 5));+        If S <> '' Then S := S + ',';+        S := S + '"' + JsonEsc(Inst.Name.Text) + '"';+        N := N + 1;+    End;+    PCBServer.PostProcess;+    Board.ViewManager_FullUpdate;+    Ops.Free;+    Result := '{' + JRaw('ok','true') + ',' + JRaw('placed', IntToStr(N)) + ',' + JRaw('refs', '[' + S + ']') + '}';+End;++{ One body as ["file",overall,standoff,width,height]: file is the model's own name, empty for an extruded body. }+Function ModelBodyJson(Body : IPCB_ComponentBody) : String;+Var+    F : String;+    R : TCoordRect;+Begin+    F := '';+    Try If Body.Model <> Nil Then F := Body.Model.FileName; Except F := ''; End;+    R := Body.BoundingRectangle;+    Result := '["' + JsonEsc(F) + '",' + IntToStr(Body.OverallHeight) + ',' + IntToStr(Body.StandoffHeight) + ',' + IntToStr(R.Right - R.Left) + ',' + IntToStr(R.Top - R.Bottom) + ']';+End;++Function GroupModelBodies(Grp : IPCB_Group) : String;+Var+    GIt  : IPCB_GroupIterator;+    Prim : IPCB_Primitive;+    S    : String;+Begin+    S := '';+    GIt := Grp.GroupIterator_Create;+    GIt.AddFilter_ObjectSet(MkSet(eComponentBodyObject));+    GIt.AddFilter_LayerSet(AllLayers);+    Prim := GIt.FirstPCBObject;+    While Prim <> Nil Do+    Begin+        If S <> '' Then S := S + ',';+        S := S + ModelBodyJson(Prim);+        Prim := GIt.NextPCBObject;+    End;+    Grp.GroupIterator_Destroy(GIt);+    Result := '[' + S + ']';+End;++{ model_check: every part of the current board, or footprint of the current library, with its bodies' models. }+Function CmdModelCheck(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    BIt   : IPCB_BoardIterator;+    Comp  : IPCB_Component;+    PLib  : IPCB_Library;+    Fp    : IPCB_LibComponent;+    S     : String;+    I     : Integer;+Begin+    S := '';+    Board := PCBServer.GetCurrentPCBBoard;+    PLib := PCBServer.GetCurrentPCBLibrary;+    If PLib <> Nil Then+    Begin+        For I := 0 To PLib.ComponentCount - 1 Do+        Begin+            Fp := PLib.GetComponent(I);+            If S <> '' Then S := S + ',';+            S := S + '["' + JsonEsc(Fp.Name) + '","' + JsonEsc(Fp.Name) + '",' + GroupModelBodies(Fp) + ']';+        End;+        Result := '{' + JRaw('ok','true') + ',' + JStr('kind','library') + ',' + JStr('document', PLib.Board.FileName) + ',' + JRaw('parts', '[' + S + ']') + '}';+        Exit;+    End;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current board or footprint library') + '}'; Exit; End;+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eComponentObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Comp := BIt.FirstPCBObject;+    While Comp <> Nil Do+    Begin+        If S <> '' Then S := S + ',';+        S := S + '["' + JsonEsc(Comp.Name.Text) + '","' + JsonEsc(Comp.Pattern) + '",' + GroupModelBodies(Comp) + ']';+        Comp := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+    Result := '{' + JRaw('ok','true') + ',' + JStr('kind','board') + ',' + JStr('document', Board.FileName) + ',' + JRaw('parts', '[' + S + ']') + '}';+End;++Function CmdSelectNet(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    BIt   : IPCB_BoardIterator;+    Prim  : IPCB_Primitive;+    NetName, N : String;+    NT, NA, NV, NP, NPoly, NO : Integer;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    NetName := Param(Lines, 'net');+    NT := 0; NA := 0; NV := 0; NP := 0; NPoly := 0; NO := 0;+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eTrackObject, eArcObject, eViaObject, ePadObject, eFillObject, eRegionObject, ePolyObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Prim := BIt.FirstPCBObject;+    While Prim <> Nil Do+    Begin+        N := '';+        If Prim.Net <> Nil Then N := Prim.Net.Name;+        If (NetName <> '') And (N = NetName) And (Not Prim.InPolygon) Then+        Begin+            Prim.Selected := True;+            If Prim.ObjectId = eTrackObject Then NT := NT + 1+            Else If Prim.ObjectId = eArcObject Then NA := NA + 1+            Else If Prim.ObjectId = eViaObject Then NV := NV + 1+            Else If Prim.ObjectId = ePadObject Then NP := NP + 1+            Else If Prim.ObjectId = ePolyObject Then NPoly := NPoly + 1+            Else NO := NO + 1;+        End+        Else If Prim.Selected Then Prim.Selected := False;+        Prim := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+    Board.ViewManager_FullUpdate;+    Result := '{' + JRaw('ok','true') + ',' + JStr('net', NetName) + ',' + JRaw('selected', IntToStr(NT + NA + NV + NP + NPoly + NO)) + ',' ++        JRaw('byKind', '{' + JRaw('tracks', IntToStr(NT)) + ',' + JRaw('arcs', IntToStr(NA)) + ',' + JRaw('vias', IntToStr(NV)) + ',' ++        JRaw('pads', IntToStr(NP)) + ',' + JRaw('pours', IntToStr(NPoly)) + ',' + JRaw('other', IntToStr(NO)) + '}') + '}';+End;++{ ------------------------------------------------------------- silkscreen --- }++Function TextJson(T : IPCB_Text; Shown : Boolean) : String;+Var+    R : TCoordRect;+Begin+    R := T.BoundingRectangle;+    Result := '[' + '"' + JsonEsc(T.Text) + '",' + IntToStr(T.XLocation) + ',' + IntToStr(T.YLocation) + ',' + IntToStr(T.Size) + ',' ++        IntToStr(Round(T.Rotation)) + ',' + JBool(Shown) + ',[' + IntToStr(R.Left) + ',' + IntToStr(R.Bottom) + ',' + IntToStr(R.Right) + ',' + IntToStr(R.Top) + ']]';+End;++{ silk_state: what the silkscreen planner needs that routing_state does not carry, RAW units.+    comps [["ref","pattern",x,y,rot,bottom,[body l,b,r,t],name,comment]]   name/comment: [text,x,y,size,rot,shown,[box]]+    texts [[layer,text,x,y,size,rot,mirror,[box]]]   free overlay text (not a component's)+    lines [[layer,x1,y1,x2,y2,width]]                free overlay tracks }+Function CmdSilkState(Lines : TStringList) : String;+Var+    Board : IPCB_Board;+    BIt   : IPCB_BoardIterator;+    Comp  : IPCB_Component;+    Prim  : IPCB_Primitive;+    Txt   : IPCB_Text;+    Trk   : IPCB_Track;+    R     : TCoordRect;+    LC, LT, LL : TStringList;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    LC := TStringList.Create; LT := TStringList.Create; LL := TStringList.Create;+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eComponentObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Comp := BIt.FirstPCBObject;+    While Comp <> Nil Do+    Begin+        R := Comp.BoundingRectangleNoNameComment;+        LC.Add('["' + JsonEsc(Comp.Name.Text) + '","' + JsonEsc(Comp.Pattern) + '",' + IntToStr(Comp.x) + ',' + IntToStr(Comp.y) + ',' ++            IntToStr(Round(Comp.Rotation)) + ',' + JBool(Comp.Layer = eBottomLayer) + ',[' + IntToStr(R.Left) + ',' + IntToStr(R.Bottom) + ',' ++            IntToStr(R.Right) + ',' + IntToStr(R.Top) + '],' + TextJson(Comp.Name, Comp.NameOn) + ',' + TextJson(Comp.Comment, Comp.CommentOn) + ']');+        Comp := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eTextObject, eTrackObject));+    BIt.AddFilter_LayerSet(MkSet(eTopOverlay, eBottomOverlay));+    BIt.AddFilter_Method(eProcessAll);+    Prim := BIt.FirstPCBObject;+    While Prim <> Nil Do+    Begin+        If (Not Prim.InComponent) And ((Prim.Layer = eTopOverlay) Or (Prim.Layer = eBottomOverlay)) Then+        Begin+            If Prim.ObjectId = eTextObject Then+            Begin+                Txt := Prim; R := Txt.BoundingRectangle;+                LT.Add('[' + IntToStr(Txt.Layer) + ',"' + JsonEsc(Txt.Text) + '",' + IntToStr(Txt.XLocation) + ',' + IntToStr(Txt.YLocation) + ',' ++                    IntToStr(Txt.Size) + ',' + IntToStr(Round(Txt.Rotation)) + ',' + JBool(Txt.MirrorFlag) + ',[' + IntToStr(R.Left) + ',' ++                    IntToStr(R.Bottom) + ',' + IntToStr(R.Right) + ',' + IntToStr(R.Top) + ']]');+            End+            Else+            Begin+                Trk := Prim;+                LL.Add('[' + IntToStr(Trk.Layer) + ',' + IntToStr(Trk.x1) + ',' + IntToStr(Trk.y1) + ',' + IntToStr(Trk.x2) + ',' + IntToStr(Trk.y2) + ',' + IntToStr(Trk.Width) + ']');+            End;+        End;+        Prim := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+    Result := '{' + JRaw('ok','true') + ',' + JRaw('top', IntToStr(eTopOverlay)) + ',' + JRaw('bottom', IntToStr(eBottomOverlay)) + ',' ++        JRaw('comps', JoinList(LC)) + ',' + JRaw('texts', JoinList(LT)) + ',' + JRaw('lines', JoinList(LL)) + '}';+    LC.Free; LT.Free; LL.Free;+End;++Function CompByRef(Board : IPCB_Board; Ref : String) : IPCB_Component;+Var+    BIt  : IPCB_BoardIterator;+    Comp : IPCB_Component;+Begin+    Result := Nil;+    BIt := Board.BoardIterator_Create;+    BIt.AddFilter_ObjectSet(MkSet(eComponentObject));+    BIt.AddFilter_LayerSet(AllLayers);+    BIt.AddFilter_Method(eProcessAll);+    Comp := BIt.FirstPCBObject;+    While Comp <> Nil Do+    Begin+        If Comp.Name.Text = Ref Then Begin Result := Comp; Break; End;+        Comp := BIt.NextPCBObject;+    End;+    Board.BoardIterator_Destroy(BIt);+End;++{ apply_silk: payload lines, RAW units, one undo step.+    N|ref|x|y|size|rot|show       the designator (x,y = the text's own location)+    V|ref|x|y|size|rot|show       the comment (value)+    T|T or B|text|x|y|size|rot    a free overlay text (B = bottom overlay, mirrored)+    L|T or B|x1|y1|x2|y2|width    a free overlay line+    DT|layer|text|x|y             delete the free overlay text at exactly that spot+    DL|layer|x1|y1|x2|y2          delete the free overlay line with exactly those ends }+Procedure SetText(T : IPCB_Text; X, Y, Sz, Rot : Integer);+Begin+    T.BeginModify;+    T.UseTTFonts := False;+    T.Size := Sz;+    T.Width := Sz Div 7;+    T.Rotation := Rot;+    T.XLocation := X;+    T.YLocation := Y;+    T.EndModify;+    T.GraphicallyInvalidate;+End;++Function CmdApplySilk(Lines : TStringList; Payload : String) : String;+Var+    Board : IPCB_Board;+    Ops   : TStringList;+    Op, Kind, Res, Results : String;+    Comp  : IPCB_Component;+    Txt   : IPCB_Text;+    Trk   : IPCB_Track;+    BIt   : IPCB_BoardIterator;+    Prim  : IPCB_Primitive;+    Lay, I, Okay, Failed : Integer;+Begin+    Board := PCBServer.GetCurrentPCBBoard;+    If Board = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','no current PCB board (open a .PcbDoc first)') + '}'; Exit; End;+    Ops := TStringList.Create; Ops.Text := Payload;+    Results := ''; Okay := 0; Failed := 0;+    PCBServer.PreProcess;+    For I := 0 To Ops.Count - 1 Do+    Begin+        Op := Trim(Ops[I]);+        If Op = '' Then Continue;+        Kind := FieldP(Op, 1); Res := 'ok'; gInputError := '';+        If (Kind = 'N') Or (Kind = 'V') Then+        Begin+            Comp := CompByRef(Board, FieldP(Op, 2));+            If Comp = Nil Then Res := 'err:no part ' + FieldP(Op, 2)+            Else+            Begin+                Comp.BeginModify;+                If Kind = 'N' Then+                Begin+                    Comp.NameAutoPosition := eAutoPos_Manual;+                    Comp.NameOn := FieldP(Op, 7) <> '0';+                    SetText(Comp.Name, AdomInt(FieldP(Op, 3)), AdomInt(FieldP(Op, 4)), AdomInt(FieldP(Op, 5)), AdomInt(FieldP(Op, 6)));+                End+                Else+                Begin+                    Comp.CommentAutoPosition := eAutoPos_Manual;+                    Comp.CommentOn := FieldP(Op, 7) <> '0';+                    SetText(Comp.Comment, AdomInt(FieldP(Op, 3)), AdomInt(FieldP(Op, 4)), AdomInt(FieldP(Op, 5)), AdomInt(FieldP(Op, 6)));+                End;+                Comp.EndModify;+                Comp.GraphicallyInvalidate;+            End;+        End+        Else If (Kind = 'T') Or (Kind = 'L') Then+        Begin+            If FieldP(Op, 2) = 'B' Then Lay := eBottomOverlay Else Lay := eTopOverlay;+            If Kind = 'T' Then+            Begin+                Txt := PCBServer.PCBObjectFactory(eTextObject, eNoDimension, eCreate_Default);+                Txt.Layer := Lay;+                Txt.Text := FieldP(Op, 3);+                Txt.MirrorFlag := Lay = eBottomOverlay;+                SetText(Txt, AdomInt(FieldP(Op, 4)), AdomInt(FieldP(Op, 5)), AdomInt(FieldP(Op, 6)), AdomInt(FieldP(Op, 7)));+                If gInputError = '' Then RegisterPrim(Board, Txt);+            End+            Else+            Begin+                Trk := PCBServer.PCBObjectFactory(eTrackObject, eNoDimension, eCreate_Default);+                Trk.Layer := Lay;+                Trk.x1 := AdomInt(FieldP(Op, 3)); Trk.y1 := AdomInt(FieldP(Op, 4));+                Trk.x2 := AdomInt(FieldP(Op, 5)); Trk.y2 := AdomInt(FieldP(Op, 6));+                Trk.Width := AdomInt(FieldP(Op, 7));+                If gInputError = '' Then RegisterPrim(Board, Trk);+            End;+        End+        Else If (Kind = 'DT') Or (Kind = 'DL') Then+        Begin+            Res := 'err:not found';+            BIt := Board.BoardIterator_Create;+            If Kind = 'DT' Then BIt.AddFilter_ObjectSet(MkSet(eTextObject)) Else BIt.AddFilter_ObjectSet(MkSet(eTrackObject));+            BIt.AddFilter_LayerSet(MkSet(eTopOverlay, eBottomOverlay));+            BIt.AddFilter_Method(eProcessAll);+            Prim := BIt.FirstPCBObject;+            While Prim <> Nil Do+            Begin+                If (Not Prim.InComponent) And (IntToStr(Prim.Layer) = FieldP(Op, 2)) Then+                Begin+                    If Kind = 'DT' Then+                    Begin+                        Txt := Prim;+                        If (Txt.Text = FieldP(Op, 3)) And (IntToStr(Txt.XLocation) = FieldP(Op, 4)) And (IntToStr(Txt.YLocation) = FieldP(Op, 5)) Then Begin Res := 'ok'; Break; End;+                    End+                    Else+                    Begin+                        Trk := Prim;+                        If (IntToStr(Trk.x1) = FieldP(Op, 3)) And (IntToStr(Trk.y1) = FieldP(Op, 4)) And (IntToStr(Trk.x2) = FieldP(Op, 5)) And (IntToStr(Trk.y2) = FieldP(Op, 6)) Then Begin Res := 'ok'; Break; End;+                    End;+                End;+                Prim := BIt.NextPCBObject;+            End;+            Board.BoardIterator_Destroy(BIt);+            If Res = 'ok' Then Board.RemovePCBObject(Prim);+        End+        Else Res := 'err:unknown op ' + Kind;+        If gInputError <> '' Then Res := 'err:bad number ' + gInputError;+        If Copy(Res, 1, 3) = 'err' Then Failed := Failed + 1 Else Okay := Okay + 1;+        If Results <> '' Then Results := Results + ',';+        Results := Results + '"' + JsonEsc(Res) + '"';+    End;+    PCBServer.PostProcess;+    gInputError := '';+    Board.ViewManager_FullUpdate;+    Ops.Free;+    Result := '{' + JRaw('ok','true') + ',' + JRaw('applied', IntToStr(Okay)) + ',' + JRaw('failed', IntToStr(Failed)) + ',' + JRaw('results', '[' + Results + ']') + '}';+End;++{ close_old_agents: every earlier generation of this agent's script project still open in Altium's+  Projects panel (each start opens a new AdomAgent_<gen>.PrjScr; the old ones piled up, ten deep by+  2026-10-15). The running generation stays. Answers how many it closed. }+Function CloseOldAgents : Integer;+Var+    WS   : IWorkspace;+    Prj  : IProject;+    Doc  : IServerDocument;+    I    : Integer;+    N    : String;+Begin+    Result := 0;+    WS := GetWorkspace;+    If WS = Nil Then Exit;+    For I := WS.DM_ProjectCount - 1 DownTo 0 Do+    Begin+        Prj := WS.DM_Projects(I);+        If Prj = Nil Then Continue;+        N := Prj.DM_ProjectFullPath;+        If (Pos('\AdomAgent_', N) > 0) And (Pos('.PRJSCR', UpperCase(N)) > 0) And (Pos(AGENT_GEN, N) = 0) Then+        Begin+            Doc := Nil;+            Try+                Doc := Client.GetDocumentByPath(Prj.DM_ProjectFullPath);+            Except+                Doc := Nil;+            End;+            If Doc <> Nil Then+            Begin+                Try+                    Client.CloseDocument(Doc);+                    Result := Result + 1;+                Except+                End;+            End;+        End;+    End;+End;++Function CmdCloseOldAgents(Lines : TStringList) : String;+Begin+    { test hook for adom/altium-bridge#30, inert unless the bridge's tests drop this file: an agent that halts on its+      first command after starting }+    If FileExists(WORKSPACE + 'test_halt_after_start') Then AdomDeliberateUndeclaredIdentifierForSelfTest(30);+    Result := '{' + JRaw('ok','true') + ',' + JRaw('closed', IntToStr(CloseOldAgents)) + '}';+End;++{ ---------------------------------------------------------------------------------------------------------------+  Schematic wiring as data (the EDA Bridge Standard's sch_* verbs). The bridge does the planning in Rust; the agent+  only dumps a sheet's wiring and applies a list of primitive steps as one undo step.+  Every item has a KEY made of its kind and raw coordinates (and text): Altium gives wires, labels and junctions no+  stable id, so the key is the id, valid until the item changes. }++Function I2S(N : Integer) : String;+Begin+    Result := IntToStr(N);+End;++Function SchKey(Obj : ISch_GraphicalObject) : String;+Var+    J : Integer;+    S : String;+Begin+    S := '';+    If (Obj.ObjectID = eWire) Or (Obj.ObjectID = eBus) Then+    Begin+        If Obj.ObjectID = eWire Then S := 'W:' Else S := 'B:';+        For J := 1 To Obj.VerticesCount Do+        Begin+            If J > 1 Then S := S + ',';+            S := S + I2S(Obj.Vertex[J].X) + ',' + I2S(Obj.Vertex[J].Y);+        End;+    End+    Else If Obj.ObjectID = eBusEntry Then S := 'E:' + I2S(Obj.Location.X) + ',' + I2S(Obj.Location.Y) + ',' + I2S(Obj.Corner.X) + ',' + I2S(Obj.Corner.Y)+    Else If Obj.ObjectID = eJunction Then S := 'J:' + I2S(Obj.Location.X) + ',' + I2S(Obj.Location.Y)+    Else If Obj.ObjectID = eNoERC Then S := 'X:' + I2S(Obj.Location.X) + ',' + I2S(Obj.Location.Y)+    Else If Obj.ObjectID = eNetLabel Then S := 'N:' + I2S(Obj.Location.X) + ',' + I2S(Obj.Location.Y) + ':' + Obj.Text+    Else If Obj.ObjectID = ePort Then S := 'P:' + I2S(Obj.Location.X) + ',' + I2S(Obj.Location.Y) + ':' + Obj.Name+    Else If Obj.ObjectID = ePowerObject Then S := 'O:' + I2S(Obj.Location.X) + ',' + I2S(Obj.Location.Y) + ':' + Obj.Text+    Else If Obj.ObjectID = eLabel Then S := 'T:' + I2S(Obj.Location.X) + ',' + I2S(Obj.Location.Y) + ':' + Obj.Text+    Else If Obj.ObjectID = eSheetSymbol Then S := 'S:' + I2S(Obj.Location.X) + ',' + I2S(Obj.Location.Y) + ':' + Obj.SheetName.Text;+    Result := S;+End;++{ sch_dump: every wiring item of the CURRENT sheet with its key and raw coordinates, every pin of every part (outer+  end = Location moved PinLength along Orientation, as sch_connect measured), and the sheet's page. k is+  MilsToCoord(1000): the bridge turns raw units into mm with it. }+Function CmdSchDump(Lines : TStringList) : String;+Var+    Doc  : ISch_Document;+    It, PIt, EIt : ISch_Iterator;+    Obj  : ISch_GraphicalObject;+    C    : ISch_Component;+    Pin  : ISch_Pin;+    En   : ISch_SheetEntry;+    S, P, V, En2 : String;+    J, Dir, HX, HY : Integer;+    BL, BB, BR, BT : Integer;+    HasB : Boolean;+    GIt  : ISch_Iterator;+    G    : ISch_GraphicalObject;+    R    : TCoordRect;+Begin+    Doc := SchServer.GetCurrentSchDocument;+    If (Doc = Nil) Or (Doc.ObjectID = eSchLib) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_schematic') + ',' + JStr('error','no current schematic sheet (open a .SchDoc first)') + '}'; Exit; End;+    S := ''; P := '';+    It := Doc.SchIterator_Create;+    It.AddFilter_ObjectSet(MkSet(eWire, eBus, eBusEntry, eJunction, eNoERC, eNetLabel, ePort, ePowerObject, eLabel, eSheetSymbol, eSchComponent));+    { top-level items only: a part's own texts and lines come with the part (they moved with it and then could not be+      found by key, measured 2026-10-27) }+    It.SetState_IterationDepth(eIterateFirstLevel);+    Obj := It.FirstSchObject;+    While Obj <> Nil Do+    Begin+        If Obj.ObjectID = eSchComponent Then+        Begin+            C := Obj;+            { the symbol's box (sheetDrawing): its pins and the body drawn for the part and display mode it shows,+              never its designator or comment }+            HasB := False; BL := 0; BB := 0; BR := 0; BT := 0;+            GIt := C.SchIterator_Create;+            GIt.AddFilter_ObjectSet(MkSet(eRectangle, eRoundRectangle, eLine, ePolyline, ePolygon, eArc, eEllipse, eEllipticalArc, ePie, eBezier));+            G := GIt.FirstSchObject;+            While G <> Nil Do+            Begin+                If ((G.OwnerPartId = C.CurrentPartID) Or (G.OwnerPartId = 0)) And (G.OwnerPartDisplayMode = C.DisplayMode) Then+                Begin+                    R := G.BoundingRectangle;+                    If Not HasB Then Begin BL := R.Left; BB := R.Bottom; BR := R.Right; BT := R.Top; HasB := True; End+                    Else Begin If R.Left < BL Then BL := R.Left; If R.Bottom < BB Then BB := R.Bottom; If R.Right > BR Then BR := R.Right; If R.Top > BT Then BT := R.Top; End;+                End;+                G := GIt.NextSchObject;+            End;+            C.SchIterator_Destroy(GIt);+            PIt := C.SchIterator_Create;+            PIt.AddFilter_ObjectSet(MkSet(ePin));+            Pin := PIt.FirstSchObject;+            While Pin <> Nil Do+            Begin+                If ((Pin.OwnerPartId = C.CurrentPartID) Or (Pin.OwnerPartId = 0)) And (Pin.OwnerPartDisplayMode = C.DisplayMode) Then+                Begin+                    Dir := Pin.Orientation;+                    HX := Pin.Location.X; HY := Pin.Location.Y;+                    If Dir = 0 Then HX := HX + Pin.PinLength+                    Else If Dir = 1 Then HY := HY + Pin.PinLength+                    Else If Dir = 2 Then HX := HX - Pin.PinLength+                    Else HY := HY - Pin.PinLength;+                    If Not HasB Then Begin BL := HX; BB := HY; BR := HX; BT := HY; HasB := True; End;+                    If HX < BL Then BL := HX; If HX > BR Then BR := HX; If HY < BB Then BB := HY; If HY > BT Then BT := HY;+                    If Pin.Location.X < BL Then BL := Pin.Location.X; If Pin.Location.X > BR Then BR := Pin.Location.X;+                    If Pin.Location.Y < BB Then BB := Pin.Location.Y; If Pin.Location.Y > BT Then BT := Pin.Location.Y;+                    If P <> '' Then P := P + ',';+                    P := P + '["' + JsonEsc(C.Designator.Text) + '","' + JsonEsc(Pin.Designator) + '","' + JsonEsc(Pin.Name) + '",' ++                        I2S(Pin.Electrical) + ',' + I2S(HX) + ',' + I2S(HY) + ',' + I2S(Dir) + ',' + JBool(Pin.IsHidden) + ',' ++                        I2S(Pin.Location.X) + ',' + I2S(Pin.Location.Y) + ']';+                End;+                Pin := PIt.NextSchObject;+            End;+            C.SchIterator_Destroy(PIt);+            If S <> '' Then S := S + ',';+            S := S + '{"t":"C","key":"","ref":"' + JsonEsc(C.Designator.Text) + '","x":' + I2S(C.Location.X) + ',"y":' + I2S(C.Location.Y) ++                ',"lib":"' + JsonEsc(C.LibReference) + '","libPath":"' + JsonEsc(C.LibraryPath) + '","part":' + I2S(C.CurrentPartID) + ',"rot":' + I2S(C.Orientation) + ',"mirror":' + JBool(C.IsMirrored) ++                ',"des":[' + I2S(C.Designator.Location.X) + ',' + I2S(C.Designator.Location.Y) + ',' + I2S(C.Designator.Orientation) + ',' + I2S(C.Designator.Justification) + ']' ++                ',"cmt":[' + I2S(C.Comment.Location.X) + ',' + I2S(C.Comment.Location.Y) + ',' + I2S(C.Comment.Orientation) + ',' + I2S(C.Comment.Justification) + ']' ++                ',"box":[' + I2S(BL) + ',' + I2S(BB) + ',' + I2S(BR) + ',' + I2S(BT) + ']}';+        End+        Else+        Begin+            V := '';+            If (Obj.ObjectID = eWire) Or (Obj.ObjectID = eBus) Then+            Begin+                For J := 1 To Obj.VerticesCount Do+                Begin+                    If J > 1 Then V := V + ',';+                    V := V + '[' + I2S(Obj.Vertex[J].X) + ',' + I2S(Obj.Vertex[J].Y) + ']';+                End;+                V := '"pts":[' + V + ']';+            End+            Else+            Begin+                V := '"x":' + I2S(Obj.Location.X) + ',"y":' + I2S(Obj.Location.Y);+                If Obj.ObjectID = eBusEntry Then V := V + ',"cx":' + I2S(Obj.Corner.X) + ',"cy":' + I2S(Obj.Corner.Y)+                Else If (Obj.ObjectID = eNetLabel) Or (Obj.ObjectID = eLabel) Then V := V + ',"text":"' + JsonEsc(Obj.Text) + '","rot":' + I2S(Obj.Orientation) + ',"just":' + I2S(Obj.Justification)+                Else If Obj.ObjectID = ePowerObject Then V := V + ',"text":"' + JsonEsc(Obj.Text) + '","rot":' + I2S(Obj.Orientation) + ',"style":' + I2S(Obj.Style)+                Else If Obj.ObjectID = ePort Then V := V + ',"text":"' + JsonEsc(Obj.Name) + '","w":' + I2S(Obj.Width) + ',"style":' + I2S(Obj.Style) + ',"io":' + I2S(Obj.IOType)+                Else If Obj.ObjectID = eSheetSymbol Then+                Begin+                    En2 := '';+                    EIt := Obj.SchIterator_Create;+                    EIt.AddFilter_ObjectSet(MkSet(eSheetEntry));+                    En := EIt.FirstSchObject;+                    While En <> Nil Do+                    Begin+                        If En2 <> '' Then En2 := En2 + ',';+                        En2 := En2 + '["' + JsonEsc(En.Name) + '",' + I2S(En.Side) + ',' + I2S(En.DistanceFromTop) + ',' + I2S(En.IOType) + ']';+                        En := EIt.NextSchObject;+                    End;+                    Obj.SchIterator_Destroy(EIt);+                    V := V + ',"text":"' + JsonEsc(Obj.SheetName.Text) + '","file":"' + JsonEsc(Obj.SheetFileName.Text) + '","w":' + I2S(Obj.XSize) + ',"h":' + I2S(Obj.YSize) + ',"entries":[' + En2 + ']';+                End;+            End;+            If S <> '' Then S := S + ',';+            S := S + '{"t":"' + Copy(SchKey(Obj), 1, 1) + '","key":"' + JsonEsc(SchKey(Obj)) + '",' + V + '}';+        End;+        Obj := It.NextSchObject;+    End;+    Doc.SchIterator_Destroy(It);+    Result := '{' + JRaw('ok','true') + ',' + JStr('document', Doc.DocumentName) + ',' + JRaw('k', I2S(MilsToCoord(1000))) + ',' ++        JRaw('page', '{"style":' + I2S(Doc.SheetStyle) + ',"custom":' + JBool(Doc.UseCustomSheet) + ',"cx":' + I2S(Doc.CustomX) + ',"cy":' + I2S(Doc.CustomY) + ',"orientation":' + I2S(Doc.WorkspaceOrientation) + '}') + ',' ++        JRaw('items', '[' + S + ']') + ',' + JRaw('pins', '[' + P + ']') + '}';+End;++Function SchFindByKey(Doc : ISch_Document; Key : String) : ISch_GraphicalObject;+Var+    It  : ISch_Iterator;+    Obj : ISch_GraphicalObject;+Begin+    Result := Nil;+    It := Doc.SchIterator_Create;+    It.AddFilter_ObjectSet(MkSet(eWire, eBus, eBusEntry, eJunction, eNoERC, eNetLabel, ePort, ePowerObject, eLabel, eSheetSymbol));+    It.SetState_IterationDepth(eIterateFirstLevel);+    Obj := It.FirstSchObject;+    While Obj <> Nil Do+    Begin+        If SchKey(Obj) = Key Then Begin Result := Obj; Break; End;+        Obj := It.NextSchObject;+    End;+    Doc.SchIterator_Destroy(It);+End;++Procedure SchRegister(Doc : ISch_Document; Obj : ISch_GraphicalObject);+Begin+    Doc.RegisterSchObjectInContainer(Obj);+    SchServer.RobotManager.SendMessage(Doc.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, Obj.I_ObjectAddress);+End;++{ points "x,y,x,y,..." (raw) onto a new or existing wire/bus }+Procedure SchSetVertices(Obj : ISch_GraphicalObject; Pts : String);+Var+    L : TStringList;+    J, N : Integer;+Begin+    L := TStringList.Create; L.Delimiter := ','; L.StrictDelimiter := True; L.DelimitedText := Pts;+    N := L.Count Div 2;+    Obj.Location := Point(AdomInt(L[0]), AdomInt(L[1]));+    Obj.VerticesCount := N;+    For J := 1 To N Do Obj.Vertex[J] := Point(AdomInt(L[2 * J - 2]), AdomInt(L[2 * J - 1]));+    L.Free;+End;++{ sch_ops: primitive steps, one undo step. Fields after the kind letter, | separated, raw coordinates:+    W|pts      a new wire through the points "x,y,x,y,..."      B|pts   a new bus+    E|x|y|cx|cy  a bus entry from (x,y) to the corner (cx,cy)      J|x|y   a junction      X|x|y  a No ERC marker+    N|text|x|y|rot|just  a net label                               T|text|x|y|rot|pt|bold|italic|just  free text+    D|key      delete the item with that key                       V|key|pts  give a wire/bus new points+    G|key|dx|dy  move an item by dx, dy                            L|key|text|x|y|rot  edit a net label+    C|ref|dx|dy  move a part by dx, dy+  Each step answers ok, or why not, and the key it made or touched. }+Function CmdSchOps(Lines : TStringList; Payload : String) : String;+Var+    Doc  : ISch_Document;+    Ops  : TStringList;+    CL   : TInterfaceList;+    Obj  : ISch_GraphicalObject;+    C    : ISch_Component;+    En   : ISch_SheetEntry;+    EIt  : ISch_Iterator;+    Op, K, Res, NewKey, S : String;+    I, J, NOk : Integer;+Begin+    Doc := SchServer.GetCurrentSchDocument;+    If (Doc = Nil) Or (Doc.ObjectID = eSchLib) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','no_current_schematic') + ',' + JStr('error','no current schematic sheet (open a .SchDoc first)') + '}'; Exit; End;+    Ops := TStringList.Create; Ops.Text := Payload;+    S := ''; NOk := 0;+    SchServer.ProcessControl.PreProcess(Doc, '');+    For I := 0 To Ops.Count - 1 Do+    Begin+        Op := Ops[I];+        If Trim(Op) = '' Then Continue;+        K := FieldP(Op, 1); Res := 'ok'; NewKey := ''; gInputError := '';+        If (K = 'W') Or (K = 'B') Then+        Begin+            If K = 'W' Then Obj := SchServer.SchObjectFactory(eWire, eCreate_Default) Else Obj := SchServer.SchObjectFactory(eBus, eCreate_Default);+            SchSetVertices(Obj, FieldP(Op, 2));+            Obj.LineWidth := eSmall;+            SchRegister(Doc, Obj); NewKey := SchKey(Obj);+        End+        Else If K = 'E' Then+        Begin+            Obj := SchServer.SchObjectFactory(eBusEntry, eCreate_Default);+            Obj.Location := Point(AdomInt(FieldP(Op, 2)), AdomInt(FieldP(Op, 3)));+            Obj.Corner := Point(AdomInt(FieldP(Op, 4)), AdomInt(FieldP(Op, 5)));+            SchRegister(Doc, Obj); NewKey := SchKey(Obj);+        End+        Else If (K = 'J') Or (K = 'X') Then+        Begin+            If K = 'J' Then Obj := SchServer.SchObjectFactory(eJunction, eCreate_Default) Else Obj := SchServer.SchObjectFactory(eNoERC, eCreate_Default);+            Obj.Location := Point(AdomInt(FieldP(Op, 2)), AdomInt(FieldP(Op, 3)));+            SchRegister(Doc, Obj); NewKey := SchKey(Obj);+        End+        Else If K = 'N' Then+        Begin+            Obj := SchServer.SchObjectFactory(eNetLabel, eCreate_Default);+            Obj.Text := FieldP(Op, 2);+            Obj.Location := Point(AdomInt(FieldP(Op, 3)), AdomInt(FieldP(Op, 4)));+            Obj.Orientation := AdomInt(FieldP(Op, 5));+            If FieldP(Op, 6) <> '' Then Obj.Justification := AdomInt(FieldP(Op, 6));+            SchRegister(Doc, Obj); NewKey := SchKey(Obj);+        End+        Else If K = 'O' Then+        Begin+            { O|net|x|y|orientation: a power port, Altium's global net label }+            Obj := SchServer.SchObjectFactory(ePowerObject, eCreate_Default);+            Obj.Text := FieldP(Op, 2);+            Obj.Location := Point(AdomInt(FieldP(Op, 3)), AdomInt(FieldP(Op, 4)));+            Obj.Style := PowerStyleOf(FieldP(Op, 2));+            Obj.Orientation := AdomInt(FieldP(Op, 5));+            Obj.ShowNetName := True;+            SchRegister(Doc, Obj); NewKey := SchKey(Obj);+        End+        Else If K = 'P' Then+        Begin+            { P|name|x|y|left(1/0): a port, Altium's hierarchical label, its connecting end at x,y }+            Obj := SchServer.SchObjectFactory(ePort, eCreate_Default);+            Obj.Name := FieldP(Op, 2);+            Obj.Width := MilsToCoord(600);+            Obj.IOType := ePortUnspecified;+            If FieldP(Op, 5) = '1' Then Begin Obj.Style := ePortLeft; Obj.Location := Point(AdomInt(FieldP(Op, 3)) - Obj.Width, AdomInt(FieldP(Op, 4))); End+            Else Begin Obj.Style := ePortRight; Obj.Location := Point(AdomInt(FieldP(Op, 3)), AdomInt(FieldP(Op, 4))); End;+            SchRegister(Doc, Obj); NewKey := SchKey(Obj);+        End+        Else If K = 'T' Then+        Begin+            Obj := SchServer.SchObjectFactory(eLabel, eCreate_Default);+            Obj.Text := FieldP(Op, 2);+            Obj.Location := Point(AdomInt(FieldP(Op, 3)), AdomInt(FieldP(Op, 4)));+            Obj.Orientation := AdomInt(FieldP(Op, 5));+            If AdomInt(FieldP(Op, 6)) > 0 Then+                Obj.FontId := SchServer.FontManager.GetFontID(AdomInt(FieldP(Op, 6)), 0, False, FieldP(Op, 8) = '1', FieldP(Op, 7) = '1', False, 'Times New Roman');+            If FieldP(Op, 9) <> '' Then Obj.Justification := AdomInt(FieldP(Op, 9));+            SchRegister(Doc, Obj); NewKey := SchKey(Obj);+        End+        Else If K = 'C' Then+        Begin+            CL := SheetCompsOf(Doc, FieldP(Op, 2));+            If CL.Count = 0 Then Res := 'designator not on this sheet';+            For J := 0 To CL.Count - 1 Do+            Begin+                C := CL[J];+                SchServer.RobotManager.SendMessage(C.I_ObjectAddress, c_BroadCast, SCHM_BeginModify, c_NoEventData);+                C.MoveByXY(AdomInt(FieldP(Op, 3)), AdomInt(FieldP(Op, 4)));+                SchServer.RobotManager.SendMessage(C.I_ObjectAddress, c_BroadCast, SCHM_EndModify, c_NoEventData);+                C.GraphicallyInvalidate;+            End;+            CL.Free;+            NewKey := FieldP(Op, 2);+        End+        Else If K = 'A' Then+        Begin+            { A|style|custom(1/0)|cx|cy|orientation: the page (empty keeps a field) }+            If FieldP(Op, 2) <> '' Then Doc.SheetStyle := AdomInt(FieldP(Op, 2));+            If FieldP(Op, 3) <> '' Then Doc.UseCustomSheet := FieldP(Op, 3) = '1';+            If FieldP(Op, 4) <> '' Then Doc.CustomX := AdomInt(FieldP(Op, 4));+            If FieldP(Op, 5) <> '' Then Doc.CustomY := AdomInt(FieldP(Op, 5));+            If FieldP(Op, 6) <> '' Then Doc.WorkspaceOrientation := AdomInt(FieldP(Op, 6));+        End+        Else If K = 'F' Then+        Begin+            { F|ref|designator or comment|x|y|orientation|justification: put a part's field text there }+            CL := SheetCompsOf(Doc, FieldP(Op, 2));+            If CL.Count = 0 Then Res := 'designator not on this sheet';+            For J := 0 To CL.Count - 1 Do+            Begin+                C := CL[J];+                SchServer.RobotManager.SendMessage(C.I_ObjectAddress, c_BroadCast, SCHM_BeginModify, c_NoEventData);+                If LowerCase(FieldP(Op, 3)) = 'comment' Then Obj := C.Comment Else Obj := C.Designator;+                Obj.Location := Point(AdomInt(FieldP(Op, 4)), AdomInt(FieldP(Op, 5)));+                Obj.Orientation := AdomInt(FieldP(Op, 6));+                Obj.Justification := AdomInt(FieldP(Op, 7));+                SchServer.RobotManager.SendMessage(C.I_ObjectAddress, c_BroadCast, SCHM_EndModify, c_NoEventData);+                C.GraphicallyInvalidate;+            End;+            CL.Free;+        End+        Else If (K = 'Y') Or (K = 'Z') Or (K = 'Q') Then+        Begin+            { sheet symbol pins. Y|symkey|name|side|dist|io adds one; Z|symkey|name deletes it;+              Q|symkey|name|newname|side|dist|io edits it (empty keeps a field) }+            Obj := SchFindByKey(Doc, FieldP(Op, 2));+            If Obj = Nil Then Res := 'not found: the sheet symbol changed since it was read'+            Else+            Begin+                SchServer.RobotManager.SendMessage(Obj.I_ObjectAddress, c_BroadCast, SCHM_BeginModify, c_NoEventData);+                If K = 'Y' Then+                Begin+                    En := SchServer.SchObjectFactory(eSheetEntry, eCreate_Default);+                    En.Name := FieldP(Op, 3);+                    En.Side := AdomInt(FieldP(Op, 4));+                    En.DistanceFromTop := AdomInt(FieldP(Op, 5));+                    En.IOType := AdomInt(FieldP(Op, 6));+                    Obj.AddSchObject(En);+                    SchServer.RobotManager.SendMessage(Obj.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, En.I_ObjectAddress);+                End+                Else+                Begin+                    EIt := Obj.SchIterator_Create;+                    EIt.AddFilter_ObjectSet(MkSet(eSheetEntry));+                    En := EIt.FirstSchObject;+                    While (En <> Nil) And (UpperCase(En.Name) <> UpperCase(FieldP(Op, 3))) Do En := EIt.NextSchObject;+                    Obj.SchIterator_Destroy(EIt);+                    If En = Nil Then Res := 'no pin ' + FieldP(Op, 3)+                    Else If K = 'Z' Then Obj.RemoveSchObject(En)+                    Else+                    Begin+                        If FieldP(Op, 4) <> '' Then En.Name := FieldP(Op, 4);+                        If FieldP(Op, 5) <> '' Then En.Side := AdomInt(FieldP(Op, 5));+                        If FieldP(Op, 6) <> '' Then En.DistanceFromTop := AdomInt(FieldP(Op, 6));+                        If FieldP(Op, 7) <> '' Then En.IOType := AdomInt(FieldP(Op, 7));+                    End;+                End;+                SchServer.RobotManager.SendMessage(Obj.I_ObjectAddress, c_BroadCast, SCHM_EndModify, c_NoEventData);+                Obj.GraphicallyInvalidate;+                NewKey := SchKey(Obj);+            End;+        End+        Else If (K = 'D') Or (K = 'V') Or (K = 'G') Or (K = 'L') Then+        Begin+            Obj := SchFindByKey(Doc, FieldP(Op, 2));+            If Obj = Nil Then Res := 'not found: the item changed since it was read'+            Else If K = 'D' Then+            Begin+                Doc.RemoveSchObject(Obj);+                SchServer.RobotManager.SendMessage(Doc.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, Obj.I_ObjectAddress);+            End+            Else+            Begin+                SchServer.RobotManager.SendMessage(Obj.I_ObjectAddress, c_BroadCast, SCHM_BeginModify, c_NoEventData);+                If K = 'V' Then SchSetVertices(Obj, FieldP(Op, 3))+                Else If K = 'G' Then Obj.MoveByXY(AdomInt(FieldP(Op, 3)), AdomInt(FieldP(Op, 4)))+                Else+                Begin+                    If FieldP(Op, 3) <> '' Then Obj.Text := FieldP(Op, 3);+                    If FieldP(Op, 4) <> '' Then Obj.Location := Point(AdomInt(FieldP(Op, 4)), AdomInt(FieldP(Op, 5)));+                    If FieldP(Op, 6) <> '' Then Obj.Orientation := AdomInt(FieldP(Op, 6));+                End;+                SchServer.RobotManager.SendMessage(Obj.I_ObjectAddress, c_BroadCast, SCHM_EndModify, c_NoEventData);+                Obj.GraphicallyInvalidate;+                NewKey := SchKey(Obj);+            End;+        End+        Else Res := 'unknown step ' + K;+        If gInputError <> '' Then Res := 'bad number ' + gInputError;+        If Res = 'ok' Then NOk := NOk + 1;+        If S <> '' Then S := S + ',';+        S := S + '{' + JStr('result', Res) + ',' + JStr('key', NewKey) + '}';+    End;+    SchServer.ProcessControl.PostProcess(Doc, '');+    K := MarkSchModified(Doc);+    Doc.GraphicallyInvalidate;+    Ops.Free;+    Result := '{' + JRaw('ok','true') + ',' + JStr('document', Doc.DocumentName) + ',' + JStr('marked', K) + ',' + JRaw('applied', I2S(NOk)) + ',' + JRaw('results', '[' + S + ']') + '}';+End;+++{ docs: every document open in Altium (each server's documents): file, kind, unsaved changes, and which one is in+  front (the focused view's document). }+Function CmdDocs(Lines : TStringList) : String;+Var+    SM   : IServerModule;+    D    : IServerDocument;+    V    : IServerDocumentView;+    S, Cur, Kd : String;+    I, J : Integer;+    Md   : Boolean;+Begin+    S := ''; Cur := '';+    Try+        V := Client.GetCurrentView;+        If (V <> Nil) And (V.OwnerDocument <> Nil) Then Cur := V.OwnerDocument.FileName;+    Except+    End;+    For I := 0 To Client.Count - 1 Do+    Begin+        SM := Client.ServerModule[I];+        If SM = Nil Then Continue;+        For J := 0 To SM.DocumentCount - 1 Do+        Begin+            D := SM.Documents[J];+            If D = Nil Then Continue;+            Kd := ''; Md := False;+            Try Kd := D.Kind; Except End;+            Try If D.Modified Then Md := True; Except End;+            Try If D.GetModified Then Md := True; Except End;+            If S <> '' Then S := S + ',';+            S := S + '{' + JStr('file', D.FileName) + ',' + JStr('kind', Kd) + ',' + JRaw('modified', JBool(Md)) + '}';+        End;+    End;+    Result := '{' + JRaw('ok','true') + ',' + JStr('current', Cur) + ',' + JRaw('documents', '[' + S + ']') + '}';+End;++{ Make the PcbLib at Path the current document (inside Altium), so GetCurrentPCBLibrary is that library. '' when it is,+  else why not. An empty Path leaves the current library as it is. }+Function CurrentLibAt(Path : String) : String;+Var+    SD : IServerDocument;+    PL : IPCB_Library;+Begin+    Result := '';+    If Path = '' Then Exit;+    SD := DocAt(Path);+    If SD = Nil Then Begin Result := Path + ' is not open in Altium'; Exit; End;+    PL := PCBServer.GetCurrentPCBLibrary;+    If (PL <> Nil) And (SamePathText(PL.Board.FileName) = SamePathText(Path)) Then Exit;+    Client.ShowDocument(SD);+    PL := PCBServer.GetCurrentPCBLibrary;+    If (PL = Nil) Or (SamePathText(PL.Board.FileName) <> SamePathText(Path)) Then Result := Path + ' did not become the current library';+End;++{ edit_fp: edit one footprint of the CURRENT PcbLib in place. name= the footprint; the payload is one operation a line,+  positions in mm from the footprint's origin (the library's, as lib_fp reads them):+    PS|pad|size|w|h   PS|pad|at|x|y   PS|pad|shape|rect|round|roundrect   PS|pad|drill|d   PS|pad|rotation|deg+    PS|pad|layer|top|bottom|multi   PS|pad|type|smd|pth|npth   PS|pad|number|new+    PD|pad   PA|name|x|y|w|h|shape|drill|layer|rot|type   GD|layerId   GL|layerId|x1|y1|x2|y2|w   GA|layerId|cx|cy|r|w|a1|a2+    MD|description   BD   (every 3D body)+  Each answers ok:<what> or err:<why>, in order; one undo step. }++{ the first drawing primitive (not a pad, a text or a body) on a layer of the footprint; layer 0 = the first 3D body }+Function FpFirstOn(Fp : IPCB_LibComponent; Lay : Integer) : IPCB_Primitive;+Var+    GIt : IPCB_GroupIterator;+    P   : IPCB_Primitive;+Begin+    Result := Nil;+    GIt := Fp.GroupIterator_Create;+    If Lay = 0 Then GIt.AddFilter_ObjectSet(MkSet(eComponentBodyObject))+    Else GIt.AddFilter_ObjectSet(MkSet(eTrackObject, eArcObject, eFillObject, eRegionObject));+    GIt.AddFilter_LayerSet(AllLayers);+    P := GIt.FirstPCBObject;+    While P <> Nil Do+    Begin+        If (Lay = 0) Or (P.Layer = Lay) Then Begin Result := P; Break; End;+        P := GIt.NextPCBObject;+    End;+    Fp.GroupIterator_Destroy(GIt);+End;++Function FpShape(S : String) : Integer;+Begin+    If S = 'rect' Then Result := eRectangular+    Else If S = 'roundrect' Then Result := eRoundedRectangular+    Else Result := eRounded;+End;++Function FpLayer(S : String) : Integer;+Begin+    If S = 'multi' Then Result := eMultiLayer+    Else If S = 'bottom' Then Result := eBottomLayer+    Else Result := eTopLayer;+End;++Function CmdEditFp(Lines : TStringList; Payload : String) : String;+Var+    LibErr : String;+    PLib  : IPCB_Library;+    Fp    : IPCB_LibComponent;+    Ops   : TStringList;+    Op, Kind, Key, Res, Results : String;+    Pad   : IPCB_Pad;+    Trk   : IPCB_Track;+    Arc   : IPCB_Arc;+    Prim, Cpy : IPCB_Primitive;+    Old   : IPCB_LibComponent;+    GIt   : IPCB_GroupIterator;+    I, K, OX, OY, Lay, Okay, Failed : Integer;+Begin+    { the library the bridge names, as the open document: a library loaded by path is a separate copy (pads added or+      deleted on it never reached the document, 2026-10-28), and open shows a document without making it current }+    LibErr := CurrentLibAt(Param(Lines, 'path'));+    If LibErr <> '' Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','library_not_current') + ',' + JStr('error', LibErr) + '}'; Exit; End;+    PLib := PCBServer.GetCurrentPCBLibrary;+    If PLib = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','not_a_pcblib') + ',' + JStr('error','the current document is not a PCB library') + '}'; Exit; End;+    Fp := FindFp(PLib, Param(Lines, 'name'));+    If Fp = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','footprint_not_found') + ',' + JStr('error','no footprint ' + Param(Lines, 'name') + ' in this library') + '}'; Exit; End;+    { the footprint on display sits on the library's origin (see lib_fp): show it before editing }+    If PLib.CurrentComponent <> Fp Then Begin PLib.CurrentComponent := Fp; PLib.Board.ViewManager_FullUpdate; End;+    OX := PLib.Board.XOrigin; OY := PLib.Board.YOrigin;+    Ops := TStringList.Create;+    Ops.Text := Payload;+    Results := ''; Okay := 0; Failed := 0;+    PCBServer.PreProcess;+    { the edit is made on a copy that is then registered in the footprint's place: on a registered footprint, pads+      and lines added or removed did not reach it (only changes to existing pads did, Develop 2026-10-28), while a+      footprint built before it is registered takes them all (build_footprint, xcopy) }+    Old := Fp;+    Fp := PCBServer.CreatePCBLibComp;+    Fp.Name := Old.Name;+    Fp.Description := Old.Description;+    Fp.Height := Old.Height;+    GIt := Old.GroupIterator_Create;+    GIt.AddFilter_LayerSet(AllLayers);+    Prim := GIt.FirstPCBObject;+    While Prim <> Nil Do+    Begin+        { the footprint on display reads from the library's origin; one not yet registered keeps its own frame,+          which Altium puts on the origin when it shows it (the copy came out 1270 mm off without this) }+        Cpy := Prim.Replicate;+        Cpy.MoveByXY(-OX, -OY);+        Fp.AddPCBObject(Cpy);+        Prim := GIt.NextPCBObject;+    End;+    Old.GroupIterator_Destroy(GIt);+    OX := 0; OY := 0;+    For I := 0 To Ops.Count - 1 Do+    Begin+        Op := Trim(Ops[I]);+        If Op = '' Then Continue;+        Kind := FieldP(Op, 1);+        Res := 'ok';+        gInputError := '';+        If (Kind = 'PS') Or (Kind = 'PD') Then+        Begin+            Pad := FpPad(Fp, FieldP(Op, 2));+            If Pad = Nil Then Res := 'err:no pad ' + FieldP(Op, 2)+            Else If Kind = 'PD' Then+            Begin+                Fp.RemovePCBObject(Pad);+                Res := 'ok:deleted pad ' + FieldP(Op, 2);+            End+            Else+            Begin+                Key := FieldP(Op, 3);+                Pad.BeginModify;+                If Key = 'size' Then Begin Pad.TopXSize := MmC(FieldP(Op, 4)); Pad.TopYSize := MmC(FieldP(Op, 5)); End+                Else If Key = 'at' Then Begin Pad.X := OX + MmC(FieldP(Op, 4)); Pad.Y := OY + MmC(FieldP(Op, 5)); End+                Else If Key = 'shape' Then Pad.TopShape := FpShape(FieldP(Op, 4))+                Else If Key = 'drill' Then Pad.HoleSize := MmC(FieldP(Op, 4))+                Else If Key = 'rotation' Then Pad.Rotation := AdomFloat(FieldP(Op, 4))+                Else If Key = 'layer' Then Pad.Layer := FpLayer(FieldP(Op, 4))+                Else If Key = 'type' Then+                Begin+                    If FieldP(Op, 4) = 'smd' Then Begin Pad.Layer := eTopLayer; Pad.HoleSize := 0; End+                    Else Begin Pad.Layer := eMultiLayer; Pad.Plated := FieldP(Op, 4) = 'pth'; End;+                End+                Else If Key = 'number' Then Pad.Name := FieldP(Op, 4)+                Else Res := 'err:unknown pad field ' + Key;+                Pad.EndModify;+                If Res = 'ok' Then Res := 'ok:pad ' + FieldP(Op, 2) + ' ' + Key;+            End;+        End+        Else If Kind = 'PA' Then+        Begin+            If FpPad(Fp, FieldP(Op, 2)) <> Nil Then Res := 'err:pad ' + FieldP(Op, 2) + ' exists'+            Else+            Begin+                Pad := PCBServer.PCBObjectFactory(ePadObject, eNoDimension, eCreate_Default);+                Pad.Name := FieldP(Op, 2);+                Pad.X := OX + MmC(FieldP(Op, 3)); Pad.Y := OY + MmC(FieldP(Op, 4));+                Pad.TopXSize := MmC(FieldP(Op, 5)); Pad.TopYSize := MmC(FieldP(Op, 6));+                Pad.TopShape := FpShape(FieldP(Op, 7));+                Pad.HoleSize := MmC(FieldP(Op, 8));+                Pad.Layer := FpLayer(FieldP(Op, 9));+                Pad.Rotation := AdomFloat(FieldP(Op, 10));+                If FieldP(Op, 11) = 'npth' Then Pad.Plated := False;+                Fp.AddPCBObject(Pad);+                Res := 'ok:added pad ' + FieldP(Op, 2);+            End;+        End+        Else If (Kind = 'GD') Or (Kind = 'BD') Then+        Begin+            If Kind = 'BD' Then Lay := 0 Else Lay := AdomInt(FieldP(Op, 2));+            K := 0;+            Prim := FpFirstOn(Fp, Lay);+            While (Prim <> Nil) And (K < 5000) Do+            Begin+                Fp.RemovePCBObject(Prim);+                K := K + 1;+                Prim := FpFirstOn(Fp, Lay);+            End;+            If Kind = 'BD' Then Res := 'ok:deleted ' + IntToStr(K) + ' 3D bodies'+            Else Res := 'ok:deleted ' + IntToStr(K) + ' shapes on layer ' + FieldP(Op, 2);+        End+        Else If Kind = 'GL' Then+        Begin+            Trk := PCBServer.PCBObjectFactory(eTrackObject, eNoDimension, eCreate_Default);+            Trk.Layer := AdomInt(FieldP(Op, 2));+            Trk.X1 := OX + MmC(FieldP(Op, 3)); Trk.Y1 := OY + MmC(FieldP(Op, 4));+            Trk.X2 := OX + MmC(FieldP(Op, 5)); Trk.Y2 := OY + MmC(FieldP(Op, 6));+            Trk.Width := MmC(FieldP(Op, 7));+            Fp.AddPCBObject(Trk);+            Res := 'ok:line on layer ' + FieldP(Op, 2);+        End+        Else If Kind = 'GA' Then+        Begin+            Arc := PCBServer.PCBObjectFactory(eArcObject, eNoDimension, eCreate_Default);+            Arc.Layer := AdomInt(FieldP(Op, 2));+            Arc.XCenter := OX + MmC(FieldP(Op, 3)); Arc.YCenter := OY + MmC(FieldP(Op, 4));+            Arc.Radius := MmC(FieldP(Op, 5));+            Arc.LineWidth := MmC(FieldP(Op, 6));+            Arc.StartAngle := AdomFloat(FieldP(Op, 7)); Arc.EndAngle := AdomFloat(FieldP(Op, 8));+            Fp.AddPCBObject(Arc);+            Res := 'ok:arc on layer ' + FieldP(Op, 2);+        End+        Else If Kind = 'MD' Then+        Begin+            Fp.Description := FieldP(Op, 2);+            Res := 'ok:description';+        End+        Else Res := 'err:unknown op ' + Kind;+        If (gInputError <> '') And (Copy(Res, 1, 3) <> 'err') Then Res := 'err:bad number ' + gInputError;+        If Copy(Res, 1, 3) = 'err' Then Failed := Failed + 1 Else Okay := Okay + 1;+        If Results <> '' Then Results := Results + ',';+        Results := Results + '"' + JsonEsc(Res) + '"';+    End;+    PLib.RemoveComponent(Old);+    PLib.RegisterComponent(Fp);+    PCBServer.PostProcess;+    gInputError := '';+    Ops.Free;+    PLib.CurrentComponent := Fp;+    PLib.Board.ViewManager_FullUpdate;+    PLib.RefreshView;+    Result := '{' + JRaw('ok','true') + ',' + JStr('footprint', Fp.Name) + ',' + JRaw('applied', IntToStr(Okay)) + ',' + JRaw('failed', IntToStr(Failed)) + ',' ++        JRaw('results', '[' + Results + ']') + '}';+End;+++{ lib_fp: one footprint of the CURRENT PcbLib in full: name=<footprint>. Pads (mil, hole, layer, shape, rotation,+  plated), other primitives counted per layer, the bounding box, description, height and 3D bodies. }+Function CmdLibFootprint(Lines : TStringList) : String;+Var+    LibErr : String;+    PLib  : IPCB_Library;+    Fp    : IPCB_LibComponent;+    GIt   : IPCB_GroupIterator;+    Prim  : IPCB_Primitive;+    Pad   : IPCB_Pad;+    S, B, L, Nm, Mf : String;+    I, OX, OY : Integer;+    R : TCoordRect;+Begin+    { the library the bridge names, as the open document: a library loaded by path is a separate copy (pads added or+      deleted on it never reached the document, 2026-10-28), and open shows a document without making it current }+    LibErr := CurrentLibAt(Param(Lines, 'path'));+    If LibErr <> '' Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','library_not_current') + ',' + JStr('error', LibErr) + '}'; Exit; End;+    PLib := PCBServer.GetCurrentPCBLibrary;+    If PLib = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','not_a_pcblib') + ',' + JStr('error','the current document is not a PCB library') + '}'; Exit; End;+    Nm := Param(Lines, 'name'); Fp := Nil;+    For I := 0 To PLib.ComponentCount - 1 Do+        If UpperCase(PLib.GetComponent(I).Name) = UpperCase(Nm) Then Begin Fp := PLib.GetComponent(I); Break; End;+    If Fp = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','footprint_not_found') + ',' + JStr('error','no footprint ' + Nm + ' in this library') + '}'; Exit; End;+    S := ''; B := ''; L := '';+    { positions from the footprint's origin. Only the footprint on display sits on the library's origin; another+      keeps raw coordinates near 0 (one library read +1270 mm, another -1272.7 mm, 2026-10-28), so it is shown first }+    If PLib.CurrentComponent <> Fp Then Begin PLib.CurrentComponent := Fp; PLib.Board.ViewManager_FullUpdate; End;+    OX := PLib.Board.XOrigin; OY := PLib.Board.YOrigin;+    GIt := Fp.GroupIterator_Create;+    GIt.AddFilter_LayerSet(AllLayers);+    Prim := GIt.FirstPCBObject;+    While Prim <> Nil Do+    Begin+        If Prim.ObjectId = ePadObject Then+        Begin+            Pad := Prim;+            If S <> '' Then S := S + ',';+            S := S + '[' + '"' + JsonEsc(Pad.Name) + '",' + IntToStr(Pad.x - OX) + ',' + IntToStr(Pad.y - OY) + ',' + IntToStr(Pad.TopXSize) + ',' + IntToStr(Pad.TopYSize) + ',' ++                IntToStr(Pad.HoleSize) + ',' + IntToStr(Pad.Layer) + ',' + IntToStr(Pad.TopShape) + ',' + IntToStr(Round(Pad.Rotation * 1000)) + ',' + JBool(Pad.Plated) + ']';+        End+        Else If Prim.ObjectId = eComponentBodyObject Then+        Begin+            Mf := '';+            Try If Prim.Model <> Nil Then Mf := Prim.Model.FileName; Except End;+            If B <> '' Then B := B + ',';+            B := B + '{' + JStr('model', Mf) + ',' + JRaw('overallHeight', IntToStr(Prim.OverallHeight)) + ',' + JRaw('standoff', IntToStr(Prim.StandoffHeight)) + '}';+        End+        Else+        Begin+            R := Prim.BoundingRectangle;+            If L <> '' Then L := L + ',';+            L := L + '[' + IntToStr(Prim.ObjectId) + ',' + IntToStr(Prim.Layer) + ',' + IntToStr(R.Left - OX) + ',' + IntToStr(R.Bottom - OY) + ',' + IntToStr(R.Right - OX) + ',' + IntToStr(R.Top - OY) + ']';+        End;+        Prim := GIt.NextPCBObject;+    End;+    Fp.GroupIterator_Destroy(GIt);+    R := Fp.BoundingRectangle;+    Result := '{' + JRaw('ok','true') + ',' + JStr('name', Fp.Name) + ',' + JStr('description', Fp.Description) + ',' + JRaw('height', IntToStr(Fp.Height)) + ',' ++        JRaw('k', IntToStr(MilsToCoord(1000))) + ',' + JRaw('origin', '[' + IntToStr(OX) + ',' + IntToStr(OY) + ']') + ',' + JStr('libraryFile', PLib.Board.FileName) + ',' + JRaw('box', '[' + IntToStr(R.Left - OX) + ',' + IntToStr(R.Bottom - OY) + ',' + IntToStr(R.Right - OX) + ',' + IntToStr(R.Top - OY) + ']') + ',' ++        JRaw('pads', '[' + S + ']') + ',' + JRaw('bodies', '[' + B + ']') + ',' + JRaw('other', '[' + L + ']') + '}';+End;+++{ new_pcb: an empty board saved at path= (Altium's default new board). Refuses an existing file. }+Function CmdNewPcb(Lines : TStringList) : String;+Var+    Path : String;+    Doc  : IServerDocument;+Begin+    Path := Param(Lines, 'path');+    If FileExists(Path) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','exists') + ',' + JStr('error', Path + ' exists') + '}'; Exit; End;+    Doc := CreateNewDocumentFromDocumentKind('PCB');+    If Doc = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','Altium made no new PCB document') + '}'; Exit; End;+        Client.ShowDocument(Doc);+    { as build_board saves its new board: name it, then save (DoSafeChangeFileNameAndSave left no file for a PCB) }+    FreePath(Path);+    Doc.SetFileName(Path);+    Doc.DoFileSave('');+    If Not FileExists(Path) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','not_saved') + ',' + JStr('error','the new board did not save to ' + Path) + '}'; Exit; End;+    Result := '{' + JRaw('ok','true') + ',' + JStr('path', Path) + ',' + JRaw('saved', 'true') + '}';+End;+++{ save_doc: save the document at path= itself. The save process saves the FOCUSED document, and open shows a document+  without focusing it, so a save by path through it saved whichever document had focus (a source library, measured+  2026-10-27). }+{ The open SimData (results) documents, every server's. }+Function SimDataDocs : TInterfaceList;+Var+    SM : IServerModule;+    D  : IServerDocument;+    I, J : Integer;+Begin+    Result := TInterfaceList.Create;+    For I := 0 To Client.Count - 1 Do+    Begin+        SM := Client.ServerModule[I];+        If SM = Nil Then Continue;+        For J := 0 To SM.DocumentCount - 1 Do+        Begin+            D := SM.Documents[J];+            If (D <> Nil) And (UpperCase(D.Kind) = 'SIMDATA') Then Result.Add(D);+        End;+    End;+    { the one in front, when the server walk misses it }+    D := Nil;+    Try D := Client.CurrentView.OwnerDocument; Except D := Nil; End;+    If (D <> Nil) And (UpperCase(D.Kind) = 'SIMDATA') And (Result.IndexOf(D) < 0) Then Result.Add(D);+End;++{ sim_run: start Altium's mixed-signal simulator on one schematic sheet (path=<SchDoc>). Earlier results documents are+  closed first (each run makes its own) and the sheet is made the CURRENT document: the run netlists whatever is in+  front, and with the last run's results in front it netlisted nothing (2026-10-29). Then WorkspaceManager:+  GenerateReport ObjectKind=Simulator|Index=1|Action=Run (the "Mixed Sim" output: <project>.nsx, then the run). The+  results document comes up after this returns: sim_collect saves and closes it. }+Function CmdSimRun(Lines : TStringList) : String;+Var+    Doc : IServerDocument;+    L   : TInterfaceList;+    Path : String;+    I : Integer;+Begin+    Path := Param(Lines, 'path');+    L := SimDataDocs;+    For I := 0 To L.Count - 1 Do Try Client.CloseDocument(L[I]); Except End;+    Doc := Nil;+    Try Doc := Client.OpenDocument('SCH', Path); Except Doc := Nil; End;+    If Doc = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','not_open') + ',' + JStr('error', Path + ' could not be opened') + '}'; Exit; End;+    Client.ShowDocument(Doc);+    ResetParameters;+    AddStringParameter('ObjectKind', 'Simulator');+    AddStringParameter('Index', '1');+    AddStringParameter('Action', 'Run');+    RunProcess('WorkspaceManager:GenerateReport');+    Result := '{' + JRaw('ok','true') + ',' + JStr('sheet', Path) + ',' + JStr('current', Client.CurrentView.OwnerDocument.FileName) + '}';+End;++{ sim_collect: the run's results document, saved (so its .sdf is on disk) and closed; sdf is empty while none is up. }+Function CmdSimCollect(Lines : TStringList) : String;+Var+    L : TInterfaceList;+    D : IServerDocument;+    Sdf : String;+Begin+    L := SimDataDocs;+    Sdf := '';+    If L.Count > 0 Then+    Begin+        D := L[L.Count - 1];+        D.DoFileSave('');+        Sdf := D.FileName;+        Try Client.CloseDocument(D); Except End;+    End;+    Result := '{' + JRaw('ok','true') + ',' + JStr('sdf', Sdf) + ',' + JRaw('open', IntToStr(L.Count)) + '}';+End;++Function CmdSaveDoc(Lines : TStringList) : String;+Var+    Doc : IServerDocument;+Begin+    Doc := Nil;+    Try Doc := Client.GetDocumentByPath(Param(Lines, 'path')); Except Doc := Nil; End;+    If Doc = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','not_open') + ',' + JStr('error', Param(Lines, 'path') + ' is not open in Altium') + '}'; Exit; End;+    Doc.DoFileSave('');+    Result := '{' + JRaw('ok','true') + ',' + JStr('path', Doc.FileName) + '}';+End;+++{ ensure_lib: open, or create and save, a library: kind=PCBLIB|SCHLIB path=<file>. It becomes the current document. }+Function CmdEnsureLib(Lines : TStringList) : String;+Var+    St : String;+Begin+    St := EnsureLibrary(Param(Lines, 'kind'), Param(Lines, 'path'));+    If Copy(St, 1, 5) = 'error' Then Result := '{' + JRaw('ok','false') + ',' + JStr('error', St) + '}'+    Else Result := '{' + JRaw('ok','true') + ',' + JStr('state', St) + ',' + JStr('path', Param(Lines, 'path')) + '}';+End;++{ how many pins a symbol holds (the empty default symbol holds none) }+Function SchChildCount(C : ISch_Component) : Integer;+Var+    It : ISch_Iterator;+    P  : ISch_GraphicalObject;+Begin+    Result := 0;+    It := C.SchIterator_Create;+    P := It.FirstSchObject;+    While P <> Nil Do+    Begin+        If P.ObjectID = ePin Then Result := Result + 1;+        P := It.NextSchObject;+    End;+    C.SchIterator_Destroy(It);+End;++{ xcopy_symbol: copy one symbol from fromLib (a .SchLib, opened if need be) into the CURRENT SchLib, under its own+  name: its graphics, pins, parameters (but the Comment, which the new symbol carries already) and model links. }+Function CmdXCopySymbol(Lines : TStringList) : String;+Var+    DstLib, SrcLib : ISch_Lib;+    Src, Dst : ISch_Component;+    It  : ISch_Iterator;+    Prim, Cpy : ISch_GraphicalObject;+    SD  : IServerDocument;+    Dflt : ISch_Component;+    NCopied : Integer;+    Dropped : String;+Begin+    DstLib := SchServer.GetCurrentSchDocument;+    If (DstLib = Nil) Or (DstLib.ObjectID <> eSchLib) Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','open the destination SchLib first') + '}'; Exit; End;+    SrcLib := SchServer.GetSchDocumentByPath(Param(Lines, 'fromLib'));+    If SrcLib = Nil Then+    Begin+        Try SD := Client.OpenDocument('SCHLIB', Param(Lines, 'fromLib')); Except SD := Nil; End;+        SrcLib := SchServer.GetSchDocumentByPath(Param(Lines, 'fromLib'));+    End;+    If SrcLib = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','source library not loadable: ' + Param(Lines, 'fromLib')) + '}'; Exit; End;+    Src := FindSchComp(SrcLib, Param(Lines, 'from'));+    If Src = Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('errorCode','symbol_not_found') + ',' + JStr('error','symbol not found in source: ' + Param(Lines, 'from')) + '}'; Exit; End;+    If FindSchComp(DstLib, Src.LibReference) <> Nil Then Begin Result := '{' + JRaw('ok','false') + ',' + JStr('error','target already has ' + Src.LibReference) + '}'; Exit; End;+    SchServer.ProcessControl.PreProcess(DstLib, '');+    Dst := SchServer.SchObjectFactory(eSchComponent, eCreate_Default);+    Dst.LibReference := Src.LibReference;+    Dst.ComponentDescription := Src.ComponentDescription;+    Dst.CurrentPartID := 1;+    Dst.DisplayMode := 0;+    Dst.Designator.Text := Src.Designator.Text;+    Dst.Comment.Text := Src.Comment.Text;+    NCopied := 0;+    It := Src.SchIterator_Create;+    Prim := It.FirstSchObject;+    While Prim <> Nil Do+    Begin+        If (Prim.ObjectID <> eParameter) Or (UpperCase(Prim.Name) <> 'COMMENT') Then+        Begin+            Cpy := Prim.Replicate;+            Dst.AddSchObject(Cpy);+            NCopied := NCopied + 1;+        End;+        Prim := It.NextSchObject;+    End;+    Src.SchIterator_Destroy(It);+    Dst.DisplayModeCount := Src.DisplayModeCount;+    DstLib.AddSchComponent(Dst);+    SchServer.RobotManager.SendMessage(DstLib.I_ObjectAddress, c_BroadCast, SCHM_PrimitiveRegistration, Dst.I_ObjectAddress);+    { a SchLib Altium has just made holds an empty Component_1: it goes once a real symbol is in }+    Dropped := '';+    Dflt := FindSchComp(DstLib, 'Component_1');+    If (Dflt <> Nil) And (UpperCase(Src.LibReference) <> 'COMPONENT_1') And (SchChildCount(Dflt) = 0) Then+    Begin+        DstLib.RemoveSchComponent(Dflt);+        Dropped := 'Component_1';+    End;+    SchServer.ProcessControl.PostProcess(DstLib, '');+    DstLib.GraphicallyInvalidate;+    Result := '{' + JRaw('ok','true') + ',' + JStr('symbol', Dst.LibReference) + ',' + JRaw('primitives', IntToStr(NCopied)) + ',' + JStr('removedDefault', Dropped) + '}';+End;+++Function Dispatch(Cmd : String; Lines : TStringList; Payload : String) : String;+Begin+    If Cmd = 'ping' Then Result := CmdPing(Lines)+    Else If Cmd = 'state' Then Result := CmdState(Lines)+    Else If Cmd = 'run_process' Then Result := CmdRunProcess(Lines)+    Else If Cmd = 'open' Then Result := CmdOpen(Lines)+    Else If Cmd = 'save_doc' Then Result := CmdSaveDoc(Lines)+    Else If Cmd = 'sim_run' Then Result := CmdSimRun(Lines)+    Else If Cmd = 'sim_collect' Then Result := CmdSimCollect(Lines)+    Else If Cmd = 'edit_fp' Then Result := CmdEditFp(Lines, Payload)+    Else If Cmd = 'close_document' Then Result := CmdCloseDocument(Lines)+    Else If Cmd = 'messages' Then Result := CmdMessages(Lines)+    Else If Cmd = 'import_step' Then Result := CmdImportStep(Lines)+    Else If Cmd = 'eval' Then Result := CmdEval(Lines, Payload)+    Else If Cmd = 'io_bench' Then Result := CmdIoBench(Lines)+    Else If Cmd = 'read_board' Then Result := CmdReadBoard(Lines)+    Else If Cmd = 'layer_occupancy' Then Result := CmdLayerOccupancy(Lines)+    Else If Cmd = 'read_schematic' Then Result := CmdReadSchematic(Lines)+    Else If Cmd = 'read_outline' Then Result := CmdReadOutline(Lines)+    Else If Cmd = 'lib_list' Then Result := CmdLibList(Lines)+    Else If Cmd = 'lib_select' Then Result := CmdLibSelect(Lines)+    Else If Cmd = 'compile' Then Result := CmdCompile(Lines)+    Else If Cmd = 'violations' Then Result := CmdViolations(Lines)+    Else If Cmd = 'outjob' Then Result := CmdOutJob(Lines)+    Else If Cmd = 'create_footprint' Then Result := CmdCreateFootprint(Lines)+    Else If Cmd = 'create_symbol' Then Result := CmdCreateSymbol(Lines)+    Else If Cmd = 'install_library' Then Result := CmdInstallLibrary(Lines)+    Else If Cmd = 'list_installed_libraries' Then Result := CmdListInstalledLibs(Lines)+    Else If Cmd = 'uninstall_library' Then Result := CmdUninstallLibrary(Lines)+    Else If Cmd = 'compile_intlib' Then Result := CmdCompileIntLib(Lines)+    Else If Cmd = 'extract_intlib' Then Result := CmdExtractIntLib(Lines)+    Else If Cmd = 'set_params' Then Result := CmdSetParams(Lines)+    Else If Cmd = 'add_body' Then Result := CmdAddBody(Lines)+    Else If Cmd = 'place_on_sch' Then Result := CmdPlaceOnSch(Lines)+    Else If Cmd = 'add_symbol_art' Then Result := CmdAddSymbolArt(Lines, Payload)+    Else If Cmd = 'selftest_halt' Then Result := CmdSelfTestHalt(Lines)+    Else If Cmd = 'symbol_prims' Then Result := CmdSymbolPrims(Lines)+    Else If Cmd = 'set_models' Then Result := CmdSetModels(Lines)+    Else If Cmd = 'place_on_pcb' Then Result := CmdPlaceOnPcb(Lines)+    Else If Cmd = 'pcb_report' Then Result := CmdPcbReport(Lines)+    Else If Cmd = 'list_designators' Then Result := CmdListDesignators(Lines)+    Else If Cmd = 'component_data' Then Result := CmdComponentData(Lines)+    Else If Cmd = 'layer_stack' Then Result := CmdLayerStack(Lines)+    Else If Cmd = 'bom_placement' Then Result := CmdBomPlacement(Lines)+    Else If Cmd = 'component_pins' Then Result := CmdComponentPins(Lines)+    Else If Cmd = 'list_nets' Then Result := CmdListNets(Lines)+    Else If Cmd = 'layers' Then Result := CmdLayers(Lines)+    Else If Cmd = 'net_connections' Then Result := CmdNetConnections(Lines)+    Else If Cmd = 'schematic_data' Then Result := CmdSchematicData(Lines)+    Else If Cmd = 'sheet_params' Then Result := CmdSheetParams(Lines)+    Else If Cmd = 'sch_connect' Then Result := CmdSchConnect(Lines, Payload)+    Else If Cmd = 'sch_dump' Then Result := CmdSchDump(Lines)+    Else If Cmd = 'docs' Then Result := CmdDocs(Lines)+    Else If Cmd = 'ensure_lib' Then Result := CmdEnsureLib(Lines)+    Else If Cmd = 'xcopy_symbol' Then Result := CmdXCopySymbol(Lines)+    Else If Cmd = 'new_pcb' Then Result := CmdNewPcb(Lines)+    Else If Cmd = 'lib_fp' Then Result := CmdLibFootprint(Lines)+    Else If Cmd = 'board_graphics' Then Result := CmdBoardGraphics(Lines)+    Else If Cmd = 'sch_ops' Then Result := CmdSchOps(Lines, Payload)+    Else If Cmd = 'sheet_symbol' Then Result := CmdSheetSymbol(Lines, Payload)+    Else If Cmd = 'sheet_ports' Then Result := CmdSheetPorts(Lines, Payload)+    Else If Cmd = 'teardrop_count' Then Result := CmdTeardropCount(Lines)+    Else If Cmd = 'comp_classes' Then Result := CmdCompClasses(Lines)+    Else If Cmd = 'panelize' Then Result := CmdPanelize(Lines)+    Else If Cmd = 'bodies' Then Result := CmdBodies(Lines)+    Else If Cmd = 'sch_edit' Then Result := CmdSchEdit(Lines, Payload)+    Else If Cmd = 'sch_place_part' Then Result := CmdSchPlacePart(Lines)+    Else If Cmd = 'doc_params' Then Result := CmdDocParams(Lines)+    Else If Cmd = 'sch_rename_at' Then Result := CmdSchRenameAt(Lines, Payload)+    Else If Cmd = 'sch_wires' Then Result := CmdSchWires(Lines, Payload)+    Else If Cmd = 'net_classes' Then Result := CmdNetClasses(Lines)+    Else If Cmd = 'selection' Then Result := CmdSelection(Lines)+    Else If Cmd = 'comp_rects' Then Result := CmdCompRects(Lines)+    Else If Cmd = 'track_width' Then Result := CmdTrackWidth(Lines)+    Else If Cmd = 'outline_tracks' Then Result := CmdOutlineTracks(Lines)+    Else If Cmd = 'layer_stack_edit' Then Result := CmdLayerStackEdit(Lines)+    Else If Cmd = 'pcb_postprocess' Then Result := CmdPcbPostProcess(Lines)+    Else If Cmd = 'project_variants' Then Result := CmdProjectVariants(Lines)+    Else If Cmd = 'diff_pairs' Then Result := CmdDiffPairs(Lines)+    Else If Cmd = 'sch_zoom' Then Result := CmdSchZoom(Lines)+    Else If Cmd = 'copper_layers' Then Result := CmdCopperLayers(Lines)+    Else If Cmd = 'read_rules' Then Result := CmdReadRules(Lines)+    Else If Cmd = 'set_layer_visibility' Then Result := CmdSetLayerVisibility(Lines)+    Else If Cmd = 'footprint_primitives' Then Result := CmdFootprintPrimitives(Lines)+    Else If Cmd = 'routing_state' Then Result := CmdRoutingState(Lines)+    Else If Cmd = 'edit_board' Then Result := CmdEditBoard(Lines, Payload)+    Else If Cmd = 'apply_pours' Then Result := CmdApplyPours(Lines, Payload)+    Else If Cmd = 'edit_pour' Then Result := CmdEditPour(Lines, Payload)+    Else If Cmd = 'pour_state' Then Result := CmdPourState(Lines)+    Else If Cmd = 'rebuild_pours' Then Result := CmdRebuildPours(Lines)+    Else If Cmd = 'remove_pours' Then Result := CmdRemovePours(Lines, Payload)+    Else If Cmd = 'routing_drc' Then Result := CmdRoutingDrc(Lines)+    Else If Cmd = 'build_board' Then Result := CmdBuildBoard(Lines, Payload)+    Else If Cmd = 'set_outline' Then Result := CmdSetOutline(Lines, Payload)+    Else If Cmd = 'set_rule' Then Result := CmdSetRule(Lines)+    Else If Cmd = 'zoom_rect' Then Result := CmdZoomRect(Lines)+    Else If Cmd = 'add_rule' Then Result := CmdAddRule(Lines)+    Else If Cmd = 'build_schematic' Then Result := CmdBuildSchematic(Lines, Payload)+    Else If Cmd = 'project_nets' Then Result := CmdProjectNets(Lines)+    Else If Cmd = 'select_net' Then Result := CmdSelectNet(Lines)+    Else If Cmd = 'place_parts' Then Result := CmdPlaceParts(Lines, Payload)+    Else If Cmd = 'model_check' Then Result := CmdModelCheck(Lines)+    Else If Cmd = 'close_old_agents' Then Result := CmdCloseOldAgents(Lines)+    Else If Cmd = 'silk_state' Then Result := CmdSilkState(Lines)+    Else If Cmd = 'apply_silk' Then Result := CmdApplySilk(Lines, Payload)+    Else If Cmd = 'build_footprint' Then Result := CmdBuildFootprint(Lines)+    Else If Cmd = 'build_symbol_modes' Then Result := CmdBuildSymbolModes(Lines)+    Else If Cmd = 'symbol_mode' Then Result := CmdSymbolMode(Lines)+    Else If Cmd = 'add_models' Then Result := CmdAddModels(Lines)+    Else If Cmd = 'remove_symbol' Then Result := CmdRemoveSymbol(Lines)+    Else If Cmd = 'clone_symbol' Then Result := CmdCloneSymbol(Lines)+    Else If Cmd = 'rename_symbol' Then Result := CmdRenameSymbol(Lines)+    Else If Cmd = 'read_symbol' Then Result := CmdReadSymbol(Lines)+    Else If Cmd = 'copy_footprint' Then Result := CmdCopyFootprint(Lines)+    Else If Cmd = 'rename_footprint' Then Result := CmdRenameFootprint(Lines)+    Else If Cmd = 'remove_footprint' Then Result := CmdRemoveFootprint(Lines)+    Else If Cmd = 'cleanup_libs' Then Result := CmdCleanupLibs(Lines)+    Else If Cmd = 'set_visibility' Then Result := CmdSetVisibility(Lines)+    Else If Cmd = 'xcopy_footprint' Then Result := CmdXCopyFootprint(Lines)+    Else If Cmd = 'normalize_bodies' Then Result := CmdNormalizeBodies(Lines)+    Else If Cmd = 'plate_vias' Then Result := CmdPlateVias(Lines)+    Else If Cmd = 'test_board' Then Result := CmdTestBoard(Lines)+    Else If Cmd = 'board_shape' Then Result := CmdBoardShape(Lines)+    Else If Cmd = 'swap_footprint' Then Result := CmdSwapFootprint(Lines)+    Else If Cmd = 'replace_fp' Then Result := CmdReplaceFp(Lines)+    Else If Cmd = 'tx_clear' Then Begin TxClear; Result := '{' + JRaw('ok','true') + ',' + JStr('note','PCBServer transaction cleared') + '}'; End+    Else If Cmd = 'stop' Then+    Begin+        gRunning := False;+        Result := '{' + JRaw('ok', 'true') + ',' + JStr('note', 'agent loop stopping') + '}';+    End+    Else Result := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'unknown command: ' + Cmd) + ',' + JStr('known', 'ping,state,run_process,open,eval,lib_list,lib_select,compile,violations,outjob,create_footprint,create_symbol,install_library,compile_intlib,extract_intlib,set_params,add_body,stop,list_designators,component_data,component_pins,list_nets,layers,net_connections,schematic_data,copper_layers,read_rules,set_layer_visibility,footprint_primitives,io_bench,read_board,layer_occupancy,read_schematic,read_outline') + '}';+End;++{ ------------------------------------------------------------------ loop --- }++{ ONE inbox file, request.txt, written atomically by the bridge (tmp + rename) and+  serialized on the bridge side. A wildcard FindFirst('request_*.txt') never matched+  in this engine (measured 2026-08-29: heartbeat fine, requests stuck on disk), so+  the inbox is a fixed name and the id rides inside. }+Function ServeOne : Boolean;+Var+    Id, ReqPath, PayPath, Cmd, Body, Payload : String;+    Lines   : TStringList;+    T0, TDetect, TRead, TDone : Integer;+Begin+    Result := False;+    ReqPath := WORKSPACE + 'request.txt';+    If Not FileExists(ReqPath) Then Exit;+    PayPath := WORKSPACE + 'request.payload';+    Lines := TStringList.Create;+    Try+        TDetect := GetTickCount;+        Lines.Text := ReadAll(ReqPath);+        DeleteFile(ReqPath);+        TRead := GetTickCount;+        Payload := '';+        If FileExists(PayPath) Then+        Begin+            Payload := ReadAll(PayPath);+            DeleteFile(PayPath);+        End;+        Id := Param(Lines, 'id');+        Cmd := Param(Lines, 'command');+        gRequests := gRequests + 1;+        { a status read does not keep the loop resident: an AI polling altium_state held Altium (and the user's+          clicks) for as long as it polled (adom/altium-bridge#27) }+        If (Cmd <> 'state') And (Cmd <> 'ping') Then gLastReqTick := GetTickCount;+        T0 := GetTickCount;+        gInputError := '';+        Try+            Body := Dispatch(Cmd, Lines, Payload);+            If gInputError <> '' Then+                Body := '{' + JRaw('ok', 'false') + ',' + JStr('errorCode', 'bad_number') + ',' ++                    JStr('error', 'a value that must be a number was not: ' + gInputError) + ',' + JStr('command', Cmd) + '}';+        Except+            Body := '{' + JRaw('ok', 'false') + ',' + JStr('error', 'exception in agent while handling ' + Cmd) + '}';+        End;+        TDone := GetTickCount;+        { wrap: id + timing + the handler body as "data"; agentPhases = read/dispatch ms + the loop tick that saw the request }+        WriteDirect(WORKSPACE + 'response_' + Id + '.json',+            '{' + JStr('id', Id) + ',' + JStr('command', Cmd) + ',' + JRaw('durationMs', IntToStr(TDone - T0)) + ',' ++            JRaw('agentPhases', '{' + JRaw('readMs', IntToStr(TRead - TDetect)) + ',' + JRaw('dispatchMs', IntToStr(TDone - T0)) + ',' + JRaw('detectTick', IntToStr(TDetect)) + ',' + JRaw('sleepMs', IntToStr(gLastSleepMs)) + ',' + JRaw('hbSaveMs', IntToStr(gLastSaveMs)) + ',' + JRaw('hbSaveTries', IntToStr(gLastSaveTries)) + ',' + JRaw('hbSaveExcept', IntToStr(gLastSaveExcept)) + ',' + JRaw('hbDeleteMs', IntToStr(gLastDeleteMs)) + ',' + JRaw('hbRenameMs', IntToStr(gLastRenameMs)) + ',' + JRaw('directMs', IntToStr(gDirectMs)) + ',' + JRaw('directTries', IntToStr(gDirectTries)) + ',' + JRaw('directExcept', IntToStr(gDirectExcept)) + '}') + ',' ++            JRaw('data', Body) + '}');+    Finally+        Lines.Free;+    End;+    Result := True;+End;++{ Register the PCB and SCH scripting APIs before the first PCBServer/SchServer+  reference. On a fresh session where no PCB document was ever opened, the PCB+  API unit is not registered and the first PCBServer call (TxClear, line ~140)+  halts the engine with an "Undeclared identifier: PostProcess" modal on the+  user's screen (altium-bridge#3, ADDevelop fresh session). Client.StartServer+  loads the editor server, which registers the API — background, no UI. }+Procedure ServersUp;+Begin+    Try+        Client.StartServer('PCB');+    Except+    End;+    Try+        Client.StartServer('SCH');+    Except+    End;+End;++Procedure StartAgent;+Var+    IdleTicks : Integer;+    Had       : Boolean;+    SleepMs   : Integer;+Begin+    ServersUp;+    TxClear;+    gRunning := True;+    gRequests := 0;+    gLastReqTick := GetTickCount;+    gLastHbTick := 0;+    IdleTicks := 0;+    gTicks := 0;+    { a stale stop file must not kill a fresh loop }+    If FileExists(WORKSPACE + 'stop') Then DeleteFile(WORKSPACE + 'stop');+    WriteAtomic(WORKSPACE + 'agent.json', '{' + JStr('version', AGENT_VERSION) + ',' + JRaw('startedTick', IntToStr(GetTickCount)) + ',' + JStr('workspace', WORKSPACE) + '}');+    Heartbeat(True);+    While gRunning Do+    Begin+        Try+            { two shutdown signals, both cheap reads: IsQuitting demonstrably never fired+              in time during the 2026-09-02 freezes, so Terminated rides beside it }+            If Client.IsQuitting Then gRunning := False;+            If Application.Terminated Then gRunning := False;+        Except+        End;+        If FileExists(WORKSPACE + 'stop') Then+        Begin+            DeleteFile(WORKSPACE + 'stop');+            gRunning := False;+        End;+        If Not gRunning Then Break;+        Had := False;+        Try+            Had := ServeOne;+        Except+        End;+        If Had Then+        Begin+            IdleTicks := 0;+            SleepMs := POLL_ACTIVE_MS;+        End+        Else+        Begin+            IdleTicks := IdleTicks + 1;+            If IdleTicks > IDLE_AFTER_TICKS Then SleepMs := POLL_IDLE_MS Else SleepMs := POLL_ACTIVE_MS;+        End;+        If (GetTickCount - gLastReqTick) > AUTO_STOP_MS Then gRunning := False;+        Heartbeat(False);+        Application.ProcessMessages;+        gLastSleepMs := SleepMs;+        gTicks := gTicks + 1;+        Sleep(SleepMs);+    End;+    Try+        WriteAtomic(WORKSPACE + 'agent.json', '{' + JStr('version', AGENT_VERSION) + ',' + JRaw('stoppedTick', IntToStr(GetTickCount)) + ',' + JRaw('requests', IntToStr(gRequests)) + '}');+        If FileExists(WORKSPACE + 'hb.txt') Then DeleteFile(WORKSPACE + 'hb.txt');+    Except+    End;+End;++{ Convenience entry points for File > Run Script users. }+Procedure StopAgent;+Begin+    WriteAtomic(WORKSPACE + 'stop', '1');+End;+

Comments

Colby Knox ·

Thank you, Carlos. You found the cause and the fix, and your fix is in altium-bridge 0.2.92, which is public now. refresh_bridges {"name":"altium"} installs it.

I applied the change by hand rather than merging this PR. Its diff rewrites the whole template from an older copy, so merging it would also have undone changes made since. The code is the same idea as yours: AdomFloat reads the ASCII digits itself instead of calling StrToFloat. The only difference is that it collects the digits as one number and divides once at the end, so a value like 0.91 comes out as exact as a Double allows. The changelog and the code comment credit you and link this PR.

We reproduced it on our test machine by setting the Windows decimal separator to a comma. 0.2.91 raised '-1.5000' is not a valid floating point value in footprint_from_land_pattern; 0.2.92 passes, and so does the full verb test suite under the same comma setting. You can put your regional setting back to normal: nothing has to change on your machine any more.

Closing this PR, since the fix has shipped.

Log in to comment.