Skip to content

Commit d5cfe23

Browse files
committedNov 20, 2013
Write blocks for point symbols
1 parent e97db8c commit d5cfe23

File tree

5 files changed

+139
-2
lines changed

5 files changed

+139
-2
lines changed
 

‎src/core/qgsdxfexport.cpp

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ double QgsDxfExport::mDxfColors[][3] =
286286
{1, 1, 1} // 255
287287
};
288288

289-
QgsDxfExport::QgsDxfExport(): mSymbologyScaleDenominator( 1.0 ), mSymbologyExport( NoSymbology ), mMapUnits( QGis::Meters ), mSymbolLayerCounter( 0 ), mNextHandleId( 10 )
289+
QgsDxfExport::QgsDxfExport(): mSymbologyScaleDenominator( 1.0 ), mSymbologyExport( NoSymbology ), mMapUnits( QGis::Meters ), mSymbolLayerCounter( 0 ), mNextHandleId( 10 ), mBlockCounter( 0 )
290290
{
291291
}
292292

@@ -310,6 +310,7 @@ int QgsDxfExport::writeToFile( QIODevice* d )
310310
QTextStream outStream( d );
311311
writeHeader( outStream );
312312
writeTables( outStream );
313+
writeBlocks( outStream );
313314
writeEntities( outStream );
314315
writeEndFile( outStream );
315316
return 0;
@@ -451,6 +452,63 @@ void QgsDxfExport::writeTables( QTextStream& stream )
451452
endSection( stream );
452453
}
453454

455+
void QgsDxfExport::writeBlocks( QTextStream& stream )
456+
{
457+
startSection( stream );
458+
stream << " 2\n";
459+
stream << "BLOCKS\n";
460+
461+
//iterate through all layers and get symbol layer pointers
462+
QList<QgsSymbolLayerV2*> slList;
463+
if ( mSymbologyExport != NoSymbology )
464+
{
465+
slList = symbolLayers();
466+
}
467+
468+
QList<QgsSymbolLayerV2*>::const_iterator slIt = slList.constBegin();
469+
for ( ; slIt != slList.constEnd(); ++slIt )
470+
{
471+
//if point symbol layer and no data defined properties: write block
472+
QgsMarkerSymbolLayerV2* ml = dynamic_cast< QgsMarkerSymbolLayerV2*>( *slIt );
473+
if ( ml )
474+
{
475+
//todo: find out if the marker symbol layer has data defined properties
476+
stream << " 0\n";
477+
stream << "BLOCK\n";
478+
stream << " 8\n"; //Layer (0 to take layer where INSERT happens)
479+
stream << "0\n";
480+
QString blockName = QString( "symbolLayer%1" ).arg( mBlockCounter );
481+
stream << " 2\n";
482+
stream << QString( "%1\n" ).arg( blockName );
483+
stream << " 70\n";
484+
stream << "64\n";
485+
486+
//x/y/z coordinates of reference point
487+
//todo: consider anchor point
488+
double size = ml->size();
489+
size *= mapUnitScaleFactor( mSymbologyScaleDenominator, ml->sizeUnit(), mMapUnits );
490+
stream << "10\n";
491+
stream << QString( "%1\n" ).arg( size / 2.0 );
492+
stream << "20\n";
493+
stream << QString( "%1\n" ).arg( size / 2.0 );
494+
stream << "30\n";
495+
stream << "0\n";
496+
stream << " 3\n";
497+
stream << QString( "%1\n" ).arg( blockName );
498+
499+
ml->writeDxf( stream, mapUnitScaleFactor( mSymbologyScaleDenominator, ml->sizeUnit(), mMapUnits ) );
500+
501+
stream << " 0\n";
502+
stream << "ENDBLK\n";
503+
stream << " 8\n";
504+
stream << "0\n";
505+
506+
mPointSymbolBlocks.insert( ml, blockName );
507+
}
508+
}
509+
endSection( stream );
510+
}
511+
454512
void QgsDxfExport::writeEntities( QTextStream& stream )
455513
{
456514
startSection( stream );
@@ -617,6 +675,32 @@ void QgsDxfExport::endSection( QTextStream& stream )
617675
stream << "ENDSEC\n";
618676
}
619677

678+
void QgsDxfExport::writePoint( QTextStream& stream, const QgsPoint& pt, const QString& layer, const QgsSymbolLayerV2* symbolLayer )
679+
{
680+
//insert block or write point directly?
681+
QHash< const QgsSymbolLayerV2*, QString >::const_iterator blockIt = mPointSymbolBlocks.find( symbolLayer );
682+
if ( !symbolLayer || blockIt == mPointSymbolBlocks.constEnd() )
683+
{
684+
//write symbol directly here
685+
}
686+
else
687+
{
688+
//insert block
689+
stream << " 0\n";
690+
stream << "INSERT\n";
691+
stream << " 8\n";
692+
stream << layer << "\n";
693+
stream << " 2\n";
694+
stream << blockIt.value() << "\n";
695+
stream << " 10\n";
696+
stream << QString( "%1\n" ).arg( pt.x() );
697+
stream << " 20\n";
698+
stream << QString( "%1\n" ).arg( pt.y() );
699+
stream << " 30\n";
700+
stream << "0\n";
701+
}
702+
}
703+
620704
void QgsDxfExport::writePolyline( QTextStream& stream, const QgsPolyline& line, const QString& layer, const QString& lineStyleName, int color,
621705
double width, bool polygon )
622706
{
@@ -700,6 +784,13 @@ void QgsDxfExport::addFeature( const QgsFeature& fet, QTextStream& stream, const
700784
//todo: write point symbols as blocks
701785

702786
QGis::WkbType geometryType = geom->wkbType();
787+
788+
//single point
789+
if ( geometryType == QGis::WKBPoint || geometryType == QGis::WKBPoint25D )
790+
{
791+
writePoint( stream, geom->asPoint(), layer, symbolLayer );
792+
}
793+
703794
//single line
704795
if ( geometryType == QGis::WKBLineString || geometryType == QGis::WKBLineString25D )
705796
{

‎src/core/qgsdxfexport.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ class QgsDxfExport
5454
void setSymbologyExport( SymbologyExport e ) { mSymbologyExport = e; }
5555
SymbologyExport symbologyExport() const { return mSymbologyExport; }
5656

57+
//get closest entry in dxf palette
58+
static int closestColorMatch( QRgb pixel );
59+
5760
private:
5861

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

6972
int mSymbolLayerCounter; //internal counter
7073
int mNextHandleId;
74+
int mBlockCounter;
75+
7176
QHash< const QgsSymbolLayerV2*, QString > mLineStyles; //symbol layer name types
77+
QHash< const QgsSymbolLayerV2*, QString > mPointSymbolBlocks; //reference to point symbol blocks
7278

7379
//AC1009
7480
void writeHeader( QTextStream& stream );
7581
void writeTables( QTextStream& stream );
82+
void writeBlocks( QTextStream& stream );
7683
void writeEntities( QTextStream& stream );
7784
void writeEntitiesSymbolLevels( QTextStream& stream, QgsVectorLayer* layer );
7885
void writeEndFile( QTextStream& stream );
7986

8087
void startSection( QTextStream& stream );
8188
void endSection( QTextStream& stream );
8289

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

110118
//functions for dxf palette
111-
static int closestColorMatch( QRgb pixel );
112119
static int color_distance( QRgb p1, int index );
113120
static QRgb createRgbEntry( qreal r, qreal g, qreal b );
114121

‎src/core/symbology-ng/qgsmarkersymbollayerv2.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "qgsmarkersymbollayerv2.h"
1717
#include "qgssymbollayerv2utils.h"
1818

19+
#include "qgsdxfexport.h"
1920
#include "qgsexpression.h"
2021
#include "qgsrendercontext.h"
2122
#include "qgslogger.h"
@@ -700,6 +701,40 @@ void QgsSimpleMarkerSymbolLayerV2::drawMarker( QPainter* p, QgsSymbolV2RenderCon
700701
}
701702
}
702703

704+
void QgsSimpleMarkerSymbolLayerV2::writeDxf( QTextStream& str, double mmMapUnitScaleFactor ) const
705+
{
706+
double size = mSize;
707+
if ( mSizeUnit == QgsSymbolV2::MM )
708+
{
709+
size *= mmMapUnitScaleFactor;
710+
}
711+
double halfSize = size / 2.0;
712+
713+
if ( mName == "circle" )
714+
{
715+
str << " 0\n";
716+
str << "CIRCLE\n";
717+
str << " 8\n";
718+
str << "0\n";
719+
//todo: linetype in group 6. Needs to be inserted into line table first
720+
721+
//color in group 62
722+
str << " 62\n";
723+
int colorIndex = QgsDxfExport::closestColorMatch( mBrush.color().rgb() );
724+
str << QString( "%1\n" ).arg( colorIndex );
725+
726+
//x/y/z center
727+
str << " 10\n";
728+
str << QString( "%1\n" ).arg( halfSize );
729+
str << " 20\n";
730+
str << QString( "%1\n" ).arg( halfSize );
731+
str << " 30\n";
732+
str << QString( "%1\n" ).arg( halfSize );
733+
str << " 40\n";
734+
str << QString( "%1\n" ).arg( halfSize );
735+
}
736+
}
737+
703738
//////////
704739

705740

‎src/core/symbology-ng/qgsmarkersymbollayerv2.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ class CORE_EXPORT QgsSimpleMarkerSymbolLayerV2 : public QgsMarkerSymbolLayerV2
7676
QgsSymbolV2::OutputUnit outlineWidthUnit() const { return mOutlineWidthUnit; }
7777
void setOutlineWidthUnit( QgsSymbolV2::OutputUnit u ) { mOutlineWidthUnit = u; }
7878

79+
void writeDxf( QTextStream& str, double mmMapUnitScaleFactor ) const;
80+
7981
protected:
8082

8183
void drawMarker( QPainter* p, QgsSymbolV2RenderContext& context );

‎src/core/symbology-ng/qgssymbollayerv2.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ class CORE_EXPORT QgsSymbolLayerV2
9292
virtual void removeDataDefinedProperty( const QString& property );
9393
virtual void removeDataDefinedProperties();
9494

95+
virtual void writeDxf( QTextStream& str, double mmMapUnitScaleFactor ) const { Q_UNUSED( str ); Q_UNUSED( mmMapUnitScaleFactor ); }
96+
9597
protected:
9698
QgsSymbolLayerV2( QgsSymbolV2::SymbolType type, bool locked = false )
9799
: mType( type ), mLocked( locked ), mRenderingPass( 0 ) {}

0 commit comments

Comments
 (0)
Please sign in to comment.