ymj_data_collect/excel_point_to_bm_json_.py

115 lines
3.2 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')
x_max_original = 0
for index, row in df.iloc[2:30].iterrows():
original_x = row[col('K')] - row[col('D')] / 2
if(original_x > x_max_original):
x_max_original = original_x
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')]}")
# 转换x坐标
x_original = row[col('K')]
x = x_max_original - x_original - row[col('D')] / 2
y = row[col('L')] - row[col('E')] / 2
# 转换中心点坐标
center_x = x_max_original - x_original
center_y = row[col('L')]
# [{
# "code": "PT001",
# "type": "250x1450",
# "x": "905",
# "y": "0",
# "center": "0.350",
# "w": "1450",
# "h": "250",
# "angle": "90\""
# },
bm_struct_array.append({
"code": str(row[col('A')]),
"type": f"{str(row[col('E')])}x{str(row[col('D')])}",# w x h
"x": str(center_x),
"y": 0,
"center": str(center_y),
"w": str(row[col('D')]),
"h": str(row[col('E')]),
"angle" : ""
})
# 画出中心点
ax.scatter(center_x, center_y, color='red', marker='o', s=10)
# 更新边界
x_min = min(x_min, x)
x_max = max(x_max, x + row[col('D')])
y_min = min(y_min, y)
y_max = max(y_max, y + row[col('E')])
# 绘制矩形
rect = Rectangle((x, y), row[col('D')], row[col('E')], linewidth=1, edgecolor='r', facecolor='none')
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(
x + row[col('D')] / 2, y + row[col('E')], str(row[col('D')]),
ha='center', va='bottom',
fontsize=6, color='green' # 宽度字体大小为10颜色为绿色
)
# 在矩形右边绘制高度E列
ax.text(
x + row[col('D')], y + row[col('E')] / 2, str(row[col('E')]),
ha='left', va='center',
fontsize=6, color='red' # 高度字体大小为10颜色为红色
)
# 设置坐标轴的范围外扩100左右
ax.set_xlim(x_min, x_max ) # 留出一些边距
ax.set_ylim(y_min, y_max ) # 留出一些边距
# 设置坐标轴的比例
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)