I made a plugin to quickly analyse dxf files for work. It works fine on most .dxf files, but for some reason it does not work on all. The ones that do not work properly are imported as an "image" of some sorts. They do contain attribute values but do not poses any lines.

However, when I import those files with the "add vector layer" tool the lines do show up! I did discover that the .dxf files that do not work also contain point data, while the files that do work only contain line data.

I use the following code to import .dxf files. I am only interested in the line data of the .dxf files. Is it possible that there is something wrong with the code?

dxf_path = self.dlg.lineEdit_dxf.text()
dxf_info = "|layername = entities|geometryType=LineString"
dxf_output_filename = os.path.splitext(os.path.basename(dxf_path))[0]
dxf_vl = QgsVectorLayer(dxf_path + dxf_info, dxf_output_filename + ".dxf", 'ogr')
if dxf_vl.isValid() == True:
  • What if you remove blank spaces in layer URI? i.e., "|layername = entities|geometryType=LineString" by "|layername=entities|geometryType=LineString" In general, you can load a layer manually, read its URI (for example by hovering the mouse pointer above layer name in QGIS layer tree), and use such URI in your PyQGIS script. – Germán Carrillo Dec 12 '16 at 2:09
  • Thank you for your suggestion. Sadly, the problem still occurs even when removing the blank spaces within the URI. What is strange is that when I manually load a .dxf file that does not work, the line data reads the same URI as I try to import. But when I try it with the above mentioned script, the URI is missing the |geometryType=LineString part! Also when trying to load the "point" data or "GeometryCollection" data from the .dxf, the resulting import does not contain a geometryType... – Frank Dec 12 '16 at 7:23
  • 1
    Note that the PyQGIS Cookbook mentions a geometrytype parameter (all letters are lower-case), not geometryType. – Germán Carrillo Dec 12 '16 at 11:54
  • Carrillo, thank you! Using geometrytype instead of geometryType solved the problem. It is weird though that the geometryType does work on some of the .dxf files. Anyway, thank for solving this problem! – Frank Dec 12 '16 at 12:00
  • Great! I've posted my comment as an answer. – Germán Carrillo Dec 12 '16 at 12:16

When constructing the layer URI, remove blank spaces and use geometrytype parameter (all in lower-case) instead of geometryType. See the PyQGIS Cookbook for reference.

The relevant part in your code would become this:

dxf_info = "|layername=entities|geometrytype=LineString"
| improve this answer | |

Your Answer

