Creating a Cylinder Between Two Points



Shows how to create a plane with a specific normal and project 3D points to 2D sketches.


# creates a cylinder between two arbitrary points
 
from math import sqrt
 
# ends of cylinder are centered on these points
cyl_p1 = [1, 5, 2]
cyl_p2 = [10, 14, 8]
 
# diameter of cylinder
diameter = 6
 
# get length of cynlinder using euclidean distance
length = sqrt((cyl_p2[0] - cyl_p1[0])**2 + (cyl_p2[1] - cyl_p1[1])**2 + (cyl_p2[2] - cyl_p1[2])**2)
 
# calculate normal vector for the plane at the first end of the cylinder
normal_vector = [cyl_p2[0] - cyl_p1[0], cyl_p2[1] - cyl_p1[1], cyl_p2[2] - cyl_p1[2]]
 
# create part
P = Part('Cylinder')
 
# create plane for one end of the cylinder
cyl_plane = P.AddPlane('Cyl Start Plane', normal_vector, cyl_p1)
 
P.AddAxis('Cylinder Axis', cyl_p1, cyl_p2)
 
# draw a circle on the plane
S = P.AddSketch('Cylinder End', cyl_plane)
[cx, cy] = S.GlobaltoPoint(cyl_p1[0], cyl_p1[1], cyl_p1[2])
S.AddCircle(cx, cy, diameter, False)
 
# extrude into a cylinder
P.AddExtrudeBoss('Cylinder', S, length, False)


Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.