-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from tscircuit/bigrefac
Major Refactor to All Schematic Objects - Use Absolute Coordinates, Breakup Functions, Fix Offsets
- Loading branch information
Showing
23 changed files
with
939 additions
and
498 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
lib/sch/svg-object-fns/create-svg-objects-for-sch-port-box-line.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import type { | ||
AnyCircuitElement, | ||
SchematicComponent, | ||
SchematicPort, | ||
} from "circuit-json" | ||
import type { SvgObject } from "lib/svg-object" | ||
import { applyToPoint, type Matrix } from "transformation-matrix" | ||
import { su } from "@tscircuit/soup-util" | ||
import { getSchStrokeSize } from "lib/utils/get-sch-stroke-size" | ||
|
||
const PIN_CIRCLE_RADIUS_MM = 0.02 | ||
|
||
/** | ||
* The schematic port box line is the line and circle that goes from the edge | ||
* of the component box to the port. | ||
*/ | ||
export const createSvgObjectsForSchPortBoxLine = ({ | ||
schPort, | ||
schComponent, | ||
transform, | ||
circuitJson, | ||
}: { | ||
schPort: SchematicPort | ||
schComponent: SchematicComponent | ||
transform: Matrix | ||
circuitJson: AnyCircuitElement[] | ||
}): SvgObject[] => { | ||
const svgObjects: SvgObject[] = [] | ||
|
||
const srcPort = su(circuitJson as any).source_port.get(schPort.source_port_id) | ||
|
||
const realEdgePos = { | ||
x: schPort.center.x, | ||
y: schPort.center.y, | ||
} | ||
|
||
// schPort.distance_from_component_edge is currently calculated incorrectly | ||
// in core | ||
const realPinLineLength = schPort.distance_from_component_edge ?? 0.4 | ||
|
||
switch (schPort.side_of_component) { | ||
case "left": | ||
realEdgePos.x += realPinLineLength | ||
break | ||
case "right": | ||
realEdgePos.x -= realPinLineLength | ||
break | ||
case "top": | ||
realEdgePos.y -= realPinLineLength | ||
break | ||
case "bottom": | ||
realEdgePos.y += realPinLineLength | ||
break | ||
} | ||
|
||
const screenSchPortPos = applyToPoint(transform, schPort.center) | ||
const screenRealEdgePos = applyToPoint(transform, realEdgePos) | ||
|
||
// Subtract the pin circle radius from the pin line length to get the end | ||
const realLineEnd = { ...schPort.center } | ||
|
||
switch (schPort.side_of_component) { | ||
case "left": | ||
realLineEnd.x += PIN_CIRCLE_RADIUS_MM | ||
break | ||
case "right": | ||
realLineEnd.x -= PIN_CIRCLE_RADIUS_MM | ||
break | ||
case "top": | ||
realLineEnd.y -= PIN_CIRCLE_RADIUS_MM | ||
break | ||
case "bottom": | ||
realLineEnd.y += PIN_CIRCLE_RADIUS_MM | ||
break | ||
} | ||
const screenLineEnd = applyToPoint(transform, realLineEnd) | ||
|
||
// Add port line | ||
svgObjects.push({ | ||
name: "line", | ||
type: "element", | ||
attributes: { | ||
class: "component-pin", | ||
x1: screenLineEnd.x.toString(), | ||
y1: screenLineEnd.y.toString(), | ||
x2: screenRealEdgePos.x.toString(), | ||
y2: screenRealEdgePos.y.toString(), | ||
"stroke-width": `${getSchStrokeSize(transform)}px`, | ||
}, | ||
value: "", | ||
children: [], | ||
}) | ||
|
||
// Add port circle | ||
svgObjects.push({ | ||
name: "circle", | ||
type: "element", | ||
attributes: { | ||
class: "component-pin", | ||
cx: screenSchPortPos.x.toString(), | ||
cy: screenSchPortPos.y.toString(), | ||
r: (Math.abs(transform.a) * PIN_CIRCLE_RADIUS_MM).toString(), | ||
"stroke-width": `${getSchStrokeSize(transform)}px`, | ||
}, | ||
value: "", | ||
children: [], | ||
}) | ||
|
||
return svgObjects | ||
} |
78 changes: 78 additions & 0 deletions
78
lib/sch/svg-object-fns/create-svg-objects-for-sch-port-pin-label.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import type { | ||
AnyCircuitElement, | ||
SchematicComponent, | ||
SchematicPort, | ||
} from "circuit-json" | ||
import type { SvgObject } from "lib/svg-object" | ||
import { colorMap } from "lib/utils/colors" | ||
import { getSchFontSize } from "lib/utils/get-sch-font-size" | ||
import { getUnitVectorFromOutsideToEdge } from "lib/utils/get-unit-vector-from-outside-to-edge" | ||
import { applyToPoint, type Matrix } from "transformation-matrix" | ||
|
||
const LABEL_DIST_FROM_EDGE_MM = 0.1 | ||
|
||
export const createSvgObjectsForSchPortPinLabel = (params: { | ||
schPort: SchematicPort | ||
schComponent: SchematicComponent | ||
transform: Matrix | ||
circuitJson: AnyCircuitElement[] | ||
}): SvgObject[] => { | ||
const svgObjects: SvgObject[] = [] | ||
const { schPort, schComponent, transform, circuitJson } = params | ||
|
||
const realPinNumberPos = { | ||
x: schPort.center.x, | ||
y: schPort.center.y, | ||
} | ||
|
||
if (!schPort.side_of_component) return [] | ||
const vecToEdge = getUnitVectorFromOutsideToEdge(schPort.side_of_component) | ||
|
||
const realPinEdgeDistance = schPort.distance_from_component_edge ?? 0.4 | ||
|
||
// Move the pin number halfway to the edge of the box component so it sits | ||
// between the edge and the port, exactly in the middle | ||
realPinNumberPos.x += | ||
vecToEdge.x * (realPinEdgeDistance + LABEL_DIST_FROM_EDGE_MM) | ||
realPinNumberPos.y += | ||
vecToEdge.y * (realPinEdgeDistance + LABEL_DIST_FROM_EDGE_MM) | ||
|
||
// Transform the pin position from local to global coordinates | ||
const screenPinNumberTextPos = applyToPoint(transform, realPinNumberPos) | ||
|
||
const label = schComponent.port_labels?.[`pin${schPort.pin_number}`] | ||
|
||
if (!label) return [] | ||
|
||
svgObjects.push({ | ||
name: "text", | ||
type: "element", | ||
attributes: { | ||
class: "pin-number", | ||
x: screenPinNumberTextPos.x.toString(), | ||
y: screenPinNumberTextPos.y.toString(), | ||
style: "font-family: sans-serif;", | ||
fill: colorMap.schematic.pin_number, | ||
"text-anchor": schPort.side_of_component === "left" ? "start" : "end", | ||
"dominant-baseline": "middle", | ||
"font-size": `${getSchFontSize(transform, "pin_number")}px`, | ||
transform: | ||
schPort.side_of_component === "top" || | ||
schPort.side_of_component === "bottom" | ||
? `rotate(-90 ${screenPinNumberTextPos.x} ${screenPinNumberTextPos.y})` | ||
: "", | ||
}, | ||
children: [ | ||
{ | ||
type: "text", | ||
value: label || "", | ||
name: "", | ||
attributes: {}, | ||
children: [], | ||
}, | ||
], | ||
value: "", | ||
}) | ||
|
||
return svgObjects | ||
} |
Oops, something went wrong.