Hide menu
Loading...
Searching...
No Matches
projector/poly_projector/poly_projector.java
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.
//
// ****************************************************************************
import cadex.*;
import cadex.ModelData.*;
import java.util.ArrayList;
public class poly_projector {
static {
try {
System.loadLibrary("MTKCore");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
public static void main(String[] args) {
String aKey = MTKLicenseKey.Value();
// Activate the license (aKey must be defined in MTKLicenseKey.java)
if (!LicenseManager.Activate(aKey)) {
System.out.println("Failed to activate Manufacturing Toolkit license.");
System.exit(1);
}
if (args.length != 2) {
System.out.println("Usage: java poly_projector <input_file> <output_folder>, where:");
System.out.println(" <input_file> is a name of the file to be read");
System.out.println(" <output_folder> is a folder to save projections to (must end with '/' or '\\\\')");
System.out.println();
System.exit(1);
}
String aSource = args[0];
String anOutFolder = args[1];
Model aModel = new Model();
ModelReader aReader = new ModelReader();
// Reading the file
if (!aReader.Read(new UTF16String(aSource), aModel)) {
System.out.println("Failed to read the file " + aSource);
System.exit(1);
}
System.out.println("Model: " + aModel.Name());
System.out.println();
UTF16String anOutX = new UTF16String(anOutFolder + "projection_X.mtk");
UTF16String anOutY = new UTF16String(anOutFolder + "projection_Y.mtk");
UTF16String anOutZ = new UTF16String(anOutFolder + "projection_Z.mtk");
if (!ComputeProjection(aModel, Direction.XDir(), new UTF16String("X"), anOutX)) {
System.out.println("Failed to write X projection.");
System.exit(1);
}
if (!ComputeProjection(aModel, Direction.YDir(), new UTF16String("Y"), anOutY)) {
System.out.println("Failed to write Y projection.");
System.exit(1);
}
if (!ComputeProjection(aModel, Direction.ZDir(), new UTF16String("Z"), anOutZ)) {
System.out.println("Failed to write Z projection.");
System.exit(1);
}
System.out.println("Output written:");
System.out.println(" " + anOutX);
System.out.println(" " + anOutY);
System.out.println(" " + anOutZ);
}
static void PrintProjectionInfo(Part thePart,
UTF16String theDirectionName,
Projector_Projection theProjection) {
System.out.println("Part [" + thePart.Name() + "], projection " + theDirectionName + ":");
System.out.println(" area = " + theProjection.Area() + " mm");
System.out.println(" outer perimeter = " + theProjection.OuterPerimeter() + " mm");
System.out.println();
}
static class ProjectionComputer extends ModelElementVoidVisitor {
public ProjectionComputer(Direction theDirection,
UTF16String theDirectionName) {
myDirection = theDirection;
myDirectionName = theDirectionName;
myProjector = new Projector_PolyProjector();
myPartProjections = new ArrayList<Projector_Projection>();
}
public boolean SaveProjection(UTF16String theFileName) {
Part aPart = new Part(new UTF16String("Projection"));
for (Projector_Projection aProjection : 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);
}
@Override
public 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;
private ArrayList<Projector_Projection> myPartProjections;
}
static boolean 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.cs:17
Contains classes, types, enums, and functions related to geometric entities.
Definition Axis1d.cs:12
Defines classes, types, enums, and functions related to topological entities and scene graph elements...
Definition AngleUnit.cs:12
Contains classes, namespaces, enums, types, and global functions related to Manufacturing Toolkit.
Definition BaseObject.cs:12