generate.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env python3
  2. from stl import mesh
  3. files = ["steamvrthread.stl", "steamvrtracker.stl"]
  4. scadFiles = ["threads.scad"]
  5. fileData = {}
  6. scadFileData = {}
  7. def getVertexId(vertices, vertex):
  8. i = 0
  9. for v in vertices:
  10. if v[0] == vertex[0] and v[1] == vertex[1] and v[2] == vertex[2]:
  11. return i
  12. i += 1
  13. return -1
  14. # 3D meshes
  15. for file in files:
  16. current = mesh.Mesh.from_file(file)
  17. vertices = []
  18. triangles = []
  19. vId = 0
  20. for triangle in current.points:
  21. t = [0, 0, 0]
  22. point = 0
  23. for i in [0, 3, 6]:
  24. v = (triangle[i], triangle[i + 1], triangle[i + 2])
  25. vertexId = getVertexId(vertices, v)
  26. if vertexId >= 0:
  27. t[point] = vertexId
  28. else:
  29. t[point] = vId
  30. vertices.append(v)
  31. vId += 1
  32. point += 1
  33. triangles.append(t)
  34. id = file[0:-4]
  35. fileData[id] = (vertices, triangles)
  36. with open("resources.hpp", "w") as f:
  37. f.write("#pragma once\n");
  38. f.write("// AUTOGENERATED FILE - DO NOT EDIT\n");
  39. for key, data in fileData.items():
  40. vString = "{"
  41. tString = "{"
  42. vLength = 0
  43. tLength = 0
  44. first = True
  45. for vertex in data[0]:
  46. for v in vertex:
  47. if not first:
  48. vString += ", "
  49. first = False
  50. vString += str(v)
  51. vLength += 1
  52. first = True
  53. for triangle in data[1]:
  54. for t in triangle:
  55. if not first:
  56. tString += ", "
  57. first = False
  58. tString += str(t)
  59. tLength += 1
  60. vString += "}"
  61. tString += "}"
  62. f.write("const float " + key + "_VERTICES[" + str(vLength) + "] " + vString + ";\n");
  63. f.write("const unsigned int " + key + "_TRIANGLES[" + str(tLength) + "] " + tString + ";\n");
  64. # OpenSCAD resources
  65. with open("scad.hpp", "w") as out:
  66. out.write("#pragma once\n");
  67. out.write("// AUTOGENERATED FILE - DO NOT EDIT\n");
  68. for file in scadFiles:
  69. filename = file[0:-5]
  70. out.write("const char* " + filename + "_SCAD = ")
  71. with open(file, "r") as f:
  72. isMultiComment = False
  73. for line in f.readlines():
  74. if line.strip().startswith("/*"):
  75. isMultiComment = True
  76. if not line.strip().startswith("//") and not isMultiComment and not len(line.strip()) == 0:
  77. l = line.strip().replace('"', '\\"')
  78. out.write("\"" + l + "\\n\"\n")
  79. if line.strip().endswith("*/"):
  80. isMultiComment = False
  81. out.write(";");