CadQuery / cadquery

A python parametric CAD scripting framework based on OCCT
https://cadquery.readthedocs.io
Other
2.95k stars 276 forks source link

Sweep circle in the normal direction #1092

Closed RemDelaporteMathurin closed 2 years ago

RemDelaporteMathurin commented 2 years ago
import cadquery as cq

pts = [
    (1, 1, 1),
    (1, 2, 2),
    (1, 3, 3),
]
p = cq.Workplane("XY").spline(pts, includeCurrent=True)
s = cq.Workplane("XY").circle(1).sweep(p)

Based on #296 , I would like to have the extrusion of the circle in the normal direction only (the idea is to make a classic tube from a set of points).

Unfortunately, this code produces a distorded tube:

image

Thanks for the help!

Jojain commented 2 years ago

You need to have the normal of your circle aligned with the tangent of your spline at the start :

pts = [
    (1, 1, 1),
    (1, 2, 2),
    (1, 3, 3),
]
p = cq.Workplane("XY").spline(pts, includeCurrent=True)
dire = p.val().tangentAt(0)
plane = cq.Plane((0,0,0), normal=dire)
s = cq.Workplane(plane).circle(1).sweep(p)
RemDelaporteMathurin commented 2 years ago

Thanks for the hint!

Jojain commented 2 years ago

Note that this is true for any curve (even straight lines) not only splines

RemDelaporteMathurin commented 2 years ago

Noted!

I tried to apply it to a more complex curve composed of a straight bit and a semi-circle but I have some weird results. Is it related?

import cadquery as cq
from cadquery import exporters
import numpy as np

target_radius = 50
L = 200
inner_radius = 6

pts = []

# line points
for y_loc in np.linspace(0, L, num=10):
    pts.append((0, 0, y_loc))

# curve points
for theta in np.linspace(0.5 * np.pi, 0 * np.pi, num=50):
    pts.append(
        (
            0,
            target_radius + target_radius * np.cos(theta),
            L + target_radius * np.sin(theta),
        )
    )

p = cq.Workplane("ZX").spline(pts[1:], includeCurrent=True)
dire = p.val().tangentAt(0)
plane = cq.Plane((0, 0, 0), normal=dire)
s = cq.Workplane(plane).circle(inner_radius).sweep(p)
exporters.export(s, "out.stl")

image

jmwright commented 2 years ago

@RemDelaporteMathurin If what you're displaying is the exported STL, you may need to adjust your tolerance settings on the export call. Does it display fine in CQ-editor?

RemDelaporteMathurin commented 2 years ago

@RemDelaporteMathurin If what you're displaying is the exported STL, you may need to adjust your tolerance settings on the export call. Does it display fine in CQ-editor?

Yes this is the STL. I'll play with the tolerances then! Thanks! I didn't use the CQ-editor I'm afraid

jmwright commented 2 years ago

You could export a STEP and use an online viewer for a double check too.