QGIS API Documentation  2.6.0-Brighton
 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 ); //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 tranparencyCode = 440 );
78 
79  int writeHandle( int code = 5, int handle = 0 );
80 
81  //draw dxf primitives
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 
89  //write line (as a polyline)
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 writeCircle( const QString& layer, QColor color, const QgsPoint& pt, double radius );
95 
96  void writeText( const QString& layer, const QString& text, const QgsPoint& pt, double size, double angle, QColor color );
97 
98  void writeMText( const QString& layer, const QString& text, const QgsPoint& pt, double width, double angle, QColor color );
99 
100  static double mapUnitScaleFactor( double scaleDenominator, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits );
101 
102  static QString dxfLayerName( const QString& name );
103 
104  private:
105  QList< QPair<QgsVectorLayer*, int> > mLayers;
106 
108  QgsRectangle mExtent;
110  double mSymbologyScaleDenominator;
111  SymbologyExport mSymbologyExport;
112  QGis::UnitType mMapUnits;
113 
114  QTextStream mTextStream;
115 
116  static int mDxfColors[][3];
117 
118  int mSymbolLayerCounter; //internal counter
119  int mNextHandleId;
120  int mBlockCounter;
121 
122  QHash< const QgsSymbolLayerV2*, QString > mLineStyles; //symbol layer name types
123  QHash< const QgsSymbolLayerV2*, QString > mPointSymbolBlocks; //reference to point symbol blocks
124 
125  //AC1009
126  void writeHeader();
127  void writeTables();
128  void writeBlocks();
129  void writeEntities();
130  void writeEntitiesSymbolLevels( QgsVectorLayer* layer );
131  void writeEndFile();
132 
133  void startSection();
134  void endSection();
135 
136  void writePoint( const QgsPoint& pt, const QString& layer, QColor color, const QgsFeature* f, const QgsSymbolLayerV2* symbolLayer, const QgsSymbolV2* symbol );
137  void writeVertex( const QgsPoint& pt, const QString& layer );
138  void writeDefaultLinetypes();
139  void writeSymbolLayerLinetype( const QgsSymbolLayerV2* symbolLayer );
140  void writeLinetype( const QString& styleName, const QVector<qreal>& pattern, QgsSymbolV2::OutputUnit u );
141 
142 #if 0
143  //AC1018
144  void writeHeaderAC1018( QTextStream& stream );
145  void writeTablesAC1018( QTextStream& stream );
146  void writeEntitiesAC1018( QTextStream& stream );
147  void writeEntitiesSymbolLevelsAC1018( QTextStream& stream, QgsVectorLayer* layer );
148  void writeSymbolLayerLinetypeAC1018( QTextStream& stream, const QgsSymbolLayerV2* symbolLayer );
149  void writeLinetypeAC1018( QTextStream& stream, const QString& styleName, const QVector<qreal>& pattern, QgsSymbolV2::OutputUnit u );
150  void writeVertexAC1018( QTextStream& stream, const QgsPoint& pt );
151  void writePolylineAC1018( QTextStream& stream, const QgsPolyline& line, const QString& layer, const QString& lineStyleName, QColor color,
152  double width = -1, bool polygon = false );
153 #endif
154 
155  QgsRectangle dxfExtent() const;
156 
157  void addFeature( const QgsSymbolV2RenderContext& ctx, const QString& layer, const QgsSymbolLayerV2* symbolLayer, const QgsSymbolV2* symbol );
158 
159  //returns dxf palette index from symbol layer color
160  static QColor colorFromSymbolLayer( const QgsSymbolLayerV2* symbolLayer, const QgsSymbolV2RenderContext& ctx );
161  QString lineStyleFromSymbolLayer( const QgsSymbolLayerV2* symbolLayer );
162 
163  //functions for dxf palette
164  static int color_distance( QRgb p1, int index );
165  static QRgb createRgbEntry( qreal r, qreal g, qreal b );
166 
167  //helper functions for symbology export
168  QgsRenderContext renderContext() const;
169 
170  QList< QPair< QgsSymbolLayerV2*, QgsSymbolV2* > > symbolLayers();
171  static int nLineTypes( const QList< QPair< QgsSymbolLayerV2*, QgsSymbolV2*> >& symbolLayers );
172  static bool hasDataDefinedProperties( const QgsSymbolLayerV2* sl, const QgsSymbolV2* symbol );
173  double dashSize() const;
174  double dotSize() const;
175  double dashSeparatorSize() const;
176  double sizeToMapUnits( double s ) const;
177  static QString lineNameFromPenStyle( Qt::PenStyle style );
178  bool layerIsScaleBasedVisible( const QgsMapLayer* layer ) const;
179 
180  int mModelSpaceBR;
181 };
182 
183 #endif // QGSDXFEXPORT_H