Files
wheel/scripts/render_shop_sprues_preview.py

38 lines
1.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Render a top view from the exported binary STL meshes using Pillow."""
from pathlib import Path
import struct
from PIL import Image, ImageDraw, ImageFont
ROOT = Path(__file__).resolve().parent.parent
OUT = ROOT / 'models' / 'print_shop_joined_eight'
image = Image.new('RGB',(1200,1120),'#f5f7fa')
draw = ImageDraw.Draw(image)
font_path='C:/Windows/Fonts/msyh.ttc'
font=ImageFont.truetype(font_path,32)
small=ImageFont.truetype(font_path,23)
tiny=ImageFont.truetype(font_path,19)
draw.text((60,35),'打印店交付:每组 8 个垫片',font=font,fill='#172d4d')
draw.text((60,90),'支架单独一个文件;以下两组分别作为拼件提交',font=small,fill='#53677e')
def draw_mesh(filename,left,top,color):
data=(OUT/filename).read_bytes()
count=struct.unpack_from('<I',data,80)[0]
if len(data)!=84+50*count:raise RuntimeError('Expected binary STL')
scale=10
# Bounding box x=-5.543..29.543, y=-6..54; flip Y for screen.
def xy(x,y):return (left+(x+5.543277)*scale,top+(54-y)*scale)
for i in range(count):
t=struct.unpack_from('<12fH',data,84+i*50)
pts=[xy(t[3],t[4]),xy(t[6],t[7]),xy(t[9],t[10])]
draw.polygon(pts,fill=color)
draw.text((150,155),'0.5 mm 垫片 × 8',font=font,fill='#996115')
draw.text((680,155),'1.0 mm 垫片 × 8',font=font,fill='#087c76')
draw_mesh('垫片0.5mm_8个拼件.stl',165,230,'#dc9b40')
draw_mesh('垫片1.0mm_8个拼件.stl',695,230,'#339b91')
draw.text((60,900),'两组均为一体拼件,每组 7 根连接杆,截面 1.8 × 1.8 mm。',font=small,fill='#304764')
draw.text((60,945),'颜色仅用于区分厚度;打印后从垫片外缘剪除连接杆并修平。',font=small,fill='#304764')
draw.text((60,990),'垫片与连接杆的实际结合面受垫片厚度限制,生产前请商家确认。',font=tiny,fill='#8a4b14')
draw.text((60,1035),'图为 STL 顶视示意,实际切片及成品以打印商评估为准。',font=tiny,fill='#53677e')
image.save(OUT/'拼件预览.png')