Initial commit

This commit is contained in:
Qihuanye
2026-04-27 14:51:49 +08:00
commit dbbfc2867d
69 changed files with 68307 additions and 0 deletions
+308
View File
@@ -0,0 +1,308 @@
import os
os.environ.setdefault("MPLCONFIGDIR", "/tmp/matplotlib")
import schemdraw
import schemdraw.elements as elm
from schemdraw.segments import SegmentText, SegmentCircle
FONT_NAME = "LXGW WenKai"
BG_COLOR = "white"
# 这里改成你自己的输出目录
OUTDIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "image"))
os.makedirs(OUTDIR, exist_ok=True)
def make_drawing():
d = schemdraw.Drawing(show=False)
d.config(
unit=1.0,
fontsize=16,
font=FONT_NAME,
lw=2.2,
margin=0.18,
bgcolor=BG_COLOR,
)
return d
def add_text(
drawing,
position,
content,
*,
rotation=0,
align=("center", "center"),
fontsize=16,
):
element = elm.Element().at(position).anchor("center").hold()
element.anchors["center"] = (0, 0)
element.segments.append(
SegmentText(
(0, 0),
content,
rotation=rotation,
align=align,
fontsize=fontsize,
font=FONT_NAME,
)
)
drawing.add(element)
def add_dot(drawing, position, *, open_dot=False, radius=0.085):
drawing.add(elm.Dot(open=open_dot, radius=radius).at(position).hold())
def wire(drawing, start, end):
drawing.add(elm.Line().at(start).to(end).hold())
def save_drawing(drawing, filename):
drawing.save(os.path.join(OUTDIR, filename), transparent=False)
# -------------------------
# 图 (a)
# -------------------------
def make_circuit_a():
d = make_drawing()
x_left = 0.9
x_meter = 3.0
x_node = 5.7
x_res_top = 7.2
x_right = 10.1
y_top = 4.2
y_bottom = 1.0
y_mid = 2.55
# 左侧电池
batt = d.add(elm.BatteryCell().at((x_left, y_bottom)).up(2.1).hold())
wire(d, batt.end, (x_left, y_top))
wire(d, batt.start, (x_left, y_bottom))
# 上边:A表 + 节点 + 1k
wire(d, (x_left, y_top), (x_meter, y_top))
am = d.add(elm.MeterA().at((x_meter, y_top)).right().hold())
wire(d, am.end, (x_node, y_top))
add_dot(d, (x_node, y_top))
wire(d, (x_node, y_top), (x_res_top, y_top))
r1 = d.add(elm.ResistorIEC().at((x_res_top, y_top)).right(1.5).hold())
wire(d, r1.end, (x_right, y_top))
# 右侧竖直 2k 电阻
r2 = d.add(elm.ResistorIEC().at((x_right, y_top)).down(1.9).hold())
wire(d, r2.end, (x_right, y_bottom))
# 中间 V 表支路
wire(d, (x_node, y_top), (x_node, 3.45))
vm = d.add(elm.MeterV().at((x_node, 3.45)).down().hold())
wire(d, vm.end, (x_node, y_bottom))
add_dot(d, (x_node, y_bottom))
# 底边闭合
wire(d, (x_left, y_bottom), (x_node, y_bottom))
wire(d, (x_node, y_bottom), (x_right, y_bottom))
# 文字
add_text(d, (8.0, 4.75), "1kΩ", fontsize=16)
add_text(d, (10.75, 2.45), "2kΩ", fontsize=16)
add_text(d, (5.4, 0.25), "(a)", fontsize=18)
save_drawing(d, "circuit_a.svg")
# -------------------------
# 图 (b)
# -------------------------
def make_circuit_b():
d = make_drawing()
x_left = 0.9
x_node = 3.4
x_meter = 5.0
x_res_top = 7.2
x_right = 10.1
y_top = 4.2
y_bottom = 1.0
# 左侧电池
batt = d.add(elm.BatteryCell().at((x_left, y_bottom)).up(2.1).hold())
wire(d, batt.end, (x_left, y_top))
wire(d, batt.start, (x_left, y_bottom))
# 上边左节点
wire(d, (x_left, y_top), (x_node, y_top))
add_dot(d, (x_node, y_top))
# 中间 V 表
wire(d, (x_node, y_top), (x_node, 3.45))
vm = d.add(elm.MeterV().at((x_node, 3.45)).down().hold())
wire(d, vm.end, (x_node, y_bottom))
add_dot(d, (x_node, y_bottom))
# 上边右支路:A + 1k
wire(d, (x_node, y_top), (x_meter, y_top))
am = d.add(elm.MeterA().at((x_meter, y_top)).right().hold())
wire(d, am.end, (x_res_top, y_top))
r1 = d.add(elm.ResistorIEC().at((x_res_top, y_top)).right(1.5).hold())
wire(d, r1.end, (x_right, y_top))
# 右侧竖直 2k
r2 = d.add(elm.ResistorIEC().at((x_right, y_top)).down(1.9).hold())
wire(d, r2.end, (x_right, y_bottom))
# 下边闭合
wire(d, (x_left, y_bottom), (x_node, y_bottom))
wire(d, (x_node, y_bottom), (x_right, y_bottom))
# 文字
add_text(d, (8.0, 4.75), "1kΩ", fontsize=16)
add_text(d, (9.1, 3.0), "2kΩ", fontsize=16)
add_text(d, (5.6, 0.25), "(b)", fontsize=18)
save_drawing(d, "circuit_b.svg")
# -------------------------
# 图 (c)
# -------------------------
def make_circuit_c():
d = make_drawing()
x_left = 1.2
x_v = 3.2
x_node = 6.0
x_right = 8.8
y_top = 4.2
y_bottom = 1.0
# 左侧电压源
# 左侧理想电压源:圆圈 + 竖线
source_center_y = 2.45
source_radius = 0.55
# 上下导线
wire(d, (x_left, y_top), (x_left, source_center_y + source_radius))
wire(d, (x_left, source_center_y - source_radius), (x_left, y_bottom))
# 圆圈
src = elm.Element().at((x_left, source_center_y)).anchor("center").hold()
src.anchors["center"] = (0, 0)
src.segments.append(SegmentCircle((0, 0), radius=source_radius))
d.add(src)
# 圆圈中间竖线
wire(d, (x_left, source_center_y - 0.5), (x_left, source_center_y + 0.5))
# 顶部 V 表
wire(d, (x_left, y_top), (x_v, y_top))
vm = d.add(elm.MeterV().at((x_v, y_top)).right().hold())
wire(d, vm.end, (x_node, y_top))
add_dot(d, (x_node, y_top))
# 右侧电阻支路
wire(d, (x_node, y_top), (x_right, y_top))
r = d.add(elm.ResistorIEC().at((x_right, y_top)).down(1.9).hold())
wire(d, r.end, (x_right, y_bottom))
# 中间开关支路
wire(d, (x_node, y_top), (x_node, 3.2))
add_dot(d, (x_node, 3.2), open_dot=True, radius=0.11)
add_dot(d, (x_node, 2.1), open_dot=True, radius=0.11)
d.add(elm.Line().at((x_node - 0.05, 2.18)).to((x_node + 0.35, 2.95)).hold())
wire(d, (x_node, 2.1), (x_node, y_bottom))
add_dot(d, (x_node, y_bottom))
# 下边闭合
wire(d, (x_left, y_bottom), (x_node, y_bottom))
wire(d, (x_node, y_bottom), (x_right, y_bottom))
# 文字
add_text(d, (0.25, 2.25), "Uₛ", fontsize=18)
add_text(d, (6.65, 2.6), "K", fontsize=18)
add_text(d, (9.5, 2.55), "R", fontsize=18)
save_drawing(d, "circuit_c.svg")
# -------------------------
# 图 (d)
# -------------------------
def make_circuit_d():
d = make_drawing()
x_left = 1.0
x_mid = 4.2
x_right = 7.2
y_top = 6.5
y_bottom = 0.7
# 外框
wire(d, (x_left, y_top), (x_mid, y_top))
add_dot(d, (x_mid, y_top))
wire(d, (x_mid, y_top), (x_right, y_top))
wire(d, (x_left, y_bottom), (x_mid, y_bottom))
add_dot(d, (x_mid, y_bottom))
wire(d, (x_mid, y_bottom), (x_right, y_bottom))
# 左支路:电流源
# 左支路:理想电流源(圆圈 + 竖线)
source_center_y = 4.2
source_radius = 0.55
wire(d, (x_left, y_top), (x_left, source_center_y + source_radius))
wire(d, (x_left, source_center_y - source_radius), (x_left, y_bottom))
# 圆圈
src = elm.Element().at((x_left, source_center_y)).anchor("center").hold()
src.anchors["center"] = (0, 0)
src.segments.append(SegmentCircle((0, 0), radius=source_radius))
d.add(src)
# 圆圈中间竖线
wire(d, (x_left, source_center_y - 0.5), (x_left, source_center_y + 0.5))
# 标注
add_text(d, (x_left + 0.7, source_center_y - 0.6), "Iₛ", fontsize=18)
# 右支路:A 表
wire(d, (x_right, y_top), (x_right, 5.0))
am = d.add(elm.MeterA().at((x_right, 5.0)).down().hold())
wire(d, am.end, (x_right, y_bottom))
# 中间支路:开关 + 可变电阻
wire(d, (x_mid, y_top), (x_mid, 5.2))
add_dot(d, (x_mid, 5.2), open_dot=True, radius=0.11)
add_dot(d, (x_mid, 4.25), open_dot=True, radius=0.11)
d.add(elm.Line().at((x_mid - 0.05, 4.35)).to((x_mid + 0.4, 5.0)).hold())
wire(d, (x_mid, 4.25), (x_mid, 3.6))
r = d.add(elm.ResistorIEC().at((x_mid, 3.6)).down(1.5).hold())
wire(d, r.end, (x_mid, y_bottom))
# 文字
add_text(d, (4.85, 4.85), "K", fontsize=18)
add_text(d, (4.8, 2.05), "R", fontsize=18)
save_drawing(d, "circuit_d.svg")
def main():
make_circuit_a()
make_circuit_b()
make_circuit_c()
make_circuit_d()
print("Created: circuit_a.svg, circuit_b.svg, circuit_c.svg, circuit_d.svg")
if __name__ == "__main__":
main()