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
32
33from pathlib import Path
34
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
44def WriteToDrawing(theFlatPattern: mtk.SheetMetal_FlatPattern, theDrawingFileName: str):
45 if not theFlatPattern:
46 print(" Failed to create flat pattern.")
47
48 aDrawingParams = mtk.DrawingParameters()
49 aDrawingParams.SetIsIgnoreBendingLines(True)
50
51 aDrawing = theFlatPattern.ToDrawing(aDrawingParams)
52
53 aDrawingModel = mtk.ModelData_Model()
54 aDrawingModel.SetDrawing(aDrawing)
55
56 aWriter = mtk.ModelData_ModelWriter()
57 if aWriter.Write(aDrawingModel, theDrawingFileName):
58 print("A drawing of the flat pattern has been saved to ", str(theDrawingFileName), sep="")
59 else:
60 print("Failed to save drawing of the flat pattern to ", str(theDrawingFileName), sep="")
61
62def PrintFlatPatternInfo(theFlatPattern: mtk.SheetMetal_FlatPattern):
63 if not theFlatPattern:
64 print(" Failed to create flat pattern.")
65 return
66
67 print(" Flat Pattern with:")
68 print(" length: ", theFlatPattern.Length(), " mm", sep="")
69 print(" width: ", theFlatPattern.Width(), " mm", sep="")
70 print(" thickness: ", theFlatPattern.Thickness(), " mm", sep="")
71 print(" perimeter: ", theFlatPattern.Perimeter(), " mm", sep="")
72
73class PartProcessor(mtk.ModelData_ModelElementVoidVisitor):
74 def __init__(self, theDrawingFolderPath: str):
75 super().__init__()
76 self.myPartIndex = 0
77 self.myUnfolder = mtk.SheetMetal_Unfolder()
78 self.myDrawingFolderPath = theDrawingFolderPath
79
80 def DrawingFileName(self, thePartName: str, theShapeIndex: str, theShapeName: str):
81 aPartName = "Part " + str(self.myPartIndex) + " [" + thePartName + "]"
82 aShapeName = theShapeName + " " + str(theShapeIndex)
83 aFileName = mtk.UTF16String(self.myDrawingFolderPath + "/" + aPartName + " - " + aShapeName + " - drawing.dxf")
84 return aFileName
85
86 def ProcessSolid(self, theSolid: mtk.ModelData_Solid, thePartName: str, theShapeIndex: int):
87 aFlatPattern = self.myUnfolder.Perform(theSolid)
88 PrintFlatPatternInfo(aFlatPattern)
89
90 aFileName = self.DrawingFileName(thePartName, theShapeIndex, "Solid")
91 WriteToDrawing(aFlatPattern, aFileName);
92
93 def ProcessShell(self, theShell: mtk.ModelData_Shell, thePartName: str, theShapeIndex: int):
94 aFlatPattern = self.myUnfolder.Perform(theShell)
95 PrintFlatPatternInfo(aFlatPattern)
96
97 aFileName = self.DrawingFileName(thePartName, theShapeIndex, "Shell")
98 WriteToDrawing(aFlatPattern, aFileName);
99
100 def VisitPart(self, thePart: mtk.ModelData_Part):
101 aPartName = "noname" if thePart.Name().IsEmpty() else str(thePart.Name())
102 aBodyList = thePart.Bodies()
103 i = 0
104 for aBody in aBodyList:
105 aShapeIt = mtk.ModelData_ShapeIterator(aBody)
106 for aShape in aShapeIt:
107 if aShape.Type() == mtk.ShapeType_Solid:
108 print("Part #", self.myPartIndex, " [\"", aPartName, "\"] - Solid #", i, " has:", sep="")
109 self.ProcessSolid(mtk.ModelData_Solid.Cast(aShape), aPartName, i)
110 i += 1
111 elif aShape.Type() == mtk.ShapeType_Shell:
112 print("Part #", self.myPartIndex, " [\"", aPartName, "\"] - Shell #", i, " has:", sep="")
113 self.ProcessShell(mtk.ModelData_Shell.Cast (aShape), aPartName, i)
114 i += 1
115 self.myPartIndex += 1
116
117def main(theSource: str, theDrawingPath: str):
118 aKey = license.Value()
119
120 if not LicenseHelper.SetupRuntimeKey() :
121 return 1
122
123 try:
124 mtk.LicenseManager.Activate(aKey)
125 except mtk.LicenseError as anException:
126 mtk.LicenseManager.Deactivate()
127 print("Failed to activate Manufacturing Toolkit license: " + anException.what())
128 return 1
129
130 aModel = mtk.ModelData_Model()
131 aReader = mtk.ModelData_ModelReader()
132
133
134 if not aReader.Read(mtk.UTF16String(theSource), aModel):
135 mtk.LicenseManager.Deactivate()
136 print("Failed to open and convert the file " + theSource)
137 return 1
138
139 print("Model: ", aModel.Name(), "\n", sep="")
140
141 aPartProcessor = PartProcessor(theDrawingPath)
142 aVisitor = mtk.ModelData_ModelElementUniqueVisitor(aPartProcessor)
143 aModel.Accept(aVisitor)
144
145 mtk.LicenseManager.Deactivate()
146
147 return 0
148
149if __name__ == "__main__":
150 if len(sys.argv) != 3:
151 print("Usage: <input_file> <output_folder>, where:")
152 print(" <input_file> is a name of the file to be read")
153 print(" <output_folder> is a name of the folder where DXF files with drawing to be written")
154 sys.exit()
155
156 aSource = os.path.abspath(sys.argv[1])
157 aRes = os.path.abspath(sys.argv[2])
158
159 sys.exit(main(aSource, aRes))