DMU50_GUI_PY/funct_inlay.py
2025-04-04 14:48:00 +02:00

86 lines
3.7 KiB
Python

import matplotlib.pyplot as plt
import numpy as np
def generate_picture_inlay(all_group_vars, picture_path, DEBUG=False):
try:
vars = all_group_vars['inlay']
x_num, y_num, prt_num = int(vars['INL_X_NUM']), int(vars['INL_Y_NUM']),int(vars['PRT_NUM'])
x_offset_mm, y_offset_mm,y_wide_mm,x_wide_mm = float(vars['INL_X_OFFSET']), float(vars['INL_Y_OFFSET']),float(vars['INL_Y_LENGTH']),float(vars['INL_X_LENGTH'])
if x_num <= 0 or y_num <= 0:
raise ValueError("INL_X_NUM and INL_Y_NUM must be positive integers.")
rect_realsize_factor = 0.9
canvas_height_mm = x_num * x_offset_mm - x_offset_mm * (1-rect_realsize_factor)
canvas_width_mm = y_num * y_offset_mm - y_offset_mm * (1-rect_realsize_factor)
x_offset = x_offset_mm / canvas_height_mm # Swapping due to axis swap
y_offset = y_offset_mm / canvas_width_mm # Swapping due to axis swap
x_wide = x_wide_mm / canvas_height_mm
y_wide = y_wide_mm / canvas_width_mm
fig, ax = plt.subplots(figsize=(12, 8))
ax.set_facecolor('black')
rect = plt.Rectangle((-1, -1),2+y_offset*y_num,2+x_offset*x_num, color='lightgrey')
ax.add_patch(rect)
for xxx in range(x_num):
for yyy in range(y_num):
# Mirroring horizontally
x_start = xxx * x_offset
y_start = ((yyy) * y_offset) # Corrected vertical mirroring
rect = plt.Rectangle((y_start, x_start), y_wide, x_wide, color='white')
ax.add_patch(rect)
for parts in range(prt_num):
col='black'
if parts ==0:
rect = plt.Rectangle((y_start+y_wide-y_wide*0.3, x_start), y_wide*0.3,x_wide*0.3, color=col)
ax.add_patch(rect)
elif parts ==1:
rect = plt.Rectangle((y_start,x_start+x_wide-x_wide*0.3), y_wide*0.3,x_wide*0.3, color=col)
ax.add_patch(rect)
elif parts ==2:
rect = plt.Rectangle((y_start+y_wide-y_wide*0.3, x_start+x_wide-x_wide*0.3), y_wide*0.3,x_wide*0.3, color=col)
ax.add_patch(rect)
elif parts ==3:
rect = plt.Rectangle((y_start, x_start), y_wide*0.3,x_wide*0.3, color=col)
ax.add_patch(rect)
# Arrows with adjusted margins
ax.arrow(1.0, 0.0, -0.3, 0, head_width=0.03, head_length=0.02, fc='green', ec='green', linewidth=2, edgecolor='white')
ax.arrow(1.0, 0.0, 0, 0.3, head_width=0.02, head_length=0.03, fc='red', ec='red', linewidth=2, edgecolor='white')
margin = 0.06
ax.set_xlim(0-margin, 1+margin)
ax.set_ylim(0-margin, 1+margin)
ax.set_xticks([]), ax.set_yticks([])
# plt.subplots_adjust(left=0.01, right=0.99, top=0.99, bottom=0.01) # Adjusting the margins
# ax.set_xlim(1, 0) # Flipping the x-axis
# ax.set_ylim(1, 0) # Flipping the y-axis
if DEBUG:
plt.show()
else:
fig.savefig(picture_path, bbox_inches='tight', pad_inches=0.5, dpi=300, transparent=True) # Adjusted padding
plt.close()
except ValueError as ve:
print(f"Invalid Input: {ve}")
except Exception as e:
print(f"Error generating matrix: {e}")
if __name__ == "__main__":
vars = {'INL_X_NUM': 8, 'INL_Y_NUM': 12, 'INL_X_OFFSET': 100, 'INL_Y_OFFSET': 125, 'PRT_NUM': 1, 'INL_X_LENGTH':50, 'INL_Y_LENGTH':50}
picture_path = "cfg_picture/TEST_inlay.jpg"
generate_picture_inlay({'inlay': vars}, picture_path, DEBUG=True)