-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new methods, absolute positions used, function breakdowns
- Loading branch information
Showing
10 changed files
with
419 additions
and
205 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
107 changes: 107 additions & 0 deletions
107
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,107 @@ | ||
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" | ||
|
||
const PIN_CIRCLE_RADIUS_PX = 3 | ||
|
||
/** | ||
* 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 = 0.2 // schPort.distance_from_component_edge ?? 0.2 | ||
|
||
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 screenLineEnd = applyToPoint(transform, schPort.center) | ||
switch (schPort.side_of_component) { | ||
case "left": | ||
screenLineEnd.x += PIN_CIRCLE_RADIUS_PX | ||
break | ||
case "right": | ||
screenLineEnd.x -= PIN_CIRCLE_RADIUS_PX | ||
break | ||
case "top": | ||
screenLineEnd.y -= PIN_CIRCLE_RADIUS_PX | ||
break | ||
case "bottom": | ||
screenLineEnd.y += PIN_CIRCLE_RADIUS_PX | ||
break | ||
} | ||
|
||
// 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": "2px", | ||
}, | ||
value: "", | ||
children: [], | ||
}) | ||
|
||
// Add port circle | ||
svgObjects.push({ | ||
name: "circle", | ||
type: "element", | ||
attributes: { | ||
class: "component-pin", | ||
cx: screenSchPortPos.x.toString(), | ||
cy: screenSchPortPos.y.toString(), | ||
r: PIN_CIRCLE_RADIUS_PX.toString(), | ||
"stroke-width": "2px", | ||
}, | ||
value: "", | ||
children: [], | ||
}) | ||
|
||
return svgObjects | ||
} |
Empty file.
106 changes: 106 additions & 0 deletions
106
lib/sch/svg-object-fns/create-svg-objects-for-sch-port-pin-number-text.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,106 @@ | ||
import type { | ||
AnyCircuitElement, | ||
SchematicComponent, | ||
SchematicPort, | ||
} from "circuit-json" | ||
import type { SvgObject } from "lib/svg-object" | ||
import { colorMap } from "lib/utils/colors" | ||
import { getUnitVectorFromOutsideToEdge } from "lib/utils/get-unit-vector-from-outside-to-edge" | ||
import { applyToPoint, type Matrix } from "transformation-matrix" | ||
|
||
export const createSvgObjectsForSchPortPinNumberText = (params: { | ||
schPort: SchematicPort | ||
schComponent: SchematicComponent | ||
transform: Matrix | ||
circuitJson: AnyCircuitElement[] | ||
}): SvgObject[] => { | ||
const svgObjects: SvgObject[] = [] | ||
const { schPort, schComponent, transform, circuitJson } = params | ||
|
||
const pinNumberOffset = 0.2 | ||
let localPinNumberTextX = schPort.center.x | ||
let localPinNumberTextY = schPort.center.y | ||
let dominantBaseline = "auto" | ||
let textAnchor = "middle" | ||
|
||
switch (schPort.side_of_component) { | ||
case "top": | ||
// For top ports, stay at the same X but offset Y upward | ||
localPinNumberTextY = schPort.center.y - pinNumberOffset // Move above the circle | ||
localPinNumberTextX = schPort.center.x // Stay aligned with port | ||
dominantBaseline = "middle" | ||
break | ||
case "bottom": | ||
// For bottom ports, stay at the same X but offset Y downward | ||
localPinNumberTextY = schPort.center.y + pinNumberOffset | ||
localPinNumberTextX = schPort.center.x | ||
dominantBaseline = "middle" | ||
textAnchor = "left" | ||
break | ||
case "left": | ||
// For left ports, stay at the same Y but offset X | ||
localPinNumberTextX = schPort.center.x - pinNumberOffset | ||
localPinNumberTextY = schPort.center.y + pinNumberOffset / 4 | ||
dominantBaseline = "auto" | ||
break | ||
case "right": | ||
// For right ports, stay at the same Y but offset X | ||
localPinNumberTextX = schPort.center.x + pinNumberOffset | ||
localPinNumberTextY = schPort.center.y + pinNumberOffset / 4 | ||
dominantBaseline = "auto" | ||
break | ||
} | ||
|
||
const realPinNumberPos = { | ||
x: schPort.center.x, | ||
y: schPort.center.y, | ||
} | ||
|
||
if (!schPort.side_of_component) return [] | ||
const vecToEdge = getUnitVectorFromOutsideToEdge(schPort.side_of_component) | ||
console.log(schPort.pin_number, schPort.side_of_component, vecToEdge) | ||
|
||
const realPinEdgeDistance = 0.2 | ||
|
||
// 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) / 2 | ||
realPinNumberPos.y += (vecToEdge.y * realPinEdgeDistance) / 2 | ||
|
||
// Transform the pin position from local to global coordinates | ||
const screenPinNumberTextPos = applyToPoint(transform, realPinNumberPos) | ||
// Move the pin number text up a bit so it doesn't hit the port line | ||
screenPinNumberTextPos.y -= 4 //px | ||
|
||
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": "middle", | ||
"dominant-baseline": dominantBaseline, | ||
"font-size": "11px", | ||
transform: | ||
schPort.side_of_component === "top" || | ||
schPort.side_of_component === "bottom" | ||
? `rotate(90deg, ${screenPinNumberTextPos.x}, ${screenPinNumberTextPos.y})` | ||
: "", | ||
}, | ||
children: [ | ||
{ | ||
type: "text", | ||
value: schPort.pin_number?.toString() || "", | ||
name: "", | ||
attributes: {}, | ||
children: [], | ||
}, | ||
], | ||
value: "", | ||
}) | ||
|
||
return svgObjects | ||
} |
Oops, something went wrong.