Skip to content

Commit 8c50511

Browse files
add arrows on LEDs
1 parent 943ed6e commit 8c50511

File tree

6 files changed

+71
-80
lines changed

6 files changed

+71
-80
lines changed

release.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,20 @@ def writefile(file, text):
2222
f.write(text)
2323

2424

25-
a = argparse.ArgumentParser("release.py")
25+
a = argparse.ArgumentParser()
2626
a.add_argument("version", help="release tag")
2727
args = a.parse_args()
2828

2929
# Patch new version into files
3030
pp_text = readfile("pyproject.toml")
3131
writefile("pyproject.toml",
32-
re.sub(r'version = "[\d.]+"', f'version = "{args.version}"', pp_text))
32+
re.sub(r'version = "[\d.]+"',
33+
f'version = "{args.version}"', pp_text))
3334

3435
init_text = readfile("schemascii/__init__.py")
3536
writefile("schemascii/__init__.py",
36-
re.sub(r'__version__ = "[\d.]+"', f'__version__ = "{args.version}"', init_text))
37+
re.sub(r'__version__ = "[\d.]+"',
38+
f'__version__ = "{args.version}"', init_text))
3739

3840

3941
cmd("python3 -m build --sdist")

schemascii/components_render.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from .utils import (Cbox, Terminal, BOMData, XML, Side,
66
polylinegon, id_text, make_text_point,
77
bunch_o_lines, deep_transform, make_plus, make_variable,
8-
sort_counterclockwise)
8+
sort_counterclockwise, light_arrows)
99
from .errors import TerminalsError, BOMError, UnsupportedComponentError
1010

1111
RENDERERS = {}
@@ -112,6 +112,9 @@ def resistor(
112112
component("R", "RV", "VR")(n_terminal(2)(no_ambiguous(resistor)))
113113

114114

115+
@component("C", "CV", "VC")
116+
@n_terminal(2)
117+
@no_ambiguous
115118
def capacitor(
116119
box: Cbox,
117120
terminals: list[Terminal],
@@ -135,9 +138,6 @@ def capacitor(
135138
box, bom_data, terminals, (("F", True), ("V", False)),
136139
text_pt, **options))
137140

138-
# Register it
139-
component("C", "CV", "VC")(n_terminal(2)(no_ambiguous(capacitor)))
140-
141141

142142
@component("B", "BT", "BAT")
143143
@polarized
@@ -184,7 +184,9 @@ def diode(
184184
deep_transform((-.3-.3j, .3-.3j), mid, angle)]
185185
triangle = deep_transform((-.3j, .3+.3j, -.3+.3j), mid, angle)
186186
text_pt = make_text_point(t1, t2, **options)
187-
return (id_text(box, bom_data, terminals, None, text_pt, **options)
187+
return ((light_arrows(mid, angle, True, **options)
188+
if box.id != "D" else "")
189+
+ id_text(box, bom_data, terminals, None, text_pt, **options)
188190
+ bunch_o_lines(lines, **options)
189191
+ polylinegon(triangle, True, **options))
190192

schemascii/utils.py

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
from enum import IntEnum
44
from math import pi
55
from cmath import phase, rect
6-
from types import GeneratorType
76
from typing import Callable
87
from .metric import format_metric_unit
8+
import re
99

1010
Cbox = namedtuple('Cbox', 'p1 p2 type id')
1111
BOMData = namedtuple('BOMData', 'type id data')
@@ -69,7 +69,7 @@ def merge_colinear(points: list[tuple[complex, complex]]) -> list[tuple[complex,
6969
return out
7070

7171

72-
def iterate_line(p1: complex, p2: complex, step: float = 1.) -> GeneratorType:
72+
def iterate_line(p1: complex, p2: complex, step: float = 1.):
7373
"Yields complex points along a line."
7474
vec = p2 - p1
7575
point = p1
@@ -96,19 +96,33 @@ def deep_transform(data, origin: complex, theta: float):
9696
"bad type to deep_transform(): " + type(data).__name__) from err
9797

9898

99+
def fix_number(n: float) -> str:
100+
"""If n is an integer, remove the trailing ".0".
101+
Otherwise round it to 2 digits."""
102+
if n.is_integer():
103+
return str(int(n))
104+
return str(round(n, 4))
105+
106+
99107
class XMLClass:
100108
def __getattr__(self, tag) -> Callable:
101109
def mk_tag(*contents, **attrs) -> str:
102110
out = f'<{tag} '
103111
for k, v in attrs.items():
104-
if v is not False:
105-
out += f'{k.removesuffix("_").replace("__", "-")}="{v}" '
112+
if v is False:
113+
continue
114+
if isinstance(v, float):
115+
v = fix_number(v)
116+
elif isinstance(v, str):
117+
v = re.sub(r"\d+(\.\d+)", lambda m: fix_number(float(m.group())), v)
118+
out += f'{k.removesuffix("_").replace("__", "-")}="{v}" '
106119
out = out.rstrip() + '>' + ''.join(contents)
107120
return out + f'</{tag}>'
108121
return mk_tag
109122

110123

111124
XML = XMLClass()
125+
del XMLClass
112126

113127

114128
def polylinegon(points: list[complex], is_polygon: bool = False, **options):
@@ -224,6 +238,16 @@ def make_plus(
224238
class_="plus")
225239

226240

241+
def arrow_points(p1: complex, p2: complex) -> list[tuple[complex, complex]]:
242+
"Return points to make an arrow from p1 pointing to p2."
243+
angle = phase(p2 - p1)
244+
tick_len = min(0.25, abs(p2 - p1))
245+
return [
246+
(p2, p1),
247+
(p2, p2 - rect(tick_len, angle + pi/5)),
248+
(p2, p2 - rect(tick_len, angle - pi/5))]
249+
250+
227251
def make_variable(
228252
center: complex,
229253
theta: float,
@@ -232,28 +256,33 @@ def make_variable(
232256
"Draw a 'variable' arrow across the component."
233257
if not is_variable:
234258
return ""
235-
theta = theta % pi
236-
return bunch_o_lines(deep_transform([
237-
(-.6+.5j, .75-.5j),
238-
(.75-.5j, .5-.55j),
239-
(.75-.5j, .7-.25j),
240-
], center, theta), **options)
241-
242-
243-
# def arrows(
244-
# center: complex,
245-
# theta: float,
246-
# out: bool,
247-
# **options):
248-
# """Draw arrows towards or away from the component
249-
# (i.e.light-emitting or light-dependent)."""
250-
# theta = theta % pi
251-
# if out:
252-
# return bunch_o_lines(deep_transform([
253-
# (-.5-.6j, .5-1.2j),
254-
# (.375-1.2j, .5-1.2j),
255-
# (.5-1.)
256-
# ], center, theta), **options)
259+
return bunch_o_lines(
260+
deep_transform(
261+
arrow_points(-1, 1),
262+
center,
263+
(theta % pi) + pi/4),
264+
**options)
265+
266+
267+
def light_arrows(
268+
center: complex,
269+
theta: float,
270+
out: bool,
271+
**options):
272+
"""Draw arrows towards or away from the component
273+
(i.e. light-emitting or light-dependent)."""
274+
a, b = 1j, .3+.3j
275+
if out:
276+
a, b = b, a
277+
return bunch_o_lines(
278+
deep_transform(
279+
arrow_points(a, b),
280+
center, theta - pi/2),
281+
**options) + bunch_o_lines(
282+
deep_transform(
283+
arrow_points(a - .5, b - .5),
284+
center, theta - pi/2),
285+
**options)
257286

258287

259288
def sort_counterclockwise(terminals: list[Terminal]) -> list[Terminal]:

test_data/test1.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-----LED1+-----
2+
LED1:red
3+
-----VR1###------
4+
VR1:10k

test_data/test_resistors.txt

Lines changed: 0 additions & 20 deletions
This file was deleted.

test_data/wires_test.txt

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)