QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 <QColor>
24 #include <QList>
25 #include <QTextStream>
26 
27 class QgsMapLayer;
28 class QgsPoint;
29 class QgsSymbolLayerV2;
30 class QIODevice;
31 
32 class CORE_EXPORT QgsDxfExport
33 {
34  public:
36  {
37  NoSymbology = 0, //export only data
38  FeatureSymbology, //Keeps the number of features and export symbology per feature (using the first symbol level)
39  SymbolLayerSymbology //Exports one feature per symbol layer (considering symbol levels)
40  };
41 
42  QgsDxfExport();
43  QgsDxfExport( const QgsDxfExport &dxfExport );
44  ~QgsDxfExport();
45  QgsDxfExport &operator=( const QgsDxfExport &dxfExport );
46 
47  void addLayers( const QList< QPair<QgsVectorLayer *, int > > &layers );
48  int writeToFile( QIODevice *d, QString codec ); //maybe add progress dialog? other parameters (e.g. scale, dpi)?
49 
50  void setSymbologyScaleDenominator( double d ) { mSymbologyScaleDenominator = d; }
51  double symbologyScaleDenominator() const { return mSymbologyScaleDenominator; }
52 
53  void setMapUnits( QGis::UnitType u ) { mMapUnits = u; }
54  QGis::UnitType mapUnits() const { return mMapUnits; }
55 
56  void setSymbologyExport( SymbologyExport e ) { mSymbologyExport = e; }
57  SymbologyExport symbologyExport() const { return mSymbologyExport; }
58 
59  void setExtent( const QgsRectangle &r ) { mExtent = r; }
60  QgsRectangle extent() const { return mExtent; }
61 
62  //get closest entry in dxf palette
63  static int closestColorMatch( QRgb pixel );
64 
65  QString layerName( const QString &id, const QgsFeature &f ) const;
66 
68  void writeGroup( int code, int i );
70  void writeGroup( int code, double d );
71  void writeGroup( int code, const QString &s );
72  void writeGroupCode( int code );
73  void writeInt( int i );
74  void writeDouble( double d );
75  void writeString( const QString &s );
76  void writeGroup( int code, const QgsPoint &p, double z = 0.0, bool skipz = false );
77  void writeGroup( QColor color, int exactMatch = 62, int rgbCode = 420, int transparencyCode = 440 );
78 
79  int writeHandle( int code = 5, int handle = 0 );
80 
82  void writePolyline( const QgsPolyline &line, const QString &layer, const QString &lineStyleName, QColor color,
83  double width = -1, bool polygon = false );
84 
85  void writePolygon( const QgsPolygon &polygon, const QString &layer, const QString &hatchPattern, QColor color );
86 
87  void writeSolid( const QString &layer, QColor color, const QgsPoint &pt1, const QgsPoint &pt2, const QgsPoint &pt3, const QgsPoint &pt4 );
88 
90  void writeLine( const QgsPoint &pt1, const QgsPoint &pt2, const QString &layer, const QString &lineStyleName, QColor color, double width = -1 );
91 
92  void writePoint( const QString &layer, QColor color, const QgsPoint &pt );
93 
94  void writeFilledCircle( const QString &layer, QColor color, const QgsPoint &pt, double radius );
95 
96  void writeCircle( const QString &layer, QColor color, const QgsPoint &pt, double radius, const QString &lineStyleName, double width );
97 
98  void writeText( const QString &layer, const QString &text, const QgsPoint &pt, double size, double angle, QColor color );
99 
100  void writeMText( const QString &layer, const QString &text, const QgsPoint &pt, double width, double angle, QColor color );
101 
102  static double mapUnitScaleFactor( double scaleDenominator, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits );
103 
105  static QString dxfLayerName( const QString &name );
106 
108  static QString dxfEncoding( const QString &name );
109 
111  static QStringList encodings();
112 
113  private:
114  QList< QPair<QgsVectorLayer*, int> > mLayers;
115 
117  QgsRectangle mExtent;
119  double mSymbologyScaleDenominator;
120  SymbologyExport mSymbologyExport;
121  QGis::UnitType mMapUnits;
122 
123  QTextStream mTextStream;
124 
125  static int mDxfColors[][3];
126  static const char *mDxfEncodings[][2];
127 
128  int mSymbolLayerCounter; //internal counter
129  int mNextHandleId;
130  int mBlockCounter;
131 
132  QHash< const QgsSymbolLayerV2*, QString > mLineStyles; //symbol layer name types
133  QHash< const QgsSymbolLayerV2*, QString > mPointSymbolBlocks; //reference to point symbol blocks
134 
135  //AC1009
136  void writeHeader( QString codepage );
137  void writeTables();
138  void writeBlocks();
139  void writeEntities();
140  void writeEntitiesSymbolLevels( QgsVectorLayer *layer );
141  void writeEndFile();
142 
143  void startSection();
144  void endSection();
145 
146  void writePoint( const QgsPoint &pt, const QString &layer, QColor color, const QgsFeature *f, const QgsSymbolLayerV2 *symbolLayer, const QgsSymbolV2 *symbol );
147  void writeVertex( const QgsPoint &pt, const QString &layer );
148  void writeDefaultLinetypes();
149  void writeSymbolLayerLinetype( const QgsSymbolLayerV2 *symbolLayer );
150  void writeLinetype( const QString &styleName, const QVector<qreal> &pattern, QgsSymbolV2::OutputUnit u );
151 
152  QgsRectangle dxfExtent() const;
153 
154  void addFeature( const QgsSymbolV2RenderContext &ctx, const QString &layer, const QgsSymbolLayerV2 *symbolLayer, const QgsSymbolV2 *symbol );
155 
156  //returns dxf palette index from symbol layer color
157  static QColor colorFromSymbolLayer( const QgsSymbolLayerV2 *symbolLayer, const QgsSymbolV2RenderContext &ctx );
158  QString lineStyleFromSymbolLayer( const QgsSymbolLayerV2 *symbolLayer );
159 
160  //functions for dxf palette
161  static int color_distance( QRgb p1, int index );
162  static QRgb createRgbEntry( qreal r, qreal g, qreal b );
163 
164  //helper functions for symbology export
165  QgsRenderContext renderContext() const;
166 
167  QList< QPair< QgsSymbolLayerV2 *, QgsSymbolV2 * > > symbolLayers();
168  static int nLineTypes( const QList< QPair< QgsSymbolLayerV2*, QgsSymbolV2*> > &symbolLayers );
169  static bool hasDataDefinedProperties( const QgsSymbolLayerV2 *sl, const QgsSymbolV2 *symbol );
170  double dashSize() const;
171  double dotSize() const;
172  double dashSeparatorSize() const;
173  double sizeToMapUnits( double s ) const;
174  static QString lineNameFromPenStyle( Qt::PenStyle style );
175  bool layerIsScaleBasedVisible( const QgsMapLayer *layer ) const;
176 
177  int mModelSpaceBR;
178 };
179 
180 #endif // QGSDXFEXPORT_H