81 lines
4.7 KiB
Python
81 lines
4.7 KiB
Python
"""Legacy gear remains centered; optional hub extends from its +Z face."""
|
|
import math
|
|
from bd_warehouse.gear import SpurGear
|
|
from build123d import Pos, Cylinder, Edge, Wire, Face, Vector, extrude
|
|
|
|
|
|
|
|
def rounded_gear(module,teeth,pressure,thickness,fillet_radius,backlash=0):
|
|
"""Transverse involute with explicit base tangents and geometric root blends."""
|
|
pitch=module*teeth/2;tip=pitch+module;root=pitch-1.25*module
|
|
base=pitch*math.cos(math.radians(pressure))
|
|
def involute(r):
|
|
t=math.sqrt(max(0,(r/base)**2-1));return t-math.atan(t)
|
|
inv_pitch=involute(pitch);half=math.pi/(2*teeth)-backlash/(2*pitch);tau=2*math.pi/teeth
|
|
start=max(root,base);t0=math.sqrt(max(0,(start/base)**2-1));t1=math.sqrt((tip/base)**2-1)
|
|
def point(r,a):return Vector(r*math.cos(a),r*math.sin(a),0)
|
|
edges=[]
|
|
for i in range(teeth):
|
|
center=i*tau;la=center-half-inv_pitch;ra=center+half+inv_pitch
|
|
ts=[t0+(t1-t0)*j/48 for j in range(49)]
|
|
left=[point(base*math.sqrt(1+t*t),la+t-math.atan(t)) for t in ts]
|
|
right=[point(base*math.sqrt(1+t*t),ra-t+math.atan(t)) for t in reversed(ts)]
|
|
rl=point(root,la+involute(start));rr=point(root,ra-involute(start))
|
|
if (rl-left[0]).length>1e-7:edges.append(Edge.make_line(rl,left[0]))
|
|
edges.append(Edge.make_spline(left,tangents=[point(1,la+t0),point(1,la+t1)]))
|
|
edges.append(Edge.make_three_point_arc(left[-1],point(tip,center),right[0]))
|
|
edges.append(Edge.make_spline(right,tangents=[-point(1,ra-t1),-point(1,ra-t0)]))
|
|
if (right[-1]-rr).length>1e-7:edges.append(Edge.make_line(right[-1],rr))
|
|
nxt=center+tau-half-inv_pitch+involute(start)
|
|
edges.append(Edge.make_three_point_arc(rr,point(root,(ra-involute(start)+nxt)/2),point(root,nxt)))
|
|
face=Face(Wire(edges))
|
|
corners=[v for v in face.vertices() if abs(math.hypot(v.X,v.Y)-root)<1e-6]
|
|
try:
|
|
if fillet_radius:face=face.fillet_2d(fillet_radius,corners)
|
|
except Exception as exc:raise ValueError('ROOT_FILLET_FIT: 圆角超过当前齿根连接段可用空间,请减小root_fillet;未静默省略圆角') from exc
|
|
return Pos(0,0,-thickness/2)*extrude(face,amount=thickness)
|
|
|
|
|
|
def validate(context, target, params):
|
|
errors = []
|
|
root = params.get("root_fillet", 0)
|
|
if type(root) not in (int,float) or not math.isfinite(root) or not 0 <= root <= params["module"]*.5:
|
|
errors.append("INVALID_ROOT_FILLET: 齿根圆角必须为0到半模数之间的有限毫米值;0保持原有无圆角齿根")
|
|
if params["tooth_count"] < 4:
|
|
errors.append("INVALID_PARAMS: 齿数太少")
|
|
backlash=params.get("backlash",0)
|
|
if type(backlash) not in (int,float) or not math.isfinite(backlash) or not 0 <= backlash <= params["module"]*.5:
|
|
errors.append("INVALID_BACKLASH: 单轮分度圆圆周齿厚减薄量必须为0到半模数之间的有限毫米值,不是径向缩放")
|
|
hub, extension, bore = (params.get(k, 0) for k in ("hub_diameter", "hub_extension", "bore_diameter"))
|
|
root_diameter = params["module"] * (params["tooth_count"] - 2.5)
|
|
if bool(hub) != bool(extension):
|
|
errors.append("INVALID_HUB: 轮毂直径和伸出长度必须同时给出正值,或同时为0")
|
|
if hub and not 0 < hub < root_diameter:
|
|
errors.append("INVALID_HUB: 轮毂直径必须小于齿根直径,以保留完整齿形")
|
|
if bore and not 0 < bore < min(root_diameter, hub or root_diameter):
|
|
errors.append("INVALID_BORE: 贯穿孔必须小于齿根及轮毂直径")
|
|
return errors
|
|
|
|
|
|
def run(context, target, params):
|
|
errors = validate(context,target,params)
|
|
if errors:raise ValueError("; ".join(errors))
|
|
if params.get("root_fillet",0) or params.get("backlash",0):
|
|
part=rounded_gear(params["module"],params["tooth_count"],params["pressure_angle"],params["thickness"],params.get("root_fillet",0),params.get("backlash",0))
|
|
else:
|
|
part = SpurGear(module=params["module"], tooth_count=params["tooth_count"],
|
|
pressure_angle=params["pressure_angle"], thickness=params["thickness"])
|
|
hub, extension, bore = (params.get(k, 0) for k in ("hub_diameter", "hub_extension", "bore_diameter"))
|
|
if hub and extension:
|
|
top = part.bounding_box().max.Z
|
|
overlap = min(params["thickness"] * .1, .1)
|
|
cylinder = Pos(0, 0, top + (extension-overlap)/2) * Cylinder(hub/2, extension+overlap)
|
|
part = part.fuse(cylinder)
|
|
if bore:
|
|
box = part.bounding_box()
|
|
cutter = Pos(0, 0, (box.min.Z+box.max.Z)/2) * Cylinder(bore/2, box.size.Z+2)
|
|
part = part.cut(cutter)
|
|
if not part.is_valid or len(part.solids()) != 1:
|
|
raise ValueError("GEAR_NOT_CONNECTED: 齿盘、轮毂及贯穿孔必须形成一个有效实体")
|
|
return Pos(params["x_offset"], params["y_offset"], params["z_offset"]) * part
|