Table of Contents

Class DemoSessionMessage

Namespace
Sample.Common
Assembly
Hi.Sample.dll
public static class DemoSessionMessage
Inheritance
DemoSessionMessage
Inherited Members

Remarks

Source Code

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Hi.Common.FileLines;
using Hi.Common.Messages;
using Hi.HiNcKits;
using Hi.MachiningProcs;
using Hi.MachiningSteps;
using Hi.NcParsers;

namespace Sample.Common;

/// <remarks>
/// ### Source Code
/// [!code-csharp[SampleCode](~/../Hi.Sample/Common/DemoSessionMessage.cs)]
/// </remarks>
public static class DemoSessionMessage
{
    #region Demo_UseSessionMessageHost
    internal static void DemoUseSessionMessageHost(LocalProjectService localProjectService)
    {
        // Session messages are partitioned by kind into three sinks on LocalProjectService:
        // - ShellProgress: session-level routine / lifecycle messages
        //   (session-scoped: null outside BeginSession/EndSession).
        // - NcDiagnosticProgress: NC-pipeline diagnostics, anchored to the NC source sentence.
        // - StepDiagnosticProgress: diagnostics anchored to a motion step.

        ShellProgress shellProgress = localProjectService.ShellProgress;
        List<IMessage> shellMessages = shellProgress == null
            ? new List<IMessage>() : shellProgress.Messages.ToList();
        foreach (IMessage message in shellMessages)
            Console.WriteLine(
                $"Shell [{message.GetSeverity()}] {message.GetId()}: {message.GetNotification()}");

        foreach (NcDiagnostic diagnostic in
            localProjectService.NcDiagnosticProgress.Diagnostics.ToList())
        {
            var ncLine = diagnostic.SentenceCarrier?.GetSentence()?.FirstIndexedFileLine;
            Console.WriteLine(
                $"NC [{diagnostic.GetSeverity()}] {diagnostic.GetId()}: {diagnostic.GetNotification()}; " +
                $"File: {ncLine?.FilePath}; LineNo: {ncLine?.GetLineNo()}; NC: {ncLine?.Line}");
        }

        foreach (StepDiagnostic diagnostic in
            localProjectService.StepDiagnosticProgress.Messages.ToList())
            Console.WriteLine(
                $"Step {diagnostic.StepIndex} [{diagnostic.GetSeverity()}] " +
                $"{diagnostic.GetId()}: {diagnostic.GetNotification()}");

        File.WriteAllLines("output-session-messages.txt",
            shellMessages.Select(m =>
            $"[{m.GetSeverity()}] {m.GetId()}: {m.GetNotification()}"));
    }
    #endregion

    #region ShowStepPresent
    internal static void ShowStepPresent(
        UserService userEnv, MachiningStep machiningStep)
    {
        foreach (var entry in userEnv.DisplayedStepPresentAccessList)
        {
            var present = entry.Value.Present;
            var valueText = string.Format("{0:" + present.DataFormatString + "}", entry.Value.GetValueFunc.Invoke(machiningStep));
            Console.WriteLine($"{present.ShortName}: {valueText} {present.TailUnitString} ({present.Name} [{entry.Key}])");
        }
    }
    #endregion
}