// -----------------------------------------------------------------------------------------
//
// Copyright (c) 2021 CNC Software, LLC
//
//
// sdk@Mastercam.com
//
//
// Creates a panel and then rotates it 45 degrees
//
// -----------------------------------------------------------------------------------------
// Assemblies Import for External Editor
#region Assemblies Import
// NOTE: Update paths to reference your Mastercam 2023 path
// #r "C:\Program Files\Mastercam 2023\Mastercam\NETHook3_0.dll"
#endregion
// Using for External Editor
#region Namespace Import
/*
using System;
using System.Collections.Generic;
using System.Linq;
using Mastercam.App.Exceptions;
using Mastercam.Curves;
using Mastercam.Database.Types;
using Mastercam.GeometryUtility;
using Mastercam.IO;
using Mastercam.Math;
using Mastercam.Support;
*/
#endregion
#region Private Consts
/// The panel level.
private const int PanelLevel = 2;
/// The dado level.
private const int DadoLevel = 3;
/// The hole level.
private const int HoleLevel = 4;
#endregion
// instantiate
var app = new App();
// Execute the script
app.Run();
#region Classes
///
/// Defines our top level App class
///
public class App
{
///
/// Main entry point to script
///
public void Run()
{
// Create a panel
var success = Create();
if (success.IsFailure)
{
DialogManager.Error(success.Error, "Rotate Geometry");
return;
}
// rotate all geometries
var geometries = SearchManager.GetGeometry();
if (geometries != null && geometries.Any())
{
foreach (var geometry in geometries)
{
geometry.Rotate(new Point3D(0, 0, 0), 45, ViewManager.CPlane);
}
GraphicsManager.FitScreen();
GraphicsManager.ClearColors(new GroupSelectionMask(true));
}
}
#region Private Methods
/// Creates a new Cabinet part.
///
/// A Result error if fail, success otherwise.
private Result Create()
{
// Start a new session
var success = FileManager.New(true);
if (!success)
{
return Result.Fail("Failed to open new session");
}
// Create Holes
var result = CreateHoles();
if (result.IsFailure)
{
return Result.Fail(result.Error);
}
// Create Dados
result = CreateDados();
if (result.IsFailure)
{
return Result.Fail(result.Error);
}
result = CreateOutline();
return result.IsFailure ? Result.Fail(result.Error) : Result.Ok();
}
/// Creates the outline.
///
/// A Result error if fail, success otherwise.
private Result CreateOutline()
{
try
{
// define the profile
var profile = new List
{
new Line3D(new Point3D(0, 0, 0), new Point3D(0, 10, 0)),
new Line3D(new Point3D(0, 10, 0), new Point3D(10, 10, 0)),
new Line3D(new Point3D(10, 10, 0), new Point3D(10, 2, 0)),
new Line3D(new Point3D(10, 2, 0), new Point3D(8, 2, 0)),
new Line3D(new Point3D(8, 2, 0), new Point3D(8, 0, 0)),
new Line3D(new Point3D(8, 0, 0), new Point3D(0, 0, 0))
};
// create profile and commit to database
profile.ForEach(l =>
{
var line = new LineGeometry(l)
{
Level = PanelLevel
};
if (!line.Commit())
{
throw new MastercamException("Failed to create panel line");
}
});
}
catch (MastercamException e)
{
return Result.Fail(e.Message);
}
catch (Exception e)
{
return Result.Fail(e.Message);
}
return Result.Ok();
}
/// Creates the dados.
///
/// A Result error if fail, success otherwise.
private Result CreateDados()
{
try
{
// bottom dado
var point1 = new Point3D(.250, .500, 0);
var point2 = new Point3D(7.500, .250, 0);
var bottomDado = GeometryCreationManager.CreateRectangle(point1, point2);
if (!bottomDado.Any())
{
return Result.Fail("Create bottom dado failed.");
}
// side dado
point1 = new Point3D(9.500, 9.750, 0);
point2 = new Point3D(9.750, 2.225, 0);
var sideDado = GeometryCreationManager.CreateRectangle(point1, point2);
if (!sideDado.Any())
{
return Result.Fail("Create side dado failed.");
}
// combine
var all = new List();
all.AddRange(bottomDado);
all.AddRange(sideDado);
// iterate over the list and set the level
all.ForEach(l =>
{
l.Level = DadoLevel;
if (!l.Commit())
{
throw new MastercamException("Failed to create dados");
}
});
}
catch (MastercamException e)
{
return Result.Fail(e.Message);
}
catch (Exception e)
{
return Result.Fail(e.Message);
}
return Result.Ok();
}
/// Creates the dowel holes.
///
/// The Result of the method
private Result CreateHoles()
{
try
{
// 2 columns 6" apart
for (var i = 1; i < 12; i += 6)
{
for (var j = 2; j <= 7; j++)
{
// 5mm holes
var hole = new ArcGeometry
{
Data =
{
Radius = 0.0635,
CenterPoint = new Point3D(i, 1.2598 * j, 0),
StartAngleDegrees = 0,
EndAngleDegrees = 360
},
Level = HoleLevel
};
if (!hole.Commit())
{
return Result.Fail("Failed to create arc");
}
}
}
}
catch (Exception e)
{
return Result.Fail(e.Message);
}
return Result.Ok();
}
#endregion
}
/// A result.
public class Result
{
///
/// Initializes a new instance of the class. Specialised constructor for use only by derived class.
///
///
/// Thrown when the requested operation is invalid.
///
///
/// True if this object is success, false if not.
///
///
/// The error.
///
protected Result(bool isSuccess, string error)
{
if (isSuccess && error != string.Empty)
{
throw new InvalidOperationException();
}
if (!isSuccess && error == string.Empty)
{
throw new InvalidOperationException();
}
IsSuccess = isSuccess;
Error = error;
}
/// Gets a value indicating whether this object is success.
/// True if this object is success, false if not.
public bool IsSuccess
{
get;
}
/// Gets the error.
/// The error.
public string Error
{
get;
}
/// Gets a value indicating whether this object is failure.
/// True if this object is failure, false if not.
public bool IsFailure => !IsSuccess;
/// Handles the Fail response.
/// The message.
/// A Result.
public static Result Fail(string message) => new Result(false, message);
/// Handles the Fail response.
/// Generic type parameter.
/// The message.
/// A Result of type T.
public static Result Fail(string message) => new Result(default(T), false, message);
/// Gets the ok.
/// A Result.
public static Result Ok() => new Result(true, string.Empty);
/// Gets the ok.
/// Generic type parameter.
/// The value.
/// A Result.
public static Result Ok(T value) => new Result(value, true, string.Empty);
}
/// A result.
/// Generic type parameter.
public class Result : Result
{
/// The value.
private readonly T value;
/// Gets the value.
/// Thrown when the requested operation is invalid.
/// The value.
public T Value
{
get
{
if (!IsSuccess)
{
throw new InvalidOperationException();
}
return value;
}
}
///
/// Describes the class. Constructor.
///
///
/// The value.
///
///
/// True if this object is success.
///
///
/// The error.
///
// ReSharper disable once StyleCop.SA1201
// ReSharper disable once StyleCop.SA1642
protected internal Result(T v, bool isSuccess, string error)
: base (isSuccess, error) => value = v;
}
#endregion