Skip to content
Permalink
Browse files

[processing] add dxf export algorithm (fix #25392)

  • Loading branch information
alexbruy committed Nov 2, 2020
1 parent 8f48769 commit 7b36a220741dfcec7a070129f54881c140e5c876
@@ -55,6 +55,7 @@ SET(QGIS_ANALYSIS_SRCS
processing/qgsalgorithmdrape.cpp
processing/qgsalgorithmdropgeometry.cpp
processing/qgsalgorithmdropmzvalues.cpp
processing/qgsalgorithmdxfexport.cpp
processing/qgsalgorithmexecutepostgisquery.cpp
processing/qgsalgorithmexecutespatialitequery.cpp
processing/qgsalgorithmexecutespatialitequeryregistered.cpp
@@ -413,6 +414,7 @@ ENDIF (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
INCLUDE_DIRECTORIES(
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_SOURCE_DIR}/src/core/
${CMAKE_SOURCE_DIR}/src/core/dxf
${CMAKE_SOURCE_DIR}/src/core/geocoding
${CMAKE_SOURCE_DIR}/src/core/geometry
${CMAKE_SOURCE_DIR}/src/core/labeling
@@ -0,0 +1,137 @@
/***************************************************************************
qgsalgorithmdxfexport.cpp
---------------------
Date : September 2020
Copyright : (C) 2020 by Alexander Bruy
Email : alexamder dot bruy at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsalgorithmdxfexport.h"

#include "qgsprocessingparameterdxflayers.h"
#include "qgsdxfexport.h"

///@cond PRIVATE


QString QgsDxfExportAlgorithm::name() const
{
return QStringLiteral( "dxfexport" );
}

QString QgsDxfExportAlgorithm::displayName() const
{
return QObject::tr( "Export DXF" );
}

QStringList QgsDxfExportAlgorithm::tags() const
{
return QObject::tr( "layer,export,dxf,cad" ).split( ',' );
}

QString QgsDxfExportAlgorithm::group() const
{
return QObject::tr( "Vector general" );
}

QString QgsDxfExportAlgorithm::groupId() const
{
return QStringLiteral( "vectorgeneral" );
}

QString QgsDxfExportAlgorithm::shortHelpString() const
{
return QObject::tr( "Exports layers to DXF file." );
}

QgsDxfExportAlgorithm *QgsDxfExportAlgorithm::createInstance() const
{
return new QgsDxfExportAlgorithm();
}

void QgsDxfExportAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterDxfLayers( QStringLiteral( "LAYERS" ), QObject::tr( "Input layers" ) ) );
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 ) );
addParameter( new QgsProcessingParameterScale( QStringLiteral( "SYMBOLOGY_SCALE" ), QObject::tr( "Symbology scale" ), 1000000 ) );
addParameter( new QgsProcessingParameterEnum( QStringLiteral( "ENCODING" ), QObject::tr( "Encoding" ), QgsDxfExport::encodings() ) );
addParameter( new QgsProcessingParameterCrs( QStringLiteral( "CRS" ), QObject::tr( "CRS" ), QStringLiteral( "EPSG:4326" ) ) );
addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "USE_LAYER_TITLE" ), QObject::tr( "Use layer title as name" ), false ) );
addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "FORCE_2D" ), QObject::tr( "Force 2D output" ), false ) );
addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "MTEXT" ), QObject::tr( "Export labels as MTEXT element" ), true ) );
addParameter( new QgsProcessingParameterFileDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "DXF" ), QObject::tr( "DXF Files" ) + " (*.dxf *.DXF)" ) );
}

QVariantMap QgsDxfExportAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
QgsMapSettings mapSettings;
mapSettings.setDestinationCrs( context.project()->crs() );
mapSettings.setTransformContext( context.transformContext() );

QList<QgsVectorLayer *> mapLayers;

QVariant layersVariant = parameters.value( parameterDefinition( QStringLiteral( "LAYERS" ) )->name() );
const QList<QgsDxfExport::DxfLayer> layers = QgsProcessingParameterDxfLayers::parameterAsLayers( layersVariant, context );
for ( const QgsDxfExport::DxfLayer &layer : layers )
{
if ( !layer.layer() )
throw QgsProcessingException( QObject::tr( "Unknown input layer" ) );

mapLayers.push_back( layer.layer() );
}

QgsDxfExport::SymbologyExport symbologyMode = static_cast< QgsDxfExport::SymbologyExport >( parameterAsInt( parameters, QStringLiteral( "SYMBOLOGY_MODE" ), context ) );
double symbologyScale = parameterAsDouble( parameters, QStringLiteral( "SYMBOLOGY_SCALE" ), context );
QString encoding = QgsDxfExport::encodings().at( parameterAsInt( parameters, QStringLiteral( "ENCODING" ), context ) );
QgsCoordinateReferenceSystem crs = parameterAsCrs( parameters, QStringLiteral( "CRS" ), context );
bool useLayerTitle = parameterAsBool( parameters, QStringLiteral( "USE_LAYER_TITLE" ), context );
bool useMText = parameterAsBool( parameters, QStringLiteral( "MTEXT" ), context );
bool force2D = parameterAsBool( parameters, QStringLiteral( "FORCE_2D" ), context );
QString outputFile = parameterAsFileOutput( parameters, QStringLiteral( "OUTPUT" ), context );

QgsDxfExport dxfExport;

dxfExport.setMapSettings( mapSettings );
dxfExport.addLayers( layers );
dxfExport.setSymbologyScale( symbologyScale );
dxfExport.setSymbologyExport( symbologyMode );
dxfExport.setLayerTitleAsName( useLayerTitle );
dxfExport.setDestinationCrs( crs );
dxfExport.setForce2d( force2D );

QgsDxfExport::Flags flags = QgsDxfExport::Flags();
if ( useMText )
flags = flags | QgsDxfExport::FlagNoMText;
dxfExport.setFlags( flags );

QFile dxfFile( outputFile );
switch ( dxfExport.writeToFile( &dxfFile, encoding ) )
{
case QgsDxfExport::ExportResult::Success:
feedback->pushInfo( QObject::tr( "DXF export completed" ) );
break;

case QgsDxfExport::ExportResult::DeviceNotWritableError:
feedback->reportError( QObject::tr( "DXF export failed, device is not writable" ), true );
break;

case QgsDxfExport::ExportResult::InvalidDeviceError:
feedback->reportError( QObject::tr( "DXF export failed, the device is invalid" ), true );
break;

case QgsDxfExport::ExportResult::EmptyExtentError:
feedback->reportError( QObject::tr( "DXF export failed, the extent could not be determined" ), true );
break;
}

QVariantMap outputs;
outputs.insert( QStringLiteral( "OUTPUT" ), outputFile );
return outputs;
}
@@ -0,0 +1,45 @@
/***************************************************************************
qgsalgorithmdxfexport.h
---------------------
Date : September 2020
Copyright : (C) 2020 by Alexander Bruy
Email : alexander dot bruy at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSDXFEXPORTALGORITHM_H
#define QGSDXFEXPORTALGORITHM_H

#define SIP_NO_FILE

#include "qgsprocessingalgorithm.h"

///@cond PRIVATE

class QgsDxfExportAlgorithm : public QgsProcessingAlgorithm
{
public:
QgsDxfExportAlgorithm() = default;
QString name() const override;
QString displayName() const override;
QStringList tags() const override;
QString group() const override;
QString groupId() const override;
QString shortHelpString() const override;
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
QgsDxfExportAlgorithm *createInstance() const override SIP_FACTORY;

protected:
QVariantMap processAlgorithm( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
};

///@endcond PRIVATE

#endif // QGSDXFEXPORTALGORITHM_H
@@ -51,6 +51,7 @@
#include "qgsalgorithmdrape.h"
#include "qgsalgorithmdropgeometry.h"
#include "qgsalgorithmdropmzvalues.h"
#include "qgsalgorithmdxfexport.h"
#include "qgsalgorithmexecutepostgisquery.h"
#include "qgsalgorithmexecutespatialitequery.h"
#include "qgsalgorithmexecutespatialitequeryregistered.h"
@@ -276,6 +277,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsDrapeToZAlgorithm() );
addAlgorithm( new QgsDropGeometryAlgorithm() );
addAlgorithm( new QgsDropMZValuesAlgorithm() );
addAlgorithm( new QgsDxfExportAlgorithm() );
addAlgorithm( new QgsExecutePostgisQueryAlgorithm() );
addAlgorithm( new QgsExecuteRegisteredSpatialiteQueryAlgorithm() );
addAlgorithm( new QgsExecuteSpatialiteQueryAlgorithm() );

0 comments on commit 7b36a22

Please sign in to comment.
You can’t perform that action at this time.