The power of GDAL, and specifically ogr2ogr is pretty impressive. This conversion is from shp to DXF, which is a somewhat universal CAD format so further conversion should be possible.
This post will cover contour export while maintaining 3D elevation, in addition to contour values as layers in CAD. The data used is OS terrain 50.
OS Terrain 50:
Contours in 3D:
ogr2ogr -f DXF contour_zfield.dxf SX99SW_line.shp -zfield PROP_VALUE
With the -zfield creating the 3d elevation.
The alternative is to just store the z-value as layers.
ogr2ogr -f DXF contour_layer.dxf SX99SW_line.shp -sql "SELECT PROP_VALUE AS Layer FROM SX99SW_line"
Layers work great:
With the ogr2ogr DXF driver, if you have an input column called “Layer” then it will be used to group features as a layer in DXF. We use a SQL query to achive this. Prop_Value is the height field in my input data.
And putting them all together:
ogr2ogr -f DXF contour_zfield_layer.dxf SX99SW_line.shp -zfield PROP_VALUE -sql "SELECT PROP_VALUE AS Layer FROM SX99SW_line"
Result not as expected, flat output:
Adding our SQL select statement removes our zfield attribute as such ogr2ogr cannot access it. Lets resolve this:
ogr2ogr -f DXF contour_zfield_2_layer.dxf SX99SW_line.shp -zfield PROP_VALUE -sql "SELECT PROP_VALUE AS Layer, * FROM SX99SW_line"
Layers and height.