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

255 lines
9.3 KiB
Python

"""
双膜片联轴器 (double diaphragm coupling) —— 一次性生成脚本。
这是一个用户描述里没有给出具体工程参数的自定义零件,按约定不编造承力特征本身
(膜片开槽的花瓣数/半径这些决定挠性和扭矩传递能力的尺寸),而是取行业里常见的
中小型联轴器量级作为假设值,所有假设值集中列在下面 ASSUMED_PARAMS,并在生成
报告里原样输出,方便用户核对/替换成真实工程数值重新生成。
结构(沿Z轴从下到上堆叠,不做布尔融合,保持独立实体,对应真实装配是螺栓连接
而不是焊接一体):
夹紧轴套A -> 膜片组A(5片) -> 中间节 -> 膜片组B(5片) -> 夹紧轴套B
"""
import json
import math
from pathlib import Path
from build123d import (
CenterArc,
Circle,
Compound,
Location,
Mode,
Plane,
Polygon,
PolarLocations,
SlotArc,
export_step,
export_stl,
extrude,
)
BASE_DIR = Path(__file__).resolve().parent.parent
# ---------------------------------------------------------------------------
# 假设参数(用户描述里没给的,按常规中小型工业联轴器量级取值,全部会写进report)
# ---------------------------------------------------------------------------
ASSUMED_PARAMS = {
"shaft_bore_diameter_mm": 20.0, # 两端轴孔直径,假设两端相同
"hub_outer_diameter_mm": 50.0,
"hub_length_mm": 35.0,
"hub_slit_width_mm": 3.0, # 夹紧开缝宽度
"hub_clamp_screw_diameter_mm": 5.5, # M5 间隙孔径
"hub_clamp_screw_count": 2,
"diaphragm_outer_diameter_mm": 65.0,
"diaphragm_disc_thickness_mm": 0.3,
"diaphragm_disc_count_per_pack": 5,
"diaphragm_outer_bolt_circle_diameter_mm": 40.0, # 连接轴套一侧
"diaphragm_inner_bolt_circle_diameter_mm": 25.0, # 连接中间节一侧
"diaphragm_bolt_diameter_mm": 5.5, # M5 间隙孔径
"diaphragm_bolt_count": 6,
"diaphragm_slot_count": 6,
"diaphragm_slot_width_mm": 3.0,
"spacer_outer_diameter_mm": 30.0,
"spacer_bore_diameter_mm": 20.0, # 减重通孔,不连接轴
"spacer_length_mm": 50.0,
"spacer_flange_outer_diameter_mm": 45.0,
"spacer_flange_thickness_mm": 6.0,
}
GIVEN_BY_USER = {
"type": "双膜片联轴器 (double diaphragm coupling)",
"end_type": "两端夹紧式轴套 (clamp-type shaft hub, both ends)",
"middle": "两组膜片,螺栓连接 (two diaphragm packs, bolted)",
"process": "车铣加工成型 (turned + milled)",
"function": "两轴连接,补偿安装偏差,传递扭矩",
}
def build_hub():
p = ASSUMED_PARAMS
R_out = p["hub_outer_diameter_mm"] / 2
R_bore = p["shaft_bore_diameter_mm"] / 2
length = p["hub_length_mm"]
slit_width = p["hub_slit_width_mm"]
hub_sketch = Plane.XY * Circle(R_out) - Plane.XY * Circle(R_bore)
hub = extrude(hub_sketch, amount=length)
half_angle = math.degrees(math.asin((slit_width / 2) / R_out))
reach = R_out * 1.5
p0 = (0, 0)
p1 = (reach * math.cos(math.radians(half_angle)), reach * math.sin(math.radians(half_angle)))
p2 = (reach * math.cos(math.radians(-half_angle)), reach * math.sin(math.radians(-half_angle)))
wedge_sketch = Plane.XY * Polygon(p0, p2, p1) # 顶点顺序保证挤出方向与hub一致(+Z)
wedge = extrude(wedge_sketch, amount=length)
hub = hub - wedge
hole_r = p["hub_clamp_screw_diameter_mm"] / 2
hole_depth = R_out * 2 + 10
z_positions = [length * 0.3, length * 0.7][: p["hub_clamp_screw_count"]]
for z_pos in z_positions:
plane = Plane(origin=(R_out - 4, 0, z_pos), z_dir=(0, 1, 0))
hole = extrude(plane * Circle(hole_r), amount=hole_depth)
hub = hub - hole
# 连接膜片组一侧(z=length端)的螺栓孔,盲孔,深度取hub长度的1/3
R_ob = p["diaphragm_outer_bolt_circle_diameter_mm"] / 2
bolt_r = p["diaphragm_bolt_diameter_mm"] / 2
bolt_depth = length / 3
conn_plane = Plane.XY.offset(length - bolt_depth)
bolt_sketch = None
for loc in PolarLocations(R_ob, p["diaphragm_bolt_count"]):
c = conn_plane * (loc * Circle(bolt_r))
bolt_sketch = c if bolt_sketch is None else bolt_sketch + c
bolts = extrude(bolt_sketch, amount=bolt_depth + 1)
hub = hub - bolts
return hub
def build_diaphragm_disc():
p = ASSUMED_PARAMS
R_out = p["diaphragm_outer_diameter_mm"] / 2
R_ob = p["diaphragm_outer_bolt_circle_diameter_mm"] / 2
R_ib = p["diaphragm_inner_bolt_circle_diameter_mm"] / 2
R_slot = (R_ob + R_ib) / 2
slot_width = p["diaphragm_slot_width_mm"]
N = p["diaphragm_slot_count"]
bolt_r = p["diaphragm_bolt_diameter_mm"] / 2
thickness = p["diaphragm_disc_thickness_mm"]
disc_sketch = Plane.XY * Circle(R_out)
sector = 360 / N
slot_span = sector - 20 # 每个扇区两端各留10度的“筋”,与螺栓孔角度对齐
for i in range(N):
start = i * sector + 10
arc = CenterArc(center=(0, 0), radius=R_slot, start_angle=start, arc_size=slot_span, mode=Mode.PRIVATE)
slot = Plane.XY * SlotArc(arc=arc.edge(), height=slot_width, mode=Mode.PRIVATE)
disc_sketch = disc_sketch - slot
for loc in PolarLocations(R_ob, N):
disc_sketch = disc_sketch - (Plane.XY * (loc * Circle(bolt_r)))
for loc in PolarLocations(R_ib, N):
disc_sketch = disc_sketch - (Plane.XY * (loc * Circle(bolt_r)))
return extrude(disc_sketch, amount=thickness)
def build_diaphragm_pack():
p = ASSUMED_PARAMS
disc = build_diaphragm_disc()
thickness = p["diaphragm_disc_thickness_mm"]
count = p["diaphragm_disc_count_per_pack"]
discs = []
for i in range(count):
discs.append(disc.moved(Location((0, 0, i * thickness))))
return Compound(discs)
def build_spacer():
p = ASSUMED_PARAMS
R_body = p["spacer_outer_diameter_mm"] / 2
R_bore = p["spacer_bore_diameter_mm"] / 2
length = p["spacer_length_mm"]
R_flange = p["spacer_flange_outer_diameter_mm"] / 2
flange_t = p["spacer_flange_thickness_mm"]
R_ib = p["diaphragm_inner_bolt_circle_diameter_mm"] / 2
bolt_r = p["diaphragm_bolt_diameter_mm"] / 2
N = p["diaphragm_bolt_count"]
body_sketch = Plane.XY * Circle(R_body) - Plane.XY * Circle(R_bore)
body = extrude(body_sketch, amount=length)
def make_flange(z_offset):
sk = Plane.XY.offset(z_offset) * Circle(R_flange) - Plane.XY.offset(z_offset) * Circle(R_bore)
fl = extrude(sk, amount=flange_t)
bolt_sketch = None
for loc in PolarLocations(R_ib, N):
c = Plane.XY.offset(z_offset) * (loc * Circle(bolt_r))
bolt_sketch = c if bolt_sketch is None else bolt_sketch + c
bolts = extrude(bolt_sketch, amount=flange_t)
return fl - bolts
flange_bottom = make_flange(-flange_t)
flange_top = make_flange(length)
return Compound([body, flange_bottom, flange_top])
def assemble():
p = ASSUMED_PARAMS
hub_len = p["hub_length_mm"]
disc_thk = p["diaphragm_disc_thickness_mm"]
disc_count = p["diaphragm_disc_count_per_pack"]
pack_thk = disc_thk * disc_count
flange_t = p["spacer_flange_thickness_mm"]
spacer_len = p["spacer_length_mm"]
z = 0.0
hub_a = build_hub().moved(Location((0, 0, z)))
z += hub_len
pack_a = build_diaphragm_pack().moved(Location((0, 0, z)))
z += pack_thk
spacer = build_spacer().moved(Location((0, 0, z + flange_t)))
z += flange_t + spacer_len + flange_t
pack_b = build_diaphragm_pack().moved(Location((0, 0, z)))
z += pack_thk
hub_b = build_hub().moved(Location((0, 0, z)))
total_length = z + hub_len
assembly = Compound([hub_a, pack_a, spacer, pack_b, hub_b])
return assembly, total_length
def main():
assembly, total_length = assemble()
name = "double_diaphragm_coupling"
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(assembly, str(step_path))
export_stl(assembly, str(stl_path))
bbox = assembly.bounding_box()
report = {
"part_name": name,
"given_by_user": GIVEN_BY_USER,
"assumed_params_mm": ASSUMED_PARAMS,
"note": "所有 assumed_params_mm 字段均为未在用户描述中给出、按常规中小型工业联轴器量级假设的数值,"
"不是任何特定厂商型号的复现;如需按真实工况选型,请替换这些参数后重新生成。",
"total_length_mm": total_length,
"max_outer_diameter_mm": ASSUMED_PARAMS["diaphragm_outer_diameter_mm"],
"volume_mm3": assembly.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}")
print(f" 总长: {total_length:.1f}mm 最大外径: {report['max_outer_diameter_mm']}mm")
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()