工具箱相关
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

DMzj.py 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. from pyautocad import Autocad, APoint, aDouble
  2. import os
  3. from posixpath import dirname
  4. import xlrd
  5. import tkinter as tk
  6. from tkinter import filedialog as tkFileDialog
  7. from tkinter import messagebox
  8. import shutil
  9. def start():
  10. window = tk.Tk() # 创建窗口对象的背景色
  11. window.title('断面名注记程序') # 设置窗口的标题
  12. window.geometry('400x250') # 设置窗口的大小
  13. title = tk.Label(window, text='断面名注记程序',
  14. font=('微软雅黑', 12), width=40, height=3)
  15. title.place(x=0, y=0, anchor='nw')
  16. choosepath = tk.Label(window, text='选择表格文件夹:',
  17. font=('微软雅黑', 12), width=20, height=2)
  18. choosepath.place(x=0, y=50, anchor='nw')
  19. path_var1 = tk.StringVar() # 输入路径
  20. entry1 = tk.Entry(window, textvariable=path_var1)
  21. entry1.place(x=170, y=65, anchor='nw')
  22. resultpath = tk.Label(window, text='选择dwg:',
  23. font=('微软雅黑', 12), width=20, height=2)
  24. resultpath.place(x=10, y=100, anchor='nw')
  25. path_var2 = tk.StringVar() # 输出路径
  26. entry2 = tk.Entry(window, textvariable=path_var2)
  27. entry2.place(x=170, y=115, anchor='nw')
  28. lebel1 = tk.Label(window, text='请确保CAD已经打开。',
  29. font=('微软雅黑', 10), width=40, height=2)
  30. lebel1.place(x=0, y=150, anchor='nw')
  31. def choose1():
  32. file_dir1 = tkFileDialog.askdirectory()
  33. path_var1.set(file_dir1)
  34. def choose2():
  35. file_dir2 = tkFileDialog.askopenfilename(filetypes=[('DWG', '*.dwg'), ('DWG', '*.DWG'),('All Files', '*')])
  36. path_var2.set(file_dir2)
  37. def tuichu():
  38. window.destroy()
  39. def main():
  40. excelpath = entry1.get()
  41. dwgpath = entry2.get()
  42. #遍历文件,得到点的坐标和注记内容
  43. list1 = []
  44. for parent,dirname,filenames in os.walk(excelpath):
  45. for filename in filenames:
  46. #排一下其他文件
  47. if '.xls' in filename:
  48. #得到表格的完整路径
  49. filepath = parent + '/' +filename
  50. #拆分出断面名字dmm
  51. dmm = filename.split('.',-1)[0]
  52. #只取excel的第一个端点
  53. xlr = xlrd.open_workbook(filepath)
  54. table = xlr.sheets()[0]
  55. list2 = []
  56. list2.append(dmm)
  57. list2.append(table.cell(1, 1).value)
  58. list2.append(table.cell(1, 2).value)
  59. list1.append(list2)
  60. #打开cad
  61. #首先链接cad
  62. acad = Autocad(create_if_not_exists=True)
  63. acad.Application.Documents.Open(dwgpath)
  64. # 当前文件模型空间中所包含的图层总数
  65. layernums = acad.ActiveDocument.Layers.count
  66. # 获取指定图层索引号
  67. layernames = [acad.ActiveDocument.Layers.Item(i).Name for i in range(layernums)]
  68. index = layernames.index("DMX")
  69. # 将指定图层设定当前
  70. acad.ActiveDocument.ActiveLayer = acad.ActiveDocument.Layers.Item(index)
  71. # 获取当前文档
  72. doc = acad.doc
  73. # 获取模型空间
  74. msp = doc.ModelSpace
  75. #先写上去,再看细节怎么调
  76. # 定义文本的插入点坐标
  77. ii = 0
  78. while ii < len(list1):
  79. # 定义文本的插入点坐标
  80. insertion_point = APoint(list1[ii][1] - 1, list1[ii][2] + 1)
  81. # 定义文本的高度和样式
  82. text_height = 2.0
  83. # 插入单行文本
  84. text = msp.AddText(list1[ii][0], insertion_point, text_height)
  85. ii = ii + 1
  86. #关闭当前
  87. acad.doc.Close()
  88. messagebox.showinfo("消息","运行成功")
  89. window.destroy()
  90. tk.Button(window, text='选择', command=choose1).place(x=320, y=60, anchor='nw')
  91. tk.Button(window, text='选择', command=choose2).place(x=320, y=110, anchor='nw')
  92. tk.Button(window,text='确认',command=main).place(x=120,y=200,anchor='nw')
  93. tk.Button(window,text='取消',command=tuichu).place(x=220,y=200,anchor='nw')
  94. window.mainloop()
  95. if __name__ == '__main__':
  96. start()