# 原理图编辑器 — 开发者库

基于 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-Hans/app/schematic-editor/
