Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3D] notify user if the 3D scene export failed #59919

Merged
merged 6 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion python/3d/auto_generated/qgs3dmapscene.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ Given screen error (in pixels) and distance from camera (in 3D world coordinates
estimates the error in world space. Takes into account camera's field of view and the screen (3D view) size.
%End

void exportScene( const Qgs3DMapExportSettings &exportSettings );
bool exportScene( const Qgs3DMapExportSettings &exportSettings );
%Docstring
Exports the scene according to the scene export settings
Returns ``False`` if the operation failed
%End


Expand Down
3 changes: 2 additions & 1 deletion python/PyQt6/3d/auto_generated/qgs3dmapscene.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ Given screen error (in pixels) and distance from camera (in 3D world coordinates
estimates the error in world space. Takes into account camera's field of view and the screen (3D view) size.
%End

void exportScene( const Qgs3DMapExportSettings &exportSettings );
bool exportScene( const Qgs3DMapExportSettings &exportSettings );
%Docstring
Exports the scene according to the scene export settings
Returns ``False`` if the operation failed
%End


Expand Down
10 changes: 8 additions & 2 deletions src/3d/qgs3dmapscene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,7 @@ void Qgs3DMapScene::onCameraNavigationModeChanged()
mCameraController->setCameraNavigationMode( mMap.cameraNavigationMode() );
}

void Qgs3DMapScene::exportScene( const Qgs3DMapExportSettings &exportSettings )
bool Qgs3DMapScene::exportScene( const Qgs3DMapExportSettings &exportSettings )
{
QVector<QString> notParsedLayers;
Qgs3DSceneExporter exporter;
Expand Down Expand Up @@ -1045,7 +1045,11 @@ void Qgs3DMapScene::exportScene( const Qgs3DMapExportSettings &exportSettings )
if ( mTerrain )
exporter.parseTerrain( mTerrain, "Terrain" );

exporter.save( exportSettings.sceneName(), exportSettings.sceneFolderPath() );
const bool sceneSaved = exporter.save( exportSettings.sceneName(), exportSettings.sceneFolderPath() );
if ( !sceneSaved )
{
return false;
}

if ( !notParsedLayers.empty() )
{
Expand All @@ -1054,6 +1058,8 @@ void Qgs3DMapScene::exportScene( const Qgs3DMapExportSettings &exportSettings )
message += layerName + "\n";
QgsMessageOutput::showMessage( tr( "3D exporter warning" ), message, QgsMessageOutput::MessageText );
}

return true;
}

QVector<const QgsChunkNode *> Qgs3DMapScene::getLayerActiveChunkNodes( QgsMapLayer *layer )
Expand Down
7 changes: 5 additions & 2 deletions src/3d/qgs3dmapscene.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,11 @@ class _3D_EXPORT Qgs3DMapScene : public QObject
*/
float worldSpaceError( float epsilon, float distance ) const;

//! Exports the scene according to the scene export settings
void exportScene( const Qgs3DMapExportSettings &exportSettings );
/**
* Exports the scene according to the scene export settings
* Returns FALSE if the operation failed
*/
bool exportScene( const Qgs3DMapExportSettings &exportSettings );

/**
* Returns the active chunk nodes of \a layer
Expand Down
12 changes: 9 additions & 3 deletions src/3d/qgs3dsceneexporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -808,22 +808,27 @@ Qgs3DExportObject *Qgs3DSceneExporter::processPoints( Qt3DCore::QEntity *entity,
return obj;
}

void Qgs3DSceneExporter::save( const QString &sceneName, const QString &sceneFolderPath, int precision )
bool Qgs3DSceneExporter::save( const QString &sceneName, const QString &sceneFolderPath, int precision )
{
if ( mObjects.isEmpty() )
{
return false;
}

const QString objFilePath = QDir( sceneFolderPath ).filePath( sceneName + QStringLiteral( ".obj" ) );
const QString mtlFilePath = QDir( sceneFolderPath ).filePath( sceneName + QStringLiteral( ".mtl" ) );

QFile file( objFilePath );
if ( !file.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
{
QgsDebugError( QStringLiteral( "Scene can not be exported to '%1'. File access error." ).arg( objFilePath ) );
return;
return false;
}
QFile mtlFile( mtlFilePath );
if ( !mtlFile.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
{
QgsDebugError( QStringLiteral( "Scene can not be exported to '%1'. File access error." ).arg( mtlFilePath ) );
return;
return false;
}

float maxfloat = std::numeric_limits<float>::max(), minFloat = std::numeric_limits<float>::lowest();
Expand Down Expand Up @@ -863,6 +868,7 @@ void Qgs3DSceneExporter::save( const QString &sceneName, const QString &sceneFol
}

QgsDebugMsgLevel( QStringLiteral( "Scene exported to '%1'" ).arg( objFilePath ), 2 );
return true;
}

QString Qgs3DSceneExporter::getObjectName( const QString &name )
Expand Down
7 changes: 5 additions & 2 deletions src/3d/qgs3dsceneexporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,11 @@ class _3D_EXPORT Qgs3DSceneExporter : public Qt3DCore::QEntity
//! Creates terrain export objects from the terrain entity
void parseTerrain( QgsTerrainEntity *terrain, const QString &layer );

//! Saves the scene to a .obj file
void save( const QString &sceneName, const QString &sceneFolderPath, int precision = 6 );
/**
* Saves the scene to a .obj file
* Returns FALSE if the operation failed
*/
bool save( const QString &sceneName, const QString &sceneFolderPath, int precision = 6 );

//! Sets whether the triangles will look smooth
void setSmoothEdges( bool smoothEdges ) { mSmoothEdges = smoothEdges; }
Expand Down
17 changes: 14 additions & 3 deletions src/app/3d/qgs3dmapcanvaswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ void Qgs3DMapCanvasWidget::exportScene()
QgsGui::enableAutoGeometryRestore( &dlg );

Qgs3DMapExportSettings exportSettings;
QgsMap3DExportWidget w( mCanvas->scene(), &exportSettings );
QgsMap3DExportWidget exportWidget( mCanvas->scene(), &exportSettings );

QDialogButtonBox *buttons = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::Ok, &dlg );

Expand All @@ -565,10 +565,21 @@ void Qgs3DMapCanvasWidget::exportScene()
connect( buttons, &QDialogButtonBox::helpRequested, &dlg, [=] { QgsHelp::openHelp( QStringLiteral( "map_views/3d_map_view.html" ) ); } );

QVBoxLayout *layout = new QVBoxLayout( &dlg );
layout->addWidget( &w, 1 );
layout->addWidget( &exportWidget, 1 );
layout->addWidget( buttons );
if ( dlg.exec() )
w.exportScene();
{
const bool success = exportWidget.exportScene();
const QString exportFilePath = QDir( exportSettings.sceneFolderPath() ).filePath( exportSettings.sceneName() + QStringLiteral( ".obj" ) );
if ( success )
{
mMessageBar->pushMessage( tr( "Export 3D scene" ), tr( "Successfully exported scene to <a href=\"%1\">%2</a>" ).arg( QUrl::fromLocalFile( exportFilePath ).toString(), QDir::toNativeSeparators( exportFilePath ) ), Qgis::MessageLevel::Success, 0 );
}
else
{
mMessageBar->pushMessage( tr( "Export 3D scene" ), tr( "Unable to export scene to <a href=\"%1\">%2</a>" ).arg( QUrl::fromLocalFile( exportFilePath ).toString(), QDir::toNativeSeparators( exportFilePath ) ), Qgis::MessageLevel::Warning, 0 );
}
}
}

void Qgs3DMapCanvasWidget::onMainCanvasLayersChanged()
Expand Down
4 changes: 2 additions & 2 deletions src/app/3d/qgsmap3dexportwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void QgsMap3DExportWidget::settingsChanged()
mExportSettings->setScale( ui->scaleSpinBox->value() );
}

void QgsMap3DExportWidget::exportScene()
bool QgsMap3DExportWidget::exportScene()
{
mScene->exportScene( *mExportSettings );
return mScene->exportScene( *mExportSettings );
}
2 changes: 1 addition & 1 deletion src/app/3d/qgsmap3dexportwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class QgsMap3DExportWidget : public QWidget
~QgsMap3DExportWidget();

void loadSettings();
void exportScene();
bool exportScene();
private slots:
void settingsChanged();

Expand Down
3 changes: 2 additions & 1 deletion tests/src/3d/testqgs3drendering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2274,7 +2274,8 @@ void TestQgs3DRendering::do3DSceneExport( const QString &testName, int zoomLevel
exporter.parseTerrain( terrainEntity, "DEM_Tile" );

QString objFileName = QString( "%1-%2" ).arg( testName ).arg( zoomLevelsCount );
exporter.save( objFileName, QDir::tempPath(), 3 );
const bool saved = exporter.save( objFileName, QDir::tempPath(), 3 );
QVERIFY( saved );

int sum = 0;
for ( auto o : qAsConst( exporter.mObjects ) )
Expand Down
Loading