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' ) # 290 160为窗口大小,+1000 +10 定义窗口弹出时的默认展示位置
# window.resizable( 0, 0 )
window['background'] = 'DimGray'
def about():
tk.messagebox.showinfo( '关于', "勘测定界坐标导入1.0\n \n\nby:放放风 2021.01.23 \n更多功能请联系:cemo9960@outlook.com" )
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 # x扣除常数
B = float( s[1][:-1] ) #y
c.append([A,B])
p.append( APoint( A, B ) )#添加点Ax,By
pnts = numpy.array( [m for n in p for m in n], dtype=numpy.float )#转一维矩阵
self.cp = c
self.pnts = pnts
self.layer = "二调图斑" #cad图层
elif r == 1: #规划
tb = [] #列表中J1位置
n = [] # 两个j1中间的值
text = [] #所有文本中两个j1中间的值
d = []
count = 0 #计数
for s in val: #循环文本列表
if "J1" in s: #如果J1在s内
tb.append( val.index( s ) ) #获取J1在文本中的位置
for q in range( int(len( tb ) / 2) ): #循环位置 1,2 3,4 5,6 共3次
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()
# 连接cad
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() # 获取text_1输入的值
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.更多功能请联系:\ncemo9960@outlook.com", "tag_1")
window.mainloop()
#text = acad.model.AddText("{0}".format(text), p, 15)
勘测定界坐标导入cad
最新推荐文章于 2023-04-23 10:56:41 发布
这是一个Python脚本,用于从文本文件中导入不同类型的坐标数据(规划、二调、供地),并根据选择的类型在AutoCAD中进行绘制。用户可以选择扣除的常数,程序会计算多边形重心,并在对应图层显示结果。此外,还提供了操作提示和帮助信息。
摘要由CSDN通过智能技术生成