ShapeDiver: How to have a Screenshot of the viewer?

The title is enough… but more precisely I would like to give to the user who is playing with my code the ability of exporting some screenshots of the actual view he is watching (as the option print in Rhino is doing).

For example I made this “print”/“screenshot”/“images” attached through Rhino Print command and I would like to do the same from ShapeDiver.

Which are the precise steps to do that?

Thanks a ton,
Michele.

@michele.farina if you want to get a screenshot of the scene shown in the viewer, you need to use the API function api.scene.getScreenshot() which gives you a base 64 image that can be downloaded. This function is described in the API documentation here.

Okay, thanks Edwin! Let’s say I have no skills in programming, have you got a general GH file in which you use some API function for use these in ShapeDiver?

Thanks another time,
M.

@michele.farina the API functions are not accessed in anyway via GH. If you want to make a quick test to get a Screenshot via the API, you can right click on the menu of your model in ShapeDiver, then click on inspect, then click on console and here you can type and enter api.scene.getScreenshot(). This will give you the base 64 image.

Hi @edsahergom this is cool, …does this function also exists in your back-end API? …I would like to generate several images from a model (with different views, like TOP, FRONT, etc.) without necessarily open up the model in our UI.

How to make screenshot in V3 viewer version? Can you send some code example for V3 for making screenshot?

Thanks in advanced.

You can use the viewer.getScreenshot() function, very similar to the V2 function.
For example, if you have a session with the default id 'model-view-viewer':

SDV.api.viewers['model-view-viewer'].getScreenshot();

You can directly test this in the console of the model view page on the platform.

Thanks.

However, we have additional problem with adding screenshot to PDF. We want to add a screenshot in the pdf data sheet. In API V2, we used: api.parameters.updateAsync({name:“Screenshot_proba”,value:file}), where file is base 64 image file type image/png. We tried to update the same parameter in API V3 with the same file value but it either returns error: “Parameter(a71d0391-b23e-4cc3-8c2b-7dbdec3195a5).isValid: Input could not be validated. [object File] is not of type file”; or inserts completely black screenshot in pdf. We used the following code:
session.parameters[" a71d0391-b23e-4cc3-8c2b-7dbdec3195a5 "].value = file;
await session.customize();
Any suggestions, what could be the problem?

For safety reasons, it is not possible to pass a base64 image directly as a file parameter. You could use the fetch API for example to convert the base64 string to a Blob, and then pass it as a parameter. This code works:

var screenshot=viewer.getScreenshot();
var imageFile;
fetch(screenshot=viewer.getScreenshot).then(res => res.blob()).then(data => imageFile=data);
session.parameters[PARAM_ID].value= imageFile;
session.customize();