IronPython script to export visualizations to PDF generates InvalidOperationException error

IronPython script to export visualizations to PDF generates InvalidOperationException error

book

Article ID: KB0079246

calendar_today

Updated On:

Products Versions
Spotfire Developer All Versions

Description

IronPython script to export a visualization to PDF results in the below InvalidOperationException error,
System.InvalidOperationException: Invalid operation 'BeginAggregatedTransaction' to the command history when in state 'Executing'.
   at Spotfire.Dxp.Framework.Commands.CommandHistory.BeginAggregatedTransaction(String displayName, Boolean transient, DocumentNode rootNode)
   at Spotfire.Dxp.Framework.Commands.CommandHistory.BeginTransientTransaction(DocumentNode rootNode)
   at Spotfire.Dxp.Application.Document.Export(PdfExportSettings2 settings)
   at _stub_$265##265(Closure , CallSite , CodeContext , Object , Object , String )
   at Microsoft.Scripting.Actions.MatchCaller.Call4[T0,T1,T2,T3,TRet](Func`6 target, CallSite site, Object[] args)
   at Microsoft.Scripting.Actions.CallSite`1.UpdateAndExecute(Object[] args)
   at Microsoft.Scripting.Actions.UpdateDelegates.Update4[T,T0,T1,T2,T3,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3)
   at $261##261(Closure , Scope , LanguageContext )
   at Spotfire.Dxp.Application.ScriptSupport.IronPythonScriptEngine.ExecuteForDebugging(String scriptCode, Dictionary`2 scope, Stream outputStream)
   at Spotfire.Dxp.Application.Scripting.ScriptManager.<>c__DisplayClass14.b__f()
   at Spotfire.Dxp.Framework.Commands.CommandHistory.Transaction(String displayName, Executor executor, Boolean visible, Boolean sticky, Guid stickyGuid)
   at Spotfire.Dxp.Framework.Commands.CommandHistory.Transaction(String displayName, Executor executor)
   at Spotfire.Dxp.Framework.DocumentModel.DocumentNode.Transaction(String displayName, Executor executor)
   at Spotfire.Dxp.Application.Scripting.ScriptManager.ExecuteScriptForDebugging(String scriptCode, Dictionary`2 scriptArguments, String& output)
   at Spotfire.Dxp.Forms.Framework.Scripting.ScriptEditDialog.RunButton_Click(Object sender, EventArgs e)
The cause of the above error is because all Iron Python scripts are executed in transaction, which means that any code that starts a progress or aggregated transaction will fail. 

Issue/Introduction

IronPython script to export visualizations to PDF generates error

Resolution

For versions 7.0 and Lower:
A potential workaround is to invoke asynchronously on the application thread and thereby get outside the transaction as below. The function g is necessary because the scripts scope is cleared after execution and therefore Application or anything else defined in the scope will not be available when the code invokes on the application thread.
from Spotfire.Dxp.Framework.ApplicationModel import ApplicationThread
 
def g(app,fileName,pdfexpsettings):
   def f():
      app.Document.Export(pdfexpsettings,fileName)
   return f
 
Application.GetService[ApplicationThread]().InvokeAsynchronously(g(Application))

For Versiosn 7.5 and higher:
The above script can be used to export the visualization to PDF or uncheck the "Execute in Transaction" option in the script editor in Spotfire Analyst.

Additional Information

API Reference: Document.Export