cadquery创建螺纹thread

发布时间 2023-10-22 16:13:18作者: Arxu
参考来源: https://github.com/CadQuery/cadquery/issues/407

import math import cadquery as cq def profile(base, pitch, h, extra=True): """
pitch 螺距 Creates a trapezoidal wire for the cross section of the thread. If the cross section only goes out to the major diameter, then it doesn't cut a cylinder for some reason. Set extra=True to extend the cross section beyond the major diameter, to be certain it clears the outer surface of the base cylinder. """ vertices = [ base + cq.Vector(-5 / 8 * h, 0, -pitch / 8) , base + cq.Vector(0, 0, -7 / 16 * pitch) ] if extra: vertices.extend([ base + cq.Vector(h, 0, -7 / 16 * pitch) , base + cq.Vector(h, 0, 7 / 16 * pitch) ]) vertices.extend([ base + cq.Vector(0, 0, 7 / 16 * pitch) , base + cq.Vector(-5 / 8 * h, 0, pitch / 8) ]) vertices.append(vertices[0]) return vertices def thread_solid(pitch=3.5, major_diam=30+1e-4, length=10, included_angle=60, offset=0): """ Makes a solid representing a metric thread. Can be unioned to a tube with ID == major_diam to make an internal thread, or cut from a cylinder with OD == major_diam for an external thread. If offset != 0, then the profile is offset before sweeping, which can be used to add clearance. """ h = pitch / (2 * math.tan(included_angle / 2 * math.pi / 180)) base = cq.Vector(major_diam / 2, 0, 0) vertices = profile(base, pitch, h) profile_wire = cq.Wire.makePolygon(vertices) if offset: profile_wire = profile_wire.offset2D(offset)[0] # offset2d returns a list, we need the one and only wire returned # make the helical path to sweep path = cq.Wire.makeHelix(pitch, length + 2 * pitch, major_diam / 2, center=cq.Vector(0, 0, -pitch)) thread = cq.Solid.sweep(profile_wire, [], path, isFrenet=True) print(thread.isValid()) return thread thread = thread_solid(length=20) screw = ( cq .Workplane(origin=(0, 0, -5)) # make sure the thread cutting tool extends below the screw .circle(30 / 2) .extrude(30) # make sure the thread cutting toold extends above the screw .cut(thread,clean=False) ) screw = ( screw .faces('<Z').workplane(offset=-10).split(False,True) .faces('<Z').workplane(offset=-10).split(True) )
  1. profile 函数:

    • profile 函数用于创建螺纹横截面的轮廓。它的参数包括 base(基准点),pitch(螺距),h(梯形的高度)和 extra(一个布尔值,控制是否扩展轮廓以确保清除基础圆柱体的外表面)。
    • 该函数计算并返回一个包含横截面轮廓顶点坐标的列表,这些顶点定义了梯形的形状。
  2. thread_solid 函数:

    • thread_solid 函数用于创建表示度量螺纹的实体。可以将这个实体与具有内径等于 major_diam 的管道进行合并,以创建内部螺纹,或者从具有外径等于 major_diam 的圆柱体中进行切割,以创建外部螺纹。offset 参数用于控制轮廓是否在扫描之前进行偏移以添加间隙。
    • 该函数使用 profile 函数创建螺纹的横截面轮廓,然后使用 cq.Wire.makeHelix 创建一个螺旋路径,最后使用 Solid.sweep 函数将横截面轮廓沿着螺旋路径进行扫描以创建螺纹实体。
  3. 在接下来的代码部分,螺纹实体被切割以创建一个螺纹工具。切割工具由一个圆柱体组成,用于在后续步骤中切割螺纹。

  4. 最后,使用 cq.Workplane 创建一个工作平面,该平面从螺纹工具的底部开始,然后在螺纹工具上创建一个圆形。接着,使用 extrude 创建一个立体图形,然后使用 cut 将这个立体图形切割螺纹,从而创建一个带有螺纹的螺栓。接着对螺栓进行分割操作以将其切成一定长度。