QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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 std::unique_ptr<QgsProcessingParameterMapTheme> mapThemeParam = std::make_unique<QgsProcessingParameterMapTheme>( QStringLiteral( "MAP_THEME" ), QObject::tr( "Map theme" ), QVariant(), true );
64 mapThemeParam->setHelp( QObject::tr( "Match layer styling to the provided map theme" ) );
65 addParameter( mapThemeParam.release() );
66 const QStringList encodings = QgsDxfExport::encodings();
67 addParameter( new QgsProcessingParameterEnum( QStringLiteral( "ENCODING" ), QObject::tr( "Encoding" ), encodings, false, encodings.at( 0 ), false, true ) );
68 addParameter( new QgsProcessingParameterCrs( QStringLiteral( "CRS" ), QObject::tr( "CRS" ), QStringLiteral( "EPSG:4326" ) ) );
69 std::unique_ptr<QgsProcessingParameterExtent> extentParam = std::make_unique<QgsProcessingParameterExtent>( QStringLiteral( "EXTENT" ), QObject::tr( "Extent" ), QVariant(), true );
70 extentParam->setHelp( QObject::tr( "Limit exported features to those with geometries intersecting the provided extent" ) );
71 addParameter( extentParam.release() );
72 addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "SELECTED_FEATURES_ONLY" ), QObject::tr( "Use only selected features" ), false ) );
73 addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "USE_LAYER_TITLE" ), QObject::tr( "Use layer title as name" ), false ) );
74 addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "FORCE_2D" ), QObject::tr( "Force 2D output" ), false ) );
75 addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "MTEXT" ), QObject::tr( "Export labels as MTEXT elements" ), true ) );
76 addParameter( new QgsProcessingParameterFileDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "DXF" ), QObject::tr( "DXF Files" ) + " (*.dxf *.DXF)" ) );
77}
78
79bool QgsDxfExportAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
80{
81 // Retrieve and clone layers
82 const QString mapTheme = parameterAsString( parameters, QStringLiteral( "MAP_THEME" ), context );
83 if ( !mapTheme.isEmpty() && context.project()->mapThemeCollection()->hasMapTheme( mapTheme ) )
84 {
85 mMapThemeStyleOverrides = context.project()->mapThemeCollection( )->mapThemeStyleOverrides( mapTheme );
86 }
87 return true;
88}
89
90QVariantMap QgsDxfExportAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
91{
92 QgsMapSettings mapSettings;
93 mapSettings.setTransformContext( context.transformContext() );
94 mapSettings.setLayerStyleOverrides( mMapThemeStyleOverrides );
95
96 QList<QgsVectorLayer *> mapLayers;
97
98 const QVariant layersVariant = parameters.value( parameterDefinition( QStringLiteral( "LAYERS" ) )->name() );
99 const QList<QgsDxfExport::DxfLayer> layers = QgsProcessingParameterDxfLayers::parameterAsLayers( layersVariant, context );
100 for ( const QgsDxfExport::DxfLayer &layer : layers )
101 {
102 if ( !layer.layer() )
103 throw QgsProcessingException( QObject::tr( "Unknown input layer" ) );
104
105 mapLayers.push_back( layer.layer() );
106 }
107
108 const Qgis::FeatureSymbologyExport symbologyMode = static_cast< Qgis::FeatureSymbologyExport >( parameterAsInt( parameters, QStringLiteral( "SYMBOLOGY_MODE" ), context ) );
109 const double symbologyScale = parameterAsDouble( parameters, QStringLiteral( "SYMBOLOGY_SCALE" ), context );
110 const QString encoding = parameterAsEnumString( parameters, QStringLiteral( "ENCODING" ), context );
111 const QgsCoordinateReferenceSystem crs = parameterAsCrs( parameters, QStringLiteral( "CRS" ), context );
112 const bool selectedFeaturesOnly = parameterAsBool( parameters, QStringLiteral( "SELECTED_FEATURES_ONLY" ), context );
113 const bool useLayerTitle = parameterAsBool( parameters, QStringLiteral( "USE_LAYER_TITLE" ), context );
114 const bool useMText = parameterAsBool( parameters, QStringLiteral( "MTEXT" ), context );
115 const bool force2D = parameterAsBool( parameters, QStringLiteral( "FORCE_2D" ), context );
116
117 QgsRectangle extent;
118 if ( parameters.value( QStringLiteral( "EXTENT" ) ).isValid() )
119 {
120 extent = parameterAsExtent( parameters, QStringLiteral( "EXTENT" ), context, crs );
121 }
122
123 const QString outputFile = parameterAsFileOutput( parameters, QStringLiteral( "OUTPUT" ), context );
124
125 QgsDxfExport dxfExport;
126
127 dxfExport.setMapSettings( mapSettings );
128 dxfExport.addLayers( layers );
129 dxfExport.setSymbologyScale( symbologyScale );
130 dxfExport.setSymbologyExport( symbologyMode );
131 dxfExport.setLayerTitleAsName( useLayerTitle );
132 dxfExport.setDestinationCrs( crs );
133 dxfExport.setForce2d( force2D );
134
135 if ( !extent.isEmpty() )
136 {
137 dxfExport.setExtent( extent );
138 }
139
141 if ( !useMText )
142 flags = flags | QgsDxfExport::FlagNoMText;
143 if ( selectedFeaturesOnly )
145 dxfExport.setFlags( flags );
146
147 QFile dxfFile( outputFile );
148 switch ( dxfExport.writeToFile( &dxfFile, encoding ) )
149 {
151 if ( !dxfExport.feedbackMessage().isEmpty() )
152 {
153 feedback->pushInfo( dxfExport.feedbackMessage() );
154 }
155 feedback->pushInfo( QObject::tr( "DXF export completed" ) );
156 break;
157
159 throw QgsProcessingException( QObject::tr( "DXF export failed, device is not writable" ) );
160 break;
161
163 throw QgsProcessingException( QObject::tr( "DXF export failed, the device is invalid" ) );
164 break;
165
167 throw QgsProcessingException( QObject::tr( "DXF export failed, the extent could not be determined" ) );
168 break;
169 }
170
171 QVariantMap outputs;
172 outputs.insert( QStringLiteral( "OUTPUT" ), outputFile );
173 return outputs;
174}
175
FeatureSymbologyExport
Options for exporting features considering their symbology.
Definition: qgis.h:4477
This class represents a coordinate reference system (CRS).
Exports QGIS layers to the DXF format.
Definition: qgsdxfexport.h:66
void setForce2d(bool force2d)
Force 2d output (eg.
Definition: qgsdxfexport.h:318
@ 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 setFlags(QgsDxfExport::Flags flags)
Sets the export flags.
@ FlagOnlySelectedFeatures
Use only selected features for the export.
Definition: qgsdxfexport.h:134
@ FlagNoMText
Export text as TEXT elements. If not set, text will be exported as MTEXT elements.
Definition: qgsdxfexport.h:133
void setDestinationCrs(const QgsCoordinateReferenceSystem &crs)
Set destination CRS.
void addLayers(const QList< QgsDxfExport::DxfLayer > &layers)
Add layers to export.
QFlags< Flag > Flags
Definition: qgsdxfexport.h:136
void setSymbologyScale(double scale)
Set reference scale for output.
Definition: qgsdxfexport.h:245
const QString feedbackMessage() const
Returns any feedback message produced while export to dxf file.
Definition: qgsdxfexport.h:238
void setExtent(const QgsRectangle &r)
Set extent of area to export.
Definition: qgsdxfexport.h:289
void setSymbologyExport(Qgis::FeatureSymbologyExport e)
Set symbology export mode.
Definition: qgsdxfexport.h:276
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:304
The QgsMapSettings class contains configuration for rendering of the map.
void setLayerStyleOverrides(const QMap< QString, QString > &overrides)
Sets the map of map layer style overrides (key: layer ID, value: style name) where a different style ...
void setTransformContext(const QgsCoordinateTransformContext &context)
Sets the coordinate transform context, which stores various information regarding which datum transfo...
bool hasMapTheme(const QString &name) const
Returns whether a map theme with a matching name exists.
QMap< QString, QString > mapThemeStyleOverrides(const QString &name)
Gets layer style overrides (for QgsMapSettings) of the visible layers for given map theme.
Contains information about the context in which a processing algorithm is executed.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
QgsProject * project() const
Returns the project in which the algorithm is being executed.
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.
QgsMapThemeCollection * mapThemeCollection
Definition: qgsproject.h:115
A rectangle specified with double values.
Definition: qgsrectangle.h:42
bool isEmpty() const
Returns true if the rectangle has no area.
Definition: qgsrectangle.h:492
const QgsCoordinateReferenceSystem & crs
Layers and optional attribute index to split into multiple layers using attribute value as layer name...
Definition: qgsdxfexport.h:75