1

I'm trying to convert shapefiles into one DXF file using pyqgis, I started with one layer, the problem is that I can't open the DXF file after conversion. Screen capture when trying to open converted dxf

I've tried two approaches but still getting the same problem.
First approach: based on this answer

from PyQt4.QtCore import QFile
dxfExport = QgsDxfExport()
layers=[QgsDxfExport.DxfLayer(iface.activeLayer())]
settings = iface.mapCanvas().mapSettings()
dxfExport.setMapSettings( settings )
dxfExport.addLayers( layers )
dxfExport.setLayerTitleAsName(True)
dxfExport.setDestinationCrs(26191)
dxfExport.setForce2d( True )
dxfFile = QFile( 'C:\\Users\\Desktop\\test\\file.dxf' )
dxfExport.writeToFile( dxfFile, 'utf-8')

second approach : based on this answer

I converted my shapefile to geojson then used FWtools to convert the geojson file to dxf.

ogr2ogr -f DXF test.dxf test.geojson

Is it possible to access source code of 'project>DXF export' funcionality ?

GreyHippo
  • 2,180
  • 2
  • 17
  • 30
ayar anasse
  • 437
  • 3
  • 12

1 Answers1

1

Have you tried using ogr2ogr with the shapefile?

ogr2ogr -f DXF myfile.dxf my_shapefile.shp
GreyHippo
  • 2,180
  • 2
  • 17
  • 30
  • Yes I did, the output can't be opened in AutoCAD – ayar anasse Oct 25 '18 at 08:22
  • Can you share your shapefile? If not try exporting the same shapefile using qgis and see if that will export properly to DXF. – GreyHippo Oct 25 '18 at 13:31
  • I don't know why it didn't work using command prompt but excuting the command in the QGIS python console fixed the problem. The code I used is :

    import subprocess command = ["C:\Program Files\QGIS 2.18\bin\ogr2ogr.exe","-f", "DXF","C:\Users\Desktop\output_dxf.dxf","C:\Users\Desktop\b\input_shp.shp"] subprocess.check_call(command) . But I steel can't convert many shapfiles to one DXF file

    – ayar anasse Oct 25 '18 at 13:43
  • You can first merge all shapefile into one (https://gis.stackexchange.com/questions/25061/merging-multiple-vector-layers-to-one-layer-using-qgis) then export to DXF – GreyHippo Oct 25 '18 at 13:59
  • unfortunately they don't have the same geometry type – ayar anasse Oct 25 '18 at 14:01