Hide menu
Loading...
Searching...
No Matches
Nesting Example

Demonstrates how to optimize the arrangement of patterns on material sheets minimizing waste.

Overview

In this example demonstrates how to optimize the layout of 2D patterns (drawings) on material sheets using the nesting tool (Nesting_Computer ). For this purpose, a console application is used, which defines nesting parameters, creates patterns, and executes the nesting process. The tool computes an optimal arrangement of parts to minimize material waste and maximize placement efficiency.


For more information about nesting, refer to the Nesting Process.

Implementation

The nesting process begins by creating an instance of the Nesting_Computer class, which serves as the primary tool for optimizing the arrangement of 2D patterns. This is followed by defining and configuring the nesting parameters using the Nesting_ComputerParameters class:

using namespace cadex;
Nesting_Computer aComputer;
// Configuring nesting parameters
aParams.SetIterationCount (10); // Number of iterations for optimization process
aParams.SetGenerationSize (10); // Initial count of random layouts
aParams.SetMutationRate (0.5); // Probability of random patterns rearrangement to escape local optima
aParams.SetPartToPartDistance (1.0); // Minimum distance between patterns
aParams.SetPartToSheetBoundaryDistance (1.0); // Minimum distance between patterns and sheet edges
aParams.SetMirrorControl (false); // Enables or disables the use of mirrored patterns
aParams.SetRotationCount (4); // Number of allowed rotation angles (e.g., 4 allows 0°, 90°, 180°, and 270°)
aParams.SetCurveTolerance (10); // Side length of squares used for polygonal approximation of curves
aComputer.SetParameters (aParams);
The nesting analyzing tool.
Definition Nesting_Computer.hxx:41
Defines parameters used in nesting process.
Definition Nesting_ComputerParameters.hxx:34
void SetPartToPartDistance(double thePartToPartDistance)
Definition Nesting_ComputerParameters.cxx:54
void SetIterationCount(size_t theIterationCount)
Definition Nesting_ComputerParameters.cxx:76
void SetCurveTolerance(double theCurveTolerance)
Definition Nesting_ComputerParameters.cxx:142
void SetPartToSheetBoundaryDistance(double thePartToSheetBoundaryDistance)
Definition Nesting_ComputerParameters.cxx:164
void SetMutationRate(double theMutationRate)
Definition Nesting_ComputerParameters.cxx:120
void SetRotationCount(size_t theRotationCount)
Definition Nesting_ComputerParameters.cxx:227
void SetMirrorControl(bool theIsMirrorControl)
Definition Nesting_ComputerParameters.cxx:206
void SetGenerationSize(size_t theGenerationSize)
Definition Nesting_ComputerParameters.cxx:98
Contains classes, namespaces, enums, types, and global functions related to Manufacturing Toolkit.
Definition LicenseManager_LicenseError.hxx:30

The dimensions and number of sheets are defined using AddMaterial() method:

using namespace cadex;
// Define material size and number (e.g., 1 sheets of 100x100 mm)
aComputer.AddMaterial (100.0, 100.0, 1);
void AddMaterial(double theLength, double theWidth, size_t theQuantity)
Definition Nesting_Computer.cxx:87

A Pattern is essentially a Drawing::View combined with metadata, such as the pattern name and number:

using namespace cadex;
class Pattern
{
public:
Pattern (const Drawing::CurveSet theShape, const UTF16String theName, size_t theNumber) :
myName (theName),
myNumber (theNumber)
{
myDrawingView.Add (theShape);
}
Drawing::View myDrawingView;
UTF16String myName;
size_t myNumber;
};
Describes drawing elements composed of 2D curves.
Definition Drawing/Geometry.hxx:43
Represents a view on a drawing sheet.
Definition View.hxx:38
Defines a Unicode (UTF-16) string wrapping a standard string.
Definition UTF16String.hxx:30

Rectangular patterns are created using the CreateRectangle function and added to a PatternVector:

using namespace cadex;
using PatternVector = vector<Pattern>;
Drawing::CurveSet CreateRectangle (double theWidth, double theHeight)
{
Drawing::CurveSet aRectangle;
aL1.SetTrim (0, theWidth);
Geom::Line2d aL2 (Geom::Point2d (theWidth, 0), Geom::Direction2d (0, 1));
aL2.SetTrim (0, theHeight);
Geom::Line2d aL3 (Geom::Point2d (theWidth, theHeight), Geom::Direction2d (-1, 0));
aL3.SetTrim (0, theWidth);
Geom::Line2d aL4 (Geom::Point2d (0, theHeight), Geom::Direction2d (0, -1));
aL4.SetTrim (0, theHeight);
aRectangle.AddCurve (aL1);
aRectangle.AddCurve (aL2);
aRectangle.AddCurve (aL3);
aRectangle.AddCurve (aL4);
return aRectangle;
}
auto aPatterns = PatternVector{Pattern (CreateRectangle (50.0, 50.0), "Rectangle 50x50", 1),
Pattern (CreateRectangle (20.0, 10.0), "Rectangle 20x10", 10)};
void AddCurve(const Geom::Curve2d &theCurve)
Adds a curve to the element.
Definition Drawing/Geometry.cxx:71
Defines a 2D Direction.
Definition Direction2d.hxx:32
Defines 2D line.
Definition Line2d.hxx:32
Defines a 3D point.
Definition Point2d.hxx:34

The patterns are loaded into the Nesting_Computer , which performs the nesting process and generates a Nesting_Data object containing the results.

using namespace cadex;
// Loading part patterns (rectangles) into the computer
for (const auto& aPattern : aPatterns) {
aComputer.AddPattern (aPattern.myDrawingView, aPattern.myNumber);
}
// Start the Nesting process
Nesting_Data aData = aComputer.Perform();
Nesting_Data Perform(const cadex::ProgressStatus &theProgressStatus=cadex::ProgressStatus())
Definition Nesting_Computer.cxx:127
void AddPattern(const Drawing::View &theDrawing, size_t theQuantity)
Definition Nesting_Computer.cxx:77
Contains information about nesting sheets.
Definition Nesting_Data.hxx:39

Example output

Below is the example output:

------- Patterns Info -------
Rectangle 50x50: 1
Rectangle 20x10: 10
------- Nesting Info -------
#0 Sheet
Nested Parts: 11
Scrap: 55%
Placement Efficiency: 90.58%
Average Scrap: 55%
Average Placement Efficiency: 90.58%

Files