Shapefiles to Revit or AutoCAD: Using Python to write DXF

29 Oct

I have shown how to get shapefiles in Revit Using Qgis, and mentioned how to do the same with AutoDesk Civil and ESRI ArcMap. But I have another way – Python.

I have written a program that will read in a point shapefile and convert it to DXF. If you have a survey point shapefile, run my script and you will have a survey point DXF. I would love to write to DWG but AutoDesk charges for that library – proprietary file formats and all….

I always start my programs with baby steps – no matter how simple the problem. So let me walk through how this code developed.

First, I drew a DXF from Python manually – lines then points.

Lines:

from dxfwrite import DXFEngine as dxf
drawing = dxf.drawing(‘testLines.dxf’)
drawing.add_layer(‘SURVEY’)
drawing.add(dxf.line((0, 0), (1, 0), color=7, layer=’SURVEY’))
drawing.add(dxf.line((2, 0), (3, 0), color=5, layer=’SURVEY’))
drawing.save()

Points:

from dxfwrite import DXFEngine as dxf
drawing = dxf.drawing(‘DXF.dxf’)
drawing.add_layer(‘SURVEY’)
points = dxf.point((1, 5), color=7, layer=’SURVEY’)

drawing.add(points)

drawing.save()

Then I wanted to try from an Excel file since this is easy practice for passing points to the DXFEngine:

from dxfwrite import DXFEngine as dxf
import xlrd
book = xlrd.open_workbook(“readdxf.xls”)
sh = book.sheet_by_index(0)
drawing = dxf.drawing(‘DXFFromExcel.dxf’)
drawing.add_layer(‘SURVEY’)

def readRow():
for rownum in range(sh.nrows):
rows=sh.row_values(rownum)
points = dxf.point((sh.cell_value(rowx=rownum, colx=0), sh.cell_value(rowx=rownum, colx=1)), color=7, layer=’SURVEY’)
drawing.add(points)
print ‘Reading Record: ‘+ str(rownum)

else:
print ‘Complete…’

readRow()
drawing.save()

Lastly, I read from a shapefile:

from dxfwrite import DXFEngine as dxf
import shapefile
drawing = dxf.drawing(‘DXFFromSHP.dxf’)
drawing.add_layer(‘SURVEY’)
openshape = shapefile.Reader(“C:\Documents and Settings\user\Desktop\surveyShapefile”)
shpPoints = openshape.shapes()

def readShapefile():
x = 0
while (x <= 100):
points = dxf.point((shpPoints[x].points[0]), color=7, layer=’SURVEY’)
drawing.add(points)
print ‘Reading Record: ‘+ str(x)
x=x+1

else:
print ‘Complete…’

readShapefile()
drawing.save()

The loop is lame and only grabs the first 100 points. I just wanted to quickly show how to do it. Modify this anyway you want – you will need to add your file names anyway.

This file can also be modified to read and write polyline shapefiles too.

You will need to install Python, shapefile.py and DXFwrite.py for this to work. I have tested the DXF files in Qgis, but not in AutoCAD or Revit.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: