From 953711bf01a061409a067445e09c0da4717fd09c Mon Sep 17 00:00:00 2001 From: iambossTC Date: Fri, 9 May 2025 09:25:14 +0800 Subject: [PATCH] add geom GUI to HiNC-2025-win-desktop. --- Common/DemoMessageAndExceptionHandling.cs | 58 +++++++ Common/DemoSessionMessage.cs | 148 ++++++++++++++++++ Geom/DemoBuildGeom.cs | 8 +- HelloHiAPI.cs | 12 +- MachineTool/DemoBuildMachineTool.cs | 2 - .../DemoBuildGeomOnlyMachiningProject.cs | 98 ++++++++++++ Machining/DemoBuildMachiningProject.cs | 70 ++++----- Machining/DemoUseMachiningProject.cs | 22 +-- 8 files changed, 352 insertions(+), 66 deletions(-) create mode 100644 Common/DemoMessageAndExceptionHandling.cs create mode 100644 Common/DemoSessionMessage.cs create mode 100644 Machining/DemoBuildGeomOnlyMachiningProject.cs diff --git a/Common/DemoMessageAndExceptionHandling.cs b/Common/DemoMessageAndExceptionHandling.cs new file mode 100644 index 0000000..612bcb4 --- /dev/null +++ b/Common/DemoMessageAndExceptionHandling.cs @@ -0,0 +1,58 @@ +using System; +using System.Threading.Tasks; +using Hi.Common; +using Hi.Common.Messages; + +namespace Sample.Common +{ + /// + /// Demonstrates common message and exception handling patterns in HiAPI applications + /// + /// + /// ### Source Code + /// [!code-csharp[SampleCode](~/../Hi.Sample/Common/DemoMessageAndExceptionHandling.cs)] + /// + public static class DemoMessageAndExceptionHandling + { + /// + /// Demonstrates normal message handling + /// + internal static void DemoNormalMessages() + { + #region Normal_Messages + MessageKit.AddMessage("Operation completed successfully."); + MessageKit.AddWarning("Please check your input."); + #endregion + } + /// + /// Demonstrates exception handling in synchronous code + /// + internal static void DemoSynchronousExceptionHandling() + { + #region Sync_Exception + try + { + // Your code here + throw new NotImplementedException("Demo exception"); + } + catch (Exception ex) + { + ExceptionUtil.ShowException(ex, null); + } + #endregion + } + /// + /// Demonstrates exception handling in asynchronous code + /// + internal static async Task DemoAsynchronousExceptionHandling() + { + #region Async_Exception + await Task.Run(() => + { + // Your async operation here + throw new NotImplementedException("Demo async exception"); + }).ShowIfCatched(null); + #endregion + } + } +} \ No newline at end of file diff --git a/Common/DemoSessionMessage.cs b/Common/DemoSessionMessage.cs new file mode 100644 index 0000000..9fbb5e6 --- /dev/null +++ b/Common/DemoSessionMessage.cs @@ -0,0 +1,148 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using Hi.Common; +using Hi.Common.FileLines; +using Hi.Geom; +using Hi.HiNcKits; +using Hi.MachiningProcs; +using Hi.MachiningSteps; +using Hi.Mech; +using Hi.Mech.Topo; +using Hi.Numerical; + +namespace Sample.Common +{ + /// + /// ### Source Code + /// [!code-csharp[SampleCode](~/../Hi.Sample/Common/DemoSessionMessage.cs)] + /// + public static class DemoSessionMessage + { + #region Demo_UseSessionMessageHost + internal static void DemoUseSessionMessageHost(MachiningProject project) + { + SessionMessageHost sessionMessageHost = project.SessionMessageHost; + + SessionMessageHost.FilterFlag filterFlags = + SessionMessageHost.FilterFlag.NC | + SessionMessageHost.FilterFlag.Progress | + SessionMessageHost.FilterFlag.Error; + string filterText = null; + var filteredSessionMessageList = sessionMessageHost + .GetFliteredList(filterFlags, filterText); + + foreach (var sessionMessage in filteredSessionMessageList) + { + //M.I.: Message Index. + Console.Write($"M.I.: {sessionMessage.Index}; Role: {sessionMessage.MessageRoleText}"); + + // For SessionMessageHost.FilterFlag.NC + var nc = sessionMessage.DirectInstantSourceCommand; + if (nc != null) + Console.Write($"Message/NC: {nc.Line}; File: {nc.FilePath}; LineNo: {nc.GetLineNo()}; "); + + // For SessionMessageHost.FilterFlag.Progress or Error. + var multiTagMessage = sessionMessage.MultiTagMessage; + if (multiTagMessage != null) + Console.WriteLine($"Message/NC: {multiTagMessage.Message}"); + var exception = sessionMessage.Exception; + if (exception != null) + Console.WriteLine($"Message/NC: {exception.Message}"); + } + File.WriteAllLines("output-session-messages.txt", + filteredSessionMessageList.Select(m => + $"Msg[{m.Index}][{m.MessageRoleText}]: {m}")); + } + #endregion + + internal static void DemoUseSessionMessageHost2(MachiningProject project) + { + SessionMessageHost sessionMessageHost = project.SessionMessageHost; + IMachiningChain machiningChain = project.MachiningEquipment?.MachiningChain; + + PresentAttribute mrrPresent = typeof(MachiningStep).GetProperty(nameof(MachiningStep.Mrr_mm3ds)).GetCustomAttribute(); + string mrrUnit = mrrPresent?.TailUnitString; + string mrrFormat = mrrPresent?.DataFormatString; + PresentAttribute torquePresent = typeof(MachiningStep).GetProperty(nameof(MachiningStep.AvgAbsTorque_Nm)).GetCustomAttribute(); + string torqueUnit = torquePresent?.TailUnitString; + string torqueFormat = torquePresent?.DataFormatString; + + SessionMessageHost.FilterFlag filterFlags = + SessionMessageHost.FilterFlag.Step | + SessionMessageHost.FilterFlag.NC | + SessionMessageHost.FilterFlag.Progress | + SessionMessageHost.FilterFlag.Error; + string filterText = null; + var filteredSessionMessageList = sessionMessageHost + .GetFliteredList(filterFlags, filterText); + + foreach (var sessionMessage in filteredSessionMessageList) + { + //M.I.: Message Index. + Console.Write($"M.I.: {sessionMessage.Index}; Role: {sessionMessage.MessageRoleText}"); + + // For SessionMessageHost.FilterFlag.Step + var step = sessionMessage.MachiningStep; + if (step != null) + { + string[] machineCoordinateValueTexts = GetMachineCoordinateValueTexts(step, machiningChain); + var machineCoordinatesText = string.Join("; ", Enumerable.Range(0, machiningChain.McCodes.Length) + .Select(i => $"MC.{machiningChain.McCodes[i]}: {machineCoordinateValueTexts[i]}")); + Console.Write($"Time: {step.AccumulatedTime:G}; MRR = {step.Mrr_mm3ds.ToString(mrrFormat)} {mrrUnit}; Torque = {step.AvgAbsTorque_Nm?.ToString(torqueFormat)} {torqueUnit}; {machineCoordinatesText}; "); + var nc_ = sessionMessageHost.GetSourceCommand(sessionMessage); + Console.WriteLine($"Message/NC: {nc_.Line}; File: {nc_.FilePath}; LineNo: {nc_.GetLineNo()}"); + } + + // For SessionMessageHost.FilterFlag.NC + var nc = sessionMessage.DirectInstantSourceCommand; + if (nc != null) + { + Console.Write($"Message/NC: {nc.Line}; File: {nc.FilePath}; LineNo: {nc.GetLineNo()}; "); + if (nc is NcLine ncLine) + Console.WriteLine($"T: {ncLine.T}; S: {ncLine.S}; F: {ncLine.F}; NC-Flags: {ncLine.FlagsText}"); + } + + // For SessionMessageHost.FilterFlag.Progress or Error. + var multiTagMessage = sessionMessage.MultiTagMessage; + if (multiTagMessage != null) + Console.WriteLine($"Message/NC: {multiTagMessage.Message}"); + var exception = sessionMessage.Exception; + if (exception != null) + Console.WriteLine($"Message/NC: {exception.Message}"); + } + } + static string[] GetMachineCoordinateValueTexts(MachiningStep step, IMachiningChain machiningChain) + { + var mcTransformers = machiningChain.McTransformers; + string[] dst = new string[mcTransformers.Length]; + if (mcTransformers != null) + { + for (int i = 0; i < mcTransformers.Length; i++) + { + if (mcTransformers[i] == null) + continue; + if (mcTransformers[i] is DynamicRotation) + dst[i] = MathUtil.ToDeg(step.GetMcValue(i).Value).ToString("F4"); + else + dst[i] = step.GetMcValue(i)?.ToString("F5"); + } + } + return dst; + } + #region ShowStepPresent + internal static void ShowStepPresent( + UserEnv 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 + } +} \ No newline at end of file diff --git a/Geom/DemoBuildGeom.cs b/Geom/DemoBuildGeom.cs index 7aec126..39fa369 100644 --- a/Geom/DemoBuildGeom.cs +++ b/Geom/DemoBuildGeom.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using Hi.Geom; using Hi.Mech.Topo; @@ -29,6 +30,7 @@ namespace Sample.Geom LongitudeNum = 30 }; Stl stl = new Stl("geom.stl"); + StlFile stlFile = new StlFile("geom.stl"); TransformationGeom transformationGeom = new TransformationGeom() { Transformer = new GeneralTransform(1, @@ -36,7 +38,9 @@ namespace Sample.Geom new StaticTranslation(new Vec3d(0, 0, 0))), Geom = stl }; - return new List([box, cylindroid, stl, transformationGeom]); + GeomCombination geomCombination = new GeomCombination(stlFile, transformationGeom); + return new List([box, cylindroid, stl, stlFile, transformationGeom]); } } + } diff --git a/HelloHiAPI.cs b/HelloHiAPI.cs index a6c06df..3b0a181 100644 --- a/HelloHiAPI.cs +++ b/HelloHiAPI.cs @@ -1,4 +1,5 @@ using Hi.Disp; +using Hi.HiNcKits; using Hi.Licenses; using Hi.MongoUtils; using System; @@ -20,18 +21,11 @@ namespace Sample static int Main(string[] args) { Console.WriteLine("HiAPI starting."); - License.LogInAll(); - DispEngine.Init(); - MongoServer.Default = MongoServer.Run(new MongoRunnerOptions() - { - MongoPort = 28100 - }); + SingleUserApp.AppBegin(); Console.WriteLine("Hello World! HiAPI."); - MongoServer.Default.Dispose(); - DispEngine.FinishDisp(); - License.LogOutAll(); + SingleUserApp.AppEnd(); Console.WriteLine("HiAPI exited."); return 0; diff --git a/MachineTool/DemoBuildMachineTool.cs b/MachineTool/DemoBuildMachineTool.cs index 26d4338..456c35b 100644 --- a/MachineTool/DemoBuildMachineTool.cs +++ b/MachineTool/DemoBuildMachineTool.cs @@ -80,7 +80,5 @@ namespace Sample.MachineTool public CodeXyzabcMachineTool GetXyzabcMachineTool() => MachineTool; /// public IMachiningChain GetMachiningChain() => MachineTool; - /// - public ISolidMachiningChain GetSolidMachiningChain() => MachineTool; } } diff --git a/Machining/DemoBuildGeomOnlyMachiningProject.cs b/Machining/DemoBuildGeomOnlyMachiningProject.cs new file mode 100644 index 0000000..da0b20d --- /dev/null +++ b/Machining/DemoBuildGeomOnlyMachiningProject.cs @@ -0,0 +1,98 @@ +using Hi.Common.XmlUtils; +using Hi.Geom; +using Hi.MachiningProcs; +using Hi.Mech.Topo; +using Hi.Milling.Apts; +using Hi.Milling.Cutters; +using Hi.MongoUtils; +using Hi.NcMech.Fixtures; +using Hi.NcMech.Workpieces; +using Hi.NcMech.Xyzabc; +using System.Collections.Generic; +using System.IO; +using System; +using Hi.NcMech.Holders; +using Hi.Licenses; +using Hi.Machining; +using Hi.MillingMech.MillingTools; +using Hi.HiNcKits; + +namespace Sample.Machining +{ + public static class DemoBuildGeomOnlyMachiningProject + { + internal static MillingCutter CreateGeomOnlyMillingCutter() + { + MillingCutter millingCutter = new MillingCutter() + { + UpperBeamGeom = new TransformationGeom() + }; + + double diameter_mm = 12; + millingCutter.ShaperProfile = new AptProfile(millingCutter, + new ColumnApt() + { + Diameter_mm = diameter_mm, + FluteHeight_mm = 40 + }); + + return millingCutter; + } + + static void Main() + { + SingleUserApp.AppBegin(); + + var projectPath = "C:/HiNC-Projects/NewProject/Main.hincproj"; + var projectDirectory = Path.GetDirectoryName(projectPath); + Console.WriteLine($"Directory of the New Project: {projectDirectory}"); + MachiningProject machiningProject = new MachiningProject(projectDirectory); + + CylindroidHolder cylindroidHolder = new CylindroidHolder() + { + Note = "BT40", + Cylindroid = new Cylindroid() + { + PairZrs = new List([ new PairZr(0,12),new PairZr(20,12), + new PairZr(20,16),new PairZr(30,16)]), + LongitudeNum = 30 + } + }; + machiningProject.MachiningToolHouse = new MachiningToolHouse() + { + [1] = new MillingTool() + { + Note = "T1", + PreservedDistanceBetweenFluteAndSpindleNose_mm = 8, + Holder = cylindroidHolder, + Cutter = CreateGeomOnlyMillingCutter() + }, + }; + + machiningProject.MachiningEquipment.Fixture = new Fixture() + { + Geom = new Box3d(new Vec3d(-40, -40, 0), new Vec3d(40, 40, 10)), + GeomToWorkpieceTransformer = new StaticTranslation(new Vec3d(0, 0, 10)), + }; + + machiningProject.MachiningEquipment.Workpiece = new Workpiece() + { + InitResolution = 0.25, + InitGeom = new Box3d(0, 0, -50, 70, 50, 0), + IdealGeom = null, + WorkpieceGeomToFixtureBuckleTransformer = new StaticTranslation(new Vec3d(0, 0, 0)), + }; + + machiningProject.MachiningEquipment.MachiningChain + = XFactory.Default.GenByFile( + "Resource", "MachineTool/PMC-B1/PMC-B1.mt", GenMode.Default); + machiningProject.MachiningEquipment.MachiningChainFile = "PMC-B1/PMC-B1.mt"; + + machiningProject.MakeXmlSourceFile(projectPath); + + machiningProject.Dispose(); + SingleUserApp.AppEnd(); + } + + } +} diff --git a/Machining/DemoBuildMachiningProject.cs b/Machining/DemoBuildMachiningProject.cs index dc15047..1c7e479 100644 --- a/Machining/DemoBuildMachiningProject.cs +++ b/Machining/DemoBuildMachiningProject.cs @@ -15,17 +15,16 @@ using Hi.NcMech.Workpieces; using Hi.NcMech.Xyzabc; using Hi.NcOpt; using Hi.Physics; -using Hi.MongoUtils; -using Hi.Licenses; using Hi.MachiningProcs; using System.IO; +using Hi.HiNcKits; namespace Sample.Machining { /// /// Demonstrates how to create and configure a programmatically. /// This sample shows how to set up , , - /// , , , + /// , , , /// and save the project configuration to a file. /// /// @@ -34,8 +33,26 @@ namespace Sample.Machining /// public static class DemoBuildMachiningProject { + internal static CylindroidHolder CreateCylindroidHolder() + { + #region ConfigureHolder + CylindroidHolder cylindroidHolder = new CylindroidHolder() + { + Note = "BT40", + Cylindroid = new Cylindroid() + { + PairZrs = new List([ new PairZr(0,12),new PairZr(20,12), + new PairZr(20,16),new PairZr(30,16)]), + LongitudeNum = 30 + } + }; + #endregion + return cylindroidHolder; + } + + #region CreateMillingCutter1 - private static MillingCutter CreateMillingCutter1() + internal static MillingCutter CreateMillingCutter1() { MillingCutter millingCutter = new MillingCutter() { @@ -91,7 +108,7 @@ namespace Sample.Machining #endregion #region CreateMillingCutter2 - private static MillingCutter CreateMillingCutter2() + internal static MillingCutter CreateMillingCutter2() { MillingCutter millingCutter = new MillingCutter() { @@ -137,55 +154,35 @@ namespace Sample.Machining [STAThread] static void Main() { - Console.WriteLine($"License Login."); - License.LogInAll(); - - Console.WriteLine($"Start Database."); - MongoServer.Default = MongoServer.Run(new MongoRunnerOptions() - { - MongoPort = 28100 - }); + SingleUserApp.AppBegin(); var projectPath = "C:/HiNC-Projects/NewProject/Main.hincproj"; var projectDirectory = Path.GetDirectoryName(projectPath); Console.WriteLine($"Directory of the New Project: {projectDirectory}"); MachiningProject machiningProject = new MachiningProject(projectDirectory); - #region ConfigureHolder - CylindroidHolder cylindroidHolder = new CylindroidHolder() - { - Note = "BT40", - Cylindroid = new Cylindroid() - { - PairZrs = new List([ new PairZr(0,12),new PairZr(20,12), - new PairZr(20,16),new PairZr(30,16)]), - LongitudeNum = 30 - } - }; - #endregion - #region ConfigureMachiningToolHouse machiningProject.MachiningToolHouse = new MachiningToolHouse() { - [1] = new StickMachiningTool() + [1] = new MillingTool() { Note = "T1", PreservedDistanceBetweenFluteAndSpindleNose_mm = 8, - Holder = cylindroidHolder, + Holder = CreateCylindroidHolder(), Cutter = CreateMillingCutter1() }, - [2] = new StickMachiningTool() + [2] = new MillingTool() { Note = "T2", PreservedDistanceBetweenFluteAndSpindleNose_mm = 8, - Holder = cylindroidHolder, + Holder = CreateCylindroidHolder(), Cutter = CreateMillingCutter2() }, - [3] = new StickMachiningTool() + [3] = new MillingTool() { Note = "T3", PreservedDistanceBetweenFluteAndSpindleNose_mm = 8, - Holder = cylindroidHolder, + Holder = CreateCylindroidHolder(), Cutter = CreateMillingCutter1() }, }; @@ -195,7 +192,7 @@ namespace Sample.Machining machiningProject.MachiningEquipment.Fixture = new Fixture() { Geom = new Box3d(new Vec3d(-40, -40, 0), new Vec3d(40, 40, 10)), - TableToWorkpieceTransformer = new StaticTranslation(new Vec3d(0, 0, 10)), + GeomToWorkpieceTransformer = new StaticTranslation(new Vec3d(0, 0, 10)), }; #endregion @@ -214,12 +211,15 @@ namespace Sample.Machining #endregion #region ConfigureMachineChain - machiningProject.MachiningEquipment.SolidMachiningChain + machiningProject.MachiningEquipment.MachiningChain = XFactory.Default.GenByFile( - "Resource/MachineTool", "PMC-B1/PMC-B1.mt", GenMode.Default); + "Resource", "MachineTool/PMC-B1/PMC-B1.mt", GenMode.Default); #endregion machiningProject.MakeXmlSourceFile(projectPath); + + machiningProject.Dispose(); + SingleUserApp.AppEnd(); } #endregion } diff --git a/Machining/DemoUseMachiningProject.cs b/Machining/DemoUseMachiningProject.cs index cba3216..b365115 100644 --- a/Machining/DemoUseMachiningProject.cs +++ b/Machining/DemoUseMachiningProject.cs @@ -1,9 +1,8 @@ using System; -using Hi.Licenses; using Hi.Common.Messages; using Hi.MachiningProcs; using Hi.Common.FileLines; -using Hi.MongoUtils; +using Hi.HiNcKits; namespace Sample.Machining { @@ -21,16 +20,7 @@ namespace Sample.Machining { static void Main() { - #region LicenseAndDatabaseSetup - Console.WriteLine($"License Login."); - License.LogInAll(); - - Console.WriteLine($"Start Database."); - MongoServer.Default = MongoServer.Run(new MongoRunnerOptions() - { - MongoPort = 28100 - }); - #endregion + SingleUserApp.AppBegin(); #region ProjectLoading var projectPath = "C:/HiNC-Projects/DemoStandardPath/Main.hincproj"; @@ -45,7 +35,7 @@ namespace Sample.Machining { if (pack.Tags.Contains(MessageFlag.Warning.ToString()) || pack.Tags.Contains(MessageFlag.Error.ToString()) || - pack.Tags.Contains(MessageFlag.SysErr.ToString())) + pack.Tags.Contains(MessageFlag.Exception.ToString())) { var sourceCommand = pack.SourceCommand; Console.WriteLine($"{pack.Message} At \"{sourceCommand?.FilePath}\" (Line {sourceCommand?.GetLineNo()}) \"{sourceCommand?.Line}\""); @@ -82,11 +72,7 @@ namespace Sample.Machining Console.WriteLine($"Close Project: {projectPath}"); machiningProject.Dispose(); - Console.WriteLine($"Close Database."); - MongoServer.Default.Dispose(); - - Console.WriteLine($"License Logout."); - License.LogOutAll(); + SingleUserApp.AppEnd(); Console.WriteLine($"Program end."); #endregion