-
Notifications
You must be signed in to change notification settings - Fork 14
/
circuitypst.typ
173 lines (151 loc) · 4.36 KB
/
circuitypst.typ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#import "components.typ"
#import "utils.typ"
#import "../typst-canvas/draw.typ": *
#import "../typst-canvas/coordinate.typ"
#import "../typst-canvas/vector.typ"
#let canvas-fill = fill
#let canvas-stroke = stroke
// #let draw-component(component) = {
// ((
// children: components.
// ),)
// }
#let node(component, position, label: none, name: none, anchor: none, fill: auto, stroke: auto, ..options) = {
assert(component in components.node, message: "Component '" + component + "' is unknown")
group(name: name, anchor: anchor, {
import "../typst-canvas/draw.typ": anchor
if fill != auto {
canvas-fill(fill)
}
if stroke != auto {
canvas-stroke(stroke)
}
set-origin(position)
anchor("center", (0,0))
components.node.at(component)
utils.anchors((
// IM A GENIUS
"north east": ("north", "-|", "east"),
"south east": ("south", "-|", "east"),
"north west": ("north", "-|", "west"),
"south west": ("south", "-|", "west"),
n: "north",
s: "south",
e: "east",
w: "west",
))
if label != none {
content("center", label)
}
})
move-to(position)
}
#let to(
component,
start,
end,
label: none,
name: none,
i: none,
v: none,
poles: none,
stroke: auto,
fill: auto,
) = {
assert(component in components.path, message: "Component '" + component + "' is unknown")
group(name: name, {
if fill != auto {
canvas-fill(fill)
}
if stroke != auto {
canvas-stroke(stroke)
}
anchor("start", start)
anchor("end", end)
// This transformation moves (0,0) to the mid point of the line and rotates such that the line from -> to lies flat on the x axis
rotate((
utils.get-angle,
// (s, e) => panic(utils.get-angle(s, e)),
"start",
"end"
))
// rotate(90deg)
set-origin(("start", 0.5, "end"))
anchor("center", (0,0))
line("start", (-0.5, 0))
line("end", (0.5, 0))
components.path.at(component)
utils.anchors((
west: (-0.5, 0),
east: (0.5, 0),
// IM A GENIUS
"north east": ("north", "-|", "east"),
"south east": ("south", "-|", "east"),
"north west": ("north", "-|", "west"),
"south west": ("south", "-|", "west"),
n: "north",
s: "south",
e: "east",
w: "west",
))
if label != none {
content("label", label)
}
if v != none {
assert(type(v) == "content" or (type(v) == "array" and i.len() == 2 and type(v.first()) == "string" and type(v.last()) == "content"), message: "Invalid voltage syntax")
let y = -0.3
let plus = ((v1, v2) => vector.add(vector.lerp(v1, v2, 0.25), (0, y, 0)), "east", "end")
let minus = ((v1, v2) => vector.add(vector.lerp(v1, v2, 0.75), (0, y, 0)), "start", "west")
content((rel: (0,y), to: ("east", 0.25, "end")), $+$)
content((rel: (0,y), to: ("start", 0.75, "west")), $-$)
content("annotation", v)
}
if i != none {
assert(type(i) == "content" or (type(i) == "array" and i.len() == 2 and type(i.first()) == "string" and type(i.last()) == "content"), message: "Invalid current label syntax")
let from = "east"
let to = "end"
let c = i
let y = 0.3
let d = 0deg
if type(i) == "array" {
let p = i.first()
c = i.last()
let ab = p.find(regex("\^|_"))
assert(ab != none, message: "'^' or '_' not found in `i`")
if ab == "_"{
y *= -1
}
let lr = p.find(regex("<|>"))
assert(lr != none, message: "'<' or '>' not found in `i`")
if p.position(lr) == 0 {
from = "west"
to = "start"
}
if lr == "<" {
d = 180deg
y *= -1
}
} else if component == "short" {
from = "west"
to = "east"
}
rotate(d)
node("currarrow", (from, 0.5, to))
content((rel: (0, y)), c)
rotate(-d)
}
if poles != none {
if type(poles) == "string" {
poles = (poles.first(), poles.last()).map(p => ("*": "circ", d: "diamondpole", s: "squarepole", o: "ocirc").at(p, default: none))
}
assert(type(poles) == "array" and poles.len() == 2)
if poles.first() != none {
node(poles.first(), "start")
}
if poles.last() != none {
node(poles.last(), "end")
}
}
})
move-to(end)
}