Advertisement
Guest User

Create sphere in STL file

a guest
Jun 12th, 2017
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.28 KB | None | 0 0
  1.  
  2. import math
  3.  
  4. out = "sphere.stl"
  5.  
  6. f = open(out, 'w')
  7.  
  8. n = 10
  9. m = 20
  10.  
  11.  
  12. dtheta = 2.0 * math.pi / m
  13. dphi = 1.0 * math.pi / n
  14.  
  15. f.write("solid Something (Meshed)\n")
  16. for i in range(0, m):
  17.     theta = 2.0 * math.pi * i * 1.0 / m
  18.     ct = math.cos(theta)
  19.     st = math.sin(theta)
  20.     ct1 = math.cos(theta + dtheta)
  21.     st1 = math.sin(theta + dtheta)
  22.     for j in range(0, n):
  23.         phi = math.pi * j * 1.0 / n - 0.5 * math.pi
  24.         cp = math.cos(phi)
  25.         sp = math.sin(phi)
  26.         cp1 = math.cos(phi + dphi)
  27.         sp1 = math.sin(phi + dphi)
  28.         f.write("\tfacet normal %.6f %.6f %.6f\n" % (ct * cp, st * cp, sp))
  29.         f.write("\t\touter loop\n")
  30.         f.write("\t\t\tvertex %.6f %.6f %.6f\n" % (ct * cp, st * cp, sp))
  31.         f.write("\t\t\tvertex %.6f %.6f %.6f\n" % (ct * cp1, st * cp1, sp1))
  32.         f.write("\t\t\tvertex %.6f %.6f %.6f\n" % (ct1 * cp, st1 * cp, sp))
  33.         f.write("\t\tendloop\n")
  34.         f.write("\tendfacet\n")
  35.         f.write("\tfacet normal %.6f %.6f %.6f\n" % (ct * cp, st * cp, sp))
  36.         f.write("\t\touter loop\n")
  37.         f.write("\t\t\tvertex %.6f %.6f %.6f\n" % (ct1 * cp1, st1 * cp1, sp1))
  38.         f.write("\t\t\tvertex %.6f %.6f %.6f\n" % (ct * cp1, st * cp1, sp1))
  39.         f.write("\t\t\tvertex %.6f %.6f %.6f\n" % (ct1 * cp, st1 * cp, sp))
  40.         f.write("\t\tendloop\n")
  41.         f.write("\tendfacet\n")
  42.    
  43. f.write("endsolid Mesh\n")
  44.  
  45. f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement