QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
qgsalgorithmdxfexport.cpp
Go to the documentation of this file.
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
17
19#include "qgsdxfexport.h"
20
22
23QString QgsDxfExportAlgorithm::name() const
24{
25 return QStringLiteral( "dxfexport" );
26}
27
28QString QgsDxfExportAlgorithm::displayName() const
29{
30 return QObject::tr( "Export layers to DXF" );
31}
32
33QStringList QgsDxfExportAlgorithm::tags() const
34{
35 return QObject::tr( "layer,export,dxf,cad,dwg" ).split( ',' );
36}
37
38QString QgsDxfExportAlgorithm::group() const
39{
40 return QObject::tr( "Vector general" );
41}
42
43QString QgsDxfExportAlgorithm::groupId() const
44{
45 return QStringLiteral( "vectorgeneral" );
46}
47
48QString QgsDxfExportAlgorithm::shortHelpString() const
49{
50 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
53QgsDxfExportAlgorithm *QgsDxfExportAlgorithm::createInstance() const
54{
55 return new QgsDxfExportAlgorithm();
56}
57
58void QgsDxfExportAlgorithm::initAlgorithm( const QVariantMap & )
59{
60 addParameter( new QgsProcessingParameterDxfLayers( QStringLiteral( "LAYERS" ), QObject::tr( "Input layers" ) ) );
61 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 addParameter( new QgsProcessingParameterScale( QStringLiteral( "SYMBOLOGY_SCALE" ), QObject::tr( "Symbology scale" ), 1000000 ) );
63 addParameter( new QgsProcessingParameterEnum( QStringLiteral( "ENCODING" ), QObject::tr( "Encoding" ), QgsDxfExport::encodings(), false, QVariant(), false, true ) );
64 addParameter( new QgsProcessingParameterCrs( QStringLiteral( "CRS" ), QObject::tr( "CRS" ), QStringLiteral( "EPSG:4326" ) ) );
65 addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "USE_LAYER_TITLE" ), QObject::tr( "Use layer title as name" ), false ) );
66 addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "FORCE_2D" ), QObject::tr( "Force 2D output" ), false ) );
67 addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "MTEXT" ), QObject::tr( "Export labels as MTEXT elements" ), true ) );
68 addParameter( new QgsProcessingParameterFileDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "DXF" ), QObject::tr( "DXF Files" ) + " (*.dxf *.DXF)" ) );
69}
70
71QVariantMap QgsDxfExportAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
72{
73 QgsMapSettings mapSettings;
74 mapSettings.setTransformContext( context.transformContext() );
75
76 QList<QgsVectorLayer *> mapLayers;
77
78 const QVariant layersVariant = parameters.value( parameterDefinition( QStringLiteral( "LAYERS" ) )->name() );
79 const QList<QgsDxfExport::DxfLayer> layers = QgsProcessingParameterDxfLayers::parameterAsLayers( layersVariant, context );
80 for ( const QgsDxfExport::DxfLayer &layer : layers )
81 {
82 if ( !layer.layer() )
83 throw QgsProcessingException( QObject::tr( "Unknown input layer" ) );
84
85 mapLayers.push_back( layer.layer() );
86 }
87
88 const QgsDxfExport::SymbologyExport symbologyMode = static_cast< QgsDxfExport::SymbologyExport >( parameterAsInt( parameters, QStringLiteral( "SYMBOLOGY_MODE" ), context ) );
89 const double symbologyScale = parameterAsDouble( parameters, QStringLiteral( "SYMBOLOGY_SCALE" ), context );
90 const QString encoding = parameterAsEnumString( parameters, QStringLiteral( "ENCODING" ), context );
91 const QgsCoordinateReferenceSystem crs = parameterAsCrs( parameters, QStringLiteral( "CRS" ), context );
92 const bool useLayerTitle = parameterAsBool( parameters, QStringLiteral( "USE_LAYER_TITLE" ), context );
93 const bool useMText = parameterAsBool( parameters, QStringLiteral( "MTEXT" ), context );
94 const bool force2D = parameterAsBool( parameters, QStringLiteral( "FORCE_2D" ), context );
95 const QString outputFile = parameterAsFileOutput( parameters, QStringLiteral( "OUTPUT" ), context );
96
97 QgsDxfExport dxfExport;
98
99 dxfExport.setMapSettings( mapSettings );
100 dxfExport.addLayers( layers );
101 dxfExport.setSymbologyScale( symbologyScale );
102 dxfExport.setSymbologyExport( symbologyMode );
103 dxfExport.setLayerTitleAsName( useLayerTitle );
104 dxfExport.setDestinationCrs( crs );
105 dxfExport.setForce2d( force2D );
106
107 QgsDxfExport::Flags flags = QgsDxfExport::Flags();
108 if ( !useMText )
109 flags = flags | QgsDxfExport::FlagNoMText;
110 dxfExport.setFlags( flags );
111
112 QFile dxfFile( outputFile );
113 switch ( dxfExport.writeToFile( &dxfFile, encoding ) )
114 {
116 feedback->pushInfo( QObject::tr( "DXF export completed" ) );
117 break;
118
120 throw QgsProcessingException( QObject::tr( "DXF export failed, device is not writable" ) );
121 break;
122
124 throw QgsProcessingException( QObject::tr( "DXF export failed, the device is invalid" ) );
125 break;
126
128 throw QgsProcessingException( QObject::tr( "DXF export failed, the extent could not be determined" ) );
129 break;
130 }
131
132 QVariantMap outputs;
133 outputs.insert( QStringLiteral( "OUTPUT" ), outputFile );
134 return outputs;
135}
136
This class represents a coordinate reference system (CRS).
Exports QGIS layers to the DXF format.
Definition: qgsdxfexport.h:65
void setForce2d(bool force2d)
Force 2d output (eg.
Definition: qgsdxfexport.h:297
@ DeviceNotWritableError
Device not writable error.
@ Success
Successful export.
@ EmptyExtentError
Empty extent, no extent given and no extent could be derived from layers.
@ InvalidDeviceError
Invalid device error.
ExportResult writeToFile(QIODevice *d, const QString &codec)
Export to a dxf file in the given encoding.
void setSymbologyExport(QgsDxfExport::SymbologyExport e)
Set symbology export mode.
Definition: qgsdxfexport.h:255
void setFlags(QgsDxfExport::Flags flags)
Sets the export flags.
@ FlagNoMText
Export text as TEXT elements. If not set, text will be exported as MTEXT elements.
Definition: qgsdxfexport.h:113
void setDestinationCrs(const QgsCoordinateReferenceSystem &crs)
Set destination CRS.
void addLayers(const QList< QgsDxfExport::DxfLayer > &layers)
Add layers to export.
void setSymbologyScale(double scale)
Set reference scale for output.
Definition: qgsdxfexport.h:221
static QStringList encodings()
Returns list of available DXF encodings.
void setMapSettings(const QgsMapSettings &settings)
Set map settings and assign layer name attributes.
void setLayerTitleAsName(bool layerTitleAsName)
Enable use of title (where set) instead of layer name, when attribute index of corresponding layer in...
Definition: qgsdxfexport.h:283
The QgsMapSettings class contains configuration for rendering of the map.
void setTransformContext(const QgsCoordinateTransformContext &context)
Sets the coordinate transform context, which stores various information regarding which datum transfo...
Contains information about the context in which a processing algorithm is executed.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Custom exception class for processing related exceptions.
Definition: qgsexception.h:83
Base class for providing feedback from a processing algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
A boolean parameter for processing algorithms.
A coordinate reference system parameter for processing algorithms.
A parameter for Processing algorithms that need a list of input vector layers to export as DXF file -...
static QList< QgsDxfExport::DxfLayer > parameterAsLayers(const QVariant &layersVariant, QgsProcessingContext &context)
Converts a QVariant value (a QVariantList) to a list of input layers.
An enum based parameter for processing algorithms, allowing for selection from predefined values.
A generic file based destination parameter, for specifying the destination path for a file (non-map l...
A double numeric parameter for map scale values.
const QgsCoordinateReferenceSystem & crs
Layers and optional attribute index to split into multiple layers using attribute value as layer name...
Definition: qgsdxfexport.h:74