Skip to content

Commit d63d435

Browse files
implement batteries and IC's
good god I touched almost every file!!
1 parent 831ed09 commit d63d435

10 files changed

+110
-39
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"Cbox",
44
"polylinegon",
55
"rendec",
6-
"schemascii"
6+
"schemascii",
7+
"tspan"
78
]
89
}

designators.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
| A, ASSY | Separable assembly or sub-assembly ||
66
| AE | Aerial, antenna ||
77
| AT | Attenuator or isolator ||
8-
| B, BT | Battery ||
8+
| B, BT, BAT | Battery | Yes |
99
| BR | Bridge rectifier ||
1010
| C | Capacitor | Yes |
1111
| CN | Connector ||

format.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ Crossed: Joined: Corner: Corner: Crossed: Crossed:
1111
| | | |
1212
```
1313

14-
Small components are notated with their reference designator: one or more uppercase letters followed by an ID number. They are always written horizontally even if the terminals are on the top and bottom. IDs are allowed to be duplicated and result in the same component values. Components can be padded on either side with `#`'s to make them bigger.
14+
Small components are notated with their reference designator: one or more uppercase letters followed by an ID, which can either be a number, or a period followed by some text. They are always written horizontally even if the terminals are on the top and bottom. IDs are allowed to be duplicated and result in the same component values. Components can be padded on either side with `#`'s to make them bigger.
1515

1616
Examples:
1717

1818
* `C33`
1919
* `Q1001`
20-
* `L51`
20+
* `L.Coil`
2121
* `F3#`
2222
* `D7#####`
2323
* `U1#####`
24-
* `####R2####`
24+
* `####R.Heater####`
2525

2626
Components are able to accept "flags", which are other punctuation characters and lowercase letters touching them.
2727

schemascii/components.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from grid import Grid
33
from utils import Cbox, BOMData
44

5-
SMALL_COMPONENT_OR_BOM = re.compile(r'#*([A-Z]+)(\d+)(:[^\s]+)?#*')
5+
SMALL_COMPONENT_OR_BOM = re.compile(r'#*([A-Z]+)(\d+|\.\w+)(:[^\s]+)?#*')
66

77

88
def find_small(grid: Grid) -> tuple[list[Cbox], list[BOMData]]:
@@ -14,11 +14,11 @@ def find_small(grid: Grid) -> tuple[list[Cbox], list[BOMData]]:
1414
for m in SMALL_COMPONENT_OR_BOM.finditer(line):
1515
if m.group(3):
1616
boms.append(BOMData(m.group(1),
17-
int(m.group(2)), m.group(3)[1:]))
17+
m.group(2), m.group(3)[1:]))
1818
else:
1919
components.append(Cbox(complex(m.start(), i), complex(m.end(),
2020
i),
21-
m.group(1), int(m.group(2))))
21+
m.group(1), m.group(2)))
2222
for z in range(*m.span(0)):
2323
grid.setmask(complex(z, i))
2424
return components, boms
@@ -36,8 +36,7 @@ def find_big(grid: Grid) -> tuple[list[Cbox], list[BOMData]]:
3636
for i, line in enumerate(grid.lines):
3737
if m1 := TOP_OF_BOX.search(line):
3838
tb = m1.group()
39-
x1 = m1.start()
40-
x2 = m1.end()
39+
x1, x2 = m1.span()
4140
y1 = i
4241
y2 = None
4342
for j, l in enumerate(grid.lines):
@@ -73,7 +72,7 @@ def find_big(grid: Grid) -> tuple[list[Cbox], list[BOMData]]:
7372
merd = results[0]
7473
boxes.append(
7574
Cbox(complex(x1, y1),
76-
complex(x2, y2),
75+
complex(x2 - 1, y2),
7776
merd.type,
7877
merd.id))
7978
boms.extend(resb)
@@ -93,3 +92,12 @@ def find_all(grid: Grid) -> tuple[list[Cbox], list[BOMData]]:
9392
b1, l1 = find_big(grid)
9493
b2, l2 = find_small(grid)
9594
return b1+b2, l1+l2
95+
96+
97+
if __name__ == '__main__':
98+
grid = Grid("../test_data/test_resistors.txt")
99+
bbb, _ = find_all(grid)
100+
all_pts = []
101+
for box in bbb:
102+
all_pts.extend([box.p1, box.p2])
103+
grid.spark(*all_pts)

schemascii/components_render.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,33 @@ def capacitor(
131131
+ make_plus(terminals, mid, angle, **kwargs))
132132

133133

134+
@component("B", "BT", "BAT")
135+
@polarized
136+
@no_ambiguous
137+
def battery(
138+
box: Cbox,
139+
terminals: list[Terminal],
140+
bom_data: BOMData | None,
141+
**kwargs):
142+
"Draw a battery cell"
143+
t1, t2 = terminals[0].pt, terminals[1].pt
144+
mid = (t1 + t2) / 2
145+
angle = phase(t1 - t2)
146+
lines = [
147+
(t1, mid + rect(.5, angle)),
148+
(t2, mid + rect(-.5, angle))] + deep_transform([
149+
(complex(.5, .5), complex(-.5, .5)),
150+
(complex(.25, .16), complex(-.25, .16)),
151+
(complex(.5, -.16), complex(-.5, -.16)),
152+
(complex(.25, -.5), complex(-.25, -.5)),
153+
], mid, angle)
154+
text_pt = make_text_point(t1, t2, **kwargs)
155+
return (id_text(
156+
box, bom_data, terminals, (("V", False), ("Ah", False)),
157+
text_pt, **kwargs)
158+
+ bunch_o_lines(lines, **kwargs))
159+
160+
134161
@component("D", "LED", "CR")
135162
@polarized
136163
@no_ambiguous
@@ -153,6 +180,42 @@ def diode(
153180
+ bunch_o_lines(lines, **kwargs)
154181
+ polylinegon(triangle, True, **kwargs))
155182

183+
184+
@component("U", "IC")
185+
@no_ambiguous
186+
def integrated_circuit(
187+
box: Cbox,
188+
terminals: list[Terminal],
189+
bom_data: BOMData | None,
190+
**kwargs):
191+
"Draw an IC"
192+
scale = kwargs.get("scale", 1)
193+
sz = (box.p2 - box.p1 + 2) * scale
194+
mid = (box.p2 + box.p1) * scale / 2
195+
out = XML.rect(
196+
x=box.p1.real * scale - scale,
197+
y=box.p1.imag * scale,
198+
width=sz.real,
199+
height=sz.imag,
200+
stroke__width=kwargs.get("stroke_width", 1),
201+
stroke=kwargs.get("stroke", "black"))
202+
for term in terminals:
203+
out += bunch_o_lines([(
204+
term.pt,
205+
term.pt + rect(1, term.side * pi / 2)
206+
)], **kwargs)
207+
out += XML.text(
208+
XML.tspan(f"{box.type}{box.id}", class_="cmp-id"),
209+
" " * bool(bom_data.data),
210+
XML.tspan(bom_data.data, class_="part-num"),
211+
x=mid.real,
212+
y=mid.imag,
213+
text__anchor="middle",
214+
font__size=kwargs.get("scale", 1),
215+
fill=kwargs.get("stroke", "black"))
216+
print("IC's in progress...")
217+
return out
218+
156219
# code for drawing
157220
# https://github.com/pfalstad/circuitjs1/tree/master/src/com/lushprojects/circuitjs1/client
158221
# https://github.com/KenKundert/svg_schematic/blob/0abb5dc/svg_schematic.py

schemascii/edgemarks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ def inner_over_edges(func: FunctionType):
1010
out = []
1111
for p, s in chain(
1212
# Top side
13-
((complex(xx, int(box.p2.imag) - 1), Side.TOP)
13+
((complex(xx, int(box.p1.imag) - 1), Side.TOP)
1414
for xx in range(int(box.p1.real), int(box.p2.real) + 1)),
1515
# Right side
16-
((complex(int(box.p2.real) + 0, yy), Side.RIGHT)
16+
((complex(int(box.p2.real) + 1, yy), Side.RIGHT)
1717
for yy in range(int(box.p1.imag), int(box.p2.imag) + 1)),
1818
# Bottom side
1919
((complex(xx, int(box.p2.imag) + 1), Side.BOTTOM)

schemascii/utils.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
class Side(IntEnum):
1818
"Which edge the flag was found on."
19-
TOP = 0
20-
RIGHT = 1
21-
BOTTOM = 2
22-
LEFT = 3
19+
RIGHT = 0
20+
TOP = 1
21+
LEFT = 2
22+
BOTTOM = 3
2323

2424

2525
def colinear(points: list[complex]) -> bool:
@@ -80,12 +80,6 @@ def iterate_line(p1: complex, p2: complex, step: float = 1.) -> GeneratorType:
8080
yield point
8181

8282

83-
def extend(p1: complex, p2: complex) -> complex:
84-
"""Extends the line from p1 to p2 by 1 in the direction of p2,
85-
returns the modified p2."""
86-
return p2 + rect(1, phase(p2 - p1))
87-
88-
8983
def deep_transform(data, origin: complex, theta: float):
9084
"""Transform the point first by translating by origin,
9185
then rotating by theta."""
@@ -206,5 +200,5 @@ def make_plus(
206200
return XML.g(
207201
bunch_o_lines(deep_transform(deep_transform(
208202
[(.125, -.125), (.125j, -.125j)], 0, theta),
209-
center + deep_transform(.33+.66j, 0, theta), 0), **kwargs),
203+
center + deep_transform(.33+.75j, 0, theta), 0), **kwargs),
210204
class_="plus")

schemascii/wires.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from math import pi
33
from cmath import rect
44
from grid import Grid
5-
from utils import iterate_line, extend, merge_colinear, XML
5+
from utils import iterate_line, merge_colinear, XML
66

77
# cSpell:ignore dydx
88

@@ -137,7 +137,6 @@ def get_wires(grid: Grid, **options) -> str:
137137

138138

139139
if __name__ == '__main__':
140-
with open('../test_data/test_resistors.txt') as f:
141-
xg = Grid('foo.txt', f.read())
142-
print(get_wires(xg, scale=20))
143-
print(xg)
140+
xg = Grid('../test_data/test_resistors.txt')
141+
print(get_wires(xg, scale=20))
142+
print(xg)

schemascii_example.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ svg.schemascii :is(.wire, .component):hover {
1515
--sch-color: lime;
1616
}
1717

18-
svg.schemascii .component :is(polyline, path, line, polygon) {
18+
svg.schemascii .component :is(polyline, path, line, polygon, rect) {
1919
stroke: var(--sch-color, red);
2020
stroke-width: calc(0.15 * var(--sch-scale, 1));
2121
stroke-linecap: round;

test_data/test_resistors.txt

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1-
*--------------------------------*
2-
| |
3-
*---+D1--+LED1--* D1:2N7001 |
4-
| LED1:red |
5-
*--------* |
1+
*-------------------------------#B.Foo
2+
| +
3+
*---+D1--+LED1--* D1:2N7001 +
4+
| LED1:red B1
5+
*--------* |B1:5V,100m
66
| |
7-
R4 R4:.3k *-----------*
7+
R4 R4:.3k *-----------* C3:2200u,2.5
88
| |
9-
*----R2#####--|---* R2:0.01K,0.5K
9+
*---R.Heater--|---* R.Heater:0.0081K,0.5K
1010
| | | R3:2000
1111
*----R3-------|---*-------*
1212
| |
13-
*-----C3+---*
14-
C3:2200u,2.5
13+
*--------------*-----C3+---*
14+
a
15+
.~~~.
16+
---d:U1 :b---
17+
.~~~.
18+
c
19+
|
20+
U1:RAND04

0 commit comments

Comments
 (0)