Add 'point_to_row' function to avoid code duplication

master
Dan Howe 7 years ago
parent d9b9a121b0
commit ff16ea841c

@ -17,9 +17,20 @@ import sys
import numpy as np import numpy as np
import pandas as pd import pandas as pd
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import argparse
import dxfgrabber import dxfgrabber
def point_to_row(point, layer, dxf_type, dxf_id):
"""Create a dict from a point with layer and e properties"""
row = {key: val for key, val in zip(['x', 'y', 'z'], [*point])}
row['layer'] = layer
row['type'] = dxf_type
row['id'] = dxf_id
return row
def convert(dxf_name, layer_name): def convert(dxf_name, layer_name):
# Read dxf file # Read dxf file
dxf = dxfgrabber.readfile(dxf_name) dxf = dxfgrabber.readfile(dxf_name)
@ -42,39 +53,26 @@ def convert(dxf_name, layer_name):
# Get coordinates of entities # Get coordinates of entities
data = [] data = []
for entity in entities: for e in entities:
if entity.dxftype == 'POINT': if e.dxftype == 'POINT':
point = entity.point point = e.point
row = {key: val for key, val in zip(['x', 'y', 'z'], [*point])} row = point_to_row(point, e.layer, e.dxftype, e.handle)
row['layer'] = entity.layer
row['id'] = entity.handle
row['type'] = entity.dxftype
data.append(row) data.append(row)
elif entity.dxftype == 'LINE': elif e.dxftype == 'LINE':
points = [entity.start, entity.end] points = [e.start, e.end]
for point in points: for point in points:
row = {key: val for key, val in zip(['x', 'y', 'z'], [*point])} row = point_to_row(point, e.layer, e.dxftype, e.handle)
row['layer'] = entity.layer
row['id'] = entity.handle
row['type'] = entity.dxftype
data.append(row) data.append(row)
elif entity.dxftype == 'POLYLINE': elif e.dxftype == 'POLYLINE':
for vertex in entity.vertices: for vertex in e.vertices:
point = vertex.location point = vertex.location
row = {key: val for key, val in zip(['x', 'y', 'z'], [*point])} row = point_to_row(point, e.layer, e.dxftype, e.handle)
row['layer'] = entity.layer
row['id'] = entity.handle
row['type'] = entity.dxftype
data.append(row) data.append(row)
elif entity.dxftype == 'LWPOLYLINE': elif e.dxftype == 'LWPOLYLINE':
for point in entity.points: for point in e.points:
row = {key: val for key, val in zip(['x', 'y', 'z'], [*point])} row = point_to_row(point, e.layer, e.dxftype, e.handle)
row['layer'] = entity.layer
row['id'] = entity.handle
row['type'] = entity.dxftype
data.append(row) data.append(row)
# Add to dataframe # Add to dataframe
points = pd.DataFrame(data) points = pd.DataFrame(data)
points = points[['layer', 'type', 'id', 'x', 'y', 'z']] points = points[['layer', 'type', 'id', 'x', 'y', 'z']]

Loading…
Cancel
Save