QGIS API Documentation 3.99.0-Master (8e76e220402)
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.h"
22#include "qgis_core.h"
23#include "qgis_sip.h"
24#include "qgsgeometry.h"
25#include "qgslabelsink.h"
26#include "qgsmapsettings.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;
45
46#define DXF_HANDSEED 100
47#define DXF_HANDMAX 9999999
48#define DXF_HANDPLOTSTYLE 0xf
49
50namespace pal // SIP_SKIP
51{
52 class LabelPosition;
53}
54
55
61#ifdef SIP_RUN
62class CORE_EXPORT QgsDxfExport
63{
64#else
65
66static const bool DEFAULT_DXF_DATA_DEFINED_BLOCKS = true;
67
68class CORE_EXPORT QgsDxfExport : public QgsLabelSink
69{
70#endif
71 public:
72
76 struct CORE_EXPORT DxfLayer
77 {
87 DxfLayer( QgsVectorLayer *vl, int layerOutputAttributeIndex = -1, bool buildDDBlocks = DEFAULT_DXF_DATA_DEFINED_BLOCKS, int ddBlocksMaxNumberOfClasses = -1, QString overriddenName = QString() )
88 : mLayer( vl )
89 , mLayerOutputAttributeIndex( layerOutputAttributeIndex )
90 , mBuildDDBlocks( buildDDBlocks )
91 , mDDBlocksMaxNumberOfClasses( ddBlocksMaxNumberOfClasses )
92 , mOverriddenName( overriddenName )
93 {}
94
96 QgsVectorLayer *layer() const {return mLayer;}
97
105 int layerOutputAttributeIndex() const {return mLayerOutputAttributeIndex;}
106
115 QString splitLayerAttribute() const;
116
123 bool buildDataDefinedBlocks() const { return mBuildDDBlocks; }
124
133 int dataDefinedBlocksMaximumNumberOfClasses() const { return mDDBlocksMaxNumberOfClasses; }
134
142 QString overriddenName() const { return mOverriddenName; }
143
144 private:
145 QgsVectorLayer *mLayer = nullptr;
146 int mLayerOutputAttributeIndex = -1;
147
151 bool mBuildDDBlocks = DEFAULT_DXF_DATA_DEFINED_BLOCKS;
152
156 int mDDBlocksMaxNumberOfClasses = -1;
157
161 QString mOverriddenName;
162 };
163
165 enum Flag SIP_ENUM_BASETYPE( IntFlag )
166 {
167 FlagNoMText = 1 << 1,
170 };
171 Q_DECLARE_FLAGS( Flags, Flag )
172
173
178 enum class ExportResult
179 {
180 Success = 0,
181 InvalidDeviceError,
182 DeviceNotWritableError,
183 EmptyExtentError
184 };
185
189 enum class VAlign : int
190 {
191 VBaseLine = 0,
192 VBottom = 1,
193 VMiddle = 2,
194 VTop = 3,
195 Undefined = 9999
196 };
197
199 enum class HAlign : int
200 {
201 HLeft = 0,
202 HCenter = 1,
203 HRight = 2,
204 HAligned = 3,
205 HMiddle = 4,
206 HFit = 5,
207 Undefined = 9999
208 };
209
226
228
229
233
234 ~QgsDxfExport() override;
235
240 void setMapSettings( const QgsMapSettings &settings );
241
247
252 QgsDxfExport::Flags flags() const;
253
259 void addLayers( const QList< QgsDxfExport::DxfLayer > &layers );
260
267 ExportResult writeToFile( QIODevice *d, const QString &codec ); //maybe add progress dialog? other parameters (e.g. scale, dpi)?
268
273 const QString feedbackMessage() const { return mFeedbackMessage; }
274
280 void setSymbologyScale( double scale ) { mSymbologyScale = scale; }
281
287 double symbologyScale() const { return mSymbologyScale; }
288
293 Qgis::DistanceUnit mapUnits() const;
294
299 void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
300
305 QgsCoordinateReferenceSystem destinationCrs() const;
306
311 void setSymbologyExport( Qgis::FeatureSymbologyExport e ) { mSymbologyExport = e; }
312
318 Qgis::FeatureSymbologyExport symbologyExport() const { return mSymbologyExport; }
319
324 void setExtent( const QgsRectangle &r ) { mExtent = r; }
325
331 QgsRectangle extent() const { return mExtent; }
332
339 void setLayerTitleAsName( bool layerTitleAsName ) { mLayerTitleAsName = layerTitleAsName; }
340
346 bool layerTitleAsName() { return mLayerTitleAsName; }
347
353 void setForce2d( bool force2d ) { mForce2d = force2d; }
354
360 bool force2d() { return mForce2d; }
361
366 static int closestColorMatch( QRgb color );
367
374 QString layerName( const QString &id, const QgsFeature &f ) const;
375
382 QString layerName( QgsVectorLayer *vl ) const;
383
390 void writeGroup( int code, int i ) SIP_PYNAME( writeGroupInt );
391
398 void writeGroup( int code, long long i ) SIP_PYNAME( writeGroupLong );
399
406 void writeGroup( int code, double d ) SIP_PYNAME( writeGroupDouble );
407
413 void writeGroup( int code, const QString &s );
414
421 void writeGroup( int code, const QgsPoint &p ) SIP_PYNAME( writeGroupPointV2 );
422
431 void writeGroup( const QColor &color, int exactMatch = 62, int rgbCode = 420, int transparencyCode = 440 );
432
437 void writeGroupCode( int code );
438
443 void writeInt( int i );
444
449 void writeDouble( double d );
450
455 void writeString( const QString &s );
456
463 int writeHandle( int code = 5, int handle = 0 );
464
474 void writePolyline( const QgsPointSequence &line, const QString &layer, const QString &lineStyleName, const QColor &color, double width = -1 ) SIP_SKIP;
475
486 void writePolyline( const QgsCurve &curve, const QString &layer, const QString &lineStyleName, const QColor &color, double width = -1 ) SIP_SKIP;
487
496 void writePolygon( const QgsRingSequence &polygon, const QString &layer, const QString &hatchPattern, const QColor &color ) SIP_SKIP;
497
507 void writePolygon( const QgsCurvePolygon &polygon, const QString &layer, const QString &hatchPattern, const QColor &color ) SIP_SKIP;
508
512 void writeLine( const QgsPoint &pt1, const QgsPoint &pt2, const QString &layer, const QString &lineStyleName, const QColor &color, double width = -1 );
513
518 void writePoint( const QString &layer, const QColor &color, const QgsPoint &pt ) SIP_PYNAME( writePointV2 );
519
524 void writeFilledCircle( const QString &layer, const QColor &color, const QgsPoint &pt, double radius ) SIP_PYNAME( writeFillCircleV2 );
525
530 void writeCircle( const QString &layer, const QColor &color, const QgsPoint &pt, double radius, const QString &lineStyleName, double width ) SIP_PYNAME( writeCircleV2 );
531
536 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 );
537
542 void writeMText( const QString &layer, const QString &text, const QgsPoint &pt, double width, double angle, const QColor &color );
543
551 static double mapUnitScaleFactor( double scale, Qgis::RenderUnit symbolUnits, Qgis::DistanceUnit mapUnits, double mapUnitsPerPixel = 1.0 );
552
559 void clipValueToMapUnitScale( double &value, const QgsMapUnitScale &scale, double pixelToMMFactor ) const;
560
562 static QString dxfLayerName( const QString &name );
563
565 static QString dxfEncoding( const QString &name );
566
568 static QStringList encodings();
569
575 void drawLabel( const QString &layerId, QgsRenderContext &context, pal::LabelPosition *label, const QgsPalLayerSettings &settings ) SIP_SKIP override;
576
585 Q_DECL_DEPRECATED void registerDxfLayer( const QString &layerId, QgsFeatureId fid, const QString &layer );
586
587 private:
588
589#ifdef SIP_RUN
590 QgsDxfExport( const QgsDxfExport &other );
591 QgsDxfExport &operator=( const QgsDxfExport & );
592#endif
593
594 struct DataDefinedBlockInfo
595 {
596 QString blockName;
597 double angle;
598 double size;
599 QgsFeature feature; //a feature representing the attribute combination (without geometry)
600 };
601
603 QgsRectangle mExtent;
605 double mSymbologyScale = 1.0;
608 bool mLayerTitleAsName = false;
609
610 QTextStream mTextStream;
611
612 int mSymbolLayerCounter = 0; //internal counter
613 int mNextHandleId = DXF_HANDSEED;
614 int mBlockCounter = 0;
615
616 QHash< const QgsSymbolLayer *, QString > mLineStyles; //symbol layer name types
617 QHash< const QgsSymbolLayer *, QString > mPointSymbolBlocks; //reference to point symbol blocks
618 QHash< const QgsSymbolLayer *, double > mPointSymbolBlockSizes; //reference to point symbol size used to create its block
619 QHash< const QgsSymbolLayer *, double > mPointSymbolBlockAngles; //reference to point symbol size used to create its block
621 QHash< const QgsSymbolLayer *, QHash <uint, DataDefinedBlockInfo> > mDataDefinedBlockInfo; // symbolLayerName -> symbolHash/Feature
622
623 //AC1009
624 void createDDBlockInfo();
625 void writeHeader( const QString &codepage );
626 void prepareRenderers();
627 void writeTables();
628 void writeBlocks();
629 void writeEntities();
630 void writeEntitiesSymbolLevels( DxfLayerJob *job );
631 void stopRenderers();
632 void writeEndFile();
633
634 void startSection();
635 void endSection();
636
637 void writePoint( const QgsPoint &pt, const QString &layer, const QColor &color, QgsSymbolRenderContext &ctx, const QgsSymbolLayer *symbolLayer, const QgsSymbol *symbol, double angle );
638 void writeDefaultLinetypes();
639 void writeSymbolLayerLinetype( const QgsSymbolLayer *symbolLayer );
640 void writeLinetype( const QString &styleName, const QVector<qreal> &pattern, Qgis::RenderUnit u );
641
645 void writeText( const QString &layer, const QString &text, pal::LabelPosition *label, const QgsPalLayerSettings &layerSettings, const QgsExpressionContext &expressionContext );
646
656 void addGeometryGeneratorSymbolLayer( QgsSymbolRenderContext &context, const QgsCoordinateTransform &ct, const QString &layer, QgsSymbolLayer *symbolLayer, bool allSymbolLayers );
657
667 void addFeature( QgsSymbolRenderContext &context, const QgsCoordinateTransform &ct, const QString &layer, const QgsSymbolLayer *symbolLayer, const QgsSymbol *symbol );
668
669 //returns dxf palette index from symbol layer color
670 static QColor colorFromSymbolLayer( const QgsSymbolLayer *symbolLayer, QgsSymbolRenderContext &ctx );
671 QString lineStyleFromSymbolLayer( const QgsSymbolLayer *symbolLayer );
672
673 //functions for dxf palette
674 static int color_distance( QRgb p1, int index );
675 static QRgb createRgbEntry( qreal r, qreal g, qreal b );
676
677 //helper functions for symbology export
678 QgsRenderContext renderContext() const;
679
680 QList< QPair< QgsSymbolLayer *, QgsSymbol * > > symbolLayers( QgsRenderContext &context );
681 static int nLineTypes( const QList< QPair< QgsSymbolLayer *, QgsSymbol *> > &symbolLayers );
682 static bool hasBlockBreakingDataDefinedProperties( const QgsSymbolLayer *sl, const QgsSymbol *symbol );
683 void writeSymbolTableBlockRef( const QString &blockName );
684 void writeSymbolLayerBlock( const QString &blockName, const QgsMarkerSymbolLayer *ml, QgsSymbolRenderContext &ctx );
685 void writePointBlockReference( const QgsPoint &pt, const QgsSymbolLayer *symbolLayer, QgsSymbolRenderContext &ctx, const QString &layer, double angle, const QString &blockName, double blockAngle, double blockSize );
686 static uint dataDefinedSymbolClassHash( const QgsFeature &fet, const QgsPropertyCollection &prop );
687
688 double dashSize() const;
689 double dotSize() const;
690 double dashSeparatorSize() const;
691 double sizeToMapUnits( double s ) const;
692 static QString lineNameFromPenStyle( Qt::PenStyle style );
693 bool layerIsScaleBasedVisible( const QgsMapLayer *layer ) const;
694
695 QHash<QString, int> mBlockHandles;
696 QString mBlockHandle;
697
699 QMap< QString, QMap<QgsFeatureId, QString> > mDxfLayerNames;
700 QgsCoordinateReferenceSystem mCrs;
701 QgsMapSettings mMapSettings;
702 QList<QgsMapLayer *> mLayerList;
703 QHash<QString, int> mLayerNameAttribute;
704 QHash<QString, int> mLayerDDBlockMaxNumberOfClasses;
705 QHash<QString, QString> mLayerOverriddenName;
706 double mFactor = 1.0;
707 bool mForce2d = false;
708
710
711 void appendCurve( const QgsCurve &c, QVector<QgsPoint> &points, QVector<double> &bulges );
712 void appendLineString( const QgsLineString &ls, QVector<QgsPoint> &points, QVector<double> &bulges );
713 void appendCircularString( const QgsCircularString &cs, QVector<QgsPoint> &points, QVector<double> &bulges );
714 void appendCompoundCurve( const QgsCompoundCurve &cc, QVector<QgsPoint> &points, QVector<double> &bulges );
715
716 QgsRenderContext mRenderContext;
717 // Internal cache for layer related information required during rendering
718 QList<DxfLayerJob *> mJobs;
719 std::unique_ptr<QgsLabelingEngine> mLabelingEngine;
720
721 QString mFeedbackMessage;
722};
723
726
727#endif // QGSDXFEXPORT_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:59
DistanceUnit
Units of distance.
Definition qgis.h:5120
@ Meters
Meters.
Definition qgis.h:5121
RenderUnit
Rendering size units.
Definition qgis.h:5290
FeatureSymbologyExport
Options for exporting features considering their symbology.
Definition qgis.h:5858
@ NoSymbology
Export only data.
Definition qgis.h:5859
Circular string geometry type.
Compound curve geometry type.
Represents a coordinate reference system (CRS).
Curve polygon geometry type.
Abstract base class for curved geometry type.
Definition qgscurve.h:36
Exports QGIS layers to the DXF format.
void setForce2d(bool force2d)
Force 2d output (eg.
ExportResult
The result of an export as dxf operation.
ExportResult writeToFile(QIODevice *d, const QString &codec)
Export to a dxf file in the given encoding.
Qgis::FeatureSymbologyExport symbologyExport() const
Gets symbology export mode.
bool layerTitleAsName()
Retrieve whether layer title (where set) instead of name shall be use.
void setFlags(QgsDxfExport::Flags flags)
Sets the export flags.
QgsRectangle extent() const
Gets extent of area to export.
Flag
Export flags.
@ FlagOnlySelectedFeatures
Use only selected features for the export.
@ FlagNoMText
Export text as TEXT elements. If not set, text will be exported as MTEXT elements.
QgsDxfExport()
Constructor for QgsDxfExport.
HAlign
Horizontal alignments.
void addLayers(const QList< QgsDxfExport::DxfLayer > &layers)
Add layers to export.
QFlags< Flag > Flags
void setSymbologyScale(double scale)
Set reference scale for output.
bool force2d()
Retrieve whether the output should be forced to 2d.
const QString feedbackMessage() const
Returns any feedback message produced while export to dxf file.
QFlags< DxfPolylineFlag > DxfPolylineFlags
void setExtent(const QgsRectangle &r)
Set extent of area to export.
QgsDxfExport::Flags flags() const
Returns the export flags.
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.
@ PolygonMesh
The polygon mesh is closed in the N direction.
@ Is3DPolyline
< Spline-fit vertices have been added
@ ContinuousPattern
The linetype pattern is generated continuously around the vertices of this polyline.
@ Closed
This is a closed polyline (or a polygon mesh closed in the M direction).
@ Is3DPolygonMesh
This is a 3D polygon mesh.
@ Curve
Curve-fit vertices have been added.
@ PolyfaceMesh
The polyline is a polyface mesh.
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...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
Abstract base class that can be used to intercept rendered labels from a labeling / rendering job.
Base class for all map layer types.
Definition qgsmaplayer.h:83
Contains configuration for rendering maps.
Struct for storing maximum and minimum scales for measurements in map units.
Abstract base class for marker symbol layers.
Contains settings for how a map layer will be labeled.
Represents a 2D point.
Definition qgspointxy.h:62
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:53
A rectangle specified with double values.
Contains information about the context of a rendering operation.
Abstract base class for symbol layers.
Encapsulates the context in which a symbol is being rendered.
Represents a vector layer which manages a vector based dataset.
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:275
#define SIP_SKIP
Definition qgis_sip.h:134
#define SIP_PYNAME(name)
Definition qgis_sip.h:89
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(QgsProjectionSelectionWidget::CrsOptions)
Holds information about each layer in a DXF job.
Encapsulates the properties of a vector layer containing features that will be exported to the DXF fi...
QString overriddenName() const
Returns the overridden layer name to be used in the exported DXF.
bool buildDataDefinedBlocks() const
Returns true if data defined point block symbols should be created.
QgsVectorLayer * layer() const
Returns the source vector layer.
int dataDefinedBlocksMaximumNumberOfClasses() const
Returns the maximum number of data defined symbol classes for which blocks are created.
DxfLayer(QgsVectorLayer *vl, int layerOutputAttributeIndex=-1, bool buildDDBlocks=DEFAULT_DXF_DATA_DEFINED_BLOCKS, int ddBlocksMaxNumberOfClasses=-1, QString overriddenName=QString())
Constructor for DxfLayer.
int layerOutputAttributeIndex() const
Returns the attribute index used to split the source layer's features into multiple exported DXF laye...