Hide menu
Loading...
Searching...
No Matches
projector/poly_projector/Program.cs
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.
//
// ****************************************************************************
using cadex;
using cadex.Geom;
using System;
using System.Collections.Generic;
namespace poly_projector
{
class Program
{
static int Main(string[] args)
{
string aKey = MTKLicenseKey.Value();
// Activate the license (aKey must be defined in mtk_license.cs)
if (!LicenseManager.Activate(aKey))
{
Console.WriteLine("Failed to activate Manufacturing Toolkit license.");
return 1;
}
if (args.Length != 2)
{
Console.WriteLine("Usage: poly_projector <input_file> <output_folder>, where:");
Console.WriteLine(" <input_file> is a name of the file to be read");
Console.WriteLine(" <output_folder> is a folder to save projections to (must end with '/' or '\\\\')");
return 1;
}
UTF16String aSource = new UTF16String(args[0]);
string anOutFolder = args[1];
Model aModel = new Model();
ModelReader aReader = new ModelReader();
// Reading the file
if (!aReader.Read(aSource, aModel))
{
Console.WriteLine("Failed to read the file " + args[0]);
return 1;
}
Console.WriteLine("Model: " + aModel.Name());
Console.WriteLine("");
UTF16String anOutX = new UTF16String(anOutFolder + "projection_X.stl");
UTF16String anOutY = new UTF16String(anOutFolder + "projection_Y.stl");
UTF16String anOutZ = new UTF16String(anOutFolder + "projection_Z.stl");
if (!ComputeProjection(aModel, Direction.XDir(), new UTF16String("X"), anOutX))
{
Console.WriteLine("Failed to write X projection.");
return 1;
}
if (!ComputeProjection(aModel, Direction.YDir(), new UTF16String("Y"), anOutY))
{
Console.WriteLine("Failed to write Y projection.");
return 1;
}
if (!ComputeProjection(aModel, Direction.ZDir(), new UTF16String("Z"), anOutZ))
{
Console.WriteLine("Failed to write Z projection.");
return 1;
}
Console.WriteLine("Output written:");
Console.WriteLine(" " + anOutX);
Console.WriteLine(" " + anOutY);
Console.WriteLine(" " + anOutZ);
return 0;
}
static void PrintProjectionInfo(Part thePart,
UTF16String theDirectionName,
Projector_Projection theProjection)
{
Console.WriteLine("Part [" + thePart.Name() + "], projection " + theDirectionName + ":");
Console.WriteLine(" area = " + theProjection.Area() + " mm");
Console.WriteLine(" outer perimeter = " + theProjection.OuterPerimeter() + " mm");
Console.WriteLine("");
}
class ProjectionComputer : ModelElementVoidVisitor
{
public ProjectionComputer(Direction theDirection,
UTF16String theDirectionName)
{
myDirection = theDirection;
myDirectionName = theDirectionName;
}
public bool SaveProjection(UTF16String theFileName)
{
Part aPart = new Part(new UTF16String("Projection"));
foreach (Projector_Projection aProjection in myPartProjections)
{
MeshBody aMeshBody = new MeshBody(aProjection.Mesh());
aPart.AddBody(aMeshBody);
}
Model anOutModel = new Model(new UTF16String("Projector"));
anOutModel.AddRoot(aPart);
ModelWriter aWriter = new ModelWriter();
return aWriter.Write(anOutModel, theFileName);
}
public override void Apply(Part thePart)
{
Projector_Projection aProjection = myProjector.Perform(thePart, myDirection);
PrintProjectionInfo(thePart, myDirectionName, aProjection);
myPartProjections.Add(aProjection);
}
private Direction myDirection;
private UTF16String myDirectionName;
private Projector_PolyProjector myProjector = new Projector_PolyProjector();
private List<Projector_Projection> myPartProjections = new List<Projector_Projection>();
}
static bool ComputeProjection(Model theModel,
Direction theDirection,
UTF16String theDirectionName,
UTF16String theOutFileName)
{
ProjectionComputer aComputer = new ProjectionComputer(theDirection, theDirectionName);
theModel.Accept(aComputer);
return aComputer.SaveProjection(theOutFileName);
}
}
}
Defines a 3D Direction.
Definition Direction.hxx:32
Defines a body that represents a polygonal mesh (faceted or tessellated).
Definition MeshBody.hxx:30
UTF16String Name() const
Returns a name.
Definition ModelElement.cxx:54
Element visitor with empty implementation.
Definition ModelElementVisitor.hxx:63
Provides MTK data model.
Definition Model.hxx:39
void AddRoot(const ModelElement &theElement)
Adds a root element to the model.
Definition Model.cxx:219
UTF16String Name() const
Returns a model name.
Definition Model.cxx:256
void Accept(ModelElementVisitor &theVisitor) const
Accepts a visitor.
Definition Model.cxx:276
Reads supported formats, see Import section.
Definition ModelReader.hxx:32
bool Read(const UTF16String &theFilePath, ModelData::Model &theModel)
Reads the file at the specified path into the specified model.
Definition ModelReader.cxx:288
Writes supported formats, see Import and Export section.
Definition ModelWriter.hxx:32
bool Write(const ModelData::Model &theModel, const UTF16String &theFilePath)
Writes the specified model to the file at the specified path.
Definition ModelWriter.cxx:121
Defines a leaf node in the scene graph hierarchy.
Definition Part.hxx:32
The poly projection tool.
Definition Projector_PolyProjector.hxx:43
Provides a computed projection results.
Definition Projector_Projection.hxx:64
double Area() const
Returns the total area of the projection.
Definition Projector_Projection.cxx:127
const ModelData::IndexedTriangleSet & Mesh() const
Returns the complete mesh representation of the projection.
Definition Projector_Projection.cxx:164
double OuterPerimeter() const
Returns the sum of regions' outer perimeter.
Definition Projector_Projection.cxx:142
Defines a Unicode (UTF-16) string wrapping a standard string.
Definition UTF16String.hxx:29
Contains classes, types, enums, and functions related to geometric entities.
Defines classes, types, enums, and functions related to topological entities and scene graph elements...
Contains classes, namespaces, enums, types, and global functions related to Manufacturing Toolkit.
Definition LicenseManager_LicenseError.hxx:29