Skip to content
Permalink
Browse files

Export labels to dxf

  • Loading branch information
mhugent committed Jan 17, 2014
1 parent fb18038 commit a7a942ceda71ac560b363518e50b5c5f7032daa2
@@ -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
@@ -16,6 +16,7 @@
***************************************************************************/

#include "qgsdxfexport.h"
#include "qgsdxfpallabeling.h"
#include "qgsvectordataprovider.h"
#include "qgspoint.h"
#include "qgsrendererv2.h"
@@ -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();
}

@@ -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 )
@@ -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 );
@@ -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();
}

@@ -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" );
@@ -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;
@@ -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
@@ -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 );
}
}
@@ -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.
You can’t perform that action at this time.