using System;
using Hi.Licenses;
using Hi.Common.Messages;
using Hi.MachiningProcs;
using Hi.Common.FileLines;
using Hi.MongoUtils;
namespace Sample.Machining
{
///
/// This sample demonstrates how to work with an existing machining project file and execute NC programs. It covers the entire workflow from project loading to executing machining operations, including setting up event handlers for monitoring machining progress and error conditions. The example shows how to configure runtime parameters like collision detection and material removal rate (MRR) monitoring, execute NC files, and properly clean up resources after machining is complete. It's particularly useful for applications that need to automate the execution of machining operations and handle events during the simulation process.
///
///
/// ### Source Code
/// [!code-csharp[SampleCode](~/../Hi.Sample/Machining/DemoUseMachiningProject.cs)]
///
public static class DemoUseMachiningProject
{
static void Main()
{
#region LicenseAndDatabaseSetup
Console.WriteLine($"License Login.");
License.LogInAll();
Console.WriteLine($"Start Database.");
MongoServer.Default = MongoServer.Run(new MongoRunnerOptions()
{
MongoPort = 28100
});
#endregion
#region ProjectLoading
var projectPath = "C:/HiNC-Projects/DemoStandardPath/Main.hincproj";
Console.WriteLine($"Load Project: {projectPath}");
MachiningProject machiningProject = MachiningProject.LoadFile(projectPath);
#endregion
#region EventHandling
Console.WriteLine($"Set message event.");
//show message if something abnormal.
machiningProject.SessionMessageHost.CollectionItemAdded += pack =>
{
if (pack.Tags.Contains(MessageFlag.Warning.ToString()) ||
pack.Tags.Contains(MessageFlag.Error.ToString()) ||
pack.Tags.Contains(MessageFlag.SysErr.ToString()))
{
var sourceCommand = pack.SourceCommand;
Console.WriteLine($"{pack.Message} At \"{sourceCommand?.FilePath}\" (Line {sourceCommand?.GetLineNo()}) \"{sourceCommand?.Line}\"");
}
};
Console.WriteLine($"Set machining step event.");
//show MRR.
machiningProject.ShellApi.MachiningStepBuilt += (preStep, curStep) =>
{
var sourceCommand = curStep.SourceCommand;
if (curStep.Mrr_mm3ds > 500) //show only the step that contains large MRR.
Console.WriteLine($"MRR = {curStep.Mrr_mm3ds} At \"{sourceCommand?.FilePath}\" (Line {sourceCommand?.GetLineNo()}) \"{sourceCommand?.Line}\"");
};
#endregion
#region MachiningExecution
Console.WriteLine($"Reset runtime status.");
machiningProject.ResetRuntime();
Console.WriteLine($"Session begin.");
machiningProject.BeginSession();
machiningProject.ShellApi.MachiningResolution_mm = 1;
machiningProject.ShellApi.EnableCollisionDetection = true;
machiningProject.ShellApi.EnablePauseOnCollision = false;
machiningProject.ShellApi.EnableMillingForceEvaluation = false;
//the path from Shell-API is relative by project directory.
machiningProject.ShellApi.PlayNcFile("NC/side.ptp");
machiningProject.ShellApi.PlayNcFile("NC/circle.ptp");
machiningProject.EndSession();
Console.WriteLine($"Session end.");
#endregion
#region CleanupResources
Console.WriteLine($"Close Project: {projectPath}");
machiningProject.Dispose();
Console.WriteLine($"Close Database.");
MongoServer.Default.Dispose();
Console.WriteLine($"License Logout.");
License.LogOutAll();
Console.WriteLine($"Program end.");
#endregion
}
}
}