Change @data/in1.json to some example data for 3D in data_3d/in1.json
--- Content from referenced files ---
Content from @C:\routenodes\data\in1.json:
{
"Canvas": {
"Width": 600,
"Height": 300
},
"Blocks": [
{
"BlockType": "A",
"Width": 100,
"Height": 60,
"Position": "100,120"
},
{
"BlockType": "B",
"Width": 100,
"Height": 60,
"Position": "400,120"
}
],
"StartNode": "200,150",
"EndNode": "400,150"
}
--- End of content ---
Assistant
Done. I've created data_3d/in1.json with 3D example data.
User
Assistant
I have created tools/visualize_3d.py with the logic to render 3D boxes.
Change @tools/visualize.py to render 3D boxes in tools/visualize_3d.py
--- Content from referenced files ---
Content from @C:\routenodes\tools\visualize.py:
#!/usr/bin/env python3
import json
import sys
from matplotlib import patches
from matplotlib import pyplot as plt
def draw_json(json_file, out_file, verbose=False):
# 读取JSON
with open(json_file, "r") as f:
js = json.load(f)
code
# 画布尺⼨
H = js["Canvas"]["Height"]
W = js["Canvas"]["Width"]
# 准备图窗
fig, ax = plt.subplots()
ax.set_aspect("equal", adjustable="box")
ax.set_xlim([0, W])
ax.set_ylim([0, H])
ax.invert_yaxis() # 左上⻆原点
if verbose:
ax.grid(True)
ax.set_title(json_file)
ax.set_xlabel("X")
ax.set_ylabel("Y")
else:
ax.set_axis_off()
# 绘制模块
for b in js["Blocks"]:
h_blk = b["Height"]
w_blk = b["Width"]
# 解析"x,y"
pos = [float(p) for p in b["Position"].split(",")]
x, y = pos
rect = patches.Rectangle(
(x, y),
w_blk,
h_blk,
facecolor=(0.8, 0.8, 0.8),
# edgecolor="b",
# linewidth=1.2,
)
ax.add_patch(rect)
if "BlockType" in b:
ax.text(
x + w_blk / 2,
y + h_blk / 2,
b["BlockType"],
ha="center",
va="center",
fontsize=12,
)
# 绘制路径
if "Lines" in js:
for line_data in js["Lines"]:
pts_str = line_data["Line"]
if len(pts_str) < 2:
continue
# 将每个"x,y"转成坐标向量
coords = []
for s in pts_str:
coords.append([float(p) for p in s.split(",")])
for i in range(len(coords) - 1):
x1, y1 = coords[i]
x2, y2 = coords[i + 1]
ax.plot([x1, x2], [y1, y2], "k-", linewidth=0.5)
# 绘制起点/终点
if "StartNode" in js:
s = [float(p) for p in js["StartNode"].split(",")]
ax.plot(
s[0],
s[1],
marker="o",
markersize=4,
markeredgecolor="none",
markerfacecolor="C1",
)
if "EndNode" in js:
e = [float(p) for p in js["EndNode"].split(",")]
ax.plot(
e[0],
e[1],
marker="s",
markersize=4,
markeredgecolor="none",
markerfacecolor="C2",
)
fig.savefig(out_file, bbox_inches="tight")
if name == "main":
if len(sys.argv) != 3:
print("Usage: python visualize.py in.json out.pdf")
exit(1)