TestCenter Reference
ScreenShot.py
Go to the documentation of this file.
2# Copyright 2009, MeVis Medical Solutions AG
3#
4# The user may use this file in accordance with the license agreement provided with
5# the Software or, alternatively, in accordance with the terms contained in a
6# written agreement between the user and MeVis Medical Solutions AG.
7#
8# For further information use the contact form at https://www.mevislab.de/contact
9#
10
11
13
14# -- system imports ----------------------------------------------------------------------------{{{-
15import os
16import time
17
18# ----------------------------------------------------------------------------------------------}}}-
19
20# -- local imports -----------------------------------------------------------------------------{{{-
21import mevis
22
23from TestSupport import Fields
24from TestSupport.TestHelper import TestHelper
25from . import Base, Fields, Logging
26
27# ----------------------------------------------------------------------------------------------}}}-
28
29
30# -- def createOffscreenScreenShot -------------------------------------------------------------{{{-
31
42def createOffscreenScreenShot(SONodeName, filename=None, transparencyType=None, size=None, backgroundColor=None):
43 context = TestHelper.getInstance().getHelperContext()
44 modOffRend = context.module("oSSOffscreenRender")
45 if not modOffRend.field("supported").value:
46 Logging.warning("Offscreen rendering is not supported on this hardware. Cannot create screenshot.")
47 return None
48
49 resultDirectory = Base.getResultDirectory()
50
51 # If no filename was given generate one.
52 if not filename:
53 i = 0
54 filename = os.path.join(resultDirectory, "Screenshot_%03d.png")
55 while os.path.isfile(filename % (i)):
56 i += 1
57 path = filename % (i)
58 else:
59 path = os.path.join(resultDirectory, filename)
60
61 if os.path.isfile(path):
62 Logging.error("File with name %s already exists!" % (path))
63 return None
64
65 changeSet = Fields.ChangeSet(context=context)
66 if transparencyType:
67 changeSet.setFieldValue("oSSOffscreenRender.transparencyType", transparencyType)
68 if size:
69 changeSet.setFieldValue("oSSOffscreenRender.size", size)
70 if backgroundColor:
71 changeSet.setFieldValue("oSSOffscreenRender.bgColor", backgroundColor)
72
73 modImgSave = context.module("oSSImgSave")
74
75 modOffRend.field("sceneGraph").connectFrom(Base.getTestCaseContext().field(SONodeName))
76 mevis.MLAB.processInventorQueue()
77 modOffRend.field("update").touch()
78 if not modOffRend.field("output0").isValid():
79 Logging.error("Creation of screenshot failed.")
80 return None
81
82 modImgSave.field("format").value = "PNG"
83 modImgSave.field("filename").value = path
84 modImgSave.field("save").touch()
85
86 return path
87
88
89# ----------------------------------------------------------------------------------------------}}}-
90
91
92# -- def createScreenShot ----------------------------------------------------------------------{{{-
93
94def createScreenShot(window, filename, grabFromScreen=True, scaleToLowRes=False):
95 helperModule = TestHelper.getInstance().getHelperContext()
96
97 pathname = Base.getResultDirectory()
98
99 if not window:
100 Logging.error("ScreenShot failed (no window available)")
101 return
102
103 tabview = window.getTabViewControl()
104 if tabview == None:
105 return
106
107 window.raiseWindow()
108 window.updateFrame()
109
110 for i in range(10):
111 time.sleep(0.002)
112 mevis.MLAB.processEvents(True)
113
114 imgList = []
115 for i in range(0, tabview.countTabs()):
116 tabview.selectTabAtIndex(i)
117
118 mevis.MLAB.priv().repaintWidget(tabview.widget())
119
120 imgName = os.path.join(pathname, filename + "." + str(i) + ".png")
121
122 if not window.createScreenshot(imgName, grabFromScreen, scaleToLowRes):
123 Logging.warning("ScreenShot failed")
124 else:
125 imgList.append(imgName)
126 return imgList
127
128
129# ----------------------------------------------------------------------------------------------}}}-
130
131
132# -- def createScreenShot ----------------------------------------------------------------------{{{-
133
135 context = TestHelper.getInstance().getHelperContext()
136 return context.field("oSSOffscreenRender.supported").value
137
138
139# ----------------------------------------------------------------------------------------------}}}-
Class to handle field changes and make them revertable.
Definition ChangeSet.py:29
canCreateScreenshot()
Returns whether a screenshot can be created on this hardware.
createOffscreenScreenShot(SONodeName, filename=None, transparencyType=None, size=None, backgroundColor=None)
Create an screenshot of the given Inventor node that must be present in the given context.
Definition ScreenShot.py:42
createScreenShot(window, filename, grabFromScreen=True, scaleToLowRes=False)
Creates a screenshot of the given MeVisLab window.
Definition ScreenShot.py:94