Allow Python scripts to use currently selected cases (#6906)

* #6891 Python : Make sure python scripts can be launched from context menu
* #6891 Python : Make currentScriptCaseId a general scripting feature
This commit is contained in:
Magne Sjaastad
2020-11-04 04:27:05 -05:00
committed by GitHub
parent cfccd6e943
commit 51359a0cda
8 changed files with 77 additions and 59 deletions

View File

@@ -20,6 +20,7 @@
#include "RicExecuteScriptForCasesFeature.h"
#include "RiaApplication.h"
#include "RiaLogging.h"
#include "RimCalcScript.h"
#include "RimCase.h"
@@ -61,30 +62,49 @@ void RicExecuteScriptForCasesFeature::onActionTriggered( bool isChecked )
RiuMainWindow* mainWindow = RiuMainWindow::instance();
mainWindow->showProcessMonitorDockPanel();
RiaApplication* app = RiaApplication::instance();
QString octavePath = app->octavePath();
if ( !octavePath.isEmpty() )
RiaApplication* app = RiaApplication::instance();
QString pathToScriptExecutable;
QProcessEnvironment processEnvironment;
if ( scriptAbsolutePath.endsWith( ".py" ) )
{
QStringList arguments = RimCalcScript::createCommandLineArguments( scriptAbsolutePath );
processEnvironment = app->pythonProcessEnvironment();
pathToScriptExecutable = app->pythonPath();
std::vector<RimCase*> selection;
caf::SelectionManager::instance()->objectsByType( &selection );
if ( pathToScriptExecutable.isEmpty() )
{
RiaLogging::warning( "Path to Python executable is empty, not able to execute script" );
}
}
else
{
processEnvironment = app->octaveProcessEnvironment();
pathToScriptExecutable = app->octavePath();
if ( pathToScriptExecutable.isEmpty() )
{
RiaLogging::warning( "Path to Octave executable is empty, not able to execute script" );
}
}
// Get case ID from selected cases in selection model
if ( !pathToScriptExecutable.isEmpty() )
{
QStringList arguments = RimCalcScript::createCommandLineArguments( scriptAbsolutePath );
std::vector<int> caseIdsInSelection;
for ( size_t i = 0; i < selection.size(); i++ )
{
RimCase* casePtr = selection[i];
caseIdsInSelection.push_back( casePtr->caseId );
std::vector<RimCase*> selection;
caf::SelectionManager::instance()->objectsByType( &selection );
for ( RimCase* rimCase : selection )
{
caseIdsInSelection.push_back( rimCase->caseId );
}
}
if ( caseIdsInSelection.size() > 0 )
{
RiaApplication::instance()->launchProcessForMultipleCases( octavePath,
arguments,
caseIdsInSelection,
app->octaveProcessEnvironment() );
}
RiaApplication::instance()->launchProcessForMultipleCases( pathToScriptExecutable,
arguments,
caseIdsInSelection,
processEnvironment );
}
}