python - 如何使用 python 将 .dxf 文件转换为 .json?
问题描述
所以基本上我正在使用 Blender 处理保存在 .dxf 文件中的一组 2D 实体。我需要使用 Python 从 Blender 中的 .dxf 文件中读取一些点,以便稍后测量它们之间的距离(它们通常是折线的转折点),它们似乎没有可以帮助我识别它们的特定名称.
我阅读了关于 dxf-parser 的信息,但它暗示了 Node.js 的使用和知识,因此我需要在 Python 中进行。最终,我也会接受一些关于如何在 python 中执行 Node.js 脚本的建议,该脚本会将我的 .dxf 文件转换为 json。
解决方案
回答您的问题:Node.js 是一个使用来自任何地方的包的框架,包括 python。你永远不会直接使用 Node.js 来做这样的转换工作,因为这样的工作应该属于一个特定的包(无论是已经开发它的人还是你正在开发的东西),比如 python。
我也在做同样的转换,这是非常直观和直接的同一个项目。我采取的做法是:
- 创建一个 python 包,作为一个整体完成转换工作
- 从https://ezdxf.readthedocs.io/en/stable/introduction.html导入 ezdxf 包
- 阅读网站上的文档,该文档为您提供了如何访问属性/类(如图层、表格、实体)的基本概念
- 通过 .dxf 文件的简单 for 循环提取所需的属性
- 导出整个对象并将其转换为 json 文件
我有一个代码片段,简要地告诉您如何为 .dxf 文件提取图层,您可以参考一下:
from datetime import datetime
import json
import sys
from typing import Optional, Tuple
from unittest.mock import _ANY
from ezdxf.filemanagement import readfile
class Scanner:
__filename: str = ''
__version: str = ''
__output: dict = {}
__layer: dict = {}
__current_layer: dict = {}
def print(self):
print(self.__output)
def __init__(self):
return
def read_file(self, file: str):
# read the name of the file as string
# & update it for the private variable
self.__filename = file
self.__output['name'] = self.__filename
try:
# store the dxf file as 'Drawing' class into __content
# self.__content = readfile('%s.dxf' % file, errors='ignore')
self.__content = readfile(file)
except IOError:
# print('Not a DXF file or a generic I/O error.')
print(IOError.strerror)
sys.exit(1)
except UnicodeDecodeError:
print('Illegal sequence of str characters')
sys.exit(2)
def analyze(self):
print('analyzing...')
self.__output['Date'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
self.__output['Unit'] = str(self.__content.units)
self.analyze_layers()
# self.analyze_entities()
# print(self.__output.get('Date'))
print('analyze finished')
def analyze_layers(self):
handle: _ANY
owner: str
name: str
flags: str
color: str
true_color: str
line_type: str
plot: str
line_weight: str
plotstyle_handle: str
material_handle: str
rgb: Optional[Tuple[int, int, int]]
description: str
transparency: float
print('analyzing layers...')
doc = self.__content
# extract information within layers &
# store them within a dict
for layer in doc.layers:
self.__current_layer['handle'] = layer.dxf.handle
self.__current_layer['owner'] = str(layer.dxf.owner)
# name_binary = layer.dxf.name
# name_utf8 = name_binary.encode('ascii').decode('utf-8')
# self.__current_layer['name'] = str(name_utf8)
self.__current_layer['name'] = layer.dxf.name
self.__current_layer['flags'] = str(layer.dxf.flags)
self.__current_layer['color'] = str(layer.color)
self.__current_layer['true_color'] = str(layer.dxf.true_color)
self.__current_layer['line_type'] = str(layer.dxf.linetype)
self.__current_layer['plot'] = str(layer.dxf.plot)
self.__current_layer['line_weight'] = str(layer.dxf.lineweight)
self.__current_layer['plotstyle_handle'] = str(
layer.dxf.plotstyle_handle)
self.__current_layer['material_handle'] = str(
layer.dxf.material_handle)
self.__current_layer['rgb'] = layer.rgb
self.__current_layer['description'] = layer.description
self.__current_layer['transparency'] = layer.transparency
# store __current_layer dict to self.__current_layer
self.__layer[str(layer.dxf.name)] = self.__current_layer
# reset self.__current_layer
self.__current_layer = {}
self.__output['layers'] = self.__layer
print('layer analyze finished')
def analyze_entities(self):
def analyze_images(self):
def tojson(self):
# Serializing json
json_object = json.dumps(self.__output, indent=4, ensure_ascii=False)
with open("sample.json", "w", encoding='utf-8') as outfile:
outfile.write(json_object)
print(json_object)
推荐阅读
- nginx - 如何在 nginx 中为某些文件扩展名添加带有代理传递的缓存控制标头
- javascript - 为什么我的数组没有填充到 HTTP 请求回调函数中?
- python - 为什么 tesseract 检测不到这个字母?
- azure - 基于客户 ID 的 Azure 服务总线队列分区
- set - 理解嵌套循环创建集合
- python - 从另一个 DataFrame 将列添加到 Pyspark DataFrame
- list - prolog 中的有效括号列表
- ubuntu - 在 ubuntu 20.04 中使用 make 命令时出错
- python - pip install gspread 安装到 python 而不是 py
- mysql - 将 MySQL 表行旋转到列