using Hi.Common.FileLines;
using Hi.Common.Messages;
using Hi.HiNcKits;
using Hi.MachiningProcs;
using Hi.NcParsers;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
namespace Sample.Machining;
///
/// Demonstrates how to load and use an existing instance.
/// This sample shows how to set up event handlers for messages and machining step objects,
/// execute NC files, and properly manage project resources using
/// .
///
///
/// ### Source Code
/// [!code-csharp[SampleCode](~/../Hi.Sample/Machining/DemoUseMachiningProject.cs)]
///
public static class DemoUseMachiningProject
{
static void Main()
{
using var loggerFactory = Microsoft.Extensions.Logging.LoggerFactory.Create(b => b.AddConsole());
LocalApp.AppBegin(loggerFactory.CreateLogger("Hi.Sample"));
LocalProjectService localProjectService = new LocalProjectService();
#region ProjectLoading
var projectPath = "C:/HiNC-Projects/DemoStandardPath/Main.hincproj";
Console.WriteLine($"Load Project: {projectPath}");
localProjectService.LoadProject(projectPath);
MachiningProject machiningProject = localProjectService.MachiningProject;
#endregion
#region EventHandling
Console.WriteLine($"Set message event.");
using StreamWriter writer = new StreamWriter("msg.txt");
//show message if something abnormal, from each partitioned message sink:
//shell (session lifecycle), NC diagnostics, and step-anchored diagnostics.
void LogIfAbnormal(IMessage message, ISentenceCarrier sentenceCarrier)
{
var severity = message.GetSeverity();
if (severity != Severity.Warning && severity != Severity.Error)
return;
var ncLine = sentenceCarrier?.GetSentence()?.FirstIndexedFileLine;
writer.WriteLine($"{message.GetNotification()} At \"{ncLine?.FilePath}\" (Line {ncLine?.GetLineNo()}) \"{ncLine?.Line}\"");
}
localProjectService.OnShellMessageAdded += (index, message)
=> LogIfAbnormal(message, null);
localProjectService.NcDiagnosticProgress.MessageAdded += (index, diagnostic)
=> LogIfAbnormal(diagnostic, diagnostic.SentenceCarrier);
localProjectService.StepDiagnosticProgress.MessageAdded += (index, diagnostic)
=> LogIfAbnormal(diagnostic, diagnostic.SentenceCarrier);
Console.WriteLine($"Set machining step event.");
//show MRR.
localProjectService.SessionShell.SessionStepBuilt += (preStep, curStep) =>
{
var sourceCommand = curStep.SourceCommand;
var indexedFileLine=sourceCommand?.GetSentence()?.FirstIndexedFileLine;
if (curStep.Mrr_mm3ds > 500) //show only the step that contains large MRR.
Console.WriteLine($"MRR = {curStep.Mrr_mm3ds} At \"{indexedFileLine?.FilePath}\" (Line {indexedFileLine?.GetLineNo()}) \"{indexedFileLine?.Line}\"");
};
#endregion
#region MachiningExecution
Console.WriteLine($"Reset runtime status.");
localProjectService.ResetRuntime();
Console.WriteLine($"Session begin.");
localProjectService.BeginSession();
localProjectService.SessionShell.MachiningResolution_mm = 1;
localProjectService.SessionShell.EnableCollisionDetection = true;
localProjectService.SessionShell.EnablePauseOnFailure = false;
localProjectService.SessionShell.EnablePhysics = false;
//the path from Shell-API is relative by project directory.
localProjectService.SessionShell.PlayNcFile("NC/side.ptp");
localProjectService.SessionShell.PlayNcFile("NC/circle.ptp");
localProjectService.EndSession();
Console.WriteLine($"Session end.");
#endregion
#region CleanupResources
Console.WriteLine($"Close Project: {projectPath}");
machiningProject.Dispose();
LocalApp.AppEnd();
Console.WriteLine($"Program end.");
#endregion
}
}