When importing dxfs, you get a window asking you to select vector layers to add. With all the dxfs I've imported so far, that has had two layers called entities, one of which is points and one of which is linestrings. I select all, click OK, and then the file imports. This has to be done for each dxf.

Today I had to import approximately 200 dxfs (which I eventually combined using the data management tools.) Having to click on select all and then on OK, to select the vector layers for each and every one of those dxfs was very tedious!

Is it possible to set QGIS to select all automatically as it imports dxfs, without my having to sit there for half an hour repeating those clicks?

(The dxfs were created from NTF tiles, using the LL2DXF converter. Unfortunately, many of them will not open in Autocad due to an invalid block name so I can't combine them in CAD if there are lots. I do have a script that can strip out the blocks, but it is a little slow and has to be used one file at a time. That's OK for a small number, but today I had a very large area of map to convert and there are other towns to do over the coming weeks.)

I hope that this is not a duplicate question. I did have a search first but can't find anything similar.

As to how this is not really a duplicate, the other questions (that look similar) are all quite a bit older and the codes might not be relevant, the answers did not explain how to actually use the codes (and those codes appear to be different from the one that eventually solved this issue,), and a moderator told me in another thread that I am not supposed to comment or ask questions in an old thread and must start my own.

The answer given in the thread that has just been linked above is completely different and has no explanation how to use it.


If you want to take the Python route, you could use the answer posted at Loading DXF file as vector layer using PyQGIS? with some slight edits. I suggest (if you haven't already done so) to copy all your .dxf files to a single directory. Copy/paste the code below into a text editor and change the path variable path = "path/to/DXF" to the path for your directory. Then in QGIS, open the Python Console from the menubar Plugins > Python Console or press Ctrl + P, copy/paste the code from the text editor. This is the code:

import glob, os

# Enter directory path which holds the .dxf files (e.g. "C:/Users/You/Desktop/DXF folder/")
path = "path/to/DXF"
for layer in glob.glob(path + "*.dxf"):
    vlayer = QgsVectorLayer(layer, 'name', 'ogr')
    subLayers = vlayer.dataProvider().subLayers()
    for subLayer in subLayers:
        geom_type = subLayer.split(':')[-1]
        uri = "%s|layername=entities|geometrytype=%s" % (layer, geom_type,)
        dfx_file_name = os.path.splitext(os.path.basename(layer))[0]
        layer_name = "%s - %s" % (dfx_file_name,geom_type,)
        sub_vlayer = QgsVectorLayer(uri, layer_name, 'ogr')

This should prevent you from sitting there for half an hour :)

Note that if this answer helps (which is essentially the same as the one I linked to) then your question should be flagged as a duplicate.

| improve this answer | |

Test the PlugIn Another DXF Importer / DXF2Shape Converter

Multiple files can be imported at the same time (batch import). One click for a lot of files.

| improve this answer | |

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.