DemoUseSessionMessageHost now reads ShellProgress / NcDiagnosticProgress / StepDiagnosticProgress (severity + id + notification, with the per-kind NC-line / step anchors). Drop DemoUseSessionMessageHost2 — its premise (reading MachiningStep objects out of the mixed message list) dissolves under the partition; step-data demos live in ShowStepPresent and SessionStepBuilt. DemoUseMachiningProject's abnormal-message log now subscribes the three sinks' MessageAdded events (shell via the OnShellMessageAdded app-lifetime bridge). Part of MixedProgress0 retirement (migration P6). Build x64 green. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
95 lines
3.7 KiB
C#
95 lines
3.7 KiB
C#
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;
|
|
|
|
/// <summary>
|
|
/// Demonstrates how to load and use an existing <see cref="MachiningProject"/> 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
|
|
/// <see cref="IDisposable.Dispose"/>.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// ### Source Code
|
|
/// [!code-csharp[SampleCode](~/../Hi.Sample/Machining/DemoUseMachiningProject.cs)]
|
|
/// </remarks>
|
|
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
|
|
}
|
|
}
|