3

I am trying to export my layer in a .dxf file. My code right now looks like this:

dxfExport = QgsDxfExport()
layers = [QgsDxfExport.DxfLayer(self.iface.activeLayer())]
settings = self.iface.mapCanvas().mapSettings()
dxfExport.setMapSettings(settings)
dxfExport.addLayers(layers)
dxfExport.setLayerTitleAsName(False)
dxfExport.setDestinationCrs(QgsCoordinateReferenceSystem("EPSG:2100"))
dxfFile = QFile('C:\\Users\\Desktop\\file.dxf')
dxfExport.writeToFile(dxfFile, 'utf-8')

What I would like to do though it's a bit more advanced. I would like based on the value of a column in the attribute table to have different layers in the .dxf file. If I have to make different layers and add the features corresponding to a different value on that column that's fine.

In the code above, even if I add more layers, there is only one LineString layer in the .dxf file.

1
  • 1
    On line 2 in your code you specify one specific layer (the actively selected layer) which you pass to the exporter on line 5. Make sure you add all the layers you want to export to layers. Use QgsProject to find all the mapLayers. Jun 9, 2023 at 3:27

1 Answer 1

2

I think that code can help you. It only does not work if the layer has one point object. I do not know why. If you can solve please tell me too.

import glob

layerList = QgsProject.instance().mapLayers().values()
output_f = 'C:\TEST'
CRS = QgsCoordinateReferenceSystem('EPSG:4326')
field_name = 'split_cl'

def split_layer(layer, field_name, output_f):
    
    CRS = QgsCoordinateReferenceSystem('EPSG:4326')
    layer_fields = layer.fields()
    idx_of_that_field = layer_fields.indexFromName(field_name)

    list_attributes = []

    for feat in layer.getFeatures():
        list_attributes.append(feat.attributes()[idx_of_that_field])
        geom = feat.geometry()

    list_attributes = set(list_attributes)
    path_to_file = output_f + '/'
    directory = str(layer.name())
    parent_dir = output_f + '/'
    path = os.path.join(parent_dir, directory)
    os.mkdir(path)
    working_dir = os.path.split(path_to_file)[0] + '/' + str(layer.name())

    for i in list_attributes:
        selection = layer.selectByExpression('{0}=\'{1}\''.format(field_name, i))
        writer = QgsVectorFileWriter.writeAsVectorFormat(layer, working_dir + "/{}.shp".format(i),
                                                        "UTF-8",
                                                        CRS,
                                                        driverName="ESRI Shapefile",
                                                        onlySelected=True)

    layer.removeSelection()

    for i in glob.glob(output_f + "/*.shp"):
        file = QFile(i)
        file.close()
        
def exportDXF_layers(input_f, output_f, file_name, CRS):
    layers = []
    vlayers = []

    for i in glob.glob(input_f + "/*.shp"):
        fileInfo = QFileInfo(i)
        baseNameproj = fileInfo.baseName()
        vlayer = QgsVectorLayer(str(i), baseNameproj, 'ogr')
        layers.append(QgsDxfExport.DxfLayer(vlayer))
        vlayers.append(vlayer)

    dxfExport = QgsDxfExport()
    settings = iface.mapCanvas().mapSettings()
    dxfExport.setMapSettings(settings)
    dxfExport.addLayers(layers)
    dxfExport.setLayerTitleAsName(True)
    dxfExport.setDestinationCrs(CRS)
    dxfExport.setForce2d(True)
    dxfExport.dxfEncoding("UTF-8")
    dxfFile = QFile(output_f + '/' + file_name + '.dxf')
    dxfExport.writeToFile(dxfFile, 'utm-8')
    dxfFile.close()

for i in layerList:
    print(i)
    split_layer(I, field_name, output_f)
    input_f = output_f+'\\'+str(i.name())
    file_name = i.name()
    exportDXF_layers(input_f, output_f, file_name, CRS)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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