Suggested solution: using the ogr library, with this approach it is possible to set the value of the "Layer" attribute of the DXF with an attribute from the inLayer.
from osgeo import ogr
import os
# Get the input Layer
inShapefile = "c://gis//wrld.shp"
inDriver = ogr.GetDriverByName("ESRI Shapefile")
inDataSource = inDriver.Open(inShapefile, 0)
inLayer = inDataSource.GetLayer()
# Create the output Layer
outDXF = "wrld.dxf"
outDriver = ogr.GetDriverByName("DXF")
# Create the output DXF
outDataSource = outDriver.CreateDataSource(outDXF)
outLayer = outDataSource.CreateLayer("wrld", geom_type=ogr.wkbPolygon)
# Add input Layer Fields to the output Layer
inLayerDefn = inLayer.GetLayerDefn()
for i in range(0, inLayerDefn.GetFieldCount()):
fieldDefn = inLayerDefn.GetFieldDefn(i)
outLayer.CreateField(fieldDefn)
# Get the output Layer's Feature Definition
outLayerDefn = outLayer.GetLayerDefn()
for i in range(0, inLayer.GetFeatureCount()):
# Get the input Feature
inFeature = inLayer.GetFeature(i)
# Create output Feature
outFeature = ogr.Feature(outLayerDefn)
# Add attribute by index from inFeature to pass to output Layer.
# Set the field_num (here 2) by the position of the field in the original file.
outFeature.SetField("Layer", inFeature.GetField(2))
# Set geometry.
geom = inFeature.GetGeometryRef()
outFeature.SetGeometry(geom)
# Add new feature to output Layer
outLayer.CreateFeature(outFeature)
inFeature = None
outFeature = None
# Save and close DataSources
inDataSource = None
outDataSource = None
EDIT: This can be achieved also by :
import subprocess
command = ["C:\\Program Files\\QGIS 2.14\\bin\\ogr2ogr.exe", "-f", "DXF", "output.dxf", "c:\\gis\\wrld.shp", "-sql", "select ISO as Layer from wrld"]
subprocess.check_call(command)
For lines use:
import subprocess
command = ["C:\\Program Files\\QGIS 2.14\\bin\\ogr2ogr.exe", "-f", "DXF", "output_l.dxf", "c:\\gis\\wrld.shp", "-sql", "select ISO as Layer from wrld", "-nlt", "MULTILINESTRING"]
subprocess.check_call(command)
[Note: ISO is a name of a field in wrld.shp]
Qgsdxfexport()
in your title but have you tried it? If so, what happened?