QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 "qgsgeometry.h"
22 #include "qgssymbolv2.h"
23 #include "qgsmapsettings.h"
24 
25 #include <QColor>
26 #include <QList>
27 #include <QTextStream>
28 
29 class QgsMapLayer;
30 class QgsPoint;
31 class QgsSymbolLayerV2;
32 class QIODevice;
34 
35 namespace pal
36 {
37  class LabelPosition;
38 };
39 
43 class CORE_EXPORT QgsDxfExport
44 {
45  public:
46 
51  struct DxfLayer
52  {
53  DxfLayer( QgsVectorLayer *vl, int layerOutputAttributeIndex = -1 )
54  : mLayer( vl )
55  , mLayerOutputAttributeIndex( layerOutputAttributeIndex )
56  {}
57 
58  QgsVectorLayer *layer() const {return mLayer;}
59  int layerOutputAttributeIndex() const {return mLayerOutputAttributeIndex;}
60 
63  };
64 
66  {
67  NoSymbology = 0, //export only data
68  FeatureSymbology, //Keeps the number of features and export symbology per feature (using the first symbol level)
69  SymbolLayerSymbology //Exports one feature per symbol layer (considering symbol levels)
70  };
71 
72  QgsDxfExport();
73  QgsDxfExport( const QgsDxfExport &dxfExport );
74  ~QgsDxfExport();
75  QgsDxfExport &operator=( const QgsDxfExport &dxfExport );
76 
81  void setMapSettings( const QgsMapSettings &settings );
82 
88  void addLayers( const QList< QgsDxfExport::DxfLayer > &layers );
89 
96  int writeToFile( QIODevice *d, const QString& codec ); //maybe add progress dialog? other parameters (e.g. scale, dpi)?
97 
102  void setSymbologyScaleDenominator( double d ) { mSymbologyScaleDenominator = d; }
103 
109  double symbologyScaleDenominator() const { return mSymbologyScaleDenominator; }
110 
115  void setMapUnits( QGis::UnitType u ) { mMapUnits = u; }
116 
122  QGis::UnitType mapUnits() const { return mMapUnits; }
123 
127  void setDestinationCrs( long crs );
128 
132  long destinationCrs();
133 
138  void setSymbologyExport( SymbologyExport e ) { mSymbologyExport = e; }
139 
145  SymbologyExport symbologyExport() const { return mSymbologyExport; }
146 
151  void setExtent( const QgsRectangle &r ) { mExtent = r; }
152 
158  QgsRectangle extent() const { return mExtent; }
159 
166  void setLayerTitleAsName( bool layerTitleAsName ) { mLayerTitleAsName = layerTitleAsName; }
167 
173  bool layerTitleAsName() { return mLayerTitleAsName; }
174 
180  void setForce2d( bool force2d ) { mForce2d = force2d; }
181 
187  bool force2d() { return mForce2d; }
188 
193  static int closestColorMatch( QRgb color );
194 
201  QString layerName( const QString &id, const QgsFeature &f ) const;
202 
209  QString layerName( QgsVectorLayer *vl ) const;
210 
217  void writeGroup( int code, int i );
218 
225  void writeGroup( int code, double d );
226 
232  void writeGroup( int code, const QString &s );
233 
243  Q_DECL_DEPRECATED void writeGroup( int code, const QgsPoint &p, double z = 0.0, bool skipz = false );
244 
252  void writeGroup( int code, const QgsPointV2 &p );
253 
262  void writeGroup( const QColor& color, int exactMatch = 62, int rgbCode = 420, int transparencyCode = 440 );
263 
268  void writeGroupCode( int code );
269 
274  void writeInt( int i );
275 
280  void writeDouble( double d );
281 
286  void writeString( const QString &s );
287 
294  int writeHandle( int code = 5, int handle = 0 );
295 
305  Q_DECL_DEPRECATED void writePolyline( const QgsPolyline &line, const QString &layer, const QString &lineStyleName, const QColor& color, double width = -1 );
306 
317  void writePolyline( const QgsPointSequenceV2 &line, const QString &layer, const QString &lineStyleName, const QColor& color, double width = -1 );
318 
327  Q_DECL_DEPRECATED void writePolygon( const QgsPolygon &polygon, const QString &layer, const QString &hatchPattern, const QColor& color );
328 
338  void writePolygon( const QgsRingSequenceV2 &polygon, const QString &layer, const QString &hatchPattern, const QColor& color );
339 
350  Q_DECL_DEPRECATED void writeSolid( const QString &layer, const QColor& color, const QgsPoint &pt1, const QgsPoint &pt2, const QgsPoint &pt3, const QgsPoint &pt4 );
351 
354  Q_DECL_DEPRECATED void writeLine( const QgsPoint &pt1, const QgsPoint &pt2, const QString &layer, const QString &lineStyleName, const QColor& color, double width = -1 );
355 
358  void writeLine( const QgsPointV2 &pt1, const QgsPointV2 &pt2, const QString &layer, const QString &lineStyleName, const QColor& color, double width = -1 );
359 
362  Q_DECL_DEPRECATED void writePoint( const QString &layer, const QColor& color, const QgsPoint &pt );
363 
367  void writePoint( const QString &layer, const QColor& color, const QgsPointV2 &pt );
368 
371  Q_DECL_DEPRECATED void writeFilledCircle( const QString &layer, const QColor& color, const QgsPoint &pt, double radius );
372 
376  void writeFilledCircle( const QString &layer, const QColor& color, const QgsPointV2 &pt, double radius );
377 
380  Q_DECL_DEPRECATED void writeCircle( const QString &layer, const QColor& color, const QgsPoint &pt, double radius, const QString &lineStyleName, double width );
381 
385  void writeCircle( const QString &layer, const QColor& color, const QgsPointV2 &pt, double radius, const QString &lineStyleName, double width );
386 
389  Q_DECL_DEPRECATED void writeText( const QString &layer, const QString &text, const QgsPoint &pt, double size, double angle, const QColor& color );
390 
394  void writeText( const QString &layer, const QString &text, const QgsPointV2 &pt, double size, double angle, const QColor& color );
395 
398  Q_DECL_DEPRECATED void writeMText( const QString &layer, const QString &text, const QgsPoint &pt, double width, double angle, const QColor& color );
399 
403  void writeMText( const QString &layer, const QString &text, const QgsPointV2 &pt, double width, double angle, const QColor& color );
404 
410  static double mapUnitScaleFactor( double scaleDenominator, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits, double mapUnitsPerPixel = 1.0 );
415  void clipValueToMapUnitScale( double& value, const QgsMapUnitScale& scale, double pixelToMMFactor ) const;
416 
418  static QString dxfLayerName( const QString &name );
419 
421  static QString dxfEncoding( const QString &name );
422 
424  static QStringList encodings();
425 
433  void drawLabel( QString layerId, QgsRenderContext& context, pal::LabelPosition* label, const QgsPalLayerSettings &settings );
434 
440  void registerDxfLayer( QString layerId, QgsFeatureId fid, QString layer );
441 
442  private:
444  QgsRectangle mExtent;
446  double mSymbologyScaleDenominator;
447  SymbologyExport mSymbologyExport;
448  QGis::UnitType mMapUnits;
449  bool mLayerTitleAsName;
450 
451  QTextStream mTextStream;
452 
453  static int mDxfColors[][3];
454  static const char *mDxfEncodings[][2];
455 
456  int mSymbolLayerCounter; //internal counter
457  int mNextHandleId;
458  int mBlockCounter;
459 
460  QHash< const QgsSymbolLayerV2*, QString > mLineStyles; //symbol layer name types
461  QHash< const QgsSymbolLayerV2*, QString > mPointSymbolBlocks; //reference to point symbol blocks
462 
463  //AC1009
464  void writeHeader( const QString& codepage );
465  void writeTables();
466  void writeBlocks();
467  void writeEntities();
468  void writeEntitiesSymbolLevels( QgsVectorLayer *layer );
469  void writeEndFile();
470 
471  void startSection();
472  void endSection();
473 
474  void writePoint( const QgsPointV2 &pt, const QString &layer, const QColor& color, QgsSymbolV2RenderContext &ctx, const QgsSymbolLayerV2 *symbolLayer, const QgsSymbolV2 *symbol, double angle );
475  void writeDefaultLinetypes();
476  void writeSymbolLayerLinetype( const QgsSymbolLayerV2 *symbolLayer );
477  void writeLinetype( const QString &styleName, const QVector<qreal> &pattern, QgsSymbolV2::OutputUnit u );
478 
479  void addFeature( QgsSymbolV2RenderContext &ctx, const QgsCoordinateTransform *ct, const QString &layer, const QgsSymbolLayerV2 *symbolLayer, const QgsSymbolV2 *symbol );
486  void addGeometryGeneratorSymbolLayer( QgsSymbolV2RenderContext &ctx, const QgsCoordinateTransform *ct, const QString &layer, QgsSymbolLayerV2 *symbolLayer, bool allSymbolLayers );
487 
488  //returns dxf palette index from symbol layer color
489  static QColor colorFromSymbolLayer( const QgsSymbolLayerV2 *symbolLayer, QgsSymbolV2RenderContext &ctx );
490  QString lineStyleFromSymbolLayer( const QgsSymbolLayerV2 *symbolLayer );
491 
492  //functions for dxf palette
493  static int color_distance( QRgb p1, int index );
494  static QRgb createRgbEntry( qreal r, qreal g, qreal b );
495 
496  //helper functions for symbology export
497  QgsRenderContext renderContext() const;
498 
500  static int nLineTypes( const QList< QPair< QgsSymbolLayerV2*, QgsSymbolV2*> > &symbolLayers );
501  static bool hasDataDefinedProperties( const QgsSymbolLayerV2 *sl, const QgsSymbolV2 *symbol );
502  double dashSize() const;
503  double dotSize() const;
504  double dashSeparatorSize() const;
505  double sizeToMapUnits( double s ) const;
506  static QString lineNameFromPenStyle( Qt::PenStyle style );
507  bool layerIsScaleBasedVisible( const QgsMapLayer *layer ) const;
508 
509  QHash<QString, int> mBlockHandles;
510  QString mBlockHandle;
511 
514  long mCrs;
515  QgsMapSettings mMapSettings;
516  QHash<QString, int> mLayerNameAttribute;
517  double mFactor;
518  bool mForce2d;
519 };
520 
521 #endif // QGSDXFEXPORT_H
static unsigned index
A rectangle specified with double values.
Definition: qgsrectangle.h:35
Base class for all map layer types.
Definition: qgsmaplayer.h:49
void setSymbologyScaleDenominator(double d)
Set reference scale for output.
Definition: qgsdxfexport.h:102
OutputUnit
The unit of the output.
Definition: qgssymbolv2.h:65
DxfLayer(QgsVectorLayer *vl, int layerOutputAttributeIndex=-1)
Definition: qgsdxfexport.h:53
SymbologyExport symbologyExport() const
Get symbology export mode.
Definition: qgsdxfexport.h:145
bool force2d()
Retrieve whether the output should be forced to 2d.
Definition: qgsdxfexport.h:187
void setSymbologyExport(SymbologyExport e)
Set symbology export mode.
Definition: qgsdxfexport.h:138
QgsVectorLayer * mLayer
Definition: qgsdxfexport.h:61
QGis::UnitType mapUnits() const
Retrieve map units.
Definition: qgsdxfexport.h:122
QgsVectorLayer * layer() const
Definition: qgsdxfexport.h:58
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:187
Layers and optional attribute index to split into multiple layers using attribute value as layer name...
Definition: qgsdxfexport.h:51
double symbologyScaleDenominator() const
Retrieve reference scale for output.
Definition: qgsdxfexport.h:109
The QgsMapSettings class contains configuration for rendering of the map.
void setForce2d(bool force2d)
Force 2d output (eg.
Definition: qgsdxfexport.h:180
Point geometry type, with support for z-dimension and m-values.
Definition: qgspointv2.h:34
A class to represent a point.
Definition: qgspoint.h:117
double ANALYSIS_EXPORT angle(Point3D *p1, Point3D *p2, Point3D *p3, Point3D *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
Contains information about the context of a rendering operation.
QgsRectangle extent() const
Get extent of area to export.
Definition: qgsdxfexport.h:158
void setLayerTitleAsName(bool layerTitleAsName)
Enable use of title (where set) instead of layer name, when attribute index of corresponding layer in...
Definition: qgsdxfexport.h:166
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:151
void setMapUnits(QGis::UnitType u)
Set map units.
Definition: qgsdxfexport.h:115
bool layerTitleAsName()
Retrieve wether layer title (where set) instead of name shall be use.
Definition: qgsdxfexport.h:173
Class for doing transforms between two map coordinate systems.
LabelPosition is a candidate feature label position.
Definition: labelposition.h:51
UnitType
Map units that qgis supports.
Definition: qgis.h:159
qint64 QgsFeatureId
Definition: qgsfeature.h:31
Represents a vector layer which manages a vector based data sets.
int layerOutputAttributeIndex() const
Definition: qgsdxfexport.h:59