QGIS API Documentation  3.37.0-Master (a5b4d9743e8)
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 
33 class QgsMapLayer;
34 class QgsPointXY;
35 class QgsSymbolLayer;
36 class QIODevice;
38 class QgsCurve;
39 class QgsCurvePolygon;
40 class QgsCircularString;
41 class QgsCompoundCurve;
42 struct DxfLayerJob;
45 
46 #define DXF_HANDSEED 100
47 #define DXF_HANDMAX 9999999
48 #define DXF_HANDPLOTSTYLE 0xf
49 
50 namespace pal // SIP_SKIP
51 {
52  class LabelPosition;
53 }
54 
55 
61 #ifdef SIP_RUN
62 class CORE_EXPORT QgsDxfExport
63 {
64 #else
65 class CORE_EXPORT QgsDxfExport : public QgsLabelSink
66 {
67 #endif
68  public:
69 
74  struct CORE_EXPORT DxfLayer
75  {
76  DxfLayer( QgsVectorLayer *vl, int layerOutputAttributeIndex = -1, bool buildDDBlocks = false, int ddBlocksMaxNumberOfClasses = -1 )
77  : mLayer( vl )
78  , mLayerOutputAttributeIndex( layerOutputAttributeIndex )
79  , mBuildDDBlocks( buildDDBlocks )
80  , mDDBlocksMaxNumberOfClasses( ddBlocksMaxNumberOfClasses )
81  {}
82 
84  QgsVectorLayer *layer() const {return mLayer;}
85 
91  int layerOutputAttributeIndex() const {return mLayerOutputAttributeIndex;}
92 
99  QString splitLayerAttribute() const;
100 
106  bool buildDataDefinedBlocks() const { return mBuildDDBlocks; }
107 
113  int dataDefinedBlocksMaximumNumberOfClasses() const { return mDDBlocksMaxNumberOfClasses; }
114 
115  private:
116  QgsVectorLayer *mLayer = nullptr;
117  int mLayerOutputAttributeIndex = -1;
118 
122  bool mBuildDDBlocks = false;
123 
127  int mDDBlocksMaxNumberOfClasses = -1;
128  };
129 
131  enum Flag SIP_ENUM_BASETYPE( IntFlag )
132  {
133  FlagNoMText = 1 << 1,
134  FlagOnlySelectedFeatures = 1 << 2,
135  };
136  Q_DECLARE_FLAGS( Flags, Flag )
137 
138 
143  enum class ExportResult
144  {
145  Success = 0,
146  InvalidDeviceError,
147  DeviceNotWritableError,
148  EmptyExtentError
149  };
150 
154  enum class VAlign : int
155  {
156  VBaseLine = 0,
157  VBottom = 1,
158  VMiddle = 2,
159  VTop = 3,
160  Undefined = 9999
161  };
162 
164  enum class HAlign : int
165  {
166  HLeft = 0,
167  HCenter = 1,
168  HRight = 2,
169  HAligned = 3,
170  HMiddle = 4,
171  HFit = 5,
172  Undefined = 9999
173  };
174 
181  {
182  Closed = 1,
183  Curve = 2,
184  Spline = 4,
185  Is3DPolyline = 8,
186  Is3DPolygonMesh = 16,
187  PolygonMesh = 32,
188  PolyfaceMesh = 64,
189  ContinuousPattern = 128,
190  };
191 
192  Q_DECLARE_FLAGS( DxfPolylineFlags, DxfPolylineFlag )
193 
194 
198 
199  ~QgsDxfExport() override;
200 
205  void setMapSettings( const QgsMapSettings &settings );
206 
211  void setFlags( QgsDxfExport::Flags flags );
212 
217  QgsDxfExport::Flags flags() const;
218 
224  void addLayers( const QList< QgsDxfExport::DxfLayer > &layers );
225 
232  ExportResult writeToFile( QIODevice *d, const QString &codec ); //maybe add progress dialog? other parameters (e.g. scale, dpi)?
233 
238  const QString feedbackMessage() const { return mFeedbackMessage; }
239 
245  void setSymbologyScale( double scale ) { mSymbologyScale = scale; }
246 
252  double symbologyScale() const { return mSymbologyScale; }
253 
258  Qgis::DistanceUnit mapUnits() const;
259 
264  void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
265 
270  QgsCoordinateReferenceSystem destinationCrs() const;
271 
276  void setSymbologyExport( Qgis::FeatureSymbologyExport e ) { mSymbologyExport = e; }
277 
283  Qgis::FeatureSymbologyExport symbologyExport() const { return mSymbologyExport; }
284 
289  void setExtent( const QgsRectangle &r ) { mExtent = r; }
290 
296  QgsRectangle extent() const { return mExtent; }
297 
304  void setLayerTitleAsName( bool layerTitleAsName ) { mLayerTitleAsName = layerTitleAsName; }
305 
311  bool layerTitleAsName() { return mLayerTitleAsName; }
312 
318  void setForce2d( bool force2d ) { mForce2d = force2d; }
319 
325  bool force2d() { return mForce2d; }
326 
331  static int closestColorMatch( QRgb color );
332 
339  QString layerName( const QString &id, const QgsFeature &f ) const;
340 
347  QString layerName( QgsVectorLayer *vl ) const;
348 
355  void writeGroup( int code, int i ) SIP_PYNAME( writeGroupInt );
356 
363  void writeGroup( int code, long long i ) SIP_PYNAME( writeGroupLong );
364 
371  void writeGroup( int code, double d ) SIP_PYNAME( writeGroupDouble );
372 
378  void writeGroup( int code, const QString &s );
379 
386  void writeGroup( int code, const QgsPoint &p ) SIP_PYNAME( writeGroupPointV2 );
387 
396  void writeGroup( const QColor &color, int exactMatch = 62, int rgbCode = 420, int transparencyCode = 440 );
397 
402  void writeGroupCode( int code );
403 
408  void writeInt( int i );
409 
414  void writeDouble( double d );
415 
420  void writeString( const QString &s );
421 
428  int writeHandle( int code = 5, int handle = 0 );
429 
439  void writePolyline( const QgsPointSequence &line, const QString &layer, const QString &lineStyleName, const QColor &color, double width = -1 ) SIP_SKIP;
440 
451  void writePolyline( const QgsCurve &curve, const QString &layer, const QString &lineStyleName, const QColor &color, double width = -1 ) SIP_SKIP;
452 
461  void writePolygon( const QgsRingSequence &polygon, const QString &layer, const QString &hatchPattern, const QColor &color ) SIP_SKIP;
462 
472  void writePolygon( const QgsCurvePolygon &polygon, const QString &layer, const QString &hatchPattern, const QColor &color ) SIP_SKIP;
473 
477  void writeLine( const QgsPoint &pt1, const QgsPoint &pt2, const QString &layer, const QString &lineStyleName, const QColor &color, double width = -1 );
478 
483  void writePoint( const QString &layer, const QColor &color, const QgsPoint &pt ) SIP_PYNAME( writePointV2 );
484 
489  void writeFilledCircle( const QString &layer, const QColor &color, const QgsPoint &pt, double radius ) SIP_PYNAME( writeFillCircleV2 );
490 
495  void writeCircle( const QString &layer, const QColor &color, const QgsPoint &pt, double radius, const QString &lineStyleName, double width ) SIP_PYNAME( writeCircleV2 );
496 
501  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 );
502 
507  void writeMText( const QString &layer, const QString &text, const QgsPoint &pt, double width, double angle, const QColor &color );
508 
516  static double mapUnitScaleFactor( double scale, Qgis::RenderUnit symbolUnits, Qgis::DistanceUnit mapUnits, double mapUnitsPerPixel = 1.0 );
517 
524  void clipValueToMapUnitScale( double &value, const QgsMapUnitScale &scale, double pixelToMMFactor ) const;
525 
527  static QString dxfLayerName( const QString &name );
528 
530  static QString dxfEncoding( const QString &name );
531 
533  static QStringList encodings();
534 
540  void drawLabel( const QString &layerId, QgsRenderContext &context, pal::LabelPosition *label, const QgsPalLayerSettings &settings ) SIP_SKIP override;
541 
550  Q_DECL_DEPRECATED void registerDxfLayer( const QString &layerId, QgsFeatureId fid, const QString &layer );
551 
552  private:
553 
554 #ifdef SIP_RUN
555  QgsDxfExport( const QgsDxfExport &other );
556  QgsDxfExport &operator=( const QgsDxfExport & );
557 #endif
558 
559  struct DataDefinedBlockInfo
560  {
561  QString blockName;
562  double angle;
563  double size;
564  QgsFeature feature; //a feature representing the attribute combination (without geometry)
565  };
566 
568  QgsRectangle mExtent;
570  double mSymbologyScale = 1.0;
573  bool mLayerTitleAsName = false;
574 
575  QTextStream mTextStream;
576 
577  int mSymbolLayerCounter = 0; //internal counter
578  int mNextHandleId = DXF_HANDSEED;
579  int mBlockCounter = 0;
580 
581  QHash< const QgsSymbolLayer *, QString > mLineStyles; //symbol layer name types
582  QHash< const QgsSymbolLayer *, QString > mPointSymbolBlocks; //reference to point symbol blocks
583  QHash< const QgsSymbolLayer *, double > mPointSymbolBlockSizes; //reference to point symbol size used to create its block
584  QHash< const QgsSymbolLayer *, double > mPointSymbolBlockAngles; //reference to point symbol size used to create its block
586  QHash< const QgsSymbolLayer *, QHash <uint, DataDefinedBlockInfo> > mDataDefinedBlockInfo; // symbolLayerName -> symbolHash/Feature
587 
588  //AC1009
589  void createDDBlockInfo();
590  void writeHeader( const QString &codepage );
591  void prepareRenderers();
592  void writeTables();
593  void writeBlocks();
594  void writeEntities();
595  void writeEntitiesSymbolLevels( DxfLayerJob *job );
596  void stopRenderers();
597  void writeEndFile();
598 
599  void startSection();
600  void endSection();
601 
602  void writePoint( const QgsPoint &pt, const QString &layer, const QColor &color, QgsSymbolRenderContext &ctx, const QgsSymbolLayer *symbolLayer, const QgsSymbol *symbol, double angle );
603  void writeDefaultLinetypes();
604  void writeSymbolLayerLinetype( const QgsSymbolLayer *symbolLayer );
605  void writeLinetype( const QString &styleName, const QVector<qreal> &pattern, Qgis::RenderUnit u );
606 
610  void writeText( const QString &layer, const QString &text, pal::LabelPosition *label, const QgsPalLayerSettings &layerSettings, const QgsExpressionContext &expressionContext );
611 
621  void addGeometryGeneratorSymbolLayer( QgsSymbolRenderContext &context, const QgsCoordinateTransform &ct, const QString &layer, QgsSymbolLayer *symbolLayer, bool allSymbolLayers );
622 
632  void addFeature( QgsSymbolRenderContext &context, const QgsCoordinateTransform &ct, const QString &layer, const QgsSymbolLayer *symbolLayer, const QgsSymbol *symbol );
633 
634  //returns dxf palette index from symbol layer color
635  static QColor colorFromSymbolLayer( const QgsSymbolLayer *symbolLayer, QgsSymbolRenderContext &ctx );
636  QString lineStyleFromSymbolLayer( const QgsSymbolLayer *symbolLayer );
637 
638  //functions for dxf palette
639  static int color_distance( QRgb p1, int index );
640  static QRgb createRgbEntry( qreal r, qreal g, qreal b );
641 
642  //helper functions for symbology export
643  QgsRenderContext renderContext() const;
644 
645  QList< QPair< QgsSymbolLayer *, QgsSymbol * > > symbolLayers( QgsRenderContext &context );
646  static int nLineTypes( const QList< QPair< QgsSymbolLayer *, QgsSymbol *> > &symbolLayers );
647  static bool hasBlockBreakingDataDefinedProperties( const QgsSymbolLayer *sl, const QgsSymbol *symbol );
648  void writeSymbolTableBlockRef( const QString &blockName );
649  void writeSymbolLayerBlock( const QString &blockName, const QgsMarkerSymbolLayer *ml, QgsSymbolRenderContext &ctx );
650  void writePointBlockReference( const QgsPoint &pt, const QgsSymbolLayer *symbolLayer, QgsSymbolRenderContext &ctx, const QString &layer, double angle, const QString &blockName, double blockAngle, double blockSize );
651  static uint dataDefinedSymbolClassHash( const QgsFeature &fet, const QgsPropertyCollection &prop );
652 
653  double dashSize() const;
654  double dotSize() const;
655  double dashSeparatorSize() const;
656  double sizeToMapUnits( double s ) const;
657  static QString lineNameFromPenStyle( Qt::PenStyle style );
658  bool layerIsScaleBasedVisible( const QgsMapLayer *layer ) const;
659 
660  QHash<QString, int> mBlockHandles;
661  QString mBlockHandle;
662 
664  QMap< QString, QMap<QgsFeatureId, QString> > mDxfLayerNames;
666  QgsMapSettings mMapSettings;
667  QList<QgsMapLayer *> mLayerList;
668  QHash<QString, int> mLayerNameAttribute;
669  QHash<QString, int> mLayerDDBlockMaxNumberOfClasses;
670  double mFactor = 1.0;
671  bool mForce2d = false;
672 
674 
675  void appendCurve( const QgsCurve &c, QVector<QgsPoint> &points, QVector<double> &bulges );
676  void appendLineString( const QgsLineString &ls, QVector<QgsPoint> &points, QVector<double> &bulges );
677  void appendCircularString( const QgsCircularString &cs, QVector<QgsPoint> &points, QVector<double> &bulges );
678  void appendCompoundCurve( const QgsCompoundCurve &cc, QVector<QgsPoint> &points, QVector<double> &bulges );
679 
680  QgsRenderContext mRenderContext;
681  // Internal cache for layer related information required during rendering
682  QList<DxfLayerJob *> mJobs;
683  std::unique_ptr<QgsLabelingEngine> mLabelingEngine;
684 
685  QString mFeedbackMessage;
686 };
687 
690 
691 #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:4090
RenderUnit
Rendering size units.
Definition: qgis.h:4221
FeatureSymbologyExport
Options for exporting features considering their symbology.
Definition: qgis.h:4443
@ 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.
Definition: qgsdxfexport.h:66
void setForce2d(bool force2d)
Force 2d output (eg.
Definition: qgsdxfexport.h:318
ExportResult
The result of an export as dxf operation.
Definition: qgsdxfexport.h:144
Qgis::FeatureSymbologyExport symbologyExport() const
Gets symbology export mode.
Definition: qgsdxfexport.h:283
bool layerTitleAsName()
Retrieve whether layer title (where set) instead of name shall be use.
Definition: qgsdxfexport.h:311
QgsRectangle extent() const
Gets extent of area to export.
Definition: qgsdxfexport.h:296
Flag
Export flags.
Definition: qgsdxfexport.h:132
HAlign
Horizontal alignments.
Definition: qgsdxfexport.h:165
QFlags< Flag > Flags
Definition: qgsdxfexport.h:136
void setSymbologyScale(double scale)
Set reference scale for output.
Definition: qgsdxfexport.h:245
bool force2d()
Retrieve whether the output should be forced to 2d.
Definition: qgsdxfexport.h:325
QFlags< DxfPolylineFlag > DxfPolylineFlags
Definition: qgsdxfexport.h:192
void setExtent(const QgsRectangle &r)
Set extent of area to export.
Definition: qgsdxfexport.h:289
double symbologyScale() const
Returns the reference scale for output.
Definition: qgsdxfexport.h:252
VAlign
Vertical alignments.
Definition: qgsdxfexport.h:155
void setSymbologyExport(Qgis::FeatureSymbologyExport e)
Set symbology export mode.
Definition: qgsdxfexport.h:276
DxfPolylineFlag
Flags for polylines.
Definition: qgsdxfexport.h:181
void setLayerTitleAsName(bool layerTitleAsName)
Enable use of title (where set) instead of layer name, when attribute index of corresponding layer in...
Definition: qgsdxfexport.h:304
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.
Definition: qgslabelsink.h:38
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:45
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.
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.
Definition: qgsrectangle.h:42
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.
Definition: labelposition.h:56
double ANALYSIS_EXPORT angle(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
Definition: MathUtils.cpp:716
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
Definition: qgsdxfexport.h:46
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28
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...
Definition: qgsdxfexport.h:75
bool buildDataDefinedBlocks() const
Flag if data defined point block symbols should be created.
Definition: qgsdxfexport.h:106
int dataDefinedBlocksMaximumNumberOfClasses() const
Returns the maximum number of data defined symbol classes for which blocks are created.
Definition: qgsdxfexport.h:113
QgsVectorLayer * layer() const
Returns the layer.
Definition: qgsdxfexport.h:84
DxfLayer(QgsVectorLayer *vl, int layerOutputAttributeIndex=-1, bool buildDDBlocks=false, int ddBlocksMaxNumberOfClasses=-1)
Definition: qgsdxfexport.h:76
int layerOutputAttributeIndex() const
Returns the attribute index used to split into multiple layers.
Definition: qgsdxfexport.h:91