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
30
31
32import sys
33from pathlib import Path
34import os
35
36import mtk.MTKCore as mtk
37
38sys.path.append(os.path.abspath(os.path.dirname(Path(__file__).resolve()) + "/../../"))
39sys.path.append(os.path.abspath(os.path.dirname(Path(__file__).resolve()) + "/../../helpers/"))
40
41import LicenseHelper
42
43import mtk_license as license
44
45import math
46
47class FirstFaceGetter(mtk.ModelData_ModelElementVoidVisitor):
48 def __init__(self):
49 mtk.ModelData_ModelElementVoidVisitor.__init__(self)
50 self.myFace = None
51
52 def VisitPart(self, thePart: mtk.ModelData_Part):
53 if self.myFace is None:
54 aBodies = thePart.Bodies()
55 if thePart.NumberOfBodies() > 0:
56 self.ExploreBRep(aBodies)
57
58 def ExploreBRep(self, theBodies: mtk.Collections_BodyList):
59 for aBody in theBodies:
60 aShapeIt = mtk.ModelData_ShapeIterator(aBody, mtk.ShapeType_Face)
61 for aShape in aShapeIt:
62 self.myFace = mtk.ModelData_Face.Cast(aShape)
63 break
64
65 def FirstFace(self):
66 return self.myFace
67
68def PrintFaceTriangulationInfo(theFace: mtk.ModelData_Face):
69 anITS = theFace.Triangulation()
70
71 print(f"Face triangulation contains {anITS.NumberOfTriangles()} triangles.")
72
73 aNumberOfTrianglesToPrint = min(4, anITS.NumberOfTriangles())
74
75 for i in range(aNumberOfTrianglesToPrint):
76 print(f"Triangle index {i} with vertices: ")
77 for j in range(3):
78 aVertexIndex = anITS.TriangleVertexIndex (i, j);
79 aPoint = anITS.TriangleVertex(i, j);
80 print(f" Vertex index {aVertexIndex} with coords",
81 f"(X: {aPoint.X()}, Y: {aPoint.Y()}, Z: {aPoint.Z()})")
82
83def main(theSource: str):
84 aKey = license.Value()
85
86 if not LicenseHelper.SetupRuntimeKey() :
87 return 1
88
89 try:
90 mtk.LicenseManager.Activate(aKey)
91 except mtk.LicenseError as anException:
92 mtk.LicenseManager.Deactivate()
93 print("Failed to activate Manufacturing Toolkit license: " + anException.what())
94 return 1
95
96 aModel = mtk.ModelData_Model()
97
98 if not mtk.ModelData_ModelReader().Read(mtk.UTF16String(theSource), aModel):
99 mtk.LicenseManager.Deactivate()
100 print("Failed to read the file " + theSource)
101 return 1
102
103
104 aParam = mtk.ModelAlgo_MeshGeneratorParameters()
105 aParam.SetAngularDeflection(math.pi * 10 / 180)
106 aParam.SetChordalDeflection(0.003)
107
108 aMesher = mtk.ModelAlgo_MeshGenerator(aParam)
109 aMesher.Generate(aModel)
110
111 aVisitor = FirstFaceGetter();
112 aModel.Accept(aVisitor);
113
114 aFace = aVisitor.FirstFace();
115 PrintFaceTriangulationInfo(aFace)
116
117 print("Completed")
118
119 mtk.LicenseManager.Deactivate()
120
121 return 0
122
123if __name__ == "__main__":
124 if len(sys.argv) != 2:
125 print("Usage: " + os.path.abspath(Path(__file__).resolve()) + " <input_file>, where:")
126 print(" <input_file> is a name of the file to be read")
127 sys.exit(1)
128
129 aSource = os.path.abspath(sys.argv[1])
130
131 sys.exit(main(aSource))