QGIS API Documentation  2.99.0-Master (cd0ba91)
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:
55  {
56  NoSymbology = 0, //export only data
57  FeatureSymbology, //Keeps the number of features and export symbology per feature (using the first symbol level)
58  SymbolLayerSymbology //Exports one feature per symbol layer (considering symbol levels)
59  };
60 
64  QgsDxfExport() = default;
65  QgsDxfExport( const QgsDxfExport &dxfExport ) SIP_SKIP;
66  QgsDxfExport &operator=( const QgsDxfExport &dxfExport );
67 
72  void setMapSettings( const QgsMapSettings &settings );
73 
79  void addLayers( const QList< QPair<QgsVectorLayer *, int > > &layers );
80 
87  int writeToFile( QIODevice *d, const QString &codec ); //maybe add progress dialog? other parameters (e.g. scale, dpi)?
88 
95  void setSymbologyScale( double scale ) { mSymbologyScale = scale; }
96 
103  double symbologyScale() const { return mSymbologyScale; }
104 
109  QgsUnitTypes::DistanceUnit mapUnits() const;
110 
116  void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
117 
123  QgsCoordinateReferenceSystem destinationCrs() const;
124 
129  void setSymbologyExport( QgsDxfExport::SymbologyExport e ) { mSymbologyExport = e; }
130 
136  QgsDxfExport::SymbologyExport symbologyExport() const { return mSymbologyExport; }
137 
142  void setExtent( const QgsRectangle &r ) { mExtent = r; }
143 
149  QgsRectangle extent() const { return mExtent; }
150 
157  void setLayerTitleAsName( bool layerTitleAsName ) { mLayerTitleAsName = layerTitleAsName; }
158 
164  bool layerTitleAsName() { return mLayerTitleAsName; }
165 
171  void setForce2d( bool force2d ) { mForce2d = force2d; }
172 
178  bool force2d() { return mForce2d; }
179 
184  static int closestColorMatch( QRgb color );
185 
192  QString layerName( const QString &id, const QgsFeature &f ) const;
193 
200  QString layerName( QgsVectorLayer *vl ) const;
201 
208  void writeGroup( int code, int i ) SIP_PYNAME( writeGroupInt );
209 
216  void writeGroup( int code, double d ) SIP_PYNAME( writeGroupDouble );
217 
223  void writeGroup( int code, const QString &s );
224 
232  void writeGroup( int code, const QgsPoint &p ) SIP_PYNAME( writeGroupPointV2 );
233 
242  void writeGroup( const QColor &color, int exactMatch = 62, int rgbCode = 420, int transparencyCode = 440 );
243 
248  void writeGroupCode( int code );
249 
254  void writeInt( int i );
255 
260  void writeDouble( double d );
261 
266  void writeString( const QString &s );
267 
274  int writeHandle( int code = 5, int handle = 0 );
275 
286  void writePolyline( const QgsPointSequence &line, const QString &layer, const QString &lineStyleName, const QColor &color, double width = -1 ) SIP_SKIP;
287 
297  void writePolygon( const QgsRingSequence &polygon, const QString &layer, const QString &hatchPattern, const QColor &color ) SIP_SKIP;
298 
303  void writeLine( const QgsPoint &pt1, const QgsPoint &pt2, const QString &layer, const QString &lineStyleName, const QColor &color, double width = -1 );
304 
310  void writePoint( const QString &layer, const QColor &color, const QgsPoint &pt ) SIP_PYNAME( writePointV2 );
311 
317  void writeFilledCircle( const QString &layer, const QColor &color, const QgsPoint &pt, double radius ) SIP_PYNAME( writeFillCircleV2 );
318 
324  void writeCircle( const QString &layer, const QColor &color, const QgsPoint &pt, double radius, const QString &lineStyleName, double width ) SIP_PYNAME( writeCircleV2 );
325 
331  void writeText( const QString &layer, const QString &text, const QgsPoint &pt, double size, double angle, const QColor &color ) SIP_PYNAME( writeTextV2 );
332 
338  void writeMText( const QString &layer, const QString &text, const QgsPoint &pt, double width, double angle, const QColor &color );
339 
344  static double mapUnitScaleFactor( double scale, QgsUnitTypes::RenderUnit symbolUnits, QgsUnitTypes::DistanceUnit mapUnits );
345 
347  static QString dxfLayerName( const QString &name );
348 
350  static QString dxfEncoding( const QString &name );
351 
353  static QStringList encodings();
354 
363  void drawLabel( const QString &layerId, QgsRenderContext &context, pal::LabelPosition *label, const QgsPalLayerSettings &settings ) SIP_SKIP;
364 
371  void registerDxfLayer( const QString &layerId, QgsFeatureId fid, const QString &layer );
372 
373  private:
375  QgsRectangle mExtent;
377  double mSymbologyScale = 1.0;
378  SymbologyExport mSymbologyExport = NoSymbology;
380  bool mLayerTitleAsName = false;
381 
382  QTextStream mTextStream;
383 
384  static int sDxfColors[][3];
385  static const char *DXF_ENCODINGS[][2];
386 
387  int mSymbolLayerCounter = 0; //internal counter
388  int mNextHandleId = DXF_HANDSEED;
389  int mBlockCounter = 0;
390 
391  QHash< const QgsSymbolLayer *, QString > mLineStyles; //symbol layer name types
392  QHash< const QgsSymbolLayer *, QString > mPointSymbolBlocks; //reference to point symbol blocks
393 
394  //AC1009
395  void writeHeader( const QString &codepage );
396  void writeTables();
397  void writeBlocks();
398  void writeEntities();
399  void writeEntitiesSymbolLevels( QgsVectorLayer *layer );
400  void writeEndFile();
401 
402  void startSection();
403  void endSection();
404 
405  void writePoint( const QgsPoint &pt, const QString &layer, const QColor &color, QgsSymbolRenderContext &ctx, const QgsSymbolLayer *symbolLayer, const QgsSymbol *symbol, double angle );
406  void writeDefaultLinetypes();
407  void writeSymbolLayerLinetype( const QgsSymbolLayer *symbolLayer );
408  void writeLinetype( const QString &styleName, const QVector<qreal> &pattern, QgsUnitTypes::RenderUnit u );
409 
410  void addFeature( QgsSymbolRenderContext &ctx, const QgsCoordinateTransform &ct, const QString &layer, const QgsSymbolLayer *symbolLayer, const QgsSymbol *symbol );
411 
412  //returns dxf palette index from symbol layer color
413  static QColor colorFromSymbolLayer( const QgsSymbolLayer *symbolLayer, QgsSymbolRenderContext &ctx );
414  QString lineStyleFromSymbolLayer( const QgsSymbolLayer *symbolLayer );
415 
416  //functions for dxf palette
417  static int color_distance( QRgb p1, int index );
418  static QRgb createRgbEntry( qreal r, qreal g, qreal b );
419 
420  //helper functions for symbology export
421  QgsRenderContext renderContext() const;
422 
423  QList< QPair< QgsSymbolLayer *, QgsSymbol * > > symbolLayers( QgsRenderContext &context );
424  static int nLineTypes( const QList< QPair< QgsSymbolLayer *, QgsSymbol *> > &symbolLayers );
425  static bool hasDataDefinedProperties( const QgsSymbolLayer *sl, const QgsSymbol *symbol );
426  double dashSize() const;
427  double dotSize() const;
428  double dashSeparatorSize() const;
429  double sizeToMapUnits( double s ) const;
430  static QString lineNameFromPenStyle( Qt::PenStyle style );
431  bool layerIsScaleBasedVisible( const QgsMapLayer *layer ) const;
432 
433  QHash<QString, int> mBlockHandles;
434  QString mBlockHandle;
435 
437  QMap< QString, QMap<QgsFeatureId, QString> > mDxfLayerNames;
439  QgsMapSettings mMapSettings;
440  QHash<QString, int> mLayerNameAttribute;
441  double mFactor = 1.0;
442  bool mForce2d = false;
443 };
444 
445 #endif // QGSDXFEXPORT_H
A rectangle specified with double values.
Definition: qgsrectangle.h:39
Base class for all map layer types.
Definition: qgsmaplayer.h:56
double symbologyScale() const
Returns the reference scale for output.
Definition: qgsdxfexport.h:103
QgsDxfExport::SymbologyExport symbologyExport() const
Get symbology export mode.
Definition: qgsdxfexport.h:136
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:178
void setSymbologyExport(QgsDxfExport::SymbologyExport e)
Set symbology export mode.
Definition: qgsdxfexport.h:129
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
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:171
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:43
QVector< QgsPoint > QgsPointSequence
QVector< QgsPointSequence > QgsRingSequence
void setSymbologyScale(double scale)
Set reference scale for output.
Definition: qgsdxfexport.h:95
Contains information about the context of a rendering operation.
QgsRectangle extent() const
Get extent of area to export.
Definition: qgsdxfexport.h:149
void setLayerTitleAsName(bool layerTitleAsName)
Enable use of title (where set) instead of layer name, when attribute index of corresponding layer in...
Definition: qgsdxfexport.h:157
void setExtent(const QgsRectangle &r)
Set extent of area to export.
Definition: qgsdxfexport.h:142
bool layerTitleAsName()
Retrieve whether layer title (where set) instead of name shall be use.
Definition: qgsdxfexport.h:164
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
qint64 QgsFeatureId
Definition: qgsfeature.h:37
Represents a vector layer which manages a vector based data sets.
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:100
#define SIP_PYNAME(name)
Definition: qgis_sip.h:74