1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30import sys
31import os
32
33from pathlib import Path
34
35import mtk.MTKCore as mtk
36
37sys.path.append(os.path.abspath(os.path.dirname(Path(__file__).resolve()) + "/../../"))
38sys.path.append(os.path.abspath(os.path.dirname(Path(__file__).resolve()) + "/../../helpers/"))
39
40import LicenseHelper
41
42import mtk_license as license
43
44class PartMeshVisitor(mtk.ModelData_ModelElementVoidVisitor):
45 def VisitPart(self, thePart: mtk.ModelData_Part):
46 print(f"Part = \"{thePart.Name()}\"")
47 aBodies = thePart.Bodies()
48 if aBodies.size() > 0:
49 self.ExploreMeshBodies(aBodies)
50
51 def ExploreMeshBodies(self, theBodies: mtk.Collections_BodyList):
52 for i in range(theBodies.size()):
53 aBody = theBodies[i]
54 if mtk.ModelData_MeshBody.CompareType(aBody):
55 aMeshBody = mtk.ModelData_MeshBody.Cast(aBody)
56 print(f"MeshBody {i}")
57 aMeshShapes = aMeshBody.Shapes()
58 for j in range(aMeshShapes.size()):
59 aMeshShape = aMeshShapes[j]
60 print(f"MeshShape {j}", end="")
61 self.PrintMeshShapeInfo(aMeshShape)
62
63 def PrintMeshShapeInfo(self, theMeshShape: mtk.ModelData_MeshShape):
64 if mtk.ModelData_IndexedTriangleSet.CompareType(theMeshShape):
65 aITS = mtk.ModelData_IndexedTriangleSet.Cast(theMeshShape)
66 print(" IndexedTriangleSet type.")
67 self.DumpTriangleSet(aITS)
68 elif mtk.ModelData_PolylineSet.CompareType(theMeshShape):
69 aPLS = mtk.ModelData_PolylineSet.Cast(theMeshShape)
70 print(" PolyLineSet type")
71 self.DumpPolylineSet(aPLS)
72 elif mtk.ModelData_Polyline2dSet.CompareType(theMeshShape):
73 aPL2dS = mtk.ModelData_Polyline2dSet.Cast(theMeshShape)
74 print(" PolyLine2dSet type")
75 self.DumpPolyline2dSet(aPL2dS)
76 elif mtk.ModelData_PointSet.CompareType(theMeshShape):
77 aPPS = mtk.ModelData_PointSet.Cast(theMeshShape)
78 print(" PolyPointSet type")
79 self.DumpPointSet(aPPS)
80 else:
81 print(" Undefined type")
82
83 def DumpPointSet(self, thePS: mtk.ModelData_PointSet):
84 aNumberOfPoints = thePS.NumberOfPoints()
85 print(f"PointSet: {aNumberOfPoints} points")
86 for i in range(aNumberOfPoints):
87 aP = thePS.Point(i)
88 print(f"Point {i}: ({aP.X()}, {aP.Y()}, {aP.Z()})")
89
90 def DumpPolylineSet(self, thePLS: mtk.ModelData_PolylineSet):
91 aNumberOfPolylines = thePLS.NumberOfPolylines()
92 print(f"PolylineSet: {aNumberOfPolylines} polylines")
93 for i in range(aNumberOfPolylines):
94 print(f"Polyline {i}:")
95 print(" Node coordinates:")
96 aPoly = thePLS.Polyline(i)
97 for j in range(aPoly.NumberOfPoints()):
98 aP = aPoly.Point(j)
99 print(f"({aP.X()}, {aP.Y()}, {aP.Z()})")
100
101 def DumpPolyline2dSet(self, thePLS: mtk.ModelData_Polyline2dSet):
102 aNumberOfPolylines2d = thePLS.NumberOfPolylines()
103 print(f"Polyline2dSet: {aNumberOfPolylines2d} polylines")
104 for i in range(aNumberOfPolylines2d):
105 print(f"Polyline2d {i}:")
106 print(" Node coordinates:")
107 aPoly = thePLS.Polyline(i)
108 for j in range(aPoly.NumberOfPoints()):
109 aP = aPoly.Point(j)
110 print(f"({aP.X()}, {aP.Y()})")
111
112 def DumpTriangleSet(self, theITS: mtk.ModelData_IndexedTriangleSet):
113 aNumberOfTriangles = theITS.NumberOfTriangles()
114 print(f"IndexedTriangleSet: {aNumberOfTriangles} triangles:")
115 for i in range(aNumberOfTriangles):
116 print(f"Triangle {i}:")
117 for j in range(3):
118 aVI = theITS.TriangleVertexIndex(i, j)
119 aV = theITS.TriangleVertex(i, j)
120 print(f" Node {j}: Vertex {aVI} ({aV.X()}, {aV.Y()}, {aV.Z()})")
121 if theITS.HasNormals():
122 aN = theITS.TriangleVertexNormal(i, j)
123 print(f" Normal: ({aN.X()}, {aN.Y()}, {aN.Z()})")
124
125def main(theSource:str):
126 aKey = license.Value()
127
128 if not LicenseHelper.SetupRuntimeKey() :
129 return 1
130
131 try:
132 mtk.LicenseManager.Activate(aKey)
133 except mtk.LicenseError as anException:
134 mtk.LicenseManager.Deactivate()
135 print("Failed to activate Manufacturing Toolkit license: " + anException.what())
136 return 1
137
138 aModel = mtk.ModelData_Model()
139
140 if not mtk.ModelData_ModelReader().Read(mtk.UTF16String(theSource), aModel):
141 mtk.LicenseManager.Deactivate()
142 print("Failed to read the file " + theSource)
143 return 1
144
145 aVisitor = PartMeshVisitor()
146 aModel.Accept(aVisitor)
147
148 mtk.LicenseManager.Deactivate()
149
150 return 0
151
152if __name__ == "__main__":
153 if len(sys.argv) != 2:
154 print("Usage: <input_file>, where:")
155 print(" <input_file> is a name of the file to be read")
156 sys.exit()
157
158 aSource = os.path.abspath(sys.argv[1])
159 sys.exit(main(aSource))