规划坐标与二调坐标格式转换

import tkinter as tk
from tkinter import filedialog, messagebox
from decimal import Decimal




    '''打开选择文件夹对话框'''
    window = tk.Tk()
    window.title("规划与二调坐标转换1.3  by: 放放风")
    window.geometry('565x320+800+200')  # 290 160为窗口大小,+1000 +10 定义窗口弹出时的默认展示位置
    window['background'] = 'Gainsboro'

    window.resizable(0, 0)

    menubar = tk.Menu(window)


    def about():
        tk.messagebox.showinfo('关于', "规划与二调坐标转换1.3\n \n\nby:放放风 2021.01.27 \n更多功能请联系:cemo9960@outlook.com")

    file_menu = tk.Menu(menubar, tearoff=False)
    menubar.add_command(label='关于', command=about,font="微软雅黑")
    window.config(menu=menubar)

    class G:
        def __init__(self):
            self.File_path = ""
            self.Save_path = ""

        def open(self):  # 打开
            File_path = filedialog.askopenfilenames(filetypes=[("文本", "txt")], title="选择需要转换的文本")  # 获取需要转换的规划文件绝对路径
            self.File_path = File_path

        def save(self):  # 保存
            Save_path = filedialog.askdirectory()
            self.Save_path = Save_path

        # 规划格式-------------------------


    myobject = G()


    def planning(path):
        n = 0
        plan = []
        J = text1_var.get()  # 前缀
        constant = Decimal(text5_var.get())  # 常数
        digit = int(text2_var.get())  # 保留位数

        with open(path, "r") as f:
            n_plan = [line.split(" ")[1:-2] for line in f.readlines()[1:]]
            d = len(n_plan)
            for val1, val2 in n_plan:
                n += 1
                if n < d:
                    plan.append([str(n), str(1), str(1), J + str(n), str(round(Decimal(val2), digit)),
                                 str(round((constant + Decimal(val1)), digit)) + "\n"])
                elif n == d:
                    plan.append([str(n), str(1), str(1), J + str(n), str(round(Decimal(val2), digit)),
                                 str(round((constant + Decimal(val1)), digit)) + "\n"])
                    plan.append([str(n + 1), str(1), str(1), J + str(1), plan[0][4], plan[0][5].replace("\n", "")])
        return plan


    # 二调格式-------------------------
    def coordinate(path):
        constant = Decimal(text6_var.get())  # 常数
        digit = int(text9_var.get())  # 保留位数
        with open(path, "r") as f2:
            plan = [
                [str(round(constant + Decimal(val1), digit)), str(round(Decimal(val2), digit)) + "\n"]
                for val1, val2 in
                [line.split()[:-2]
                 for line in
                 f2.readlines()[1:]]]
            plan.append(plan[2])
        return plan


    # --------------------------------
    def get_Two_coordinate():
        myobject.open()
        if not myobject.File_path:
            tk.messagebox.showinfo('提示', "请选择需要转换的文本!")
            return
        myobject.save()
        if not myobject.Save_path:
            tk.messagebox.showinfo('提示', "请选择需要保存的路径!")
            return

        paths = myobject.File_path  # 获取需要转换的规划文件绝对路径
        save_path = myobject.Save_path  # 返回需要保存的绝对路径
        H = text4_var.get()  # 后缀
        for path in paths:  # 循环规划文件路径

            name = path.split("/")[-1].replace(".txt", H)  # 获取规划文件文件名

            filer = open(save_path + "/" + name, "w")  # 打开文本,没有创建

            for line in coordinate(path):  # 循环列表

                filer.writelines(",".join(line))  # 列表用“,”合并,写入
            filer.close()  # 关闭文本
        tk.messagebox.showinfo('结果', "二调坐标转换成功!")
        return


    # ------------------------------------------------------------------
    def get_plan():
        myobject.open()
        if not myobject.File_path:
            tk.messagebox.showinfo('提示', "请选择需要转换的文本!")
            return
        myobject.save()
        H = text3_var.get()  # 后缀
        if not myobject.Save_path:
            tk.messagebox.showinfo('提示', "请选择需要保存的路径!")
            return
        paths = myobject.File_path  # 获取需要转换的规划文件绝对路径
        save_path = myobject.Save_path  # 返回需要保存的绝对路径

        for path in paths:  # 循环规划文件路径

            name = path.split("/")[-1].replace(".txt", H)  # 获取规划文件文件名

            filer = open(save_path + "/" + name, "w")  # 打开文本,没有创建

            for line in planning(path):  # 循环列表
                filer.writelines(",".join(line))  # 列表用“,”合并,写入
            filer.close()  # 关闭文本
        tk.messagebox.showinfo('结果', "规划坐标转换成功!")
        return


    # -------------------------------------------------------------------------
    cjip = tk.PhotoImage(file="D:\\a5.gif")
    lable8 = tk.Label(window, text="[规划坐标]", width=15, font="微软雅黑", bg="Yellow").grid(row=0, column=0)

    lable1 = tk.Label(window, text="[界址点号前缀]", width=15, font="微软雅黑").grid(row=1, column=0)
    text1_var = tk.StringVar()  # 获取text_1输入的值
    text1_var.set(r'J')
    text1 = tk.Entry(window, textvariable=text1_var, bd=3, width=15, font="微软雅黑").grid(row=1, column=2)

    lable5 = tk.Label(window, text="[+40]", width=15, font="微软雅黑").grid(row=2, column=0)
    text5_var = tk.StringVar()  # 获取text_1输入的值
    text5_var.set(r'0')
    text5 = tk.Entry(window, textvariable=text5_var, bd=3, width=15, font="微软雅黑").grid(row=2, column=2)

    lable2 = tk.Label(window, text="[保留位数]", width=15, font="微软雅黑").grid(row=3, column=0)
    text2_var = tk.StringVar()  # 获取text_1输入的值
    text2_var.set(r'3')
    text2 = tk.Entry(window, textvariable=text2_var, bd=3, width=15, font="微软雅黑").grid(row=3, column=2)

    lable3 = tk.Label(window, text="[文件后缀]", width=15, font="微软雅黑").grid(row=4, column=0)
    text3_var = tk.StringVar()  # 获取text_1输入的值
    text3_var.set(r'(规划坐标).txt')
    text3 = tk.Entry(window, textvariable=text3_var, bd=3, width=15, font="微软雅黑").grid(row=4, column=2)

    lable7 = tk.Label(window, text="[二调坐标]", width=15, font="微软雅黑", bg="Yellow").grid(row=5, column=0)
    labl6 = tk.Label(window, text="[+40]", width=15, font="微软雅黑").grid(row=6, column=0)
    text6_var = tk.StringVar()  # 获取text_1输入的值
    text6_var.set(r'40000000')
    text6 = tk.Entry(window, textvariable=text6_var, bd=3, width=15, font="微软雅黑").grid(row=6, column=2)

    lable9 = tk.Label(window, text="[保留位数]", width=15, font="微软雅黑").grid(row=7, column=0)
    text9_var = tk.StringVar()  # 获取text_1输入的值
    text9_var.set(r'3')
    text9 = tk.Entry(window, textvariable=text9_var, bd=3, width=15, font="微软雅黑").grid(row=7, column=2)

    lable4 = tk.Label(window, text="[文件后缀]", width=15, font="微软雅黑").grid(row=8, column=0)
    text4_var = tk.StringVar()  # 获取text_1输入的值
    text4_var.set(r'(二调坐标).txt')
    text4 = tk.Entry(window, textvariable=text4_var, bd=3, width=15, font="微软雅黑").grid(row=8, column=2)

    tk.Button(window, width=15, height=2, text="规划", command=get_plan, font="微软雅黑").grid(row=3, column=3, rowspan=2) 
    tk.Button(window, width=15, height=2, text="二调", command=get_Two_coordinate, font="微软雅黑").grid(row=7,
                                                                                                   column=3,
                                                                                                   rowspan=2)

    tk.Label(window, image=cjip, height=94, width=180).grid(row=0, column=3, rowspan=3)

    window.mainloop()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值