ymj_data_collect/excel_point_to_bm_json_.py

123 lines
3.6 KiB
Python
Raw Normal View History

2025-02-25 10:15:21 +08:00
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
def col(char):
return ord(char) - ord('A') + 0
# 读取Excel文件指定表格路径
file_path = '预埋件位置.xlsx'
# 使用pandas读取Excel
df = pd.read_excel(file_path, sheet_name='测量结果') # 根据你的Excel表格来设置sheet_name
# 打印列名
print("DataFrame 列名:", df.columns)
# 创建一个图形和坐标轴
fig, ax = plt.subplots()
# 初始化边界变量
x_min, x_max = float('inf'), float('-inf')
y_min, y_max = float('inf'), float('-inf')
2025-02-27 15:18:00 +08:00
x_center_max = 0 # 在右测坐标系中。所有预埋件中心点的x坐标最大的值
x_center_max_w = 0
x_center_max_h = 0
2025-02-25 10:15:21 +08:00
for index, row in df.iloc[2:30].iterrows():
2025-02-27 15:18:00 +08:00
original_x_center = row[col('K')]
if(original_x_center > x_center_max):
x_center_max = original_x_center
x_center_max_w = row[col('D')]
x_center_max_h = row[col('E')]
print(f"x_center_max_original={x_center_max}")
# 以为预埋件中心点的x最大的矩形的左下角作为坐标原点
x_center_max_left_bottom = x_center_max + x_center_max_w / 2
2025-02-25 10:15:21 +08:00
bm_struct_array = []
for index, row in df.iloc[2:30].iterrows():
# 打印B列和C列的值
print(f"{index+2} -> A列-编号: {row[col('A')]}, D列-测量宽度: {row[col('D')]}, E列-测量高度: {row[col('E')]}K列-测量中心x: {row[col('K')]}, L列-测量中心y: {row[col('L')]}")
2025-02-27 15:18:00 +08:00
# 使用新的中心点之后x坐标需要转换
x_center_original = row[col('K')]
center_x =(-x_center_original) + x_center_max_left_bottom + 350
# center_x = -x_center_original
2025-02-25 10:15:21 +08:00
center_y = row[col('L')]
2025-02-27 15:18:00 +08:00
width = row[col('D')]
height = row[col('E')]
2025-02-28 09:29:29 +08:00
# 左上角
2025-02-27 15:18:00 +08:00
x = center_x - width / 2
2025-02-28 09:29:29 +08:00
# 左上角
y = center_y + height / 2
# 左下角
x_left_bottom = center_x - width / 2
# 左下角
y_left_bottom = center_y - height / 2
2025-02-27 15:18:00 +08:00
2025-02-25 10:15:21 +08:00
# [{
# "code": "PT001",
# "type": "250x1450",
# "x": "905",
# "y": "0",
# "center": "0.350",
# "w": "1450",
# "h": "250",
# "angle": "90\""
# },
bm_struct_array.append({
2025-02-27 15:18:00 +08:00
"code": int(row[col('A')]),
"type": f"{height}x{width}",# h x w
"x": int(center_x),
2025-02-25 10:15:21 +08:00
"y": 0,
2025-02-27 15:18:00 +08:00
"center": int(center_y),
"w":width,
"h": height,
"angle" : "0"
2025-02-25 10:15:21 +08:00
})
# 画出中心点
ax.scatter(center_x, center_y, color='red', marker='o', s=10)
# 绘制矩形
2025-02-28 09:29:29 +08:00
rect = Rectangle((x_left_bottom, y_left_bottom),width, height, linewidth=1, edgecolor='r', facecolor='none')
2025-02-25 10:15:21 +08:00
ax.add_patch(rect)
# 在矩形中心绘制编号A列
ax.text(
center_x+60, center_y, str(row[col('A')]),
ha='center', va='center',
fontsize=8, color='blue' # 编号字体大小为12颜色为蓝色
)
# 在矩形上边绘制宽度D列
ax.text(
2025-02-28 09:29:29 +08:00
x + row[col('D')] / 2, y_left_bottom + row[col('E')], str(row[col('D')]),
2025-02-25 10:15:21 +08:00
ha='center', va='bottom',
fontsize=6, color='green' # 宽度字体大小为10颜色为绿色
)
# 在矩形右边绘制高度E列
ax.text(
2025-02-28 09:29:29 +08:00
x + width, y_left_bottom + height / 2, height,
2025-02-25 10:15:21 +08:00
ha='left', va='center',
fontsize=6, color='red' # 高度字体大小为10颜色为红色
)
2025-02-27 15:18:00 +08:00
# 设置坐标轴的范围
# ax.set_xlim(x_min, x_max ) # 留出一些边距
# ax.set_ylim(y_min, y_max ) # 留出一些边距
2025-02-25 10:15:21 +08:00
# 设置坐标轴的比例
ax.set_aspect('equal')
# 显示图形
plt.show()
# 保存bm_struct_array 为json到本地
import json
with open('bm_struct_array.json', 'w') as f:
json.dump(bm_struct_array, f)