123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- from pyautocad import Autocad, APoint, aDouble
- import os
- from posixpath import dirname
- import xlrd
- import tkinter as tk
- from tkinter import filedialog as tkFileDialog
- from tkinter import messagebox
- import shutil
-
- def start():
- window = tk.Tk() # 创建窗口对象的背景色
- window.title('断面线图表合并程序') # 设置窗口的标题
- window.geometry('400x250') # 设置窗口的大小
- title = tk.Label(window, text='断面线图表合并程序',
- font=('微软雅黑', 12), width=40, height=3)
- title.place(x=0, y=0, anchor='nw')
- choosepath = tk.Label(window, text='选择dwg文件夹:',
- font=('微软雅黑', 12), width=20, height=2)
- choosepath.place(x=0, y=50, anchor='nw')
- path_var1 = tk.StringVar() # 输入路径
- entry1 = tk.Entry(window, textvariable=path_var1)
- entry1.place(x=170, y=65, anchor='nw')
- resultpath = tk.Label(window, text='选择输出文件夹:',
- font=('微软雅黑', 12), width=20, height=2)
- resultpath.place(x=0, y=100, anchor='nw')
- path_var2 = tk.StringVar() # 输出路径
- entry2 = tk.Entry(window, textvariable=path_var2)
- entry2.place(x=170, y=115, anchor='nw')
- lebel1 = tk.Label(window, text='请确保CAD已经打开。',
- font=('微软雅黑', 10), width=40, height=2)
- lebel1.place(x=0, y=145, anchor='nw')
-
- def choose1():
- file_dir1 = tkFileDialog.askdirectory()
- path_var1.set(file_dir1)
-
- def choose2():
- file_dir2 = tkFileDialog.askdirectory()
- path_var2.set(file_dir2)
-
- def tuichu():
- window.destroy()
-
- def main():
- excelpath = entry1.get()
- outpath = entry2.get()
- #首先链接cad
- acad = Autocad(create_if_not_exists=True)
- mm = 0
- #遍历文件夹每个表格
- for parent,dirname,filenames in os.walk(excelpath):
- for filename in filenames:
- #排一下其他文件
- if '.dwg' in filename:
- #得到表格的完整路径
- filepath = parent + '/' +filename
- #拆分出断面名字dmm
- dmm = filename.split('.',-1)[0]
- #先复制,再打开,再另存覆盖?
- #判断下存在就删除
- #第一个点只需要z值,那么把断面名之类的信息也放在这里
- if mm == 0:
- outfpath = outpath + '\\合并断面.dwg'
- if os.path.exists(outfpath):
- os.remove(outfpath)
- shutil.copy(filepath, outfpath)
- acad.Application.Documents.Open(outfpath)
- target_doc = acad.ActiveDocument
- else:
- acad.Application.Documents.Open(filepath)
- doc = acad.ActiveDocument
- #把所有文字全部踢出来
- handle_string = 'select'
- text1 = list(acad.iter_objects(["Text", "Line","Circle"]))
- for t1 in text1:
- handle_string += ' (handent "'+t1.Handle+'")'
- #命令
- handle_string += '\n\n'
- doc.SendCommand(handle_string)
- #移动的距离做算法
- num1 = (1960 * mm) - 170
- str1 = 'move\n-170,-180,0\n' + str(num1) + ',-180,0\n'
- #移动
- # doc.SendCommand('move\n-80,-140,0\n760,-140,0\n')
- doc.SendCommand(str1)
- #再次选择
- doc.SendCommand(handle_string)
- #复制
- doc.SendCommand('copybase\n-170,-180,0\n')
- # target_doc.SendCommand('PASTEORIG\n')
- target_doc.SendCommand('PASTEORIG\n')
- #但不保存
- doc.Close(False)
- # #另存为
- # acad.doc.SaveAs(outfpath)
- #关闭当前
- print(dmm + ' FINISH')
- mm = mm + 1
- #最后保存
- target_doc.Close()
-
-
- messagebox.showinfo("消息","运行成功")
- window.destroy()
-
- tk.Button(window, text='选择', command=choose1).place(x=320, y=60, anchor='nw')
- tk.Button(window, text='选择', command=choose2).place(x=320, y=110, anchor='nw')
- tk.Button(window,text='确认',command=main).place(x=120,y=185,anchor='nw')
- tk.Button(window,text='取消',command=tuichu).place(x=220,y=185,anchor='nw')
-
- window.mainloop()
-
- if __name__ == '__main__':
- start()
|