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_center_max = 0 # 在右测坐标系中。所有预埋件中心点的x坐标最大的值 x_center_max_w = 0 x_center_max_h = 0 for index, row in df.iloc[2:30].iterrows(): 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 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_center_original = row[col('K')] center_x =(-x_center_original) + x_center_max_left_bottom + 350 # center_x = -x_center_original center_y = row[col('L')] width = row[col('D')] height = row[col('E')] # 左上角 x = center_x - width / 2 # 左上角 y = center_y + height / 2 # 左下角 x_left_bottom = center_x - width / 2 # 左下角 y_left_bottom = center_y - height / 2 # [{ # "code": "PT001", # "type": "250x1450", # "x": "905", # "y": "0", # "center": "0.350", # "w": "1450", # "h": "250", # "angle": "90\"" # }, bm_struct_array.append({ "code": int(row[col('A')]), "type": f"{height}x{width}",# h x w "x": int(center_x), "y": 0, "center": int(center_y), "w":width, "h": height, "angle" : "0" }) # 画出中心点 ax.scatter(center_x, center_y, color='red', marker='o', s=10) # 绘制矩形 rect = Rectangle((x_left_bottom, y_left_bottom),width, height, 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_left_bottom + row[col('E')], str(row[col('D')]), ha='center', va='bottom', fontsize=6, color='green' # 宽度字体大小为10,颜色为绿色 ) # 在矩形右边绘制高度(E列) ax.text( x + width, y_left_bottom + height / 2, height, ha='left', va='center', fontsize=6, color='red' # 高度字体大小为10,颜色为红色 ) # 设置坐标轴的范围 # 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)