# 回路図エディター — 開発者向けライブラリ

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`

用紙上の 1 ミリあたりのピクセル数（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)`

プロジェクトの部品。記号番号ごとに 1 行、並べ替え済み（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)`

プリミティブ（primitives() の結果）を canvas の 2D コンテキストに描きます。

### `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 字の接続と 3 本以上の配線の交差）。

### `SchematicEditor.labelName(n)`

ラベルまたは電源シンボルのネット名。

### `SchematicEditor.labelPlace(n)`

記号の外側のラベルの位置：下、上、または右。

### `SchematicEditor.labelText(n)`

記号のラベルのテキスト：記号番号、値、テキストを 1 行ずつ。

### `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)`

描かれている線の 1 区間を (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)`

シミュレーター結果 → ネットの電圧と部品の電流（AC 解析では 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)`

図形をそれぞれの中心で 90 度回転します（1：時計回り、-1：反時計回り）。

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

コネクターの経路を再計算します。

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

SVG パスデータ（M L H V Q C A Z）を sx と sy で拡大縮小します。

### `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/ja/app/schematic-editor/
