Hide menu
Loading...
Searching...
No Matches
visualization/offscreen/offscreen.py

Refer to the Offscreen Rendering Example.

1#!/usr/bin/env python3
2
3# $Id$
4
5# Copyright (C) 2008-2014, Roman Lygin. All rights reserved.
6# Copyright (C) 2014-2023, CADEX. All rights reserved.
7
8# This file is part of the CAD Exchanger software.
9
10# You may use this file under the terms of the BSD license as follows:
11
12# Redistribution and use in source and binary forms, with or without
13# modification, are permitted provided that the following conditions are met:
14# * Redistributions of source code must retain the above copyright notice,
15# this list of conditions and the following disclaimer.
16# * Redistributions in binary form must reproduce the above copyright notice,
17# this list of conditions and the following disclaimer in the documentation
18# and/or other materials provided with the distribution.
19
20# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30# POSSIBILITY OF SUCH DAMAGE.
31
32
33import sys
34from pathlib import Path
35import os
36
37import cadexchanger.CadExCore as cadex
38import cadexchanger.CadExView as view
39
40sys.path.append(os.path.abspath(os.path.dirname(Path(__file__).resolve()) + r"/../../"))
41
42def main(theSource: str, theDest: str):
43 anAbsolutePathToRuntimeKey = os.path.abspath(os.path.dirname(Path(__file__).resolve()) + r"/runtime_key.lic")
44 if not cadex.LicenseManager.CADExLicense_ActivateRuntimeKeyFromAbsolutePath(anAbsolutePathToRuntimeKey):
45 print("Failed to activate CAD Exchanger license.")
46 return 1
47
49 aModel = cadex.ModelData_Model()
50
51 # Reading the file
52 if not aReader.Read(cadex.Base_UTF16String(theSource), aModel):
53 print("Failed to read the file " + theSource)
54 return 1
55
56 # Convert model into visualization entities
57 aFactory = view.ModelPrs_SceneNodeFactory()
58 aRootNode = aFactory.CreateGraph(aModel, cadex.ModelData_RM_Any)
59 aRootNode.SetDisplayMode(view.ModelPrs_DM_ShadedWithBoundaries)
60
61 # Create scene and display all entities
62 aScene = view.ModelPrs_Scene()
63 aScene.AddRoot(aRootNode)
64
65 # Setup offscreen viewport with transparent background and perspective camera
66 aViewPort = view.ModelPrs_OffscreenViewPort()
67 aViewPort.Resize(800, 600)
68 aViewPort.SetCameraProjectionType(view.ModelPrs_CPT_Perspective)
69 aViewPort.SetCameraPositionType(view.ModelPrs_CMT_Default)
70 aBackgroundColor = cadex.ModelData_Color(0x00000000)
71 aStyle = view.ModelPrs_BackgroundStyle(aBackgroundColor)
72 aViewPort.SetBackgroundStyle(aStyle)
73
74 # Attach viewport to the scene
75 if not aViewPort.AttachToScene(aScene):
76 print("Unable to attach viewport to scene")
77 return 1
78
79 # Apply scene changes to viewport and wait until all async operations will be finished
80 aScene.Update()
81 aScene.Wait()
82
83 # Fit and center model on the image
84 aViewPort.FitAll()
85
86 # Grab rendered frame into image
87 if not aViewPort.GrabToImage(cadex.Base_UTF16String(theDest)):
88 print("Failed to write the file " + theDest)
89 return 1
90
91 return 0
92
93if __name__ == "__main__":
94 if len(sys.argv) != 2:
95 print(" <input_file> is a name of the file to be read")
96 print(" <output_file> is a name of the PNG file to save the model")
97 sys.exit(1)
98
99 aSource = os.path.abspath(sys.argv[1])
100 aDest = os.path.abspath(sys.argv[2])
101
102 sys.exit(main(aSource, aDest))
Defines a Unicode (UTF-16) string wrapping a standard string.
Definition: Base_UTF16String.hxx:34
Defines an RGBA color (with alpha channel).
Definition: ModelData_Color.hxx:34
Provides CAD Exchanger data model.
Definition: ModelData_Model.hxx:41
Reads any format that CAD Exchanger can import.
Definition: ModelData_ModelReader.hxx:33