工具箱相关
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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('400x300') # 设置窗口的大小
  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='选择输出文件夹:',
  23. font=('微软雅黑', 12), width=20, height=2)
  24. resultpath.place(x=0, 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. oripath = tk.Label(window, text='选择原始模板:',
  29. font=('微软雅黑', 12), width=20, height=2)
  30. oripath.place(x=0, y=150, anchor='nw')
  31. path_var3 = tk.StringVar() # 输出路径
  32. entry3 = tk.Entry(window, textvariable=path_var3)
  33. entry3.place(x=170, y=165, anchor='nw')
  34. lebel1 = tk.Label(window, text='请确保CAD已经打开。',
  35. font=('微软雅黑', 10), width=40, height=2)
  36. lebel1.place(x=0, y=190, anchor='nw')
  37. def choose1():
  38. file_dir1 = tkFileDialog.askdirectory()
  39. path_var1.set(file_dir1)
  40. def choose2():
  41. file_dir2 = tkFileDialog.askdirectory()
  42. path_var2.set(file_dir2)
  43. def choose3():
  44. file_dir3 = tkFileDialog.askopenfilename(filetypes=[('DWG', '*.dwg'), ('DWG', '*.DWG'),('All Files', '*')])
  45. path_var3.set(file_dir3)
  46. def tuichu():
  47. window.destroy()
  48. def main():
  49. excelpath = entry1.get()
  50. outpath = entry2.get()
  51. ori = entry3.get()
  52. #首先链接cad
  53. acad = Autocad(create_if_not_exists=True)
  54. #遍历文件夹每个表格
  55. for parent,dirname,filenames in os.walk(excelpath):
  56. for filename in filenames:
  57. #排一下其他文件
  58. if '.xls' in filename:
  59. #得到表格的完整路径
  60. filepath = parent + '/' +filename
  61. #拆分出断面名字dmm
  62. dmm = filename.split('.',-1)[0]
  63. #先复制,再打开,再另存覆盖?
  64. #判断下存在就删除
  65. outfpath = outpath + '\\' + dmm + '号断面.dwg'
  66. if os.path.exists(outfpath):
  67. os.remove(outfpath)
  68. shutil.copy(ori, outfpath)
  69. acad.Application.Documents.Open(outfpath)
  70. #打开表格,从第二行开始读
  71. xlr = xlrd.open_workbook(filepath)
  72. table = xlr.sheets()[0]
  73. row = table.nrows
  74. #设置一条二位多段线line
  75. line1 = []
  76. ii = 1
  77. while ii < row:
  78. #第一个点只需要z值,那么把断面名之类的信息也放在这里
  79. if ii == 1:
  80. #两个标题的修改
  81. text1 = list(acad.iter_objects("Text"))
  82. for t1 in text1:
  83. if '号断面图' in t1.TextString:
  84. t1.TextString = dmm + '号断面图'
  85. if '断面名称' in t1.TextString:
  86. t1.TextString = '断面名称:' + dmm
  87. if t1.TextString == '左端点':
  88. t2 = t1
  89. #Z值
  90. z1 = float(table.cell(ii, 3).value)
  91. #计算方式,(z-3400)*5
  92. num1 = z1 - 3400
  93. num2 = num1 * 10
  94. #设置dmlist[x,y,z]
  95. pt1 = [0,num2,0]
  96. line1 = line1 + pt1
  97. ii = ii + 1
  98. else:
  99. #判定下是否有值,没有就跳过
  100. try:
  101. #首先是文字
  102. str1 = table.cell(ii, 4).value
  103. dis1 = float(table.cell(ii, 5).value)
  104. #计算公式2 * dis1
  105. num3 = dis1 * 10
  106. #复制
  107. copy = t2.Copy()
  108. #移动
  109. center = APoint(0,-19.9995,0)
  110. pt = APoint(num3,-19.9995,0)
  111. copy.move(center,pt)
  112. #重新赋值
  113. copy.TextString = str1
  114. #然后是线
  115. #Z值
  116. z2 = float(table.cell(ii, 3).value)
  117. #计算方式,(z-3400)*5
  118. num4 = z2 - 3400
  119. num5 = num4 * 10
  120. #设置dmlist[x,y,z]
  121. pt2 = [num3,num5,0]
  122. line1 = line1 + pt2
  123. ii = ii + 1
  124. except:
  125. ii = ii + 1
  126. #把线绘制出来
  127. pnts = aDouble(line1)
  128. plineObj = acad.model.AddPolyLine(pnts)
  129. #指定图层
  130. plineObj.Layer = "DMX"
  131. # #另存为
  132. # acad.doc.SaveAs(outfpath)
  133. #关闭当前
  134. acad.doc.Close()
  135. print(dmm + ' FINISH')
  136. messagebox.showinfo("消息","运行成功")
  137. window.destroy()
  138. tk.Button(window, text='选择', command=choose1).place(x=320, y=60, anchor='nw')
  139. tk.Button(window, text='选择', command=choose2).place(x=320, y=110, anchor='nw')
  140. tk.Button(window, text='选择', command=choose3).place(x=320, y=160, anchor='nw')
  141. tk.Button(window,text='确认',command=main).place(x=120,y=230,anchor='nw')
  142. tk.Button(window,text='取消',command=tuichu).place(x=220,y=230,anchor='nw')
  143. window.mainloop()
  144. if __name__ == '__main__':
  145. start()