I have a simple script to export a layer to a dxf file:
layer = iface.activeLayer()
dxf = QgsDxfExport()
dxfPath = os.path.realpath("C:/abc/test.dxf")
extent = QgsRectangle()
dxflayers = []
dxflayer = dxf.DxfLayer(layer, -1)
dxflayers.append(dxflayer)
extent.combineExtentWith(layer.extent())
extent = extent.buffered(100) # To include features close to edge of extent
dxf.setDestinationCrs(layer.crs())
dxf.addLayers(dxflayers)
dxf.setExtent(extent)
f = QFile(dxfPath)
f.open(QIODevice.ReadWrite)
dxf.writeToFile(f, "utf-8")
f.close()
# Load new file to Qgis
iface.mainWindow().blockSignals(True)
newLayer = QgsVectorLayer(dxfPath, "DXF", "ogr")
iface.mainWindow().blockSignals(False)
dxfGroup = QgsLayerTreeGroup("New DXF", True)
QgsProject.instance().layerTreeRoot().addChildNode(dxfGroup)
newLayer.setCrs(layer.crs())
allLayers = newLayer.dataProvider().subLayers()
for lay in allLayers:
geomType = lay.split(
QgsDataProvider.SUBLAYER_SEPARATOR
)[3]
uri = f"{dxfPath}|layername=entities|geometrytype={geomType}"
layerName = f"DXF - {geomType}"
iface.mainWindow().blockSignals(True)
l = QgsVectorLayer(uri, layerName, "ogr")
iface.mainWindow().blockSignals(False)
l.setCrs(layer.crs())
QgsProject.instance().addMapLayer(l, False)
dxfGroup.addLayer(l)
But since Qgis 3.6 i have a problem that not all features are exported. I think it's the commit dxf export: don't label invisible features (fixes #19604) that cause this problem.
My layer is a shapefile with a rule based style and only some of the rules are visible on the map, but i want to export all features when i create the dxf.
How can i change the behavior of the QgsDxfExport() to include all features?
I don't want to change the style on the layer because i don't know if it's saved or not.