Hide menu
Loading...
Searching...
No Matches
nesting/nesting_computer/main.cxx

Refer to the Nesting Example

// ****************************************************************************
// $Id$
//
// Copyright (C) 2008-2014, Roman Lygin. All rights reserved.
// Copyright (C) 2014-2025, CADEX. All rights reserved.
//
// This file is part of the Manufacturing Toolkit software.
//
// You may use this file under the terms of the BSD license as follows:
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// ****************************************************************************
#include <cadex/Base/UTF16String.hxx>
#include <cadex/Drawing/Drawing.hxx>
#include <cadex/Drawing/Geometry.hxx>
#include <cadex/Drawing/View.hxx>
#include <cadex/Geom/Line2d.hxx>
#include <cadex/Geom/Point2d.hxx>
#include <cadex/LicenseManager_Activate.h>
#include <cadex/Nesting_Computer.hxx>
#include <cadex/Nesting_ComputerParameters.hxx>
#include <cadex/Nesting_Data.hxx>
#include <iostream>
#include <vector>
#include "../../mtk_license.cxx"
using namespace cadex;
using namespace std;
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;
};
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;
}
void PrintPatternsInfo (const PatternVector& thePatterns)
{
cout << "------- Patterns Info -------" << endl;
for (const auto& aPattern : thePatterns) {
cout << aPattern.myName << ": " << aPattern.myNumber << endl;
}
}
void PrintNestingInfo (const Nesting_Data& theData)
{
cout << endl;
cout << "------- Nesting Info -------" << endl;
double aTotalEfficiency = 0.0;
double aTotalScrap = 0.0;
const auto& aSheets = theData.Sheets();
for (size_t i = 0; i < aSheets.size(); ++i) {
cout << "#" << i << " Sheet" << endl;
// Number of parts
cout << " Nested Parts: " << aSheets[i].NestedParts() << endl;
// Sheet free space precent
aTotalScrap += aSheets[i].Scrap();
cout << " Scrap: " << aSheets[i].Scrap() * 100 << "%" << endl;
// Profitable space usage percent
aTotalEfficiency += aSheets[i].PlacementEfficiency();
cout << " Placement Efficiency: " << aSheets[i].PlacementEfficiency() * 100 << "%" << endl;
cout << endl;
}
cout << "Average Scrap: " << aTotalScrap / aSheets.size() * 100 << "%" << endl;
cout << "Average Placement Efficiency: " << aTotalEfficiency / aSheets.size() * 100 << "%" << endl;
}
int main()
{
auto aKey = MTKLicenseKey::Value();
// Activate the license (aKey must be defined in mtk_license.cxx)
if (!CADExLicense_Activate (aKey)) {
cerr << "Failed to activate Manufacturing Toolkit license." << endl;
return 1;
}
// Creating a Nesting_Computer instance
Nesting_Computer aComputer;
// Configuring nesting parameters
aParams.SetIterationCount (10); // Number of iterations for optimization process; higher values improve results
aParams.SetGenerationSize (10); // Initial count of random layouts; larger values may improve optimization
aParams.SetMutationRate (0.5); // Probability of random shape rearrangement to escape local optima
aParams.SetPartToPartDistance (1.0); // Minimum distance between shapes
aParams.SetPartToSheetBoundaryDistance (1.0); // Minimum distance between shapes and sheet edges
aParams.SetMirrorControl (false); // Allows mirrored shapes to improve layout efficiency
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);
// Define material size and number (e.g., 1 sheets of 100x100 mm)
aComputer.AddMaterial (100.0, 100.0, 1);
auto aPatterns = PatternVector{Pattern (CreateRectangle (50.0, 50.0), "Rectangle 50x50", 1),
Pattern (CreateRectangle (20.0, 10.0), "Rectangle 20x10", 10)};
PrintPatternsInfo (aPatterns);
// 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();
// Estimation of the result nesting
PrintNestingInfo (aData);
return 0;
}
Describes drawing elements composed of 2D curves.
Definition Drawing/Geometry.hxx:43
void AddCurve(const Geom::Curve2d &theCurve)
Adds a curve to the element.
Definition Drawing/Geometry.cxx:71
Represents a view on a drawing sheet.
Definition View.hxx:38
Defines a 2D Direction.
Definition Direction2d.hxx:32
Defines 2D line.
Definition Line2d.hxx:32
Defines a 3D point.
Definition Point2d.hxx:34
The nesting analyzing tool.
Definition Nesting_Computer.hxx:41
void AddMaterial(double theLength, double theWidth, size_t theQuantity)
Definition Nesting_Computer.cxx:86
Nesting_Data Perform(const cadex::ProgressStatus &theProgressStatus=cadex::ProgressStatus())
Definition Nesting_Computer.cxx:126
void AddPattern(const Drawing::View &theDrawing, size_t theQuantity)
Definition Nesting_Computer.cxx:76
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 information about nesting sheets.
Definition Nesting_Data.hxx:39
const std::vector< Nesting_Sheet > & Sheets() const
Definition Nesting_Data.cxx:70
Defines a Unicode (UTF-16) string wrapping a standard string.
Definition UTF16String.hxx:30
Defines classes, namespaces, enums, types, and global functions related to Manufacturing Toolkit.
Definition LicenseManager_LicenseError.hxx:30