Skip to content

Commit

Permalink
Support root.schematicDisabled to skip rendering all schematic compon…
Browse files Browse the repository at this point in the history
…ents
  • Loading branch information
AnasSarkiz committed Dec 2, 2024
1 parent 424c257 commit d2fcaf5
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 52 deletions.
3 changes: 1 addition & 2 deletions lib/Circuit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class Circuit {
db: SoupUtilObjects
root: Circuit | null = null
isRoot = true
schematicDisabled = false

_hasRenderedAtleastOnce = false

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -162,7 +162,6 @@ export class Circuit {
this._guessRootComponent()
return this.firstChild?.selectAll(selector) ?? []
}

selectOne(
selector: string,
opts?: { type?: "component" | "port" },
Expand Down
5 changes: 5 additions & 0 deletions lib/components/base-components/NormalComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export class NormalComponent<
additionalAliases?: Record<`pin${number}`, string[]>
} = {},
) {
if (this.root?.schematicDisabled) return
const { config } = this
const portsToCreate: Port[] = []

Expand Down Expand Up @@ -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()
Expand All @@ -373,6 +375,7 @@ export class NormalComponent<
}

_doInitialSchematicComponentRenderWithSymbol() {
if (this.root?.schematicDisabled) return
const { db } = this.root!
const { _parsedProps: props } = this

Expand All @@ -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()!
Expand Down Expand Up @@ -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]
Expand Down
74 changes: 38 additions & 36 deletions lib/components/normal-components/Chip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,42 +31,44 @@ export class Chip<PinLabels extends string = never> 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() {
Expand Down
30 changes: 16 additions & 14 deletions lib/components/normal-components/PinHeader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,22 @@ export class PinHeader extends NormalComponent<typeof pinHeaderProps> {
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
}
}
1 change: 1 addition & 0 deletions lib/components/primitive-components/NetAlias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export class NetAlias extends PrimitiveComponent<typeof netAliasProps> {
}

doInitialSchematicComponentRender(): void {
if (this.root?.schematicDisabled) return
const { db } = this.root!
const { _parsedProps: props } = this

Expand Down
1 change: 1 addition & 0 deletions lib/components/primitive-components/Trace/Trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,7 @@ export class Trace
}

doInitialSchematicTraceRender(): void {
if (this.root?.schematicDisabled) return
const { db } = this.root!
const { _parsedProps: props, parent } = this

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions tests/examples/example9-shcematicDisabled.test.tsx
Original file line number Diff line number Diff line change
@@ -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(
<board width={10} height={10}>
<chip
name="U1"
manufacturerPartNumber="part-number"
schX={0}
schY={0}
schWidth={1}
schHeight={5}
footprint="ssop28Db"
/>
<diode
name="LED1"
footprint="0805"
symbolName="diode"
schX={2}
schY={1}
/>
<trace path={[".LED1 > port.right", ".U1 > .pin20"]} />
</board>,
)

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)
})

0 comments on commit d2fcaf5

Please sign in to comment.