Hide menu
Loading...
Searching...
No Matches
sheet_metal/unfolder/main.cxx

Refer to the Sheet Metal Unfolder Example

// ****************************************************************************
// $Id$
//
// Copyright (C) 2008-2014, Roman Lygin. All rights reserved.
// Copyright (C) 2014-2023, CADEX. All rights reserved.
//
// This file is part of the CAD Exchanger 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/LicenseManager_Activate.h>
#include <cadex/ModelAlgo_ValidationProperty.hxx>
#include <cadex/ModelData_Body.hxx>
#include <cadex/ModelData_BodyList.hxx>
#include <cadex/ModelData_BRepRepresentation.hxx>
#include <cadex/ModelData_Drawing.hxx>
#include <cadex/ModelData_ModelReader.hxx>
#include <cadex/ModelData_ModelWriter.hxx>
#include <cadex/ModelData_SceneGraphElementUniqueVisitor.hxx>
#include <cadex/ModelData_Shell.hxx>
#include <cadex/ModelData_Solid.hxx>
#include <cadex/SheetMetal_FlatPattern.hxx>
#include <cadex/SheetMetal_Unfolder.hxx>
#include <iostream>
#include <vector>
#include "../../../cadex_license.cxx"
#include "../../../mtk_license.cxx"
using namespace cadex;
using namespace std;
bool WriteToDrawing (const SheetMetal_FlatPattern& theFlatPattern, const Base_UTF16String& theFilePath)
{
ModelData_Drawing aDrawing = theFlatPattern.ToDrawing();
if (!aDrawing) {
return false;
}
ModelData_Model aDrawingModel;
aDrawingModel.SetDrawing (aDrawing);
bool aRes = aWriter.Write (aDrawingModel, theFilePath);
return aRes;
}
void PrintFlatPattern (const SheetMetal_FlatPattern& theFlatPattern)
{
cout << " Flat Pattern with:" << endl;
cout << " length: " << theFlatPattern.Length() << " mm" << endl;
cout << " width: " << theFlatPattern.Width() << " mm" << endl;
cout << " thickness: " << theFlatPattern.Thickness() << " mm" << endl;
cout << " perimeter: " << theFlatPattern.Perimeter() << " mm" << endl;
}
void PrintFlatPatternAndWriteToDrawing (const SheetMetal_FlatPattern& theFlatPattern,
const Base_UTF16String& theDrawingFileName)
{
if (!theFlatPattern) {
cerr << " Failed to create flat pattern.";
return;
}
PrintFlatPattern (theFlatPattern);
if (WriteToDrawing (theFlatPattern, theDrawingFileName)) {
cout << " A drawing of the flat pattern has been saved to " << theDrawingFileName << endl;
} else {
cerr << " Failed to save drawing of the flat pattern to " << theDrawingFileName << endl;
}
}
//Compute approximate thickness value, which can be used as the input thickness value for SheetMetal_Unfolder.
double CalculateInitialThicknessValue (const ModelData_Shape& theShape)
{
double aVolume = ModelAlgo_ValidationProperty::ComputeVolume (theShape);
double aSurfaceArea = ModelAlgo_ValidationProperty::ComputeSurfaceArea (theShape);
double aThickness = aVolume / (aSurfaceArea / 2.);
return aThickness;
}
class PartProcessor : public ModelData_Model::VoidElementVisitor
{
public:
PartProcessor (const Base_UTF16String& theDrawingFolderPath) :
myDrawingFolderPath (theDrawingFolderPath)
{}
void operator() (const ModelData_Part& thePart) override
{
auto aPartName = thePart.Name().IsEmpty() ? "noname" : thePart.Name();
auto aBRep = thePart.BRepRepresentation();
if (aBRep) {
const auto& aBodyList = aBRep.Get();
for (size_t i = 0, n = aBodyList.Size(); i < n; ++i) {
const auto& aBody = aBodyList[i];
ModelData_Shape::Iterator aShapeIt (aBody);
while (aShapeIt.HasNext()) {
const auto& aShape = aShapeIt.Next();
if (aShape.Type() == ModelData_ST_Solid) {
cout << "Part #" << myPartIndex << " [\"" << aPartName << "\"] - solid #" << std::to_string (i) << " has:" << endl;
ProcessSolid (ModelData_Solid::Cast (aShape), aPartName, i);
} else if (aShape.Type() == ModelData_ST_Shell) {
cout << "Part #" << myPartIndex << " [\"" << aPartName << "\"] - shell #" << std::to_string (i) << " has:" << endl;
ProcessShell (ModelData_Shell::Cast (aShape), aPartName, i);
}
}
}
}
++myPartIndex;
}
void ProcessSolid (const ModelData_Solid& theSolid, const Base_UTF16String& thePartName, size_t theShapeIndex)
{
double aThickness = CalculateInitialThicknessValue (theSolid);
auto aFlatPattern = myUnfolder.Perform (theSolid, aThickness);
Base_UTF16String aFileName = DrawingFileName (thePartName, theShapeIndex, "solid");
PrintFlatPatternAndWriteToDrawing (aFlatPattern, aFileName);
}
void ProcessShell (const ModelData_Shell& theShell, const Base_UTF16String& thePartName, size_t theShapeIndex)
{
auto aFlatPattern = myUnfolder.Perform (theShell);
Base_UTF16String aFileName = DrawingFileName (thePartName, theShapeIndex, "shell");
PrintFlatPatternAndWriteToDrawing (aFlatPattern, aFileName);
}
private:
Base_UTF16String DrawingFileName (const Base_UTF16String& thePartName,
size_t theShapeIndex, const Base_UTF16String& theShapeName)
{
Base_UTF16String aPartName = Base_UTF16String ("Part ") + std::to_string (myPartIndex).c_str() + " [" + thePartName + "]";
Base_UTF16String aShapeName = theShapeName + " " + std::to_string (theShapeIndex).c_str();
Base_UTF16String aFileName = myDrawingFolderPath + "/" + aPartName + " - " + aShapeName + " - drawing.dxf";
return aFileName;
}
SheetMetal_Unfolder myUnfolder;
Base_UTF16String myDrawingFolderPath;
size_t myPartIndex = 0;
};
int main (int argc, char* argv[])
{
auto aKey = LicenseKey::Value();
auto anMTKKey = MTKLicenseKey::Value();
// Activate the license (aKey must be defined in cadex_license.cxx
// and anMTKKey must be defined in mtk_license.cxx)
if (!CADExLicense_Activate (aKey)) {
cerr << "Failed to activate CAD Exchanger license." << endl;
return 1;
}
if (!CADExLicense_Activate (anMTKKey)) {
cerr << "Failed to activate Manufacturing Toolkit license." << endl;
return 1;
}
if (argc != 3) {
cerr << "Usage: " << argv[0] << " <input_file> <output_folder>, where:" << endl;
cerr << " <input_file> is a name of the file to be read" << endl;
cerr << " <output_folder> is a name of the folder where DXF files with drawing to be written" << endl;
return 1;
}
const char* aSource = argv[1];
const char* aDrawingPath = argv[2];
// Reading the file
if (!aReader.Read (aSource, aModel)) {
cerr << "Failed to read the file " << aSource << endl;
return 1;
}
cout << "Model: " << aModel.Name() << "\n" << endl;
//processing
PartProcessor aPartProcessor (aDrawingPath);
ModelData_SceneGraphElementUniqueVisitor aVisitor (aPartProcessor);
aModel.Accept (aVisitor);
return 0;
}
Defines a Unicode (UTF-16) string wrapping a standard string.
Definition: Base_UTF16String.hxx:34
bool IsEmpty() const
Returns true if the string is empty.
Definition: Base_UTF16String.cxx:233
const ModelData_BodyList & Get() const
Returns an associated topological object.
Definition: ModelData_BRepRepresentation.cxx:626
Base_UTF16String Name() const
Definition: ModelData_BaseObject.cxx:218
Represents a single 2D drawing of a model.
Definition: ModelData_Drawing.hxx:37
Element visitor with empty implementation.
Definition: ModelData_Model.hxx:113
Provides CAD Exchanger data model.
Definition: ModelData_Model.hxx:43
void SetDrawing(const ModelData_Drawing &theDrawing)
Sets the drawing of the model.
Definition: ModelData_Model.cxx:864
const Base_UTF16String & Name() const
Returns a model name.
Definition: ModelData_Model.cxx:358
void Accept(ElementVisitor &theVisitor) const
Accepts a visitor.
Definition: ModelData_Model.cxx:882
Reads any format that CAD Exchanger can import.
Definition: ModelData_ModelReader.hxx:33
bool Read(const Base_UTF16String &theFilePath, ModelData_Model &theModel)
Reads the file at the specified path into the specified model.
Definition: ModelData_ModelReader.cxx:182
Writes any format that CAD Exchanger can export.
Definition: ModelData_ModelWriter.hxx:33
bool Write(const ModelData_Model &theModel, const Base_UTF16String &theFilePath)
Writes the specified model to the file at the specified path.
Definition: ModelData_ModelWriter.cxx:143
Defines a leaf node in the scene graph hiearchy.
Definition: ModelData_Part.hxx:35
ModelData_BRepRepresentation BRepRepresentation() const
Definition: ModelData_Part.cxx:360
Defines a visitor that visits each unique element only once.
Definition: ModelData_SceneGraphElementUniqueVisitor.hxx:33
Iterates over subshapes in a shape.
Definition: ModelData_Shape.hxx:41
Base class of topological shapes.
Definition: ModelData_Shape.hxx:37
Defines a connected set of faces.
Definition: ModelData_Shell.hxx:31
Defines a topological solid.
Definition: ModelData_Solid.hxx:31
Describes a flat pattern for sheet metal models.
Definition: SheetMetal_FlatPattern.hxx:38
double Length() const
Returns length of the unfolded model in mm .
Definition: SheetMetal_FlatPattern.cxx:140
double Perimeter() const
Returns perimeter of the unfolded model in mm .
Definition: SheetMetal_FlatPattern.cxx:158
ModelData_Drawing ToDrawing() const
Converts the flat pattern to a drawing.
Definition: SheetMetal_FlatPattern.cxx:164
double Thickness() const
Returns thickness of the unfolded model in mm .
Definition: SheetMetal_FlatPattern.cxx:152
double Width() const
Returns width of the unfolded model in mm .
Definition: SheetMetal_FlatPattern.cxx:146
Is used to unfold sheet metal models.
Definition: SheetMetal_Unfolder.hxx:39
Defines classes, types, and global functions related to CAD Exchanger.
Definition: A3DSTestLib.hxx:22