A block is just another kind of entity space, which can be inserted multiple times into other layouts and blocks by the INSERT entity also called block references, this is a very powerful and important concept of the DXF format.
块 Block 是一组可以被多次使用的实体集合,DXFTYPE 为 INSERT。
读取思路:
1)先过滤该图层的 INSERT 类型的 entity
2)通过名称寻找 Block
3)遍历该 Block 下的 entities
import json
import os
import ezdxf
import pyproj
from ezdxf.addons import geo
# XX请改成自己的数据
crs = pyproj.CRS('EPSG:XX')
wgs84 = pyproj.CRS('EPSG:4326')
def transformer(data, crs, wgs84):
result = []
if type(data) == tuple:
project = pyproj.Transformer.from_crs(crs, wgs84, always_xy=True)
wgs84_pts = project.transform(data[0], data[1])
result.append(wgs84_pts)
else:
for points in data:
if type(points) == list:
points = transformer(points, crs, wgs84)
result.append(points)
else:
project = pyproj.Transformer.from_crs(crs, wgs84, always_xy=True)
wgs84_pts = project.transform(points[0], points[1])
result.append(wgs84_pts)
return result
def printEzdxf(file, output, layername):
doc = ezdxf.readfile(file)
msp = doc.modelspace()
features = []
entities = msp.query('*[layer=="' + layername + '"]')
n = 0
for entity in entities:
print("dxftype: ", entity.DXFTYPE)
if entity.DXFTYPE == 'INSERT':
block = doc.blocks.get(track.dxf.name)
for block_entity in block.entity_space.entities:
if block_entity.DXFTYPE == 'POINT' \
or block_entity.DXFTYPE == 'LWPOLYLINE' or block_entity.DXFTYPE == 'LINE' or block_entity.DXFTYPE == 'POLYLINE' \
or block_entity.DXFTYPE == 'CIRCLE' \
or block_entity.DXFTYPE == 'ARC' \
or block_entity.DXFTYPE == 'SOLID' \
or block_entity.DXFTYPE == 'HATCH':
properties = {
"handle": str(entity.dxf.handle),
"type": str(entity.DXFTYPE),
"layer": str(entity.dxf.layer),
"color": str(entity.dxf.color),
"edge": 'block',
"block_name": str(track.dxf.name),
}
for attr in track.attribs:
properties[attr.dxf.tag] = attr.dxf.text
# print('attr: ', attr.dxf.tag)
features.append(
convert_by_control(block_entity, doc, coord_convert_params, properties,
entity.dxf.insert,
True))
n = n + 1
print('percent: ', n / len(entities) * 100)
feature_collection = {
"type": "FeatureCollection",
"features": features
}
with open(output, "wt", encoding="utf8") as fp:
json.dump(feature_collection, fp, indent=2)
print("output: ", output)
def export_geojson(entity, doc):
try:
geo_proxy = ezdxf.addons.geo.proxy(entity)
geometry = geo_proxy.__geo_interface__
coordinates = transformer(geometry['coordinates'], crs, wgs84)
print('geometry-after: ', coordinates)
layer = doc.layers.get(str(entity.dxf.layer).lower())
geo = {
"type": "Feature",
"geometry": {
"type": geometry['type'],
"coordinates": coordinates
},
"properties": {
"handle": str(entity.dxf.handle),
"type": str(entity.DXFTYPE),
"layer": str(entity.dxf.layer),
"color": str(layer.color),
}
}
return geo
except Exception as e:
print("!!! Error found in export_geojson(): ", e)
pass
if __name__ == "__main__":
try:
path = 'XX'
file = 'XX.dxf'
layername = 'XX'
print('file-start: ', file)
printEzdxf(os.path.join(path, file), file.split("\\")[-1].split(".")[0] + ".json", layername)
print('file-done: ', file)
print("### main(): done")
except Exception as e:
print("!!! Error found: ", e)
pass