Hide menu
Loading...
Searching...
No Matches
exploring/drawings/drawings.py

Refer to the Drawing Exploration Example.

1# $Id$
2
3# Copyright (C) 2008-2014, Roman Lygin. All rights reserved.
4# Copyright (C) 2014-2025, 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 os
31import sys
32
33from pathlib import Path
34
35import manufacturingtoolkit.CadExMTK as mtk
36
37sys.path.append(os.path.abspath(os.path.dirname(Path(__file__).resolve()) + "/../../"))
38
39import mtk_license as license
40
41# Explore view content
42class DrawingElementVisitor(mtk.Drawing_ElementVoidVisitor):
43 def __init__(self):
44 super().__init__()
45 self.myCurveSetCounter = 0
46
47 def VisitCurveSet(self, theElement: mtk.Drawing_CurveSet):
48 print (f"------- CurveSet <{self.myCurveSetCounter}>")
49 print (f"------- number of curves: {theElement.NumberOfCurves()}")
50 self.myCurveSetCounter += 1
51
52# Iterate views
53def ExploreSheet(theSheet: mtk.Drawing_Sheet):
54 aViewIt = mtk.Drawing_Sheet_ViewIterator(theSheet)
55 aViewCounter = 0
56 while aViewIt.HasNext():
57 aView = aViewIt.Next()
58 print (f"---- View <{aViewCounter}>")
59 aVisitor = DrawingElementVisitor()
60 aView.Accept(aVisitor)
61 aViewCounter += 1
62
63# Iterate sheets
64def ExploreDrawing(theDrawing: mtk.Drawing_Drawing):
65 aSheetIt = mtk.Drawing_Drawing_SheetIterator(theDrawing)
66 aSheetCounter = 0
67 while (aSheetIt.HasNext()):
68 aSheet = aSheetIt.Next()
69 print (f"-- Sheet <{aSheetCounter}>")
70 ExploreSheet(aSheet)
71 aSheetCounter += 1
72
73def main(theSource: str):
74 aKey = license.Value()
75
76 if not mtk.LicenseManager.Activate(aKey):
77 print("Failed to activate Manufacturing Toolkit license.")
78 return 1
79
80 # Enable drawings reading
81 aReaderParameters = mtk.ModelData_ModelReaderParameters()
82 aReaderParameters.SetReadDrawing(True)
83 aReader = mtk.ModelData_ModelReader()
84 aReader.SetParameters(aReaderParameters)
85
86 aModel = mtk.ModelData_Model()
87
88 if not aReader.Read (mtk.UTF16String(theSource), aModel):
89 print(f"Failed to read the file {theSource}")
90 return 1
91
92 aDrawing = aModel.Drawing()
93 if aDrawing.IsNull():
94 print("The model doesn't contain a drawing")
95 return 1
96
97 print(f"Drawing \"{aModel.Name()}\":")
98 ExploreDrawing (aDrawing)
99
100 return 0
101
102if __name__ == "__main__":
103 if len(sys.argv) != 2:
104 print("Usage: <input_file>, where:")
105 print(" <input_file> is a name of the file to be read")
106 sys.exit()
107
108 aSource = os.path.abspath(sys.argv[1])
109
110 sys.exit(main(aSource))