Skip to content

Commit c577b65

Browse files
authoredApr 20, 2018
Merge pull request #6810 from mhugent/dxf_fixes_2_18
Dxf fixes for 2 18
2 parents 9bf55aa + 543f501 commit c577b65

File tree

9 files changed

+219
-44
lines changed

9 files changed

+219
-44
lines changed
 

‎python/core/dxf/qgsdxfexport.sip

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,17 @@ class QgsDxfExport
375375
//! @note added in 2.15
376376
void writeMText( const QString &layer, const QString &text, const QgsPointV2 &pt, double width, double angle, const QColor& color );
377377

378-
static double mapUnitScaleFactor( double scaleDenominator, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits );
378+
/** Returns scale factor for conversion to map units
379+
* @param scaleDenominator the map scale denominator
380+
* @param symbolUnits the symbol output units
381+
* @param mapUnits the map units
382+
* @param mapUnitsPerPixel Map units per pixel*/
383+
static double mapUnitScaleFactor( double scaleDenominator, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits, double mapUnitsPerPixel = 1.0 );
384+
/** Clips value to scale minimum/maximum
385+
@param value the value to clip
386+
@param scale the scale dependent minimum/maximum values
387+
@param pixelToMMFactor pixels per mm*/
388+
void clipValueToMapUnitScale( double& value, const QgsMapUnitScale& scale, double pixelToMMFactor ) const;
379389

380390
//! Return cleaned layer name for use in DXF
381391
static QString dxfLayerName( const QString &name );

‎src/core/dxf/qgsdxfexport.cpp

Lines changed: 115 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include "qgsdxfexport.h"
3030
#include "qgsdxfpallabeling.h"
31+
#include "qgsgeometrygeneratorsymbollayerv2.h"
3132
#include "qgsvectordataprovider.h"
3233
#include "qgspoint.h"
3334
#include "qgsrendererv2.h"
@@ -932,7 +933,7 @@ void QgsDxfExport::writeBlocks()
932933
writeGroup( 1, "" );
933934

934935
// maplayer 0 -> block receives layer from INSERT statement
935-
ml->writeDxf( *this, mapUnitScaleFactor( mSymbologyScaleDenominator, ml->sizeUnit(), mMapUnits ), "0", ctx );
936+
ml->writeDxf( *this, mapUnitScaleFactor( mSymbologyScaleDenominator, ml->sizeUnit(), mMapUnits, ctx.renderContext().mapToPixel().mapUnitsPerPixel() ), "0", ctx );
936937

937938
writeGroup( 0, "ENDBLK" );
938939
writeHandle();
@@ -967,6 +968,9 @@ void QgsDxfExport::writeEntities()
967968
ctx.setScaleFactor( 96.0 / 25.4 );
968969
ctx.setMapToPixel( QgsMapToPixel( 1.0 / mFactor, mExtent.center().x(), mExtent.center().y(), mExtent.width() * mFactor, mExtent.height() * mFactor, 0 ) );
969970

971+
ctx.expressionContext().appendScope( QgsExpressionContextUtils::projectScope() );
972+
ctx.expressionContext().appendScope( QgsExpressionContextUtils::globalScope() );
973+
970974
// label engine
971975
QgsLabelingEngineV2 engine;
972976
engine.readSettingsFromProject();
@@ -1082,7 +1086,22 @@ void QgsDxfExport::writeEntities()
10821086
int nSymbolLayers = ( *symbolIt )->symbolLayerCount();
10831087
for ( int i = 0; i < nSymbolLayers; ++i )
10841088
{
1085-
addFeature( sctx, ct, lName, ( *symbolIt )->symbolLayer( i ), *symbolIt );
1089+
QgsSymbolLayerV2* sl = ( *symbolIt )->symbolLayer( i );
1090+
if ( !sl )
1091+
{
1092+
continue;
1093+
}
1094+
1095+
bool isGeometryGenerator = ( sl->layerType() == "GeometryGenerator" );
1096+
1097+
if ( isGeometryGenerator )
1098+
{
1099+
addGeometryGeneratorSymbolLayer( sctx, ct, lName, sl, true );
1100+
}
1101+
else
1102+
{
1103+
addFeature( sctx, ct, lName, sl, *symbolIt );
1104+
}
10861105
}
10871106
}
10881107
}
@@ -1094,7 +1113,15 @@ void QgsDxfExport::writeEntities()
10941113
{
10951114
continue;
10961115
}
1097-
addFeature( sctx, ct, lName, s->symbolLayer( 0 ), s );
1116+
1117+
if ( s->symbolLayer( 0 )->layerType() == "GeometryGenerator" )
1118+
{
1119+
addGeometryGeneratorSymbolLayer( sctx, ct, lName, s->symbolLayer( 0 ), false );
1120+
}
1121+
else
1122+
{
1123+
addFeature( sctx, ct, lName, s->symbolLayer( 0 ), s );
1124+
}
10981125
}
10991126

11001127
if ( lp )
@@ -3424,7 +3451,7 @@ void QgsDxfExport::writePoint( const QgsPointV2 &pt, const QString& layer, const
34243451
const QgsMarkerSymbolLayerV2* msl = dynamic_cast< const QgsMarkerSymbolLayerV2* >( symbolLayer );
34253452
if ( msl && symbol )
34263453
{
3427-
if ( symbolLayer->writeDxf( *this, mapUnitScaleFactor( mSymbologyScaleDenominator, msl->sizeUnit(), mMapUnits ), layer, ctx, QPointF( pt.x(), pt.y() ) ) )
3454+
if ( symbolLayer->writeDxf( *this, mapUnitScaleFactor( mSymbologyScaleDenominator, msl->sizeUnit(), mMapUnits, ctx.renderContext().mapToPixel().mapUnitsPerPixel() ), layer, ctx, QPointF( pt.x(), pt.y() ) ) )
34283455
{
34293456
return;
34303457
}
@@ -3956,6 +3983,46 @@ void QgsDxfExport::addFeature( QgsSymbolV2RenderContext& ctx, const QgsCoordinat
39563983
}
39573984
}
39583985

3986+
void QgsDxfExport::addGeometryGeneratorSymbolLayer( QgsSymbolV2RenderContext &ctx, const QgsCoordinateTransform *ct, const QString &layer, QgsSymbolLayerV2 *symbolLayer, bool allSymbolLayers )
3987+
{
3988+
QgsGeometryGeneratorSymbolLayerV2* gg = dynamic_cast<QgsGeometryGeneratorSymbolLayerV2*>( symbolLayer );
3989+
if ( !gg )
3990+
{
3991+
return;
3992+
}
3993+
3994+
const QgsFeature* fet = ctx.feature();
3995+
if ( !fet )
3996+
{
3997+
return;
3998+
}
3999+
4000+
QgsFeature f = *fet;
4001+
4002+
QgsExpressionContext& expressionContext = ctx.renderContext().expressionContext();
4003+
QgsExpression geomExpr( gg->geometryExpression() );
4004+
geomExpr.prepare( &expressionContext );
4005+
QgsGeometry geom = geomExpr.evaluate( &expressionContext ).value<QgsGeometry>();
4006+
f.setGeometry( geom );
4007+
4008+
QgsSymbolV2* symbol = gg->subSymbol();
4009+
if ( symbol && symbol->symbolLayerCount() > 0 )
4010+
{
4011+
QgsExpressionContextScope* symbolExpressionContextScope = symbol->symbolRenderContext()->expressionContextScope();
4012+
symbolExpressionContextScope->setFeature( f );
4013+
4014+
ctx.setFeature( &f );
4015+
4016+
int nSymbolLayers = allSymbolLayers ? symbol->symbolLayerCount() : 1;
4017+
for ( int i = 0; i < nSymbolLayers; ++i )
4018+
{
4019+
addFeature( ctx, ct, layer, symbol->symbolLayer( i ), symbol );
4020+
}
4021+
4022+
ctx.setFeature( fet );
4023+
}
4024+
}
4025+
39594026
QColor QgsDxfExport::colorFromSymbolLayer( const QgsSymbolLayerV2* symbolLayer, QgsSymbolV2RenderContext &ctx )
39604027
{
39614028
if ( !symbolLayer )
@@ -4036,14 +4103,53 @@ QgsRenderContext QgsDxfExport::renderContext() const
40364103
return context;
40374104
}
40384105

4039-
double QgsDxfExport::mapUnitScaleFactor( double scaleDenominator, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits )
4106+
double QgsDxfExport::mapUnitScaleFactor( double scaleDenominator, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits, double mapUnitsPerPixel )
40404107
{
40414108
if ( symbolUnits == QgsSymbolV2::MapUnit )
40424109
{
40434110
return 1.0;
40444111
}
4045-
// MM symbol unit
4046-
return scaleDenominator * QgsUnitTypes::fromUnitToUnitFactor( QGis::Meters, mapUnits ) / 1000.0;
4112+
else if ( symbolUnits == QgsSymbolV2::MM )
4113+
{
4114+
return ( scaleDenominator * QgsUnitTypes::fromUnitToUnitFactor( QGis::Meters, mapUnits ) / 1000.0 );
4115+
}
4116+
else if ( symbolUnits == QgsSymbolV2::Pixel )
4117+
{
4118+
return mapUnitsPerPixel;
4119+
}
4120+
return 1.0;
4121+
}
4122+
4123+
void QgsDxfExport::clipValueToMapUnitScale( double& value, const QgsMapUnitScale& scale, double pixelToMMFactor ) const
4124+
{
4125+
if ( !scale.minSizeMMEnabled && !scale.maxSizeMMEnabled )
4126+
{
4127+
return;
4128+
}
4129+
4130+
double mapUnitsPerPixel = mMapSettings.mapToPixel().mapUnitsPerPixel();
4131+
4132+
double minSizeMU = -DBL_MAX;
4133+
if ( scale.minSizeMMEnabled )
4134+
{
4135+
minSizeMU = scale.minSizeMM * pixelToMMFactor * mapUnitsPerPixel;
4136+
}
4137+
if ( !qgsDoubleNear( scale.minScale, 0.0 ) )
4138+
{
4139+
minSizeMU = qMax( minSizeMU, value );
4140+
}
4141+
value = qMax( value, minSizeMU );
4142+
4143+
double maxSizeMU = DBL_MAX;
4144+
if ( scale.maxSizeMMEnabled )
4145+
{
4146+
maxSizeMU = scale.maxSizeMM * pixelToMMFactor * mapUnitsPerPixel;
4147+
}
4148+
if ( !qgsDoubleNear( scale.maxScale, 0.0 ) )
4149+
{
4150+
maxSizeMU = qMin( maxSizeMU, value );
4151+
}
4152+
value = qMin( value, maxSizeMU );
40474153
}
40484154

40494155
QList< QPair< QgsSymbolLayerV2*, QgsSymbolV2* > > QgsDxfExport::symbolLayers( QgsRenderContext &context )
@@ -4174,7 +4280,7 @@ void QgsDxfExport::writeLinetype( const QString& styleName, const QVector<qreal>
41744280
QVector<qreal>::const_iterator dashIt = pattern.constBegin();
41754281
for ( ; dashIt != pattern.constEnd(); ++dashIt )
41764282
{
4177-
length += ( *dashIt * mapUnitScaleFactor( mSymbologyScaleDenominator, u, mMapUnits ) );
4283+
length += ( *dashIt * mapUnitScaleFactor( mSymbologyScaleDenominator, u, mMapUnits, mMapSettings.mapToPixel().mapUnitsPerPixel() ) );
41784284
}
41794285

41804286
writeGroup( 0, "LTYPE" );
@@ -4195,7 +4301,7 @@ void QgsDxfExport::writeLinetype( const QString& styleName, const QVector<qreal>
41954301
{
41964302
// map units or mm?
41974303
double segmentLength = ( isGap ? -*dashIt : *dashIt );
4198-
segmentLength *= mapUnitScaleFactor( mSymbologyScaleDenominator, u, mMapUnits );
4304+
segmentLength *= mapUnitScaleFactor( mSymbologyScaleDenominator, u, mMapUnits, mMapSettings.mapToPixel().mapUnitsPerPixel() );
41994305
writeGroup( 49, segmentLength );
42004306
writeGroup( 74, 0 );
42014307
isGap = !isGap;

‎src/core/dxf/qgsdxfexport.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,17 @@ class CORE_EXPORT QgsDxfExport
402402
//! @note added in 2.15
403403
void writeMText( const QString &layer, const QString &text, const QgsPointV2 &pt, double width, double angle, const QColor& color );
404404

405-
static double mapUnitScaleFactor( double scaleDenominator, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits );
405+
/** Returns scale factor for conversion to map units
406+
* @param scaleDenominator the map scale denominator
407+
* @param symbolUnits the symbol output units
408+
* @param mapUnits the map units
409+
* @param mapUnitsPerPixel Map units per pixel*/
410+
static double mapUnitScaleFactor( double scaleDenominator, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits, double mapUnitsPerPixel = 1.0 );
411+
/** Clips value to scale minimum/maximum
412+
@param value the value to clip
413+
@param scale the scale dependent minimum/maximum values
414+
@param pixelToMMFactor pixels per mm*/
415+
void clipValueToMapUnitScale( double& value, const QgsMapUnitScale& scale, double pixelToMMFactor ) const;
406416

407417
//! Return cleaned layer name for use in DXF
408418
static QString dxfLayerName( const QString &name );
@@ -467,6 +477,13 @@ class CORE_EXPORT QgsDxfExport
467477
void writeLinetype( const QString &styleName, const QVector<qreal> &pattern, QgsSymbolV2::OutputUnit u );
468478

469479
void addFeature( QgsSymbolV2RenderContext &ctx, const QgsCoordinateTransform *ct, const QString &layer, const QgsSymbolLayerV2 *symbolLayer, const QgsSymbolV2 *symbol );
480+
/** Writes geometry generator symbol layer
481+
@param ctx the symbol render context
482+
@param ct the coordinate transform
483+
@param layer the layer name
484+
@param symbolLayer the symbollayer to write to the dxf file
485+
@param allSymbolLayers if true, all symbol layers of the subsymbol are writeen. If false, only the first one is written*/
486+
void addGeometryGeneratorSymbolLayer( QgsSymbolV2RenderContext &ctx, const QgsCoordinateTransform *ct, const QString &layer, QgsSymbolLayerV2 *symbolLayer, bool allSymbolLayers );
470487

471488
//returns dxf palette index from symbol layer color
472489
static QColor colorFromSymbolLayer( const QgsSymbolLayerV2 *symbolLayer, QgsSymbolV2RenderContext &ctx );

‎src/core/dxf/qgsdxfpaintengine.cpp

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ QgsDxfPaintEngine::QgsDxfPaintEngine( const QgsDxfPaintDevice* dxfDevice, QgsDxf
2424
: QPaintEngine( QPaintEngine::AllFeatures /*QPaintEngine::PainterPaths | QPaintEngine::PaintOutsidePaintEvent*/ )
2525
, mPaintDevice( dxfDevice )
2626
, mDxf( dxf )
27+
, mOpacity( 1.0 )
2728
{
2829
}
2930

@@ -64,6 +65,11 @@ void QgsDxfPaintEngine::updateState( const QPaintEngineState& state )
6465

6566
if ( state.state() & QPaintEngine::DirtyBrush )
6667
mBrush = state.brush();
68+
69+
if ( state.state() & QPaintEngine::DirtyOpacity )
70+
{
71+
mOpacity = state.opacity();
72+
}
6773
}
6874

6975
void QgsDxfPaintEngine::setRing( QgsPointSequenceV2 &polyline, const QPointF *points, int pointCount )
@@ -86,12 +92,12 @@ void QgsDxfPaintEngine::drawPolygon( const QPointF *points, int pointCount, Poly
8692
if ( mode == QPaintEngine::PolylineMode )
8793
{
8894
if ( mPen.style() != Qt::NoPen && mPen.brush().style() != Qt::NoBrush )
89-
mDxf->writePolyline( polygon.at( 0 ), mLayer, "CONTINUOUS", mPen.color(), currentWidth() );
95+
mDxf->writePolyline( polygon.at( 0 ), mLayer, "CONTINUOUS", penColor(), currentWidth() );
9096
}
9197
else
9298
{
9399
if ( mBrush.style() != Qt::NoBrush )
94-
mDxf->writePolygon( polygon, mLayer, "SOLID", mBrush.color() );
100+
mDxf->writePolygon( polygon, mLayer, "SOLID", brushColor() );
95101
}
96102
}
97103

@@ -122,7 +128,7 @@ void QgsDxfPaintEngine::drawPath( const QPainterPath& path )
122128
endPolygon();
123129

124130
if ( !mPolygon.isEmpty() && mBrush.style() != Qt::NoBrush )
125-
mDxf->writePolygon( mPolygon, mLayer, "SOLID", mBrush.color() );
131+
mDxf->writePolygon( mPolygon, mLayer, "SOLID", brushColor() );
126132

127133
mPolygon.clear();
128134
}
@@ -198,7 +204,7 @@ void QgsDxfPaintEngine::drawLines( const QLineF* lines, int lineCount )
198204
{
199205
mDxf->writeLine( toDxfCoordinates( lines[i].p1() ),
200206
toDxfCoordinates( lines[i].p2() ),
201-
mLayer, "CONTINUOUS", mPen.color(), currentWidth() );
207+
mLayer, "CONTINUOUS", penColor(), currentWidth() );
202208
}
203209
}
204210

@@ -291,3 +297,25 @@ int QgsDxfPaintEngine::faculty( int n )
291297

292298
return result;
293299
}
300+
301+
QColor QgsDxfPaintEngine::penColor() const
302+
{
303+
if ( qgsDoubleNear( mOpacity, 1.0 ) )
304+
{
305+
return mPen.color();
306+
}
307+
QColor c = mPen.color();
308+
c.setAlphaF( c.alphaF() * mOpacity );
309+
return c;
310+
}
311+
312+
QColor QgsDxfPaintEngine::brushColor() const
313+
{
314+
if ( qgsDoubleNear( mOpacity, 1.0 ) )
315+
{
316+
return mBrush.color();
317+
}
318+
QColor c = mBrush.color();
319+
c.setAlphaF( c.alphaF() * mOpacity );
320+
return c;
321+
}

‎src/core/dxf/qgsdxfpaintengine.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ class CORE_EXPORT QgsDxfPaintEngine: public QPaintEngine
6161
QTransform mTransform;
6262
QPen mPen;
6363
QBrush mBrush;
64+
/** Opacity*/
65+
double mOpacity;
6466
QString mLayer;
6567
QPointF mShift;
6668
QgsRingSequenceV2 mPolygon;
@@ -84,6 +86,11 @@ class CORE_EXPORT QgsDxfPaintEngine: public QPaintEngine
8486
static int lower( int n, int i );
8587
static double power( double a, int b );
8688
static int faculty( int n );
89+
90+
/** Returns current pen color*/
91+
QColor penColor() const;
92+
/** Returns current brush color*/
93+
QColor brushColor() const;
8794
};
8895

8996
#endif // QGSDXFPAINTENGINE_H

‎src/core/symbology-ng/qgsfillsymbollayerv2.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ double QgsSimpleFillSymbolLayerV2::dxfWidth( const QgsDxfExport& e, QgsSymbolV2R
416416
context.setOriginalValueVariable( mBorderWidth );
417417
width = evaluateDataDefinedProperty( QgsSymbolLayerV2::EXPR_WIDTH_BORDER, context, mBorderWidth ).toDouble();
418418
}
419-
return width * e.mapUnitScaleFactor( e.symbologyScaleDenominator(), mBorderWidthUnit, e.mapUnits() );
419+
return width * e.mapUnitScaleFactor( e.symbologyScaleDenominator(), mBorderWidthUnit, e.mapUnits(), context.renderContext().mapToPixel().mapUnitsPerPixel() );
420420
}
421421

422422
QColor QgsSimpleFillSymbolLayerV2::dxfColor( QgsSymbolV2RenderContext &context ) const
@@ -1702,7 +1702,7 @@ double QgsImageFillSymbolLayer::dxfWidth( const QgsDxfExport& e, QgsSymbolV2Rend
17021702
context.setOriginalValueVariable( mOutlineWidth );
17031703
width = evaluateDataDefinedProperty( QgsSymbolLayerV2::EXPR_WIDTH, context, mOutlineWidth ).toDouble();
17041704
}
1705-
return width * e.mapUnitScaleFactor( e.symbologyScaleDenominator(), mOutlineWidthUnit, e.mapUnits() );
1705+
return width * e.mapUnitScaleFactor( e.symbologyScaleDenominator(), mOutlineWidthUnit, e.mapUnits(), context.renderContext().mapToPixel().mapUnitsPerPixel() );
17061706
}
17071707

17081708
QColor QgsImageFillSymbolLayer::dxfColor( QgsSymbolV2RenderContext &context ) const

0 commit comments

Comments
 (0)