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