Skip to content

Commit d84c34e

Browse files
committedFeb 19, 2019
dwg import: support nested blocks (refs #20392)
1 parent 355deb5 commit d84c34e

File tree

2 files changed

+53
-21
lines changed

2 files changed

+53
-21
lines changed
 

‎src/app/dwg/qgsdwgimporter.cpp

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2194,7 +2194,7 @@ void QgsDwgImporter::addKnot( const DRW_Entity &data )
21942194

21952195
void QgsDwgImporter::addInsert( const DRW_Insert &data )
21962196
{
2197-
OGRLayerH layer = OGR_DS_GetLayerByName( mDs.get(), "inserts" );
2197+
OGRLayerH layer = OGR_DS_GetLayerByName( mDs.get(), "inserts" );
21982198
Q_ASSERT( layer );
21992199
OGRFeatureDefnH dfn = OGR_L_GetLayerDefn( layer );
22002200
Q_ASSERT( dfn );
@@ -2615,10 +2615,10 @@ bool QgsDwgImporter::expandInserts( QString &error )
26152615
return false;
26162616
}
26172617

2618-
QHash<QString, int> blockhandle;
2619-
26202618
OGR_L_ResetReading( blocks );
26212619

2620+
mBlocks.clear();
2621+
26222622
gdal::ogr_feature_unique_ptr f;
26232623
for ( ;; )
26242624
{
@@ -2628,20 +2628,26 @@ bool QgsDwgImporter::expandInserts( QString &error )
26282628

26292629
QString name = QString::fromUtf8( OGR_F_GetFieldAsString( f.get(), nameIdx ) );
26302630
int handle = OGR_F_GetFieldAsInteger( f.get(), handleIdx );
2631-
blockhandle.insert( name, handle );
2631+
mBlocks.insert( name, handle );
26322632
}
26332633

2634-
OGRLayerH inserts = OGR_DS_GetLayerByName( mDs.get(), "inserts" );
2634+
return expandInserts( error, -1, QTransform() );
2635+
}
2636+
2637+
bool QgsDwgImporter::expandInserts( QString &error, int block, QTransform base )
2638+
{
2639+
QgsDebugMsg( QString( "expanding block:%1" ).arg( block ) );
2640+
OGRLayerH inserts = OGR_DS_ExecuteSQL( mDs.get(), QStringLiteral( "SELECT * FROM inserts WHERE block=%1" ).arg( block ).toUtf8().constData(), nullptr, nullptr );
26352641
if ( !inserts )
26362642
{
2637-
QgsDebugMsg( QStringLiteral( "could not open layer 'inserts'" ) );
2643+
QgsDebugMsg( QStringLiteral( "could not query layer 'inserts'" ) );
26382644
return false;
26392645
}
26402646

2641-
dfn = OGR_L_GetLayerDefn( inserts );
2647+
OGRFeatureDefnH dfn = OGR_L_GetLayerDefn( inserts );
26422648
Q_ASSERT( dfn );
26432649

2644-
nameIdx = OGR_FD_GetFieldIndex( dfn, "name" );
2650+
int nameIdx = OGR_FD_GetFieldIndex( dfn, "name" );
26452651
int xscaleIdx = OGR_FD_GetFieldIndex( dfn, "xscale" );
26462652
int yscaleIdx = OGR_FD_GetFieldIndex( dfn, "yscale" );
26472653
int zscaleIdx = OGR_FD_GetFieldIndex( dfn, "zscale" );
@@ -2669,17 +2675,22 @@ bool QgsDwgImporter::expandInserts( QString &error )
26692675

26702676
OGR_L_ResetReading( inserts );
26712677

2672-
mTime.start();
2678+
if ( block == -1 )
2679+
mTime.start();
26732680

26742681
gdal::ogr_feature_unique_ptr insert;
26752682
int i = 0, errors = 0;
26762683
for ( int i = 0; true; ++i )
26772684
{
2678-
if ( mTime.elapsed() > 1000 )
2685+
if ( block == -1 && mTime.elapsed() > 1000 )
26792686
{
26802687
progress( tr( "Expanding block reference %1/%2…" ).arg( i ).arg( n ) );
26812688
mTime.restart();
26822689
}
2690+
else if ( i % 1000 == 0 )
2691+
{
2692+
QgsDebugMsg( QStringLiteral( "%1: expanding insert %2/%3…" ).arg( block, 0, 16 ).arg( i ).arg( n ) );
2693+
}
26832694

26842695
insert.reset( OGR_L_GetNextFeature( inserts ) );
26852696
if ( !insert )
@@ -2714,7 +2725,7 @@ bool QgsDwgImporter::expandInserts( QString &error )
27142725
}
27152726
double blockLinewidth = OGR_F_GetFieldAsDouble( insert.get(), linewidthIdx );
27162727

2717-
int handle = blockhandle.value( name, -1 );
2728+
int handle = mBlocks.value( name, -1 );
27182729
if ( handle < 0 )
27192730
{
27202731
QgsDebugMsg( QStringLiteral( "Block '%1' not found" ).arg( name ) );
@@ -2728,18 +2739,23 @@ bool QgsDwgImporter::expandInserts( QString &error )
27282739

27292740
QTransform t;
27302741
t.translate( p.x(), p.y() ).scale( xscale, yscale ).rotateRadians( angle );
2742+
t *= base;
27312743

2744+
OGRLayerH src = nullptr;
27322745
Q_FOREACH ( const QString &name, QStringList() << "hatches" << "lines" << "polylines" << "texts" << "points" )
27332746
{
2747+
if ( src )
2748+
OGR_DS_ReleaseResultSet( mDs.get(), src );
2749+
27342750
OGRLayerH src = OGR_DS_ExecuteSQL( mDs.get(), QStringLiteral( "SELECT * FROM %1 WHERE block=%2" ).arg( name ).arg( handle ).toUtf8().constData(), nullptr, nullptr );
27352751
if ( !src )
27362752
{
2737-
QgsDebugMsg( QStringLiteral( "%1: could not execute query for block %2" ).arg( name ).arg( handle ) );
2753+
QgsDebugMsg( QStringLiteral( "%1: could not open layer %1" ).arg( name ) );
27382754
continue;
27392755
}
27402756

2741-
OGRLayerH dst = OGR_DS_GetLayerByName( mDs.get(), name.toUtf8().constData() );
2742-
Q_ASSERT( dst );
2757+
GIntBig n = OGR_L_GetFeatureCount( src, 0 );
2758+
Q_UNUSED( n );
27432759

27442760
dfn = OGR_L_GetLayerDefn( src );
27452761
Q_ASSERT( dfn );
@@ -2762,10 +2778,18 @@ bool QgsDwgImporter::expandInserts( QString &error )
27622778

27632779
OGR_L_ResetReading( src );
27642780

2781+
OGRLayerH dst = OGR_DS_GetLayerByName( mDs.get(), name.toUtf8().constData() );
2782+
Q_ASSERT( dst );
2783+
27652784
gdal::ogr_feature_unique_ptr f;
27662785
int j = 0;
27672786
for ( ;; )
27682787
{
2788+
if ( j % 1000 == 0 )
2789+
{
2790+
QgsDebugMsg( QStringLiteral( "%1.%2: %3/%4 copied" ).arg( block, 0, 16 ).arg( handle, 0, 16 ).arg( j ).arg( n ) );
2791+
}
2792+
27692793
f.reset( OGR_L_GetNextFeature( src ) );
27702794
if ( !f )
27712795
break;
@@ -2844,9 +2868,15 @@ bool QgsDwgImporter::expandInserts( QString &error )
28442868
++j;
28452869
}
28462870

2871+
QgsDebugMsgLevel( QStringLiteral( "%1: %2 features copied" ).arg( name ).arg( j ), 5 );
2872+
}
2873+
2874+
if ( src )
28472875
OGR_DS_ReleaseResultSet( mDs.get(), src );
28482876

2849-
QgsDebugMsgLevel( QStringLiteral( "%1: %2 features copied" ).arg( name ).arg( j ), 5 );
2877+
if ( !expandInserts( error, handle, t ) )
2878+
{
2879+
QgsDebugMsg( QString( "%1: Expanding %2 failed" ).arg( block ).arg( handle ) );
28502880
}
28512881
}
28522882

@@ -2910,12 +2940,12 @@ void QgsDwgImporter::cleanText( QString &res )
29102940
{
29112941
QString prev( res );
29122942

2913-
res = res.replace( QRegularExpression( "\\\\f[0-9A-Za-z| ]{0,};" ), QStringLiteral( "" ) ); // font setting
2914-
res = res.replace( QRegularExpression( "([^\\\\]|^){" ), QStringLiteral( "\\1" ) ); // grouping
2915-
res = res.replace( QRegularExpression( "([^\\\\])}" ), QStringLiteral( "\\1" ) );
2916-
res = res.replace( QRegularExpression( "([^\\\\]|^)\\\\[loLOkx]" ), QStringLiteral( "\\1" ) ); // underline, overstrike, strike through
2917-
res = res.replace( QRegularExpression( "([^\\\\]|^)\\\\[HWACQ]\\d*(\\.\\d*)?[xX]?;?" ), QStringLiteral( "\\1" ) ); // text height, width, alignment, color and slanting
2918-
res = res.replace( QRegularExpression( "([^\\\\]|^)\\\\[ACQ]\\d+;" ), QStringLiteral( "\\1" ) ); // alignment, color and slanting
2943+
res = res.replace( QRegularExpression( "\\\\f[0-9A-Za-z| ]{0,};" ), QString( "" ) ); // font setting
2944+
res = res.replace( QRegularExpression( "([^\\\\]|^){" ), QStringLiteral( "\\1" ) ); // grouping
2945+
res = res.replace( QRegularExpression( "([^\\\\])}" ), QStringLiteral( "\\1" ) );
2946+
res = res.replace( QRegularExpression( "([^\\\\]|^)\\\\[loLOkx]" ), QStringLiteral( "\\1" ) ); // underline, overstrike, strike through
2947+
res = res.replace( QRegularExpression( "([^\\\\]|^)\\\\[HhWwAaCcQq]\\d*(\\.\\d*)?[xX]?;?" ), QStringLiteral( "\\1" ) ); // text height, width, alignment, color and slanting
2948+
res = res.replace( QRegularExpression( "([^\\\\]|^)\\\\[ACQ]\\d+;" ), QStringLiteral( "\\1" ) ); // alignment, color and slanting
29192949

29202950
if ( res == prev )
29212951
break;

‎src/app/dwg/qgsdwgimporter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ class QgsDwgImporter : public DRW_Interface
201201
bool lineFromSpline( const DRW_Spline &data, QgsLineString &l );
202202

203203
bool expandInserts( QString &error );
204+
bool expandInserts( QString &error, int block, QTransform base );
204205

205206
bool createFeature( OGRLayerH layer, OGRFeatureH f, const QgsAbstractGeometry &g ) const;
206207

@@ -217,6 +218,7 @@ class QgsDwgImporter : public DRW_Interface
217218
QHash<QString, double> mLayerLinewidth;
218219
QHash<QString, QString> mLayerLinetype;
219220
QHash<QString, QString> mLinetype;
221+
QHash<QString, int> mBlocks;
220222

221223
QLabel *mLabel = nullptr;
222224
int mEntities = 0;

0 commit comments

Comments
 (0)
Please sign in to comment.