Hide menu
Loading...
Searching...
No Matches
utilities/thumbnail_generation/thumbnail_generation.py
Refer to the mtk_thumbnail_generation_example_page.
1# $Id$
2
3# Copyright (C) 2008-2014, Roman Lygin. All rights reserved.
4# Copyright (C) 2014-2026, CADEX. All rights reserved.
5
6# This file is part of the Manufacturing Toolkit software.
7
8# You may use this file under the terms of the BSD license as follows:
9
10# Redistribution and use in source and binary forms, with or without
11# modification, are permitted provided that the following conditions are met:
12# * Redistributions of source code must retain the above copyright notice,
13# this list of conditions and the following disclaimer.
14# * Redistributions in binary form must reproduce the above copyright notice,
15# this list of conditions and the following disclaimer in the documentation
16# and/or other materials provided with the distribution.
17
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28# POSSIBILITY OF SUCH DAMAGE.
29
30import sys
31import os
32from pathlib import Path
33
34import mtk.MTKCore as mtk
35import mtk.MTKView as view
36
37sys.path.append(os.path.abspath(os.path.dirname(Path(__file__).resolve()) + r"/../../"))
38sys.path.append(os.path.abspath(os.path.dirname(Path(__file__).resolve()) + "/../../helpers/"))
39
40import LicenseHelper
41
42import mtk_license as license
43
44def main(theSource: str, theDest: str):
45 aKey = license.Value()
46
47 if not LicenseHelper.SetupRuntimeKey() :
48 return 1
49
50 try:
51 mtk.LicenseManager.Activate(aKey)
52 except mtk.LicenseError as anException:
53 mtk.LicenseManager.Deactivate()
54 print("Failed to activate Manufacturing Toolkit license: " + anException.what())
55 return 1
56
57 aModel = mtk.ModelData_Model()
58
59 # Reading the file
60 if not mtk.ModelData_ModelReader().Read(mtk.UTF16String(theSource), aModel):
61 mtk.LicenseManager.Deactivate()
62 print("Failed to read the file " + theSource)
63 return 1
64
65 aWriter = view.View_ImageWriter()
66 aWriterParameters = view.View_ImageWriterParameters()
67
68 # Set writer parameters
69 aWriterParameters.SetImageWidth(750)
70 aWriterParameters.SetImageHeight(500)
71 aWriterParameters.SetViewIsFitAll(True)
72 aBackgroundColor = view.View_ColorBackgroundStyle(mtk.Materials_Color(255, 255, 255))
73 aWriterParameters.SetViewBackground(aBackgroundColor)
74
75 aWriter.SetParameters(aWriterParameters)
76
77 # Generate the model image and save it into the file
78 if not aWriter.WriteFile(aModel, mtk.UTF16String(theDest)):
79 mtk.LicenseManager.Deactivate()
80 print("Failed to write the image file " + theDest)
81 return 1
82
83 print("Thumbnail successfully generated: " + theDest)
84
85 mtk.LicenseManager.Deactivate()
86
87 return 0
88
89if __name__ == "__main__":
90 if len(sys.argv) != 3:
91 print(" <input_file> is a name of the model file to be read")
92 print(" <output_file> is a name of the image file to save the model")
93 sys.exit(1)
94
95 aSource = os.path.abspath(sys.argv[1])
96 aDest = os.path.abspath(sys.argv[2])
97
98 sys.exit(main(aSource, aDest))
99