plg_test.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import ezdxf
  2. import math
  3. # 创建一个新的 DXF 文档
  4. doc = ezdxf.new()
  5. msp = doc.modelspace()
  6. # 定义四边形的四个顶点(这里是一个简单的矩形)
  7. points = [
  8. (0, 0), # 左下角
  9. (10, 0), # 右下角
  10. (10, 5), # 右上角
  11. (0, 5), # 左上角
  12. (0, 0) # 闭合多边形,回到起点
  13. ]
  14. # 创建一个多边形(PLINE,多段线)来表示四边形的边界(这一步是可选的,只是为了可视化)
  15. # boundary = msp.add_lwpolyline(points, close=True)
  16. # boundary.rgb = (254, 254, 254) # 设置为白色,以便与填充颜色对比(可选)
  17. # 创建填充图案(HATCH)
  18. hatch = msp.add_hatch()
  19. # 设置填充图案的类型(例如,SOLID 表示实体填充)
  20. hatch.dxf.pattern_name = "SOLID"
  21. # 设置填充图案的颜色为 RGB(255, 0, 0),即红色
  22. hatch.rgb = (128, 0, 0)
  23. # 添加填充边界(使用之前创建的多边形的顶点)
  24. # 注意:ezdxf 要求边界是由一系列闭合的、共面的、非自交的环组成的
  25. # 在这里,我们只有一个简单的四边形,所以只添加一个环
  26. hatch_boundary_paths = [(point[0], point[1], 0.0) for point in points[:-1]] # 转换为三维点,但 Z 坐标为 0
  27. edge_path = hatch.paths.add_edge_path()
  28. for i in range(3):
  29. edge_path.add_line(hatch_boundary_paths[i], hatch_boundary_paths[i + 1])
  30. edge_path.add_line(hatch_boundary_paths[-1], hatch_boundary_paths[0])
  31. # 保存 DXF 文件
  32. doc.saveas("filled_rectangle.dxf")