QGIS API Documentation 4.1.0-Master (ca2ac17535b)
Loading...
Searching...
No Matches
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.h"
22#include "qgis_core.h"
23#include "qgis_sip.h"
24#include "qgsgeometry.h"
25#include "qgslabelsink.h"
26#include "qgsmapsettings.h"
27#include "qgsrendercontext.h"
28
29#include <QColor>
30#include <QList>
31#include <QTextStream>
32
33class QgsMapLayer;
34class QgsPointXY;
35class QgsSymbolLayer;
36class QIODevice;
38class QgsCurve;
39class QgsCurvePolygon;
42struct DxfLayerJob;
45
46#define DXF_HANDSEED 100
47#define DXF_HANDMAX 9999999
48#define DXF_HANDPLOTSTYLE 0xf
49
50namespace pal // SIP_SKIP
51{
52 class LabelPosition;
53}
54
55
61#ifdef SIP_RUN
62class CORE_EXPORT QgsDxfExport
63{
64#else
65
66static const bool DEFAULT_DXF_DATA_DEFINED_BLOCKS = true;
67
68class CORE_EXPORT QgsDxfExport : public QgsLabelSink
69{
70#endif
71 friend class TestQgsDxfExport;
72
73 public:
77 struct CORE_EXPORT DxfLayer
78 {
88 DxfLayer( QgsVectorLayer *vl, int layerOutputAttributeIndex = -1, bool buildDDBlocks = DEFAULT_DXF_DATA_DEFINED_BLOCKS, int ddBlocksMaxNumberOfClasses = -1, QString overriddenName = QString() )
89 : mLayer( vl )
90 , mLayerOutputAttributeIndex( layerOutputAttributeIndex )
91 , mBuildDDBlocks( buildDDBlocks )
92 , mDDBlocksMaxNumberOfClasses( ddBlocksMaxNumberOfClasses )
93 , mOverriddenName( overriddenName )
94 {}
95
97 QgsVectorLayer *layer() const { return mLayer; }
98
106 int layerOutputAttributeIndex() const { return mLayerOutputAttributeIndex; }
107
116 QString splitLayerAttribute() const;
117
124 bool buildDataDefinedBlocks() const { return mBuildDDBlocks; }
125
134 int dataDefinedBlocksMaximumNumberOfClasses() const { return mDDBlocksMaxNumberOfClasses; }
135
143 QString overriddenName() const { return mOverriddenName; }
144
145 private:
146 QgsVectorLayer *mLayer = nullptr;
147 int mLayerOutputAttributeIndex = -1;
148
152 bool mBuildDDBlocks = DEFAULT_DXF_DATA_DEFINED_BLOCKS;
153
157 int mDDBlocksMaxNumberOfClasses = -1;
158
162 QString mOverriddenName;
163 };
164
166 enum Flag SIP_ENUM_BASETYPE( IntFlag )
167 {
168 FlagNoMText = 1 << 1,
171 };
172 Q_DECLARE_FLAGS( Flags, Flag )
173
174
179 enum class ExportResult
180 {
181 Success = 0,
182 InvalidDeviceError,
183 DeviceNotWritableError,
184 EmptyExtentError
185 };
186
190 enum class VAlign : int
191 {
192 VBaseLine = 0,
193 VBottom = 1,
194 VMiddle = 2,
195 VTop = 3,
196 Undefined = 9999
197 };
198
200 enum class HAlign : int
201 {
202 HLeft = 0,
203 HCenter = 1,
204 HRight = 2,
205 HAligned = 3,
206 HMiddle = 4,
207 HFit = 5,
208 Undefined = 9999
209 };
210
227
229
230
234
235 ~QgsDxfExport() override;
236
241 void setMapSettings( const QgsMapSettings &settings );
242
248
253 QgsDxfExport::Flags flags() const;
254
260 void addLayers( const QList< QgsDxfExport::DxfLayer > &layers );
261
268 ExportResult writeToFile( QIODevice *d, const QString &codec ); //maybe add progress dialog? other parameters (e.g. scale, dpi)?
269
274 const QString feedbackMessage() const { return mFeedbackMessage; }
275
281 void setSymbologyScale( double scale ) { mSymbologyScale = scale; }
282
288 double symbologyScale() const { return mSymbologyScale; }
289
294 Qgis::DistanceUnit mapUnits() const;
295
300 void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
301
306 QgsCoordinateReferenceSystem destinationCrs() const;
307
312 void setSymbologyExport( Qgis::FeatureSymbologyExport e ) { mSymbologyExport = e; }
313
319 Qgis::FeatureSymbologyExport symbologyExport() const { return mSymbologyExport; }
320
325 void setExtent( const QgsRectangle &r ) { mExtent = r; }
326
332 QgsRectangle extent() const { return mExtent; }
333
340 void setLayerTitleAsName( bool layerTitleAsName ) { mLayerTitleAsName = layerTitleAsName; }
341
347 bool layerTitleAsName() { return mLayerTitleAsName; }
348
354 void setForce2d( bool force2d ) { mForce2d = force2d; }
355
361 bool force2d() { return mForce2d; }
362
367 static int closestColorMatch( QRgb color );
368
375 QString layerName( const QString &id, const QgsFeature &f ) const;
376
383 QString layerName( QgsVectorLayer *vl ) const;
384
391 void writeGroup( int code, int i ) SIP_PYNAME( writeGroupInt );
392
399 void writeGroup( int code, long long i ) SIP_PYNAME( writeGroupLong );
400
407 void writeGroup( int code, double d ) SIP_PYNAME( writeGroupDouble );
408
414 void writeGroup( int code, const QString &s );
415
422 void writeGroup( int code, const QgsPoint &p ) SIP_PYNAME( writeGroupPointV2 );
423
432 void writeGroup( const QColor &color, int exactMatch = 62, int rgbCode = 420, int transparencyCode = 440 );
433
438 void writeGroupCode( int code );
439
444 void writeInt( int i );
445
450 void writeDouble( double d );
451
456 void writeString( const QString &s );
457
464 int writeHandle( int code = 5, int handle = 0 );
465
475 void writePolyline( const QgsPointSequence &line, const QString &layer, const QString &lineStyleName, const QColor &color, double width = -1 ) SIP_SKIP;
476
487 void writePolyline( const QgsCurve &curve, const QString &layer, const QString &lineStyleName, const QColor &color, double width = -1 ) SIP_SKIP;
488
497 void writePolygon( const QgsRingSequence &polygon, const QString &layer, const QString &hatchPattern, const QColor &color ) SIP_SKIP;
498
508 void writePolygon( const QgsCurvePolygon &polygon, const QString &layer, const QString &hatchPattern, const QColor &color ) SIP_SKIP;
509
513 void writeLine( const QgsPoint &pt1, const QgsPoint &pt2, const QString &layer, const QString &lineStyleName, const QColor &color, double width = -1 );
514
519 void writePoint( const QString &layer, const QColor &color, const QgsPoint &pt ) SIP_PYNAME( writePointV2 );
520
525 void writeFilledCircle( const QString &layer, const QColor &color, const QgsPoint &pt, double radius ) SIP_PYNAME( writeFillCircleV2 );
526
531 void writeCircle( const QString &layer, const QColor &color, const QgsPoint &pt, double radius, const QString &lineStyleName, double width ) SIP_PYNAME( writeCircleV2 );
532
537 void writeText(
538 const QString &layer,
539 const QString &text,
540 const QgsPoint &pt,
541 double size,
542 double angle,
543 const QColor &color,
544 QgsDxfExport::HAlign hali = QgsDxfExport::HAlign::Undefined,
545 QgsDxfExport::VAlign vali = QgsDxfExport::VAlign::Undefined
546 ) SIP_PYNAME( writeTextV2 );
547
552 void writeMText( const QString &layer, const QString &text, const QgsPoint &pt, double width, double angle, const QColor &color );
553
562 Q_DECL_DEPRECATED static double mapUnitScaleFactor( double scale, Qgis::RenderUnit symbolUnits, Qgis::DistanceUnit mapUnits, double mapUnitsPerPixel = 1.0 ) SIP_DEPRECATED;
563
569 static double mapUnitScaleFactor( const QgsRenderContext &renderContext, Qgis::RenderUnit symbolUnits );
570
577 void clipValueToMapUnitScale( double &value, const QgsMapUnitScale &scale, double pixelToMMFactor ) const;
578
580 static QString dxfLayerName( const QString &name );
581
583 static QString dxfEncoding( const QString &name );
584
586 static QStringList encodings();
587
593 void drawLabel( const QString &layerId, QgsRenderContext &context, pal::LabelPosition *label, const QgsPalLayerSettings &settings ) SIP_SKIP override;
594
603 Q_DECL_DEPRECATED void registerDxfLayer( const QString &layerId, QgsFeatureId fid, const QString &layer );
604
605 private:
606#ifdef SIP_RUN
607 QgsDxfExport( const QgsDxfExport &other );
608 QgsDxfExport &operator=( const QgsDxfExport & );
609#endif
610
611 struct DataDefinedBlockInfo
612 {
613 QString blockName;
614 double angle;
615 double size;
616 QgsFeature feature; //a feature representing the attribute combination (without geometry)
617 };
618
620 QgsRectangle mExtent;
622 double mSymbologyScale = 1.0;
625 bool mLayerTitleAsName = false;
626
627 QTextStream mTextStream;
628
629 int mSymbolLayerCounter = 0; //internal counter
630 int mNextHandleId = DXF_HANDSEED;
631 int mBlockCounter = 0;
632
633 QHash< const QgsSymbolLayer *, QString > mLineStyles; //symbol layer name types
634 QHash< const QgsSymbolLayer *, QString > mPointSymbolBlocks; //reference to point symbol blocks
635 QHash< const QgsSymbolLayer *, double > mPointSymbolBlockSizes; //reference to point symbol size used to create its block
636 QHash< const QgsSymbolLayer *, double > mPointSymbolBlockAngles; //reference to point symbol size used to create its block
638 QHash< const QgsSymbolLayer *, QHash<uint, DataDefinedBlockInfo> > mDataDefinedBlockInfo; // symbolLayerName -> symbolHash/Feature
639
640 //AC1009
641 void createDDBlockInfo();
642 void writeHeader( const QString &codepage );
643 void prepareRenderers();
644 void writeTables();
645 void writeBlocks();
646 void writeEntities();
647 void writeEntitiesSymbolLevels( DxfLayerJob *job );
648 void stopRenderers();
649 void writeEndFile();
650
651 void startSection();
652 void endSection();
653
654 void writePoint( const QgsPoint &pt, const QString &layer, const QColor &color, QgsSymbolRenderContext &ctx, const QgsSymbolLayer *symbolLayer, const QgsSymbol *symbol, double angle );
655 void writeDefaultLinetypes();
656 void writeSymbolLayerLinetype( const QgsSymbolLayer *symbolLayer );
657 void writeLinetype( const QString &styleName, const QVector<qreal> &pattern, Qgis::RenderUnit u );
658
662 void writeText( const QString &layer, const QString &text, pal::LabelPosition *label, const QgsPalLayerSettings &layerSettings, const QgsExpressionContext &expressionContext );
663
673 void addGeometryGeneratorSymbolLayer( QgsSymbolRenderContext &context, const QgsCoordinateTransform &ct, const QString &layer, QgsSymbolLayer *symbolLayer, bool allSymbolLayers );
674
684 void addFeature( QgsSymbolRenderContext &context, const QgsCoordinateTransform &ct, const QString &layer, const QgsSymbolLayer *symbolLayer, const QgsSymbol *symbol );
685
686 //returns dxf palette index from symbol layer color
687 static QColor colorFromSymbolLayer( const QgsSymbolLayer *symbolLayer, QgsSymbolRenderContext &ctx );
688 QString lineStyleFromSymbolLayer( const QgsSymbolLayer *symbolLayer );
689
690 //functions for dxf palette
691 static int color_distance( QRgb p1, int index );
692 static QRgb createRgbEntry( qreal r, qreal g, qreal b );
693
694 //helper functions for symbology export
695 QgsRenderContext renderContext() const;
696
697 QList< QPair< QgsSymbolLayer *, QgsSymbol * > > symbolLayers( QgsRenderContext &context );
698 static int nLineTypes( const QList< QPair< QgsSymbolLayer *, QgsSymbol *> > &symbolLayers );
699 static bool hasBlockBreakingDataDefinedProperties( const QgsSymbolLayer *sl, const QgsSymbol *symbol );
701 static bool isSymbolLayerEnabled( const QgsSymbolLayer *layer, QgsSymbolRenderContext &context );
702 void writeSymbolTableBlockRef( const QString &blockName );
703 void writeSymbolLayerBlock( const QString &blockName, const QgsMarkerSymbolLayer *ml, QgsSymbolRenderContext &ctx );
704 void writePointBlockReference(
705 const QgsPoint &pt, const QgsSymbolLayer *symbolLayer, QgsSymbolRenderContext &ctx, const QString &layer, double angle, const QString &blockName, double blockAngle, double blockSize
706 );
707 static uint dataDefinedSymbolClassHash( const QgsFeature &fet, const QgsPropertyCollection &prop, const QgsExpressionContext &context );
708
709 double dashSize() const;
710 double dotSize() const;
711 double dashSeparatorSize() const;
712 double sizeToMapUnits( double s ) const;
713 static QString lineNameFromPenStyle( Qt::PenStyle style );
714 bool layerIsScaleBasedVisible( const QgsMapLayer *layer ) const;
715
716 QHash<QString, int> mBlockHandles;
717 QString mBlockHandle;
718
720 QMap< QString, QMap<QgsFeatureId, QString> > mDxfLayerNames;
721 QgsCoordinateReferenceSystem mCrs;
722 QgsMapSettings mMapSettings;
723 QList<QgsMapLayer *> mLayerList;
724 QHash<QString, int> mLayerNameAttribute;
725 QHash<QString, int> mLayerDDBlockMaxNumberOfClasses;
726 QHash<QString, QString> mLayerOverriddenName;
727 double mFactor = 1.0;
728 bool mForce2d = false;
729
731
732 void appendCurve( const QgsCurve &c, QVector<QgsPoint> &points, QVector<double> &bulges );
733 void appendLineString( const QgsLineString &ls, QVector<QgsPoint> &points, QVector<double> &bulges );
734 void appendCircularString( const QgsCircularString &cs, QVector<QgsPoint> &points, QVector<double> &bulges );
735 void appendCompoundCurve( const QgsCompoundCurve &cc, QVector<QgsPoint> &points, QVector<double> &bulges );
736
737 QgsRenderContext mRenderContext;
738 // Internal cache for layer related information required during rendering
739 QList<DxfLayerJob *> mJobs;
740 std::unique_ptr<QgsLabelingEngine> mLabelingEngine;
741
742 QString mFeedbackMessage;
743};
744
747
748#endif // QGSDXFEXPORT_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:62
DistanceUnit
Units of distance.
Definition qgis.h:5396
@ Meters
Meters.
Definition qgis.h:5397
RenderUnit
Rendering size units.
Definition qgis.h:5566
FeatureSymbologyExport
Options for exporting features considering their symbology.
Definition qgis.h:6139
@ NoSymbology
Export only data.
Definition qgis.h:6140
Circular string geometry type.
Compound curve geometry type.
Represents a coordinate reference system (CRS).
Curve polygon geometry type.
Abstract base class for curved geometry type.
Definition qgscurve.h:36
Exports QGIS layers to the DXF format.
void setForce2d(bool force2d)
Force 2d output (eg.
ExportResult
The result of an export as dxf operation.
ExportResult writeToFile(QIODevice *d, const QString &codec)
Export to a dxf file in the given encoding.
Qgis::FeatureSymbologyExport symbologyExport() const
Gets symbology export mode.
bool layerTitleAsName()
Retrieve whether layer title (where set) instead of name shall be use.
void setFlags(QgsDxfExport::Flags flags)
Sets the export flags.
QgsRectangle extent() const
Gets extent of area to export.
Flag
Export flags.
@ FlagOnlySelectedFeatures
Use only selected features for the export.
@ FlagNoMText
Export text as TEXT elements. If not set, text will be exported as MTEXT elements.
QgsDxfExport()
Constructor for QgsDxfExport.
HAlign
Horizontal alignments.
void addLayers(const QList< QgsDxfExport::DxfLayer > &layers)
Add layers to export.
QFlags< Flag > Flags
friend class TestQgsDxfExport
void setSymbologyScale(double scale)
Set reference scale for output.
bool force2d()
Retrieve whether the output should be forced to 2d.
const QString feedbackMessage() const
Returns any feedback message produced while export to dxf file.
QFlags< DxfPolylineFlag > DxfPolylineFlags
void setExtent(const QgsRectangle &r)
Set extent of area to export.
QgsDxfExport::Flags flags() const
Returns the export flags.
double symbologyScale() const
Returns the reference scale for output.
VAlign
Vertical alignments.
void setSymbologyExport(Qgis::FeatureSymbologyExport e)
Set symbology export mode.
DxfPolylineFlag
Flags for polylines.
@ PolygonMesh
The polygon mesh is closed in the N direction.
@ Is3DPolyline
< Spline-fit vertices have been added
@ ContinuousPattern
The linetype pattern is generated continuously around the vertices of this polyline.
@ Closed
This is a closed polyline (or a polygon mesh closed in the M direction).
@ Is3DPolygonMesh
This is a 3D polygon mesh.
@ Curve
Curve-fit vertices have been added.
@ PolyfaceMesh
The polyline is a polyface mesh.
void setMapSettings(const QgsMapSettings &settings)
Set map settings and assign layer name attributes.
void setLayerTitleAsName(bool layerTitleAsName)
Enable use of title (where set) instead of layer name, when attribute index of corresponding layer in...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
Abstract base class that can be used to intercept rendered labels from a labeling / rendering job.
Base class for all map layer types.
Definition qgsmaplayer.h:83
Contains configuration for rendering maps.
Struct for storing maximum and minimum scales for measurements in map units.
Abstract base class for marker symbol layers.
Contains settings for how a map layer will be labeled.
Represents a 2D point.
Definition qgspointxy.h:62
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:53
A rectangle specified with double values.
Contains information about the context of a rendering operation.
Abstract base class for symbol layers.
Encapsulates the context in which a symbol is being rendered.
Represents a vector layer which manages a vector based dataset.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
#define SIP_DEPRECATED
Definition qgis_sip.h:113
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:272
#define SIP_SKIP
Definition qgis_sip.h:133
#define SIP_PYNAME(name)
Definition qgis_sip.h:88
QVector< QgsPointSequence > QgsRingSequence
QVector< QgsPoint > QgsPointSequence
#define DXF_HANDSEED
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsProjectionSelectionWidget::CrsOptions)
Holds information about each layer in a DXF job.
Encapsulates the properties of a vector layer containing features that will be exported to the DXF fi...
QString overriddenName() const
Returns the overridden layer name to be used in the exported DXF.
bool buildDataDefinedBlocks() const
Returns true if data defined point block symbols should be created.
QgsVectorLayer * layer() const
Returns the source vector layer.
int dataDefinedBlocksMaximumNumberOfClasses() const
Returns the maximum number of data defined symbol classes for which blocks are created.
DxfLayer(QgsVectorLayer *vl, int layerOutputAttributeIndex=-1, bool buildDDBlocks=DEFAULT_DXF_DATA_DEFINED_BLOCKS, int ddBlocksMaxNumberOfClasses=-1, QString overriddenName=QString())
Constructor for DxfLayer.
int layerOutputAttributeIndex() const
Returns the attribute index used to split the source layer's features into multiple exported DXF laye...