Skip to content

Commit 09103b5

Browse files
committedNov 13, 2013
First attempt to write AC1018
1 parent 421c030 commit 09103b5

File tree

2 files changed

+339
-9
lines changed

2 files changed

+339
-9
lines changed
 

‎src/core/qgsdxfexport.cpp

Lines changed: 321 additions & 7 deletions
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 )
289+
QgsDxfExport::QgsDxfExport(): mSymbologyScaleDenominator( 1.0 ), mSymbologyExport( NoSymbology ), mMapUnits( QGis::Meters ), mSymbolLayerCounter( 0 ), mNextHandleId( 10 )
290290
{
291291
}
292292

@@ -308,8 +308,8 @@ int QgsDxfExport::writeToFile( QIODevice* d )
308308
}
309309

310310
QTextStream outStream( d );
311-
writeHeader( outStream );
312-
writeTables( outStream );
311+
writeHeaderAC1018( outStream );
312+
writeTablesAC1018( outStream );
313313
writeEntities( outStream );
314314
writeEndFile( outStream );
315315
return 0;
@@ -703,7 +703,7 @@ void QgsDxfExport::addFeature( const QgsFeature& fet, QTextStream& stream, const
703703
//single line
704704
if ( geometryType == QGis::WKBLineString || geometryType == QGis::WKBLineString25D )
705705
{
706-
writePolyline( stream, geom->asPolyline(), layer, lineStyleName, c, width, false );
706+
writePolylineAC1018( stream, geom->asPolyline(), layer, lineStyleName, c, width, false );
707707
}
708708

709709
//multiline
@@ -713,7 +713,7 @@ void QgsDxfExport::addFeature( const QgsFeature& fet, QTextStream& stream, const
713713
QgsMultiPolyline::const_iterator lIt = multiLine.constBegin();
714714
for ( ; lIt != multiLine.constEnd(); ++lIt )
715715
{
716-
writePolyline( stream, *lIt, layer, lineStyleName, c, width, false );
716+
writePolylineAC1018( stream, *lIt, layer, lineStyleName, c, width, false );
717717
}
718718
}
719719

@@ -724,7 +724,7 @@ void QgsDxfExport::addFeature( const QgsFeature& fet, QTextStream& stream, const
724724
QgsPolygon::const_iterator polyIt = polygon.constBegin();
725725
for ( ; polyIt != polygon.constEnd(); ++polyIt ) //iterate over rings
726726
{
727-
writePolyline( stream, *polyIt, layer, lineStyleName, c, width, true );
727+
writePolylineAC1018( stream, *polyIt, layer, lineStyleName, c, width, true );
728728
}
729729
}
730730

@@ -738,7 +738,7 @@ void QgsDxfExport::addFeature( const QgsFeature& fet, QTextStream& stream, const
738738
QgsPolygon::const_iterator polyIt = mpIt->constBegin();
739739
for ( ; polyIt != mpIt->constEnd(); ++polyIt )
740740
{
741-
writePolyline( stream, *polyIt, layer, lineStyleName, c, width, true );
741+
writePolylineAC1018( stream, *polyIt, layer, lineStyleName, c, width, true );
742742
}
743743
}
744744
}
@@ -1001,3 +1001,317 @@ void QgsDxfExport::writeLinestyle( QTextStream& stream, const QString& styleName
10011001
isSpace = !isSpace;
10021002
}
10031003
}
1004+
1005+
/******************************************************AC_1018 methods***************************************************************/
1006+
1007+
void QgsDxfExport::writeHeaderAC1018( QTextStream& stream )
1008+
{
1009+
stream << "999\n";
1010+
stream << "DXF created from QGIS\n";
1011+
startSection( stream );
1012+
stream << " 2\n";
1013+
stream << "HEADER\n";
1014+
//ACADVER
1015+
stream << " 9\n";
1016+
stream << "$ACADVER\n";
1017+
stream << " 1\n";
1018+
stream << "AC1018\n";
1019+
1020+
QgsRectangle ext = dxfExtent();
1021+
if ( !ext.isEmpty() )
1022+
{
1023+
//EXTMIN
1024+
stream << " 9\n";
1025+
stream << "$EXTMIN\n";
1026+
stream << " 10\n";
1027+
stream << ext.xMinimum() << "\n";
1028+
stream << " 20\n";
1029+
stream << ext.yMinimum() << "\n";
1030+
stream << " 30\n";
1031+
stream << "0\n";
1032+
//EXTMAX
1033+
stream << " 9\n";
1034+
stream << "$EXTMAX\n";
1035+
stream << " 10\n";
1036+
stream << ext.xMaximum() << "\n";
1037+
stream << " 20\n";
1038+
stream << ext.yMaximum() << "\n";
1039+
stream << " 30\n";
1040+
stream << "0\n";
1041+
}
1042+
//LTSCALE
1043+
stream << " 9\n";
1044+
stream << "$LTSCALE\n";
1045+
stream << " 40\n";
1046+
stream << "1.0\n";
1047+
//PDMODE
1048+
stream << " 9\n";
1049+
stream << "$PDMODE\n";
1050+
stream << " 70\n";
1051+
stream << "33\n";
1052+
//PDSIZE
1053+
stream << " 9\n";
1054+
stream << "$PDSIZE\n";
1055+
stream << " 40\n";
1056+
stream << "1\n";
1057+
endSection( stream );
1058+
//PSLTSCALE
1059+
stream << " 9\n";
1060+
stream << "$PSLTSCALE\n";
1061+
stream << " 70\n";
1062+
stream << "0\n";
1063+
}
1064+
1065+
void QgsDxfExport::writeTablesAC1018( QTextStream& stream )
1066+
{
1067+
startSection( stream );
1068+
stream << " 2\n";
1069+
stream << "TABLES\n";
1070+
1071+
//todo: VPORT table
1072+
1073+
//iterate through all layers and get symbol layer pointers
1074+
QList<QgsSymbolLayerV2*> slList;
1075+
if ( mSymbologyExport != NoSymbology )
1076+
{
1077+
slList = symbolLayers();
1078+
}
1079+
1080+
//LTYPE
1081+
mLineStyles.clear();
1082+
stream << " 0\n";
1083+
stream << "TABLE\n";
1084+
stream << " 2\n";
1085+
stream << "LTYPE\n";
1086+
stream << " 5\n";
1087+
stream << QString( "%1\n" ).arg( mNextHandleId++ );
1088+
stream << "100\n";
1089+
stream << "AcDbSymbolTable\n";
1090+
stream << " 70\n";
1091+
stream << QString( "%1\n" ).arg( nLineTypes( slList ) + 1 ); //number of linetypes
1092+
1093+
//add continuous style as default
1094+
stream << " 0\n";
1095+
stream << "LTYPE\n";
1096+
stream << " 5\n";
1097+
stream << QString( "%1\n" ).arg( mNextHandleId++ );
1098+
stream << "100\n";
1099+
stream << "AcDbSymbolTableRecord\n";
1100+
stream << "100\n";
1101+
stream << "AcDbLinetypeTableRecord\n";
1102+
stream << " 2\n";
1103+
stream << "CONTINUOUS\n";
1104+
stream << " 70\n";
1105+
stream << "64\n";
1106+
stream << " 3\n";
1107+
stream << "Defaultstyle\n";
1108+
stream << " 72\n";
1109+
stream << "65\n";
1110+
stream << " 73\n";
1111+
stream << "0\n";
1112+
stream << " 40\n"; //todo: add segments in group 49
1113+
stream << "0.0\n";
1114+
1115+
//add symbol layer linestyles
1116+
QList<QgsSymbolLayerV2*>::const_iterator slIt = slList.constBegin();
1117+
for ( ; slIt != slList.constEnd(); ++slIt )
1118+
{
1119+
writeSymbolLayerLinestyleAC1018( stream, *slIt );
1120+
}
1121+
1122+
stream << " 0\n";
1123+
stream << "ENDTAB\n";
1124+
1125+
//LAYER
1126+
stream << " 0\n";
1127+
stream << "TABLE\n";
1128+
stream << " 2\n";
1129+
stream << "LAYER\n";
1130+
stream << " 5\n";
1131+
stream << QString( "%1\n" ).arg( mNextHandleId++ );
1132+
stream << "100\n";
1133+
stream << "AcDbSymbolTable\n";
1134+
stream << " 70\n";
1135+
stream << mLayers.count() << "\n";
1136+
QList< QgsMapLayer* >::const_iterator layerIt = mLayers.constBegin();
1137+
for ( ; layerIt != mLayers.constEnd(); ++layerIt )
1138+
{
1139+
stream << " 0\n";
1140+
stream << "LAYER\n";
1141+
stream << "100\n";
1142+
stream << "AcDbSymbolTableRecord\n";
1143+
stream << "100\n";
1144+
stream << "AcDbLayerTableRecord\n";
1145+
stream << " 2\n";
1146+
if ( *layerIt )
1147+
{
1148+
stream << ( *layerIt )->name() << "\n";
1149+
}
1150+
stream << " 70\n"; //layer property
1151+
stream << "64\n";
1152+
stream << " 62\n"; //layer color
1153+
stream << "1\n";
1154+
stream << " 6\n"; //layer line type
1155+
stream << "CONTINUOUS\n";
1156+
}
1157+
stream << " 0\n";
1158+
stream << "ENDTAB\n";
1159+
1160+
//todo: VIEW table
1161+
1162+
//todo: UCS table
1163+
1164+
//APPID
1165+
stream << " 0\n";
1166+
stream << "TABLE\n";
1167+
stream << " 2\n";
1168+
stream << "APPID\n";
1169+
stream << " 5\n";
1170+
stream << QString( "%1\n" ).arg( mNextHandleId++ );
1171+
stream << "100\n";
1172+
stream << "AcDbSymbolTable\n";
1173+
stream << " 70\n";
1174+
stream << " 1\n";
1175+
1176+
stream << " 0\n";
1177+
stream << "APPID\n";
1178+
stream << " 5\n";
1179+
stream << QString( "%1\n" ).arg( mNextHandleId++ );
1180+
stream << "100\n";
1181+
stream << "AcDbSymbolTableRecord\n";
1182+
stream << "100\n";
1183+
stream << "AcDbRegAppTableRecord\n";
1184+
stream << " 2\n";
1185+
stream << "ACAD\n";
1186+
stream << " 70\n";
1187+
stream << " 0\n";
1188+
stream << " 0\n";
1189+
stream << "ENDTAB\n";
1190+
1191+
//todo: DIMSTYLE table
1192+
1193+
//todo: BLOCK_RECORD table
1194+
1195+
endSection( stream );
1196+
}
1197+
1198+
void QgsDxfExport::writeSymbolLayerLinestyleAC1018( QTextStream& stream, const QgsSymbolLayerV2* symbolLayer )
1199+
{
1200+
if ( !symbolLayer )
1201+
{
1202+
return;
1203+
}
1204+
1205+
//QgsSimpleLineSymbolLayer can have customDashVector() / customDashPatternUnit()
1206+
const QgsSimpleLineSymbolLayerV2* simpleLine = dynamic_cast< const QgsSimpleLineSymbolLayerV2* >( symbolLayer );
1207+
if ( simpleLine )
1208+
{
1209+
if ( simpleLine->useCustomDashPattern() )
1210+
{
1211+
++mSymbolLayerCounter;
1212+
QString name = QString( "symbolLayer%1" ).arg( mSymbolLayerCounter );
1213+
QVector<qreal> dashPattern = simpleLine->customDashVector();
1214+
writeLinestyleAC1018( stream, name, dashPattern, simpleLine->customDashPatternUnit() );
1215+
mLineStyles.insert( symbolLayer, name );
1216+
}
1217+
}
1218+
}
1219+
1220+
void QgsDxfExport::writeLinestyleAC1018( QTextStream& stream, const QString& styleName, const QVector<qreal>& pattern, QgsSymbolV2::OutputUnit u )
1221+
{
1222+
double length = 0;
1223+
QVector<qreal>::const_iterator dashIt = pattern.constBegin();
1224+
for ( ; dashIt != pattern.constEnd(); ++dashIt )
1225+
{
1226+
length += *dashIt;
1227+
}
1228+
1229+
stream << " 0\n";
1230+
stream << "LTYPE\n";
1231+
stream << " 5\n";
1232+
stream << QString( "%1\n" ).arg( mNextHandleId++ );
1233+
stream << "100\n";
1234+
stream << "AcDbSymbolTableRecord\n";
1235+
stream << "100\n";
1236+
stream << "AcDbLinetypeTableRecord\n";
1237+
stream << " 2\n";
1238+
stream << QString( "%1\n" ).arg( styleName );
1239+
stream << " 70\n";
1240+
stream << "64\n";
1241+
stream << " 3\n";
1242+
stream << "\n";
1243+
stream << " 72\n";
1244+
stream << "65\n";
1245+
stream << " 73\n";
1246+
stream << QString( "%1\n" ).arg( pattern.size() ); //number of segments
1247+
stream << " 40\n"; //total length of segments
1248+
stream << QString( "%1\n" ).arg( length );
1249+
1250+
dashIt = pattern.constBegin();
1251+
bool isSpace = false;
1252+
for ( ; dashIt != pattern.constEnd(); ++dashIt )
1253+
{
1254+
stream << " 49\n";
1255+
1256+
//map units or mm?
1257+
double segmentLength = ( isSpace ? -*dashIt : *dashIt );
1258+
segmentLength *= mapUnitScaleFactor( mSymbologyScaleDenominator, u, mMapUnits );
1259+
stream << QString( "%1\n" ).arg( segmentLength );
1260+
isSpace = !isSpace;
1261+
}
1262+
}
1263+
1264+
void QgsDxfExport::writeEntitiesAC1018( QTextStream& stream )
1265+
{
1266+
//todo...
1267+
}
1268+
1269+
void QgsDxfExport::writeEntitiesSymbolLevelsAC1018( QTextStream& stream, QgsVectorLayer* layer )
1270+
{
1271+
//todo...
1272+
}
1273+
1274+
void QgsDxfExport::writePolylineAC1018( QTextStream& stream, const QgsPolyline& line, const QString& layer, const QString& lineStyleName, int color,
1275+
double width, bool polygon )
1276+
{
1277+
stream << " 0\n";
1278+
stream << "LWPOLYLINE\n";
1279+
stream << " 5\n";
1280+
stream << QString( "%1\n" ).arg( mNextHandleId++ );
1281+
stream << " 8\n";
1282+
stream << layer << "\n";
1283+
stream << "100\n";
1284+
stream << "AcDbEntity\n";
1285+
stream << "100\n";
1286+
stream << "AcDbPolyline\n";
1287+
1288+
stream << " 6\n";
1289+
stream << QString( "%1\n" ).arg( lineStyleName );
1290+
1291+
stream << " 62\n";
1292+
stream << color << "\n";
1293+
1294+
stream << " 90\n";
1295+
stream << QString( "%1\n" ).arg( line.size() );
1296+
1297+
stream << " 70\n";
1298+
int type = polygon ? 1 : 0;
1299+
stream << type << "\n";
1300+
1301+
stream << " 43\n";
1302+
stream << width << "\n";
1303+
1304+
QgsPolyline::const_iterator lineIt = line.constBegin();
1305+
for ( ; lineIt != line.constEnd(); ++lineIt )
1306+
{
1307+
writeVertexAC1018( stream, *lineIt );
1308+
}
1309+
}
1310+
1311+
void QgsDxfExport::writeVertexAC1018( QTextStream& stream, const QgsPoint& pt )
1312+
{
1313+
stream << " 10\n";
1314+
stream << pt.x() << "\n";
1315+
stream << " 20\n";
1316+
stream << pt.y() << "\n";
1317+
}

‎src/core/qgsdxfexport.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ class QgsDxfExport
6767
static double mDxfColors[][3];
6868

6969
int mSymbolLayerCounter; //internal counter
70+
int mNextHandleId;
7071
QHash< const QgsSymbolLayerV2*, QString > mLineStyles; //symbol layer name types
7172

73+
//AC1009
7274
void writeHeader( QTextStream& stream );
7375
void writeTables( QTextStream& stream );
7476
void writeEntities( QTextStream& stream );
@@ -81,6 +83,20 @@ class QgsDxfExport
8183
void writePolyline( QTextStream& stream, const QgsPolyline& line, const QString& layer, const QString& lineStyleName, int color,
8284
double width = -1, bool polygon = false );
8385
void writeVertex( QTextStream& stream, const QgsPoint& pt, const QString& layer );
86+
void writeSymbolLayerLinestyle( QTextStream& stream, const QgsSymbolLayerV2* symbolLayer );
87+
void writeLinestyle( QTextStream& stream, const QString& styleName, const QVector<qreal>& pattern, QgsSymbolV2::OutputUnit u );
88+
89+
//AC1018
90+
void writeHeaderAC1018( QTextStream& stream );
91+
void writeTablesAC1018( QTextStream& stream );
92+
void writeEntitiesAC1018( QTextStream& stream );
93+
void writeEntitiesSymbolLevelsAC1018( QTextStream& stream, QgsVectorLayer* layer );
94+
void writeSymbolLayerLinestyleAC1018( QTextStream& stream, const QgsSymbolLayerV2* symbolLayer );
95+
void writeLinestyleAC1018( QTextStream& stream, const QString& styleName, const QVector<qreal>& pattern, QgsSymbolV2::OutputUnit u );
96+
void writeVertexAC1018( QTextStream& stream, const QgsPoint& pt );
97+
void writePolylineAC1018( QTextStream& stream, const QgsPolyline& line, const QString& layer, const QString& lineStyleName, int color,
98+
double width = -1, bool polygon = false );
99+
84100

85101
QgsRectangle dxfExtent() const;
86102

@@ -104,8 +120,8 @@ class QgsDxfExport
104120
QList<QgsSymbolLayerV2*> symbolLayers();
105121
static int nLineTypes( const QList<QgsSymbolLayerV2*>& symbolLayers );
106122

107-
void writeSymbolLayerLinestyle( QTextStream& stream, const QgsSymbolLayerV2* symbolLayer );
108-
void writeLinestyle( QTextStream& stream, const QString& styleName, const QVector<qreal>& pattern, QgsSymbolV2::OutputUnit u );
123+
124+
109125
};
110126

111127
#endif // QGSDXFEXPORT_H

0 commit comments

Comments
 (0)
Please sign in to comment.