QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
58 #ifdef SIP_RUN
59 class CORE_EXPORT QgsDxfExport
60 {
61 #else
62 class CORE_EXPORT QgsDxfExport : public QgsLabelSink
63 {
64 #endif
65  public:
66 
71  struct CORE_EXPORT DxfLayer
72  {
73  DxfLayer( QgsVectorLayer *vl, int layerOutputAttributeIndex = -1 )
74  : mLayer( vl )
75  , mLayerOutputAttributeIndex( layerOutputAttributeIndex )
76  {}
77 
79  QgsVectorLayer *layer() const {return mLayer;}
80 
86  int layerOutputAttributeIndex() const {return mLayerOutputAttributeIndex;}
87 
94  QString splitLayerAttribute() const;
95 
96  private:
97  QgsVectorLayer *mLayer = nullptr;
98  int mLayerOutputAttributeIndex = -1;
99  };
100 
102  {
103  NoSymbology = 0,
105  SymbolLayerSymbology
106  };
107 
109  enum Flag
110  {
111  FlagNoMText = 1 << 1,
112  };
113  Q_DECLARE_FLAGS( Flags, Flag )
114 
115 
120  enum class ExportResult
121  {
122  Success = 0,
123  InvalidDeviceError,
124  DeviceNotWritableError,
125  EmptyExtentError
126  };
127 
131  enum class VAlign : int
132  {
133  VBaseLine = 0,
134  VBottom = 1,
135  VMiddle = 2,
136  VTop = 3,
137  Undefined = 9999
138  };
139 
141  enum class HAlign : int
142  {
143  HLeft = 0,
144  HCenter = 1,
145  HRight = 2,
146  HAligned = 3,
147  HMiddle = 4,
148  HFit = 5,
149  Undefined = 9999
150  };
151 
158  {
159  Closed = 1,
160  Curve = 2,
161  Spline = 4,
162  Is3DPolyline = 8,
163  Is3DPolygonMesh = 16,
164  PolygonMesh = 32,
165  PolyfaceMesh = 64,
166  ContinuousPattern = 128,
167  };
168 
169  Q_DECLARE_FLAGS( DxfPolylineFlags, DxfPolylineFlag )
170 
171 
175 
176  ~QgsDxfExport() override;
177 
182  void setMapSettings( const QgsMapSettings &settings );
183 
189  void setFlags( QgsDxfExport::Flags flags );
190 
196  QgsDxfExport::Flags flags() const;
197 
203  void addLayers( const QList< QgsDxfExport::DxfLayer > &layers );
204 
211  ExportResult writeToFile( QIODevice *d, const QString &codec ); //maybe add progress dialog? other parameters (e.g. scale, dpi)?
212 
219  void setSymbologyScale( double scale ) { mSymbologyScale = scale; }
220 
227  double symbologyScale() const { return mSymbologyScale; }
228 
233  QgsUnitTypes::DistanceUnit mapUnits() const;
234 
240  void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
241 
247  QgsCoordinateReferenceSystem destinationCrs() const;
248 
253  void setSymbologyExport( QgsDxfExport::SymbologyExport e ) { mSymbologyExport = e; }
254 
260  QgsDxfExport::SymbologyExport symbologyExport() const { return mSymbologyExport; }
261 
266  void setExtent( const QgsRectangle &r ) { mExtent = r; }
267 
273  QgsRectangle extent() const { return mExtent; }
274 
281  void setLayerTitleAsName( bool layerTitleAsName ) { mLayerTitleAsName = layerTitleAsName; }
282 
288  bool layerTitleAsName() { return mLayerTitleAsName; }
289 
295  void setForce2d( bool force2d ) { mForce2d = force2d; }
296 
302  bool force2d() { return mForce2d; }
303 
308  static int closestColorMatch( QRgb color );
309 
316  QString layerName( const QString &id, const QgsFeature &f ) const;
317 
324  QString layerName( QgsVectorLayer *vl ) const;
325 
332  void writeGroup( int code, int i ) SIP_PYNAME( writeGroupInt );
333 
340  void writeGroup( int code, double d ) SIP_PYNAME( writeGroupDouble );
341 
347  void writeGroup( int code, const QString &s );
348 
356  void writeGroup( int code, const QgsPoint &p ) SIP_PYNAME( writeGroupPointV2 );
357 
366  void writeGroup( const QColor &color, int exactMatch = 62, int rgbCode = 420, int transparencyCode = 440 );
367 
372  void writeGroupCode( int code );
373 
378  void writeInt( int i );
379 
384  void writeDouble( double d );
385 
390  void writeString( const QString &s );
391 
398  int writeHandle( int code = 5, int handle = 0 );
399 
410  void writePolyline( const QgsPointSequence &line, const QString &layer, const QString &lineStyleName, const QColor &color, double width = -1 ) SIP_SKIP;
411 
422  void writePolyline( const QgsCurve &curve, const QString &layer, const QString &lineStyleName, const QColor &color, double width = -1 ) SIP_SKIP;
423 
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 
450  void writeLine( const QgsPoint &pt1, const QgsPoint &pt2, const QString &layer, const QString &lineStyleName, const QColor &color, double width = -1 );
451 
457  void writePoint( const QString &layer, const QColor &color, const QgsPoint &pt ) SIP_PYNAME( writePointV2 );
458 
464  void writeFilledCircle( const QString &layer, const QColor &color, const QgsPoint &pt, double radius ) SIP_PYNAME( writeFillCircleV2 );
465 
471  void writeCircle( const QString &layer, const QColor &color, const QgsPoint &pt, double radius, const QString &lineStyleName, double width ) SIP_PYNAME( writeCircleV2 );
472 
478  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 );
479 
485  void writeMText( const QString &layer, const QString &text, const QgsPoint &pt, double width, double angle, const QColor &color );
486 
494  static double mapUnitScaleFactor( double scale, QgsUnitTypes::RenderUnit symbolUnits, QgsUnitTypes::DistanceUnit mapUnits, double mapUnitsPerPixel = 1.0 );
495 
502  void clipValueToMapUnitScale( double &value, const QgsMapUnitScale &scale, double pixelToMMFactor ) const;
503 
505  static QString dxfLayerName( const QString &name );
506 
508  static QString dxfEncoding( const QString &name );
509 
511  static QStringList encodings();
512 
518  void drawLabel( const QString &layerId, QgsRenderContext &context, pal::LabelPosition *label, const QgsPalLayerSettings &settings ) SIP_SKIP override;
519 
528  Q_DECL_DEPRECATED void registerDxfLayer( const QString &layerId, QgsFeatureId fid, const QString &layer );
529 
530  private:
531 
532 #ifdef SIP_RUN
533  QgsDxfExport( const QgsDxfExport &other );
534  QgsDxfExport &operator=( const QgsDxfExport & );
535 #endif
536  QgsRectangle mExtent;
539  double mSymbologyScale = 1.0;
540  SymbologyExport mSymbologyExport = NoSymbology;
542  bool mLayerTitleAsName = false;
543 
544  QTextStream mTextStream;
545 
546  int mSymbolLayerCounter = 0; //internal counter
547  int mNextHandleId = DXF_HANDSEED;
548  int mBlockCounter = 0;
549 
550  QHash< const QgsSymbolLayer *, QString > mLineStyles; //symbol layer name types
551  QHash< const QgsSymbolLayer *, QString > mPointSymbolBlocks; //reference to point symbol blocks
552 
553  //AC1009
554  void writeHeader( const QString &codepage );
555  void prepareRenderers();
556  void writeTables();
557  void writeBlocks();
558  void writeEntities();
559  void writeEntitiesSymbolLevels( DxfLayerJob *job );
560  void stopRenderers();
561  void writeEndFile();
562 
563  void startSection();
564  void endSection();
565 
566  void writePoint( const QgsPoint &pt, const QString &layer, const QColor &color, QgsSymbolRenderContext &ctx, const QgsSymbolLayer *symbolLayer, const QgsSymbol *symbol, double angle );
567  void writeDefaultLinetypes();
568  void writeSymbolLayerLinetype( const QgsSymbolLayer *symbolLayer );
569  void writeLinetype( const QString &styleName, const QVector<qreal> &pattern, QgsUnitTypes::RenderUnit u );
570 
574  void writeText( const QString &layer, const QString &text, pal::LabelPosition *label, const QgsPalLayerSettings &layerSettings, const QgsExpressionContext &expressionContext );
575 
584  void addGeometryGeneratorSymbolLayer( QgsSymbolRenderContext &ctx, const QgsCoordinateTransform &ct, const QString &layer, QgsSymbolLayer *symbolLayer, bool allSymbolLayers );
585 
586  void addFeature( QgsSymbolRenderContext &ctx, const QgsCoordinateTransform &ct, const QString &layer, const QgsSymbolLayer *symbolLayer, const QgsSymbol *symbol );
587 
588  //returns dxf palette index from symbol layer color
589  static QColor colorFromSymbolLayer( const QgsSymbolLayer *symbolLayer, QgsSymbolRenderContext &ctx );
590  QString lineStyleFromSymbolLayer( const QgsSymbolLayer *symbolLayer );
591 
592  //functions for dxf palette
593  static int color_distance( QRgb p1, int index );
594  static QRgb createRgbEntry( qreal r, qreal g, qreal b );
595 
596  //helper functions for symbology export
597  QgsRenderContext renderContext() const;
598 
599  QList< QPair< QgsSymbolLayer *, QgsSymbol * > > symbolLayers( QgsRenderContext &context );
600  static int nLineTypes( const QList< QPair< QgsSymbolLayer *, QgsSymbol *> > &symbolLayers );
601  static bool hasDataDefinedProperties( const QgsSymbolLayer *sl, const QgsSymbol *symbol );
602  double dashSize() const;
603  double dotSize() const;
604  double dashSeparatorSize() const;
605  double sizeToMapUnits( double s ) const;
606  static QString lineNameFromPenStyle( Qt::PenStyle style );
607  bool layerIsScaleBasedVisible( const QgsMapLayer *layer ) const;
608 
609  QHash<QString, int> mBlockHandles;
610  QString mBlockHandle;
611 
613  QMap< QString, QMap<QgsFeatureId, QString> > mDxfLayerNames;
615  QgsMapSettings mMapSettings;
616  QHash<QString, int> mLayerNameAttribute;
617  double mFactor = 1.0;
618  bool mForce2d = false;
619 
620  QgsDxfExport::Flags mFlags = QgsDxfExport::Flags();
621 
622  void appendCurve( const QgsCurve &c, QVector<QgsPoint> &points, QVector<double> &bulges );
623  void appendLineString( const QgsLineString &ls, QVector<QgsPoint> &points, QVector<double> &bulges );
624  void appendCircularString( const QgsCircularString &cs, QVector<QgsPoint> &points, QVector<double> &bulges );
625  void appendCompoundCurve( const QgsCompoundCurve &cc, QVector<QgsPoint> &points, QVector<double> &bulges );
626 
627  QgsRenderContext mRenderContext;
628  // Internal cache for layer related information required during rendering
629  QList<DxfLayerJob *> mJobs;
630  std::unique_ptr<QgsLabelingEngine> mLabelingEngine;
631 };
632 
633 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDxfExport::Flags )
634 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDxfExport::DxfPolylineFlags )
635 
636 #endif // QGSDXFEXPORT_H
QgsCurve
Abstract base class for curved geometry type.
Definition: qgscurve.h:36
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:370
SIP_PYNAME
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
QgsUnitTypes::RenderUnit
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:167
QgsDxfExport
Definition: qgsdxfexport.h:63
QgsDxfExport::setSymbologyExport
void setSymbologyExport(QgsDxfExport::SymbologyExport e)
Set symbology export mode.
Definition: qgsdxfexport.h:253
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:38
QgsPalLayerSettings
Definition: qgspallabeling.h:207
QgsDxfExport::DxfPolylineFlag
DxfPolylineFlag
Flags for polylines.
Definition: qgsdxfexport.h:158
QgsDxfExport::DxfLayer::layer
QgsVectorLayer * layer() const
Returns the layer.
Definition: qgsdxfexport.h:79
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:51
pal::LabelPosition
LabelPosition is a candidate feature label position.
Definition: labelposition.h:56
QgsCurvePolygon
Curve polygon geometry type.
Definition: qgscurvepolygon.h:35
QgsLabelSink
Abstract base class that can be used to intercept rendered labels from a labeling / rendering job.
Definition: qgslabelsink.h:38
qgis.h
QgsDxfExport::DxfLayer::DxfLayer
DxfLayer(QgsVectorLayer *vl, int layerOutputAttributeIndex=-1)
Definition: qgsdxfexport.h:73
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:58
QgsSymbol
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:64
qgsmapsettings.h
QgsLineString
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:44
QgsUnitTypes::DistanceUnit
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:68
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
pal
Definition: qgsdiagramrenderer.h:49
QgsSymbolRenderContext
Definition: qgssymbol.h:695
QgsDxfExport::force2d
bool force2d()
Retrieve whether the output should be forced to 2d.
Definition: qgsdxfexport.h:302
QgsSymbolLayer
Definition: qgssymbollayer.h:53
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsCircularString
Circular string geometry type.
Definition: qgscircularstring.h:35
DxfLayerJob
Holds information about each layer in a DXF job.
Definition: qgsdxfexport_p.h:32
qgslabelsink.h
DXF_HANDSEED
#define DXF_HANDSEED
Definition: qgsdxfexport.h:44
qgis_sip.h
Q_DECLARE_OPERATORS_FOR_FLAGS
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.
QgsDxfExport::setExtent
void setExtent(const QgsRectangle &r)
Set extent of area to export.
Definition: qgsdxfexport.h:266
QgsUnitTypes
Helper functions for various unit types.
Definition: qgsunittypes.h:39
QgsUnitTypes::DistanceMeters
@ DistanceMeters
Meters.
Definition: qgsunittypes.h:69
QgsDxfExport::HAlign
HAlign
Horizontal alignments.
Definition: qgsdxfexport.h:142
QgsDxfExport::Flag
Flag
Export flags.
Definition: qgsdxfexport.h:110
QgsMapUnitScale
Struct for storing maximum and minimum scales for measurements in map units.
Definition: qgsmapunitscale.h:38
QgsRingSequence
QVector< QgsPointSequence > QgsRingSequence
Definition: qgsabstractgeometry.h:50
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:206
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:44
QgsDxfExport::setForce2d
void setForce2d(bool force2d)
Force 2d output (eg.
Definition: qgsdxfexport.h:295
QgsDxfExport::DxfLayer::layerOutputAttributeIndex
int layerOutputAttributeIndex() const
Returns the attribute index used to split into multiple layers.
Definition: qgsdxfexport.h:86
QgsDxfExport::SymbologyExport
SymbologyExport
Definition: qgsdxfexport.h:102
QgsDxfExport::VAlign
VAlign
Vertical alignments.
Definition: qgsdxfexport.h:132
qgsgeometry.h
QgsDxfExport::DxfLayer
Layers and optional attribute index to split into multiple layers using attribute value as layer name...
Definition: qgsdxfexport.h:72
QgsPointSequence
QVector< QgsPoint > QgsPointSequence
Definition: qgsabstractgeometry.h:46
c
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
Definition: porting_processing.dox:1
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsMapLayer
Base class for all map layer types.
Definition: qgsmaplayer.h:83
QgsFeature
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
QgsDxfExport::symbologyScale
double symbologyScale() const
Returns the reference scale for output.
Definition: qgsdxfexport.h:227
QgsDxfExport::ExportResult
ExportResult
The result of an export as dxf operation.
Definition: qgsdxfexport.h:121
QgsDxfExport::extent
QgsRectangle extent() const
Gets extent of area to export.
Definition: qgsdxfexport.h:273
QgsDxfExport::FeatureSymbology
@ FeatureSymbology
Keeps the number of features and export symbology per feature (using the first symbol level)
Definition: qgsdxfexport.h:104
QgsMapSettings
The QgsMapSettings class contains configuration for rendering of the map.
Definition: qgsmapsettings.h:88
QgsCoordinateTransform
Class for doing transforms between two map coordinate systems.
Definition: qgscoordinatetransform.h:53
QgsDxfExport::symbologyExport
QgsDxfExport::SymbologyExport symbologyExport() const
Gets symbology export mode.
Definition: qgsdxfexport.h:260
MathUtils::angle
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
QgsDxfExport::setLayerTitleAsName
void setLayerTitleAsName(bool layerTitleAsName)
Enable use of title (where set) instead of layer name, when attribute index of corresponding layer in...
Definition: qgsdxfexport.h:281
qgssymbol.h
QgsCompoundCurve
Compound curve geometry type.
Definition: qgscompoundcurve.h:32
QgsDxfExport::layerTitleAsName
bool layerTitleAsName()
Retrieve whether layer title (where set) instead of name shall be use.
Definition: qgsdxfexport.h:288
QgsFeatureId
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28