QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
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;
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
77 struct CORE_EXPORT DxfLayer
78 {
79 DxfLayer( QgsVectorLayer *vl, int layerOutputAttributeIndex = -1, bool buildDDBlocks = DEFAULT_DXF_DATA_DEFINED_BLOCKS, int ddBlocksMaxNumberOfClasses = -1, QString overriddenName = QString() )
80 : mLayer( vl )
81 , mLayerOutputAttributeIndex( layerOutputAttributeIndex )
82 , mBuildDDBlocks( buildDDBlocks )
83 , mDDBlocksMaxNumberOfClasses( ddBlocksMaxNumberOfClasses )
84 , mOverriddenName( overriddenName )
85 {}
86
88 QgsVectorLayer *layer() const {return mLayer;}
89
95 int layerOutputAttributeIndex() const {return mLayerOutputAttributeIndex;}
96
103 QString splitLayerAttribute() const;
104
110 bool buildDataDefinedBlocks() const { return mBuildDDBlocks; }
111
117 int dataDefinedBlocksMaximumNumberOfClasses() const { return mDDBlocksMaxNumberOfClasses; }
118
123 QString overriddenName() const { return mOverriddenName; }
124
125 private:
126 QgsVectorLayer *mLayer = nullptr;
127 int mLayerOutputAttributeIndex = -1;
128
132 bool mBuildDDBlocks = DEFAULT_DXF_DATA_DEFINED_BLOCKS;
133
137 int mDDBlocksMaxNumberOfClasses = -1;
138
142 QString mOverriddenName;
143 };
144
146 enum Flag SIP_ENUM_BASETYPE( IntFlag )
147 {
148 FlagNoMText = 1 << 1,
149 FlagOnlySelectedFeatures = 1 << 2,
150 FlagHairlineWidthExport = 1 << 3
151 };
152 Q_DECLARE_FLAGS( Flags, Flag )
153
154
159 enum class ExportResult
160 {
161 Success = 0,
162 InvalidDeviceError,
163 DeviceNotWritableError,
164 EmptyExtentError
165 };
166
170 enum class VAlign : int
171 {
172 VBaseLine = 0,
173 VBottom = 1,
174 VMiddle = 2,
175 VTop = 3,
176 Undefined = 9999
177 };
178
180 enum class HAlign : int
181 {
182 HLeft = 0,
183 HCenter = 1,
184 HRight = 2,
185 HAligned = 3,
186 HMiddle = 4,
187 HFit = 5,
188 Undefined = 9999
189 };
190
197 {
198 Closed = 1,
199 Curve = 2,
200 Spline = 4,
201 Is3DPolyline = 8,
202 Is3DPolygonMesh = 16,
203 PolygonMesh = 32,
204 PolyfaceMesh = 64,
205 ContinuousPattern = 128,
206 };
207
209
210
214
215 ~QgsDxfExport() override;
216
221 void setMapSettings( const QgsMapSettings &settings );
222
227 void setFlags( QgsDxfExport::Flags flags );
228
233 QgsDxfExport::Flags flags() const;
234
240 void addLayers( const QList< QgsDxfExport::DxfLayer > &layers );
241
248 ExportResult writeToFile( QIODevice *d, const QString &codec ); //maybe add progress dialog? other parameters (e.g. scale, dpi)?
249
254 const QString feedbackMessage() const { return mFeedbackMessage; }
255
261 void setSymbologyScale( double scale ) { mSymbologyScale = scale; }
262
268 double symbologyScale() const { return mSymbologyScale; }
269
274 Qgis::DistanceUnit mapUnits() const;
275
280 void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
281
286 QgsCoordinateReferenceSystem destinationCrs() const;
287
292 void setSymbologyExport( Qgis::FeatureSymbologyExport e ) { mSymbologyExport = e; }
293
299 Qgis::FeatureSymbologyExport symbologyExport() const { return mSymbologyExport; }
300
305 void setExtent( const QgsRectangle &r ) { mExtent = r; }
306
312 QgsRectangle extent() const { return mExtent; }
313
320 void setLayerTitleAsName( bool layerTitleAsName ) { mLayerTitleAsName = layerTitleAsName; }
321
327 bool layerTitleAsName() { return mLayerTitleAsName; }
328
334 void setForce2d( bool force2d ) { mForce2d = force2d; }
335
341 bool force2d() { return mForce2d; }
342
347 static int closestColorMatch( QRgb color );
348
355 QString layerName( const QString &id, const QgsFeature &f ) const;
356
363 QString layerName( QgsVectorLayer *vl ) const;
364
371 void writeGroup( int code, int i ) SIP_PYNAME( writeGroupInt );
372
379 void writeGroup( int code, long long i ) SIP_PYNAME( writeGroupLong );
380
387 void writeGroup( int code, double d ) SIP_PYNAME( writeGroupDouble );
388
394 void writeGroup( int code, const QString &s );
395
402 void writeGroup( int code, const QgsPoint &p ) SIP_PYNAME( writeGroupPointV2 );
403
412 void writeGroup( const QColor &color, int exactMatch = 62, int rgbCode = 420, int transparencyCode = 440 );
413
418 void writeGroupCode( int code );
419
424 void writeInt( int i );
425
430 void writeDouble( double d );
431
436 void writeString( const QString &s );
437
444 int writeHandle( int code = 5, int handle = 0 );
445
455 void writePolyline( const QgsPointSequence &line, const QString &layer, const QString &lineStyleName, const QColor &color, double width = -1 ) SIP_SKIP;
456
467 void writePolyline( const QgsCurve &curve, const QString &layer, const QString &lineStyleName, const QColor &color, double width = -1 ) SIP_SKIP;
468
477 void writePolygon( const QgsRingSequence &polygon, const QString &layer, const QString &hatchPattern, const QColor &color ) SIP_SKIP;
478
488 void writePolygon( const QgsCurvePolygon &polygon, const QString &layer, const QString &hatchPattern, const QColor &color ) SIP_SKIP;
489
493 void writeLine( const QgsPoint &pt1, const QgsPoint &pt2, const QString &layer, const QString &lineStyleName, const QColor &color, double width = -1 );
494
499 void writePoint( const QString &layer, const QColor &color, const QgsPoint &pt ) SIP_PYNAME( writePointV2 );
500
505 void writeFilledCircle( const QString &layer, const QColor &color, const QgsPoint &pt, double radius ) SIP_PYNAME( writeFillCircleV2 );
506
511 void writeCircle( const QString &layer, const QColor &color, const QgsPoint &pt, double radius, const QString &lineStyleName, double width ) SIP_PYNAME( writeCircleV2 );
512
517 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 );
518
523 void writeMText( const QString &layer, const QString &text, const QgsPoint &pt, double width, double angle, const QColor &color );
524
532 static double mapUnitScaleFactor( double scale, Qgis::RenderUnit symbolUnits, Qgis::DistanceUnit mapUnits, double mapUnitsPerPixel = 1.0 );
533
540 void clipValueToMapUnitScale( double &value, const QgsMapUnitScale &scale, double pixelToMMFactor ) const;
541
543 static QString dxfLayerName( const QString &name );
544
546 static QString dxfEncoding( const QString &name );
547
549 static QStringList encodings();
550
556 void drawLabel( const QString &layerId, QgsRenderContext &context, pal::LabelPosition *label, const QgsPalLayerSettings &settings ) SIP_SKIP override;
557
566 Q_DECL_DEPRECATED void registerDxfLayer( const QString &layerId, QgsFeatureId fid, const QString &layer );
567
568 private:
569
570#ifdef SIP_RUN
571 QgsDxfExport( const QgsDxfExport &other );
572 QgsDxfExport &operator=( const QgsDxfExport & );
573#endif
574
575 struct DataDefinedBlockInfo
576 {
577 QString blockName;
578 double angle;
579 double size;
580 QgsFeature feature; //a feature representing the attribute combination (without geometry)
581 };
582
584 QgsRectangle mExtent;
586 double mSymbologyScale = 1.0;
589 bool mLayerTitleAsName = false;
590
591 QTextStream mTextStream;
592
593 int mSymbolLayerCounter = 0; //internal counter
594 int mNextHandleId = DXF_HANDSEED;
595 int mBlockCounter = 0;
596
597 QHash< const QgsSymbolLayer *, QString > mLineStyles; //symbol layer name types
598 QHash< const QgsSymbolLayer *, QString > mPointSymbolBlocks; //reference to point symbol blocks
599 QHash< const QgsSymbolLayer *, double > mPointSymbolBlockSizes; //reference to point symbol size used to create its block
600 QHash< const QgsSymbolLayer *, double > mPointSymbolBlockAngles; //reference to point symbol size used to create its block
602 QHash< const QgsSymbolLayer *, QHash <uint, DataDefinedBlockInfo> > mDataDefinedBlockInfo; // symbolLayerName -> symbolHash/Feature
603
604 //AC1009
605 void createDDBlockInfo();
606 void writeHeader( const QString &codepage );
607 void prepareRenderers();
608 void writeTables();
609 void writeBlocks();
610 void writeEntities();
611 void writeEntitiesSymbolLevels( DxfLayerJob *job );
612 void stopRenderers();
613 void writeEndFile();
614
615 void startSection();
616 void endSection();
617
618 void writePoint( const QgsPoint &pt, const QString &layer, const QColor &color, QgsSymbolRenderContext &ctx, const QgsSymbolLayer *symbolLayer, const QgsSymbol *symbol, double angle );
619 void writeDefaultLinetypes();
620 void writeSymbolLayerLinetype( const QgsSymbolLayer *symbolLayer );
621 void writeLinetype( const QString &styleName, const QVector<qreal> &pattern, Qgis::RenderUnit u );
622
626 void writeText( const QString &layer, const QString &text, pal::LabelPosition *label, const QgsPalLayerSettings &layerSettings, const QgsExpressionContext &expressionContext );
627
637 void addGeometryGeneratorSymbolLayer( QgsSymbolRenderContext &context, const QgsCoordinateTransform &ct, const QString &layer, QgsSymbolLayer *symbolLayer, bool allSymbolLayers );
638
648 void addFeature( QgsSymbolRenderContext &context, const QgsCoordinateTransform &ct, const QString &layer, const QgsSymbolLayer *symbolLayer, const QgsSymbol *symbol );
649
650 //returns dxf palette index from symbol layer color
651 static QColor colorFromSymbolLayer( const QgsSymbolLayer *symbolLayer, QgsSymbolRenderContext &ctx );
652 QString lineStyleFromSymbolLayer( const QgsSymbolLayer *symbolLayer );
653
654 //functions for dxf palette
655 static int color_distance( QRgb p1, int index );
656 static QRgb createRgbEntry( qreal r, qreal g, qreal b );
657
658 //helper functions for symbology export
659 QgsRenderContext renderContext() const;
660
661 QList< QPair< QgsSymbolLayer *, QgsSymbol * > > symbolLayers( QgsRenderContext &context );
662 static int nLineTypes( const QList< QPair< QgsSymbolLayer *, QgsSymbol *> > &symbolLayers );
663 static bool hasBlockBreakingDataDefinedProperties( const QgsSymbolLayer *sl, const QgsSymbol *symbol );
664 void writeSymbolTableBlockRef( const QString &blockName );
665 void writeSymbolLayerBlock( const QString &blockName, const QgsMarkerSymbolLayer *ml, QgsSymbolRenderContext &ctx );
666 void writePointBlockReference( const QgsPoint &pt, const QgsSymbolLayer *symbolLayer, QgsSymbolRenderContext &ctx, const QString &layer, double angle, const QString &blockName, double blockAngle, double blockSize );
667 static uint dataDefinedSymbolClassHash( const QgsFeature &fet, const QgsPropertyCollection &prop );
668
669 double dashSize() const;
670 double dotSize() const;
671 double dashSeparatorSize() const;
672 double sizeToMapUnits( double s ) const;
673 static QString lineNameFromPenStyle( Qt::PenStyle style );
674 bool layerIsScaleBasedVisible( const QgsMapLayer *layer ) const;
675
676 QHash<QString, int> mBlockHandles;
677 QString mBlockHandle;
678
680 QMap< QString, QMap<QgsFeatureId, QString> > mDxfLayerNames;
682 QgsMapSettings mMapSettings;
683 QList<QgsMapLayer *> mLayerList;
684 QHash<QString, int> mLayerNameAttribute;
685 QHash<QString, int> mLayerDDBlockMaxNumberOfClasses;
686 QHash<QString, QString> mLayerOverriddenName;
687 double mFactor = 1.0;
688 bool mForce2d = false;
689
691
692 void appendCurve( const QgsCurve &c, QVector<QgsPoint> &points, QVector<double> &bulges );
693 void appendLineString( const QgsLineString &ls, QVector<QgsPoint> &points, QVector<double> &bulges );
694 void appendCircularString( const QgsCircularString &cs, QVector<QgsPoint> &points, QVector<double> &bulges );
695 void appendCompoundCurve( const QgsCompoundCurve &cc, QVector<QgsPoint> &points, QVector<double> &bulges );
696
697 QgsRenderContext mRenderContext;
698 // Internal cache for layer related information required during rendering
699 QList<DxfLayerJob *> mJobs;
700 std::unique_ptr<QgsLabelingEngine> mLabelingEngine;
701
702 QString mFeedbackMessage;
703};
704
707
708#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:4669
RenderUnit
Rendering size units.
Definition qgis.h:4839
FeatureSymbologyExport
Options for exporting features considering their symbology.
Definition qgis.h:5147
@ 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.
QFlags< Flag > Flags
void setSymbologyScale(double scale)
Set reference scale for output.
bool force2d()
Retrieve whether the output should be forced to 2d.
QFlags< DxfPolylineFlag > DxfPolylineFlags
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:58
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:76
The QgsMapSettings class contains configuration for rendering of the map.
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.
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 grouped map of multiple QgsProperty objects, each referenced by a integer key value.
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:231
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...
QString overriddenName() const
Returns the overridden layer name to be used in the exported DXF.
bool buildDataDefinedBlocks() const
Flag if data defined point block symbols should be created.
QgsVectorLayer * layer() const
Returns the 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())
int layerOutputAttributeIndex() const
Returns the attribute index used to split into multiple layers.