First commit
commit
d9b9a121b0
@ -0,0 +1,101 @@
|
|||||||
|
"""Extract points from a dxf file, and save in csv format
|
||||||
|
|
||||||
|
Example:
|
||||||
|
Extract coordinates from all layers
|
||||||
|
> python dxf_to_xyz.py file.dxf
|
||||||
|
|
||||||
|
Extract coordinates from one layer
|
||||||
|
> python dxf_to_xyz.py file.dxf 'survey'
|
||||||
|
"""
|
||||||
|
|
||||||
|
__author__ = "D. Howe"
|
||||||
|
__version__ = "0.1.0"
|
||||||
|
__email__ = "d.howe@wrl.unsw.edu.au"
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import numpy as np
|
||||||
|
import pandas as pd
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
import dxfgrabber
|
||||||
|
|
||||||
|
|
||||||
|
def convert(dxf_name, layer_name):
|
||||||
|
# Read dxf file
|
||||||
|
dxf = dxfgrabber.readfile(dxf_name)
|
||||||
|
|
||||||
|
# Get data from DTM
|
||||||
|
print('Available layers in {}'.format(dxf_name))
|
||||||
|
layers = sorted([layer.name for layer in dxf.layers])
|
||||||
|
for layer in layers:
|
||||||
|
print(layer)
|
||||||
|
|
||||||
|
if layer_name:
|
||||||
|
print("\nExtracting data from layer '{}'".format(layer_name))
|
||||||
|
entities = [
|
||||||
|
e for e in dxf.entities if e.layer.lower() == layer_name.lower()
|
||||||
|
]
|
||||||
|
else:
|
||||||
|
# Take all layers if no layer name given
|
||||||
|
print('\nExtracing data from all layers')
|
||||||
|
entities = [e for e in dxf.entities]
|
||||||
|
|
||||||
|
# Get coordinates of entities
|
||||||
|
data = []
|
||||||
|
for entity in entities:
|
||||||
|
if entity.dxftype == 'POINT':
|
||||||
|
point = entity.point
|
||||||
|
row = {key: val for key, val in zip(['x', 'y', 'z'], [*point])}
|
||||||
|
row['layer'] = entity.layer
|
||||||
|
row['id'] = entity.handle
|
||||||
|
row['type'] = entity.dxftype
|
||||||
|
data.append(row)
|
||||||
|
elif entity.dxftype == 'LINE':
|
||||||
|
points = [entity.start, entity.end]
|
||||||
|
for point in points:
|
||||||
|
row = {key: val for key, val in zip(['x', 'y', 'z'], [*point])}
|
||||||
|
row['layer'] = entity.layer
|
||||||
|
row['id'] = entity.handle
|
||||||
|
row['type'] = entity.dxftype
|
||||||
|
data.append(row)
|
||||||
|
elif entity.dxftype == 'POLYLINE':
|
||||||
|
for vertex in entity.vertices:
|
||||||
|
point = vertex.location
|
||||||
|
row = {key: val for key, val in zip(['x', 'y', 'z'], [*point])}
|
||||||
|
row['layer'] = entity.layer
|
||||||
|
row['id'] = entity.handle
|
||||||
|
row['type'] = entity.dxftype
|
||||||
|
data.append(row)
|
||||||
|
elif entity.dxftype == 'LWPOLYLINE':
|
||||||
|
for point in entity.points:
|
||||||
|
row = {key: val for key, val in zip(['x', 'y', 'z'], [*point])}
|
||||||
|
row['layer'] = entity.layer
|
||||||
|
row['id'] = entity.handle
|
||||||
|
row['type'] = entity.dxftype
|
||||||
|
data.append(row)
|
||||||
|
|
||||||
|
|
||||||
|
# Add to dataframe
|
||||||
|
points = pd.DataFrame(data)
|
||||||
|
points = points[['layer', 'type', 'id', 'x', 'y', 'z']]
|
||||||
|
|
||||||
|
# Export points
|
||||||
|
if layer_name:
|
||||||
|
csv_name = dxf_name.replace('.dxf', '-{}.csv'.format(layer_name))
|
||||||
|
else:
|
||||||
|
csv_name = dxf_name.replace('.dxf', '.csv')
|
||||||
|
points.to_csv(csv_name, index=False)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
dxf_name = sys.argv[1]
|
||||||
|
try:
|
||||||
|
layer_name = sys.argv[2]
|
||||||
|
except IndexError:
|
||||||
|
layer_name = None
|
||||||
|
|
||||||
|
convert(dxf_name, layer_name)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
Loading…
Reference in New Issue