Files
2026-09-24 10:40:57 +08:00

102 lines
6.1 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.
"""Envelope-driven conical tooth body: spherical involute projected to transverse conical sections.
Spherical involute relation cross-checked against Apache-2.0 CQ_Gears:
https://github.com/meadiode/cq_gears/blob/main/cq_gears/utils.py (s_inv).
No robot dimensions are embedded. Envelope-driven, not a certified mating pair.
"""
import math
from build123d import Edge,Wire,Face,Sketch,Vector,loft,Axis
def spherical_involute(base,polar):
return math.acos(max(-1,min(1,math.cos(polar)/math.cos(base))))/math.sin(base)-math.acos(max(-1,min(1,math.tan(base)/math.tan(polar))))
def make_profile(z, large_diameter,small_diameter,height,teeth,pressure_angle,twist_angle,root_clearance=0):
radius=large_diameter/2
tip=math.atan((large_diameter-small_diameter)/(2*height))
apex=radius/math.tan(tip)
pitch=math.atan(math.tan(tip)*teeth/(teeth+2))
base=math.asin(math.cos(math.radians(pressure_angle))*math.sin(pitch))
depth=radius*4.5/(teeth+2)+root_clearance
root=math.atan((radius-depth)/apex)
invpitch=spherical_involute(base,pitch)
phase=math.radians(twist_angle)*z/height
axial=apex-z
def point(gamma,theta):
r=axial*math.tan(gamma)
return Vector(r*math.cos(theta+phase),r*math.sin(theta+phase),z)
def tangent(gamma,theta,side,travel):
# The spherical involute meets its base cone in the radial direction.
# Explicit endpoint tangents avoid the interpolation cusp error there.
eps=1e-6
derivative=0.0 if gamma-base<eps else (spherical_involute(base,gamma+eps)-spherical_involute(base,gamma-eps))/(2*eps)
r=axial*math.tan(gamma);dr=axial/(math.cos(gamma)**2);angle=theta+phase
dt=side*derivative
return Vector(dr*math.cos(angle)-r*math.sin(angle)*dt,dr*math.sin(angle)+r*math.cos(angle)*dt,0).normalized()*travel
edges=[];tau=2*math.pi/teeth;half=math.pi/(2*teeth)
for i in range(teeth):
center=i*tau
start=max(root,base)
gammas=[start+(tip-start)*j/24 for j in range(25)]
left=[point(g,center-half-invpitch+spherical_involute(base,g)) for g in gammas]
right=[point(g,center+half+invpitch-spherical_involute(base,g)) for g in reversed(gammas)]
root_left_angle=center-half-invpitch+spherical_involute(base,start)
root_right_angle=center+half+invpitch-spherical_involute(base,start)
root_left=point(root,root_left_angle);root_right=point(root,root_right_angle)
if (root_left-left[0]).length>1e-7:edges.append(Edge.make_line(root_left,left[0]))
left_angles=[center-half-invpitch+spherical_involute(base,g) for g in (start,tip)]
edges.append(Edge.make_spline(left,tangents=[tangent(g,a,1,1) for g,a in zip((start,tip),left_angles)]))
edges.append(Edge.make_three_point_arc(left[-1],point(tip,center),right[0]))
right_angles=[center+half+invpitch-spherical_involute(base,g) for g in (tip,start)]
edges.append(Edge.make_spline(right,tangents=[tangent(g,a,-1,-1) for g,a in zip((tip,start),right_angles)]))
if (right[-1]-root_right).length>1e-7:edges.append(Edge.make_line(right[-1],root_right))
nxt=center+tau-half-invpitch+spherical_involute(base,start)
edges.append(Edge.make_three_point_arc(root_right,point(root,(root_right_angle+nxt)/2),point(root,nxt)))
face=Face(Wire(edges))
root_radius=axial*math.tan(root)
vertices=[v for v in face.vertices() if abs(math.hypot(v.X,v.Y)-root_radius)<1e-5]
# Small concave blends of the actual root intersections, scaled with the cone.
# This is a design rule, not a recovered manufacturer's cutter radius.
if vertices:
radius=.12*(large_diameter/(teeth+2))*axial/apex
face=face.fillet_2d(radius,vertices)
return Sketch([face])
def build(large,small,height,teeth,pressure,twist,root_clearance=0):
count=2 if not twist else max(5,math.ceil(abs(twist)/3)+1)
profiles=[make_profile(height*i/(count-1),large,small,height,teeth,pressure,twist,root_clearance) for i in range(count)]
return loft(profiles,ruled=not bool(twist))
def validate(context,target,params):
large=params.get('large_tip_diameter',0);small=params.get('small_tip_diameter',0);height=params.get('height',0)
teeth=params.get('tooth_count',0);pressure=params.get('pressure_angle',20);twist=params.get('twist_angle',0)
if any(type(v) not in (int,float) or not math.isfinite(v) for v in (large,small,height,pressure,twist)):
return ['BEVEL_PARAMS: 参数必须是有限数值']
if not 0<small<large or height<=0 or type(teeth) is not int or not 8<=teeth<=100:
return ['BEVEL_PARAMS: 大端齿顶径>小端齿顶径>0,高度>0,齿数8–100']
if not 10<=pressure<=35 or abs(twist)>45:
return ['BEVEL_PARAMS: 压力角10–35度,整个齿段转位角绝对值不超过45度']
clearance=params.get('root_clearance',0)
if type(clearance) not in (int,float) or not math.isfinite(clearance) or not 0<=clearance<=.75*large/(teeth+2):
return ['BEVEL_ROOT_CLEARANCE: 大端附加齿根径向让位须为0至0.75倍等效模数的有限毫米值;沿锥面同比减小,不能替代配对校验']
tip=math.atan((large-small)/(2*height))
if not math.radians(5)<=tip<=math.radians(65):
return ['BEVEL_CONE: 由齿顶包络推导的锥半角须在5–65度之间;检查直径差与齿部轴向高度']
pitch=math.atan(math.tan(tip)*teeth/(teeth+2));base=math.asin(math.cos(math.radians(pressure))*math.sin(pitch))
remaining=math.pi/(2*teeth)+spherical_involute(base,pitch)-spherical_involute(base,tip)
if remaining<=1e-4:
return ['BEVEL_TOOTH: 这些参数使齿顶齿厚消失,应调整锥角/齿数/压力角;不能构造自交牙型']
return []
def run(context,target,params):
errors=validate(context,target,params)
if errors:raise ValueError('; '.join(errors))
result=build(params['large_tip_diameter'],params['small_tip_diameter'],params['height'],params['tooth_count'],params.get('pressure_angle',20),params.get('twist_angle',0),params.get('root_clearance',0))
if not result.is_valid or len(result.solids())!=1:raise ValueError('BEVEL_INVALID: 齿坯不是有效单实体')
return result