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 os
31import sys
32from pathlib import Path
33
34import manufacturingtoolkit.MTKCore as mtk
35
36sys.path.append(os.path.abspath(os.path.dirname(Path(__file__).resolve()) + "/../../"))
37import mtk_license as license
38
39def main(theSource: str, theOutFolder: str) -> int:
40 aKey = license.Value()
41
42
43 if not mtk.LicenseManager.Activate(aKey):
44 print("Failed to activate Manufacturing Toolkit license.")
45 return 1
46
47 aModel = mtk.ModelData_Model()
48 aReader = mtk.ModelData_ModelReader()
49
50
51 if not aReader.Read(mtk.UTF16String(theSource), aModel):
52 print("Failed to read the file " + theSource)
53 return 1
54
55 print("Model: " + str(aModel.Name()) + "\n")
56
57 aProjectionDataCollection = [
58 (mtk.UTF16String("X"), mtk.Geom_Direction.XDir()),
59 (mtk.UTF16String("Y"), mtk.Geom_Direction.YDir()),
60 (mtk.UTF16String("Z"), mtk.Geom_Direction.ZDir()),
61 ]
62
63 for aProjectionName, aProjectionDirection in aProjectionDataCollection:
64 anOutFilePath = mtk.UTF16String(theOutFolder + "projection_" + str(aProjectionName) + ".stl")
65
66 if ComputeProjection(aModel, aProjectionDirection, aProjectionName, anOutFilePath):
67 print("The output projection is written: " + str(anOutFilePath) + "\n")
68 else:
69 print("Failed to save the result: " + str(anOutFilePath) + "\n")
70
71 return 0
72
73if __name__ == "__main__":
74 if len(sys.argv) != 3:
75 print("Usage: " + sys.argv[0] + " <input_file> <output_folder>, where:")
76 print(" <input_file> is a name of the file to be read")
77 print(" <output_folder> is a folder to save projections to (must end with '/' or '\\')")
78 sys.exit(1)
79
80 aSource = os.path.abspath(sys.argv[1])
81 aOutFolder = os.path.abspath(sys.argv[2])
82
83 sys.exit(main(aSource, aOutFolder))
84
85def PrintProjectionInfo(thePart: mtk.ModelData_Part,
86 theDirectionName: mtk.UTF16String,
87 theProjection: mtk.Projector_Projection) -> None:
88 print(f"Part [{thePart.Name()}], projection {theDirectionName}:")
89 if theProjection:
90 print(f" area = {theProjection.Area()} mm")
91 print(f" outer perimeter = {theProjection.OuterPerimeter()} mm")
92 else:
93 print(" undefined")
94 print("")
95
96class ProjectionComputer(mtk.ModelData_ModelElementVoidVisitor):
97 def __init__(self, theDirection: mtk.Geom_Direction, theDirectionName: mtk.UTF16String):
98 super().__init__()
99 self.myDirection = theDirection
100 self.myDirectionName = theDirectionName
101 self.myProjector = mtk.Projector_PolyProjector()
102 self.myPartProjections = []
103
104 def SaveProjection(self, theFileName: mtk.UTF16String) -> bool:
105 if len(self.myPartProjections) == 0:
106 return False
107
108 aPart = mtk.ModelData_Part(mtk.UTF16String("Projections"))
109
110 for aProjection in self.myPartProjections:
111 aMeshBody = mtk.ModelData_MeshBody(aProjection.Mesh())
112 aPart.AddBody(aMeshBody)
113
114 anOutModel = mtk.ModelData_Model(mtk.UTF16String("Projector"))
115 anOutModel.AddRoot(aPart)
116
117 aWriter = mtk.ModelData_ModelWriter()
118 return aWriter.Write(anOutModel, theFileName)
119
120 def VisitPart(self, thePart: mtk.ModelData_Part):
121 aProjection = self.myProjector.Perform(thePart, self.myDirection)
122 PrintProjectionInfo(thePart, self.myDirectionName, aProjection)
123 if aProjection:
124 self.myPartProjections.append(aProjection)
125
126def ComputeProjection(theModel: mtk.ModelData_Model,
127 theDirection: mtk.Geom_Direction,
128 theDirectionName: mtk.UTF16String,
129 theOutFileName: mtk.UTF16String) -> bool:
130 aComputer = ProjectionComputer(theDirection, theDirectionName)
131 theModel.Accept(aComputer)
132 return aComputer.SaveProjection(theOutFileName)