ShaftDrawer.py 3.6 KB

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