3

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.

2

The only way I see is to;

  1. Copy the current style/renderer
  2. Change it to a new simple one
  3. Then restore after DxfExporter is done.

In QgsMapLayerStyleManager it's possible to do "setOverrideStyle" if you already have another style added. I haven't found any way to create a new simple style and add it to the QgsMapLayerStyleManager.

But it's possible to make a child class from DxfLayer and add your own "setOverrideStyle" functions, like this:


class MyDxfLayer(QgsDxfExport.DxfLayer):
    def __init__(self, vectorLayer, attributeIndex):
        super(MyDxfLayer, self).__init__(vectorLayer, attributeIndex)
        self.layer = vectorLayer
        self.attributeIndex = attributeIndex
        self.renderer = None

    def setOverrideStyle(self):
        from qgis.core import QgsFeatureRenderer
        self.renderer = self.layer.renderer().clone()
        newRenderer = QgsFeatureRenderer.defaultRenderer(
            self.layer.geometryType()
        )
        self.layer.setRenderer(newRenderer)

    def restoreOverrideStyle(self):
        if self.renderer is None:
            return
        self.layer.setRenderer(self.renderer)
        self.renderer = None

Then change the line:

dxflayer = dxf.DxfLayer(layer, 4)

to:

dxflayer = MyDxfLayer(layer, 4)
dxflayer.setOverrideStyle()

and after dxf.writeToFile(f, "utf-8") restore the previous style

for l in dxflayers:
    l.restoreOverrideStyle()

In this way QGis 3.6 export all feature of a layer to your DXF file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.