python对 CAD图斑面积的统计及标注

在这里插入图片描述

import win32com.client
import pythoncom
import tkinter as tk
from tkinter import filedialog, messagebox
import numpy
from tkinter import ttk

'''打开选择文件夹对话框'''
window = tk.Tk()
window.title("CAD标注及面积统计  by: 放放风")
window.geometry('720x300+800+200')  # 290 160为窗口大小,+1000 +10 定义窗口弹出时的默认展示位置

window.resizable( 0, 0 )
window['background'] = 'DimGray'

r_value = tk.IntVar()

def vtpnt(x, y, z=0):
    """坐标点转化为浮点数"""
    return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, (x, y, z))


def vtobj(obj):
    """转化为对象数组"""
    return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_DISPATCH, obj)


def vtFloat(list):
    """列表转化为浮点数"""
    return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, list)


def vtInt(list):
    """列表转化为整数"""
    return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_I2, list)


def vtVariant(list):
    """列表转化为变体"""
    return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_VARIANT, list)


def list_format_conversion(old_list, step=2, deduction=None):
    new_list = []
    for counte in range(0, len(old_list), step):
        new_list.append(list(old_list[counte:counte + step])[:deduction])
    return new_list


def rrd(point, f):
    if f == 0:
        point = str(int(point))
    elif f > 0:
        SP = str(numpy.around(point, f))
        sy = SP.split(".")
        n = len(sy[1])
        if n < f:
            sj = f - n
            point = sy[0] + "." + sy[1] + "0" * sj
        else:
            point = SP
    elif f < 0:
        tk.messagebox.showinfo('提示', "取位不可为负")
    return point


class Extract:

    def __init__(self):
        self.face_properties = []
        self.r = None

    def r_print(self):  # 获取单选框值
        self.r = r_value.get()

    def Select_element(self):
        acad = win32com.client.Dispatch("AutoCAD.Application")
        doc = acad.ActiveDocument
        doc.Utility.Prompt("\n醉后不知天在水\n满船清梦压星河\n")
        mp = doc.ModelSpace  # 模型空间
        tk.messagebox.showinfo('提示', "请在屏幕拾取图元,以Enter键结束")
        polyline_name = text1_var.get()
        polyline_number = text2_var.get()
        polyline_mm_round = text3_var.get()  # 平方米保留位数
        polyline_mu_round = text4_var.get()  # 亩保留位数
        font_size = text5_var.get()
        font_height = text6_var.get()

        mm_Company = text7_var.get()
        Mu_Company = text8_var.get()
        while True:
            try:
                doc.SelectionSets.Item("SS1").Delete()
            except:
                tk.messagebox.showinfo('警告', "Delete selection failed")
            slt = doc.SelectionSets.Add("SS1")
            slt.SelectOnScreen()
            Text_point = doc.Utility.GetPoint()  # 获取屏幕指定坐标
            Text_point_number = vtpnt(Text_point[0], Text_point[1])  # 转为cad坐标格式,编号坐标
            Text_point_area = vtpnt(Text_point[0], Text_point[1] - font_height)  # 平方米/亩标注坐标

            for x in slt:
                if x.ObjectName == "AcDbPolyline":

                    Polygon_area = x.Area  # 多边形面积
                    Polygon_Mu_area = Polygon_area / 666.6666666667
                    Polygon_area = str(rrd(Polygon_area, polyline_mm_round)) + mm_Company  #
                    Polygon_Mu_area = str(rrd(Polygon_Mu_area, polyline_mu_round)) + Mu_Company  #

                    polyline_text_number = polyline_name + str(polyline_number)  # 前缀+编号
                    tree.insert("", "end", text=polyline_number,
                                values=(polyline_text_number, Polygon_area, Polygon_Mu_area))
                    self.face_properties.append(
                        [polyline_text_number, "\t", Polygon_area, "\t", Polygon_Mu_area, "\n"])  # {编号:多边形平方米面积}

                    mp.AddText(polyline_text_number, Text_point_number, font_size)  # 添加编号注记

                    if self.r == 2:
                        mp.AddText(Polygon_area, Text_point_area, font_size)  # 添加平方米注记
                    elif self.r == 3:
                        mp.AddText(Polygon_Mu_area, Text_point_area, font_size)  # 添加亩注记
                    polyline_number += 1

    def save_text(self):
        Save_path = filedialog.askdirectory()
        new_filer = open(Save_path + "/面积汇总.txt", "w")
        new_lines = ["地块编号", "\t", "平方米", "\t", "亩", "\n"]
        new_filer.writelines(new_lines)
        for items in self.face_properties:
            new_filer.writelines(items)
        new_filer.close()
        messagebox.showinfo("提示", "成功")


objectA = Extract()
tk.Button(window, text="拾取及标注", width=15, height=1, command=objectA.Select_element, bg="Silver").grid(row=8, column=3)
tk.Button(window, text="导出文本", width=15, height=1, command=objectA.save_text, bg="Silver").grid(row=8, column=4)

tree = ttk.Treeview(window, height=12, show="headings")
tree.grid(row=0, column=3, rowspan=8, columnspan=2)
tree["columns"] = ("Num", "Area(m2)", "Area(Mu)")

tree.column("Num", width=150)
tree.column("Area(m2)", width=150)
tree.column("Area(Mu)", width=150)

tree.heading("Num", text="编号")
tree.heading("Area(m2)", text="面积(m²)")
tree.heading("Area(Mu)", text="面积(Mu)")

lable0 = tk.Label(window, text="[  文本属性  ]", width=38).grid(row=0, column=0, columnspan=2)

lable1 = tk.Label(window, text="[  编号前缀  ]", width=15).grid(row=1, column=0)

text1_var = tk.StringVar()  # 获取text_1输入的值
text1_var.set("地块")
text1 = tk.Entry(window, textvariable=text1_var, bd=5).grid(row=1, column=1)

lable2 = tk.Label(window, text="[编号起始位置]", width=15).grid(row=2, column=0)
text2_var = tk.IntVar()  # 获取text_2输入的值
text2_var.set("1")
text2 = tk.Entry(window, textvariable=text2_var, bd=5).grid(row=2, column=1)

tk.Radiobutton(window, text='[平方米]', variable=r_value, value=2, command=objectA.r_print, width=12).grid(row=3, column=0)
lable3 = tk.Label(window, text="[保留位数]", width=15).grid(row=4, column=0)
text3_var = tk.IntVar()  # 获取text_3输入的值
text3_var.set("2")
text3 = tk.Entry(window, textvariable=text3_var, bd=5).grid(row=4, column=1)

text7_var = tk.StringVar()  # 获取text_3输入的值
text7_var.set(r"平方米")
text7 = tk.Entry(window, textvariable=text7_var, bd=5).grid(row=3, column=1)

tk.Radiobutton(window, text='[   亩   ]', variable=r_value, value=3, command=objectA.r_print, width=12).grid(row=5,
                                                                                                            column=0)
lable4 = tk.Label(window, text="[保留位数]", width=15).grid(row=6, column=0)
text4_var = tk.IntVar()  # 获取text_3输入的值
text4_var.set("3")
text4 = tk.Entry(window, textvariable=text4_var, bd=5).grid(row=6, column=1)

text8_var = tk.StringVar()  # 获取text_3输入的值
text8_var.set(r"亩")
text8 = tk.Entry(window, textvariable=text8_var, bd=5).grid(row=5, column=1)

lable5 = tk.Label(window, text="[注记大小]", width=15).grid(row=7, column=0)
text5_var = tk.IntVar()  # 获取text_3输入的值
text5_var.set("1.5")
text5 = tk.Entry(window, textvariable=text5_var, bd=5).grid(row=7, column=1)

lable6 = tk.Label(window, text="[注记间隔]", width=15).grid(row=8, column=0)
text6_var = tk.IntVar()  # 获取text_3输入的值
text6_var.set("2.5")
text6 = tk.Entry(window, textvariable=text6_var, bd=5).grid(row=8, column=1)

window.mainloop()

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值