Hide menu
Loading...
Searching...
No Matches
exploring/mesh/mesh.java

Refer to the mtk_mesh_representation_example_page.

// ****************************************************************************
// $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.
//
// ****************************************************************************
import cadex.*;
import cadex.Collections.*;
import cadex.Geom.*;
import cadex.ModelData.*;
import java.util.*;
public class mesh {
static {
try {
System.loadLibrary("CadExMTK");
} 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 (the key 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 != 1) {
System.out.println("Usage: " + " <input_file>, where:");
System.out.println(" <input_file> is a name of the file to be read");
System.exit(1);
}
String aSource = args[0];
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);
}
PartMeshVisitor aVisitor = new PartMeshVisitor();
aModel.Accept(aVisitor);
System.exit(0);
}
static class PartMeshVisitor extends ModelElementVoidVisitor {
@Override
public void Apply(Part thePart) {
System.out.println("Part = \"" + thePart.Name() + "\"");
BodyList aBodies = thePart.Bodies();
if (!aBodies.isEmpty()) {
ExploreMeshBodies(aBodies);
}
}
private void ExploreMeshBodies(BodyList theBodies) {
for (int i = 0; i < theBodies.size(); ++i) {
Body aBody = theBodies.get(i);
if (MeshBody.CompareType(aBody)) {
MeshBody aMeshBody = MeshBody.Cast(aBody);
System.out.println("MeshBody " + i);
MeshShapeList aMeshShapes = aMeshBody.Shapes();
for (int j = 0; j < aMeshShapes.size(); ++j) {
MeshShape aMeshShape = aMeshShapes.get(j);
System.out.print("MeshShape " + j);
PrintMeshShapeInfo(aMeshShape);
}
}
}
}
private void PrintMeshShapeInfo(MeshShape theMeshShape) {
if (IndexedTriangleSet.CompareType(theMeshShape)) {
IndexedTriangleSet aITS = IndexedTriangleSet.Cast(theMeshShape);
System.out.println(" IndexedTriangleSet type.");
DumpTriangleSet(aITS);
} else if (PolylineSet.CompareType(theMeshShape)) {
PolylineSet aPLS = PolylineSet.Cast(theMeshShape);
System.out.println(" PolyLineSet type");
DumpPolylineSet(aPLS);
} else if (Polyline2dSet.CompareType(theMeshShape)) {
Polyline2dSet aPL2dS = Polyline2dSet.Cast(theMeshShape);
System.out.println(" PolyLine2dSet type");
DumpPolyline2dSet(aPL2dS);
} else if (PointSet.CompareType(theMeshShape)) {
PointSet aPPS = PointSet.Cast(theMeshShape);
System.out.println(" PolyPointSet type");
DumpPointSet(aPPS);
} else {
System.out.println(" Undefined type");
}
}
private void DumpPointSet(PointSet thePS) {
int aNumberOfPoints = (int) thePS.NumberOfPoints();
System.out.println("PointSet: " + aNumberOfPoints + " points");
for (int i = 0; i < aNumberOfPoints; ++i) {
Point aP = thePS.Point(i);
System.out.println("Point " + i + ": (" + aP.X() + ", " + aP.Y() + ", " + aP.Z() + ")");
}
}
private void DumpPolylineSet(PolylineSet thePLS) {
int aNumberOfPolylines = (int) thePLS.NumberOfPolylines();
System.out.println("PolylineSet: " + aNumberOfPolylines + " polylines");
for (int i = 0; i < aNumberOfPolylines; ++i) {
System.out.println("Polyline " + i + ":");
System.out.println(" Node coordinates:");
Polyline aPoly = thePLS.Polyline(i);
int n = (int) aPoly.NumberOfPoints();
for (int j = 0; j < n; ++j) {
Point aP = aPoly.Point(j);
System.out.println("(" + aP.X() + ", " + aP.Y() + ", " + aP.Z() + ")");
}
}
}
private void DumpPolyline2dSet(Polyline2dSet thePLS) {
int aNumberOfPolylines2d = (int) thePLS.NumberOfPolylines();
System.out.println("Polyline2dSet: " + aNumberOfPolylines2d + " polylines");
for (int i = 0; i < aNumberOfPolylines2d; ++i) {
System.out.println("Polyline2d " + i + ":");
System.out.println(" Node coordinates:");
Polyline2d aPoly = thePLS.Polyline(i);
int n = (int) aPoly.NumberOfPoints();
for (int j = 0; j < n; ++j) {
Point2d aP = aPoly.Point(j);
System.out.println("(" + aP.X() + ", " + aP.Y() + ")");
}
}
}
private void DumpTriangleSet(IndexedTriangleSet theITS) {
int aNumberOfTriangles = (int) theITS.NumberOfTriangles();
System.out.println("IndexedTriangleSet: " + aNumberOfTriangles + " triangles:");
for (int i = 0; i < aNumberOfTriangles; ++i) {
System.out.println("Triangle " + i + ":");
for (int j = 0; j < 3; ++j) {
int aVI = theITS.TriangleVertexIndex(i, j);
Point aV = theITS.TriangleVertex(i, j);
System.out.println(" Node " + j + ": Vertex " + aVI + " (" + aV.X() + ", " + aV.Y() + ", " + aV.Z() + ")");
if (theITS.HasNormals()) {
cadex.Geom.Vector aN = theITS.TriangleVertexNormal(i, j);
System.out.println(" Normal: (" + aN.X() + ", " + aN.Y() + ", " + aN.Z() + ")");
}
}
}
}
}
}
Defines a 3D vector.
Definition Vector.hxx:34
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:30