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