// -----------------------------------------------------------------------------------------
//
// Copyright (c) 2025 CNC Software, LLC
//
//
// https://my.mastercam.com/submit-case/
//
//
// Shows examples of creating .NET point, line, arc, rectangle
// and setting some misc properties.
//
// -----------------------------------------------------------------------------------------
// Assemblies Import for External Editor
#region Assemblies Import
// NOTE: Update paths to reference your Mastercam 202x path
// #r "C:\Program Files\Mastercam 202x\NETHook3_0.dll"
#endregion
// Using for External Editor
#region Namespace Import
/*
using Mastercam.IO;
using Mastercam.IO.Types;
using Mastercam.BasicGeometry;
using Mastercam.Math;
using Mastercam.Curves;
*/
#endregion
// instantiate
var app = new App();
// Execute the script
app.Run();
#region Classes
///
/// Defines our top level App class
///
public class App
{
#region Public Methods
///
/// Main entry point to script
///
public void Run()
{
RunScript();
}
#endregion
#region Private Methods
/// run the examples
private void RunScript()
{
// clear the screen for a new session
FileManager.New();
var toGroup = new List();
// create a point in the center
var level = CreatePoint();
if(level != -1){
toGroup.Add(level);
}
// create a line
level = CreateLine();
if(level != -1){
toGroup.Add(level);
}
// create an arc
level = CreateArc();
if(level != -1){
toGroup.Add(level);
}
if (!toGroup.Any()){
DialogManager.Error(AppStrings.Messages.NoEntitiesCreated, AppStrings.Messages.WindowName);
return;
}
if (LevelsManager.AddLevelsToNamedGroup(new List(), toGroup, AppStrings.Messages.GroupName) == 0)
{
DialogManager.Error(AppStrings.Messages.AddLevelFailure, AppStrings.Messages.WindowName);
}
// zoom all
GraphicsManager.FitScreen();
}
///
/// Create a point
///
private int CreatePoint()
{
/*
Dim point
point = New McPt()
With point
.X = 0
.Y = 0
.Z = 0
.PType = 0
End With
*/
// New way to define a point
var point = new PointGeometry();
point.Data.x = 0;
point.Data.y = 0;
point.Data.z = 0;
point.PointStyle = PointStyleType.Dot;
// set some properties
point.Level = 9;
point.Color = 15;
// create by commiting to the database
var success = point.Commit();
if (!success)
{
DialogManager.Error(AppStrings.Messages.CreatePointFail, AppStrings.Messages.WindowName);
return -1;
}
// update the level for this geometry
return SetLevel(point.Level, "Point Levels");
}
///
/// Create a line
///
private int CreateLine()
{
/*
Dim line
line = New McLn()
With line
.X1 = 0
.Y1 = 0
.Z1 = 0
.X2 = 0
.Y2 = 0
.Z2 = 0
End With
*/
// define line end point co-ordinates
var point1 = new Point3D(0, 0, 0);
var point2 = new Point3D(10, 0, 0);
// define a new line
var line = new LineGeometry(point1, point2);
// set some properties
line.Level = 10;
line.Color = 5;
// create by commiting to the database
var success = line.Commit();
if (!success)
{
DialogManager.Error(AppStrings.Messages.CreateLineFail, AppStrings.Messages.WindowName);
return -1;
}
// update the level for this geometry
return SetLevel(line.Level, "Line Levels");
}
///
/// Creates an Arc
///
private int CreateArc()
{
/*
Dim arc
arc = New McAr()
With arc
.X = 0
.Y = 0
.Z = 0
.R = 0
.SA = 0
.SW = 0
.View = 0
End With
*/
// define a starting circle
var circle = new ArcGeometry();
circle.Data.Radius = .25;
circle.Data.StartAngleDegrees = 0.0;
circle.Data.EndAngleDegrees = 360.00;
circle.Data.CenterPoint.x = 0;
circle.Data.CenterPoint.y = 0;
circle.Data.CenterPoint.z = 0;
// set some properties
circle.Level = 11;
circle.Color = 10;
// commit to the database
var success = circle.Commit();
if (!success)
{
DialogManager.Error(AppStrings.Messages.CreateArcFail, AppStrings.Messages.WindowName);
return -1;
}
// update the level for this geometry
return SetLevel(circle.Level, "Arc Levels");
}
///
/// Sets the level name for the level number
///
/// The level number
/// The level name
/// Level on success, -1 otherwise
private int SetLevel(int number, string name)
{
// set the level name
var success = LevelsManager.SetLevelName(number, name);
if (!success)
{
DialogManager.Error(AppStrings.Messages.SetDataFailure, AppStrings.Messages.WindowName);
return -1;
}
// if we get here we are good to go
return number;
}
#endregion
}
///
/// Abstract class that contains all of the scripts strings.
///
public abstract class AppStrings
{
public static string Title = "Points Lines Arcs Debug Messages";
public static class Messages
{
public static string NoEntitiesCreated = "Failed to create any entities";
public static string GroupName = "Geometry";
public static string WindowName = "Mastercam";
public static string AddLevelFailure = "Failed to add level to group";
public static string SetDataFailure = "Failed to set level data for entity.";
public static string CreatePointFail = "CreatePoint failed.";
public static string CreateLineFail = "CreateLine failed.";
public static string CreateArcFail = "CreateArc failed.";
}
}
#endregion