QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
16 #include "qgsalgorithmdxfexport.h"
17 
19 #include "qgsdxfexport.h"
20 
22 
23 QString QgsDxfExportAlgorithm::name() const
24 {
25  return QStringLiteral( "dxfexport" );
26 }
27 
28 QString QgsDxfExportAlgorithm::displayName() const
29 {
30  return QObject::tr( "Export layers to DXF" );
31 }
32 
33 QStringList QgsDxfExportAlgorithm::tags() const
34 {
35  return QObject::tr( "layer,export,dxf,cad,dwg" ).split( ',' );
36 }
37 
38 QString QgsDxfExportAlgorithm::group() const
39 {
40  return QObject::tr( "Vector general" );
41 }
42 
43 QString QgsDxfExportAlgorithm::groupId() const
44 {
45  return QStringLiteral( "vectorgeneral" );
46 }
47 
48 QString 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 
53 QgsDxfExportAlgorithm *QgsDxfExportAlgorithm::createInstance() const
54 {
55  return new QgsDxfExportAlgorithm();
56 }
57 
58 void 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 
71 QVariantMap 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 
QgsDxfExport::encodings
static QStringList encodings()
Returns list of available DXF encodings.
Definition: qgsdxfexport.cpp:2192
QgsDxfExport
Exports QGIS layers to the DXF format.
Definition: qgsdxfexport.h:64
QgsDxfExport::addLayers
void addLayers(const QList< QgsDxfExport::DxfLayer > &layers)
Add layers to export.
Definition: qgsdxfexport.cpp:91
QgsDxfExport::setSymbologyExport
void setSymbologyExport(QgsDxfExport::SymbologyExport e)
Set symbology export mode.
Definition: qgsdxfexport.h:255
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:37
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:105
QgsProcessingFeedback::pushInfo
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
Definition: qgsprocessingfeedback.cpp:77
QgsDxfExport::setDestinationCrs
void setDestinationCrs(const QgsCoordinateReferenceSystem &crs)
Set destination CRS.
Definition: qgsdxfexport.cpp:2382
QgsDxfExport::FlagNoMText
@ FlagNoMText
Export text as TEXT elements. If not set, text will be exported as MTEXT elements.
Definition: qgsdxfexport.h:113
QgsDxfExport::ExportResult::EmptyExtentError
@ EmptyExtentError
Empty extent, no extent given and no extent could be derived from layers.
QgsProcessingParameterScale
A double numeric parameter for map scale values.
Definition: qgsprocessingparameters.h:2408
QgsDxfExport::writeToFile
ExportResult writeToFile(QIODevice *d, const QString &codec)
Export to a dxf file in the given encoding.
Definition: qgsdxfexport.cpp:192
QgsProcessingParameterCrs
A coordinate reference system parameter for processing algorithms.
Definition: qgsprocessingparameters.h:1740
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
QgsProcessingParameterFileDestination
A generic file based destination parameter, for specifying the destination path for a file (non-map l...
Definition: qgsprocessingparameters.h:3451
QgsDxfExport::setSymbologyScale
void setSymbologyScale(double scale)
Set reference scale for output.
Definition: qgsdxfexport.h:221
QgsProcessingParameterDxfLayers
A parameter for Processing algorithms that need a list of input vector layers to export as DXF file -...
Definition: qgsprocessingparameterdxflayers.h:45
QgsDxfExport::setFlags
void setFlags(QgsDxfExport::Flags flags)
Sets the export flags.
Definition: qgsdxfexport.cpp:81
QgsDxfExport::setMapSettings
void setMapSettings(const QgsMapSettings &settings)
Set map settings and assign layer name attributes.
Definition: qgsdxfexport.cpp:76
QgsProcessingContext::transformContext
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Definition: qgsprocessingcontext.h:165
QgsMapSettings::setTransformContext
void setTransformContext(const QgsCoordinateTransformContext &context)
Sets the coordinate transform context, which stores various information regarding which datum transfo...
Definition: qgsmapsettings.cpp:473
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:211
qgsalgorithmdxfexport.h
QgsDxfExport::setForce2d
void setForce2d(bool force2d)
Force 2d output (eg.
Definition: qgsdxfexport.h:297
QgsDxfExport::SymbologyExport
SymbologyExport
Definition: qgsdxfexport.h:103
QgsDxfExport::ExportResult::DeviceNotWritableError
@ DeviceNotWritableError
Device not writable error.
QgsProcessingParameterBoolean
A boolean parameter for processing algorithms.
Definition: qgsprocessingparameters.h:1709
QgsDxfExport::DxfLayer
Layers and optional attribute index to split into multiple layers using attribute value as layer name...
Definition: qgsdxfexport.h:73
QgsDxfExport::ExportResult::InvalidDeviceError
@ InvalidDeviceError
Invalid device error.
qgsdxfexport.h
QgsProcessingParameterDxfLayers::parameterAsLayers
static QList< QgsDxfExport::DxfLayer > parameterAsLayers(const QVariant &layersVariant, QgsProcessingContext &context)
Converts a QVariant value (a QVariantList) to a list of input layers.
Definition: qgsprocessingparameterdxflayers.cpp:156
QgsProcessingParameterEnum
An enum based parameter for processing algorithms, allowing for selection from predefined values.
Definition: qgsprocessingparameters.h:2540
QgsMapSettings
The QgsMapSettings class contains configuration for rendering of the map. The rendering itself is don...
Definition: qgsmapsettings.h:88
qgsprocessingparameterdxflayers.h
QgsDxfExport::setLayerTitleAsName
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
QgsProcessingException
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82
QgsDxfExport::ExportResult::Success
@ Success
Successful export.