QGIS API Documentation 4.3.0-Master (16eddb2670e)
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;
45class QTextCodec;
46
47#define DXF_HANDSEED 100
48#define DXF_HANDMAX 9999999
49#define DXF_HANDPLOTSTYLE 0xf
50
51namespace pal // SIP_SKIP
52{
53 class LabelPosition;
54}
55
56
62#ifdef SIP_RUN
63class CORE_EXPORT QgsDxfExport
64{
65#else
66
67static const bool DEFAULT_DXF_DATA_DEFINED_BLOCKS = true;
68
69class CORE_EXPORT QgsDxfExport : public QgsLabelSink
70{
71#endif
72 friend class TestQgsDxfExport;
73
74 public:
78 struct CORE_EXPORT DxfLayer
79 {
89 DxfLayer( QgsVectorLayer *vl, int layerOutputAttributeIndex = -1, bool buildDDBlocks = DEFAULT_DXF_DATA_DEFINED_BLOCKS, int ddBlocksMaxNumberOfClasses = -1, QString overriddenName = QString() )
90 : mLayer( vl )
91 , mLayerOutputAttributeIndex( layerOutputAttributeIndex )
92 , mBuildDDBlocks( buildDDBlocks )
93 , mDDBlocksMaxNumberOfClasses( ddBlocksMaxNumberOfClasses )
94 , mOverriddenName( overriddenName )
95 {}
96
98 QgsVectorLayer *layer() const { return mLayer; }
99
107 int layerOutputAttributeIndex() const { return mLayerOutputAttributeIndex; }
108
117 QString splitLayerAttribute() const;
118
125 bool buildDataDefinedBlocks() const { return mBuildDDBlocks; }
126
135 int dataDefinedBlocksMaximumNumberOfClasses() const { return mDDBlocksMaxNumberOfClasses; }
136
144 QString overriddenName() const { return mOverriddenName; }
145
146 private:
147 QgsVectorLayer *mLayer = nullptr;
148 int mLayerOutputAttributeIndex = -1;
149
153 bool mBuildDDBlocks = DEFAULT_DXF_DATA_DEFINED_BLOCKS;
154
158 int mDDBlocksMaxNumberOfClasses = -1;
159
163 QString mOverriddenName;
164 };
165
167 enum Flag SIP_ENUM_BASETYPE( IntFlag )
168 {
169 FlagNoMText = 1 << 1,
172 };
173 Q_DECLARE_FLAGS( Flags, Flag )
174
175
180 enum class ExportResult
181 {
182 Success = 0,
183 InvalidDeviceError,
184 DeviceNotWritableError,
185 EmptyExtentError,
186 InvalidEncodingError
187 };
188
192 enum class VAlign : int
193 {
194 VBaseLine = 0,
195 VBottom = 1,
196 VMiddle = 2,
197 VTop = 3,
198 Undefined = 9999
199 };
200
202 enum class HAlign : int
203 {
204 HLeft = 0,
205 HCenter = 1,
206 HRight = 2,
207 HAligned = 3,
208 HMiddle = 4,
209 HFit = 5,
210 Undefined = 9999
211 };
212
229
231
232
236
237 ~QgsDxfExport() override;
238
243 void setMapSettings( const QgsMapSettings &settings );
244
250
255 QgsDxfExport::Flags flags() const;
256
262 void addLayers( const QList< QgsDxfExport::DxfLayer > &layers );
263
270 ExportResult writeToFile( QIODevice *d, const QString &codec ); //maybe add progress dialog? other parameters (e.g. scale, dpi)?
271
276 const QString feedbackMessage() const { return mFeedbackMessage; }
277
283 void setSymbologyScale( double scale ) { mSymbologyScale = scale; }
284
290 double symbologyScale() const { return mSymbologyScale; }
291
296 Qgis::DistanceUnit mapUnits() const;
297
302 void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
303
308 QgsCoordinateReferenceSystem destinationCrs() const;
309
314 void setSymbologyExport( Qgis::FeatureSymbologyExport e ) { mSymbologyExport = e; }
315
321 Qgis::FeatureSymbologyExport symbologyExport() const { return mSymbologyExport; }
322
327 void setExtent( const QgsRectangle &r ) { mExtent = r; }
328
334 QgsRectangle extent() const { return mExtent; }
335
342 void setLayerTitleAsName( bool layerTitleAsName ) { mLayerTitleAsName = layerTitleAsName; }
343
349 bool layerTitleAsName() { return mLayerTitleAsName; }
350
356 void setForce2d( bool force2d ) { mForce2d = force2d; }
357
363 bool force2d() { return mForce2d; }
364
369 static int closestColorMatch( QRgb color );
370
377 QString layerName( const QString &id, const QgsFeature &f ) const;
378
385 QString layerName( QgsVectorLayer *vl ) const;
386
393 void writeGroup( int code, int i ) SIP_PYNAME( writeGroupInt );
394
401 void writeGroup( int code, long long i ) SIP_PYNAME( writeGroupLong );
402
409 void writeGroup( int code, double d ) SIP_PYNAME( writeGroupDouble );
410
416 void writeGroup( int code, const QString &s );
417
424 void writeGroup( int code, const QgsPoint &p ) SIP_PYNAME( writeGroupPointV2 );
425
434 void writeGroup( const QColor &color, int exactMatch = 62, int rgbCode = 420, int transparencyCode = 440 );
435
440 void writeGroupCode( int code );
441
446 void writeInt( int i );
447
452 void writeDouble( double d );
453
458 void writeString( const QString &s );
459
466 int writeHandle( int code = 5, int handle = 0 );
467
477 void writePolyline( const QgsPointSequence &line, const QString &layer, const QString &lineStyleName, const QColor &color, double width = -1 ) SIP_SKIP;
478
489 void writePolyline( const QgsCurve &curve, const QString &layer, const QString &lineStyleName, const QColor &color, double width = -1 ) SIP_SKIP;
490
499 void writePolygon( const QgsRingSequence &polygon, const QString &layer, const QString &hatchPattern, const QColor &color ) SIP_SKIP;
500
510 void writePolygon( const QgsCurvePolygon &polygon, const QString &layer, const QString &hatchPattern, const QColor &color ) SIP_SKIP;
511
515 void writeLine( const QgsPoint &pt1, const QgsPoint &pt2, const QString &layer, const QString &lineStyleName, const QColor &color, double width = -1 );
516
521 void writePoint( const QString &layer, const QColor &color, const QgsPoint &pt ) SIP_PYNAME( writePointV2 );
522
527 void writeFilledCircle( const QString &layer, const QColor &color, const QgsPoint &pt, double radius ) SIP_PYNAME( writeFillCircleV2 );
528
533 void writeCircle( const QString &layer, const QColor &color, const QgsPoint &pt, double radius, const QString &lineStyleName, double width ) SIP_PYNAME( writeCircleV2 );
534
539 void writeText(
540 const QString &layer,
541 const QString &text,
542 const QgsPoint &pt,
543 double size,
544 double angle,
545 const QColor &color,
546 QgsDxfExport::HAlign hali = QgsDxfExport::HAlign::Undefined,
547 QgsDxfExport::VAlign vali = QgsDxfExport::VAlign::Undefined
548 ) SIP_PYNAME( writeTextV2 );
549
554 void writeMText( const QString &layer, const QString &text, const QgsPoint &pt, double width, double angle, const QColor &color );
555
564 Q_DECL_DEPRECATED static double mapUnitScaleFactor( double scale, Qgis::RenderUnit symbolUnits, Qgis::DistanceUnit mapUnits, double mapUnitsPerPixel = 1.0 ) SIP_DEPRECATED;
565
571 static double mapUnitScaleFactor( const QgsRenderContext &renderContext, Qgis::RenderUnit symbolUnits );
572
579 void clipValueToMapUnitScale( double &value, const QgsMapUnitScale &scale, double pixelToMMFactor ) const;
580
582 static QString dxfLayerName( const QString &name );
583
585 static QString dxfEncoding( const QString &name );
586
588 static QStringList encodings();
589
595 void drawLabel( const QString &layerId, QgsRenderContext &context, pal::LabelPosition *label, const QgsPalLayerSettings &settings ) SIP_SKIP override;
596
605 Q_DECL_DEPRECATED void registerDxfLayer( const QString &layerId, QgsFeatureId fid, const QString &layer );
606
607 private:
608#ifdef SIP_RUN
609 QgsDxfExport( const QgsDxfExport &other );
610 QgsDxfExport &operator=( const QgsDxfExport & );
611#endif
612
613 struct DataDefinedBlockInfo
614 {
615 QString blockName;
616 double angle;
617 double size;
618 QgsFeature feature; //a feature representing the attribute combination (without geometry)
619 };
620
622 QgsRectangle mExtent;
624 double mSymbologyScale = 1.0;
627 bool mLayerTitleAsName = false;
628
629 QIODevice *mDevice = nullptr;
630 QTextCodec *mCodec = nullptr;
631
632 int mSymbolLayerCounter = 0; //internal counter
633 int mNextHandleId = DXF_HANDSEED;
634 int mBlockCounter = 0;
635
636 QHash< const QgsSymbolLayer *, QString > mLineStyles; //symbol layer name types
637 QHash< const QgsSymbolLayer *, QString > mPointSymbolBlocks; //reference to point symbol blocks
638 QHash< const QgsSymbolLayer *, double > mPointSymbolBlockSizes; //reference to point symbol size used to create its block
639 QHash< const QgsSymbolLayer *, double > mPointSymbolBlockAngles; //reference to point symbol size used to create its block
641 QHash< const QgsSymbolLayer *, QHash<uint, DataDefinedBlockInfo> > mDataDefinedBlockInfo; // symbolLayerName -> symbolHash/Feature
642
643 //AC1009
644 void createDDBlockInfo();
645 void writeHeader( const QString &codepage );
646 void prepareRenderers();
647 void writeTables();
648 void writeBlocks();
649 void writeEntities();
650 void writeEntitiesSymbolLevels( DxfLayerJob *job );
651 void stopRenderers();
652 void writeEndFile();
653
654 void startSection();
655 void endSection();
656
657 void writePoint( const QgsPoint &pt, const QString &layer, const QColor &color, QgsSymbolRenderContext &ctx, const QgsSymbolLayer *symbolLayer, const QgsSymbol *symbol, double angle );
658 void writeDefaultLinetypes();
659 void writeSymbolLayerLinetype( const QgsSymbolLayer *symbolLayer );
660 void writeLinetype( const QString &styleName, const QVector<qreal> &pattern, Qgis::RenderUnit u );
661
665 void writeText( const QString &layer, const QString &text, pal::LabelPosition *label, const QgsPalLayerSettings &layerSettings, const QgsExpressionContext &expressionContext );
666
676 void addGeometryGeneratorSymbolLayer( QgsSymbolRenderContext &context, const QgsCoordinateTransform &ct, const QString &layer, QgsSymbolLayer *symbolLayer, bool allSymbolLayers );
677
687 void addFeature( QgsSymbolRenderContext &context, const QgsCoordinateTransform &ct, const QString &layer, const QgsSymbolLayer *symbolLayer, const QgsSymbol *symbol );
688
689 //returns dxf palette index from symbol layer color
690 static QColor colorFromSymbolLayer( const QgsSymbolLayer *symbolLayer, QgsSymbolRenderContext &ctx );
691 QString lineStyleFromSymbolLayer( const QgsSymbolLayer *symbolLayer );
692
693 //functions for dxf palette
694 static int color_distance( QRgb p1, int index );
695 static QRgb createRgbEntry( qreal r, qreal g, qreal b );
696
697 //helper functions for symbology export
698 QgsRenderContext renderContext() const;
699
700 QList< QPair< QgsSymbolLayer *, QgsSymbol * > > symbolLayers( QgsRenderContext &context );
701 static int nLineTypes( const QList< QPair< QgsSymbolLayer *, QgsSymbol *> > &symbolLayers );
702 static bool hasBlockBreakingDataDefinedProperties( const QgsSymbolLayer *sl, const QgsSymbol *symbol );
704 static bool isSymbolLayerEnabled( const QgsSymbolLayer *layer, QgsSymbolRenderContext &context );
705 void writeSymbolTableBlockRef( const QString &blockName );
706 void writeSymbolLayerBlock( const QString &blockName, const QgsMarkerSymbolLayer *ml, QgsSymbolRenderContext &ctx );
707 void writePointBlockReference(
708 const QgsPoint &pt, const QgsSymbolLayer *symbolLayer, QgsSymbolRenderContext &ctx, const QString &layer, double angle, const QString &blockName, double blockAngle, double blockSize
709 );
710 static uint dataDefinedSymbolClassHash( const QgsFeature &fet, const QgsPropertyCollection &prop, const QgsExpressionContext &context );
711
712 double dashSize() const;
713 double dotSize() const;
714 double dashSeparatorSize() const;
715 double sizeToMapUnits( double s ) const;
716 static QString lineNameFromPenStyle( Qt::PenStyle style );
717 bool layerIsScaleBasedVisible( const QgsMapLayer *layer ) const;
718
719 QHash<QString, int> mBlockHandles;
720 QString mBlockHandle;
721
723 QMap< QString, QMap<QgsFeatureId, QString> > mDxfLayerNames;
724 QgsCoordinateReferenceSystem mCrs;
725 QgsMapSettings mMapSettings;
726 QList<QgsMapLayer *> mLayerList;
727 QHash<QString, int> mLayerNameAttribute;
728 QHash<QString, int> mLayerDDBlockMaxNumberOfClasses;
729 QHash<QString, QString> mLayerOverriddenName;
730 double mFactor = 1.0;
731 bool mForce2d = false;
732
734
735 void appendCurve( const QgsCurve &c, QVector<QgsPoint> &points, QVector<double> &bulges );
736 void appendLineString( const QgsLineString &ls, QVector<QgsPoint> &points, QVector<double> &bulges );
737 void appendCircularString( const QgsCircularString &cs, QVector<QgsPoint> &points, QVector<double> &bulges );
738 void appendCompoundCurve( const QgsCompoundCurve &cc, QVector<QgsPoint> &points, QVector<double> &bulges );
739
740 QgsRenderContext mRenderContext;
741 // Internal cache for layer related information required during rendering
742 QList<DxfLayerJob *> mJobs;
743 std::unique_ptr<QgsLabelingEngine> mLabelingEngine;
744
745 QString mFeedbackMessage;
746};
747
750
751#endif // QGSDXFEXPORT_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:62
DistanceUnit
Units of distance.
Definition qgis.h:5568
@ Meters
Meters.
Definition qgis.h:5569
RenderUnit
Rendering size units.
Definition qgis.h:5758
FeatureSymbologyExport
Options for exporting features considering their symbology.
Definition qgis.h:6377
@ NoSymbology
Export only data.
Definition qgis.h:6378
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.
@ FlagHairlineWidthExport
Export all lines with minimum width and don't fill polygons.
@ 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
This is a 3D polyline.
@ 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.
@ Spline
Spline-fit vertices have been added.
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:277
#define SIP_SKIP
Definition qgis_sip.h:138
#define SIP_PYNAME(name)
Definition qgis_sip.h:88
QVector< QgsPointSequence > QgsRingSequence
QVector< QgsPoint > QgsPointSequence
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsTimeZoneComboBox::StandardItems)
#define DXF_HANDSEED
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
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...