Use libredwg and dxfgrabber to parse dwg/dxf information in win10

Because the information in the CAD file needs to be parsed in the work for subsequent processing and analysis. Here is a brief introduction to my method, the specific implementation cannot be explained in detail due to work reasons, here is just the core code part.

1. Tool installation

pip install dxfgrabber

Two, dwg to dxf

Here we need to use dwg2dxf.exe , which we can find in the libredwg folder installed before, and use python to call the command line to transform the dwg file. Why is it transformed here? Because dwg is an encrypted file of AutoCAD, different versions have different parsing methods, and the dxf format is an open source file format. At present, we use python to directly parse the information.

    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 + '"'))

Three, use python to parse dxf files

Dxfgrabber is used here , and the specific usage method can refer to its development document.

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)

Four, summary

This method can analyze part of the dwg data information, but it cannot solve the layer information, or some information will be missing, so it will cause confusion when the information is reconstructed. Of course, this method also has advantages. It can run directly without relying on CAD, and the speed is relatively fast. I will also record that I use objectARX to parse the CAD information. Relatively speaking, this method is relatively stable. After all, I use AutoCAD low-level code directly, which is relatively more accurate and easier to parse. It is recommended that friends give priority to ObjectARX when doing related work. There are also many materials on the Internet.

Guess you like

Origin blog.csdn.net/wxplol/article/details/105070922