QGIS API Documentation 4.3.0-Master (7c7bd4d7018)
Loading...
Searching...
No Matches
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
18#include "qgsdxfexport.h"
20
21#include <QString>
22
23using namespace Qt::StringLiterals;
24
26
27QString QgsDxfExportAlgorithm::name() const
28{
29 return u"dxfexport"_s;
30}
31
32QString QgsDxfExportAlgorithm::displayName() const
33{
34 return QObject::tr( "Export layers to DXF" );
35}
36
37QStringList QgsDxfExportAlgorithm::tags() const
38{
39 return QObject::tr( "layer,export,dxf,cad,dwg" ).split( ',' );
40}
41
42QString QgsDxfExportAlgorithm::group() const
43{
44 return QObject::tr( "Vector general" );
45}
46
47QString QgsDxfExportAlgorithm::groupId() const
48{
49 return u"vectorgeneral"_s;
50}
51
52QString QgsDxfExportAlgorithm::shortHelpString() const
53{
54 return QObject::tr(
55 "Exports layers to a 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.\n\n"
56 "If no field is chosen, you can still override the output layer name by directly entering a new output layer name in the Configure Layer panel or by preferring layer title (set in layer "
57 "properties) to layer name."
58 );
59}
60
61QString QgsDxfExportAlgorithm::shortDescription() const
62{
63 return QObject::tr( "Exports layers to a DXF file." );
64}
65
66QgsDxfExportAlgorithm *QgsDxfExportAlgorithm::createInstance() const
67{
68 return new QgsDxfExportAlgorithm();
69}
70
71void QgsDxfExportAlgorithm::initAlgorithm( const QVariantMap & )
72{
73 addParameter( new QgsProcessingParameterDxfLayers( u"LAYERS"_s, QObject::tr( "Input layers" ) ) );
74 addParameter(
75 new QgsProcessingParameterEnum( u"SYMBOLOGY_MODE"_s, QObject::tr( "Symbology mode" ), QStringList() << QObject::tr( "No Symbology" ) << QObject::tr( "Feature Symbology" ) << QObject::tr( "Symbol Layer Symbology" ), false, 0 )
76 );
77 addParameter( new QgsProcessingParameterScale( u"SYMBOLOGY_SCALE"_s, QObject::tr( "Symbology scale" ), 1000000 ) );
78 auto mapThemeParam = std::make_unique<QgsProcessingParameterMapTheme>( u"MAP_THEME"_s, QObject::tr( "Map theme" ), QVariant(), true );
79 mapThemeParam->setHelp( QObject::tr( "Match layer styling to the provided map theme" ) );
80 addParameter( mapThemeParam.release() );
81 const QStringList encodings = QgsDxfExport::encodings();
82 addParameter( new QgsProcessingParameterEnum( u"ENCODING"_s, QObject::tr( "Encoding" ), encodings, false, encodings.at( 0 ), false, true ) );
83 addParameter( new QgsProcessingParameterCrs( u"CRS"_s, QObject::tr( "CRS" ), u"EPSG:4326"_s ) );
84 auto extentParam = std::make_unique<QgsProcessingParameterExtent>( u"EXTENT"_s, QObject::tr( "Extent" ), QVariant(), true );
85 extentParam->setHelp( QObject::tr( "Limit exported features to those with geometries intersecting the provided extent" ) );
86 addParameter( extentParam.release() );
87 addParameter( new QgsProcessingParameterBoolean( u"SELECTED_FEATURES_ONLY"_s, QObject::tr( "Use only selected features" ), false ) );
88 auto useTitleParam = std::make_unique<QgsProcessingParameterBoolean>( u"USE_LAYER_TITLE"_s, QObject::tr( "Use layer title as name" ), false );
89 useTitleParam->setHelp( QObject::tr( "If no attribute is chosen and layer name is not being overridden, prefer layer title (set in layer properties) to layer name" ) );
90 addParameter( useTitleParam.release() );
91 addParameter( new QgsProcessingParameterBoolean( u"FORCE_2D"_s, QObject::tr( "Force 2D output" ), false ) );
92 addParameter( new QgsProcessingParameterBoolean( u"MTEXT"_s, QObject::tr( "Export labels as MTEXT elements" ), true ) );
93 addParameter( new QgsProcessingParameterBoolean( u"EXPORT_LINES_WITH_ZERO_WIDTH"_s, QObject::tr( "Export lines with zero width" ) ), false );
94 addParameter( new QgsProcessingParameterFileDestination( u"OUTPUT"_s, QObject::tr( "DXF" ), QObject::tr( "DXF Files" ) + " (*.dxf *.DXF)" ) );
95}
96
97bool QgsDxfExportAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
98{
99 // Retrieve and clone layers
100 const QString mapTheme = parameterAsString( parameters, u"MAP_THEME"_s, context );
101 if ( QgsProject *project = context.project() )
102 {
103 if ( !mapTheme.isEmpty() && project->mapThemeCollection()->hasMapTheme( mapTheme ) )
104 {
105 mMapThemeStyleOverrides = project->mapThemeCollection()->mapThemeStyleOverrides( mapTheme );
106 }
107 mScaleMethod = project->scaleMethod();
108 }
109 return true;
110}
111
112QVariantMap QgsDxfExportAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
113{
114 QgsMapSettings mapSettings;
115 mapSettings.setTransformContext( context.transformContext() );
116 mapSettings.setLayerStyleOverrides( mMapThemeStyleOverrides );
117 mapSettings.setScaleMethod( mScaleMethod );
118 mapSettings.setEllipsoid( context.ellipsoid() );
119
120 QList<QgsVectorLayer *> mapLayers;
121
122 const QVariant layersVariant = parameters.value( parameterDefinition( u"LAYERS"_s )->name() );
123 const QList<QgsDxfExport::DxfLayer> layers = QgsProcessingParameterDxfLayers::parameterAsLayers( layersVariant, context );
124 for ( const QgsDxfExport::DxfLayer &layer : layers )
125 {
126 if ( !layer.layer() )
127 throw QgsProcessingException( QObject::tr( "Unknown input layer" ) );
128
129 mapLayers.push_back( layer.layer() );
130 }
131
132 const Qgis::FeatureSymbologyExport symbologyMode = static_cast<Qgis::FeatureSymbologyExport>( parameterAsInt( parameters, u"SYMBOLOGY_MODE"_s, context ) );
133 const double symbologyScale = parameterAsDouble( parameters, u"SYMBOLOGY_SCALE"_s, context );
134 const QString encoding = parameterAsEnumString( parameters, u"ENCODING"_s, context );
135 const QgsCoordinateReferenceSystem crs = parameterAsCrs( parameters, u"CRS"_s, context );
136 const bool selectedFeaturesOnly = parameterAsBool( parameters, u"SELECTED_FEATURES_ONLY"_s, context );
137 const bool useLayerTitle = parameterAsBool( parameters, u"USE_LAYER_TITLE"_s, context );
138 const bool useMText = parameterAsBool( parameters, u"MTEXT"_s, context );
139 const bool force2D = parameterAsBool( parameters, u"FORCE_2D"_s, context );
140 const bool exportLinesWithZeroWidth = parameterAsBool( parameters, u"EXPORT_LINES_WITH_ZERO_WIDTH"_s, context );
141
142 QgsRectangle extent;
143 if ( parameters.value( u"EXTENT"_s ).isValid() )
144 {
145 extent = parameterAsExtent( parameters, u"EXTENT"_s, context, crs );
146 }
147
148 const QString outputFile = parameterAsFileOutput( parameters, u"OUTPUT"_s, context );
149
150 QgsDxfExport dxfExport;
151
152 dxfExport.setMapSettings( mapSettings );
153 dxfExport.addLayers( layers );
154 dxfExport.setSymbologyScale( symbologyScale );
155 dxfExport.setSymbologyExport( symbologyMode );
156 dxfExport.setLayerTitleAsName( useLayerTitle );
157 dxfExport.setDestinationCrs( crs );
158 dxfExport.setForce2d( force2D );
159
160 if ( !extent.isEmpty() )
161 {
162 dxfExport.setExtent( extent );
163 }
164
166 if ( !useMText )
167 flags = flags | QgsDxfExport::FlagNoMText;
168 if ( selectedFeaturesOnly )
170 if ( exportLinesWithZeroWidth )
172 dxfExport.setFlags( flags );
173
174 QFile dxfFile( outputFile );
175 switch ( dxfExport.writeToFile( &dxfFile, encoding ) )
176 {
178 if ( !dxfExport.feedbackMessage().isEmpty() )
179 {
180 feedback->pushInfo( dxfExport.feedbackMessage() );
181 }
182 feedback->pushInfo( QObject::tr( "DXF export completed" ) );
183 break;
184
186 throw QgsProcessingException( QObject::tr( "DXF export failed, device is not writable" ) );
187 break;
188
190 throw QgsProcessingException( QObject::tr( "DXF export failed, the device is invalid" ) );
191 break;
192
194 throw QgsProcessingException( QObject::tr( "DXF export failed, the extent could not be determined" ) );
195 break;
196 }
197
198 QVariantMap outputs;
199 outputs.insert( u"OUTPUT"_s, outputFile );
200 return outputs;
201}
202
FeatureSymbologyExport
Options for exporting features considering their symbology.
Definition qgis.h:6180
Represents a coordinate reference system (CRS).
Exports QGIS layers to the DXF format.
void setForce2d(bool force2d)
Force 2d output (eg.
@ 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.
@ FlagHairlineWidthExport
Export all lines with minimum width and don't fill polygons.
@ FlagOnlySelectedFeatures
Use only selected features for the export.
@ FlagNoMText
Export text as TEXT elements. If not set, text will be exported as MTEXT elements.
void setDestinationCrs(const QgsCoordinateReferenceSystem &crs)
Set destination CRS.
void addLayers(const QList< QgsDxfExport::DxfLayer > &layers)
Add layers to export.
QFlags< Flag > Flags
void setSymbologyScale(double scale)
Set reference scale for output.
const QString feedbackMessage() const
Returns any feedback message produced while export to dxf file.
void setExtent(const QgsRectangle &r)
Set extent of area to export.
void setSymbologyExport(Qgis::FeatureSymbologyExport e)
Set symbology export mode.
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...
Contains configuration for rendering maps.
bool setEllipsoid(const QString &ellipsoid)
Sets the ellipsoid by its acronym.
void setScaleMethod(Qgis::ScaleCalculationMethod method)
Sets the method to use for scale calculations for 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...
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.
QString ellipsoid() const
Returns the ellipsoid to use for distance and area calculations.
Custom exception class for processing related exceptions.
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.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:114
A rectangle specified with double values.
Encapsulates the properties of a vector layer containing features that will be exported to the DXF fi...