UNPKG

@skillpet/circuit

Version:

Circuit diagram library — render electrical schematics from JSON, with interactive SVG, themes, and Vue/React components

117 lines (116 loc) 5.06 kB
/** * Drawing canvas — aligned with Python `schemdraw.Drawing` (SVG subset). */ import { ImageFormat, type BBox, type ImageType } from "./types.js"; import { type SchemdrawStyle } from "./config.js"; import { Point, type XY } from "./geometry/point.js"; import { Container } from "./container.js"; import { Element } from "./element.js"; import type { SegmentLike } from "./segment.js"; /** Metadata attached to an element for interactive / introspection use. */ export interface ElementMeta { id: string; type: string; tooltip?: string; } /** * 电路画布。构造返回的实例经 `Proxy` 支持 Python 式锚点:`drawing.foo` 在已 `setAnchor('foo')` 且不与成员名冲突时得到 {@link Point}。 * TypeScript 若报「无此属性」,可用 {@link Drawing.getAnchor} 或 `anchors['foo']`。 */ export declare class Drawing { readonly elements: Element[]; /** Named positions in drawing coordinates (`set_anchor` / Python `Drawing.anchors`). */ anchors: Record<string, [number, number]>; /** Extra SVG `<defs>` fragments (Python `Drawing.svgdefs`). */ readonly svgdefs: string[]; dwgparams: Record<string, unknown>; _here: Point; _theta: number; private readonly _state; outfile?: string; private readonly _elementMeta; setElementMeta(el: Element, meta: ElementMeta): void; getElementMeta(el: Element): ElementMeta | undefined; /** * Current placement position (Python `Drawing.here` / `_here`). * Get/set both call `push_element(None)` like Python — flushes any pending implicit `add`. */ get here(): Point; set here(v: Point | XY); /** Current drawing direction in degrees (Python `Drawing.theta`). Getter flushes implicit stack. */ get theta(): number; set theta(v: number); /** Two-terminal default length (Python `Drawing.unit`). */ get unit(): number; set unit(v: number); constructor(opts?: Partial<SchemdrawStyle> & { file?: string; }); /** * Typed read of a named anchor (same coordinates as Python `drawing.name` after `set_anchor`). * Dot access `drawing.name` also works on instances returned by `new Drawing()` / `withDrawing`. */ getAnchor(name: string): Point | undefined; enter(): void; exit(): void; containsElement(el: Element): boolean; add(el: Element): Element; /** Remove last element; restore `here` / `theta` from last remaining (Python `Drawing.undo`). */ undo(): void; /** Merge drawing defaults (Python `Drawing.config`). */ config(partial: Partial<SchemdrawStyle>): void; /** Append multiple elements (Python `Drawing.add_elements`). */ addElements(...els: Element[]): void; /** * Grouping box for implicit `add` stack (Python `Drawing.container()` / `with drawing.container():`). * Call {@link Container.enter} before child elements and {@link Container.exit} after, then {@link add} the container. */ container(opts?: { cornerradius?: number; padx?: number; pady?: number; }): Container; /** Offset the current placement position (Python `Drawing.move`). */ move(dx?: number, dy?: number): void; /** Set placement position relative to `ref` (Python `Drawing.move_from`). */ moveFrom(ref: Point, dx?: number, dy?: number, theta?: number): void; /** Save `here` / `theta` on a stack (Python `Drawing.push`). */ push(): void; /** Restore last pushed `here` / `theta` (Python `Drawing.pop`). */ pop(): void; /** * Save / restore `here` and `theta` around a callback — same stack as `push`/`pop` * (Python `with drawing.hold():`). */ withHold<T>(fn: () => T): T; /** * Manual alternative to `withHold`: call `hold()` then `close()` when done * (Python `HoldState` context manager). */ hold(): { close: () => void; }; /** Append raw XML inside `<defs>` (Python `Drawing.add_svgdef`). */ addSvgDef(svgdef: string): void; /** Record a named anchor at the current position (Python `Drawing.set_anchor`). */ setAnchor(name: string): void; /** * Flattened transformed segments — `params` = user ∪ elm ∪ defaults (**no** drawing `_dwgparams`), * aligned with Python `Drawing.get_segments`. */ getSegments(): SegmentLike[]; /** * Write SVG to a file (Node only). Extension should be `.svg`. * In browser environments this method throws — use {@link toSvgString} instead. */ save(path: string): Promise<void>; getBBox(): BBox; /** Render to SVG string (user units, SVG backend). */ toSvgString(): string; /** * Python `Drawing.get_imagedata` — 当前构建仅支持 SVG(UTF-8 字节);PNG 等栅格格式未接入。 */ getImageData(fmt?: ImageFormat | ImageType | string): Uint8Array; } /** Run `fn` with drawing stack (implicit `add` for elements created inside). */ export declare function withDrawing(fn: (d: Drawing) => void, opts?: Partial<SchemdrawStyle>): Drawing;