admin管理员组

文章数量:1122846

I've been trying to create a wave spring using Cadquery but the orientation is all wrong, it rotates with the sweep function instead of staying stable.

At this point I have tried all the arguments available in sweep (like normal, aux spine etc.,) but none of them hit the mark.

I initially tried using a rectangle which should be the case for a wave spring but then for simplicity used a circle. As can be seen from the image the orientation of the sweep changes (signalled by the black path line becoming visible then going behind the sweep over spins) regardless of defining aux spine. here is my code:

    import cadquery as cq
    import math
    
    t1 = 0  
    t2 = 50  
    num_points = 500  
    radius = 50
    offset_in_x = 2
    circle_diameter = 4.0  
    circle_radius = circle_diameter / 2
    
    points = []
    aux_points = []
    delta_t = (t2 - t1) / num_points
    for i in range(num_points + 1):
        t = t1 + i * delta_t
        x = radius * math.sin(t)
        y = radius * math.cos(t)
        z = 7 * math.sin(3.5 * t) + 3 * t
        points.append((x, y, z))
        aux_points.append((x + offset_in_x, y, z+offset_in_x))
    
    wave_path = cq.Workplane("XY").spline(points)
    aux_spine = cq.Workplane("XY").spline(aux_points)
    
    
    result = (
        cq.Workplane("YZ")
        .center(radius, 0)
        .circle(circle_radius)
        .sweep(wave_path, isFrenet=False, auxSpine=aux_spine)
    )
    
    show_object(result, name="WaveSpring")

Example Image

I've been trying to create a wave spring using Cadquery but the orientation is all wrong, it rotates with the sweep function instead of staying stable.

At this point I have tried all the arguments available in sweep (like normal, aux spine etc.,) but none of them hit the mark.

I initially tried using a rectangle which should be the case for a wave spring but then for simplicity used a circle. As can be seen from the image the orientation of the sweep changes (signalled by the black path line becoming visible then going behind the sweep over spins) regardless of defining aux spine. here is my code:

    import cadquery as cq
    import math
    
    t1 = 0  
    t2 = 50  
    num_points = 500  
    radius = 50
    offset_in_x = 2
    circle_diameter = 4.0  
    circle_radius = circle_diameter / 2
    
    points = []
    aux_points = []
    delta_t = (t2 - t1) / num_points
    for i in range(num_points + 1):
        t = t1 + i * delta_t
        x = radius * math.sin(t)
        y = radius * math.cos(t)
        z = 7 * math.sin(3.5 * t) + 3 * t
        points.append((x, y, z))
        aux_points.append((x + offset_in_x, y, z+offset_in_x))
    
    wave_path = cq.Workplane("XY").spline(points)
    aux_spine = cq.Workplane("XY").spline(aux_points)
    
    
    result = (
        cq.Workplane("YZ")
        .center(radius, 0)
        .circle(circle_radius)
        .sweep(wave_path, isFrenet=False, auxSpine=aux_spine)
    )
    
    show_object(result, name="WaveSpring")

Example Image

Share Improve this question asked yesterday HamzaHamza 11 bronze badge New contributor Hamza is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

1 Answer 1

Reset to default 0

You can use lofting. Instead of sweeping a single profile, multiple rectangular profiles are created at intervals along the path and lifted together. This ensures consistent orientation without unexpected twists or rotations.

You can modify wave_amplitude, wave_frequency, and general path points for different spring designs.

import cadquery as cq
import math

# Parameters for wave spring
t1 = 0
t2 = 50
num_points = 100  # Lower number since we're lofting
radius = 50
wave_amplitude = 7.0  # Height of wave
wave_frequency = 3.5  # How fast the wave oscillates
rect_width = 2.0  # Thickness of the spring
rect_height = 4.0  # Height of the rectangle profile

# Create points for the wavy path
points = []
delta_t = (t2 - t1) / num_points
for i in range(num_points + 1):
    t = t1 + i * delta_t
    x = radius * math.sin(t)
    y = radius * math.cos(t)
    z = wave_amplitude * math.sin(wave_frequency * t) + 3 * t
    points.append((x, y, z))

# Create plane profiles (rectangles) along the path
profiles = []
for point in points:
    x, y, z = point
    profile = cq.Workplane("XY").center(x, y).rect(rect_width, rect_height).toPending()
    profiles.append(profile)

result = cq.Workplane()
result = result.add(profile.loft(combine=True))

show_object(result)

本文标签: pythonCadQuery Orientation issues with Sweep FunctionStack Overflow