Introduction
A snapshot is a way to preserve an instance of the Document without modifying the underlying instances. This snapshot can then be used as a copy of the Document instead of the actual data. A snapshot is particularly useful when doing exports when we in fact want a frozen instance of the analysis that can be exported to a static document. Sometimes, due to implementation limitations of the scripting language, this type of snapshot cannot be acquired. Instead the following error is reported.
System.InvalidOperationException: Attempt take snapshot on application thread in state 'Executing'.
To avoid this problem, wrap the code inside a function without any outside references. The point of this is that we then can execute the code directly from the main application thread where a Spotfire snapshot can be taken. We are now executing from the thread that was previously blocked.
Example performing an image export
# Copyright © 2017. TIBCO Software Inc. Licensed under TIBCO BSD-style license. import System from System.Drawing import Bitmap, Rectangle from System.Drawing import Graphics, Image from Spotfire.Dxp.Framework.ApplicationModel import ApplicationThread app = Document.GetService(ApplicationThread) image = Bitmap(800, 600) def f(visual=visual, document=Document, app=app, image=image, Graphics=Graphics, Rectangle=Rectangle): try: vis = visual gfx = Graphics.FromImage(image) rect = Rectangle(0,0,image.Width,image.Height) visualBounds = document.ActivePageReference.GetVisualBounds(vis, rect) vis.Render(gfx, visualBounds) except: return # DO SOMETHING WITH THE IMAGE HERE image.Save(r'C:\Temp\image.bmp') app.InvokeAsynchronously(f)
In general, the important point here is that all the objects used in the method are provided by the passed parameters. This is done so that the parameters are still available when the context switch to the main thread is done. For example, if you also wanted to add a reference to a number named "myNumber", add it to the method parameters as follows.
Example of method parameters
myNumber = 10 def f(visual=visual, document=Document, app=app, image=image, Graphics=Graphics, Rectangle=Rectangle, myNumber=myNumber)
References
License: TIBCO BSD-Style License
Recommended Comments
There are no comments to display.