Skip to content

Commit a7a942c

Browse files
committed
Export labels to dxf
1 parent fb18038 commit a7a942c

8 files changed

+344
-6
lines changed

src/core/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ SET(QGIS_CORE_SRCS
170170
dxf/qgsdxfexport.cpp
171171
dxf/qgsdxfpaintdevice.cpp
172172
dxf/qgsdxfpaintengine.cpp
173+
dxf/qgsdxfpallabeling.cpp
173174

174175
pal/costcalculator.cpp
175176
pal/feature.cpp

src/core/dxf/qgsdxfexport.cpp

+60-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
***************************************************************************/
1717

1818
#include "qgsdxfexport.h"
19+
#include "qgsdxfpallabeling.h"
1920
#include "qgsvectordataprovider.h"
2021
#include "qgspoint.h"
2122
#include "qgsrendererv2.h"
@@ -473,6 +474,26 @@ void QgsDxfExport::writeTables()
473474
writeGroup( 6, "CONTINUOUS" );
474475
}
475476
writeGroup( 0, "ENDTAB" );
477+
478+
//STYLE
479+
writeGroup( 0, "TABLE" );
480+
writeGroup( 2, "STYLE" );
481+
writeGroup( 70, 1 );
482+
483+
//provide only standard font for the moment
484+
writeGroup( 0, "STYLE" );
485+
writeGroup( 2, "STANDARD" );
486+
writeGroup( 70, 64 );
487+
writeGroup( 40, 0.0 );
488+
writeGroup( 41, 1.0 );
489+
writeGroup( 50, 0.0 );
490+
writeGroup( 71, 0 );
491+
writeGroup( 42, 5.0 );
492+
writeGroup( 3, "romans.shx" );
493+
writeGroup( 4, "" );
494+
495+
writeGroup( 0, "ENDTAB" );
496+
476497
endSection();
477498
}
478499

@@ -537,6 +558,10 @@ void QgsDxfExport::writeEntities()
537558
startSection();
538559
writeGroup( 2, "ENTITIES" );
539560

561+
//label engine
562+
QgsDxfPalLabeling labelEngine( this, mExtent.isEmpty() ? dxfExtent() : mExtent, mSymbologyScaleDenominator );
563+
QgsRenderContext& ctx = labelEngine.renderContext();
564+
540565
//iterate through the maplayers
541566
QList< QgsMapLayer* >::iterator layerIt = mLayers.begin();
542567
for ( ; layerIt != mLayers.end(); ++layerIt )
@@ -547,21 +572,32 @@ void QgsDxfExport::writeEntities()
547572
continue;
548573
}
549574

550-
QgsRenderContext ctx = renderContext();
551-
ctx.setRendererScale( mSymbologyScaleDenominator );
552575
QgsSymbolV2RenderContext sctx( ctx, QgsSymbolV2::MM , 1.0, false, 0, 0 );
553576
QgsFeatureRendererV2* renderer = vl->rendererV2();
554577
renderer->startRender( ctx, vl );
555578

579+
//todo: call mLabeling.prepareLayer(...)
580+
QSet<int> attrIndex;
581+
bool labelLayer = ( labelEngine.prepareLayer( vl, attrIndex, ctx ) != 0 );
582+
556583
if ( mSymbologyExport == QgsDxfExport::SymbolLayerSymbology && renderer->usingSymbolLevels() )
557584
{
558585
writeEntitiesSymbolLevels( vl );
559586
renderer->stopRender( ctx );
560587
continue;
561588
}
562589

590+
//combine renderer and label attributes
591+
const QgsFields& fields = vl->pendingFields();
592+
QList<QString> attributes = renderer->usedAttributes();
593+
QSet<int>::const_iterator attrIndexIt = attrIndex.constBegin();
594+
for ( ; attrIndexIt != attrIndex.constEnd(); ++attrIndexIt )
595+
{
596+
attributes.append( fields.at( *attrIndexIt ).name() );
597+
}
598+
563599
QgsFeatureRequest freq = QgsFeatureRequest().setSubsetOfAttributes(
564-
renderer->usedAttributes(), vl->pendingFields() );
600+
attributes, vl->pendingFields() );
565601
if ( !mExtent.isEmpty() )
566602
{
567603
freq.setFilterRect( mExtent );
@@ -609,11 +645,18 @@ void QgsDxfExport::writeEntities()
609645
}
610646
addFeature( sctx, dxfLayerName( vl->name() ), s->symbolLayer( 0 ), s );
611647
}
648+
649+
if ( labelLayer )
650+
{
651+
labelEngine.registerFeature( vl, fet, ctx );
652+
}
612653
}
613654
}
614655
renderer->stopRender( ctx );
615656
}
616657

658+
labelEngine.drawLabeling( ctx );
659+
617660
endSection();
618661
}
619662

@@ -828,6 +871,20 @@ void QgsDxfExport::writeCircle( const QString& layer, int color, const QgsPoint&
828871
writeGroup( 40, radius );
829872
}
830873

874+
void QgsDxfExport::writeText( const QString& layer, const QString& text, const QgsPoint& pt, double size, double angle )
875+
{
876+
writeGroup( 0, "TEXT" );
877+
writeGroup( 8, layer );
878+
//todo: color with code 64
879+
writeGroup( 10, pt.x() );
880+
writeGroup( 20, pt.y() );
881+
writeGroup( 30, 0 );
882+
writeGroup( 40, size );
883+
writeGroup( 1, text );
884+
writeGroup( 50, angle );
885+
writeGroup( 7, "STANDARD" ); //so far only support for standard font
886+
}
887+
831888
void QgsDxfExport::writeSolid( const QString& layer, int color, const QgsPoint& pt1, const QgsPoint& pt2, const QgsPoint& pt3, const QgsPoint& pt4 )
832889
{
833890
writeGroup( 0, "SOLID" );

src/core/dxf/qgsdxfexport.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,13 @@ class CORE_EXPORT QgsDxfExport
8383

8484
void writeCircle( const QString& layer, int color, const QgsPoint& pt, double radius );
8585

86+
void writeText( const QString& layer, const QString& text, const QgsPoint& pt, double size, double angle );
87+
8688
static double mapUnitScaleFactor( double scaleDenominator, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits );
8789

90+
static QString dxfLayerName( const QString& name );
91+
92+
8893
private:
8994

9095
QList< QgsMapLayer* > mLayers;
@@ -159,7 +164,6 @@ class CORE_EXPORT QgsDxfExport
159164
double dashSeparatorSize() const;
160165
double sizeToMapUnits( double s ) const;
161166
static QString lineNameFromPenStyle( Qt::PenStyle style );
162-
static QString dxfLayerName( const QString& name );
163167
};
164168

165169
#endif // QGSDXFEXPORT_H

src/core/dxf/qgsdxfpallabeling.cpp

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/***************************************************************************
2+
qgsdxfpallabeling.cpp
3+
---------------------
4+
begin : January 2014
5+
copyright : (C) 2014 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+
#include "qgsdxfpallabeling.h"
19+
#include "qgsdxfexport.h"
20+
#include "qgsmaplayerregistry.h"
21+
#include "qgspalgeometry.h"
22+
#include "pal/pointset.h"
23+
#include "pal/labelposition.h"
24+
25+
using namespace pal;
26+
27+
QgsDxfPalLabeling::QgsDxfPalLabeling( QgsDxfExport* dxf, const QgsRectangle& bbox, double scale ): QgsPalLabeling(), mDxfExport( dxf )
28+
{
29+
mMapRenderer.setExtent( bbox );
30+
31+
//todo: adapt to other map units than meters
32+
int dpi = 96;
33+
double factor = 1000 * dpi / scale / 25.4;
34+
mMapRenderer.setOutputSize( QSizeF( bbox.width() * factor, bbox.height() * factor ), dpi );
35+
mMapRenderer.setScale( scale );
36+
//mMapRenderer.setLayer necessary?
37+
init( &mMapRenderer );
38+
39+
mImage = new QImage( 10, 10, QImage::Format_ARGB32_Premultiplied );
40+
mImage->setDotsPerMeterX( 96 / 25.4 * 1000 );
41+
mImage->setDotsPerMeterY( 96 / 25.4 * 1000 );
42+
mPainter = new QPainter( mImage );
43+
mRenderContext.setPainter( mPainter );
44+
mRenderContext.setRendererScale( scale );
45+
mRenderContext.setExtent( bbox );
46+
}
47+
48+
QgsDxfPalLabeling::~QgsDxfPalLabeling()
49+
{
50+
delete mPainter;
51+
delete mImage;
52+
}
53+
54+
void QgsDxfPalLabeling::drawLabel( pal::LabelPosition* label, QgsRenderContext& context, QgsPalLayerSettings& tmpLyr, DrawLabelType drawType, double dpiRatio )
55+
{
56+
//debug: print label infos
57+
if ( mDxfExport )
58+
{
59+
//label text
60+
QString text = (( QgsPalGeometry* )label->getFeaturePart()->getUserGeometry() )->text();
61+
62+
//layer name
63+
QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer( QString( label->getLayerName() ) );
64+
if ( !layer )
65+
{
66+
return;
67+
}
68+
QString layerName = mDxfExport->dxfLayerName( layer->name() );
69+
70+
//angle
71+
double angle = label->getAlpha() * 180 / M_PI;
72+
73+
mDxfExport->writeText( layerName, text, QgsPoint( label->getX(), label->getY() ), label->getHeight(), angle );
74+
}
75+
}

src/core/dxf/qgsdxfpallabeling.h

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/***************************************************************************
2+
qgsdxfpallabeling.h
3+
-------------------
4+
begin : January 2014
5+
copyright : (C) 2014 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 QGSDXFPALLABELING_H
19+
#define QGSDXFPALLABELING_H
20+
21+
#include "qgspallabeling.h"
22+
#include "qgsmaprenderer.h"
23+
#include "qgsrendercontext.h"
24+
25+
class QgsDxfExport;
26+
27+
class CORE_EXPORT QgsDxfPalLabeling: public QgsPalLabeling
28+
{
29+
public:
30+
QgsDxfPalLabeling( QgsDxfExport* dxf, const QgsRectangle& bbox, double scale );
31+
~QgsDxfPalLabeling();
32+
33+
QgsRenderContext& renderContext() { return mRenderContext; }
34+
void drawLabel( pal::LabelPosition* label, QgsRenderContext& context, QgsPalLayerSettings& tmpLyr, DrawLabelType drawType, double dpiRatio = 1.0 );
35+
36+
private:
37+
QgsDxfExport* mDxfExport;
38+
QgsMapRenderer mMapRenderer;
39+
QgsRenderContext mRenderContext;
40+
41+
//only used for render context
42+
QImage* mImage;
43+
QPainter* mPainter;
44+
};
45+
46+
#endif // QGSDXFPALLABELING_H

0 commit comments

Comments
 (0)