首页 > Python资料 博客日记

Python解析.dwg格式文件信息提取

2024-08-20 09:00:06Python资料围观50

这篇文章介绍了Python解析.dwg格式文件信息提取,分享给大家做个参考,收藏Python资料网收获更多编程知识

一、装环境

这里搞的人头疼,装了一大堆,各种报错,最后也不知道是靠哪个包运行成功的,反正成了

1、下载 ODAFileConverter

从Open Design Alliance官网(ODA文件转换器下载页面)下载适用于Ubuntu的DEB包。

2、安装 ODAFileConverter

(1)、安装 gdebi 来处理依赖关系

sudo apt update
sudo apt install gdebi-core

(2)、使用 gdebi 安装 DEB 包

gdebi ODAFileConverter_QT6_lnxX64_8.3dll_25.4.deb

3、解决 libxcb库问题

现代的Ubuntu版本(例如22.04)可能缺少 libxcb-util.so.0 库。可以通过创建符号链接来解决这个问题:

(1)、安装依赖

sudo apt-get update
sudo apt-get -f install
sudo apt install libxcb-xinerama0 libxcb-util1 libxkbcommon-x11-0 libglu1-mesa libxrender1 libxi6 libxrandr2 xvfb gdal-bin
apt-get install aptitude
sudo aptitude install qt5-default qtbase5-dev qtbase5-dev-tools

(2)、设置环境变量

vim ~/.bashrc
# 写入下面的内容
export XDG_RUNTIME_DIR=/tmp/runtime-$USER
mkdir -p $XDG_RUNTIME_DIR
chmod 700 $XDG_RUNTIME_DIR
# :wq保存
source ~/.bashrc

(3)、确认 libxcb-util.so.1 是否存在

ls /usr/lib/x86_64-linux-gnu/libxcb-util.so.1

(4)、创建符号链接

cd /usr/lib/x86_64-linux-gnu
sudo ln -s libxcb-util.so.1 libxcb-util.so.0

4、安装python包

conda install -c conda-forge gdal
pip install ezdxf

二、上代码

from ezdxf.addons import odafc
import ezdxf
from osgeo import ogr
import sys
import os
import json
import re

class dwg_analysis:
    def __init__(self, filepath, format):
        self.filepath = filepath
        self.format = format

    def dwg2data(self):
        dxf_file = os.path.join(os.path.dirname(self.filepath), os.path.basename(self.filepath).split(".")[0] + ".dxf")
        odafc.convert(self.filepath, dxf_file, version='R2000', replace=True)  
        
        doc = ezdxf.readfile(dxf_file)
        msp = doc.modelspace()

        data = {}
                 # 获取TEXT实体
        texts = msp.query('TEXT')
        text_data = []
        if format == "dwg_txt":
            for text in texts:
                decoded_str = re.sub(r'\\U\+([0-9A-Fa-f]{4})', lambda m: chr(int(m.group(1), 16)), text.dxf.text)
                text_data.append(decoded_str)
            filtered_list = [item for item in text_data if not (isinstance(item, (int, float)) or (isinstance(item, str) and str.isdigit(item)) or (isinstance(item, str) and item.isdigit()))]
            data['TEXT'] = self.remove_duplicates(filtered_list)
            return data

        for text in texts:
            decoded_str = re.sub(r'\\U\+([0-9A-Fa-f]{4})', lambda m: chr(int(m.group(1), 16)), text.dxf.text)
            text_info = {
                'text': decoded_str,
                'insert': (text.dxf.insert[0], text.dxf.insert[1]),
                'height': text.dxf.height,
                'rotation': text.dxf.rotation,
                'style': text.dxf.style,
                'layer': text.dxf.layer
            }
            text_data.append(text_info)

        data['TEXT'] = text_data
                # 获取LINE实体
        lines = msp.query('LINE')
        line_data = []
        for line in lines:
            line_data.append({
                'start': (line.dxf.start[0], line.dxf.start[1]),
                'end': (line.dxf.end[0], line.dxf.end[1])
            })
        data['LINE'] = line_data

        # 获取POLYLINE实体
        polylines = msp.query('POLYLINE')
        polyline_data = []
        for polyline in polylines:
            points = []
            for point in polyline.points():
                points.append((point[0], point[1]))
            polyline_data.append(points)
        data['POLYLINE'] = polyline_data

        # 获取CIRCLE实体
        circles = msp.query('CIRCLE')
        circle_data = []
        for circle in circles:
            circle_data.append({
                'center': (circle.dxf.center[0], circle.dxf.center[1]),
                'radius': circle.dxf.radius
            })
        data['CIRCLE'] = circle_data

        # 获取ARC实体
        arcs = msp.query('ARC')
        arc_data = []
        for arc in arcs:
            arc_data.append({
                'center': (arc.dxf.center[0], arc.dxf.center[1]),
                'radius': arc.dxf.radius,
                'start_angle': arc.dxf.start_angle,
                'end_angle': arc.dxf.end_angle
            })
        data['ARC'] = arc_data

        # 获取ELLIPSE实体
        ellipses = msp.query('ELLIPSE')
        ellipse_data = []
        for ellipse in ellipses:
            ellipse_data.append({
                'center': (ellipse.dxf.center[0], ellipse.dxf.center[1]),
                'major_axis': (ellipse.dxf.major_axis[0], ellipse.dxf.major_axis[1]),
                'ratio': ellipse.dxf.ratio,
                'start_param': ellipse.dxf.start_param,
                'end_param': ellipse.dxf.end_param
            })
        data['ELLIPSE'] = ellipse_data

        return data
    def remove_duplicates(self,lst):
        res = []
        seen = {}
        for i in lst:
            if i not in seen:
                seen[i] = 1
                res.append(i)
        return res
# 示例调用
if __name__ == "__main__":
    # DWG文件路径
    DWG_path = "/home/hyh/data/Maintenance_test_data/AIN.dwg"
    format = "dwg_txt"
    dwf = dwg_analysis(DWG_path,format)
    dwf_txt = dwf.dwg2data()
    output_path = os.path.join(os.path.dirname(DWG_path), os.path.basename(DWG_path).split(".")[0] + "_" +format+ ".json")
    with open(output_path, 'w', encoding='utf-8') as f:
        json.dump(dwf_txt, f, ensure_ascii=False, indent=4)




版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!

标签:

相关文章

本站推荐