# 電路圖編輯器 — 開發人員程式庫

基於 diagram-editor 的電路圖：帶元件編號和參數值的元件、網路、匯流排、電源、ERC、材料清單、GOST 和 ISO 圖框、SPICE 和 KiCad 網路表、KiCad 匯入匯出、模擬。

## 安裝

```sh
npm install @openlibsdev/schematic-editor
```

```js
import SchematicEditor from "https://cdn.jsdelivr.net/npm/@openlibsdev/schematic-editor@1.0.0/+esm";
```

## 用法

```js
import SchematicEditor from "@openlibsdev/schematic-editor";

const p = SchematicEditor.createProject(), d = p.pages[0].diagram;
const r = SchematicEditor.addComponent(p, d, "elResistor", 40, 40);   // r.ref === "R1"
SchematicEditor.renumber(p, "gost");
console.log(SchematicEditor.nets(p).nets.length);   // nets with their pins
console.log(SchematicEditor.erc(p));                 // [{ level, code, page, ids, ... }]
const spice = SchematicEditor.spiceNetlist(p);
```

## API

### `SchematicEditor.createProject()`

建立電路圖專案。

### `SchematicEditor.addComponent(x, d, type, px, py, props)`

新增元件，使用下一個元件編號和預設參數值。

### `SchematicEditor.addEdge(d, from, to, props)`

用導線連線引腳。

### `SchematicEditor.renumber(x, style)`

按位置重新編號元件編號（GOST 或 IEC 風格）。

### `SchematicEditor.nets(x)`

電路圖的電氣網路：{nets: [{name, pins, wires, labels, power}], byEdge, byPin}。

### `SchematicEditor.erc(x)`

電氣規則檢查：問題列表。

### `SchematicEditor.bomGrouped(x)`

按參數值分組的材料清單。

### `SchematicEditor.pe3Rows(x, opts)`

GOST 元件明細表（PE3）的行。

### `SchematicEditor.spiceNetlist(x, opts)`

SPICE 網路表。

### `SchematicEditor.kicadNetlist(x, opts)`

KiCad 網路表。

### `SchematicEditor.kicadSchematic(x, opts)`

KiCad .kicad_sch 格式的電路圖。

### `SchematicEditor.kicadImport(input, opts)`

開啟 KiCad 電路圖。

### `SchematicEditor.simNetlist(x, analysis)`

內建模擬器使用的網路表。

### `SchematicEditor.logicModel(x)`

用於邏輯模擬的模型。

### `SchematicEditor.toSVG(d, opts)`

SVG 形式的電路圖。

### `SchematicEditor.toBlob(d, format, opts)`

檔案形式的電路圖。

### `SchematicEditor.serialize(x)`

專案 → JSON。

### `SchematicEditor.parse(data)`

JSON → 專案。

### `SchematicEditor.PARTS`

元件庫。

### `SchematicEditor.ALIGNS`

文本對齊：左、中、右。

### `SchematicEditor.APP`

庫的名稱、版本和釋出日期：{name, version, created}。

### `SchematicEditor.ARROWS`

線條端點：箭頭、三角形、菱形、圓形以及 ER 的鴉腳端。

### `SchematicEditor.BAND`

容器標題欄的高度（像素）。

### `SchematicEditor.DASHES`

線條樣式：實線、虛線、點線（以虛線模式表示）。

### `SchematicEditor.DEFAULT_VALUES`

新元件按型別的初始值（10 kΩ、100 nF、…）。

### `SchematicEditor.DOTS`

線條連線點的模式：自動、開、關。

### `SchematicEditor.EDGE_DEFAULTS`

新線條的預設屬性：端點、型別、箭頭、顏色、粗細、樣式、文本。

### `SchematicEditor.FIELDS`

元件的標準欄位：Footprint, Datasheet, Manufacturer, MPN（進入元件清單和 KiCad 網表）。

### `SchematicEditor.FORMATS`

匯出格式及其 MIME 型別。

### `SchematicEditor.FRAMES`

圖框：無、GOST、ISO。

### `SchematicEditor.GATES`

用於邏輯模擬的邏輯閘和元件，帶其功能和引腳。

### `SchematicEditor.IMAGE_LIMIT`

圖中保留的最大圖片，按其 data URL 的位元組數計（3 MB）。

### `SchematicEditor.KICAD`

每種元件型別的 KiCad 庫符號和引腳順序，用於 KiCad 網表和原理圖。

### `SchematicEditor.LABELS`

網路標籤型別：本地（連線其所在頁的導線）和全域性（所有頁面）。

### `SchematicEditor.LINE_KINDS`

線條型別：直線、折線（直角）、曲線。

### `SchematicEditor.MIN_SIZE`

形狀的最小寬度和高度（像素）。

### `SchematicEditor.MM`

圖紙上每毫米的像素數（4）。

### `SchematicEditor.NODE_DEFAULTS`

新形狀的預設屬性：文本、填充、不透明度、邊框、字型、對齊。

### `SchematicEditor.PAGE_DEFAULTS`

頁面的預設設定：背景、透明度、網格、吸附、網格大小、邊距、連線點。

### `SchematicEditor.PIN_SIDES`

自訂符號引腳可放置的邊：左、右、上、下。

### `SchematicEditor.PIN_TYPES`

引腳的電氣型別：無源、輸入、輸出、雙向、電源輸入、電源輸出、未連線。

### `SchematicEditor.POWER`

電源符號及其各自給出的網路名：VCC, VDD, GND, GNDA, Earth。

### `SchematicEditor.PREFIX`

IEC 和 GOST 樣式中每種元件型別的位號字首（R, C, VD, …）。

### `SchematicEditor.SHAPES`

形狀型別。

### `SchematicEditor.SHAPE_SETS`

按編輯器提供方式分組的符號：電氣、邏輯、儀器、網路（標籤、電源、地）、元件、自訂符號。

### `SchematicEditor.SHEETS`

紙張尺寸（毫米）：A4、A3、A2、A1。

### `SchematicEditor.SIDES`

線條可連線的邊：自動、上、右、下、左。

### `SchematicEditor.SIM_MODELS`

模擬器標準元件的 SPICE 模型（二極體、電晶體、運算放大器、LM317）。

### `SchematicEditor.SOURCE_TYPES`

模擬中作為電源的元件型別：電池、直流、交流、電流源、函式發生器。

### `SchematicEditor.SPICE`

每種元件型別的 SPICE 元件字母、引腳順序和模型。

### `SchematicEditor.STYLES`

位號樣式：iec, gost。

### `SchematicEditor.TB_FIELDS`

標題欄的欄位：標題、文件名稱和編號、單位、簽名、日期、比例、…

### `SchematicEditor.TEMPLATES`

現成電路：帶開關的 LED、電晶體開關、半加器、放大器、電源、RC 濾波器、多諧振盪器、整流器、鎖存器、繼電器驅動、ATtiny 閃燈等。

### `SchematicEditor.addBend(d, e, x, y)`

線上條經過某點附近處新增折點；返回其索引，或 -1。

### `SchematicEditor.addNode(d, type, x, y, props)`

在某點新增形狀。

### `SchematicEditor.addPowerUnit(x, d, n)`

多單元元件的電源單元尚未放置時，將其放在節點旁；返回該單元。

### `SchematicEditor.addSymbol(x, def)`

向專案新增自訂符號定義；返回其型別。

### `SchematicEditor.align(d, ids, how)`

對齊專案。

### `SchematicEditor.anchor(n, side)`

形狀輪廓某條邊的中點（考慮旋轉）。

### `SchematicEditor.assignRef(x, n, style)`

給元件分配位號（沒有值時還會給預設值）。

### `SchematicEditor.bounds(d, ids)`

包圍給定元素（或全部元素）的框；空圖為 null。

### `SchematicEditor.busMembers(name)`

匯流排名所代表的訊號：“D[0..7]”→ D0…D7，“{SDA SCL}”→ SDA, SCL；不是匯流排名時為 null。

### `SchematicEditor.buses(x)`

專案的匯流排：[{page, edge, name, members, used}]。

### `SchematicEditor.clone(d)`

圖的深複製。

### `SchematicEditor.components(x)`

專案的元件，每個位號一行，已排序（R1, R2, R10）。

### `SchematicEditor.contents(d, c)`

位於容器內部的形狀。

### `SchematicEditor.createDiagram()`

建立空白圖表。

### `SchematicEditor.cssFont(it)`

元素的 CSS 字型字串（樣式、粗細、大小、字型族）。

### `SchematicEditor.decorationBox(d)`

用 setDecorations 設定的頁面裝飾（例如圖框）的框，或 null。

### `SchematicEditor.dipPins(n)`

DIP 封裝的引腳：1…n/2 沿左側向下，其餘沿右側向上。

### `SchematicEditor.distToSegment(p, a, b)`

點到線段的距離。

### `SchematicEditor.distribute(d, ids, axis)`

均勻分佈專案。

### `SchematicEditor.drawPrimitives(ctx, prims)`

在 canvas 2D 上下文中繪製圖元（來自 primitives()）。

### `SchematicEditor.duplicate(d, ids, dx, dy)`

複製元素並偏移 (dx, dy) 貼上；返回新的 id。

### `SchematicEditor.edgeAt(d, x, y, tol)`

經過某點附近的最上層線條，或 null。

### `SchematicEditor.edgeEnds(d, e)`

線條實際結束的位置：[起點, 終點]，各自帶有點、邊、引腳以及是否空閒。

### `SchematicEditor.electricShape(W, H, o)`

根據尺寸和繪圖（線條、引腳、標籤位置）建立電氣符號形狀，供 registerShape 使用。

### `SchematicEditor.exportBox(d, ids)`

要匯出的區域：給定元素及其外部標籤，或帶裝飾的整個圖。

### `SchematicEditor.extract(d, ids)`

將圖的一部分（形狀及其間的線條）複製為可插入別處的片段。

### `SchematicEditor.fieldColumns(x)`

元件清單的列：標準欄位，然後是正在使用的自訂欄位名。

### `SchematicEditor.find(d, id)`

按 id 獲取形狀或線條。

### `SchematicEditor.findEdge(d, id)`

按 id 獲取線條，或 null。

### `SchematicEditor.findNamedColor(text, lang)`

按指定語言的顏色名稱查詢命名顏色（“深藍色”→ #00008b）。

### `SchematicEditor.findNode(d, id)`

按 id 獲取形狀，或 null。

### `SchematicEditor.fitToSheet(x)`

當某頁的圖放不下時，把它移入圖框的工作區；返回移動的頁數。

### `SchematicEditor.footprintsFor(type)`

為某種元件型別推薦的 KiCad 封裝。

### `SchematicEditor.gateFn(fn, ins)`

邏輯函式對輸入值 0、1、X（未知）和 Z（未驅動）的輸出。

### `SchematicEditor.genWave(value)`

函式發生器的值（“sine 1 V 1 kHz”“pulse 5 V 10 kHz 20%”或 SPICE 波形）→ {spice, wave, amp, freq}。

### `SchematicEditor.group(d, ids)`

將專案編組。

### `SchematicEditor.insert(d, part, dx, dy)`

插入由 extract 生成的片段，偏移 (dx, dy)；組會得到新名稱。返回新的 id。

### `SchematicEditor.isClosed(n)`

開關或觸點是否閉合。

### `SchematicEditor.isComponent(n)`

節點是否為元件（有位號），而不是導線、標籤或圖形。

### `SchematicEditor.itemsInRect(d, box)`

完全位於矩形內的形狀和線條的 id。

### `SchematicEditor.junctions(d)`

繪製連線點的位置（T 形連線以及三根或更多導線的交叉處）。

### `SchematicEditor.labelName(n)`

標籤或電源符號的網路名。

### `SchematicEditor.labelPlace(n)`

符號外部標籤的位置：下方、上方或右側。

### `SchematicEditor.labelText(n)`

符號標籤的文本：位號、值和文本，每項一行。

### `SchematicEditor.layoutTree(d, opts)`

將形狀排列成樹形。

### `SchematicEditor.like(type)`

某元件型別在繪製和模擬時所採用的標準型別。

### `SchematicEditor.listNamedColors(lang)`

列出命名顏色及其在指定語言中的名稱。

### `SchematicEditor.loadFonts(d)`

載入圖中使用的所有字型族的字型檔案，使繪製和測量使用真實字型。

### `SchematicEditor.loadImages(d)`

在繪製前載入圖中的所有圖片。

### `SchematicEditor.logicSettle(model, st)`

使邏輯電路在某一時刻穩定：逐步執行直到不再變化；觸發器在時鐘上升沿翻轉。

### `SchematicEditor.logicState(model, prev)`

邏輯模擬的狀態：輸出、時鐘和時間（從上一狀態繼續）。

### `SchematicEditor.markup(d, opts)`

不含外層 <svg> 的 SVG 元素形式的圖（用於放入你自己的 SVG）。

### `SchematicEditor.move(d, ids, dx, dy)`

移動專案。

### `SchematicEditor.moveSegment(d, e, k, dx, dy)`

將線條按所繪製的一段移動 (dx, dy)。

### `SchematicEditor.namedColorTexts(lang)`

指定語言的顏色欄位標籤（取色器的文本），缺少該語言時為英文。

### `SchematicEditor.nearestPin(n, x, y, toward)`

形狀上離某點最近的引腳的索引，或 -1。

### `SchematicEditor.nextLabel(x)`

新網路標籤的名稱：NET1, NET2, …（專案中第一個未用的名稱）。

### `SchematicEditor.nextRef(x, prefix)`

整個專案中某字首的下一個空閒位號：R1, R2, …

### `SchematicEditor.nodeAt(d, x, y, pad)`

某點下方的最上層形狀（容器只在其標題欄和邊框上）。

### `SchematicEditor.normalize(d)`

任意物件 → 有效的圖：未知值替換為預設值，id 設為唯一，斷開的線條被刪除。

### `SchematicEditor.normalizeProject(p)`

任意物件 → 帶頁面的有效專案。

### `SchematicEditor.normalizeSymbol(def)`

檢查自訂符號定義並補全缺少的部分。

### `SchematicEditor.order(d, ids, how)`

更改形狀的繪製順序：置於頂層、置於底層、上移一層、下移一層。

### `SchematicEditor.orthoThrough(a, sa, vias, b, sb)`

從一點經過中間點到另一點的直角折線，沿給定的邊出入。

### `SchematicEditor.parseAny(data)`

來自檔案的專案：我們的格式（JSON）或 KiCad 的（.kicad_sch）。

### `SchematicEditor.pe3Sheets(x, opts)`

排在帶圖框的 A4 圖紙上的 GOST 元件目錄（PE3）。

### `SchematicEditor.pinType(type, i)`

某元件型別引腳的電氣型別。

### `SchematicEditor.pins(n)`

形狀顯示時的引腳（電氣和邏輯符號）：[{x, y, side}]。

### `SchematicEditor.placeAt(x, d, type, p)`

將符號的第一個引腳放在某點（導線上的標籤會用連線點分割導線）。

### `SchematicEditor.powerVolts(name)`

電源網路名所表示的電壓：“+5 V”→ 5，“3V3”→ 3.3；VCC、VDD 等為 null。

### `SchematicEditor.primitives(d, opts)`

以繪圖圖元（路徑、文本、圖片）列表表示的圖，可直接用於 canvas 或 SVG。

### `SchematicEditor.projectSymbols(x)`

專案中定義的自訂符號的型別。

### `SchematicEditor.readFiles(files, opts)`

來自所選檔案的專案：我們的 .json、KiCad 的 .kicad_sch（含其圖紙）或它們的 .zip。

### `SchematicEditor.readSim(result, built)`

模擬結果 → 網路電壓和元件電流（交流分析時以 dB 和度表示）。

### `SchematicEditor.refPrefix(type, style)`

某元件型別在某種樣式（IEC 或 GOST）中的位號字首。

### `SchematicEditor.refreshCustomSet(x)`

根據專案更新編輯器的“自訂符號”集。

### `SchematicEditor.refreshRefs(x, d, ids)`

給貼上或複製的元件分配下一個空閒位號。

### `SchematicEditor.registerShape(type, shape)`

新增一種形狀型別。

### `SchematicEditor.remove(d, ids)`

刪除專案。

### `SchematicEditor.removeBend(e, i)`

按索引刪除線條的一個折點。

### `SchematicEditor.removeSymbol(x, type)`

刪除未使用的自訂符號定義；返回是否已刪除。

### `SchematicEditor.resetBends(e)`

刪除線條的所有手動折點（恢復自動佈線）。

### `SchematicEditor.rotate(d, ids, dir)`

將形狀繞各自中心旋轉四分之一圈（1：順時針，-1：逆時針）。

### `SchematicEditor.route(d, e)`

重新計算連線線路徑。

### `SchematicEditor.scalePath(d, sx, sy)`

按 sx 和 sy 縮放 SVG 路徑資料（M L H V Q C A Z）。

### `SchematicEditor.setCornerJoins(on)`

開啟或關閉導線拐角處的連線點。

### `SchematicEditor.setDecorations(fn)`

設定繪製頁面裝飾（例如圖框和標題欄）的函式。

### `SchematicEditor.setFieldAll(x, type, value, name, text)`

為某型別（及值）的每個元件設定欄位；返回更改的數量。

### `SchematicEditor.setProjectHook(fn)`

設定每開啟一個專案都會呼叫的函式（用於遷移或補全其資料）。

### `SchematicEditor.setRefText(fn)`

設定格式化標籤中位號的函式。

### `SchematicEditor.setSheetProject(x, labels)`

設定繪製圖框和標題欄所用的專案及表格文本。

### `SchematicEditor.setStyle(x, style)`

設定整個專案的位號樣式：IEC 或 GOST。

### `SchematicEditor.setUnit(x, n, unit)`

將節點改為其封裝的另一個單元；原來的單元與其互換位置。

### `SchematicEditor.sheetBox(sh)`

圖紙的像素框及其毫米尺寸。

### `SchematicEditor.sheetInner(sh)`

圖紙的內框（毫米）。

### `SchematicEditor.sheetOf(x)`

專案的圖紙設定：尺寸、方向、圖框、標題欄欄位。

### `SchematicEditor.sheetPlan(box)`

圖按實際尺寸分割成 A4 紙張的方式：方向、列、行、分塊大小。

### `SchematicEditor.sheetSize(sh)`

圖紙的紙張尺寸（毫米）及其方向。

### `SchematicEditor.sheetWorkArea(sh, first)`

圖紙上可放置圖形的區域：去掉標題欄的圖框。

### `SchematicEditor.siblings(x, n)`

節點所在封裝中已放置的其他單元。

### `SchematicEditor.simErrors(info, errors)`

模擬器報告的問題（收斂、奇異矩陣、…），以編輯器會解釋的代碼表示。

### `SchematicEditor.snap(d, v)`

吸附開啟時將座標取整到網格。

### `SchematicEditor.spiceNumber(t)`

SPICE 記法或帶單位的數字（“10m”“1 MHz”“4.7k”）→ {text, value}，或 null。

### `SchematicEditor.spiceValue(v)`

元件值 → SPICE 記法：“10 kΩ”→“10k”，“1 MΩ”→“1Meg”。

### `SchematicEditor.splitWire(d, edgeId, x, y)`

在某點用連線點分割導線；返回連線點形狀，或 null。

### `SchematicEditor.styleOf(x)`

專案的位號樣式：“iec”或“gost”。

### `SchematicEditor.symbolGeometry(d)`

自訂符號定義的圖形：本體、引腳和引線。

### `SchematicEditor.symbolName(type)`

符號型別的名稱，或 null。

### `SchematicEditor.symbolOf(type)`

符號型別的定義，或 null。

### `SchematicEditor.symbolPreview(def)`

用於預覽的自訂符號定義的 SVG 圖片。

### `SchematicEditor.symbolUses(x, type)`

某符號型別在專案中被放置的次數。

### `SchematicEditor.template(name, texts, style)`

按模板名稱獲取的現成電路，帶你自己的文本，採用 IEC 或 GOST 樣式。

### `SchematicEditor.textBox(n)`

放置形狀文本的框，以及其行和對齊方式。

### `SchematicEditor.textFontData`

文本和字型區塊的資料：字型列表、載入字形樣式、新增字型檔案、某種語言的區塊標籤以及預設值。

### `SchematicEditor.toCanvas(d, opts)`

<canvas> 形式的圖表。

### `SchematicEditor.ungroup(d, ids)`

取消給定元素的分組；返回離開組的形狀數量。

### `SchematicEditor.unitOf(n)`

多單元元件節點的單元：{base, unit, units, multi}，或 null。

### `SchematicEditor.unitRef(n, style)`

帶單元的位號：U1A（IEC）或 DD1.1（GOST）。

### `SchematicEditor.unitTag(type, style)`

單元給位號新增的部分：“A”（IEC）或“.1”（GOST）。

### `SchematicEditor.unitsOf(x, n, style)`

供編輯器使用的節點封裝單元：哪些已放置、是否相同，以及電源單元是否已放置。

### `SchematicEditor.updateSymbol(x, type, def)`

修改自訂符號定義；每個已放置的副本都會使用新圖形。

### `SchematicEditor.visualBox(n)`

形狀在影像中佔據的全部範圍：其框和外部標籤。

### `SchematicEditor.wirePoint(d, x, y, tol, except)`

在一定距離內最近導線上的點，或 null（鬆開的導線端會落在這裡）。

### `SchematicEditor.withContents(d, ids)`

這些 id 以及其中每個容器的內容。

### `SchematicEditor.withGroups(d, ids)`

這些 id 以及其所在組的其他成員。

### `SchematicEditor.wrapText(text, it, maxWidth)`

將文本分成不超過指定寬度的行（單詞保持完整）。

## 相依套件

- @imagemagick/magick-wasm 0.0.35 (Apache-2.0)
- @imagemagick/magick-wasm 0.0.35 (Apache-2.0)
- @simonwep/pickr 1.10.2 (MIT)
- @simonwep/pickr 1.10.2 (MIT)
- chart.js 4.4.1 (MIT)
- dejavu-fonts-ttf 2.37.3 (Bitstream Vera Fonts License (free, redistributable); DejaVu changes are public domain)
- eecircuit-engine 1.9.0 (MIT)
- imagetracerjs 1.2.6 (Unlicense)
- jspdf 2.5.2 (MIT)
- jszip 3.10.1 ((MIT OR GPL-3.0-or-later))

- GitHub: https://github.com/OpenLibsDev/schematic-editor
- npm: https://www.npmjs.com/package/@openlibsdev/schematic-editor
- 開啟應用程式: https://openlibs.dev/zh-Hant/app/schematic-editor/
