python读取数据库数据对dxf文件进行标注并另存

   python读取数据库数据对dxf文件文字区域进行标注,并另存。
   链接数据库,读取数据,根据数据对dxf文件进行方框标注,并判断文字区域旋转角,最后另存为dxf文件。
   [具体项目代码](https://download.csdn.net/download/shuishuishuitian/12919743)
import os
import cv2
import numpy as np
import ezdxf
from PIL import Image
import pymysql


def database(coding):
    con = pymysql.connect(host='', port=3306, user='root', passwd='', db='',
                          charset='utf8', )  # 数据库连接
    # 使用cursor()方法获取操作游标
    cursor = con.cursor()
    # SQL 查询
    sql = "select * from '' where code_content = '%s'" % coding
    try:
        # 执行SQL语句
        cursor.execute(sql)
        # 获取所有记录列表
        results = cursor.fetchall()
        # 精确查询
        return results
    except:
        print("Error: unable to fecth data")
    cursor.close()  # 关闭数据库连接
    con.close()


def database_blurry(coding):
    con = pymysql.connect(host='', port=3306, user='root', passwd='', db='',
                          charset='utf8', )  # 数据库连接
    # 使用cursor()方法获取操作游标
    cursor = con.cursor()
    # SQL 查询
    sql = "select * from '' where code_content LIKE '%%%%%s%%%%'" % coding
    try:
        # 执行SQL语句
        cursor.execute(sql)
        # 获取所有记录列表
        results = cursor.fetchall()
        # 精确查询
        return results
    except:
        print("Error: unable to fecth data")
    cursor.close()  # 关闭数据库连接
    con.close()


def database_query(coding):
    data = []
    data_dxf_2 = []
    compare_1 = []
    compare_2 = []
    data_change = []
    ### 精确查询
    results = database(coding)
    for it in results:
        data.append(it)

    ### 模糊查询
    results = database_blurry(coding)
    for it in results:
        if not coding == it[2]:
            data_change.append(it)

    ### 联想查询,可无限添加条件
    cod = coding
    for i in range(len(coding)):
        if coding.isdigit() == True:
            results = database(coding)
            for it in results:
                compare_1.append(it)
            for k in range(i, 0, -1):
                data_dxf_2.append(cod[len(cod) - k])
            str3 = ''.join(data_dxf_2)
            # print(coding,str3)
            results = database(str3)
            for it in results:
                compare_2.append(it)
            # print(str3)
            # print(compare_1, compare_2)
            for cnt, i in enumerate(compare_1):
                for cnt2, j in enumerate(compare_2):
                    pts1 = np.array(j[3].strip().split(','), float)
                    pts2 = np.array(i[3].strip().split(','), float)
                    A = pts1[0] - pts2[0]
                    B = pts1[1] - pts2[1]
                    if (A > -0.5 and A < 0.5) or (B > 0.5 and B < 0.5):
                        # data_change.append(compare_1[cnt])
                        data_change.append(compare_1[cnt])
            break
        else:
            coding = coding[:-1]
            # print(coding)

    return (data, data_change)
def dxf_both_draw(data_all, data_all_change, dir_dxf, C):
    if not os.path.exists('queryDxf/'):
        os.makedirs('queryDxf/')  # 创建存放画图后dxf文件夹
    path_dxf = os.path.join(dir_dxf, C)  # 查询的dxf路径
    path_query_dxf = os.path.join('queryDxf/', C)  # 画图后dxf存放地址
    doc = ezdxf.readfile(path_dxf)
    msp = doc.modelspace()
    for i, myList_small in enumerate(data_all):
        if C == myList_small[1]:  # 判断查询的是第几个图名
            if myList_small[4] == '90.0':  # 判断旋转角,90.0为竖直方向
                length = len(myList_small[2])  # 判断字符长度
                pts = np.array(myList_small[3].strip().split(','), float)
                # print(pts)
                for i in range(10):
                    points = [(pts[0] - float(myList_small[5]) - i * 0.1, pts[1] - i * 0.1),
                              (pts[0] + float(myList_small[5]) + i * 0.1, pts[1] - i * 0.1),
                              (pts[0] + float(myList_small[5]) + i * 0.1,
                               pts[1] + length * float(myList_small[5]) * 3 / 5 + i * 0.1),
                              (pts[0] - float(myList_small[5]) - i * 0.1,
                               pts[1] + length * float(myList_small[5]) * 3 / 5 + i * 0.1),
                              (pts[0] - float(myList_small[5]) - i * 0.1, pts[1] - i * 0.1)]
                    msp.add_lwpolyline(points, dxfattribs={'color': 150})

            else:  # 水平方向
                length = len(myList_small[2])
                pts = np.array(myList_small[3].strip().split(','), float)
                for i in range(10):
                    points = [(pts[0] - i * 0.1, pts[1] - float(myList_small[5]) - i * 0.1),
                              (pts[0] + length * float(myList_small[5]) * 3 / 5 + i * 0.1,
                               pts[1] - float(myList_small[5]) - i * 0.1),
                              (pts[0] + length * float(myList_small[5]) * 3 / 5 + i * 0.1,
                               pts[1] + float(myList_small[5]) + i * 0.1),
                              (pts[0] - i * 0.1, pts[1] + float(myList_small[5]) + i * 0.1),
                              (pts[0] - i * 0.1, pts[1] - float(myList_small[5]) - i * 0.1)]
                    msp.add_lwpolyline(points, dxfattribs={'color': 150})
    for i, myList_small in enumerate(data_all_change):
        if C == myList_small[1]:  # 判断查询的是第几个图名
            if myList_small[4] == '90.0':  # 判断旋转角,90.0为竖直方向
                length = len(myList_small[2])  # 判断字符长度
                pts = np.array(myList_small[3].strip().split(','), float)
                # print(pts)
                for i in range(10):
                    points = [(pts[0] - float(myList_small[5]) - i * 0.1, pts[1] - i * 0.1),
                              (pts[0] + float(myList_small[5]) + i * 0.1, pts[1] - i * 0.1),
                              (pts[0] + float(myList_small[5]) + i * 0.1,
                               pts[1] + length * float(myList_small[5]) * 3 / 5 + i * 0.1),
                              (pts[0] - float(myList_small[5]) - i * 0.1,
                               pts[1] + length * float(myList_small[5]) * 3 / 5 + i * 0.1),
                              (pts[0] - float(myList_small[5]) - i * 0.1, pts[1] - i * 0.1)]
                    msp.add_lwpolyline(points, dxfattribs={'color': 100})

            else:  # 水平方向
                length = len(myList_small[2])
                pts = np.array(myList_small[3].strip().split(','), float)
                for i in range(10):
                    points = [(pts[0] - i * 0.1, pts[1] - float(myList_small[5]) - i * 0.1),
                              (pts[0] + length * float(myList_small[5]) * 3 / 5 + i * 0.1,
                               pts[1] - float(myList_small[5]) - i * 0.1),
                              (pts[0] + length * float(myList_small[5]) * 3 / 5 + i * 0.1,
                               pts[1] + float(myList_small[5]) + i * 0.1),
                              (pts[0] - i * 0.1, pts[1] + float(myList_small[5]) + i * 0.1),
                              (pts[0] - i * 0.1, pts[1] - float(myList_small[5]) - i * 0.1)]
                    msp.add_lwpolyline(points, dxfattribs={'color': 100})
    doc.saveas(path_query_dxf)
def inquire_draw(query_char, dir_dxf):

    (data_all, data_all_change) = database_query(query_char)  # 查询

    if len(data_all) == 0 and len(data_all_change) == 0:
        print("查无结果")
    else:
        myList_up = []  # 创造显示查询数据暂时存储的数组
        for cnt, sub_data in enumerate(data_all_change):
            if not sub_data[1] in myList_up:  # 去掉图名重复显示
                myList_up.append(sub_data[1])
        for cnt, sub_data in enumerate(data_all):
            if not sub_data[1] in myList_up:  # 去掉图名重复显示
                myList_up.append(sub_data[1])

        while (1):
            print(myList_up)
            print('请输入想要查寻图名:')
            number_inquire = input()
            C = str(number_inquire)  # C为查询输入图名
            dxf_both_draw(data_all, data_all_change, dir_dxf, C)
            print('查询成功')


if __name__ == '__main__':
    dir_dxf = '/DXF/'  # dxf原图路径
    print('请输入想要查寻编码:')
    number_inquire = input()
    query_char = str(number_inquire)
    inquire_draw(query_char, dir_dxf)



  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值