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

HBhhdmtable.py 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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='选择dwg文件夹:',
  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. lebel1 = tk.Label(window, text='请确保CAD已经打开。',
  29. font=('微软雅黑', 10), width=40, height=2)
  30. lebel1.place(x=0, y=145, anchor='nw')
  31. def choose1():
  32. file_dir1 = tkFileDialog.askdirectory()
  33. path_var1.set(file_dir1)
  34. def choose2():
  35. file_dir2 = tkFileDialog.askdirectory()
  36. path_var2.set(file_dir2)
  37. def tuichu():
  38. window.destroy()
  39. def main():
  40. excelpath = entry1.get()
  41. outpath = entry2.get()
  42. #首先链接cad
  43. acad = Autocad(create_if_not_exists=True)
  44. mm = 0
  45. #遍历文件夹每个表格
  46. for parent,dirname,filenames in os.walk(excelpath):
  47. for filename in filenames:
  48. #排一下其他文件
  49. if '.dwg' in filename:
  50. #得到表格的完整路径
  51. filepath = parent + '/' +filename
  52. #拆分出断面名字dmm
  53. dmm = filename.split('.',-1)[0]
  54. #先复制,再打开,再另存覆盖?
  55. #判断下存在就删除
  56. #第一个点只需要z值,那么把断面名之类的信息也放在这里
  57. if mm == 0:
  58. outfpath = outpath + '\\合并断面.dwg'
  59. if os.path.exists(outfpath):
  60. os.remove(outfpath)
  61. shutil.copy(filepath, outfpath)
  62. acad.Application.Documents.Open(outfpath)
  63. target_doc = acad.ActiveDocument
  64. else:
  65. acad.Application.Documents.Open(filepath)
  66. doc = acad.ActiveDocument
  67. #把所有文字全部踢出来
  68. handle_string = 'select'
  69. text1 = list(acad.iter_objects(["Text", "Line","Circle"]))
  70. for t1 in text1:
  71. handle_string += ' (handent "'+t1.Handle+'")'
  72. #命令
  73. handle_string += '\n\n'
  74. doc.SendCommand(handle_string)
  75. #移动的距离做算法
  76. num1 = (1960 * mm) - 170
  77. str1 = 'move\n-170,-180,0\n' + str(num1) + ',-180,0\n'
  78. #移动
  79. # doc.SendCommand('move\n-80,-140,0\n760,-140,0\n')
  80. doc.SendCommand(str1)
  81. #再次选择
  82. doc.SendCommand(handle_string)
  83. #复制
  84. doc.SendCommand('copybase\n-170,-180,0\n')
  85. # target_doc.SendCommand('PASTEORIG\n')
  86. target_doc.SendCommand('PASTEORIG\n')
  87. #但不保存
  88. doc.Close(False)
  89. # #另存为
  90. # acad.doc.SaveAs(outfpath)
  91. #关闭当前
  92. print(dmm + ' FINISH')
  93. mm = mm + 1
  94. #最后保存
  95. target_doc.Close()
  96. messagebox.showinfo("消息","运行成功")
  97. window.destroy()
  98. tk.Button(window, text='选择', command=choose1).place(x=320, y=60, anchor='nw')
  99. tk.Button(window, text='选择', command=choose2).place(x=320, y=110, anchor='nw')
  100. tk.Button(window,text='确认',command=main).place(x=120,y=185,anchor='nw')
  101. tk.Button(window,text='取消',command=tuichu).place(x=220,y=185,anchor='nw')
  102. window.mainloop()
  103. if __name__ == '__main__':
  104. start()