Skip to content

Commit 9e0c637

Browse files
feat(screenshot): new rpc
missing unit tests
1 parent b3eccb3 commit 9e0c637

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"rpc": "screenshot",
3+
"type": "object",
4+
"properties": {},
5+
"required": [],
6+
"additionalProperties": false
7+
}

src/opengeodeweb_viewer/vtk_protocol.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1+
# Standard library imports
12
import json
23
import os
4+
5+
# Third party imports
6+
import vtk
37
from vtk.web import protocols as vtk_protocols
8+
from vtkmodules.vtkIOImage import vtkPNGWriter
9+
from vtkmodules.vtkRenderingCore import (vtkWindowToImageFilter)
410
from wslink import register as exportRpc
5-
import vtk
11+
12+
# Local application imports
613
from .function import validate_schemas
714

15+
16+
817
schemas = os.path.join(os.path.dirname(__file__), "rpc/schemas")
918

1019
with open(os.path.join(schemas, "create_visualization.json"), "r") as file:
@@ -35,6 +44,8 @@
3544
set_color_json = json.load(file)
3645
with open(os.path.join(schemas, "set_vertex_attribute.json"), "r") as file:
3746
set_vertex_attribute_json = json.load(file)
47+
with open(os.path.join(schemas, "take_screenshot.json"), "r") as file:
48+
take_screenshot_json = json.load(file)
3849

3950

4051
class VtkView(vtk_protocols.vtkWebProtocol):
@@ -271,6 +282,22 @@ def setVertexAttribute(self, params):
271282
mapper.SetScalarModeToUsePointFieldData()
272283
self.render()
273284

285+
@exportRpc(take_screenshot_json["rpc"])
286+
def takeScreenshot(self, params):
287+
validate_schemas(params, take_screenshot_json)
288+
print(f"{params=}", flush=True)
289+
renderWindow = self.getView("-1")
290+
w2if = vtkWindowToImageFilter()
291+
w2if.SetInput(renderWindow)
292+
w2if.SetInputBufferTypeToRGB()
293+
w2if.ReadFrontBufferOff()
294+
w2if.Update()
295+
296+
writer = vtkPNGWriter()
297+
writer.SetFileName(os.path.join(self.DATA_FOLDER_PATH, 'screenshot.png'))
298+
writer.SetInputConnection(w2if.GetOutputPort())
299+
writer.Write()
300+
274301
def get_data_base(self):
275302
return self.getSharedObject("db")
276303

0 commit comments

Comments
 (0)