Skip to content

Commit

Permalink
Write blocks for point symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Nov 20, 2013
1 parent e97db8c commit d5cfe23
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 2 deletions.
93 changes: 92 additions & 1 deletion src/core/qgsdxfexport.cpp
Expand Up @@ -286,7 +286,7 @@ double QgsDxfExport::mDxfColors[][3] =
{1, 1, 1} // 255
};

QgsDxfExport::QgsDxfExport(): mSymbologyScaleDenominator( 1.0 ), mSymbologyExport( NoSymbology ), mMapUnits( QGis::Meters ), mSymbolLayerCounter( 0 ), mNextHandleId( 10 )
QgsDxfExport::QgsDxfExport(): mSymbologyScaleDenominator( 1.0 ), mSymbologyExport( NoSymbology ), mMapUnits( QGis::Meters ), mSymbolLayerCounter( 0 ), mNextHandleId( 10 ), mBlockCounter( 0 )
{
}

Expand All @@ -310,6 +310,7 @@ int QgsDxfExport::writeToFile( QIODevice* d )
QTextStream outStream( d );
writeHeader( outStream );
writeTables( outStream );
writeBlocks( outStream );
writeEntities( outStream );
writeEndFile( outStream );
return 0;
Expand Down Expand Up @@ -451,6 +452,63 @@ void QgsDxfExport::writeTables( QTextStream& stream )
endSection( stream );
}

void QgsDxfExport::writeBlocks( QTextStream& stream )
{
startSection( stream );
stream << " 2\n";
stream << "BLOCKS\n";

//iterate through all layers and get symbol layer pointers
QList<QgsSymbolLayerV2*> slList;
if ( mSymbologyExport != NoSymbology )
{
slList = symbolLayers();
}

QList<QgsSymbolLayerV2*>::const_iterator slIt = slList.constBegin();
for ( ; slIt != slList.constEnd(); ++slIt )
{
//if point symbol layer and no data defined properties: write block
QgsMarkerSymbolLayerV2* ml = dynamic_cast< QgsMarkerSymbolLayerV2*>( *slIt );
if ( ml )
{
//todo: find out if the marker symbol layer has data defined properties
stream << " 0\n";
stream << "BLOCK\n";
stream << " 8\n"; //Layer (0 to take layer where INSERT happens)
stream << "0\n";
QString blockName = QString( "symbolLayer%1" ).arg( mBlockCounter );
stream << " 2\n";
stream << QString( "%1\n" ).arg( blockName );
stream << " 70\n";
stream << "64\n";

//x/y/z coordinates of reference point
//todo: consider anchor point
double size = ml->size();
size *= mapUnitScaleFactor( mSymbologyScaleDenominator, ml->sizeUnit(), mMapUnits );
stream << "10\n";
stream << QString( "%1\n" ).arg( size / 2.0 );
stream << "20\n";
stream << QString( "%1\n" ).arg( size / 2.0 );
stream << "30\n";
stream << "0\n";
stream << " 3\n";
stream << QString( "%1\n" ).arg( blockName );

ml->writeDxf( stream, mapUnitScaleFactor( mSymbologyScaleDenominator, ml->sizeUnit(), mMapUnits ) );

stream << " 0\n";
stream << "ENDBLK\n";
stream << " 8\n";
stream << "0\n";

mPointSymbolBlocks.insert( ml, blockName );
}
}
endSection( stream );
}

void QgsDxfExport::writeEntities( QTextStream& stream )
{
startSection( stream );
Expand Down Expand Up @@ -617,6 +675,32 @@ void QgsDxfExport::endSection( QTextStream& stream )
stream << "ENDSEC\n";
}

void QgsDxfExport::writePoint( QTextStream& stream, const QgsPoint& pt, const QString& layer, const QgsSymbolLayerV2* symbolLayer )
{
//insert block or write point directly?
QHash< const QgsSymbolLayerV2*, QString >::const_iterator blockIt = mPointSymbolBlocks.find( symbolLayer );
if ( !symbolLayer || blockIt == mPointSymbolBlocks.constEnd() )
{
//write symbol directly here
}
else
{
//insert block
stream << " 0\n";
stream << "INSERT\n";
stream << " 8\n";
stream << layer << "\n";
stream << " 2\n";
stream << blockIt.value() << "\n";
stream << " 10\n";
stream << QString( "%1\n" ).arg( pt.x() );
stream << " 20\n";
stream << QString( "%1\n" ).arg( pt.y() );
stream << " 30\n";
stream << "0\n";
}
}

void QgsDxfExport::writePolyline( QTextStream& stream, const QgsPolyline& line, const QString& layer, const QString& lineStyleName, int color,
double width, bool polygon )
{
Expand Down Expand Up @@ -700,6 +784,13 @@ void QgsDxfExport::addFeature( const QgsFeature& fet, QTextStream& stream, const
//todo: write point symbols as blocks

QGis::WkbType geometryType = geom->wkbType();

//single point
if ( geometryType == QGis::WKBPoint || geometryType == QGis::WKBPoint25D )
{
writePoint( stream, geom->asPoint(), layer, symbolLayer );
}

//single line
if ( geometryType == QGis::WKBLineString || geometryType == QGis::WKBLineString25D )
{
Expand Down
9 changes: 8 additions & 1 deletion src/core/qgsdxfexport.h
Expand Up @@ -54,6 +54,9 @@ class QgsDxfExport
void setSymbologyExport( SymbologyExport e ) { mSymbologyExport = e; }
SymbologyExport symbologyExport() const { return mSymbologyExport; }

//get closest entry in dxf palette
static int closestColorMatch( QRgb pixel );

private:

QList< QgsMapLayer* > mLayers;
Expand All @@ -68,18 +71,23 @@ class QgsDxfExport

int mSymbolLayerCounter; //internal counter
int mNextHandleId;
int mBlockCounter;

QHash< const QgsSymbolLayerV2*, QString > mLineStyles; //symbol layer name types
QHash< const QgsSymbolLayerV2*, QString > mPointSymbolBlocks; //reference to point symbol blocks

//AC1009
void writeHeader( QTextStream& stream );
void writeTables( QTextStream& stream );
void writeBlocks( QTextStream& stream );
void writeEntities( QTextStream& stream );
void writeEntitiesSymbolLevels( QTextStream& stream, QgsVectorLayer* layer );
void writeEndFile( QTextStream& stream );

void startSection( QTextStream& stream );
void endSection( QTextStream& stream );

void writePoint( QTextStream& stream, const QgsPoint& pt, const QString& layer, const QgsSymbolLayerV2* symbolLayer );
void writePolyline( QTextStream& stream, const QgsPolyline& line, const QString& layer, const QString& lineStyleName, int color,
double width = -1, bool polygon = false );
void writeVertex( QTextStream& stream, const QgsPoint& pt, const QString& layer );
Expand Down Expand Up @@ -108,7 +116,6 @@ class QgsDxfExport
double widthFromSymbolLayer( const QgsSymbolLayerV2* symbolLayer );

//functions for dxf palette
static int closestColorMatch( QRgb pixel );
static int color_distance( QRgb p1, int index );
static QRgb createRgbEntry( qreal r, qreal g, qreal b );

Expand Down
35 changes: 35 additions & 0 deletions src/core/symbology-ng/qgsmarkersymbollayerv2.cpp
Expand Up @@ -16,6 +16,7 @@
#include "qgsmarkersymbollayerv2.h"
#include "qgssymbollayerv2utils.h"

#include "qgsdxfexport.h"
#include "qgsexpression.h"
#include "qgsrendercontext.h"
#include "qgslogger.h"
Expand Down Expand Up @@ -700,6 +701,40 @@ void QgsSimpleMarkerSymbolLayerV2::drawMarker( QPainter* p, QgsSymbolV2RenderCon
}
}

void QgsSimpleMarkerSymbolLayerV2::writeDxf( QTextStream& str, double mmMapUnitScaleFactor ) const
{
double size = mSize;
if ( mSizeUnit == QgsSymbolV2::MM )
{
size *= mmMapUnitScaleFactor;
}
double halfSize = size / 2.0;

if ( mName == "circle" )
{
str << " 0\n";
str << "CIRCLE\n";
str << " 8\n";
str << "0\n";
//todo: linetype in group 6. Needs to be inserted into line table first

//color in group 62
str << " 62\n";
int colorIndex = QgsDxfExport::closestColorMatch( mBrush.color().rgb() );
str << QString( "%1\n" ).arg( colorIndex );

//x/y/z center
str << " 10\n";
str << QString( "%1\n" ).arg( halfSize );
str << " 20\n";
str << QString( "%1\n" ).arg( halfSize );
str << " 30\n";
str << QString( "%1\n" ).arg( halfSize );
str << " 40\n";
str << QString( "%1\n" ).arg( halfSize );
}
}

//////////


Expand Down
2 changes: 2 additions & 0 deletions src/core/symbology-ng/qgsmarkersymbollayerv2.h
Expand Up @@ -76,6 +76,8 @@ class CORE_EXPORT QgsSimpleMarkerSymbolLayerV2 : public QgsMarkerSymbolLayerV2
QgsSymbolV2::OutputUnit outlineWidthUnit() const { return mOutlineWidthUnit; }
void setOutlineWidthUnit( QgsSymbolV2::OutputUnit u ) { mOutlineWidthUnit = u; }

void writeDxf( QTextStream& str, double mmMapUnitScaleFactor ) const;

protected:

void drawMarker( QPainter* p, QgsSymbolV2RenderContext& context );
Expand Down
2 changes: 2 additions & 0 deletions src/core/symbology-ng/qgssymbollayerv2.h
Expand Up @@ -92,6 +92,8 @@ class CORE_EXPORT QgsSymbolLayerV2
virtual void removeDataDefinedProperty( const QString& property );
virtual void removeDataDefinedProperties();

virtual void writeDxf( QTextStream& str, double mmMapUnitScaleFactor ) const { Q_UNUSED( str ); Q_UNUSED( mmMapUnitScaleFactor ); }

protected:
QgsSymbolLayerV2( QgsSymbolV2::SymbolType type, bool locked = false )
: mType( type ), mLocked( locked ), mRenderingPass( 0 ) {}
Expand Down

0 comments on commit d5cfe23

Please sign in to comment.