Advertisement
Guest User

Extrude circle along a path - output stl file

a guest
Oct 18th, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.21 KB | None | 0 0
  1. # install euclid and numpy packages
  2. from euclid import Vector3, Matrix4
  3. import numpy as np
  4. import math
  5.  
  6. execfile("stl-writer.py")
  7.  
  8. # number of points on the circle
  9. PRECISE=100
  10. # number of circles to be placed across the path
  11. NUM_CIRCLES = 500
  12.  
  13. # connect corresponding points between two instances of same polygon
  14. def get_mesh(poly1, poly2, precision):
  15.   faces = []
  16.   for i in range(precision):
  17.     p1 = i
  18.     p2 = (i + 1) % precision
  19.     face = [poly1[p1], poly1[p2], poly2[p2], poly2[p1]]
  20.     faces.append(face)
  21.     # print str(i) + " " + str(face)
  22.   # print faces
  23.   return faces
  24.  
  25.  
  26. # draw a catmull spline
  27. def spline_4p(t, p_1, p0, p1, p2 ):
  28.     """ Catmull-Rom
  29.        (Ps can be numpy vectors or arrays too: colors, curves ...)
  30.    """
  31.         # wikipedia Catmull-Rom -> Cubic_Hermite_spline
  32.         # 0 -> p0,  1 -> p1,  1/2 -> (- p_1 + 9 p0 + 9 p1 - p2) / 16
  33.     # assert 0 <= t <= 1
  34.     return (
  35.           t*((2-t)*t - 1)     * p_1
  36.         + (t*t*(3*t - 5) + 2) * p0
  37.         + t*((4 - 3*t)*t + 1) * p1
  38.         + (t-1)*t*t           * p2 ) / 2
  39.  
  40.  
  41. # control points, note first and last points are repeated
  42. P = np.array([[0, 0, 0], [0, 0, 0], [1, 1, 2], [0.5, 0, 3], [-1, 2, 4], [-0.5, 0, 6], [-0.5, 0, 6]])
  43.  
  44. # calculate the path with NUM_CIRCLE points along it
  45. path = np.array(P[0])
  46. k=NUM_CIRCLES/(len(P) - 3)
  47. print k
  48. for j in range( 1, len(P)-2 ):  # skip the ends
  49.     for t in range(1,k):  # t: 0 .1 .2 .. .9
  50.         p = spline_4p( 1.0*t/k, P[j-1], P[j], P[j+1], P[j+2] )
  51.         path = np.vstack((path, p))
  52.  
  53. #path = np.vstack((path, P[2:4]))
  54.  
  55. cylinder = []
  56.  
  57. for i in range(3,NUM_CIRCLES-3):
  58.   shape1 = [Vector3(math.cos(x) + path[i-1][0], math.sin(x) + path[i-1][1], path[i-1][2]) for x in np.linspace(0,2*math.pi,PRECISE,endpoint=False)]
  59.   shape2 = [Vector3(math.cos(x) + path[i][0], math.sin(x) + path[i][1], path[i][2]) for x in np.linspace(0,2*math.pi,PRECISE,endpoint=False)]
  60.   faces = get_mesh(shape1, shape2, PRECISE)
  61.   cylinder.extend(faces)
  62.  
  63. with open('cylinder.stl', 'wb') as fp:
  64.   writer = ASCII_STL_Writer(fp)
  65.   faces = []
  66.   for face in cylinder:
  67.     faces.append([face[0].xyz, face[1].xyz, face[2].xyz, face[3].xyz])
  68.   #print faces
  69.   writer.add_faces(faces)
  70.   writer.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement