Hide menu
Loading...
Searching...
No Matches
exploring/mesh/Program.cs

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.
//
// ****************************************************************************
using cadex;
using cadex.Geom;
using System;
namespace mesh
{
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 != 1)
{
Console.WriteLine("Usage: " +
$"{System.Reflection.Assembly.GetExecutingAssembly().Location} <input_file>, where:");
Console.WriteLine($" <input_file> is a name of the file to be read");
Console.WriteLine($"");
return 1;
}
string aSource = args[0];
var aModel = new Model();
var aReader = new ModelReader();
if (!aReader.Read(new UTF16String(aSource), aModel))
{
Console.WriteLine("Failed to read the file " + aSource);
return 1;
}
PartMeshVisitor aVisitor = new PartMeshVisitor();
aModel.Accept(aVisitor);
return 0;
}
class PartMeshVisitor : ModelElementVoidVisitor
{
public override void Apply(Part thePart)
{
Console.WriteLine($"Part = \"{thePart.Name()}\"");
var aBodies = thePart.Bodies();
if (!aBodies.IsEmpty)
{
ExploreMeshBodies(aBodies);
}
}
private void ExploreMeshBodies(BodyList theBodies)
{
for (int i = 0; i < theBodies.Count; ++i)
{
var aBody = theBodies[i];
if (MeshBody.CompareType(aBody))
{
var aMeshBody = MeshBody.Cast(aBody);
Console.WriteLine($"MeshBody {i}");
var aMeshShapes = aMeshBody.Shapes();
for (int j = 0; j < aMeshShapes.Count; ++j)
{
var aMeshShape = aMeshShapes[j];
Console.Write($"MeshShape {j}");
PrintMeshShapeInfo(aMeshShape);
}
}
}
}
private void PrintMeshShapeInfo(MeshShape theMeshShape)
{
if (IndexedTriangleSet.CompareType(theMeshShape))
{
var aITS = IndexedTriangleSet.Cast(theMeshShape);
Console.WriteLine(" IndexedTriangleSet type.");
DumpTriangleSet(aITS);
}
else if (PolylineSet.CompareType(theMeshShape))
{
var aPLS = PolylineSet.Cast(theMeshShape);
Console.WriteLine(" PolyLineSet type");
DumpPolylineSet(aPLS);
}
else if (Polyline2dSet.CompareType(theMeshShape))
{
var aPL2dS = Polyline2dSet.Cast(theMeshShape);
Console.WriteLine(" PolyLine2dSet type");
DumpPolyline2dSet(aPL2dS);
}
else if (PointSet.CompareType(theMeshShape))
{
var aPPS = PointSet.Cast(theMeshShape);
Console.WriteLine(" PolyPointSet type");
DumpPointSet(aPPS);
}
else
{
Console.WriteLine(" Undefined type");
}
}
private void DumpPointSet(PointSet thePS)
{
uint aNumberOfPoints = thePS.NumberOfPoints();
Console.WriteLine($"PointSet: {aNumberOfPoints} points");
for (uint i = 0; i < aNumberOfPoints; ++i)
{
var aP = thePS.Point(i);
Console.WriteLine($"Point {i}: ({aP.X()}, {aP.Y()}, {aP.Z()})");
}
}
private void DumpPolylineSet(PolylineSet thePLS)
{
uint aNumberOfPolylines = thePLS.NumberOfPolylines();
Console.WriteLine($"PolylineSet: {aNumberOfPolylines} polylines");
for (uint i = 0; i < aNumberOfPolylines; ++i)
{
Console.WriteLine($"Polyline {i}:");
Console.WriteLine(" Node coordinates:");
var aPoly = thePLS.Polyline(i);
for (uint j = 0; j < aPoly.NumberOfPoints(); ++j)
{
var aP = aPoly.Point(j);
Console.WriteLine($"({aP.X()}, {aP.Y()}, {aP.Z()})");
}
}
}
private void DumpPolyline2dSet(Polyline2dSet thePLS)
{
uint aNumberOfPolylines2d = thePLS.NumberOfPolylines();
Console.WriteLine($"Polyline2dSet: {aNumberOfPolylines2d} polylines");
for (uint i = 0; i < aNumberOfPolylines2d; ++i)
{
Console.WriteLine($"Polyline2d {i}:");
Console.WriteLine(" Node coordinates:");
var aPoly = thePLS.Polyline(i);
for (uint j = 0; j < aPoly.NumberOfPoints(); ++j)
{
var aP = aPoly.Point(j);
Console.WriteLine($"({aP.X()}, {aP.Y()})");
}
}
}
private void DumpTriangleSet(IndexedTriangleSet theITS)
{
int aNumberOfTriangles = (int)theITS.NumberOfTriangles();
Console.WriteLine($"IndexedTriangleSet: {aNumberOfTriangles} triangles:");
for (uint i = 0; i < aNumberOfTriangles; ++i)
{
Console.WriteLine($"Triangle {i}:");
for (uint j = 0; j < 3; ++j)
{
int aVI = theITS.TriangleVertexIndex(i, j);
var aV = theITS.TriangleVertex(i, j);
Console.WriteLine($" Node {j}: Vertex {aVI} ({aV.X()}, {aV.Y()}, {aV.Z()})");
if (theITS.HasNormals())
{
var aN = theITS.TriangleVertexNormal(i, j);
Console.WriteLine($" Normal: ({aN.X()}, {aN.Y()}, {aN.Z()})");
}
}
}
}
}
}
}
Activates the license key.
Definition LicenseManager.cs:48
Defines a polygonal shape consisting of triangles.
Definition IndexedTriangleSet.cs:76
cadex.Geom.Point TriangleVertex(uint theTriangleIndex, uint theVertexSlot)
Returns a vertex of a triangle.
Definition IndexedTriangleSet.cs:188
cadex.Geom.Vector TriangleVertexNormal(uint theTriangleIndex, uint theVertexSlot)
Returns a normal at vertex in a triangle.
Definition IndexedTriangleSet.cs:213
int TriangleVertexIndex(uint theTriangleIndex, uint theVertexSlot)
Returns a vertex index in a triangle.
Definition IndexedTriangleSet.cs:202
uint NumberOfTriangles()
Returns a number of triangles.
Definition IndexedTriangleSet.cs:245
bool HasNormals()
Returns true if the triangle set has explicitly defined normals.
Definition IndexedTriangleSet.cs:163
Defines a body that represents a polygonal mesh (faceted or tessellated).
Definition MeshBody.cs:19
Base class for all polygonal geometry containers.
Definition MeshShape.cs:19
Element visitor with empty implementation.
Definition ModelElementVoidVisitor.cs:20
Provides MTK data model.
Definition Model.cs:30
Reads supported formats, see Import section.
Definition ModelReader.cs:17
Defines a leaf node in the scene graph hierarchy.
Definition Part.cs:23
Describes drawing elements composed of 2D points.
Definition ModelData/PointSet.cs:22
uint NumberOfPoints()
Definition ModelData/PointSet.cs:93
cadex.Geom.Point Point(uint theIndex)
Definition ModelData/PointSet.cs:85
Defines a polygonal shape consisting of polylines.
Definition Polyline2dSet.cs:17
cadex.Geom.Polyline2d Polyline(uint theIndex)
Returns the polyline at position theIndex in polyline set.
Definition Polyline2dSet.cs:76
uint NumberOfPolylines()
Returns a number of polylines in polyline set.
Definition Polyline2dSet.cs:86
Defines a polygonal shape consisting of polylines.
Definition PolylineSet.cs:17
uint NumberOfPolylines()
Returns a number of polylines in polyline set.
Definition PolylineSet.cs:86
cadex.Geom.Polyline Polyline(uint theIndex)
Returns the polyline at position theIndex in polyline set.
Definition PolylineSet.cs:76
Defines a Unicode (UTF-16) string wrapping a standard string.
Definition UTF16String.cs:17
Definition ArrayDouble2.cs:12
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