QGIS API Documentation  3.6.0-Noosa (5873452)
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 
38 #define DXF_HANDSEED 100
39 #define DXF_HANDMAX 9999999
40 #define DXF_HANDPLOTSTYLE 0xf
41 
42 namespace pal SIP_SKIP
43 {
44  class LabelPosition;
45 }
46 
51 class CORE_EXPORT QgsDxfExport
52 {
53  public:
54 
59  struct DxfLayer
60  {
61  DxfLayer( QgsVectorLayer *vl, int layerOutputAttributeIndex = -1 )
62  : mLayer( vl )
63  , mLayerOutputAttributeIndex( layerOutputAttributeIndex )
64  {}
65 
67  QgsVectorLayer *layer() const {return mLayer;}
68 
73  int layerOutputAttributeIndex() const {return mLayerOutputAttributeIndex;}
74 
75  private:
76  QgsVectorLayer *mLayer = nullptr;
77  int mLayerOutputAttributeIndex = -1;
78  };
79 
81  {
82  NoSymbology = 0,
84  SymbolLayerSymbology
85  };
86 
88  enum Flag
89  {
90  FlagNoMText = 1 << 1,
91  };
92  Q_DECLARE_FLAGS( Flags, Flag )
93 
94 
97  QgsDxfExport() = default;
98  QgsDxfExport( const QgsDxfExport &dxfExport ) SIP_SKIP;
99  QgsDxfExport &operator=( const QgsDxfExport &dxfExport );
100 
105  void setMapSettings( const QgsMapSettings &settings );
106 
112  void setFlags( QgsDxfExport::Flags flags );
113 
119  QgsDxfExport::Flags flags() const;
120 
126  void addLayers( const QList< QgsDxfExport::DxfLayer > &layers );
127 
134  int writeToFile( QIODevice *d, const QString &codec ); //maybe add progress dialog? other parameters (e.g. scale, dpi)?
135 
142  void setSymbologyScale( double scale ) { mSymbologyScale = scale; }
143 
150  double symbologyScale() const { return mSymbologyScale; }
151 
156  QgsUnitTypes::DistanceUnit mapUnits() const;
157 
163  void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
164 
170  QgsCoordinateReferenceSystem destinationCrs() const;
171 
176  void setSymbologyExport( QgsDxfExport::SymbologyExport e ) { mSymbologyExport = e; }
177 
183  QgsDxfExport::SymbologyExport symbologyExport() const { return mSymbologyExport; }
184 
189  void setExtent( const QgsRectangle &r ) { mExtent = r; }
190 
196  QgsRectangle extent() const { return mExtent; }
197 
204  void setLayerTitleAsName( bool layerTitleAsName ) { mLayerTitleAsName = layerTitleAsName; }
205 
211  bool layerTitleAsName() { return mLayerTitleAsName; }
212 
218  void setForce2d( bool force2d ) { mForce2d = force2d; }
219 
225  bool force2d() { return mForce2d; }
226 
231  static int closestColorMatch( QRgb color );
232 
239  QString layerName( const QString &id, const QgsFeature &f ) const;
240 
247  QString layerName( QgsVectorLayer *vl ) const;
248 
255  void writeGroup( int code, int i ) SIP_PYNAME( writeGroupInt );
256 
263  void writeGroup( int code, double d ) SIP_PYNAME( writeGroupDouble );
264 
270  void writeGroup( int code, const QString &s );
271 
279  void writeGroup( int code, const QgsPoint &p ) SIP_PYNAME( writeGroupPointV2 );
280 
289  void writeGroup( const QColor &color, int exactMatch = 62, int rgbCode = 420, int transparencyCode = 440 );
290 
295  void writeGroupCode( int code );
296 
301  void writeInt( int i );
302 
307  void writeDouble( double d );
308 
313  void writeString( const QString &s );
314 
321  int writeHandle( int code = 5, int handle = 0 );
322 
333  void writePolyline( const QgsPointSequence &line, const QString &layer, const QString &lineStyleName, const QColor &color, double width = -1 ) SIP_SKIP;
334 
344  void writePolygon( const QgsRingSequence &polygon, const QString &layer, const QString &hatchPattern, const QColor &color ) SIP_SKIP;
345 
350  void writeLine( const QgsPoint &pt1, const QgsPoint &pt2, const QString &layer, const QString &lineStyleName, const QColor &color, double width = -1 );
351 
357  void writePoint( const QString &layer, const QColor &color, const QgsPoint &pt ) SIP_PYNAME( writePointV2 );
358 
364  void writeFilledCircle( const QString &layer, const QColor &color, const QgsPoint &pt, double radius ) SIP_PYNAME( writeFillCircleV2 );
365 
371  void writeCircle( const QString &layer, const QColor &color, const QgsPoint &pt, double radius, const QString &lineStyleName, double width ) SIP_PYNAME( writeCircleV2 );
372 
378  void writeText( const QString &layer, const QString &text, const QgsPoint &pt, double size, double angle, const QColor &color ) SIP_PYNAME( writeTextV2 );
379 
385  void writeMText( const QString &layer, const QString &text, const QgsPoint &pt, double width, double angle, const QColor &color );
386 
394  static double mapUnitScaleFactor( double scale, QgsUnitTypes::RenderUnit symbolUnits, QgsUnitTypes::DistanceUnit mapUnits, double mapUnitsPerPixel = 1.0 );
395 
402  void clipValueToMapUnitScale( double &value, const QgsMapUnitScale &scale, double pixelToMMFactor ) const;
403 
405  static QString dxfLayerName( const QString &name );
406 
408  static QString dxfEncoding( const QString &name );
409 
411  static QStringList encodings();
412 
421  void drawLabel( const QString &layerId, QgsRenderContext &context, pal::LabelPosition *label, const QgsPalLayerSettings &settings ) SIP_SKIP;
422 
429  void registerDxfLayer( const QString &layerId, QgsFeatureId fid, const QString &layer );
430 
431  private:
433  QgsRectangle mExtent;
435  double mSymbologyScale = 1.0;
436  SymbologyExport mSymbologyExport = NoSymbology;
438  bool mLayerTitleAsName = false;
439 
440  QTextStream mTextStream;
441 
442  static int sDxfColors[][3];
443  static const char *DXF_ENCODINGS[][2];
444 
445  int mSymbolLayerCounter = 0; //internal counter
446  int mNextHandleId = DXF_HANDSEED;
447  int mBlockCounter = 0;
448 
449  QHash< const QgsSymbolLayer *, QString > mLineStyles; //symbol layer name types
450  QHash< const QgsSymbolLayer *, QString > mPointSymbolBlocks; //reference to point symbol blocks
451 
452  //AC1009
453  void writeHeader( const QString &codepage );
454  void writeTables();
455  void writeBlocks();
456  void writeEntities();
457  void writeEntitiesSymbolLevels( QgsVectorLayer *layer );
458  void writeEndFile();
459 
460  void startSection();
461  void endSection();
462 
463  void writePoint( const QgsPoint &pt, const QString &layer, const QColor &color, QgsSymbolRenderContext &ctx, const QgsSymbolLayer *symbolLayer, const QgsSymbol *symbol, double angle );
464  void writeDefaultLinetypes();
465  void writeSymbolLayerLinetype( const QgsSymbolLayer *symbolLayer );
466  void writeLinetype( const QString &styleName, const QVector<qreal> &pattern, QgsUnitTypes::RenderUnit u );
467 
476  void addGeometryGeneratorSymbolLayer( QgsSymbolRenderContext &ctx, const QgsCoordinateTransform &ct, const QString &layer, QgsSymbolLayer *symbolLayer, bool allSymbolLayers );
477 
478  void addFeature( QgsSymbolRenderContext &ctx, const QgsCoordinateTransform &ct, const QString &layer, const QgsSymbolLayer *symbolLayer, const QgsSymbol *symbol );
479 
480  //returns dxf palette index from symbol layer color
481  static QColor colorFromSymbolLayer( const QgsSymbolLayer *symbolLayer, QgsSymbolRenderContext &ctx );
482  QString lineStyleFromSymbolLayer( const QgsSymbolLayer *symbolLayer );
483 
484  //functions for dxf palette
485  static int color_distance( QRgb p1, int index );
486  static QRgb createRgbEntry( qreal r, qreal g, qreal b );
487 
488  //helper functions for symbology export
489  QgsRenderContext renderContext() const;
490 
491  QList< QPair< QgsSymbolLayer *, QgsSymbol * > > symbolLayers( QgsRenderContext &context );
492  static int nLineTypes( const QList< QPair< QgsSymbolLayer *, QgsSymbol *> > &symbolLayers );
493  static bool hasDataDefinedProperties( const QgsSymbolLayer *sl, const QgsSymbol *symbol );
494  double dashSize() const;
495  double dotSize() const;
496  double dashSeparatorSize() const;
497  double sizeToMapUnits( double s ) const;
498  static QString lineNameFromPenStyle( Qt::PenStyle style );
499  bool layerIsScaleBasedVisible( const QgsMapLayer *layer ) const;
500 
501  QHash<QString, int> mBlockHandles;
502  QString mBlockHandle;
503 
505  QMap< QString, QMap<QgsFeatureId, QString> > mDxfLayerNames;
507  QgsMapSettings mMapSettings;
508  QHash<QString, int> mLayerNameAttribute;
509  double mFactor = 1.0;
510  bool mForce2d = false;
511 
512  QgsDxfExport::Flags mFlags = nullptr;
513 
514 };
515 
516 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDxfExport::Flags )
517 
518 #endif // QGSDXFEXPORT_H
A rectangle specified with double values.
Definition: qgsrectangle.h:41
Base class for all map layer types.
Definition: qgsmaplayer.h:64
double symbologyScale() const
Returns the reference scale for output.
Definition: qgsdxfexport.h:150
QgsDxfExport::SymbologyExport symbologyExport() const
Gets symbology export mode.
Definition: qgsdxfexport.h:183
DxfLayer(QgsVectorLayer *vl, int layerOutputAttributeIndex=-1)
Definition: qgsdxfexport.h:61
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:61
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:225
qint64 QgsFeatureId
Definition: qgsfeatureid.h:25
void setSymbologyExport(QgsDxfExport::SymbologyExport e)
Set symbology export mode.
Definition: qgsdxfexport.h:176
QgsVectorLayer * layer() const
Returns the layer.
Definition: qgsdxfexport.h:67
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:59
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:38
The QgsMapSettings class contains configuration for rendering of the map.
#define SIP_SKIP
Definition: qgis_sip.h:119
void setForce2d(bool force2d)
Force 2d output (eg.
Definition: qgsdxfexport.h:218
Keeps the number of features and export symbology per feature (using the first symbol level) ...
Definition: qgsdxfexport.h:83
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:54
QVector< QgsPoint > QgsPointSequence
QVector< QgsPointSequence > QgsRingSequence
void setSymbologyScale(double scale)
Set reference scale for output.
Definition: qgsdxfexport.h:142
Contains information about the context of a rendering operation.
QgsRectangle extent() const
Gets extent of area to export.
Definition: qgsdxfexport.h:196
Flag
Export flags.
Definition: qgsdxfexport.h:88
void setLayerTitleAsName(bool layerTitleAsName)
Enable use of title (where set) instead of layer name, when attribute index of corresponding layer in...
Definition: qgsdxfexport.h:204
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:189
bool layerTitleAsName()
Retrieve whether layer title (where set) instead of name shall be use.
Definition: qgsdxfexport.h:211
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
Represents a vector layer which manages a vector based data sets.
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:111
int layerOutputAttributeIndex() const
Returns the attribute index used to split into multiple layers.
Definition: qgsdxfexport.h:73
#define SIP_PYNAME(name)
Definition: qgis_sip.h:74