Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
First implementation of dxf feature writing (but it does not work yet)
  • Loading branch information
mhugent committed Sep 13, 2013
1 parent edc3b5f commit ddee21c
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 2 deletions.
83 changes: 82 additions & 1 deletion src/core/qgsdxfexport.cpp
Expand Up @@ -16,6 +16,10 @@
***************************************************************************/

#include "qgsdxfexport.h"
#include "qgsvectordataprovider.h"
#include "qgspoint.h"
#include "qgsrendererv2.h"
#include "qgsvectorlayer.h"
#include <QIODevice>
#include <QTextStream>

Expand All @@ -28,7 +32,7 @@ QgsDxfExport::~QgsDxfExport()

}

int QgsDxfExport::writeToFile( QIODevice* d )
int QgsDxfExport::writeToFile( QIODevice* d, SymbologyExport s )
{
if ( !d )
{
Expand All @@ -42,6 +46,8 @@ int QgsDxfExport::writeToFile( QIODevice* d )

QTextStream outStream( d );
writeHeader( outStream );
writeEntities( outStream );
writeEndFile( outStream );
return 0;
}

Expand All @@ -61,8 +67,83 @@ void QgsDxfExport::writeHeader( QTextStream& stream )
stream << "ENDSEC\n";
}

void QgsDxfExport::writeEntities( QTextStream& stream )
{
stream << "0\n";
stream << "SECTION\n";
stream << " 2\n";
stream << "ENTITIES\n";

//iterate through the maplayers
QList< QgsMapLayer* >::iterator layerIt = mLayers.begin();
for ( ; layerIt != mLayers.end(); ++layerIt )
{
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( *layerIt );
if ( !vl )
{
continue;
}
QgsVectorDataProvider* dp = vl->dataProvider();
if ( !dp )
{
continue;
}

QgsFeatureRendererV2* renderer = vl->rendererV2();
QgsFeatureIterator featureIt = vl->getFeatures( QgsFeatureRequest().setSubsetOfAttributes(
renderer->usedAttributes(), dp->fields() ) );
QgsFeature fet;
while ( featureIt.nextFeature( fet ) )
{
//get geometry and write it. Todo: consider symbolisation
QgsGeometry* geom = fet.geometry();
if ( geom )
{
//try with line first
writePolyline( stream, geom->asPolyline(), vl->name() ); //todo.......
}
}
}

stream << " 0\n";
stream << "ENDSEC\n";
}

void QgsDxfExport::writeEndFile( QTextStream& stream )
{
stream << " 0\n";
stream << "ENDSEC\n";
}

void QgsDxfExport::writePolyline( QTextStream& stream, const QgsPolyline& line, const QString& layer, bool closed )
{
stream << " 0\n";
stream << "POLYLINE\n";
stream << " 8\n";
stream << layer << "\n";
stream << " 66\n";
stream << " 1\n";
stream << " 70\n";
int type = closed ? 32 : 0;
stream << type << "\n";

QgsPolyline::const_iterator lineIt = line.constBegin();
for ( ; lineIt != line.constEnd(); ++lineIt )
{
writeVertex( stream, *lineIt, layer );
}
}

void QgsDxfExport::writeVertex( QTextStream& stream, const QgsPoint& pt, const QString& layer )
{
stream << " 0\n";
stream << "VERTEX\n";
stream << " 8\n";
stream << layer << "\n";
stream << " 10\n";
stream << pt.x() << "\n";
stream << " 20\n";
stream << pt.y() << "\n";
stream << " 30\n";
stream << "0.0\n";
}
16 changes: 15 additions & 1 deletion src/core/qgsdxfexport.h
Expand Up @@ -18,27 +18,41 @@
#ifndef QGSDXFEXPORT_H
#define QGSDXFEXPORT_H

#include "qgsgeometry.h"
#include <QList>

class QgsMapLayer;
class QgsPoint;
class QIODevice;
class QTextStream;

class QgsDxfExport
{
public:
enum SymbologyExport
{
NoSymbology = 0, //export only data
FeatureSymbology, //Keeps the number of features and export symbology per feature
SymbolLayerSymbology //Exports one feature per symbol layer (considering symbol levels)
};

QgsDxfExport();
~QgsDxfExport();

void addLayers( QList< QgsMapLayer* >& layers ) { mLayers = layers; }
int writeToFile( QIODevice* d ); //maybe add progress dialog? //other parameters (e.g. scale, dpi)?
int writeToFile( QIODevice* d, SymbologyExport s = SymbolLayerSymbology ); //maybe add progress dialog? //other parameters (e.g. scale, dpi)?

private:

QList< QgsMapLayer* > mLayers;

void writeHeader( QTextStream& stream );
void writeEntities( QTextStream& stream );
void writeEndFile( QTextStream& stream );

void writePolyline( QTextStream& stream, const QgsPolyline& line, const QString& layer, bool closed = false );
void writeVertex( QTextStream& stream, const QgsPoint& pt, const QString& layer );

//collect styles
//writeEntities

Expand Down

0 comments on commit ddee21c

Please sign in to comment.