Hide menu
Loading...
Searching...
No Matches
nesting/nesting_computer/nesting_computer.py

Refer to the Nesting 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
32from pathlib import Path
33
34import manufacturingtoolkit.CadExMTK as mtk
35
36sys.path.append(os.path.abspath(os.path.dirname(Path(__file__).resolve()) + "/../../"))
37
38import mtk_license as license
39
40class Pattern:
41 def __init__(self, theShape: mtk.Drawing_CurveSet, theName: str, theNumber: int):
42 self.myDrawingDrawing_View = mtk.Drawing_View()
43 self.myDrawingDrawing_View.Add(theShape)
44 self.myName = theName
45 self.myNumber = theNumber
46
47def main():
48 aKey = license.Value()
49
50 if not mtk.LicenseManager.Activate(aKey):
51 print("Failed to activate Manufacturing Toolkit license.")
52 return 1
53
54 aComputer = mtk.Nesting_Computer()
55
56 # Configuring nesting parameters
57 aParams = mtk.Nesting_ComputerParameters()
58 aParams.SetIterationCount(10) # Number of iterations for optimization process
59 aParams.SetGenerationSize(10) # Initial count of random layouts; larger values may improve optimization
60 aParams.SetMutationRate(0.5) # Probability of random shape rearrangement to escape local optima
61 aParams.SetPartToPartDistance(1.0) # Minimum distance between shapes
62 aParams.SetPartToSheetBoundaryDistance(1.0) # Minimum distance between shapes and sheet edges
63 aParams.SetMirrorControl(False) # Allows mirrored shapes to improve layout efficiency
64 aParams.SetRotationCount(4) # Number of allowed rotation angles (e.g., 4 allows 0°, 90°, 180°, and 270°)
65 aParams.SetCurveTolerance(10) # Side length of squares used for polygonal approximation of curves
66
67 aComputer.SetParameters(aParams)
68
69 # Define material size and number (e.g., 1 sheets of 100x100 mm)
70 aComputer.AddMaterial(100.0, 100.0, 1)
71
72 aPatterns = [
73 Pattern(CreateRectangle(50.0, 50.0), "Rectangle 50x50", 1),
74 Pattern(CreateRectangle(20.0, 10.0), "Rectangle 20x10", 10)
75 ]
76
77 PrintPatternsInfo(aPatterns)
78
79 # Load patterns into the computer
80 for aPattern in aPatterns:
81 aComputer.AddPattern(aPattern.myDrawingDrawing_View, aPattern.myNumber)
82
83 # Start the Nesting process
84 aData = aComputer.Perform()
85
86 # Print nesting information
87 PrintNestingInfo(aData)
88
89def CreateRectangle(theWidth: float, theHeight: float) -> mtk.Drawing_CurveSet:
90 aRectangle = mtk.Drawing_CurveSet()
91
92 aL1 = mtk.Geom_Line2d(mtk.Geom_Point2d(0, 0), mtk.Geom_Direction2d(1, 0))
93 aL1.SetTrim(0, theWidth)
94
95 aL2 = mtk.Geom_Line2d(mtk.Geom_Point2d(theWidth, 0), mtk.Geom_Direction2d(0, 1))
96 aL2.SetTrim(0, theHeight)
97
98 aL3 = mtk.Geom_Line2d(mtk.Geom_Point2d(theWidth, theHeight), mtk.Geom_Direction2d(-1, 0))
99 aL3.SetTrim(0, theWidth)
100
101 aL4 = mtk.Geom_Line2d(mtk.Geom_Point2d(0, theHeight), mtk.Geom_Direction2d(0, -1))
102 aL4.SetTrim(0, theHeight)
103
104 aRectangle.AddCurve(aL1)
105 aRectangle.AddCurve(aL2)
106 aRectangle.AddCurve(aL3)
107 aRectangle.AddCurve(aL4)
108
109 return aRectangle
110
111def PrintPatternsInfo(thePatterns: list):
112 print("------- Patterns Info -------")
113 for aPattern in thePatterns:
114 print(f"{aPattern.myName}: {aPattern.myNumber}")
115
116def PrintNestingInfo(theData: mtk.Nesting_Data):
117 print("\n------- Nesting Info -------")
118
119 aTotalEfficiency = 0.0
120 aTotalScrap = 0.0
121 aSheets = theData.Sheets()
122
123 for i in range(len(aSheets)):
124 print(f"# Sheet {i}")
125 print(f" Nested Parts: {aSheets[i].NestedParts()}")
126 aTotalScrap += aSheets[i].Scrap()
127 print(f" Scrap: {aSheets[i].Scrap() * 100}%")
128 aTotalEfficiency += aSheets[i].PlacementEfficiency()
129 print(f" Placement Efficiency: {aSheets[i].PlacementEfficiency() * 100}%\n")
130
131 print(f"Average Scrap: {aTotalScrap / len(aSheets) * 100}%")
132 print(f"Average Placement Efficiency: {aTotalEfficiency / len(aSheets) * 100}%")
133
134if __name__ == "__main__":
135 main()