QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 "qgssymbol.h" // for OutputUnit enum
26 #include "qgsmapsettings.h"
27 #include "qgslabelsink.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;
43 
44 #define DXF_HANDSEED 100
45 #define DXF_HANDMAX 9999999
46 #define DXF_HANDPLOTSTYLE 0xf
47 
48 namespace pal // SIP_SKIP
49 {
50  class LabelPosition;
51 }
52 
53 
59 #ifdef SIP_RUN
60 class CORE_EXPORT QgsDxfExport
61 {
62 #else
63 class CORE_EXPORT QgsDxfExport : public QgsLabelSink
64 {
65 #endif
66  public:
67 
72  struct CORE_EXPORT DxfLayer
73  {
74  DxfLayer( QgsVectorLayer *vl, int layerOutputAttributeIndex = -1 )
75  : mLayer( vl )
76  , mLayerOutputAttributeIndex( layerOutputAttributeIndex )
77  {}
78 
80  QgsVectorLayer *layer() const {return mLayer;}
81 
87  int layerOutputAttributeIndex() const {return mLayerOutputAttributeIndex;}
88 
95  QString splitLayerAttribute() const;
96 
97  private:
98  QgsVectorLayer *mLayer = nullptr;
99  int mLayerOutputAttributeIndex = -1;
100  };
101 
103  {
104  NoSymbology = 0,
106  SymbolLayerSymbology
107  };
108 
110  enum Flag
111  {
112  FlagNoMText = 1 << 1,
113  };
114  Q_DECLARE_FLAGS( Flags, Flag )
115 
116 
121  enum class ExportResult
122  {
123  Success = 0,
124  InvalidDeviceError,
125  DeviceNotWritableError,
126  EmptyExtentError
127  };
128 
132  enum class VAlign : int
133  {
134  VBaseLine = 0,
135  VBottom = 1,
136  VMiddle = 2,
137  VTop = 3,
138  Undefined = 9999
139  };
140 
142  enum class HAlign : int
143  {
144  HLeft = 0,
145  HCenter = 1,
146  HRight = 2,
147  HAligned = 3,
148  HMiddle = 4,
149  HFit = 5,
150  Undefined = 9999
151  };
152 
159  {
160  Closed = 1,
161  Curve = 2,
162  Spline = 4,
163  Is3DPolyline = 8,
164  Is3DPolygonMesh = 16,
165  PolygonMesh = 32,
166  PolyfaceMesh = 64,
167  ContinuousPattern = 128,
168  };
169 
170  Q_DECLARE_FLAGS( DxfPolylineFlags, DxfPolylineFlag )
171 
172 
176 
177  ~QgsDxfExport() override;
178 
183  void setMapSettings( const QgsMapSettings &settings );
184 
190  void setFlags( QgsDxfExport::Flags flags );
191 
197  QgsDxfExport::Flags flags() const;
198 
204  void addLayers( const QList< QgsDxfExport::DxfLayer > &layers );
205 
212  ExportResult writeToFile( QIODevice *d, const QString &codec ); //maybe add progress dialog? other parameters (e.g. scale, dpi)?
213 
220  void setSymbologyScale( double scale ) { mSymbologyScale = scale; }
221 
228  double symbologyScale() const { return mSymbologyScale; }
229 
234  QgsUnitTypes::DistanceUnit mapUnits() const;
235 
241  void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
242 
248  QgsCoordinateReferenceSystem destinationCrs() const;
249 
254  void setSymbologyExport( QgsDxfExport::SymbologyExport e ) { mSymbologyExport = e; }
255 
261  QgsDxfExport::SymbologyExport symbologyExport() const { return mSymbologyExport; }
262 
267  void setExtent( const QgsRectangle &r ) { mExtent = r; }
268 
274  QgsRectangle extent() const { return mExtent; }
275 
282  void setLayerTitleAsName( bool layerTitleAsName ) { mLayerTitleAsName = layerTitleAsName; }
283 
289  bool layerTitleAsName() { return mLayerTitleAsName; }
290 
296  void setForce2d( bool force2d ) { mForce2d = force2d; }
297 
303  bool force2d() { return mForce2d; }
304 
309  static int closestColorMatch( QRgb color );
310 
317  QString layerName( const QString &id, const QgsFeature &f ) const;
318 
325  QString layerName( QgsVectorLayer *vl ) const;
326 
333  void writeGroup( int code, int i ) SIP_PYNAME( writeGroupInt );
334 
341  void writeGroup( int code, double d ) SIP_PYNAME( writeGroupDouble );
342 
348  void writeGroup( int code, const QString &s );
349 
357  void writeGroup( int code, const QgsPoint &p ) SIP_PYNAME( writeGroupPointV2 );
358 
367  void writeGroup( const QColor &color, int exactMatch = 62, int rgbCode = 420, int transparencyCode = 440 );
368 
373  void writeGroupCode( int code );
374 
379  void writeInt( int i );
380 
385  void writeDouble( double d );
386 
391  void writeString( const QString &s );
392 
399  int writeHandle( int code = 5, int handle = 0 );
400 
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 
434  void writePolygon( const QgsRingSequence &polygon, const QString &layer, const QString &hatchPattern, const QColor &color ) SIP_SKIP;
435 
445  void writePolygon( const QgsCurvePolygon &polygon, const QString &layer, const QString &hatchPattern, const QColor &color ) SIP_SKIP;
446 
451  void writeLine( const QgsPoint &pt1, const QgsPoint &pt2, const QString &layer, const QString &lineStyleName, const QColor &color, double width = -1 );
452 
458  void writePoint( const QString &layer, const QColor &color, const QgsPoint &pt ) SIP_PYNAME( writePointV2 );
459 
465  void writeFilledCircle( const QString &layer, const QColor &color, const QgsPoint &pt, double radius ) SIP_PYNAME( writeFillCircleV2 );
466 
472  void writeCircle( const QString &layer, const QColor &color, const QgsPoint &pt, double radius, const QString &lineStyleName, double width ) SIP_PYNAME( writeCircleV2 );
473 
479  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 );
480 
486  void writeMText( const QString &layer, const QString &text, const QgsPoint &pt, double width, double angle, const QColor &color );
487 
495  static double mapUnitScaleFactor( double scale, QgsUnitTypes::RenderUnit symbolUnits, QgsUnitTypes::DistanceUnit mapUnits, double mapUnitsPerPixel = 1.0 );
496 
503  void clipValueToMapUnitScale( double &value, const QgsMapUnitScale &scale, double pixelToMMFactor ) const;
504 
506  static QString dxfLayerName( const QString &name );
507 
509  static QString dxfEncoding( const QString &name );
510 
512  static QStringList encodings();
513 
519  void drawLabel( const QString &layerId, QgsRenderContext &context, pal::LabelPosition *label, const QgsPalLayerSettings &settings ) SIP_SKIP override;
520 
529  Q_DECL_DEPRECATED void registerDxfLayer( const QString &layerId, QgsFeatureId fid, const QString &layer );
530 
531  private:
532 
533 #ifdef SIP_RUN
534  QgsDxfExport( const QgsDxfExport &other );
535  QgsDxfExport &operator=( const QgsDxfExport & );
536 #endif
538  QgsRectangle mExtent;
540  double mSymbologyScale = 1.0;
541  SymbologyExport mSymbologyExport = NoSymbology;
543  bool mLayerTitleAsName = false;
544 
545  QTextStream mTextStream;
546 
547  int mSymbolLayerCounter = 0; //internal counter
548  int mNextHandleId = DXF_HANDSEED;
549  int mBlockCounter = 0;
550 
551  QHash< const QgsSymbolLayer *, QString > mLineStyles; //symbol layer name types
552  QHash< const QgsSymbolLayer *, QString > mPointSymbolBlocks; //reference to point symbol blocks
553 
554  //AC1009
555  void writeHeader( const QString &codepage );
556  void prepareRenderers();
557  void writeTables();
558  void writeBlocks();
559  void writeEntities();
560  void writeEntitiesSymbolLevels( DxfLayerJob *job );
561  void stopRenderers();
562  void writeEndFile();
563 
564  void startSection();
565  void endSection();
566 
567  void writePoint( const QgsPoint &pt, const QString &layer, const QColor &color, QgsSymbolRenderContext &ctx, const QgsSymbolLayer *symbolLayer, const QgsSymbol *symbol, double angle );
568  void writeDefaultLinetypes();
569  void writeSymbolLayerLinetype( const QgsSymbolLayer *symbolLayer );
570  void writeLinetype( const QString &styleName, const QVector<qreal> &pattern, QgsUnitTypes::RenderUnit u );
571 
575  void writeText( const QString &layer, const QString &text, pal::LabelPosition *label, const QgsPalLayerSettings &layerSettings, const QgsExpressionContext &expressionContext );
576 
585  void addGeometryGeneratorSymbolLayer( QgsSymbolRenderContext &ctx, const QgsCoordinateTransform &ct, const QString &layer, QgsSymbolLayer *symbolLayer, bool allSymbolLayers );
586 
587  void addFeature( QgsSymbolRenderContext &ctx, const QgsCoordinateTransform &ct, const QString &layer, const QgsSymbolLayer *symbolLayer, const QgsSymbol *symbol );
588 
589  //returns dxf palette index from symbol layer color
590  static QColor colorFromSymbolLayer( const QgsSymbolLayer *symbolLayer, QgsSymbolRenderContext &ctx );
591  QString lineStyleFromSymbolLayer( const QgsSymbolLayer *symbolLayer );
592 
593  //functions for dxf palette
594  static int color_distance( QRgb p1, int index );
595  static QRgb createRgbEntry( qreal r, qreal g, qreal b );
596 
597  //helper functions for symbology export
598  QgsRenderContext renderContext() const;
599 
600  QList< QPair< QgsSymbolLayer *, QgsSymbol * > > symbolLayers( QgsRenderContext &context );
601  static int nLineTypes( const QList< QPair< QgsSymbolLayer *, QgsSymbol *> > &symbolLayers );
602  static bool hasDataDefinedProperties( const QgsSymbolLayer *sl, const QgsSymbol *symbol );
603  double dashSize() const;
604  double dotSize() const;
605  double dashSeparatorSize() const;
606  double sizeToMapUnits( double s ) const;
607  static QString lineNameFromPenStyle( Qt::PenStyle style );
608  bool layerIsScaleBasedVisible( const QgsMapLayer *layer ) const;
609 
610  QHash<QString, int> mBlockHandles;
611  QString mBlockHandle;
612 
614  QMap< QString, QMap<QgsFeatureId, QString> > mDxfLayerNames;
616  QgsMapSettings mMapSettings;
617  QHash<QString, int> mLayerNameAttribute;
618  double mFactor = 1.0;
619  bool mForce2d = false;
620 
621  QgsDxfExport::Flags mFlags = QgsDxfExport::Flags();
622 
623  void appendCurve( const QgsCurve &c, QVector<QgsPoint> &points, QVector<double> &bulges );
624  void appendLineString( const QgsLineString &ls, QVector<QgsPoint> &points, QVector<double> &bulges );
625  void appendCircularString( const QgsCircularString &cs, QVector<QgsPoint> &points, QVector<double> &bulges );
626  void appendCompoundCurve( const QgsCompoundCurve &cc, QVector<QgsPoint> &points, QVector<double> &bulges );
627 
628  QgsRenderContext mRenderContext;
629  // Internal cache for layer related information required during rendering
630  QList<DxfLayerJob *> mJobs;
631  std::unique_ptr<QgsLabelingEngine> mLabelingEngine;
632 };
633 
634 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDxfExport::Flags )
635 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDxfExport::DxfPolylineFlags )
636 
637 #endif // QGSDXFEXPORT_H
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:36
Exports QGIS layers to the DXF format.
Definition: qgsdxfexport.h:64
void setForce2d(bool force2d)
Force 2d output (eg.
Definition: qgsdxfexport.h:296
ExportResult
The result of an export as dxf operation.
Definition: qgsdxfexport.h:122
@ FeatureSymbology
Keeps the number of features and export symbology per feature (using the first symbol level)
Definition: qgsdxfexport.h:105
void setSymbologyExport(QgsDxfExport::SymbologyExport e)
Set symbology export mode.
Definition: qgsdxfexport.h:254
bool layerTitleAsName()
Retrieve whether layer title (where set) instead of name shall be use.
Definition: qgsdxfexport.h:289
QgsRectangle extent() const
Gets extent of area to export.
Definition: qgsdxfexport.h:274
Flag
Export flags.
Definition: qgsdxfexport.h:111
HAlign
Horizontal alignments.
Definition: qgsdxfexport.h:143
bool force2d()
Retrieve whether the output should be forced to 2d.
Definition: qgsdxfexport.h:303
void setExtent(const QgsRectangle &r)
Set extent of area to export.
Definition: qgsdxfexport.h:267
double symbologyScale() const
Returns the reference scale for output.
Definition: qgsdxfexport.h:228
VAlign
Vertical alignments.
Definition: qgsdxfexport.h:133
QgsDxfExport::SymbologyExport symbologyExport() const
Gets symbology export mode.
Definition: qgsdxfexport.h:261
DxfPolylineFlag
Flags for polylines.
Definition: qgsdxfexport.h:159
void setLayerTitleAsName(bool layerTitleAsName)
Enable use of title (where set) instead of layer name, when attribute index of corresponding layer in...
Definition: qgsdxfexport.h:282
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
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:44
Base class for all map layer types.
Definition: qgsmaplayer.h:85
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:44
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:38
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:65
Helper functions for various unit types.
Definition: qgsunittypes.h:39
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:68
@ DistanceMeters
Meters.
Definition: qgsunittypes.h:69
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:167
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:786
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_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:44
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsField::ConfigurationFlags) CORE_EXPORT QDataStream &operator<<(QDataStream &out
Writes the field to stream out. QGIS version compatibility is not guaranteed.
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:73
DxfLayer(QgsVectorLayer *vl, int layerOutputAttributeIndex=-1)
Definition: qgsdxfexport.h:74
QgsVectorLayer * layer() const
Returns the layer.
Definition: qgsdxfexport.h:80
int layerOutputAttributeIndex() const
Returns the attribute index used to split into multiple layers.
Definition: qgsdxfexport.h:87