Branch data Line data Source code
1 : : /*************************************************************************** 2 : : qgsalgorithmdxfexport.cpp 3 : : --------------------- 4 : : Date : September 2020 5 : : Copyright : (C) 2020 by Alexander Bruy 6 : : Email : alexander dot bruy at gmail dot com 7 : : *************************************************************************** 8 : : * * 9 : : * This program is free software; you can redistribute it and/or modify * 10 : : * it under the terms of the GNU General Public License as published by * 11 : : * the Free Software Foundation; either version 2 of the License, or * 12 : : * (at your option) any later version. * 13 : : * * 14 : : ***************************************************************************/ 15 : : 16 : : #include "qgsalgorithmdxfexport.h" 17 : : 18 : : #include "qgsprocessingparameterdxflayers.h" 19 : : #include "qgsdxfexport.h" 20 : : 21 : : ///@cond PRIVATE 22 : : 23 : 0 : QString QgsDxfExportAlgorithm::name() const 24 : : { 25 : 0 : return QStringLiteral( "dxfexport" ); 26 : : } 27 : : 28 : 0 : QString QgsDxfExportAlgorithm::displayName() const 29 : : { 30 : 0 : return QObject::tr( "Export layers to DXF" ); 31 : : } 32 : : 33 : 0 : QStringList QgsDxfExportAlgorithm::tags() const 34 : : { 35 : 0 : return QObject::tr( "layer,export,dxf,cad,dwg" ).split( ',' ); 36 : 0 : } 37 : : 38 : 0 : QString QgsDxfExportAlgorithm::group() const 39 : : { 40 : 0 : return QObject::tr( "Vector general" ); 41 : : } 42 : : 43 : 0 : QString QgsDxfExportAlgorithm::groupId() const 44 : : { 45 : 0 : return QStringLiteral( "vectorgeneral" ); 46 : : } 47 : : 48 : 0 : QString QgsDxfExportAlgorithm::shortHelpString() const 49 : : { 50 : 0 : return QObject::tr( "Exports layers to DXF file. For each layer, you can choose a field whose values are used to split features in generated destination layers in the DXF output." ); 51 : : } 52 : : 53 : 0 : QgsDxfExportAlgorithm *QgsDxfExportAlgorithm::createInstance() const 54 : : { 55 : 0 : return new QgsDxfExportAlgorithm(); 56 : : } 57 : : 58 : 0 : void QgsDxfExportAlgorithm::initAlgorithm( const QVariantMap & ) 59 : : { 60 : 0 : addParameter( new QgsProcessingParameterDxfLayers( QStringLiteral( "LAYERS" ), QObject::tr( "Input layers" ) ) ); 61 : 0 : addParameter( new QgsProcessingParameterEnum( QStringLiteral( "SYMBOLOGY_MODE" ), QObject::tr( "Symbology mode" ), QStringList() << QObject::tr( "No Symbology" ) << QObject::tr( "Feature Symbology" ) << QObject::tr( "Symbol Layer Symbology" ), false, 0 ) ); 62 : 0 : addParameter( new QgsProcessingParameterScale( QStringLiteral( "SYMBOLOGY_SCALE" ), QObject::tr( "Symbology scale" ), 1000000 ) ); 63 : 0 : addParameter( new QgsProcessingParameterEnum( QStringLiteral( "ENCODING" ), QObject::tr( "Encoding" ), QgsDxfExport::encodings(), false, QVariant(), false, true ) ); 64 : 0 : addParameter( new QgsProcessingParameterCrs( QStringLiteral( "CRS" ), QObject::tr( "CRS" ), QStringLiteral( "EPSG:4326" ) ) ); 65 : 0 : addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "USE_LAYER_TITLE" ), QObject::tr( "Use layer title as name" ), false ) ); 66 : 0 : addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "FORCE_2D" ), QObject::tr( "Force 2D output" ), false ) ); 67 : 0 : addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "MTEXT" ), QObject::tr( "Export labels as MTEXT elements" ), true ) ); 68 : 0 : addParameter( new QgsProcessingParameterFileDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "DXF" ), QObject::tr( "DXF Files" ) + " (*.dxf *.DXF)" ) ); 69 : 0 : } 70 : : 71 : 0 : QVariantMap QgsDxfExportAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) 72 : : { 73 : 0 : QgsMapSettings mapSettings; 74 : 0 : mapSettings.setTransformContext( context.transformContext() ); 75 : : 76 : 0 : QList<QgsVectorLayer *> mapLayers; 77 : : 78 : 0 : QVariant layersVariant = parameters.value( parameterDefinition( QStringLiteral( "LAYERS" ) )->name() ); 79 : 0 : const QList<QgsDxfExport::DxfLayer> layers = QgsProcessingParameterDxfLayers::parameterAsLayers( layersVariant, context ); 80 : 0 : for ( const QgsDxfExport::DxfLayer &layer : layers ) 81 : : { 82 : 0 : if ( !layer.layer() ) 83 : 0 : throw QgsProcessingException( QObject::tr( "Unknown input layer" ) ); 84 : : 85 : 0 : mapLayers.push_back( layer.layer() ); 86 : : } 87 : : 88 : 0 : QgsDxfExport::SymbologyExport symbologyMode = static_cast< QgsDxfExport::SymbologyExport >( parameterAsInt( parameters, QStringLiteral( "SYMBOLOGY_MODE" ), context ) ); 89 : 0 : double symbologyScale = parameterAsDouble( parameters, QStringLiteral( "SYMBOLOGY_SCALE" ), context ); 90 : 0 : QString encoding = QgsDxfExport::encodings().at( parameterAsInt( parameters, QStringLiteral( "ENCODING" ), context ) ); 91 : 0 : QgsCoordinateReferenceSystem crs = parameterAsCrs( parameters, QStringLiteral( "CRS" ), context ); 92 : 0 : bool useLayerTitle = parameterAsBool( parameters, QStringLiteral( "USE_LAYER_TITLE" ), context ); 93 : 0 : bool useMText = parameterAsBool( parameters, QStringLiteral( "MTEXT" ), context ); 94 : 0 : bool force2D = parameterAsBool( parameters, QStringLiteral( "FORCE_2D" ), context ); 95 : 0 : QString outputFile = parameterAsFileOutput( parameters, QStringLiteral( "OUTPUT" ), context ); 96 : : 97 : 0 : QgsDxfExport dxfExport; 98 : : 99 : 0 : dxfExport.setMapSettings( mapSettings ); 100 : 0 : dxfExport.addLayers( layers ); 101 : 0 : dxfExport.setSymbologyScale( symbologyScale ); 102 : 0 : dxfExport.setSymbologyExport( symbologyMode ); 103 : 0 : dxfExport.setLayerTitleAsName( useLayerTitle ); 104 : 0 : dxfExport.setDestinationCrs( crs ); 105 : 0 : dxfExport.setForce2d( force2D ); 106 : : 107 : 0 : QgsDxfExport::Flags flags = QgsDxfExport::Flags(); 108 : 0 : if ( !useMText ) 109 : 0 : flags = flags | QgsDxfExport::FlagNoMText; 110 : 0 : dxfExport.setFlags( flags ); 111 : : 112 : 0 : QFile dxfFile( outputFile ); 113 : 0 : switch ( dxfExport.writeToFile( &dxfFile, encoding ) ) 114 : : { 115 : : case QgsDxfExport::ExportResult::Success: 116 : 0 : feedback->pushInfo( QObject::tr( "DXF export completed" ) ); 117 : 0 : break; 118 : : 119 : : case QgsDxfExport::ExportResult::DeviceNotWritableError: 120 : 0 : throw QgsProcessingException( QObject::tr( "DXF export failed, device is not writable" ) ); 121 : : break; 122 : : 123 : : case QgsDxfExport::ExportResult::InvalidDeviceError: 124 : 0 : throw QgsProcessingException( QObject::tr( "DXF export failed, the device is invalid" ) ); 125 : : break; 126 : : 127 : : case QgsDxfExport::ExportResult::EmptyExtentError: 128 : 0 : throw QgsProcessingException( QObject::tr( "DXF export failed, the extent could not be determined" ) ); 129 : : break; 130 : : } 131 : : 132 : 0 : QVariantMap outputs; 133 : 0 : outputs.insert( QStringLiteral( "OUTPUT" ), outputFile ); 134 : 0 : return outputs; 135 : 0 : } 136 : : 137 : : ///@endcond