123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- 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='选择表格文件夹:',
- 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='选择dwg:',
- font=('微软雅黑', 12), width=20, height=2)
- resultpath.place(x=10, 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=150, anchor='nw')
-
- def choose1():
- file_dir1 = tkFileDialog.askdirectory()
- path_var1.set(file_dir1)
-
- def choose2():
- file_dir2 = tkFileDialog.askopenfilename(filetypes=[('DWG', '*.dwg'), ('DWG', '*.DWG'),('All Files', '*')])
- path_var2.set(file_dir2)
-
- def tuichu():
- window.destroy()
-
- def main():
- excelpath = entry1.get()
- dwgpath = entry2.get()
- #遍历文件,得到点的坐标和注记内容
- list1 = []
- 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]
- #只取excel的第一个端点
- xlr = xlrd.open_workbook(filepath)
- table = xlr.sheets()[0]
- list2 = []
- list2.append(dmm)
- list2.append(table.cell(1, 1).value)
- list2.append(table.cell(1, 2).value)
- list1.append(list2)
- #打开cad
- #首先链接cad
- acad = Autocad(create_if_not_exists=True)
- acad.Application.Documents.Open(dwgpath)
- # 当前文件模型空间中所包含的图层总数
- layernums = acad.ActiveDocument.Layers.count
- # 获取指定图层索引号
- layernames = [acad.ActiveDocument.Layers.Item(i).Name for i in range(layernums)]
- index = layernames.index("DMX")
- # 将指定图层设定当前
- acad.ActiveDocument.ActiveLayer = acad.ActiveDocument.Layers.Item(index)
- # 获取当前文档
- doc = acad.doc
- # 获取模型空间
- msp = doc.ModelSpace
- #先写上去,再看细节怎么调
- # 定义文本的插入点坐标
- ii = 0
- while ii < len(list1):
- # 定义文本的插入点坐标
- insertion_point = APoint(list1[ii][1] - 1, list1[ii][2] + 1)
- # 定义文本的高度和样式
- text_height = 2.0
- # 插入单行文本
- text = msp.AddText(list1[ii][0], insertion_point, text_height)
- ii = ii + 1
- #关闭当前
- acad.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=200,anchor='nw')
- tk.Button(window,text='取消',command=tuichu).place(x=220,y=200,anchor='nw')
-
- window.mainloop()
-
- if __name__ == '__main__':
- start()
|