ShaftDrawer.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import math
  2. import time
  3. from core import rotate_point_around_another
  4. from ezdxf import math as mt
  5. import ezdxf
  6. class ShaftDrawer:
  7. def __init__(self, msp, gap, center, route, color):
  8. self.msp = msp
  9. self.gap = gap
  10. self.center = center
  11. self.route = route
  12. self.color = color
  13. def calculate_dodecagon_points(self):
  14. # 存储结果的列表
  15. points = []
  16. points_rotated = []
  17. Cx, Cy = self.center
  18. r = 1 / 2 * self.gap
  19. # 正十二边形有12个顶点,每个顶点间隔30度
  20. for i in range(12):
  21. # 将角度从度转换为弧度
  22. theta = math.radians(30 * i)
  23. # 计算x和y坐标
  24. x = Cx + r * math.cos(theta)
  25. y = Cy + r * math.sin(theta)
  26. # 将坐标添加到列表中
  27. points.append((x, y))
  28. return points
  29. def calculate_outside_octagon_points(self):
  30. # 存储结果的列表
  31. points = []
  32. Cx, Cy = self.center
  33. r = 1 / 4 * self.gap
  34. # 正八边形有8个顶点,每个顶点间隔45度
  35. for i in range(8):
  36. # 将角度从度转换为弧度
  37. theta = math.radians(45 * i)
  38. # 计算x和y坐标
  39. x = Cx + r * math.cos(theta)
  40. y = Cy + r * math.sin(theta)
  41. x_y_ = rotate_point_around_another((x, y), self.center, math.radians(12.5))
  42. # 将坐标添加到列表中
  43. points.append(x_y_)
  44. return points
  45. def calculate_inner_octagon_points(self):
  46. # 存储结果的列表
  47. points = []
  48. Cx, Cy = self.center
  49. r = 1 / 8 * self.gap
  50. # 正八边形有8个顶点,每个顶点间隔45度
  51. for i in range(8):
  52. # 将角度从度转换为弧度
  53. theta = math.radians(45 * i)
  54. # 计算x和y坐标
  55. x = Cx + r * math.cos(theta)
  56. y = Cy + r * math.sin(theta)
  57. # 将坐标添加到列表中
  58. points.append((x, y))
  59. return points
  60. def draw_polygon(self, points):
  61. i = 0
  62. while i < len(points) - 1:
  63. self.msp.add_line(points[i], points[i + 1], {"color": self.color})
  64. i = i + 1
  65. self.msp.add_line(points[-1], points[0], {
  66. "color": self.color,
  67. })
  68. def draw_shaft_drawer(self):
  69. points12 = self.calculate_dodecagon_points()
  70. self.draw_polygon(points12)
  71. points8_out = self.calculate_outside_octagon_points()
  72. self.draw_polygon(points8_out)
  73. points8_in = self.calculate_inner_octagon_points()
  74. self.draw_polygon(points8_in)
  75. # for i,points8 in enumerate(points8_in):
  76. # msp.add_text(text=str(i),dxfattribs={'insert':points8})
  77. carc = mt.arc.ConstructionArc()
  78. b = carc.from_3p(start_point=points8_in[1], def_point=points8_in[3], end_point=points8_in[5])
  79. self.msp.add_arc(center=b.center, radius=b.radius, start_angle=b.start_angle, end_angle=b.end_angle)
  80. hatch = self.msp.add_hatch(color=self.color)
  81. edge_path = hatch.paths.add_edge_path()
  82. edge_path.add_arc(center=b.center, radius=b.radius, start_angle=b.start_angle, end_angle=b.end_angle)
  83. if __name__ == '__main__':
  84. doc = ezdxf.new()
  85. msp = doc.modelspace()
  86. sd = ShaftDrawer(msp, 1000, (10, 10), 10)
  87. sd.draw_shaft_drawer()
  88. doc.saveas(f'shaft{str(time.time())}.dxf')