8000 add jacks, revise part numbers behavior · dragoncoder047/schemascii@e85264c · GitHub
[go: up one dir, main page]

Skip to content

Commit e85264c

Browse files
add jacks, revise part numbers behavior
1 parent a823d6a commit e85264c

File tree

5 files changed

+79
-26
lines changed

5 files changed

+79
-26
lines changed

schemascii/__main__.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,28 @@ def cli_main():
1919
help="Output SVG file. (default input file plus .svg)")
2020
ap.add_argument("--padding",
2121
help="Amount of padding to add on the edges.",
22-
type=int)
22+
type=int,
23+
default=10)
2324
ap.add_argument("--scale",
2425
help="Scale at which to enlarge the entire diagram by.",
25-
type=int)
26+
type=int,
27+
default=15)
2628
ap.add_argument("--stroke_width",
2729
help="Width of the lines",
28-
type=int)
30+
type=int,
31+
default=2)
2932
ap.add_argument("--stroke",
30-
help="Color of the lines.")
33+
help="Color of the lines.",
34+
default="black")
3135
ap.add_argument("--label",
3236
help="Component label style "
3337
"(L=include label, V=include value, VL=both)",
34-
choices="L V VL".split())
38+
choices=["L", "V", "VL"],
39+
default="VL")
40+
ap.add_argument("--nolabels",
41+
help="Turns off labels on all components, "
42+
"except for part numbers on ICs.",
43+
action="store_true")
3544
args = ap.parse_args()
3645
if args.out_file is None:
3746
args.out_file = args.in_file + ".svg"
@@ -42,7 +51,7 @@ def cli_main():
4251
try:
4352
result_svg = render(args.in_file, text, **vars(args))
4453
except Error as err:
45-
print(err, file=sys.stderr)
54+
print(type(err).__name__ + ":", err, file=sys.stderr)
4655
sys.exit(1)
4756
if args.out_file == "-":
4857
print(result_svg)

schemascii/components_render.py

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@ def de_ambiguous(
5050
if len(bom_data) > 1:
5151
raise BOMError(
5252
f"Ambiguous BOM data for {box.type}{box.id}: {bom_data!r}")
53+
if not bom_data:
54+
bom_data = [BOMData(box.type, box.id, "")]
5355
return func(
5456
box,
5557
terminals,
56-
bom_data[0] if bom_data else None,
58+
bom_data[0],
5759
**options)
5860
return de_ambiguous
5961

@@ -86,7 +88,7 @@ def sort_terminals(
8688
def resistor(
8789
box: Cbox,
8890
terminals: list[Terminal],
89-
bom_data: BOMData | None,
91+
bom_data: BOMData,
9092
**options):
9193
"Draw a resistor"
9294
t1, t2 = terminals[0].pt, terminals[1].pt
@@ -114,7 +116,7 @@ def resistor(
114116
def capacitor(
115117
box: Cbox,
116118
terminals: list[Terminal],
117-
bom_data: BOMData | None,
119+
bom_data: BOMData,
118120
**options):
119121
"Draw a capacitor"
120122
t1, t2 = terminals[0].pt, terminals[1].pt
@@ -141,7 +143,7 @@ def capacitor(
141143
def battery(
142144
box: Cbox,
143145
terminals: list[Terminal],
144-
bom_data: BOMData | None,
146+
bom_data: BOMData,
145147
**options):
146148
"Draw a battery cell"
147149
t1, t2 = terminals[0].pt, terminals[1].pt
@@ -168,7 +170,7 @@ def battery(
168170
def diode(
169171
box: Cbox,
170172
terminals: list[Terminal],
171-
bom_data: BOMData | None,
173+
bom_data: BOMData,
172174
**options):
173175
"Draw a diode or LED"
174176
t1, t2 = terminals[0].pt, terminals[1].pt
@@ -198,13 +200,14 @@ def diode(
198200
def integrated_circuit(
199201
box: Cbox,
200202
terminals: list[Terminal],
201-
bom_data: BOMData | None,
203+
bom_data: BOMData,
202204
**options):
203205
"Draw an IC"
204206
label_style = options["label"]
205207
scale = options["scale"]
206208
sz = (box.p2 - box.p1) * scale
207209
mid = (box.p2 + box.p1) * scale / 2
210+
part_num, *pin_labels = map(str.strip, bom_data.data.split(","))
208211
out = XML.rect(
209212
x=box.p1.real * scale,
210213
y=box.p1.imag * scale,
@@ -218,20 +221,59 @@ def integrated_circuit(
218221
term.pt,
219222
term.pt + rect(1, SIDE_TO_ANGLE_MAP[term.side])
220223
)], **options)
221-
out += XML.text(
222-
(XML.tspan(f"{box.type}{box.id}", class_="cmp-id")
223-
* bool("L" in label_style)),
224-
" " * (bool(bom_data.data) and "L" in label_style),
225-
XML.tspan(bom_data.data, class_="part-num") * bool("V" in label_style),
226-
x=mid.real,
227-
y=mid.imag,
228-
text__anchor="middle",
229-
font__size=options["scale"],
230-
fill=options["stroke"])
224+
if "V" in label_style:
225+
out += XML.text(
226+
XML.tspan(part_num, class_="part-num"),
227+
x=mid.real,
228+
y=mid.imag,
229+
text__anchor="middle",
230+
font__size=options["scale"],
231+
fill=options["stroke"])
232+
mid -= 1j * scale
233+
if "L" in label_style and not options["nolabels"]:
234+
out += XML.text(
235+
XML.tspan(f"{box.type}{box.id}", class_="cmp-id"),
236+
x=mid.real,
237+
y=mid.imag,
238+
text__anchor="middle",
239+
font__size=options["scale"],
240+
fill=options["stroke"])
231241
warn("ICs are not fully implemented yet.")
232242
return out
233243

234-
# code for drawing
244+
245+
@component("J", "P")
246+
@n_terminal(1)
247+
@no_ambiguous
248+
def jack(
249+
box: Cbox,
250+
terminals: list[Terminal],
251+
bom_data: BOMData,
252+
**options):
253+
"Draw a jack connector or plug"
254+
scale = options["scale"]
255+
sc_t1 = terminals[0].pt * scale
256+
sc_t2 = sc_t1 + rect(scale, SIDE_TO_ANGLE_MAP[terminals[0].side])
257+
sc_text_pt = sc_t2 + rect(scale * 2, SIDE_TO_ANGLE_MAP[terminals[0].side])
258+
return (
259+
XML.line(
260+
x1=sc_t1.real,
261+
x2=sc_t2.real,
262+
y1=sc_t1.imag,
263+
y2=sc_t2.imag,
264+
stroke__width=options["stroke_width"],
265+
stroke=options["stroke"])
266+
+ XML.circle(
267+
cx=sc_t2.real,
268+
cy=sc_t2.imag,
269+
r=scale / 4,
270+
stroke__width=options["stroke_width"],
271+
stroke=options["stroke"],
272+
fill="none")
273+
+ id_text(box, bom_data, terminals, None, sc_text_pt, **options))
274+
275+
276+
# code for drawing stuff
235277
# https://github.com/pfalstad/circuitjs1/tree/master/src/com/lushprojects/circuitjs1/client
236278
# https://github.com/KenKundert/svg_schematic/blob/0abb5dc/svg_schematic.py
237279
# https://yqnn.github.io/svg-path-editor/

schemascii/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ def id_text(
147147
unit: str | list[str] | None,
148148
point: complex | None = None,
149149
**options):
150-
"Format the component ID and value around the point"
150+
"Format the component ID and value around the point."
151+
if options["nolabels"]:
152+
return ""
151153
label_style = options["label"]
152154
if point is None:
153155
point = sum(t.pt for t in terminals) / len(terminals)

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

test_data/test_charge_pump.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
| | | |
33
| R1 .~~~. |
44
| | : :-*
5-
| o-----: :---+C2--*--D2+--*----------J1
5+
| *-----: :---+C2--*--D2+--*----------J1
66
| | :U1 : | |
77
| R2 :555: | |
88
| | *-: :-* | |

0 commit comments

Comments
 (0)
0