Hide menu
Loading...
Searching...
No Matches
projector/poly_projector/main.cxx
Refer to the Projector Example
// ****************************************************************************
//
// Copyright (C) 2008-2014, Roman Lygin. All rights reserved.
// Copyright (C) 2014-2026, 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/Geom/Direction.hxx>
#include <cadex/LicenseManager_Activate.h>
#include <cadex/LicenseManager_LicenseError.hxx>
#include <cadex/ModelData/IndexedTriangleSet.hxx>
#include <cadex/ModelData/MeshBody.hxx>
#include <cadex/ModelData/Model.hxx>
#include <cadex/ModelData/ModelReader.hxx>
#include <cadex/ModelData/ModelWriter.hxx>
#include <cadex/ModelData/Part.hxx>
#include <cadex/Projector_PolyProjector.hxx>
#include <cadex/Projector_Projection.hxx>
#include <iostream>
#include <vector>
#include "../../mtk_license.cxx"
using namespace cadex;
using namespace std;
static void PrintProjectionInfo (
const ModelData::Part& thePart,
const UTF16String& theDirectionName,
const Projector_Projection& theProjection)
{
std::cout << "Part [" << thePart.Name() << "], projection " << theDirectionName << ":" << std::endl;
std::cout << " area = " << theProjection.Area() << " mm" << std::endl;
std::cout << " outer perimeter = " << theProjection.OuterPerimeter() << " mm" << std::endl;
std::cout << std::endl;
}
class ProjectionComputer : public ModelData::ModelElementVoidVisitor
{
public:
ProjectionComputer (const Geom::Direction& theDirection, const UTF16String& theDirectionName) :
myDirection (theDirection),
myDirectionName (theDirectionName)
{}
bool SaveProjection (const UTF16String& theFileName) const
{
ModelData::Part aPart (UTF16String ("Projections"));
for (const auto& aProjection : myPartProjections) {
ModelData::MeshBody aMeshBody (aProjection.Mesh());
aPart.AddBody (aMeshBody);
}
ModelData::Model anOutModel (UTF16String ("Projector"));
anOutModel.AddRoot (aPart);
return aWriter.Write (anOutModel, theFileName);
}
protected:
void operator() (const ModelData::Part& thePart) override
{
Projector_Projection aProjection = myProjector.Perform (thePart, myDirection);
PrintProjectionInfo (thePart, myDirectionName, aProjection);
myPartProjections.push_back (aProjection);
}
private:
Geom::Direction myDirection;
const UTF16String& myDirectionName;
std::vector<Projector_Projection> myPartProjections;
};
static bool ComputeProjection (
const ModelData::Model& theModel,
const Geom::Direction& theDirection,
const UTF16String& theDirectionName,
const UTF16String& theOutFileName)
{
ProjectionComputer aComputer (theDirection, theDirectionName);
theModel.Accept (aComputer);
return aComputer.SaveProjection (theOutFileName);
}
int main (int argc, char* argv[])
{
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;
}
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 folder to save projections to (must end with '/' or '\\\\')" << endl;
return 1;
}
const UTF16String aSource (argv[1]);
const UTF16String anOutFolder (argv[2]);
// Reading the file
if (!aReader.Read (aSource, aModel)) {
cerr << "Failed to read the file " << argv[1] << endl;
return 1;
}
cout << "Model: " << aModel.Name() << "\n" << endl;
std::vector<std::pair<UTF16String, Geom::Direction>> aProjectionDataCollection = {
{ "X", Geom::Direction::XDir() },
{ "Y", Geom::Direction::YDir() },
{ "Z", Geom::Direction::ZDir() }
};
for (const auto& aProjectionData : aProjectionDataCollection) {
const auto& aProjectionName = aProjectionData.first;
const auto& aProjectionDirection = aProjectionData.second;
UTF16String anOutFilePath = anOutFolder + "projection_" + aProjectionName + ".stl";
if (ComputeProjection (aModel, aProjectionDirection, aProjectionName, anOutFilePath)) {
cout << "The output projection is written: " << anOutFilePath << endl << endl;
} else {
cerr << "Failed to write the output file: " << anOutFilePath << endl;
}
}
return 0;
}
Defines a 3D Direction.
Definition Direction.cs:17
Defines a body that represents a polygonal mesh (faceted or tessellated).
Definition MeshBody.cs:19
cadex.UTF16String Name()
Returns empty string if the model element has no name (by default).
Definition ModelElement.cs:67
Element visitor with empty implementation.
Definition ModelElementVoidVisitor.cs:20
Provides MTK data model.
Definition Model.cs:31
void Accept(cadex.ModelData.ModelElementVisitor theVisitor)
Accepts a visitor.
Definition Model.cs:176
cadex.UTF16String Name()
Returns empty string if the model has no name (by default).
Definition Model.cs:90
Reads supported formats, see Import section.
Definition ModelReader.cs:17
bool Read(cadex.UTF16String theFilePath, cadex.ModelData.Model theModel)
Reads the file at the specified path into the specified model.
Definition ModelReader.cs:82
Writes supported formats, see Import and Export section.
Definition ModelWriter.cs:17
bool Write(cadex.ModelData.Model theModel, cadex.UTF16String theFilePath)
Writes the specified model to the file at the specified path.
Definition ModelWriter.cs:73
Defines a leaf node in the scene graph hierarchy.
Definition Part.cs:23
The poly projection tool.
Definition Projector_PolyProjector.cs:22
Provides a computed projection results.
Definition Projector_Projection.cs:21
Defines a Unicode (UTF-16) string wrapping a standard string.
Definition UTF16String.cs:17
Contains classes, namespaces, enums, types, and global functions related to Manufacturing Toolkit.
Definition BaseObject.cs:12