//-----------------------------------------------------------------------------------------
//
// Copyright (c) 2024 CNC Software, LLC
//
//
// sdk@mastercam.com
//
//
// Demonstrates the following:
// 1. Opens a Mastercam part file
// 2. Searches all operation comments containing note=MX=60
// 3. Gets the chain from the operation containing note=MX=60
// 4. Reverse's the chains direct and flips and tool side direction
// 5. Updates the operations StockToLeaveOnFloors property
// 6. Updates the operations tool name
//
// -----------------------------------------------------------------------------------------
// instantiate
var app = new App();
// Execute the script
app.Run();
#region Classes
///
/// Defines our top level App class
///
public class App
{
private const string messageTitle = "Reverse Chain";
// sample part file name located with this script
private const string part = "NET-Scripts\\ReverseChainAndToolSide\\reverse_chain.mcam";
///
/// Main entry point to script
///
public void Run()
{
// Build path to part file
var filePath = Path.Combine(SettingsManager.SharedDirectory, part);
if (!File.Exists(filePath))
{
var message = "Part file is missing: {0}";
DialogManager.Error(string.Format(message, filePath), messageTitle);
return;
}
// Open the part file
if (!FileManager.Open(true, filePath))
{
var message = "Failed to open file: {0}";
DialogManager.Error(string.Format(message, filePath), messageTitle);
return;
}
// Get all toolpaths, filtering on contours and return a list
var contours = SearchManager.GetOperations(OperationType.Contour).ToList();
// Find the first contour with the note MX=60
var mx = (ContourOperation) contours.FirstOrDefault(o => o.Name.Contains("MX=60"));
if (mx != null)
{
// Update stock to leave on floors value
mx.CutParams.LeaveStock = new LeaveStockParams()
{
OnFloors = 0.025
} ;
// Reverse chains
var chains = mx.GetChainArray().ToList();
foreach (var chain in chains)
{
// Update direction
chain.Direction = chain.Direction == ChainDirectionType.Clockwise
? ChainDirectionType.CounterClockwise
: ChainDirectionType.Clockwise;
}
// Cache current comp direction
var direction = mx.CutterComp.Direction;
// Flip
mx.CutterComp.Direction = direction == CutterCompDir.CutterCompLeft
? CutterCompDir.CutterCompRight
: CutterCompDir.CutterCompLeft;
// Update the operation tool name
mx.OperationTool.Name = "Updated via API";
if (!mx.OperationTool.Commit())
{
var message = "Failed to rename tool: {0}";
DialogManager.Error(string.Format(message, mx.OperationTool.Name), messageTitle);
}
// Save all changes
var success = mx.Commit();
if (success)
{
// Force a regen
mx.Regenerate();
// Save the file
if (!FileManager.Save())
{
var message = "Failed to save part file: {0}";
DialogManager.Error(string.Format(message, filePath), messageTitle);
return;
}
}
}
else
{
DialogManager.Error("Toolpath with note MX=60 not found.", messageTitle);
}
}
}
#endregion