QGIS API Documentation  2.99.0-Master (19b062c)
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,
58  SymbolLayerSymbology
59  };
60 
62  enum Flag
63  {
64  FlagNoMText = 1 << 1,
65  };
66  Q_DECLARE_FLAGS( Flags, Flag )
67 
68 
71  QgsDxfExport() = default;
72  QgsDxfExport( const QgsDxfExport &dxfExport ) SIP_SKIP;
73  QgsDxfExport &operator=( const QgsDxfExport &dxfExport );
74 
79  void setMapSettings( const QgsMapSettings &settings );
80 
86  void setFlags( QgsDxfExport::Flags flags );
87 
93  QgsDxfExport::Flags flags() const;
94 
100  void addLayers( const QList< QPair<QgsVectorLayer *, int > > &layers );
101 
108  int writeToFile( QIODevice *d, const QString &codec ); //maybe add progress dialog? other parameters (e.g. scale, dpi)?
109 
116  void setSymbologyScale( double scale ) { mSymbologyScale = scale; }
117 
124  double symbologyScale() const { return mSymbologyScale; }
125 
130  QgsUnitTypes::DistanceUnit mapUnits() const;
131 
137  void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
138 
144  QgsCoordinateReferenceSystem destinationCrs() const;
145 
150  void setSymbologyExport( QgsDxfExport::SymbologyExport e ) { mSymbologyExport = e; }
151 
157  QgsDxfExport::SymbologyExport symbologyExport() const { return mSymbologyExport; }
158 
163  void setExtent( const QgsRectangle &r ) { mExtent = r; }
164 
170  QgsRectangle extent() const { return mExtent; }
171 
178  void setLayerTitleAsName( bool layerTitleAsName ) { mLayerTitleAsName = layerTitleAsName; }
179 
185  bool layerTitleAsName() { return mLayerTitleAsName; }
186 
192  void setForce2d( bool force2d ) { mForce2d = force2d; }
193 
199  bool force2d() { return mForce2d; }
200 
205  static int closestColorMatch( QRgb color );
206 
213  QString layerName( const QString &id, const QgsFeature &f ) const;
214 
221  QString layerName( QgsVectorLayer *vl ) const;
222 
229  void writeGroup( int code, int i ) SIP_PYNAME( writeGroupInt );
230 
237  void writeGroup( int code, double d ) SIP_PYNAME( writeGroupDouble );
238 
244  void writeGroup( int code, const QString &s );
245 
253  void writeGroup( int code, const QgsPoint &p ) SIP_PYNAME( writeGroupPointV2 );
254 
263  void writeGroup( const QColor &color, int exactMatch = 62, int rgbCode = 420, int transparencyCode = 440 );
264 
269  void writeGroupCode( int code );
270 
275  void writeInt( int i );
276 
281  void writeDouble( double d );
282 
287  void writeString( const QString &s );
288 
295  int writeHandle( int code = 5, int handle = 0 );
296 
307  void writePolyline( const QgsPointSequence &line, const QString &layer, const QString &lineStyleName, const QColor &color, double width = -1 ) SIP_SKIP;
308 
318  void writePolygon( const QgsRingSequence &polygon, const QString &layer, const QString &hatchPattern, const QColor &color ) SIP_SKIP;
319 
324  void writeLine( const QgsPoint &pt1, const QgsPoint &pt2, const QString &layer, const QString &lineStyleName, const QColor &color, double width = -1 );
325 
331  void writePoint( const QString &layer, const QColor &color, const QgsPoint &pt ) SIP_PYNAME( writePointV2 );
332 
338  void writeFilledCircle( const QString &layer, const QColor &color, const QgsPoint &pt, double radius ) SIP_PYNAME( writeFillCircleV2 );
339 
345  void writeCircle( const QString &layer, const QColor &color, const QgsPoint &pt, double radius, const QString &lineStyleName, double width ) SIP_PYNAME( writeCircleV2 );
346 
352  void writeText( const QString &layer, const QString &text, const QgsPoint &pt, double size, double angle, const QColor &color ) SIP_PYNAME( writeTextV2 );
353 
359  void writeMText( const QString &layer, const QString &text, const QgsPoint &pt, double width, double angle, const QColor &color );
360 
365  static double mapUnitScaleFactor( double scale, QgsUnitTypes::RenderUnit symbolUnits, QgsUnitTypes::DistanceUnit mapUnits );
366 
368  static QString dxfLayerName( const QString &name );
369 
371  static QString dxfEncoding( const QString &name );
372 
374  static QStringList encodings();
375 
384  void drawLabel( const QString &layerId, QgsRenderContext &context, pal::LabelPosition *label, const QgsPalLayerSettings &settings ) SIP_SKIP;
385 
392  void registerDxfLayer( const QString &layerId, QgsFeatureId fid, const QString &layer );
393 
394  private:
396  QgsRectangle mExtent;
398  double mSymbologyScale = 1.0;
399  SymbologyExport mSymbologyExport = NoSymbology;
401  bool mLayerTitleAsName = false;
402 
403  QTextStream mTextStream;
404 
405  static int sDxfColors[][3];
406  static const char *DXF_ENCODINGS[][2];
407 
408  int mSymbolLayerCounter = 0; //internal counter
409  int mNextHandleId = DXF_HANDSEED;
410  int mBlockCounter = 0;
411 
412  QHash< const QgsSymbolLayer *, QString > mLineStyles; //symbol layer name types
413  QHash< const QgsSymbolLayer *, QString > mPointSymbolBlocks; //reference to point symbol blocks
414 
415  //AC1009
416  void writeHeader( const QString &codepage );
417  void writeTables();
418  void writeBlocks();
419  void writeEntities();
420  void writeEntitiesSymbolLevels( QgsVectorLayer *layer );
421  void writeEndFile();
422 
423  void startSection();
424  void endSection();
425 
426  void writePoint( const QgsPoint &pt, const QString &layer, const QColor &color, QgsSymbolRenderContext &ctx, const QgsSymbolLayer *symbolLayer, const QgsSymbol *symbol, double angle );
427  void writeDefaultLinetypes();
428  void writeSymbolLayerLinetype( const QgsSymbolLayer *symbolLayer );
429  void writeLinetype( const QString &styleName, const QVector<qreal> &pattern, QgsUnitTypes::RenderUnit u );
430 
431  void addFeature( QgsSymbolRenderContext &ctx, const QgsCoordinateTransform &ct, const QString &layer, const QgsSymbolLayer *symbolLayer, const QgsSymbol *symbol );
432 
433  //returns dxf palette index from symbol layer color
434  static QColor colorFromSymbolLayer( const QgsSymbolLayer *symbolLayer, QgsSymbolRenderContext &ctx );
435  QString lineStyleFromSymbolLayer( const QgsSymbolLayer *symbolLayer );
436 
437  //functions for dxf palette
438  static int color_distance( QRgb p1, int index );
439  static QRgb createRgbEntry( qreal r, qreal g, qreal b );
440 
441  //helper functions for symbology export
442  QgsRenderContext renderContext() const;
443 
444  QList< QPair< QgsSymbolLayer *, QgsSymbol * > > symbolLayers( QgsRenderContext &context );
445  static int nLineTypes( const QList< QPair< QgsSymbolLayer *, QgsSymbol *> > &symbolLayers );
446  static bool hasDataDefinedProperties( const QgsSymbolLayer *sl, const QgsSymbol *symbol );
447  double dashSize() const;
448  double dotSize() const;
449  double dashSeparatorSize() const;
450  double sizeToMapUnits( double s ) const;
451  static QString lineNameFromPenStyle( Qt::PenStyle style );
452  bool layerIsScaleBasedVisible( const QgsMapLayer *layer ) const;
453 
454  QHash<QString, int> mBlockHandles;
455  QString mBlockHandle;
456 
458  QMap< QString, QMap<QgsFeatureId, QString> > mDxfLayerNames;
460  QgsMapSettings mMapSettings;
461  QHash<QString, int> mLayerNameAttribute;
462  double mFactor = 1.0;
463  bool mForce2d = false;
464 
465  QgsDxfExport::Flags mFlags = 0;
466 
467 };
468 
469 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDxfExport::Flags )
470 
471 #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:124
QgsDxfExport::SymbologyExport symbologyExport() const
Get symbology export mode.
Definition: qgsdxfexport.h:157
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:199
void setSymbologyExport(QgsDxfExport::SymbologyExport e)
Set symbology export mode.
Definition: qgsdxfexport.h:150
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:192
Keeps the number of features and export symbology per feature (using the first symbol level) ...
Definition: qgsdxfexport.h:57
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:116
Contains information about the context of a rendering operation.
QgsRectangle extent() const
Get extent of area to export.
Definition: qgsdxfexport.h:170
Flag
Export flags.
Definition: qgsdxfexport.h:62
void setLayerTitleAsName(bool layerTitleAsName)
Enable use of title (where set) instead of layer name, when attribute index of corresponding layer in...
Definition: qgsdxfexport.h:178
void setExtent(const QgsRectangle &r)
Set extent of area to export.
Definition: qgsdxfexport.h:163
bool layerTitleAsName()
Retrieve whether layer title (where set) instead of name shall be use.
Definition: qgsdxfexport.h:185
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