diff --git a/lib/Circuit.ts b/lib/Circuit.ts index 8a9ce57a..6621631c 100644 --- a/lib/Circuit.ts +++ b/lib/Circuit.ts @@ -19,6 +19,7 @@ export class Circuit { db: SoupUtilObjects root: Circuit | null = null isRoot = true + schematicDisabled = false _hasRenderedAtleastOnce = false @@ -86,7 +87,6 @@ export class Circuit { this._guessRootComponent() } const { firstChild, db } = this - if (!firstChild) throw new Error("Project has no root component") firstChild.parent = this as any firstChild.runRenderCycle() @@ -162,7 +162,6 @@ export class Circuit { this._guessRootComponent() return this.firstChild?.selectAll(selector) ?? [] } - selectOne( selector: string, opts?: { type?: "component" | "port" }, diff --git a/lib/components/base-components/NormalComponent.ts b/lib/components/base-components/NormalComponent.ts index 24a61252..93014d7c 100644 --- a/lib/components/base-components/NormalComponent.ts +++ b/lib/components/base-components/NormalComponent.ts @@ -106,6 +106,7 @@ export class NormalComponent< additionalAliases?: Record<`pin${number}`, string[]> } = {}, ) { + if (this.root?.schematicDisabled) return const { config } = this const portsToCreate: Port[] = [] @@ -354,6 +355,7 @@ export class NormalComponent< * You can override this method to do more complicated things. */ doInitialSchematicComponentRender() { + if (this.root?.schematicDisabled) return const { schematicSymbolName } = this.config if (schematicSymbolName) { return this._doInitialSchematicComponentRenderWithSymbol() @@ -373,6 +375,7 @@ export class NormalComponent< } _doInitialSchematicComponentRenderWithSymbol() { + if (this.root?.schematicDisabled) return const { db } = this.root! const { _parsedProps: props } = this @@ -396,6 +399,7 @@ export class NormalComponent< } _doInitialSchematicComponentRenderWithSchematicBoxDimensions() { + if (this.root?.schematicDisabled) return const { db } = this.root! const { _parsedProps: props } = this const dimensions = this._getSchematicBoxDimensions()! @@ -614,6 +618,7 @@ export class NormalComponent< } getPortsFromSchematicSymbol(): Port[] { + if (this.root?.schematicDisabled) return [] const { config } = this if (!config.schematicSymbolName) return [] const symbol: SchSymbol = (symbols as any)[config.schematicSymbolName] diff --git a/lib/components/normal-components/Chip.ts b/lib/components/normal-components/Chip.ts index 20def03e..491f3d8f 100644 --- a/lib/components/normal-components/Chip.ts +++ b/lib/components/normal-components/Chip.ts @@ -31,42 +31,44 @@ export class Chip extends NormalComponent< manufacturer_part_number: props.manufacturerPartNumber, supplier_part_numbers: props.supplierPartNumbers, }) - const dimensions = this._getSchematicBoxDimensions() - const hasTopOrBottomPins = - props.schPortArrangement?.topSide !== undefined || - props.schPortArrangement?.bottomSide !== undefined - const schematic_box_width = dimensions?.getSize().width - const schematic_box_height = dimensions?.getSize().height - const manufacturer_part_number_text = db.schematic_text.insert({ - text: props.manufacturerPartNumber ?? "", - schematic_component_id: source_component.source_component_id, - anchor: "left", - rotation: 0, - position: { - x: hasTopOrBottomPins - ? (props.schX ?? 0) + (schematic_box_width ?? 0) / 2 + 0.1 - : (props.schX ?? 0) - (schematic_box_width ?? 0) / 2, - y: hasTopOrBottomPins - ? (props.schY ?? 0) + (schematic_box_height ?? 0) / 2 + 0.35 - : (props.schY ?? 0) - (schematic_box_height ?? 0) / 2 - 0.13, - }, - color: "#006464", - }) - const component_name_text = db.schematic_text.insert({ - text: props.name ?? "", - schematic_component_id: source_component.source_component_id, - anchor: "left", - rotation: 0, - position: { - x: hasTopOrBottomPins - ? (props.schX ?? 0) + (schematic_box_width ?? 0) / 2 + 0.1 - : (props.schX ?? 0) - (schematic_box_width ?? 0) / 2, - y: hasTopOrBottomPins - ? (props.schY ?? 0) + (schematic_box_height ?? 0) / 2 + 0.55 - : (props.schY ?? 0) + (schematic_box_height ?? 0) / 2 + 0.13, - }, - color: "#006464", - }) + if (!this.parent?.root?.schematicDisabled) { + const dimensions = this._getSchematicBoxDimensions() + const hasTopOrBottomPins = + props.schPortArrangement?.topSide !== undefined || + props.schPortArrangement?.bottomSide !== undefined + const schematic_box_width = dimensions?.getSize().width + const schematic_box_height = dimensions?.getSize().height + const manufacturer_part_number_text = db.schematic_text.insert({ + text: props.manufacturerPartNumber ?? "", + schematic_component_id: source_component.source_component_id, + anchor: "left", + rotation: 0, + position: { + x: hasTopOrBottomPins + ? (props.schX ?? 0) + (schematic_box_width ?? 0) / 2 + 0.1 + : (props.schX ?? 0) - (schematic_box_width ?? 0) / 2, + y: hasTopOrBottomPins + ? (props.schY ?? 0) + (schematic_box_height ?? 0) / 2 + 0.35 + : (props.schY ?? 0) - (schematic_box_height ?? 0) / 2 - 0.13, + }, + color: "#006464", + }) + const component_name_text = db.schematic_text.insert({ + text: props.name ?? "", + schematic_component_id: source_component.source_component_id, + anchor: "left", + rotation: 0, + position: { + x: hasTopOrBottomPins + ? (props.schX ?? 0) + (schematic_box_width ?? 0) / 2 + 0.1 + : (props.schX ?? 0) - (schematic_box_width ?? 0) / 2, + y: hasTopOrBottomPins + ? (props.schY ?? 0) + (schematic_box_height ?? 0) / 2 + 0.55 + : (props.schY ?? 0) + (schematic_box_height ?? 0) / 2 + 0.13, + }, + color: "#006464", + }) + } this.source_component_id = source_component.source_component_id! } doInitialPcbComponentRender() { diff --git a/lib/components/normal-components/PinHeader.ts b/lib/components/normal-components/PinHeader.ts index 1f98f689..54398bc6 100644 --- a/lib/components/normal-components/PinHeader.ts +++ b/lib/components/normal-components/PinHeader.ts @@ -62,20 +62,22 @@ export class PinHeader extends NormalComponent { pin_count: props.pinCount, gender: props.gender, } as SourceSimplePinHeader) - const dimensions = this._getSchematicBoxDimensions() - const schematic_box_width = dimensions?.getSize().width - const schematic_box_height = dimensions?.getSize().height - const component_name_text = db.schematic_text.insert({ - text: props.name ?? "", - schematic_component_id: source_component.source_component_id, - anchor: "left", - rotation: 0, - position: { - x: (props.schX ?? 0) - (schematic_box_width ?? 0) / 2, - y: (props.schY ?? 0) + (schematic_box_height ?? 0) / 2 + 0.13, - }, - color: "#006464", - }) + if (!this.parent?.root?.schematicDisabled) { + const dimensions = this._getSchematicBoxDimensions() + const schematic_box_width = dimensions?.getSize().width + const schematic_box_height = dimensions?.getSize().height + const component_name_text = db.schematic_text.insert({ + text: props.name ?? "", + schematic_component_id: source_component.source_component_id, + anchor: "left", + rotation: 0, + position: { + x: (props.schX ?? 0) - (schematic_box_width ?? 0) / 2, + y: (props.schY ?? 0) + (schematic_box_height ?? 0) / 2 + 0.13, + }, + color: "#006464", + }) + } this.source_component_id = source_component.source_component_id } } diff --git a/lib/components/primitive-components/NetAlias.ts b/lib/components/primitive-components/NetAlias.ts index 02dbdba6..de89a53d 100644 --- a/lib/components/primitive-components/NetAlias.ts +++ b/lib/components/primitive-components/NetAlias.ts @@ -13,6 +13,7 @@ export class NetAlias extends PrimitiveComponent { } doInitialSchematicComponentRender(): void { + if (this.root?.schematicDisabled) return const { db } = this.root! const { _parsedProps: props } = this diff --git a/lib/components/primitive-components/Trace/Trace.ts b/lib/components/primitive-components/Trace/Trace.ts index 642deb6a..fa8c7b75 100644 --- a/lib/components/primitive-components/Trace/Trace.ts +++ b/lib/components/primitive-components/Trace/Trace.ts @@ -597,6 +597,7 @@ export class Trace } doInitialSchematicTraceRender(): void { + if (this.root?.schematicDisabled) return const { db } = this.root! const { _parsedProps: props, parent } = this diff --git a/tests/examples/__snapshots__/example9-shcematicDisabled-schematic.snap.svg b/tests/examples/__snapshots__/example9-shcematicDisabled-schematic.snap.svg new file mode 100644 index 00000000..9b060b64 --- /dev/null +++ b/tests/examples/__snapshots__/example9-shcematicDisabled-schematic.snap.svg @@ -0,0 +1,17 @@ +-2,-2-2,-1-2,0-2,1-2,2-1,-2-1,-1-1,0-1,1-1,20,-20,-10,00,10,21,-21,-11,01,11,22,-22,-12,02,12,23,-23,-13,03,13,24,-24,-14,04,14,2 \ No newline at end of file diff --git a/tests/examples/example9-shcematicDisabled.test.tsx b/tests/examples/example9-shcematicDisabled.test.tsx new file mode 100644 index 00000000..695d3082 --- /dev/null +++ b/tests/examples/example9-shcematicDisabled.test.tsx @@ -0,0 +1,41 @@ +import { expect, test } from "bun:test" +import { getTestFixture } from "../fixtures/get-test-fixture" + +test("Should not render any schematic components", async () => { + const { circuit } = getTestFixture() + circuit.schematicDisabled = true + + circuit.add( + + + + port.right", ".U1 > .pin20"]} /> + , + ) + + circuit.render() + + const schematicComponents = circuit + .getCircuitJson() + .filter((c) => c.type === "schematic_component") + expect(schematicComponents.length).toBe(0) + const traces = circuit + .getCircuitJson() + .filter((c) => c.type === "schematic_trace") + expect(traces.length).toBe(0) + expect(circuit).toMatchSchematicSnapshot(import.meta.path) +})