QGIS API Documentation  2.99.0-Master (dcec6bb)
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 namespace pal SIP_SKIP
39 {
40  class LabelPosition;
41 }
42 
46 class CORE_EXPORT QgsDxfExport
47 {
48  public:
50  {
51  NoSymbology = 0, //export only data
52  FeatureSymbology, //Keeps the number of features and export symbology per feature (using the first symbol level)
53  SymbolLayerSymbology //Exports one feature per symbol layer (considering symbol levels)
54  };
55 
56  QgsDxfExport();
57  QgsDxfExport( const QgsDxfExport &dxfExport ) SIP_SKIP;
58  QgsDxfExport &operator=( const QgsDxfExport &dxfExport );
59 
64  void setMapSettings( const QgsMapSettings &settings );
65 
71  void addLayers( const QList< QPair<QgsVectorLayer *, int > > &layers );
72 
79  int writeToFile( QIODevice *d, const QString &codec ); //maybe add progress dialog? other parameters (e.g. scale, dpi)?
80 
87  void setSymbologyScale( double scale ) { mSymbologyScale = scale; }
88 
95  double symbologyScale() const { return mSymbologyScale; }
96 
101  QgsUnitTypes::DistanceUnit mapUnits() const;
102 
108  void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
109 
115  QgsCoordinateReferenceSystem destinationCrs() const;
116 
121  void setSymbologyExport( QgsDxfExport::SymbologyExport e ) { mSymbologyExport = e; }
122 
128  QgsDxfExport::SymbologyExport symbologyExport() const { return mSymbologyExport; }
129 
134  void setExtent( const QgsRectangle &r ) { mExtent = r; }
135 
141  QgsRectangle extent() const { return mExtent; }
142 
149  void setLayerTitleAsName( bool layerTitleAsName ) { mLayerTitleAsName = layerTitleAsName; }
150 
156  bool layerTitleAsName() { return mLayerTitleAsName; }
157 
162  static int closestColorMatch( QRgb color );
163 
170  QString layerName( const QString &id, const QgsFeature &f ) const;
171 
178  QString layerName( QgsVectorLayer *vl ) const;
179 
186  void writeGroup( int code, int i ) SIP_PYNAME( writeGroupInt );
187 
194  void writeGroup( int code, double d ) SIP_PYNAME( writeGroupDouble );
195 
201  void writeGroup( int code, const QString &s );
202 
210  void writeGroup( int code, const QgsPoint &p ) SIP_PYNAME( writeGroupPointV2 );
211 
220  void writeGroup( const QColor &color, int exactMatch = 62, int rgbCode = 420, int transparencyCode = 440 );
221 
226  void writeGroupCode( int code );
227 
232  void writeInt( int i );
233 
238  void writeDouble( double d );
239 
244  void writeString( const QString &s );
245 
252  int writeHandle( int code = 5, int handle = 0 );
253 
264  void writePolyline( const QgsPointSequence &line, const QString &layer, const QString &lineStyleName, const QColor &color, double width = -1 ) SIP_SKIP;
265 
275  void writePolygon( const QgsRingSequence &polygon, const QString &layer, const QString &hatchPattern, const QColor &color ) SIP_SKIP;
276 
279  void writeLine( const QgsPoint &pt1, const QgsPoint &pt2, const QString &layer, const QString &lineStyleName, const QColor &color, double width = -1 );
280 
284  void writePoint( const QString &layer, const QColor &color, const QgsPoint &pt ) SIP_PYNAME( writePointV2 );
285 
289  void writeFilledCircle( const QString &layer, const QColor &color, const QgsPoint &pt, double radius ) SIP_PYNAME( writeFillCircleV2 );
290 
294  void writeCircle( const QString &layer, const QColor &color, const QgsPoint &pt, double radius, const QString &lineStyleName, double width ) SIP_PYNAME( writeCircleV2 );
295 
299  void writeText( const QString &layer, const QString &text, const QgsPoint &pt, double size, double angle, const QColor &color ) SIP_PYNAME( writeTextV2 );
300 
304  void writeMText( const QString &layer, const QString &text, const QgsPoint &pt, double width, double angle, const QColor &color );
305 
310  static double mapUnitScaleFactor( double scale, QgsUnitTypes::RenderUnit symbolUnits, QgsUnitTypes::DistanceUnit mapUnits );
311 
313  static QString dxfLayerName( const QString &name );
314 
316  static QString dxfEncoding( const QString &name );
317 
319  static QStringList encodings();
320 
328  void drawLabel( const QString &layerId, QgsRenderContext &context, pal::LabelPosition *label, const QgsPalLayerSettings &settings ) SIP_SKIP;
329 
335  void registerDxfLayer( const QString &layerId, QgsFeatureId fid, const QString &layer );
336 
337  private:
339  QgsRectangle mExtent;
341  double mSymbologyScale;
342  SymbologyExport mSymbologyExport;
343  QgsUnitTypes::DistanceUnit mMapUnits;
344  bool mLayerTitleAsName;
345 
346  QTextStream mTextStream;
347 
348  static int sDxfColors[][3];
349  static const char *DXF_ENCODINGS[][2];
350 
351  int mSymbolLayerCounter; //internal counter
352  int mNextHandleId;
353  int mBlockCounter;
354 
355  QHash< const QgsSymbolLayer *, QString > mLineStyles; //symbol layer name types
356  QHash< const QgsSymbolLayer *, QString > mPointSymbolBlocks; //reference to point symbol blocks
357 
358  //AC1009
359  void writeHeader( const QString &codepage );
360  void writeTables();
361  void writeBlocks();
362  void writeEntities();
363  void writeEntitiesSymbolLevels( QgsVectorLayer *layer );
364  void writeEndFile();
365 
366  void startSection();
367  void endSection();
368 
369  void writePoint( const QgsPoint &pt, const QString &layer, const QColor &color, QgsSymbolRenderContext &ctx, const QgsSymbolLayer *symbolLayer, const QgsSymbol *symbol, double angle );
370  void writeDefaultLinetypes();
371  void writeSymbolLayerLinetype( const QgsSymbolLayer *symbolLayer );
372  void writeLinetype( const QString &styleName, const QVector<qreal> &pattern, QgsUnitTypes::RenderUnit u );
373 
374  void addFeature( QgsSymbolRenderContext &ctx, const QgsCoordinateTransform &ct, const QString &layer, const QgsSymbolLayer *symbolLayer, const QgsSymbol *symbol );
375 
376  //returns dxf palette index from symbol layer color
377  static QColor colorFromSymbolLayer( const QgsSymbolLayer *symbolLayer, QgsSymbolRenderContext &ctx );
378  QString lineStyleFromSymbolLayer( const QgsSymbolLayer *symbolLayer );
379 
380  //functions for dxf palette
381  static int color_distance( QRgb p1, int index );
382  static QRgb createRgbEntry( qreal r, qreal g, qreal b );
383 
384  //helper functions for symbology export
385  QgsRenderContext renderContext() const;
386 
387  QList< QPair< QgsSymbolLayer *, QgsSymbol * > > symbolLayers( QgsRenderContext &context );
388  static int nLineTypes( const QList< QPair< QgsSymbolLayer *, QgsSymbol *> > &symbolLayers );
389  static bool hasDataDefinedProperties( const QgsSymbolLayer *sl, const QgsSymbol *symbol );
390  double dashSize() const;
391  double dotSize() const;
392  double dashSeparatorSize() const;
393  double sizeToMapUnits( double s ) const;
394  static QString lineNameFromPenStyle( Qt::PenStyle style );
395  bool layerIsScaleBasedVisible( const QgsMapLayer *layer ) const;
396 
397  QHash<QString, int> mBlockHandles;
398  QString mBlockHandle;
399 
401  QMap< QString, QMap<QgsFeatureId, QString> > mDxfLayerNames;
403  QgsMapSettings mMapSettings;
404  QHash<QString, int> mLayerNameAttribute;
405  double mFactor;
406 };
407 
408 #endif // QGSDXFEXPORT_H
A rectangle specified with double values.
Definition: qgsrectangle.h:38
Base class for all map layer types.
Definition: qgsmaplayer.h:54
double symbologyScale() const
Returns the reference scale for output.
Definition: qgsdxfexport.h:95
QgsDxfExport::SymbologyExport symbologyExport() const
Get symbology export mode.
Definition: qgsdxfexport.h:128
A class to represent a 2D point.
Definition: qgspointxy.h:42
void setSymbologyExport(QgsDxfExport::SymbologyExport e)
Set symbology export mode.
Definition: qgsdxfexport.h:121
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:61
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.cc:857
The QgsMapSettings class contains configuration for rendering of the map.
#define SIP_SKIP
Definition: qgis_sip.h:119
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:36
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:42
void setSymbologyScale(double scale)
Set reference scale for output.
Definition: qgsdxfexport.h:87
Contains information about the context of a rendering operation.
QgsRectangle extent() const
Get extent of area to export.
Definition: qgsdxfexport.h:141
void setLayerTitleAsName(bool layerTitleAsName)
Enable use of title (where set) instead of layer name, when attribute index of corresponding layer in...
Definition: qgsdxfexport.h:149
void setExtent(const QgsRectangle &r)
Set extent of area to export.
Definition: qgsdxfexport.h:134
bool layerTitleAsName()
Retrieve whether layer title (where set) instead of name shall be use.
Definition: qgsdxfexport.h:156
This class represents a coordinate reference system (CRS).
QList< QgsPointSequence > QgsRingSequence
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
QList< QgsPoint > QgsPointSequence
Represents a vector layer which manages a vector based data sets.
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:95
#define SIP_PYNAME(name)
Definition: qgis_sip.h:74