QGIS API Documentation  3.12.1-București (121cc00ff0)
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 
28 #include <QColor>
29 #include <QList>
30 #include <QTextStream>
31 
32 class QgsMapLayer;
33 class QgsPointXY;
34 class QgsSymbolLayer;
35 class QIODevice;
37 class QgsCurve;
38 class QgsCurvePolygon;
39 class QgsCircularString;
40 class QgsCompoundCurve;
41 struct DxfLayerJob;
42 
43 #define DXF_HANDSEED 100
44 #define DXF_HANDMAX 9999999
45 #define DXF_HANDPLOTSTYLE 0xf
46 
47 namespace pal // SIP_SKIP
48 {
49  class LabelPosition;
50 }
51 
52 
57 class CORE_EXPORT QgsDxfExport
58 {
59  public:
60 
65  struct CORE_EXPORT DxfLayer
66  {
67  DxfLayer( QgsVectorLayer *vl, int layerOutputAttributeIndex = -1 )
68  : mLayer( vl )
69  , mLayerOutputAttributeIndex( layerOutputAttributeIndex )
70  {}
71 
73  QgsVectorLayer *layer() const {return mLayer;}
74 
80  int layerOutputAttributeIndex() const {return mLayerOutputAttributeIndex;}
81 
88  QString splitLayerAttribute() const;
89 
90  private:
91  QgsVectorLayer *mLayer = nullptr;
92  int mLayerOutputAttributeIndex = -1;
93  };
94 
96  {
97  NoSymbology = 0,
99  SymbolLayerSymbology
100  };
101 
103  enum Flag
104  {
105  FlagNoMText = 1 << 1,
106  };
107  Q_DECLARE_FLAGS( Flags, Flag )
108 
109 
114  enum class ExportResult
115  {
116  Success = 0,
117  InvalidDeviceError,
118  DeviceNotWritableError,
119  EmptyExtentError
120  };
121 
125  enum class VAlign : int
126  {
127  VBaseLine = 0,
128  VBottom = 1,
129  VMiddle = 2,
130  VTop = 3,
131  Undefined = 9999
132  };
133 
135  enum class HAlign : int
136  {
137  HLeft = 0,
138  HCenter = 1,
139  HRight = 2,
140  HAligned = 3,
141  HMiddle = 4,
142  HFit = 5,
143  Undefined = 9999
144  };
145 
152  {
153  Closed = 1,
154  Curve = 2,
155  Spline = 4,
156  Is3DPolyline = 8,
157  Is3DPolygonMesh = 16,
158  PolygonMesh = 32,
159  PolyfaceMesh = 64,
160  ContinuousPattern = 128,
161  };
162 
163  Q_DECLARE_FLAGS( DxfPolylineFlags, DxfPolylineFlag )
164 
165 
168  QgsDxfExport();
169 
170  ~QgsDxfExport();
171 
176  void setMapSettings( const QgsMapSettings &settings );
177 
183  void setFlags( QgsDxfExport::Flags flags );
184 
190  QgsDxfExport::Flags flags() const;
191 
197  void addLayers( const QList< QgsDxfExport::DxfLayer > &layers );
198 
205  ExportResult writeToFile( QIODevice *d, const QString &codec ); //maybe add progress dialog? other parameters (e.g. scale, dpi)?
206 
213  void setSymbologyScale( double scale ) { mSymbologyScale = scale; }
214 
221  double symbologyScale() const { return mSymbologyScale; }
222 
227  QgsUnitTypes::DistanceUnit mapUnits() const;
228 
234  void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
235 
241  QgsCoordinateReferenceSystem destinationCrs() const;
242 
247  void setSymbologyExport( QgsDxfExport::SymbologyExport e ) { mSymbologyExport = e; }
248 
254  QgsDxfExport::SymbologyExport symbologyExport() const { return mSymbologyExport; }
255 
260  void setExtent( const QgsRectangle &r ) { mExtent = r; }
261 
267  QgsRectangle extent() const { return mExtent; }
268 
275  void setLayerTitleAsName( bool layerTitleAsName ) { mLayerTitleAsName = layerTitleAsName; }
276 
282  bool layerTitleAsName() { return mLayerTitleAsName; }
283 
289  void setForce2d( bool force2d ) { mForce2d = force2d; }
290 
296  bool force2d() { return mForce2d; }
297 
302  static int closestColorMatch( QRgb color );
303 
310  QString layerName( const QString &id, const QgsFeature &f ) const;
311 
318  QString layerName( QgsVectorLayer *vl ) const;
319 
326  void writeGroup( int code, int i ) SIP_PYNAME( writeGroupInt );
327 
334  void writeGroup( int code, double d ) SIP_PYNAME( writeGroupDouble );
335 
341  void writeGroup( int code, const QString &s );
342 
350  void writeGroup( int code, const QgsPoint &p ) SIP_PYNAME( writeGroupPointV2 );
351 
360  void writeGroup( const QColor &color, int exactMatch = 62, int rgbCode = 420, int transparencyCode = 440 );
361 
366  void writeGroupCode( int code );
367 
372  void writeInt( int i );
373 
378  void writeDouble( double d );
379 
384  void writeString( const QString &s );
385 
392  int writeHandle( int code = 5, int handle = 0 );
393 
404  void writePolyline( const QgsPointSequence &line, const QString &layer, const QString &lineStyleName, const QColor &color, double width = -1 ) SIP_SKIP;
405 
416  void writePolyline( const QgsCurve &curve, const QString &layer, const QString &lineStyleName, const QColor &color, double width = -1 ) SIP_SKIP;
417 
427  void writePolygon( const QgsRingSequence &polygon, const QString &layer, const QString &hatchPattern, const QColor &color ) SIP_SKIP;
428 
438  void writePolygon( const QgsCurvePolygon &polygon, const QString &layer, const QString &hatchPattern, const QColor &color ) SIP_SKIP;
439 
444  void writeLine( const QgsPoint &pt1, const QgsPoint &pt2, const QString &layer, const QString &lineStyleName, const QColor &color, double width = -1 );
445 
451  void writePoint( const QString &layer, const QColor &color, const QgsPoint &pt ) SIP_PYNAME( writePointV2 );
452 
458  void writeFilledCircle( const QString &layer, const QColor &color, const QgsPoint &pt, double radius ) SIP_PYNAME( writeFillCircleV2 );
459 
465  void writeCircle( const QString &layer, const QColor &color, const QgsPoint &pt, double radius, const QString &lineStyleName, double width ) SIP_PYNAME( writeCircleV2 );
466 
472  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 );
473 
479  void writeMText( const QString &layer, const QString &text, const QgsPoint &pt, double width, double angle, const QColor &color );
480 
488  static double mapUnitScaleFactor( double scale, QgsUnitTypes::RenderUnit symbolUnits, QgsUnitTypes::DistanceUnit mapUnits, double mapUnitsPerPixel = 1.0 );
489 
496  void clipValueToMapUnitScale( double &value, const QgsMapUnitScale &scale, double pixelToMMFactor ) const;
497 
499  static QString dxfLayerName( const QString &name );
500 
502  static QString dxfEncoding( const QString &name );
503 
505  static QStringList encodings();
506 
515  void drawLabel( const QString &layerId, QgsRenderContext &context, pal::LabelPosition *label, const QgsPalLayerSettings &settings ) SIP_SKIP;
516 
523  void registerDxfLayer( const QString &layerId, QgsFeatureId fid, const QString &layer );
524 
525  private:
526 
527 #ifdef SIP_RUN
528  QgsDxfExport( const QgsDxfExport &other );
529  QgsDxfExport &operator=( const QgsDxfExport & );
530 #endif
531  QgsRectangle mExtent;
534  double mSymbologyScale = 1.0;
535  SymbologyExport mSymbologyExport = NoSymbology;
537  bool mLayerTitleAsName = false;
538 
539  QTextStream mTextStream;
540 
541  int mSymbolLayerCounter = 0; //internal counter
542  int mNextHandleId = DXF_HANDSEED;
543  int mBlockCounter = 0;
544 
545  QHash< const QgsSymbolLayer *, QString > mLineStyles; //symbol layer name types
546  QHash< const QgsSymbolLayer *, QString > mPointSymbolBlocks; //reference to point symbol blocks
547 
548  //AC1009
549  void writeHeader( const QString &codepage );
550  void prepareRenderers();
551  void writeTables();
552  void writeBlocks();
553  void writeEntities();
554  void writeEntitiesSymbolLevels( DxfLayerJob *job );
555  void stopRenderers();
556  void writeEndFile();
557 
558  void startSection();
559  void endSection();
560 
561  void writePoint( const QgsPoint &pt, const QString &layer, const QColor &color, QgsSymbolRenderContext &ctx, const QgsSymbolLayer *symbolLayer, const QgsSymbol *symbol, double angle );
562  void writeDefaultLinetypes();
563  void writeSymbolLayerLinetype( const QgsSymbolLayer *symbolLayer );
564  void writeLinetype( const QString &styleName, const QVector<qreal> &pattern, QgsUnitTypes::RenderUnit u );
565 
569  void writeText( const QString &layer, const QString &text, pal::LabelPosition *label, const QgsPalLayerSettings &layerSettings, const QgsExpressionContext &expressionContext );
570 
579  void addGeometryGeneratorSymbolLayer( QgsSymbolRenderContext &ctx, const QgsCoordinateTransform &ct, const QString &layer, QgsSymbolLayer *symbolLayer, bool allSymbolLayers );
580 
581  void addFeature( QgsSymbolRenderContext &ctx, const QgsCoordinateTransform &ct, const QString &layer, const QgsSymbolLayer *symbolLayer, const QgsSymbol *symbol );
582 
583  //returns dxf palette index from symbol layer color
584  static QColor colorFromSymbolLayer( const QgsSymbolLayer *symbolLayer, QgsSymbolRenderContext &ctx );
585  QString lineStyleFromSymbolLayer( const QgsSymbolLayer *symbolLayer );
586 
587  //functions for dxf palette
588  static int color_distance( QRgb p1, int index );
589  static QRgb createRgbEntry( qreal r, qreal g, qreal b );
590 
591  //helper functions for symbology export
592  QgsRenderContext renderContext() const;
593 
594  QList< QPair< QgsSymbolLayer *, QgsSymbol * > > symbolLayers( QgsRenderContext &context );
595  static int nLineTypes( const QList< QPair< QgsSymbolLayer *, QgsSymbol *> > &symbolLayers );
596  static bool hasDataDefinedProperties( const QgsSymbolLayer *sl, const QgsSymbol *symbol );
597  double dashSize() const;
598  double dotSize() const;
599  double dashSeparatorSize() const;
600  double sizeToMapUnits( double s ) const;
601  static QString lineNameFromPenStyle( Qt::PenStyle style );
602  bool layerIsScaleBasedVisible( const QgsMapLayer *layer ) const;
603 
604  QHash<QString, int> mBlockHandles;
605  QString mBlockHandle;
606 
608  QMap< QString, QMap<QgsFeatureId, QString> > mDxfLayerNames;
610  QgsMapSettings mMapSettings;
611  QHash<QString, int> mLayerNameAttribute;
612  double mFactor = 1.0;
613  bool mForce2d = false;
614 
615  QgsDxfExport::Flags mFlags = nullptr;
616 
617  void appendCurve( const QgsCurve &c, QVector<QgsPoint> &points, QVector<double> &bulges );
618  void appendLineString( const QgsLineString &ls, QVector<QgsPoint> &points, QVector<double> &bulges );
619  void appendCircularString( const QgsCircularString &cs, QVector<QgsPoint> &points, QVector<double> &bulges );
620  void appendCompoundCurve( const QgsCompoundCurve &cc, QVector<QgsPoint> &points, QVector<double> &bulges );
621 
622  QgsRenderContext mRenderContext;
623  // Internal cache for layer related information required during rendering
624  QList<DxfLayerJob *> mJobs;
625  std::unique_ptr<QgsLabelingEngine> mLabelingEngine;
626 };
627 
628 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDxfExport::Flags )
629 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDxfExport::DxfPolylineFlags )
630 
631 #endif // QGSDXFEXPORT_H
A rectangle specified with double values.
Definition: qgsrectangle.h:41
Base class for all map layer types.
Definition: qgsmaplayer.h:79
double symbologyScale() const
Returns the reference scale for output.
Definition: qgsdxfexport.h:221
QgsDxfExport::SymbologyExport symbologyExport() const
Gets symbology export mode.
Definition: qgsdxfexport.h:254
DxfLayer(QgsVectorLayer *vl, int layerOutputAttributeIndex=-1)
Definition: qgsdxfexport.h:67
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:62
A class to represent a 2D point.
Definition: qgspointxy.h:43
bool force2d()
Retrieve whether the output should be forced to 2d.
Definition: qgsdxfexport.h:296
qint64 QgsFeatureId
Definition: qgsfeatureid.h:25
Curve polygon geometry type.
Holds information about each layer in a DXF job.
void setSymbologyExport(QgsDxfExport::SymbologyExport e)
Set symbology export mode.
Definition: qgsdxfexport.h:247
QgsVectorLayer * layer() const
Returns the layer.
Definition: qgsdxfexport.h:73
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
Layers and optional attribute index to split into multiple layers using attribute value as layer name...
Definition: qgsdxfexport.h:65
const QgsCoordinateReferenceSystem & crs
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
#define DXF_HANDSEED
Definition: qgsdxfexport.h:43
The QgsMapSettings class contains configuration for rendering of the map.
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
void setForce2d(bool force2d)
Force 2d output (eg.
Definition: qgsdxfexport.h:289
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Abstract base class for curved geometry type.
Definition: qgscurve.h:35
VAlign
Vertical alignments.
Definition: qgsdxfexport.h:125
Keeps the number of features and export symbology per feature (using the first symbol level) ...
Definition: qgsdxfexport.h:98
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:66
QVector< QgsPoint > QgsPointSequence
QVector< QgsPointSequence > QgsRingSequence
void setSymbologyScale(double scale)
Set reference scale for output.
Definition: qgsdxfexport.h:213
Contains information about the context of a rendering operation.
HAlign
Horizontal alignments.
Definition: qgsdxfexport.h:135
ExportResult
The result of an export as dxf operation.
Definition: qgsdxfexport.h:114
QgsRectangle extent() const
Gets extent of area to export.
Definition: qgsdxfexport.h:267
Flag
Export flags.
Definition: qgsdxfexport.h:103
void setLayerTitleAsName(bool layerTitleAsName)
Enable use of title (where set) instead of layer name, when attribute index of corresponding layer in...
Definition: qgsdxfexport.h:275
Struct for storing maximum and minimum scales for measurements in map units.
void setExtent(const QgsRectangle &r)
Set extent of area to export.
Definition: qgsdxfexport.h:260
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:43
bool layerTitleAsName()
Retrieve whether layer title (where set) instead of name shall be use.
Definition: qgsdxfexport.h:282
This class represents a coordinate reference system (CRS).
Class for doing transforms between two map coordinate systems.
LabelPosition is a candidate feature label position.
Definition: labelposition.h:55
Compound curve geometry type.
Circular string geometry type.
Represents a vector layer which manages a vector based data sets.
DxfPolylineFlag
Flags for polylines.
Definition: qgsdxfexport.h:151
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:145
int layerOutputAttributeIndex() const
Returns the attribute index used to split into multiple layers.
Definition: qgsdxfexport.h:80
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81