TestCenter Reference
ScreenShot.py
Go to the documentation of this file.
1 #
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 from builtins import str
12 from builtins import range
13 
14 # Already ported to Python 3
15 
17 
18 # -- system imports ----------------------------------------------------------------------------{{{-
19 import os
20 import sys
21 import traceback
22 import time
23 # ----------------------------------------------------------------------------------------------}}}-
24 
25 # -- local imports -----------------------------------------------------------------------------{{{-
26 import mevis
27 
28 from TestSupport.TestHelper import TestHelper
29 from . import Base, Fields, Image, Logging
30 # ----------------------------------------------------------------------------------------------}}}-
31 
32 # -- def createOffscreenScreenShot -------------------------------------------------------------{{{-
33 
44 def createOffscreenScreenShot (SONodeName, filename=None, transparencyType=None, size=None, backgroundColor=None):
45  context = TestHelper.getInstance().getHelperContext()
46  modOffRend = context.module('oSSOffscreenRender')
47  if not modOffRend.field('supported').value:
48  Logging.warning('Offscreen rendering is not supported on this hardware. Cannot create screenshot.')
49  return None
50 
51  resultDirectory = Base.getResultDirectory()
52 
53  # If no filename was given generate one.
54  if not filename:
55  i = 0
56  filename = os.path.join(resultDirectory, "Screenshot_%03d.png")
57  while os.path.isfile(filename % (i)):
58  i += 1
59  path = filename % (i)
60  else:
61  path = os.path.join(resultDirectory, filename)
62 
63  if os.path.isfile(path):
64  Logging.error("File with name %s already exists!" % (path))
65  return None
66 
67  changeSet = Fields.ChangeSet(context=context)
68  if transparencyType:
69  changeSet.setFieldValue("oSSOffscreenRender.transparencyType", transparencyType)
70  if size:
71  changeSet.setFieldValue("oSSOffscreenRender.size", size)
72  if backgroundColor:
73  changeSet.setFieldValue("oSSOffscreenRender.bgColor", backgroundColor)
74 
75  modImgSave = context.module('oSSImgSave')
76 
77  modOffRend.field("sceneGraph").connectFrom(Base.getTestCaseContext().field(SONodeName))
78  mevis.MLAB.processInventorQueue()
79  modOffRend.field("update").touch()
80  if not modOffRend.field('output0').isValid():
81  Logging.error('Creation of screenshot failed.')
82  return None
83 
84  modImgSave.field("format").value = "PNG"
85  modImgSave.field('filename').value = path
86  modImgSave.field('save').touch()
87 
88  return path
89 # ----------------------------------------------------------------------------------------------}}}-
90 
91 # -- def createScreenShot ----------------------------------------------------------------------{{{-
92 
93 def createScreenShot (window, filename):
94  helperModule = TestHelper.getInstance().getHelperContext()
95 
96  pathname = Base.getResultDirectory()
97 
98  if not window:
99  Logging.error("ScreenShot failed (no window available)")
100  return
101 
102  tabview = window.getTabViewControl()
103  if tabview == None:
104  return
105 
106  window.raiseWindow()
107  window.updateFrame()
108 
109  for i in range(10):
110  time.sleep(0.002)
111  mevis.MLAB.processEvents(True)
112 
113  imgList = []
114  for i in range(0, tabview.countTabs()):
115  tabview.selectTabAtIndex(i)
116 
117  mevis.MLAB.priv().repaintWidget(tabview.widget())
118 
119  imgName = os.path.join(pathname, filename+"."+str(i)+".png")
120 
121  if not window.createScreenshot(imgName):
122  Logging.warning("ScreenShot failed")
123  else:
124  imgList.append(imgName)
125  return imgList
126 # ----------------------------------------------------------------------------------------------}}}-
127 
128 # -- def createScreenShot ----------------------------------------------------------------------{{{-
129 
131  context = TestHelper.getInstance().getHelperContext()
132  return context.field('oSSOffscreenRender.supported').value
133 
134 # ----------------------------------------------------------------------------------------------}}}-
135 
Class to handle field changes and make them revertable.
Definition: ChangeSet.py:35
def touch(fieldName, verbose=True)
Touch the given field.
Definition: Fields.py:33
def 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:44
def canCreateScreenshot()
Returns whether a screenshot can be created on this hardware.
Definition: ScreenShot.py:130
def createScreenShot(window, filename)
Create a screenshot of the given MeVisLab window.
Definition: ScreenShot.py:93