QGIS API Documentation 3.36.0-Maidenhead (09951dc0acf)
Loading...
Searching...
No Matches
qgsdxfexport.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsdxfexport.h
3 --------------
4 begin : September 2013
5 copyright : (C) 2013 by Marco Hugentobler
6 email : marco at sourcepole dot ch
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#ifndef QGSDXFEXPORT_H
19#define QGSDXFEXPORT_H
20
21#include "qgis_core.h"
22#include "qgis_sip.h"
23#include "qgis.h"
24#include "qgsgeometry.h"
25#include "qgsmapsettings.h"
26#include "qgslabelsink.h"
27#include "qgsrendercontext.h"
28
29#include <QColor>
30#include <QList>
31#include <QTextStream>
32
33class QgsMapLayer;
34class QgsPointXY;
35class QgsSymbolLayer;
36class QIODevice;
38class QgsCurve;
39class QgsCurvePolygon;
42struct DxfLayerJob;
44
45#define DXF_HANDSEED 100
46#define DXF_HANDMAX 9999999
47#define DXF_HANDPLOTSTYLE 0xf
48
49namespace pal // SIP_SKIP
50{
51 class LabelPosition;
52}
53
54
60#ifdef SIP_RUN
61class CORE_EXPORT QgsDxfExport
62{
63#else
64class CORE_EXPORT QgsDxfExport : public QgsLabelSink
65{
66#endif
67 public:
68
73 struct CORE_EXPORT DxfLayer
74 {
75 DxfLayer( QgsVectorLayer *vl, int layerOutputAttributeIndex = -1 )
76 : mLayer( vl )
77 , mLayerOutputAttributeIndex( layerOutputAttributeIndex )
78 {}
79
81 QgsVectorLayer *layer() const {return mLayer;}
82
88 int layerOutputAttributeIndex() const {return mLayerOutputAttributeIndex;}
89
96 QString splitLayerAttribute() const;
97
98 private:
99 QgsVectorLayer *mLayer = nullptr;
100 int mLayerOutputAttributeIndex = -1;
101 };
102
104 enum Flag SIP_ENUM_BASETYPE( IntFlag )
105 {
106 FlagNoMText = 1 << 1,
107 };
108 Q_DECLARE_FLAGS( Flags, Flag )
109
110
115 enum class ExportResult
116 {
117 Success = 0,
118 InvalidDeviceError,
119 DeviceNotWritableError,
120 EmptyExtentError
121 };
122
126 enum class VAlign : int
127 {
128 VBaseLine = 0,
129 VBottom = 1,
130 VMiddle = 2,
131 VTop = 3,
132 Undefined = 9999
133 };
134
136 enum class HAlign : int
137 {
138 HLeft = 0,
139 HCenter = 1,
140 HRight = 2,
141 HAligned = 3,
142 HMiddle = 4,
143 HFit = 5,
144 Undefined = 9999
145 };
146
153 {
154 Closed = 1,
155 Curve = 2,
156 Spline = 4,
157 Is3DPolyline = 8,
158 Is3DPolygonMesh = 16,
159 PolygonMesh = 32,
160 PolyfaceMesh = 64,
161 ContinuousPattern = 128,
162 };
163
164 Q_DECLARE_FLAGS( DxfPolylineFlags, DxfPolylineFlag )
165
166
170
171 ~QgsDxfExport() override;
172
177 void setMapSettings( const QgsMapSettings &settings );
178
183 void setFlags( QgsDxfExport::Flags flags );
184
189 QgsDxfExport::Flags flags() const;
190
196 void addLayers( const QList< QgsDxfExport::DxfLayer > &layers );
197
204 ExportResult writeToFile( QIODevice *d, const QString &codec ); //maybe add progress dialog? other parameters (e.g. scale, dpi)?
205
210 const QString feedbackMessage() const { return mFeedbackMessage; }
211
217 void setSymbologyScale( double scale ) { mSymbologyScale = scale; }
218
224 double symbologyScale() const { return mSymbologyScale; }
225
230 Qgis::DistanceUnit mapUnits() const;
231
236 void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
237
242 QgsCoordinateReferenceSystem destinationCrs() const;
243
248 void setSymbologyExport( Qgis::FeatureSymbologyExport e ) { mSymbologyExport = e; }
249
255 Qgis::FeatureSymbologyExport symbologyExport() const { return mSymbologyExport; }
256
261 void setExtent( const QgsRectangle &r ) { mExtent = r; }
262
268 QgsRectangle extent() const { return mExtent; }
269
276 void setLayerTitleAsName( bool layerTitleAsName ) { mLayerTitleAsName = layerTitleAsName; }
277
283 bool layerTitleAsName() { return mLayerTitleAsName; }
284
290 void setForce2d( bool force2d ) { mForce2d = force2d; }
291
297 bool force2d() { return mForce2d; }
298
303 static int closestColorMatch( QRgb color );
304
311 QString layerName( const QString &id, const QgsFeature &f ) const;
312
319 QString layerName( QgsVectorLayer *vl ) const;
320
327 void writeGroup( int code, int i ) SIP_PYNAME( writeGroupInt );
328
335 void writeGroup( int code, long long i ) SIP_PYNAME( writeGroupLong );
336
343 void writeGroup( int code, double d ) SIP_PYNAME( writeGroupDouble );
344
350 void writeGroup( int code, const QString &s );
351
358 void writeGroup( int code, const QgsPoint &p ) SIP_PYNAME( writeGroupPointV2 );
359
368 void writeGroup( const QColor &color, int exactMatch = 62, int rgbCode = 420, int transparencyCode = 440 );
369
374 void writeGroupCode( int code );
375
380 void writeInt( int i );
381
386 void writeDouble( double d );
387
392 void writeString( const QString &s );
393
400 int writeHandle( int code = 5, int handle = 0 );
401
411 void writePolyline( const QgsPointSequence &line, const QString &layer, const QString &lineStyleName, const QColor &color, double width = -1 ) SIP_SKIP;
412
423 void writePolyline( const QgsCurve &curve, const QString &layer, const QString &lineStyleName, const QColor &color, double width = -1 ) SIP_SKIP;
424
433 void writePolygon( const QgsRingSequence &polygon, const QString &layer, const QString &hatchPattern, const QColor &color ) SIP_SKIP;
434
444 void writePolygon( const QgsCurvePolygon &polygon, const QString &layer, const QString &hatchPattern, const QColor &color ) SIP_SKIP;
445
449 void writeLine( const QgsPoint &pt1, const QgsPoint &pt2, const QString &layer, const QString &lineStyleName, const QColor &color, double width = -1 );
450
455 void writePoint( const QString &layer, const QColor &color, const QgsPoint &pt ) SIP_PYNAME( writePointV2 );
456
461 void writeFilledCircle( const QString &layer, const QColor &color, const QgsPoint &pt, double radius ) SIP_PYNAME( writeFillCircleV2 );
462
467 void writeCircle( const QString &layer, const QColor &color, const QgsPoint &pt, double radius, const QString &lineStyleName, double width ) SIP_PYNAME( writeCircleV2 );
468
473 void writeText( const QString &layer, const QString &text, const QgsPoint &pt, double size, double angle, const QColor &color, QgsDxfExport::HAlign hali = QgsDxfExport::HAlign::Undefined, QgsDxfExport::VAlign vali = QgsDxfExport::VAlign::Undefined ) SIP_PYNAME( writeTextV2 );
474
479 void writeMText( const QString &layer, const QString &text, const QgsPoint &pt, double width, double angle, const QColor &color );
480
488 static double mapUnitScaleFactor( double scale, Qgis::RenderUnit symbolUnits, Qgis::DistanceUnit mapUnits, double mapUnitsPerPixel = 1.0 );
489
496 void clipValueToMapUnitScale( double &value, const QgsMapUnitScale &scale, double pixelToMMFactor ) const;
497
499 static QString dxfLayerName( const QString &name );
500
502 static QString dxfEncoding( const QString &name );
503
505 static QStringList encodings();
506
512 void drawLabel( const QString &layerId, QgsRenderContext &context, pal::LabelPosition *label, const QgsPalLayerSettings &settings ) SIP_SKIP override;
513
522 Q_DECL_DEPRECATED void registerDxfLayer( const QString &layerId, QgsFeatureId fid, const QString &layer );
523
524 private:
525
526#ifdef SIP_RUN
527 QgsDxfExport( const QgsDxfExport &other );
528 QgsDxfExport &operator=( const QgsDxfExport & );
529#endif
531 QgsRectangle mExtent;
533 double mSymbologyScale = 1.0;
536 bool mLayerTitleAsName = false;
537
538 QTextStream mTextStream;
539
540 int mSymbolLayerCounter = 0; //internal counter
541 int mNextHandleId = DXF_HANDSEED;
542 int mBlockCounter = 0;
543
544 QHash< const QgsSymbolLayer *, QString > mLineStyles; //symbol layer name types
545 QHash< const QgsSymbolLayer *, QString > mPointSymbolBlocks; //reference to point symbol blocks
546 QHash< const QgsSymbolLayer *, double > mPointSymbolBlockSizes; //reference to point symbol size used to create its block
547 QHash< const QgsSymbolLayer *, double > mPointSymbolBlockAngles; //reference to point symbol size used to create its block
548
549 //AC1009
550 void writeHeader( const QString &codepage );
551 void prepareRenderers();
552 void writeTables();
553 void writeBlocks();
554 void writeEntities();
555 void writeEntitiesSymbolLevels( DxfLayerJob *job );
556 void stopRenderers();
557 void writeEndFile();
558
559 void startSection();
560 void endSection();
561
562 void writePoint( const QgsPoint &pt, const QString &layer, const QColor &color, QgsSymbolRenderContext &ctx, const QgsSymbolLayer *symbolLayer, const QgsSymbol *symbol, double angle );
563 void writeDefaultLinetypes();
564 void writeSymbolLayerLinetype( const QgsSymbolLayer *symbolLayer );
565 void writeLinetype( const QString &styleName, const QVector<qreal> &pattern, Qgis::RenderUnit u );
566
570 void writeText( const QString &layer, const QString &text, pal::LabelPosition *label, const QgsPalLayerSettings &layerSettings, const QgsExpressionContext &expressionContext );
571
581 void addGeometryGeneratorSymbolLayer( QgsSymbolRenderContext &context, const QgsCoordinateTransform &ct, const QString &layer, QgsSymbolLayer *symbolLayer, bool allSymbolLayers );
582
592 void addFeature( QgsSymbolRenderContext &context, const QgsCoordinateTransform &ct, const QString &layer, const QgsSymbolLayer *symbolLayer, const QgsSymbol *symbol );
593
594 //returns dxf palette index from symbol layer color
595 static QColor colorFromSymbolLayer( const QgsSymbolLayer *symbolLayer, QgsSymbolRenderContext &ctx );
596 QString lineStyleFromSymbolLayer( const QgsSymbolLayer *symbolLayer );
597
598 //functions for dxf palette
599 static int color_distance( QRgb p1, int index );
600 static QRgb createRgbEntry( qreal r, qreal g, qreal b );
601
602 //helper functions for symbology export
603 QgsRenderContext renderContext() const;
604
605 QList< QPair< QgsSymbolLayer *, QgsSymbol * > > symbolLayers( QgsRenderContext &context );
606 static int nLineTypes( const QList< QPair< QgsSymbolLayer *, QgsSymbol *> > &symbolLayers );
607 static bool hasBlockBreakingDataDefinedProperties( const QgsSymbolLayer *sl, const QgsSymbol *symbol );
608
609 double dashSize() const;
610 double dotSize() const;
611 double dashSeparatorSize() const;
612 double sizeToMapUnits( double s ) const;
613 static QString lineNameFromPenStyle( Qt::PenStyle style );
614 bool layerIsScaleBasedVisible( const QgsMapLayer *layer ) const;
615
616 QHash<QString, int> mBlockHandles;
617 QString mBlockHandle;
618
620 QMap< QString, QMap<QgsFeatureId, QString> > mDxfLayerNames;
622 QgsMapSettings mMapSettings;
623 QList<QgsMapLayer *> mLayerList;
624 QHash<QString, int> mLayerNameAttribute;
625 double mFactor = 1.0;
626 bool mForce2d = false;
627
628 QgsDxfExport::Flags mFlags = QgsDxfExport::Flags();
629
630 void appendCurve( const QgsCurve &c, QVector<QgsPoint> &points, QVector<double> &bulges );
631 void appendLineString( const QgsLineString &ls, QVector<QgsPoint> &points, QVector<double> &bulges );
632 void appendCircularString( const QgsCircularString &cs, QVector<QgsPoint> &points, QVector<double> &bulges );
633 void appendCompoundCurve( const QgsCompoundCurve &cc, QVector<QgsPoint> &points, QVector<double> &bulges );
634
635 QgsRenderContext mRenderContext;
636 // Internal cache for layer related information required during rendering
637 QList<DxfLayerJob *> mJobs;
638 std::unique_ptr<QgsLabelingEngine> mLabelingEngine;
639
640 QString mFeedbackMessage;
641};
642
643Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDxfExport::Flags )
644Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDxfExport::DxfPolylineFlags )
645
646#endif // QGSDXFEXPORT_H
The Qgis class provides global constants for use throughout the application.
Definition qgis.h:54
DistanceUnit
Units of distance.
Definition qgis.h:4062
RenderUnit
Rendering size units.
Definition qgis.h:4193
FeatureSymbologyExport
Options for exporting features considering their symbology.
Definition qgis.h:4415
@ NoSymbology
Export only data.
Circular string geometry type.
Compound curve geometry type.
This class represents a coordinate reference system (CRS).
Class for doing transforms between two map coordinate systems.
Curve polygon geometry type.
Abstract base class for curved geometry type.
Definition qgscurve.h:35
Exports QGIS layers to the DXF format.
void setForce2d(bool force2d)
Force 2d output (eg.
ExportResult
The result of an export as dxf operation.
Qgis::FeatureSymbologyExport symbologyExport() const
Gets symbology export mode.
bool layerTitleAsName()
Retrieve whether layer title (where set) instead of name shall be use.
QgsRectangle extent() const
Gets extent of area to export.
Flag
Export flags.
HAlign
Horizontal alignments.
void setSymbologyScale(double scale)
Set reference scale for output.
bool force2d()
Retrieve whether the output should be forced to 2d.
void setExtent(const QgsRectangle &r)
Set extent of area to export.
double symbologyScale() const
Returns the reference scale for output.
VAlign
Vertical alignments.
void setSymbologyExport(Qgis::FeatureSymbologyExport e)
Set symbology export mode.
DxfPolylineFlag
Flags for polylines.
void setLayerTitleAsName(bool layerTitleAsName)
Enable use of title (where set) instead of layer name, when attribute index of corresponding layer in...
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:56
Abstract base class that can be used to intercept rendered labels from a labeling / rendering job.
Line string geometry type, with support for z-dimension and m-values.
Base class for all map layer types.
Definition qgsmaplayer.h:75
The QgsMapSettings class contains configuration for rendering of the map.
Struct for storing maximum and minimum scales for measurements in map units.
Contains settings for how a map layer will be labeled.
A class to represent a 2D point.
Definition qgspointxy.h:60
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
A rectangle specified with double values.
Contains information about the context of a rendering operation.
Abstract base class for all rendered symbols.
Definition qgssymbol.h:94
Represents a vector layer which manages a vector based data sets.
LabelPosition is a candidate feature label position.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:278
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_PYNAME(name)
Definition qgis_sip.h:81
QVector< QgsPointSequence > QgsRingSequence
QVector< QgsPoint > QgsPointSequence
#define DXF_HANDSEED
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsTextRendererUtils::CurvedTextFlags)
const QgsCoordinateReferenceSystem & crs
Holds information about each layer in a DXF job.
Layers and optional attribute index to split into multiple layers using attribute value as layer name...
QgsVectorLayer * layer() const
Returns the layer.
DxfLayer(QgsVectorLayer *vl, int layerOutputAttributeIndex=-1)
int layerOutputAttributeIndex() const
Returns the attribute index used to split into multiple layers.