48 lines
2.5 KiB
Python
48 lines
2.5 KiB
Python
"""A connected ISO 60-degree external thread, never a round-wire approximation."""
|
|
from build123d import Align, Cone, Cylinder, Pos
|
|
from bd_warehouse.thread import IsoThread
|
|
|
|
|
|
def validate(context, target, params):
|
|
diameter, pitch, length = (params[k] for k in ('major_diameter', 'pitch', 'length'))
|
|
minor_radius = diameter / 2 - 0.5412658773652742 * pitch
|
|
errors = []
|
|
if minor_radius <= 0 or params.get('inner_diameter', 0) / 2 >= minor_radius - .05:
|
|
errors.append('THREAD_WALL: 内孔必须小于牙底直径并保留实体壁厚')
|
|
if length < pitch or length / pitch > 100:
|
|
errors.append('THREAD_LENGTH: 螺纹长度必须为1至100个螺距')
|
|
chamfer = params.get('end_chamfer', 0)
|
|
if chamfer and (2*chamfer >= length or chamfer >= (diameter-params.get('inner_diameter',0))/2):
|
|
errors.append('THREAD_CHAMFER: 引导倒角必须小于长度的一半及端部壁厚')
|
|
return errors
|
|
|
|
|
|
def run(context, target, params):
|
|
length = params['length']
|
|
chamfer = params.get('end_chamfer', 0)
|
|
finish = 'square' if chamfer else params.get('end_finish', 'square')
|
|
thread = IsoThread(params['major_diameter'], params['pitch'], length,
|
|
external=True, hand=params.get('hand', 'right'),
|
|
end_finishes=(finish, finish), simple=False)
|
|
align = (Align.CENTER, Align.CENTER, Align.MIN)
|
|
# Fuse with the thread's actual root radius and its designed overlap.
|
|
core = Cylinder(thread.min_radius, length, align=align)
|
|
result = core.fuse(*thread.solids())
|
|
if result.volume < core.volume - 1e-4:
|
|
raise ValueError("THREAD_FUSION: 融合丢失芯体")
|
|
if chamfer:
|
|
# Clip the OUTER crest envelope with measured 45-degree cones. Never
|
|
# mistake the complete inner rim for a fragmented outer thread edge.
|
|
radius = params['major_diameter']/2
|
|
overrun = min(.1, chamfer/2)
|
|
height = chamfer+overrun
|
|
lower = Pos(0,0,-overrun)*(Cylinder(radius+1,height,align=align)-Cone(radius-chamfer-overrun,radius,height,align=align))
|
|
upper = Pos(0,0,length-chamfer)*(Cylinder(radius+1,height,align=align)-Cone(radius,radius-chamfer-overrun,height,align=align))
|
|
result = result.cut(*lower.solids(), *upper.solids())
|
|
bore = params.get('inner_diameter', 0)
|
|
if bore:
|
|
result = result - Cylinder(bore / 2, length, align=align)
|
|
if not result.is_valid or len(result.solids()) != 1:
|
|
raise ValueError('THREAD_INVALID: 真实螺纹与芯体未形成单一有效实体')
|
|
return result
|