参考了网络上其他的人的做法,并未成功,仔细研究了Auto CAD 官网中关于二次开发和ActiveX的相关文档,尝试出来,因为实际工作中会使用,所以使用tk做了UI
from pyautocad import Autocad, APoint
import win32com.client
import os
import ctypes
import tkinter as tk
from tkinter import *
from tkinter.ttk import *
from tkinter import filedialog
global c1,d1,e1,t1,s2fname,t2,t3
def openfiles2():
s2fname = filedialog.askopenfilename(title='选择文件', filetypes=[('bom', '*.DWG'),('All Files', '*')])
return s2fname
def finddim():
# 连接到 AutoCAD
acad = Autocad(create_if_not_exists=True)
#t1.insert(1.0,'start',2.0,'\n')
#print(acad.ActiveDocument.ModelSpace)
print('work:')
for entity in acad.ActiveDocument.ModelSpace:
#print(entity.EntityName)
#print(round(entity.Measurement,2))
#print(round(entity.ToleranceUpperLimit,2))
#print('-'+round(entity.ToleranceLowerLimit,2))
try:
#t1.insert(1.0,dim,2.0,'\n')
#t1.insert(1.0,tolup,2.0,'\n')
#t1.insert(1.0,tollow,2.0,'\n')
t1.insert(END,round(entity.Measurement,2))
t1.insert(END,'\n')
t2.insert(END,round(entity.ToleranceUpperLimit,2))
t2.insert(END,'\n')
t3.insert(END,(0-round(entity.ToleranceLowerLimit,2)))
t3.insert(END,'\n')
except:
continue
def button_a():
file1=openfiles2()
#ojec(file1)
#findline(file1)
t2.insert('',file1)
return file1
def button_b():
file2=openfiles2()
#findcol(file2)
t3.insert('',file2)
return file2
def button_c():
#file1=t2.get()
finddim()
print('OK')
def clean_text():
t1.delete('1.0','end')
t2.delete('1.0','end')
t3.delete('1.0','end')
ctypes.windll.shcore.SetProcessDpiAwareness(1)
ScaleFactor=ctypes.windll.shcore.GetScaleFactorForDevice(0)
root = tk.Tk()
root.tk.call('tk', 'scaling', ScaleFactor/75)
root.title("Read DWG by 庞霄")
root.geometry('320x500+200+10')
#解决分辨率问题
#a=Button(root, text='DRAWING',command=button_a)
#a.grid(row=0,column=0,sticky=E+W)
#t2=Entry(root)
#t2.grid(row=0,column=1,sticky=W)
#t3=Entry(root)
#t3.grid(row=1,column=1,sticky=W)
#t1.grid(row=3,columnspan=1,sticky=S+W+E+N)
l1=Label(root,text='dim')
l1.grid(row=2,column=0)
l1=Label(root,text='+')
l1.grid(row=2,column=2)
l1=Label(root,text='-')
l1.grid(row=2,column=4)
t1=Text(root,width=8,height=15)
t1.grid(row=3,column=0,sticky=S)
t2=Text(root,width=8,height=15)
t2.grid(row=3,column=2,sticky=S)
t3=Text(root,width=8,height=15)
t3.grid(row=3,column=4,sticky=S)
s1=Scrollbar(root,orient=tk.VERTICAL)
s2=Scrollbar(root,orient=tk.VERTICAL)
s3=Scrollbar(root,orient=tk.VERTICAL)
t1.configure(yscrollcommand=s1.set,font='Times 9')
t2.configure(yscrollcommand=s2.set,font='Times 9')
t3.configure(yscrollcommand=s3.set,font='Times 9')
s1.grid(row=3,column=1,sticky=S+W+E+N)
s2.grid(row=3,column=3,sticky=S+W+E+N)
s3.grid(row=3,column=5,sticky=S+W+E+N)
s1.configure(command=t1.yview)
s2.configure(command=t2.yview)
s3.configure(command=t3.yview)
f=Button(root, text='OK',command=button_c)
f.place(x=0,y=340)
#f.grid(row=4,column=0,sticky=W)
gg=Button(root, text='清除',command=clean_text)
gg.place(x=0,y=380)
#gg.grid(row=4,column=1,sticky=W)
root.mainloop()