Skip to content

Commit

Permalink
Export labels to dxf
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Jan 17, 2014
1 parent fb18038 commit a7a942c
Show file tree
Hide file tree
Showing 8 changed files with 344 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -170,6 +170,7 @@ SET(QGIS_CORE_SRCS
dxf/qgsdxfexport.cpp
dxf/qgsdxfpaintdevice.cpp
dxf/qgsdxfpaintengine.cpp
dxf/qgsdxfpallabeling.cpp

pal/costcalculator.cpp
pal/feature.cpp
Expand Down
63 changes: 60 additions & 3 deletions src/core/dxf/qgsdxfexport.cpp
Expand Up @@ -16,6 +16,7 @@
***************************************************************************/

#include "qgsdxfexport.h"
#include "qgsdxfpallabeling.h"
#include "qgsvectordataprovider.h"
#include "qgspoint.h"
#include "qgsrendererv2.h"
Expand Down Expand Up @@ -473,6 +474,26 @@ void QgsDxfExport::writeTables()
writeGroup( 6, "CONTINUOUS" );
}
writeGroup( 0, "ENDTAB" );

//STYLE
writeGroup( 0, "TABLE" );
writeGroup( 2, "STYLE" );
writeGroup( 70, 1 );

//provide only standard font for the moment
writeGroup( 0, "STYLE" );
writeGroup( 2, "STANDARD" );
writeGroup( 70, 64 );
writeGroup( 40, 0.0 );
writeGroup( 41, 1.0 );
writeGroup( 50, 0.0 );
writeGroup( 71, 0 );
writeGroup( 42, 5.0 );
writeGroup( 3, "romans.shx" );
writeGroup( 4, "" );

writeGroup( 0, "ENDTAB" );

endSection();
}

Expand Down Expand Up @@ -537,6 +558,10 @@ void QgsDxfExport::writeEntities()
startSection();
writeGroup( 2, "ENTITIES" );

//label engine
QgsDxfPalLabeling labelEngine( this, mExtent.isEmpty() ? dxfExtent() : mExtent, mSymbologyScaleDenominator );
QgsRenderContext& ctx = labelEngine.renderContext();

//iterate through the maplayers
QList< QgsMapLayer* >::iterator layerIt = mLayers.begin();
for ( ; layerIt != mLayers.end(); ++layerIt )
Expand All @@ -547,21 +572,32 @@ void QgsDxfExport::writeEntities()
continue;
}

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

//todo: call mLabeling.prepareLayer(...)
QSet<int> attrIndex;
bool labelLayer = ( labelEngine.prepareLayer( vl, attrIndex, ctx ) != 0 );

if ( mSymbologyExport == QgsDxfExport::SymbolLayerSymbology && renderer->usingSymbolLevels() )
{
writeEntitiesSymbolLevels( vl );
renderer->stopRender( ctx );
continue;
}

//combine renderer and label attributes
const QgsFields& fields = vl->pendingFields();
QList<QString> attributes = renderer->usedAttributes();
QSet<int>::const_iterator attrIndexIt = attrIndex.constBegin();
for ( ; attrIndexIt != attrIndex.constEnd(); ++attrIndexIt )
{
attributes.append( fields.at( *attrIndexIt ).name() );
}

QgsFeatureRequest freq = QgsFeatureRequest().setSubsetOfAttributes(
renderer->usedAttributes(), vl->pendingFields() );
attributes, vl->pendingFields() );
if ( !mExtent.isEmpty() )
{
freq.setFilterRect( mExtent );
Expand Down Expand Up @@ -609,11 +645,18 @@ void QgsDxfExport::writeEntities()
}
addFeature( sctx, dxfLayerName( vl->name() ), s->symbolLayer( 0 ), s );
}

if ( labelLayer )
{
labelEngine.registerFeature( vl, fet, ctx );
}
}
}
renderer->stopRender( ctx );
}

labelEngine.drawLabeling( ctx );

endSection();
}

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

void QgsDxfExport::writeText( const QString& layer, const QString& text, const QgsPoint& pt, double size, double angle )
{
writeGroup( 0, "TEXT" );
writeGroup( 8, layer );
//todo: color with code 64
writeGroup( 10, pt.x() );
writeGroup( 20, pt.y() );
writeGroup( 30, 0 );
writeGroup( 40, size );
writeGroup( 1, text );
writeGroup( 50, angle );
writeGroup( 7, "STANDARD" ); //so far only support for standard font
}

void QgsDxfExport::writeSolid( const QString& layer, int color, const QgsPoint& pt1, const QgsPoint& pt2, const QgsPoint& pt3, const QgsPoint& pt4 )
{
writeGroup( 0, "SOLID" );
Expand Down
6 changes: 5 additions & 1 deletion src/core/dxf/qgsdxfexport.h
Expand Up @@ -83,8 +83,13 @@ class CORE_EXPORT QgsDxfExport

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

void writeText( const QString& layer, const QString& text, const QgsPoint& pt, double size, double angle );

static double mapUnitScaleFactor( double scaleDenominator, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits );

static QString dxfLayerName( const QString& name );


private:

QList< QgsMapLayer* > mLayers;
Expand Down Expand Up @@ -159,7 +164,6 @@ class CORE_EXPORT QgsDxfExport
double dashSeparatorSize() const;
double sizeToMapUnits( double s ) const;
static QString lineNameFromPenStyle( Qt::PenStyle style );
static QString dxfLayerName( const QString& name );
};

#endif // QGSDXFEXPORT_H
75 changes: 75 additions & 0 deletions src/core/dxf/qgsdxfpallabeling.cpp
@@ -0,0 +1,75 @@
/***************************************************************************
qgsdxfpallabeling.cpp
---------------------
begin : January 2014
copyright : (C) 2014 by Marco Hugentobler
email : marco at sourcepole dot ch
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsdxfpallabeling.h"
#include "qgsdxfexport.h"
#include "qgsmaplayerregistry.h"
#include "qgspalgeometry.h"
#include "pal/pointset.h"
#include "pal/labelposition.h"

using namespace pal;

QgsDxfPalLabeling::QgsDxfPalLabeling( QgsDxfExport* dxf, const QgsRectangle& bbox, double scale ): QgsPalLabeling(), mDxfExport( dxf )
{
mMapRenderer.setExtent( bbox );

//todo: adapt to other map units than meters
int dpi = 96;
double factor = 1000 * dpi / scale / 25.4;
mMapRenderer.setOutputSize( QSizeF( bbox.width() * factor, bbox.height() * factor ), dpi );
mMapRenderer.setScale( scale );
//mMapRenderer.setLayer necessary?
init( &mMapRenderer );

mImage = new QImage( 10, 10, QImage::Format_ARGB32_Premultiplied );
mImage->setDotsPerMeterX( 96 / 25.4 * 1000 );
mImage->setDotsPerMeterY( 96 / 25.4 * 1000 );
mPainter = new QPainter( mImage );
mRenderContext.setPainter( mPainter );
mRenderContext.setRendererScale( scale );
mRenderContext.setExtent( bbox );
}

QgsDxfPalLabeling::~QgsDxfPalLabeling()
{
delete mPainter;
delete mImage;
}

void QgsDxfPalLabeling::drawLabel( pal::LabelPosition* label, QgsRenderContext& context, QgsPalLayerSettings& tmpLyr, DrawLabelType drawType, double dpiRatio )
{
//debug: print label infos
if ( mDxfExport )
{
//label text
QString text = (( QgsPalGeometry* )label->getFeaturePart()->getUserGeometry() )->text();

//layer name
QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer( QString( label->getLayerName() ) );
if ( !layer )
{
return;
}
QString layerName = mDxfExport->dxfLayerName( layer->name() );

//angle
double angle = label->getAlpha() * 180 / M_PI;

mDxfExport->writeText( layerName, text, QgsPoint( label->getX(), label->getY() ), label->getHeight(), angle );
}
}
46 changes: 46 additions & 0 deletions src/core/dxf/qgsdxfpallabeling.h
@@ -0,0 +1,46 @@
/***************************************************************************
qgsdxfpallabeling.h
-------------------
begin : January 2014
copyright : (C) 2014 by Marco Hugentobler
email : marco at sourcepole dot ch
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSDXFPALLABELING_H
#define QGSDXFPALLABELING_H

#include "qgspallabeling.h"
#include "qgsmaprenderer.h"
#include "qgsrendercontext.h"

class QgsDxfExport;

class CORE_EXPORT QgsDxfPalLabeling: public QgsPalLabeling
{
public:
QgsDxfPalLabeling( QgsDxfExport* dxf, const QgsRectangle& bbox, double scale );
~QgsDxfPalLabeling();

QgsRenderContext& renderContext() { return mRenderContext; }
void drawLabel( pal::LabelPosition* label, QgsRenderContext& context, QgsPalLayerSettings& tmpLyr, DrawLabelType drawType, double dpiRatio = 1.0 );

private:
QgsDxfExport* mDxfExport;
QgsMapRenderer mMapRenderer;
QgsRenderContext mRenderContext;

//only used for render context
QImage* mImage;
QPainter* mPainter;
};

#endif // QGSDXFPALLABELING_H

0 comments on commit a7a942c

Please sign in to comment.