123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- 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('400x300') # 设置窗口的大小
- title = tk.Label(window, text='断面线图表绘制程序',
- font=('微软雅黑', 12), width=40, height=3)
- title.place(x=0, y=0, anchor='nw')
- choosepath = tk.Label(window, text='选择表格文件夹:',
- 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')
- oripath = tk.Label(window, text='选择原始模板:',
- font=('微软雅黑', 12), width=20, height=2)
- oripath.place(x=0, y=150, anchor='nw')
- path_var3 = tk.StringVar() # 输出路径
- entry3 = tk.Entry(window, textvariable=path_var3)
- entry3.place(x=170, y=165, anchor='nw')
- lebel1 = tk.Label(window, text='请确保CAD已经打开。',
- font=('微软雅黑', 10), width=40, height=2)
- lebel1.place(x=0, y=190, 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 choose3():
- file_dir3 = tkFileDialog.askopenfilename(filetypes=[('DWG', '*.dwg'), ('DWG', '*.DWG'),('All Files', '*')])
- path_var3.set(file_dir3)
-
- def tuichu():
- window.destroy()
-
- def main():
- excelpath = entry1.get()
- outpath = entry2.get()
- ori = entry3.get()
- #首先链接cad
- acad = Autocad(create_if_not_exists=True)
- #遍历文件夹每个表格
- for parent,dirname,filenames in os.walk(excelpath):
- for filename in filenames:
- #排一下其他文件
- if '.xls' in filename:
- #得到表格的完整路径
- filepath = parent + '/' +filename
- #拆分出断面名字dmm
- dmm = filename.split('.',-1)[0]
- #先复制,再打开,再另存覆盖?
- #判断下存在就删除
- outfpath = outpath + '\\' + dmm + '号断面.dwg'
- if os.path.exists(outfpath):
- os.remove(outfpath)
- shutil.copy(ori, outfpath)
- acad.Application.Documents.Open(outfpath)
- #打开表格,从第二行开始读
- xlr = xlrd.open_workbook(filepath)
- table = xlr.sheets()[0]
- row = table.nrows
- #设置一条二位多段线line
- line1 = []
- ii = 1
- while ii < row:
- #第一个点只需要z值,那么把断面名之类的信息也放在这里
- if ii == 1:
- #两个标题的修改
- text1 = list(acad.iter_objects("Text"))
- for t1 in text1:
- if '号断面图' in t1.TextString:
- t1.TextString = dmm + '号断面图'
- if '断面名称' in t1.TextString:
- t1.TextString = '断面名称:' + dmm
- if t1.TextString == '左端点':
- t2 = t1
- #Z值
- z1 = float(table.cell(ii, 3).value)
- #计算方式,(z-3400)*5
- num1 = z1 - 3400
- num2 = num1 * 10
- #设置dmlist[x,y,z]
- pt1 = [0,num2,0]
- line1 = line1 + pt1
- ii = ii + 1
- else:
- #判定下是否有值,没有就跳过
- try:
- #首先是文字
- str1 = table.cell(ii, 4).value
- dis1 = float(table.cell(ii, 5).value)
- #计算公式2 * dis1
- num3 = dis1 * 10
- #复制
- copy = t2.Copy()
- #移动
- center = APoint(0,-19.9995,0)
- pt = APoint(num3,-19.9995,0)
- copy.move(center,pt)
- #重新赋值
- copy.TextString = str1
- #然后是线
- #Z值
- z2 = float(table.cell(ii, 3).value)
- #计算方式,(z-3400)*5
- num4 = z2 - 3400
- num5 = num4 * 10
- #设置dmlist[x,y,z]
- pt2 = [num3,num5,0]
- line1 = line1 + pt2
- ii = ii + 1
- except:
- ii = ii + 1
- #把线绘制出来
- pnts = aDouble(line1)
- plineObj = acad.model.AddPolyLine(pnts)
- #指定图层
- plineObj.Layer = "DMX"
- # #另存为
- # acad.doc.SaveAs(outfpath)
- #关闭当前
- acad.doc.Close()
- print(dmm + ' FINISH')
-
-
- 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=choose3).place(x=320, y=160, anchor='nw')
- tk.Button(window,text='确认',command=main).place(x=120,y=230,anchor='nw')
- tk.Button(window,text='取消',command=tuichu).place(x=220,y=230,anchor='nw')
-
- window.mainloop()
-
- if __name__ == '__main__':
- start()
|