qgis / QGIS

QGIS is a free, open source, cross platform (lin/win/mac) geographical information system (GIS)
https://qgis.org
GNU General Public License v2.0
10.59k stars 3k forks source link

Import DXF file from PyQGIS is not the same result if import from "Import Layers from DXF/DWG" menu #33198

Open oddworldng opened 4 years ago

oddworldng commented 4 years ago

Describe the bug I'm developing a simple plugin to load multiple DXF files to the TOC (PyQGIS), the problem is that it isn't the same result if I load this file from QGIS menu.

How to Reproduce

In QGIS menu:

In PyQGIS:

dfx_file = r"file.dxf"
vlayer = QgsVectorLayer(dfx_file, 'layer_test', 'ogr')
subLayers = vlayer.dataProvider().subLayers()

#For each sublayer, diferent type of geometry, load a layer to map.
for subLayer in subLayers:
    #Extract the geometry type
    geom_type = subLayer.split('!!::!!')[3]
    #Set the path
    uri = "%s|layername=entities|geometrytype=%s" % (dfx_file, geom_type,)
    #Name for sub layer
    dfx_file_name = os.path.splitext(os.path.basename(dfx_file))[0]
    layer_name = "%s - %s" % (dfx_file_name,geom_type,)
    #Create layer
    sub_vlayer = QgsVectorLayer(uri, layer_name, 'ogr')
    #Add layer to map
    QgsProject.instance().addMapLayer(sub_vlayer)

QGIS and OS versions

I'm using Windows 10 and QGIS 3.10 'A Coruña'.

Additional context

For example, QGIS attribute table is not the same in both cases.

This is a dxf example file: E1822P_Modelo_Alumbrado_propuesto.dxf

andreasneumann commented 4 years ago

This is probably not a bug. There are simply two ways of opening/importing DXF in QGIS:

The latter imports the DXF/DWG into a geopackage, and expands block geometries, styles, label settings, etc. and creates symbology rules in QGIS.

The code you list above is doing the first option.

I am not sure how the second method can be replicated using Python code. You would have to ask @jef-n for support. You can open a support request with his company Norbit (https://www.norbit.de/)

andreasneumann commented 4 years ago

Not sure if the second method has Python API exposed. @jef-n would know more about it.

oddworldng commented 4 years ago

Would be great to have access to this functionality from PyQGIS

andreasneumann commented 4 years ago

I think this should be changed to a feature request. I just checked the Python API. There is some DXF stuff in the API, but all is export related.

oddworldng commented 2 years ago

Hi, any news on this issue?

alansaviolobo commented 8 months ago

subscribing