rrplot 1.3.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
rrplot/hvac.py ADDED
@@ -0,0 +1,305 @@
1
+ # -*- coding: utf-8 -*-
2
+ """HVAC 符号族:references/hvacsvg/symbols 全部 17 个 SVG 转成的代码图元。
3
+
4
+ 每个符号是一个工厂,参数语义与 load_svg_part 一致::
5
+
6
+ from rrplot import hvac
7
+
8
+ tank = hvac.thermal_tank(scale=1.5, bridge=44,
9
+ moves={"right": (160, 72)}, label_dy=-97)
10
+ node = d.add(tank, "蓄热水箱", loc=(770, 375))
11
+
12
+ 工厂参数:
13
+ scale 整体缩放(端口偏移、线宽、标签位置一并缩放)
14
+ moves {连接点 id: (x, y)} 覆盖连接点在 viewBox 里的坐标
15
+ bridge 从每个连接点沿边缘法向朝内补一段引管(符号单位)
16
+ label_dy 标签相对锚点的竖直偏移;None 画在符号下方,0 压在符号中心
17
+
18
+ 图形全部写在 160×100 的 viewBox 坐标里(中心 80,50),绘制时统一乘
19
+ scale 再平移到锚点;stroke-width 换算与 svgparts 同规则。同一个 SVG
20
+ <path> 的多条子路径画在同一个 PathPatch 里 —— Agg 对复合路径与分离
21
+ 路径的描边光栅化不同,按原分组才能与 load_svg_part 逐像素一致。
22
+ SVG 原文件只作形状参考,形状以本模块为准。
23
+ """
24
+ from matplotlib.patches import Circle, Ellipse, FancyBboxPatch, PathPatch, Rectangle
25
+ from matplotlib.path import Path
26
+
27
+ from ._primitives import line, txt
28
+ from .part import Part
29
+ from .style import LINE, LW_PIPE
30
+
31
+ VB_W, VB_H = 160.0, 100.0
32
+
33
+ RED, GREEN, BLUE, PURPLE = "#e53935", "#2e9e58", "#1677ff", "#7b3ff2"
34
+
35
+
36
+ # =========================
37
+ # viewBox 画布辅助
38
+ # =========================
39
+ def _lw(sw, s):
40
+ """SVG stroke-width → matplotlib linewidth(与 svgparts 同规则)。"""
41
+ return sw * max(s, 0.5)
42
+
43
+
44
+ def _rect(ax, X, Y, s, x, y, w, h, fc, ec, sw, rx=0):
45
+ lw = _lw(sw, s)
46
+ x0, y0, ww, hh = X(x), Y(y), w * s, h * s
47
+ if rx:
48
+ r = rx * s
49
+ ax.add_patch(FancyBboxPatch((x0 + r, y0 + r), ww - 2 * r, hh - 2 * r,
50
+ boxstyle=f"round,pad={r}", facecolor=fc,
51
+ edgecolor=ec, linewidth=lw, zorder=1))
52
+ else:
53
+ ax.add_patch(Rectangle((x0, y0), ww, hh, facecolor=fc, edgecolor=ec,
54
+ linewidth=lw, zorder=1))
55
+
56
+
57
+ def _circle(ax, X, Y, s, cx, cy, r, fc, ec, sw):
58
+ ax.add_patch(Circle((X(cx), Y(cy)), r * s, facecolor=fc, edgecolor=ec,
59
+ linewidth=_lw(sw, s), zorder=1))
60
+
61
+
62
+ def _ellipse(ax, X, Y, s, cx, cy, rx, ry, fc, ec, sw):
63
+ ax.add_patch(Ellipse((X(cx), Y(cy)), 2 * rx * s, 2 * ry * s,
64
+ facecolor=fc, edgecolor=ec,
65
+ linewidth=_lw(sw, s), zorder=1))
66
+
67
+
68
+ def _pathpatch(ax, X, Y, verts, codes, color, sw, s, fill="none"):
69
+ pts = [(X(a), Y(b)) for a, b in verts]
70
+ ax.add_patch(PathPatch(Path(pts, codes), facecolor=fill, edgecolor=color,
71
+ linewidth=_lw(sw, s), joinstyle="round",
72
+ capstyle="round", zorder=1))
73
+
74
+
75
+ def _lines(ax, X, Y, s, subs, color, sw):
76
+ """若干条开放折线画进同一个 PathPatch(对应 SVG 一个 <path>)。"""
77
+ verts, codes = [], []
78
+ for pts in subs:
79
+ verts += list(pts)
80
+ codes += [Path.MOVETO] + [Path.LINETO] * (len(pts) - 1)
81
+ _pathpatch(ax, X, Y, verts, codes, color, sw, s)
82
+
83
+
84
+ def _stroke(ax, X, Y, s, pts, color, sw):
85
+ _lines(ax, X, Y, s, [pts], color, sw)
86
+
87
+
88
+ def _fillpolys(ax, X, Y, s, polys, color, sw):
89
+ """若干闭合多边形画进同一个 PathPatch(填充 = 描边同色)。"""
90
+ verts, codes = [], []
91
+ for pts in polys:
92
+ verts += list(pts) + [pts[0]]
93
+ codes += ([Path.MOVETO] + [Path.LINETO] * (len(pts) - 1)
94
+ + [Path.CLOSEPOLY])
95
+ _pathpatch(ax, X, Y, verts, codes, color, sw, s, fill=color)
96
+
97
+
98
+ # =========================
99
+ # 符号本体(viewBox 坐标)
100
+ # =========================
101
+ def _heat_pump(ax, X, Y, s): # 01_空气能主机
102
+ _rect(ax, X, Y, s, 22, 12, 116, 76, "#eef2f5", "#444444", 2, rx=3)
103
+ for cy in (34, 65):
104
+ _rect(ax, X, Y, s, 35, cy - 12, 90, 24, "#dfe5ea", "#777777", 1)
105
+ _circle(ax, X, Y, s, 80, cy, 16, "#c8d0d8", "#333333", 2)
106
+ _lines(ax, X, Y, s,
107
+ [[(80, cy - 16), (80, cy + 16)],
108
+ [(64, cy), (96, cy)],
109
+ [(69, cy - 11), (91, cy + 11)],
110
+ [(91, cy - 11), (69, cy + 11)]], "#333333", 1.5)
111
+
112
+
113
+ def _water_pump(ax, X, Y, s): # 02_水泵
114
+ _stroke(ax, X, Y, s, [(18, 50), (45, 50)], BLUE, 7)
115
+ _circle(ax, X, Y, s, 65, 50, 20, BLUE, "#124f9d", 2)
116
+ _pathpatch(ax, X, Y,
117
+ [(54, 39), (69, 43), (71, 56), (62, 63)],
118
+ [Path.MOVETO, Path.CURVE4, Path.CURVE4, Path.CURVE4],
119
+ "#ffffff", 3, s)
120
+ _rect(ax, X, Y, s, 83, 37, 28, 26, "#287ee0", "#124f9d", 2, rx=3)
121
+ _stroke(ax, X, Y, s, [(111, 50), (139, 50)], BLUE, 7)
122
+
123
+
124
+ def _stop_valve(color):
125
+ """两段短管 + 对顶三角 + 白心圆(03 防冻阀 / 07 排水阀 / 08 补水阀)。"""
126
+ def body(ax, X, Y, s):
127
+ _lines(ax, X, Y, s, [[(10, 50), (55, 50)], [(105, 50), (150, 50)]],
128
+ color, 5)
129
+ _fillpolys(ax, X, Y, s, [[(55, 36), (80, 50), (55, 64)],
130
+ [(105, 36), (80, 50), (105, 64)]], color, 1)
131
+ _circle(ax, X, Y, s, 80, 50, 7, "#ffffff", color, 3)
132
+ return body
133
+
134
+
135
+ def _thermal_tank(ax, X, Y, s): # 04_蓄热水箱
136
+ _ellipse(ax, X, Y, s, 80, 14, 38, 9, "#f5f7f8", "#555555", 2)
137
+ _rect(ax, X, Y, s, 42, 14, 76, 70, "#f5f7f8", "#555555", 2)
138
+ _ellipse(ax, X, Y, s, 80, 84, 38, 9, "#edf1f3", "#555555", 2)
139
+ _ellipse(ax, X, Y, s, 80, 14, 15, 4, "none", "#777777", 1)
140
+ _lines(ax, X, Y, s, [[(25, 32), (42, 32)], [(118, 32), (135, 32)],
141
+ [(25, 70), (42, 70)], [(118, 70), (135, 70)]],
142
+ RED, 4)
143
+ _stroke(ax, X, Y, s, [(80, 48), (80, 68)], "#ff8c00", 4)
144
+ _circle(ax, X, Y, s, 80, 44, 5, "#ff8c00", "black", 1)
145
+
146
+
147
+ def _electric_heater(ax, X, Y, s): # 05_电加热
148
+ _rect(ax, X, Y, s, 55, 16, 50, 68, "#f7f7f7", "#555555", 2, rx=8)
149
+ verts = [(80, 27), (80, 69)]
150
+ codes = [Path.MOVETO, Path.LINETO]
151
+ for y0 in (40, 52, 64):
152
+ verts += [(67, y0), (75, y0 - 7), (85, y0 - 7), (93, y0)]
153
+ codes += [Path.MOVETO] + [Path.CURVE4] * 3
154
+ _pathpatch(ax, X, Y, verts, codes, "#e66b00", 3, s)
155
+ _circle(ax, X, Y, s, 80, 50, 4, "#ff9d00", "black", 1)
156
+
157
+
158
+ def _safety_valve(ax, X, Y, s): # 06_安全阀
159
+ _stroke(ax, X, Y, s, [(20, 55), (140, 55)], GREEN, 5)
160
+ _stroke(ax, X, Y, s, [(65, 55), (65, 28), (95, 28), (95, 55)], GREEN, 5)
161
+ _lines(ax, X, Y, s, [[(58, 28), (102, 28)], [(70, 18), (90, 18)]],
162
+ GREEN, 5)
163
+ _stroke(ax, X, Y, s, [(80, 55), (80, 73)], GREEN, 3)
164
+
165
+
166
+ def _auto_vent(ax, X, Y, s): # 09_自动排气阀
167
+ _stroke(ax, X, Y, s, [(80, 85), (80, 58)], PURPLE, 5)
168
+ _rect(ax, X, Y, s, 61, 36, 38, 23, "#a56cff", "#5d2bc2", 2, rx=4)
169
+ _stroke(ax, X, Y, s, [(69, 36), (69, 25), (91, 25), (91, 36)],
170
+ "#5d2bc2", 3)
171
+ _stroke(ax, X, Y, s, [(74, 25), (86, 25)], "#5d2bc2", 3)
172
+
173
+
174
+ def _fan_coil(ax, X, Y, s): # 10_风机盘管
175
+ _rect(ax, X, Y, s, 20, 22, 120, 56, "#eef2f5", "#555555", 2, rx=4)
176
+ _rect(ax, X, Y, s, 38, 34, 84, 32, "#e0e8ef", "#287ee0", 2, rx=2)
177
+ _lines(ax, X, Y, s, [[(45, y0), (115, y0)] for y0 in (43, 51, 59)],
178
+ "#287ee0", 2)
179
+ _rect(ax, X, Y, s, 10, 39, 10, 22, "#d8dde2", "#555555", 1)
180
+ _rect(ax, X, Y, s, 140, 39, 10, 22, "#d8dde2", "#555555", 1)
181
+
182
+
183
+ def _corrugated(ax, X, Y, s): # 11_不锈钢波纹管
184
+ _stroke(ax, X, Y, s, [(12, 50), (148, 50)], "#777777", 14)
185
+ _stroke(ax, X, Y, s, [(12, 50), (148, 50)], "#d8dde2", 9)
186
+ _lines(ax, X, Y, s, [[(x0, 42), (x0, 58)] for x0 in range(20, 141, 8)],
187
+ "#777777", 2)
188
+ _rect(ax, X, Y, s, 3, 40, 10, 20, "#bbbbbb", "#555555", 1)
189
+ _rect(ax, X, Y, s, 147, 40, 10, 20, "#bbbbbb", "#555555", 1)
190
+
191
+
192
+ _FLOOR_SPIRAL = [(18, 12), (142, 12), (142, 88), (18, 88), (18, 22),
193
+ (132, 22), (132, 78), (28, 78), (28, 32), (122, 32),
194
+ (122, 68), (38, 68), (38, 42), (112, 42), (112, 58),
195
+ (48, 58), (48, 52), (102, 52), (102, 48), (58, 48),
196
+ (58, 52)]
197
+
198
+
199
+ def _floor_heating(ax, X, Y, s): # 12_地暖
200
+ _stroke(ax, X, Y, s, _FLOOR_SPIRAL, RED, 4)
201
+ _lines(ax, X, Y, s, [[(8, 50), (18, 50)], [(142, 50), (152, 50)]],
202
+ RED, 4)
203
+
204
+
205
+ def _manifold(color):
206
+ """分水器:竖汇管 + 三条支管 + 端部白心圆(13 供 / 14 进)。"""
207
+ def body(ax, X, Y, s):
208
+ _stroke(ax, X, Y, s, [(25, 20), (25, 80)], color, 7)
209
+ _lines(ax, X, Y, s, [[(25, y0), (140, y0)] for y0 in (30, 50, 70)],
210
+ color, 5)
211
+ for y0 in (30, 50, 70):
212
+ _circle(ax, X, Y, s, 140, y0, 5, "#ffffff", color, 3)
213
+ return body
214
+
215
+
216
+ def _supply_pipe(ax, X, Y, s): # 15_供水管_热水
217
+ _stroke(ax, X, Y, s, [(10, 50), (140, 50)], RED, 5)
218
+ _stroke(ax, X, Y, s, [(125, 42), (140, 50), (125, 58)], RED, 4)
219
+
220
+
221
+ def _return_pipe(ax, X, Y, s): # 16_回水管
222
+ _stroke(ax, X, Y, s, [(10, 50), (140, 50)], BLUE, 5)
223
+ _stroke(ax, X, Y, s, [(25, 42), (10, 50), (25, 58)], BLUE, 4)
224
+
225
+
226
+ def _ball_valve(ax, X, Y, s): # 17_球阀
227
+ _lines(ax, X, Y, s, [[(10, 50), (58, 50)], [(102, 50), (150, 50)]],
228
+ RED, 5)
229
+ _fillpolys(ax, X, Y, s, [[(58, 34), (80, 50), (58, 66)],
230
+ [(102, 34), (80, 50), (102, 66)]], RED, 1)
231
+ _circle(ax, X, Y, s, 80, 50, 9, "#ffffff", RED, 3)
232
+ _stroke(ax, X, Y, s, [(80, 50), (94, 40)], RED, 3)
233
+
234
+
235
+ # =========================
236
+ # 工厂
237
+ # =========================
238
+ def _hvac(name, pts, body):
239
+ """构造符号工厂;pts 为 {连接点 id: (x, y)}(viewBox 坐标)。"""
240
+ def make(scale=1.0, moves=None, bridge=0, label_dy=None):
241
+ cx, cy = VB_W / 2, VB_H / 2
242
+ P = {k: tuple(moves[k]) if moves and k in moves else v
243
+ for k, v in pts.items()}
244
+ port_off = {k: ((px - cx) * scale, (py - cy) * scale)
245
+ for k, (px, py) in P.items()}
246
+ dy = cy * scale + 24 if label_dy is None else label_dy
247
+
248
+ def draw(ax, x, y, label=None, **kw):
249
+ ox, oy = x - cx * scale, y - cy * scale
250
+
251
+ def X(px):
252
+ return px * scale + ox
253
+
254
+ def Y(py):
255
+ return py * scale + oy
256
+
257
+ body(ax, X, Y, scale)
258
+ if bridge:
259
+ for px, py in P.values():
260
+ if px <= 0:
261
+ seg = ((px, py), (px + bridge, py))
262
+ elif px >= VB_W:
263
+ seg = ((px, py), (px - bridge, py))
264
+ elif py <= 0:
265
+ seg = ((px, py), (px, py + bridge))
266
+ else:
267
+ seg = ((px, py), (px, py - bridge))
268
+ line(ax, [(X(a), Y(b)) for a, b in seg],
269
+ lw=LW_PIPE, color=LINE)
270
+ if label:
271
+ txt(ax, x, y + dy, label, size=17)
272
+
273
+ return Part(name, draw, port_off, {})
274
+ return make
275
+
276
+
277
+ _TWO = {"in": (0, 50), "out": (160, 50)}
278
+ _MANIFOLD_PORTS = {"main": (0, 50), "loop1": (160, 30),
279
+ "loop2": (160, 50), "loop3": (160, 70)}
280
+
281
+ heat_pump = _hvac("空气能主机",
282
+ {"water_out": (160, 40), "water_in": (0, 60)}, _heat_pump)
283
+ water_pump = _hvac("水泵", _TWO, _water_pump)
284
+ anti_freeze_valve = _hvac("防冻阀", _TWO, _stop_valve(RED))
285
+ thermal_tank = _hvac("蓄热水箱",
286
+ {"upper": (0, 32), "lower": (0, 70), "right": (160, 50)},
287
+ _thermal_tank)
288
+ electric_heater = _hvac("电加热", {"center": (0, 50), "out": (160, 50)},
289
+ _electric_heater)
290
+ safety_valve = _hvac("安全阀",
291
+ {"in": (0, 55), "out": (160, 55), "drain": (80, 100)},
292
+ _safety_valve)
293
+ drain_valve = _hvac("排水阀", _TWO, _stop_valve(GREEN))
294
+ makeup_valve = _hvac("补水阀", _TWO, _stop_valve(GREEN))
295
+ auto_vent = _hvac("自动排气阀", {"pipe": (80, 100)}, _auto_vent)
296
+ fan_coil = _hvac("风机盘管",
297
+ {"water_in": (0, 42), "water_out": (160, 58), "air": (80, 0)},
298
+ _fan_coil)
299
+ corrugated_pipe = _hvac("不锈钢波纹管", _TWO, _corrugated)
300
+ floor_heating = _hvac("地暖", _TWO, _floor_heating)
301
+ supply_manifold = _hvac("分水器(供)", _MANIFOLD_PORTS, _manifold(RED))
302
+ return_manifold = _hvac("分水器(进)", _MANIFOLD_PORTS, _manifold(BLUE))
303
+ supply_pipe = _hvac("供水管(热水)", _TWO, _supply_pipe)
304
+ return_pipe = _hvac("回水管", _TWO, _return_pipe)
305
+ ball_valve = _hvac("球阀", _TWO, _ball_valve)
rrplot/part.py ADDED
@@ -0,0 +1,40 @@
1
+ # -*- coding: utf-8 -*-
2
+ """部件数据模型与端口方向基础设施。"""
3
+ from dataclasses import dataclass, field
4
+
5
+ _PORT_DIR = {"n": (0, -1), "s": (0, 1), "e": (1, 0), "w": (-1, 0)}
6
+
7
+
8
+ @dataclass
9
+ class Part:
10
+ """一个以中心为锚点的可绘制部件。"""
11
+
12
+ name: str
13
+ draw: object
14
+ ports: dict = field(default_factory=dict)
15
+ _dirs: dict = field(default_factory=dict)
16
+
17
+ def port(self, key):
18
+ try:
19
+ return self.ports[key]
20
+ except KeyError:
21
+ raise KeyError(
22
+ f"部件 {self.name!r} 没有端口 {key!r},"
23
+ f"可用端口:{', '.join(sorted(self.ports)) or '(无)'}"
24
+ ) from None
25
+
26
+ def direction(self, key):
27
+ """端口外法向单位向量,优先取显式声明。"""
28
+ if key in self._dirs:
29
+ return _PORT_DIR[self._dirs[key]]
30
+ dx, dy = self.port(key)
31
+ if dx == 0 and dy == 0:
32
+ return (0, 0)
33
+ if abs(dx) >= abs(dy):
34
+ return (1 if dx > 0 else -1, 0)
35
+ return (0, 1 if dy > 0 else -1)
36
+
37
+
38
+ def make_part(name, ports, draw, dirs=None):
39
+ """创建部件实例,供部件定义模块使用。"""
40
+ return Part(name, draw, ports, dirs or {})