Class DemoUseMachiningProject
Demonstrates how to load and use an existing 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 Dispose().
public static class DemoUseMachiningProject
- Inheritance
-
DemoUseMachiningProject
- Inherited Members
Remarks
Source Code
using System;
using Hi.Common.Messages;
using Hi.MachiningProcs;
using Hi.Common.FileLines;
using Hi.HiNcKits;
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()
{
SingleUserApp.AppBegin();
#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.Exception.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();
SingleUserApp.AppEnd();
Console.WriteLine($"Program end.");
#endregion
}
}
}