Skip to content

Commit

Permalink
Merge pull request #135 from tscircuit/feat/schematic
Browse files Browse the repository at this point in the history
  • Loading branch information
imrishabh18 authored Oct 4, 2024
2 parents e6ab233 + 8dc8dea commit 052e05b
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 69 deletions.
Binary file modified bun.lockb
Binary file not shown.
97 changes: 59 additions & 38 deletions lib/components/base-components/NormalComponent.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
import { Footprint } from "../primitive-components/Footprint"
import { ZodType, z } from "zod"
import { PrimitiveComponent } from "./PrimitiveComponent"
import { Port } from "../primitive-components/Port"
import { symbols, type BaseSymbolName, type SchSymbol } from "schematic-symbols"
import { fp } from "footprinter"
import {
isValidElement as isReactElement,
isValidElement,
type ReactElement,
type ReactNode,
} from "react"
import {
createInstanceFromReactElement,
type ReactSubtree,
} from "lib/fiber/create-instance-from-react-element"
import { getPortFromHints } from "lib/utils/getPortFromHints"
import { createComponentsFromSoup } from "lib/utils/createComponentsFromSoup"
import { Net } from "../primitive-components/Net"
import { createNetsFromProps } from "lib/utils/components/createNetsFromProps"
import { getBoundsOfPcbComponents } from "lib/utils/get-bounds-of-pcb-components"
import type {
CadModelJscad,
CadModelObj,
CadModelProp,
CadModelStl,
} from "@tscircuit/props"
import { rotation } from "circuit-json"
import { fp } from "footprinter"
import {
type ReactSubtree,
createInstanceFromReactElement,
} from "lib/fiber/create-instance-from-react-element"
import { createNetsFromProps } from "lib/utils/components/createNetsFromProps"
import { createComponentsFromSoup } from "lib/utils/createComponentsFromSoup"
import { getBoundsOfPcbComponents } from "lib/utils/get-bounds-of-pcb-components"
import { getPortFromHints } from "lib/utils/getPortFromHints"
import {
type ReactElement,
isValidElement as isReactElement,
isValidElement,
} from "react"
import { type SchSymbol, symbols } from "schematic-symbols"
import { ZodType, z } from "zod"
import { Footprint } from "../primitive-components/Footprint"
import { Port } from "../primitive-components/Port"
import { PrimitiveComponent } from "./PrimitiveComponent"

const rotation3 = z.object({
x: rotation,
Expand Down Expand Up @@ -85,6 +83,41 @@ export class NormalComponent<
initPorts() {
const { config } = this
const portsToCreate: Port[] = []

// Handle schPortArrangement
const schPortArrangement = this._parsedProps.schPortArrangement as any
if (schPortArrangement) {
for (const side in schPortArrangement) {
const pins = schPortArrangement[side].pins
if (Array.isArray(pins)) {
for (const pinNumber of pins) {
portsToCreate.push(new Port({ pinNumber }))
}
}
}
}

const pinLabels: Record<string, string> | undefined =
this._parsedProps.pinLabels
if (pinLabels) {
for (let [pinNumber, label] of Object.entries(pinLabels)) {
pinNumber = pinNumber.replace("pin", "")
let existingPort = portsToCreate.find(
(p) => p._parsedProps.pinNumber === Number(pinNumber),
)
if (!existingPort) {
existingPort = new Port({
pinNumber: parseInt(pinNumber),
name: label,
})
portsToCreate.push(existingPort)
} else {
existingPort.externallyAddedAliases.push(label)
existingPort.props.name = label
}
}
}

if (config.schematicSymbolName) {
const sym = symbols[
`${config.schematicSymbolName}_horz` as keyof typeof symbols
Expand All @@ -105,23 +138,11 @@ export class NormalComponent<
}

const portsFromFootprint = this.getPortsFromFootprint()
portsToCreate.push(...portsFromFootprint)

const pinLabels: Record<string, string> | undefined =
this._parsedProps.pinLabels
if (pinLabels) {
for (let [pinNumber, label] of Object.entries(pinLabels)) {
pinNumber = pinNumber.replace("pin", "")
const existingPort = portsToCreate.find(
(p) => p._parsedProps.pinNumber === Number(pinNumber),
)
if (!existingPort) {
throw new Error(
`Could not find port for pin number ${pinNumber} in chip ${this.getString()}`,
)
}
existingPort.externallyAddedAliases.push(label)
existingPort.props.name = label
for (const port of portsFromFootprint) {
if (
!portsToCreate.some((p) => p.isMatchingAnyOf(port.getNameAndAliases()))
) {
portsToCreate.push(port)
}
}

Expand Down
38 changes: 27 additions & 11 deletions lib/utils/schematic/getAllDimensionsForSchematicBox.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { current } from "circuit-json"
import { getSizeOfSidesFromPortArrangement } from "./getSizeOfSidesFromPortArrangement"
import { schematicPortArrangement } from "@tscircuit/props"
import { z } from "zod"

export type VerticalPortSideConfiguration = {
direction?: "top-to-bottom" | "bottom-to-top"
Expand Down Expand Up @@ -58,6 +55,12 @@ interface Params {

type Side = "left" | "right" | "top" | "bottom"

function isExplicitPinMappingArrangement(
arrangement: PortArrangement,
): arrangement is ExplicitPinMappingArrangement {
return (arrangement as ExplicitPinMappingArrangement).leftSide !== undefined
}

export interface SchematicBoxDimensions {
pinCount: number
getPortPositionByPinNumber(pinNumber: number): { x: number; y: number }
Expand Down Expand Up @@ -87,7 +90,6 @@ export const getAllDimensionsForSchematicBox = (
let sidePinCounts = params.schPortArrangement
? getSizeOfSidesFromPortArrangement(params.schPortArrangement)
: null

const sideLengths: Record<Side, number> = {
left: 0,
right: 0,
Expand Down Expand Up @@ -136,7 +138,11 @@ export const getAllDimensionsForSchematicBox = (
let truePinIndex = 0
// moving downward from the top-left corner
for (let sideIndex = 0; sideIndex < sidePinCounts.leftSize; sideIndex++) {
const pinNumber = truePinIndex + 1 // TODO check mapping from schPortArrangement
const pinNumber =
params.schPortArrangement &&
isExplicitPinMappingArrangement(params.schPortArrangement)
? params.schPortArrangement?.leftSide?.pins[sideIndex]!
: truePinIndex + 1
const pinStyle =
params.schPinStyle?.[`pin${pinNumber}`] ?? params.schPinStyle?.[pinNumber]

Expand Down Expand Up @@ -167,7 +173,11 @@ export const getAllDimensionsForSchematicBox = (
currentDistanceFromEdge = 0
// moving rightward from the left-bottom corner
for (let sideIndex = 0; sideIndex < sidePinCounts.bottomSize; sideIndex++) {
const pinNumber = truePinIndex + 1 // TODO check mapping from schPortArrangement
const pinNumber =
params.schPortArrangement &&
isExplicitPinMappingArrangement(params.schPortArrangement)
? params.schPortArrangement.bottomSide?.pins[sideIndex]!
: truePinIndex + 1
const pinStyle =
params.schPinStyle?.[`pin${pinNumber}`] ?? params.schPinStyle?.[pinNumber]

Expand Down Expand Up @@ -198,7 +208,11 @@ export const getAllDimensionsForSchematicBox = (
currentDistanceFromEdge = 0
// moving upward from the bottom-right corner
for (let sideIndex = 0; sideIndex < sidePinCounts.rightSize; sideIndex++) {
const pinNumber = truePinIndex + 1 // TODO check mapping from schPortArrangement
const pinNumber =
params.schPortArrangement &&
isExplicitPinMappingArrangement(params.schPortArrangement)
? params.schPortArrangement.rightSide?.pins[sideIndex]!
: truePinIndex + 1
const pinStyle =
params.schPinStyle?.[`pin${pinNumber}`] ?? params.schPinStyle?.[pinNumber]

Expand Down Expand Up @@ -229,7 +243,11 @@ export const getAllDimensionsForSchematicBox = (
currentDistanceFromEdge = 0
// moving leftward from the top-right corner
for (let sideIndex = 0; sideIndex < sidePinCounts.topSize; sideIndex++) {
const pinNumber = truePinIndex + 1 // TODO check mapping from schPortArrangement
const pinNumber =
params.schPortArrangement &&
isExplicitPinMappingArrangement(params.schPortArrangement)
? params.schPortArrangement.topSide?.pins[sideIndex]!
: truePinIndex + 1
const pinStyle =
params.schPinStyle?.[`pin${pinNumber}`] ?? params.schPinStyle?.[pinNumber]

Expand Down Expand Up @@ -321,9 +339,7 @@ export const getAllDimensionsForSchematicBox = (
(p) => p.pinNumber.toString() === pinNumber.toString(),
)
if (!port) {
throw new Error(
`Could not find port for pin number ${pinNumber}, available pins: ${truePortsWithPositions.map((tp) => tp.pinNumber).join(", ")}`,
)
return { x: 0, y: 0 }
}
return port
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@types/react": "^18.3.3",
"@types/react-reconciler": "^0.28.8",
"bun-match-svg": "0.0.2",
"circuit-to-svg": "^0.0.37",
"circuit-to-svg": "^0.0.39",
"debug": "^4.3.6",
"howfat": "^0.3.8",
"looks-same": "^9.0.1",
Expand Down
17 changes: 10 additions & 7 deletions tests/components/normal-components/__snapshots__/chip.snap.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 052e05b

Please sign in to comment.