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