Files
CADmind/backend/scripts/generate_z_bracket.py
2026-09-24 10:40:57 +08:00

163 lines
6.5 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.
"""
Z型折弯支架 (Z-shaped offset bend bracket) —— 一次性生成脚本。
用户描述的是钣金折弯件("整体呈Z形折弯结构,一端长槽孔、一端圆孔,钣金折弯加工,
用于构件间偏移位置的连接,可微调装配位置"),没给具体尺寸,按约定取常规小型
钣金支架量级为假设值,全部列在 ASSUMED_PARAMS 并写进report。
做法说明:Z形支架沿宽度方向横截面恒定,所以不需要专门的钣金折弯能力——把完整的
Z形轮廓直接画成一个2D多边形,在轮廓上的4个折弯角点做圆角,再整体拉伸出宽度。
第一版做法是"三个矩形分别拉伸再布尔合并,最后在3D实体上挑边倒角",结果两个折弯
处的圆角效果不一致(一个有弧度一个是直角)——排查后发现:三个矩形重叠拼接会在
拼接处产生额外的模糊边界,3D边选择器picking到的边在这类多面搭接的位置上算出来
的圆角效果不可靠(倒了但体积几乎没变化,说明没真正生效,而且不报错,很容易被
忽略)。改成"先在2D轮廓上对角点做圆角,再一次性拉伸成实体",从根上避免了拼接
歧义,圆角在2D阶段就是确定的、可验证的(倒角前后轮廓面积必须有实际变化)。
结构:
底板(远端圆孔,可穿螺栓固定) -- 折弯1 -- 腹板(提供Z形偏移高度) -- 折弯2 -- 顶板(远端长槽孔,可微调滑动装配位置)
"""
import json
from pathlib import Path
from build123d import (
Axis,
Circle,
Plane,
Polygon,
Pos,
SlotOverall,
export_step,
export_stl,
extrude,
fillet,
)
BASE_DIR = Path(__file__).resolve().parent.parent
ASSUMED_PARAMS = {
"thickness_mm": 3.0, # 钣金厚度,常规小型结构支架取3mm
"width_mm": 30.0, # 支架宽度
"bottom_flange_length_mm": 40.0, # 底板可见长度(不含与腹板重叠部分)
"top_flange_length_mm": 40.0, # 顶板可见长度
"offset_height_mm": 30.0, # Z形偏移高度:顶板底面相对底板底面抬高的距离
"bend_radius_mm": 3.0, # 折弯内侧圆角半径,按经验取值≈板厚
# 折弯外侧圆角 = 内侧圆角 + 板厚,是钣金折弯的标准惯例,不是独立假设值
"round_hole_diameter_mm": 8.5, # 底板端圆孔,M8间隙孔
"round_hole_x_offset_mm": 20.0, # 圆孔中心距折弯线的距离
"slot_length_mm": 20.0, # 顶板端长槽孔总长
"slot_width_mm": 8.5, # 长槽孔宽度,与圆孔同规格便于配对紧固件
"slot_x_offset_mm": 20.0, # 长槽孔中心距折弯线的距离
}
GIVEN_BY_USER = {
"type": "Z型折弯支架 (Z-shaped offset bend bracket)",
"shape": "整体呈Z形折弯结构",
"holes": "一端长槽孔、一端圆孔",
"process": "钣金折弯加工",
"function": "用于构件间偏移位置的连接,可微调装配位置",
}
def build():
p = ASSUMED_PARAMS
T, W = p["thickness_mm"], p["width_mm"]
L1, L2, H = p["bottom_flange_length_mm"], p["top_flange_length_mm"], p["offset_height_mm"]
bend_r = p["bend_radius_mm"]
outer_r = bend_r + T
# 注意: Z形的两个折弯是"反手性"的(跟C/U形不同),凹凸性不对称分布——
# 用叉积验证过:真正的外侧(凸)角是(T,0)和(0,H+T),内侧(凹)角是(T,H)和(0,T),
# 不是直觉以为的"两个角都在x=0"或"两个角都在x=T"那种对称组合。
pts = [
(-L1, 0), (T, 0), (T, H), (L2, H), (L2, H + T),
(0, H + T), (0, T), (-L1, T),
]
profile = Plane.XY * Polygon(*pts)
part = extrude(profile, amount=W).rotate(Axis.X, 90).translate((0, W / 2, 0))
def y_edges(p):
return [
e for e in p.edges()
if e.bounding_box().size.Y > W * 0.5
and e.bounding_box().size.X < 0.01
and e.bounding_box().size.Z < 0.01
]
def edges_near(p, targets):
found = [
e for e in y_edges(p)
if any(abs(e.bounding_box().min.X - x) < 0.5 and abs(e.bounding_box().min.Z - z) < 0.5 for x, z in targets)
]
assert len(found) == len(targets), f"预期{len(targets)}条边,实际找到{len(found)}条: {targets}"
return found
volume_before = part.volume
outer_edges = edges_near(part, [(T, 0), (0, H + T)])
part = fillet(outer_edges, radius=outer_r)
inner_edges = edges_near(part, [(T, H), (0, T)])
part = fillet(inner_edges, radius=bend_r)
assert abs(part.volume - volume_before) > 1.0, "折弯圆角疑似没有真正生效(体积几乎没变化)"
round_hole = extrude(
Plane.XY.offset(T) * (Pos(-p["round_hole_x_offset_mm"], 0, 0) * Circle(p["round_hole_diameter_mm"] / 2)),
amount=-T,
)
part = part - round_hole
slot_sketch = Plane.XY.offset(H + T) * (
Pos(p["slot_x_offset_mm"], 0, 0) * SlotOverall(p["slot_length_mm"], p["slot_width_mm"])
)
slot_cutter = extrude(slot_sketch, amount=-T)
part = part - slot_cutter
return part
def main():
part = build()
name = "z_bend_bracket"
out_dir = BASE_DIR / "output" / name
out_dir.mkdir(parents=True, exist_ok=True)
step_path = out_dir / "model.step"
stl_path = out_dir / "model.stl"
export_step(part, str(step_path))
export_stl(part, str(stl_path))
bbox = part.bounding_box()
report = {
"part_name": name,
"given_by_user": GIVEN_BY_USER,
"assumed_params_mm": ASSUMED_PARAMS,
"note": "assumed_params_mm 均为用户描述中未给出、按常规小型钣金支架量级假设的数值,"
"折弯内圆角按经验取≈板厚,外圆角=内圆角+板厚(标准钣金惯例);"
"如需按真实装配间隙/受力选型,请替换参数后重新生成。",
"solid_count": len(part.solids()),
"volume_mm3": part.volume,
"bounding_box_mm": {
"min": [bbox.min.X, bbox.min.Y, bbox.min.Z],
"max": [bbox.max.X, bbox.max.Y, bbox.max.Z],
},
"step_file": str(step_path),
"stl_file": str(stl_path),
}
print(f"[成功] {name} (实体数: {report['solid_count']})")
print(f" 体积: {report['volume_mm3']:.2f} mm^3")
print(f" STEP: {step_path}")
print(f" STL: {stl_path}")
report_path = out_dir / "report.json"
report_path.write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding="utf-8")
print(f" 报告(含全部假设参数): {report_path}")
if __name__ == "__main__":
main()