win10中使用libredwg和dxfgrabber解析dwg/dxf信息

因为工作中需要解析CAD文件中的信息,用于后续的处理和分析。这里简单介绍下我的方法,具体实现因为工作原因不能详细说明,这里只是讲了一下核心的代码部分。

一、工具安装

pip install dxfgrabber

二、dwg转dxf

这里要用到dwg2dxf.exe,我们在之前安装的libredwg文件夹中可以找到,通过python调用命令行的形式来转化dwg文件。这里为什么要转化呢?因为dwg是AutoCAD公司的加密后的文件,不同版本的解析方法不同,而dxf格式为开源的文件格式,目前我们使用python直接进行解析信息。

    commd="dwg2dxf.exe"    
    # 设置转化工具参数
    verbosity = " -v{}".format(verbosity)
    versions = " --r{}".format(versions)
    outfile = ' -o "{}"'.format(outfile_path)

    try:
        subprocess.run(
            '"' + commd + '"' + verbosity + versions + outfile + " " + '"' + infiles + '"',
            timeout=20,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
        # rc, out = subprocess.getstatusoutput('"'+commd+'"' + verbosity + versions + outfile +" "+ '"'+infiles+'"')
        # print(out)
    except:
        error_txt.append(infiles)
        print("dwg to dxf fail!!!,please check to see if it contains "
              "'gbk' char .({})".format('"' + commd + '"' + verbosity + versions + outfile + " " + '"' + infiles + '"'))

三、使用python解析dxf文件

这里使用了dxfgrabber,具体使用方法可以参考其开发文档。

import dxfgrabber

dxf = dxfgrabber.readfile(file_name)

    # 解析dxf文件
    # 存储文本
    txt=[]
    insert_block=[]
    for type in dxf.entities:
        if type.dxftype == "MTEXT":
            # 保存多行文本信息
            txt.append(TEXTINFO(type.raw_text,
                                type.insert[0],type.insert[1],
                                type.height,type.rect_width,
                                type.font,type.line_spacing))

        elif type.dxftype == "TEXT":
            # 保存单行文本信息、
            txt.append(TEXTINFO(type.text,
                                type.insert[0],type.insert[1],
                                type.height,type.width,
                                type.font))
        elif type.dxftype=="LINE":
              print("=======================>")
              print("坐标:", type.start,type.end)
        elif type.dxftype == "INSERT":
            if not type.name in dxf.blocks:
                continue

            block_info=BLOCKINFO()
            blocks=dxf.blocks[type.name]
            for entity in blocks:
                if entity.dxftype=="TEXT":
                    block_info.add_text(entity.text,
                                        entity.insert[0], entity.insert[1],
                                        entity.height, entity.width,
                                        entity.font)
                elif entity.dxftype == "MTEXT":
                    block_info.add_text(entity.raw_text,
                                        entity.insert[0], entity.insert[1],
                                        entity.height, entity.rect_width,
                                        entity.font, entity.line_spacing)

                elif entity.dxftype=="LINE":
                    block_info.add_line(entity.start,entity.end)
            insert_block.append(block_info)

四、总结

这种方法能解析一部分dwg数据信息,但是其不能解决层的信息,或有部分会有信息缺失的现象,所以对信息重构时会造成困扰。当然这种方法也有优点,他不依靠CAD而能够直接运行,速度相对来说也比较快。后续我也会记录下我是用objectARX来解析CAD信息,相对来说这种方法比较稳定,毕竟是直接带哦用AutoCAD底层代码,相对来说更加准确和容易解析。建议小伙伴们在做相关工作时优先考虑ObjectARX,网上也有很多资料。

猜你喜欢

转载自blog.csdn.net/wxplol/article/details/105070922