from pyautocad import Autocad, APoint
import numpy
import tkinter as tk
from tkinter import filedialog, messagebox
from decimal import Decimal
import datetime
today = datetime.date.today()
now = datetime.datetime.now()
print(now.hour,type(now.hour))
'''打开选择文件夹对话框'''
window = tk.Tk()
window.title( "勘测定界坐标导入1.0 by: 放放风" )
window.geometry( '490x170+800+200' )
window['background'] = 'DimGray'
def about():
tk.messagebox.showinfo( '关于', "勘测定界坐标导入1.0\n \n\nby:放放风 2021.01.23 \n更多功能请联系:[email protected]" )
menubar = tk.Menu( window )
file_menu = tk.Menu( menubar, tearoff=False )
menubar.add_command( label='关于', command=about )
window.config( menu=menubar )
r_value = tk.IntVar()
class Variable:
def __init__(self):
self.one = ""
self.File_path = ""
self.File_name = ""
self.Save_path = ""
self.pnts = None
self.layer = None
self.r = None
self.cp = None
self.gd_name = None
def r_print(self):
self.r = r_value.get()
def get_gravity_point(self,points):
"""
@brief 获取多边形的重心点
@param points The points
@return The center of gravity point.
"""
if len( points ) <= 2:
return list()
area = Decimal( 0.0 )
x, y = Decimal( 0.0 ), Decimal( 0.0 )
for i in range( len( points ) ):
lng = Decimal( points[i][0] )
lat = Decimal( points[i][1] )
nextlng = Decimal( points[i - 1][0] )
nextlat = Decimal( points[i - 1][1] )
tmp_area = (nextlng * lat - nextlat * lng) / Decimal( 2.0 )
area += tmp_area
x += tmp_area * (lng + nextlng) / Decimal( 3.0 )
y += tmp_area * (lat + nextlat) / Decimal( 3.0 )
x = x / area
y = y / area
self.cp = APoint(float( x ), float( y ))
def open(self):
File_path = filedialog.askopenfilenames( filetypes=[("文本", "txt")], title="选择需要导入的文本" )
if not File_path:
tk.messagebox.showinfo( '警告', "请选择文本!" )
else:
self.File_path = File_path
def formatting(self, path, r):
constant = float(text1_var.get())
with open( path ) as f:
p = []
c = []
val = f.readlines()
if r == 2:
val = [x.split( "," ) for x in val]
for s in val[:-1]:
A = float( s[0] ) - constant
B = float( s[1][:-1] )
c.append([A,B])
p.append( APoint( A, B ) )
pnts = numpy.array( [m for n in p for m in n], dtype=numpy.float )
self.cp = c
self.pnts = pnts
self.layer = "二调图斑"
elif r == 1:
tb = []
n = []
text = []
d = []
count = 0
for s in val:
if "J1" in s:
tb.append( val.index( s ) )
for q in range( int(len( tb ) / 2) ):
text.append( val[tb[count]:tb[count + 1]] )
count += 2
for r in text:
for w in r:
k = w.split( "," )
A = float( k[4] )
B = float( k[5] ) - constant
d.append([B,A])
n.append( APoint( B, A ) )
p.append( n )
c.append(d)
n = []
d = []
self.cp = c
self.pnts = p
self.layer = "规划图斑"
elif r == 3:
tb = []
text = []
n = []
count = 0
d = []
gd_name = []
for s in val:
if "@" in s:
tb.append( val.index( s ) )
gd_name.append("地块"+s.split(",")[0])
self.gd_name = gd_name
for q in range( len( tb ) ):
if q < len( tb ) - 1:
text.append( val[tb[count] + 1:tb[count + 1] - 1] )
count += 1
if q == len( tb ) - 1:
text.append( val[tb[count] + 1:-1] )
for r in text:
for w in r:
k = w.split( "," )
A = float( k[2] )
B = float( k[3] ) - constant
n.append( APoint( B, A ) )
d.append([B,A])
p.append( n )
c.append(d)
n = []
d = []
self.cp = c
self.pnts = p
self.layer = "供地图斑"
myobject = Variable()
def map():
myobject.open()
acad = Autocad( create_if_not_exists=True )
if 5 < now.hour < 10:
acad.prompt( '早安,打工人!' )
elif 10 < now.hour < 14:
acad.prompt( '午安,打工人!' )
elif 14 < now.hour < 18:
acad.prompt( '下午好,打工人!' )
elif now.hour > 18:
acad.prompt( '晚安,打工人!' )
for path in myobject.File_path:
myobject.formatting( path, myobject.r )
name = path.split("/")[-1][:-4]
layerobj = acad.ActiveDocument.Layers.Add( myobject.layer )
acad.ActiveDocument.ActiveLayer = layerobj
ClrNum = 1
layerobj.color = ClrNum
if myobject.r == 3 or myobject.r == 1:
for pnts in myobject.pnts:
pnts = numpy.array( [m for n in pnts for m in n], dtype=numpy.float )
pnts = Autocad.aDouble( pnts )
pline_obj = acad.model.AddPolyLine( pnts )
pline_obj.Closed = True
if myobject.r == 1:
for c in myobject.cp:
myobject.get_gravity_point(c)
text = acad.model.AddText( "{0}".format( name ), myobject.cp, 1.5 )
if myobject.r == 3:
n = 0
for c in myobject.cp:
myobject.get_gravity_point(c)
text = acad.model.AddText( "{0}".format( myobject.gd_name[n] ), myobject.cp, 1.5 )
n += 1
n = 0
else:
pnts = Autocad.aDouble( myobject.pnts )
pline_obj = acad.model.AddPolyLine( pnts )
pline_obj.Closed = True
myobject.get_gravity_point(myobject.cp)
text = acad.model.AddText( "{0}".format( name ), myobject.cp, 1.5 )
acad.ActiveDocument.Application.preferences.OpenSave.AutoSaveInterval = 0.51
tk.messagebox.showinfo( '提示', "转换成功,请检查数据" )
tk.Radiobutton( window, text='规划坐标', variable=r_value, value=1, command=myobject.r_print , width=12).grid( row=1, column=0 )
tk.Radiobutton( window, text='二调坐标', variable=r_value, value=2, command=myobject.r_print , width=12).grid( row=2, column=0 )
tk.Radiobutton( window, text='供地坐标', variable=r_value, value=3, command=myobject.r_print , width=12).grid( row=3, column=0 )
lable1 = tk.Label(window, text="[ 扣除40 ]", width=15).grid(row=0, column=0)
text1_var = tk.StringVar()
text1_var.set(r'40000000')
text1 = tk.Entry(window, textvariable=text1_var, bd=5).grid(row=0, column=1)
tk.Button( window, text="导入cad", width=15, height=1, command=map, bg="Silver" ).grid( row=5, column=0 )
text20 = tk.Text(window, width=50, height=12)
text20.grid(row=1, column=1, rowspan=5, columnspan=2)
text20.tag_config("tag_1", backgroun="yellow", foreground="red")
text20.insert("insert",
"1.导入时须打开cad!\n\n2.规划:cad中地块编号为文本名称;支持导入合并坐标\n\n"
"3.二调:同上,不支持合并坐标导入\n\n"
"4.供地:cad中地块编号为【地块】+ 地块编号\n\n")
text20.insert("end", "5.更多功能请联系:\[email protected]", "tag_1")
window.mainloop()