Hide menu
Loading...
Searching...
No Matches
sheet_metal/unfolder/unfolder.py

Refer to the Sheet Metal Unfolder Example

1# $Id$
2#
3# Copyright (C) 2008-2014, Roman Lygin. All rights reserved.
4# Copyright (C) 2014-2025, CADEX. All rights reserved.
5#
6# This file is part of the Manufacturing Toolkit software.
7#
8# You may use this file under the terms of the BSD license as follows:
9#
10# Redistribution and use in source and binary forms, with or without
11# modification, are permitted provided that the following conditions are met:
12# * Redistributions of source code must retain the above copyright notice,
13# this list of conditions and the following disclaimer.
14# * Redistributions in binary form must reproduce the above copyright notice,
15# this list of conditions and the following disclaimer in the documentation
16# and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28# POSSIBILITY OF SUCH DAMAGE.
29
30import os
31import sys
32
33from pathlib import Path
34
35import manufacturingtoolkit.CadExMTK as mtk
36
37sys.path.append(os.path.abspath(os.path.dirname(Path(__file__).resolve()) + "/../../"))
38
39import mtk_license as license
40
41def PrintFlatPatternInfo(theFlatPattern: mtk.SheetMetal_FlatPattern):
42 if theFlatPattern.IsNull():
43 print(" Failed to create flat pattern.")
44 return
45
46 print(" Flat Pattern with:")
47 print(" length: ", theFlatPattern.Length(), " mm", sep="")
48 print(" width: ", theFlatPattern.Width(), " mm", sep="")
49 print(" thickness: ", theFlatPattern.Thickness(), " mm", sep="")
50 print(" perimeter: ", theFlatPattern.Perimeter(), " mm", sep="")
51
52class PartProcessor(mtk.ModelData_ModelElementVoidVisitor):
53 def __init__(self, theDrawingFolderPath: str):
54 super().__init__()
55 self.myPartIndex = 0
56 self.myUnfolder = mtk.SheetMetal_Unfolder()
57 self.myDrawingFolderPath = theDrawingFolderPath
58
59 def ProcessSolid(self, theSolid: mtk.ModelData_Solid, thePartName: str, theShapeIndex: int):
60 aFlatPattern = self.myUnfolder.Perform(theSolid)
61 PrintFlatPatternInfo(aFlatPattern)
62
63 def ProcessShell(self, theShell: mtk.ModelData_Shell, thePartName: str, theShapeIndex: int):
64 aFlatPattern = self.myUnfolder.Perform(theShell)
65 PrintFlatPatternInfo(aFlatPattern)
66
67 def VisitPart(self, thePart: mtk.ModelData_Part):
68 aPartName = "noname" if thePart.Name().IsEmpty() else str(thePart.Name())
69 aBodyList = thePart.Bodies()
70 i = 0
71 for aBody in aBodyList:
72 aShapeIt = mtk.ModelData_ShapeIterator(aBody)
73 for aShape in aShapeIt:
74 if aShape.Type() == mtk.ShapeType_Solid:
75 print("Part #", self.myPartIndex, " [\"", aPartName, "\"] - solid #", i, " has:", sep="")
76 self.ProcessSolid(mtk.ModelData_Solid.Cast(aShape), aPartName, i)
77 i += 1
78 elif aShape.Type() == mtk.ShapeType_Shell:
79 print("Part #", self.myPartIndex, " [\"", aPartName, "\"] - shell #", i, " has:", sep="")
80 self.ProcessShell(mtk.ModelData_Shell.Cast (aShape), aPartName, i)
81 i += 1
82 self.myPartIndex += 1
83
84def main(theSource: str, theDrawingPath: str):
85 aKey = license.Value()
86
87 if not mtk.LicenseManager.Activate(aKey):
88 print("Failed to activate Manufacturing Toolkit license.")
89 return 1
90
91 aModel = mtk.ModelData_Model()
92 aReader = mtk.ModelData_ModelReader()
93
94 # Reading the file
95 if not aReader.Read(mtk.UTF16String(theSource), aModel):
96 print("Failed to open and convert the file " + theSource)
97 return 1
98
99 print("Model: ", aModel.Name(), "\n", sep="")
100
101 aPartProcessor = PartProcessor(theDrawingPath)
102 aVisitor = mtk.ModelData_ModelElementUniqueVisitor(aPartProcessor)
103 aModel.Accept(aVisitor)
104
105 return 0
106
107if __name__ == "__main__":
108 if len(sys.argv) != 3:
109 print("Usage: <input_file> <output_folder>, where:")
110 print(" <input_file> is a name of the file to be read")
111 print(" <output_folder> is a name of the folder where DXF files with drawing to be written")
112 sys.exit()
113
114 aSource = os.path.abspath(sys.argv[1])
115 aRes = os.path.abspath(sys.argv[2])
116
117 sys.exit(main(aSource, aRes))