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 self.myHatchCounter = 0
47 self.myTextCounter = 0
48
49 def VisitCurveSet(self, theElement: mtk.Drawing_CurveSet):
50 print (f"------- CurveSet <{self.myCurveSetCounter}>")
51 print (f"--------- number of curves: {theElement.NumberOfCurves()}")
52 self.myCurveSetCounter += 1
53
54 def VisitHatch(self, theElement: mtk.Drawing_Hatch):
55 print (f"------- Hatch <{self.myHatchCounter}>")
56 print (f"--------- number of contours: {theElement.NumberOfContours()}")
57 aNumberOfCurves = 0
58 for i in range (theElement.NumberOfContours()):
59 aNumberOfCurves += theElement.Contour (i).NumberOfCurves()
60 print (f"--------- number of curves: {aNumberOfCurves}")
61 print (f"--------- pattern: {self.PatternToString (theElement.Pattern())}")
62 print (f"--------- angle: {theElement.Angle()}")
63 print (f"--------- scale: {theElement.Scale()}")
64 self.myHatchCounter += 1
65
66 def VisitText(self, theElement: mtk.Drawing_Text):
67 print (f"------- Text <{self.myTextCounter}>")
68 print (f"--------- text: \"{theElement.GetText()}\"")
69 print (f"--------- origin: {self.PointToString(theElement.TextOrigin())}")
70 print (f"--------- rotation: {theElement.TextProperties().Rotation()}")
71 print (f"--------- font size: {theElement.FontSize()}")
72 self.myTextCounter += 1
73
74 def PointToString(self, thePoint: mtk.Geom_Point2d):
75 return f"({thePoint.X()}; {thePoint.Y()})"
76
77 def PatternToString(self, thePattern):
78 aMap = {
79 mtk.Drawing_Hatch.PatternType_Solid: "Solid",
80 mtk.Drawing_Hatch.PatternType_ANSI_31: "ANSI 31",
81 mtk.Drawing_Hatch.PatternType_ANSI_32: "ANSI 32",
82 mtk.Drawing_Hatch.PatternType_ANSI_33: "ANSI 33",
83 mtk.Drawing_Hatch.PatternType_ANSI_34: "ANSI 34",
84 mtk.Drawing_Hatch.PatternType_ANSI_35: "ANSI 35",
85 mtk.Drawing_Hatch.PatternType_ANSI_36: "ANSI 36",
86 mtk.Drawing_Hatch.PatternType_ANSI_37: "ANSI 37",
87 mtk.Drawing_Hatch.PatternType_ANSI_38: "ANSI 38",
88 mtk.Drawing_Hatch.PatternType_ISO_02: "ISO 02",
89 mtk.Drawing_Hatch.PatternType_ISO_03: "ISO 03",
90 mtk.Drawing_Hatch.PatternType_ISO_04: "ISO 04",
91 mtk.Drawing_Hatch.PatternType_ISO_05: "ISO 05",
92 mtk.Drawing_Hatch.PatternType_ISO_06: "ISO 06",
93 mtk.Drawing_Hatch.PatternType_ISO_07: "ISO 07",
94 mtk.Drawing_Hatch.PatternType_ISO_08: "ISO 08",
95 mtk.Drawing_Hatch.PatternType_ISO_09: "ISO 09",
96 mtk.Drawing_Hatch.PatternType_ISO_10: "ISO 10",
97 mtk.Drawing_Hatch.PatternType_ISO_11: "ISO 11",
98 mtk.Drawing_Hatch.PatternType_ISO_12: "ISO 12",
99 mtk.Drawing_Hatch.PatternType_ISO_13: "ISO 13",
100 mtk.Drawing_Hatch.PatternType_ISO_14: "ISO 14",
101 mtk.Drawing_Hatch.PatternType_ISO_15: "ISO 15"
102 }
103 if thePattern in aMap: return aMap[thePattern]
104 else: return "Other"
105
106# Iterate views
107def ExploreSheet(theSheet: mtk.Drawing_Sheet):
108 aViewIt = mtk.Drawing_Sheet_ViewIterator(theSheet)
109 aViewCounter = 0
110 while aViewIt.HasNext():
111 aView = aViewIt.Next()
112 print (f"---- View <{aViewCounter}>")
113 aVisitor = DrawingElementVisitor()
114 aView.Accept(aVisitor)
115 aViewCounter += 1
116
117# Iterate sheets
118def ExploreDrawing(theDrawing: mtk.Drawing_Drawing):
119 aSheetIt = mtk.Drawing_Drawing_SheetIterator(theDrawing)
120 aSheetCounter = 0
121 while (aSheetIt.HasNext()):
122 aSheet = aSheetIt.Next()
123 print (f"-- Sheet <{aSheetCounter}>")
124 ExploreSheet(aSheet)
125 aSheetCounter += 1
126
127def main(theSource: str):
128 aKey = license.Value()
129
130 if not mtk.LicenseManager.Activate(aKey):
131 print("Failed to activate Manufacturing Toolkit license.")
132 return 1
133
134 # Enable drawings reading
135 aReaderParameters = mtk.ModelData_ModelReaderParameters()
136 aReaderParameters.SetReadDrawing(True)
137 aReader = mtk.ModelData_ModelReader()
138 aReader.SetParameters(aReaderParameters)
139
140 aModel = mtk.ModelData_Model()
141
142 if not aReader.Read (mtk.UTF16String(theSource), aModel):
143 print(f"Failed to read the file {theSource}")
144 return 1
145
146 aDrawing = aModel.Drawing()
147 if aDrawing.IsNull():
148 print("The model doesn't contain a drawing")
149 return 1
150
151 print(f"Drawing \"{aModel.Name()}\":")
152 ExploreDrawing (aDrawing)
153
154 return 0
155
156if __name__ == "__main__":
157 if len(sys.argv) != 2:
158 print("Usage: <input_file>, where:")
159 print(" <input_file> is a name of the file to be read")
160 sys.exit()
161
162 aSource = os.path.abspath(sys.argv[1])
163
164 sys.exit(main(aSource))