Hide menu
Loading...
Searching...
No Matches
utilities/progress_bar/progress_bar.py
Refer to the Progress Bar Example.
1#!/usr/bin/env python3
2
3# $Id$
4
5# Copyright (C) 2008-2014, Roman Lygin. All rights reserved.
6# Copyright (C) 2014-2026, CADEX. All rights reserved.
7
8# This file is part of the Manufacturing Toolkit 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
32import sys
33import os
34
35from pathlib import Path
36
37import mtk.MTKCore as mtk
38
39sys.path.append(os.path.abspath(os.path.dirname(Path(__file__).resolve()) + r"/../../"))
40sys.path.append(os.path.abspath(os.path.dirname(Path(__file__).resolve()) + "/../../helpers/"))
41
42import LicenseHelper
43
44import mtk_license as license
45
46class ProgressBarObserver(mtk.ProgressStatus_Observer):
47 def __init__(self):
48 super().__init__()
49
50 def ChangedValue(self, theInfo: mtk.ProgressStatus):
51 print(theInfo.Value())
52
53 def Completed(self, theInfo: mtk.ProgressStatus):
54 print(f"{theInfo.Value()}: complete!")
55
56def main(theSource: str):
57 aKey = license.Value()
58
59 if not LicenseHelper.SetupRuntimeKey() :
60 return 1
61
62 try:
63 mtk.LicenseManager.Activate(aKey)
64 except mtk.LicenseError as anException:
65 mtk.LicenseManager.Deactivate()
66 print("Failed to activate Manufacturing Toolkit license: " + anException.what())
67 return 1
68
69 aModel = mtk.ModelData_Model()
70
71 # An observer must outlive progress status,
72 # so it is created outside the using scope
73 anObserver = ProgressBarObserver()
74
75 with mtk.ProgressStatus() as aStatus:
76 # Register an observer to progress status
77 aStatus.Register(anObserver)
78
79 # The top scope occupies the whole progress status range
80 with mtk.ProgressScope(aStatus) as aTopScope:
81 # 50% of TopScope for file importing
82 with mtk.ProgressScope(aTopScope, 50) as aReaderScope:
83 aReader = mtk.ModelData_ModelReader()
84 # Connect progress status object
85 aReader.SetProgressStatus(aStatus)
86 aReader.Read(mtk.UTF16String(theSource), aModel)
87
88 if not aModel.IsEmpty() and not aStatus.WasCanceled():
89 # The remaining 50% of TopScope for meshing
90 with mtk.ProgressScope(aTopScope, 50):
91 aMesher = mtk.ModelAlgo_MeshGenerator()
92 # Connect progress status object
93 aMesher.SetProgressStatus(aStatus)
94 aMesher.Generate(aModel)
95
96 # Observer will be automatically unregistered from progress status on destruction (end of the "with" scope).
97
98 mtk.LicenseManager.Deactivate()
99
100 return 0
101
102if __name__ == "__main__":
103 if len(sys.argv) != 2:
104 print("Usage: " + os.path.abspath(Path(__file__).resolve()) + " <input_file>, where:")
105 print(" <input_file> is a name of the file to be read")
106 sys.exit(1)
107
108 aSource = os.path.abspath(sys.argv[1])
109
110 sys.exit(main(aSource))