using System;
using Hi.Licenses;
using Hi.Common.Messages;
using Hi.MachiningProcs;
using Hi.Common.FileLines;
using Hi.MongoUtils;
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()
{
#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
}
}
}