Compare commits

...

2 Commits

Author SHA1 Message Date
dd4c3ef28f refactor(Sample): adapt demos to XFactory IProgress<object> signature
Made-with: Cursor
2026-04-11 11:19:36 +08:00
e5ea1961a5 refactor(demos): update machining demos and remove legacy demo message handling
Made-with: Cursor
2026-04-08 16:33:17 +08:00
7 changed files with 86 additions and 137 deletions

View File

@ -1,57 +0,0 @@
using System;
using System.Threading.Tasks;
using Hi.Common;
using Hi.Common.Messages;
namespace Sample.Common;
/// <summary>
/// Demonstrates common message and exception handling patterns in HiAPI applications
/// </summary>
/// <remarks>
/// ### Source Code
/// [!code-csharp[SampleCode](~/../Hi.Sample/Common/DemoMessageAndExceptionHandling.cs)]
/// </remarks>
public static class DemoMessageAndExceptionHandling
{
/// <summary>
/// Demonstrates normal message handling
/// </summary>
internal static void DemoNormalMessages()
{
#region Normal_Messages
MessageUtil.ReportMessage("Operation completed successfully.");
MessageUtil.ReportWarning("Please check your input.");
#endregion
}
/// <summary>
/// Demonstrates exception handling in synchronous code
/// </summary>
internal static void DemoSynchronousExceptionHandling()
{
#region Sync_Exception
try
{
// Your code here
throw new NotImplementedException("Demo exception");
}
catch (Exception ex)
{
ExceptionUtil.ShowException(ex, null);
}
#endregion
}
/// <summary>
/// Demonstrates exception handling in asynchronous code
/// </summary>
internal static async Task DemoAsynchronousExceptionHandling()
{
#region Async_Exception
await Task.Run(() =>
{
// Your async operation here
throw new NotImplementedException("Demo async exception");
}).ShowIfCatched(null);
#endregion
}
}

View File

@ -18,7 +18,8 @@ namespace Sample
static int Main(string[] args) static int Main(string[] args)
{ {
Console.WriteLine("HiAPI starting."); Console.WriteLine("HiAPI starting.");
LocalApp.AppBegin(); using var loggerFactory = Microsoft.Extensions.Logging.LoggerFactory.Create(b => b.AddConsole());
LocalApp.AppBegin(loggerFactory.CreateLogger("Hi.Sample"));
Console.WriteLine("Hello World! HiAPI."); Console.WriteLine("Hello World! HiAPI.");

View File

@ -1,4 +1,4 @@
using Hi.Common.XmlUtils; using Hi.Common.XmlUtils;
using Hi.Geom; using Hi.Geom;
using Hi.Mech; using Hi.Mech;
using Hi.Mech.Topo; using Hi.Mech.Topo;
@ -25,7 +25,7 @@ namespace Sample.MachineTool
{ {
static DemoBuildMachineTool() static DemoBuildMachineTool()
{ {
XFactory.Regs.Add(XName, (xml, baseDirectory, relFile, res) => new DemoBuildMachineTool()); XFactory.Regs.Add(XName, (xml, baseDirectory, relFile, progress, res) => new DemoBuildMachineTool());
} }
/// <summary> /// <summary>
/// Generates an XYZ-ABC machine tool instance from embedded resources. /// Generates an XYZ-ABC machine tool instance from embedded resources.

View File

@ -1,4 +1,4 @@
using Hi.Common.XmlUtils; using Hi.Common.XmlUtils;
using Hi.Geom; using Hi.Geom;
using Hi.MachiningProcs; using Hi.MachiningProcs;
using Hi.Mech.Topo; using Hi.Mech.Topo;
@ -42,7 +42,8 @@ public static class DemoBuildGeomOnlyMachiningProject
static void Main() static void Main()
{ {
LocalApp.AppBegin(); using var loggerFactory = Microsoft.Extensions.Logging.LoggerFactory.Create(b => b.AddConsole());
LocalApp.AppBegin(loggerFactory.CreateLogger("Hi.Sample"));
LocalProjectService localProjectService = new LocalProjectService(); LocalProjectService localProjectService = new LocalProjectService();
var projectPath = "C:/HiNC-Projects/NewProject/Main.hincproj"; var projectPath = "C:/HiNC-Projects/NewProject/Main.hincproj";
@ -82,9 +83,10 @@ public static class DemoBuildGeomOnlyMachiningProject
WorkpieceGeomToFixtureBuckleTransformer = new StaticTranslation(new Vec3d(0, 0, 0)), WorkpieceGeomToFixtureBuckleTransformer = new StaticTranslation(new Vec3d(0, 0, 0)),
}; };
IProgress<object> progress = null;
localProjectService.MachiningChain localProjectService.MachiningChain
= XFactory.GenByFile<CodeXyzabcMachineTool>( = XFactory.GenByFile<CodeXyzabcMachineTool>(
"Resource", "MachineTool/PMC-B1/PMC-B1.mt", GenMode.Default); "Resource", "MachineTool/PMC-B1/PMC-B1.mt", progress);
localProjectService.MachiningChainFile = "PMC-B1/PMC-B1.mt"; localProjectService.MachiningChainFile = "PMC-B1/PMC-B1.mt";
localProjectService.SaveProject(); localProjectService.SaveProject();

View File

@ -1,4 +1,4 @@
using System; using System;
using Hi.Milling.Apts; using Hi.Milling.Apts;
using Hi.Common.XmlUtils; using Hi.Common.XmlUtils;
using Hi.Geom; using Hi.Geom;
@ -142,7 +142,8 @@ public static class DemoBuildMachiningProject
[STAThread] [STAThread]
static void Main() static void Main()
{ {
LocalApp.AppBegin(); using var loggerFactory = Microsoft.Extensions.Logging.LoggerFactory.Create(b => b.AddConsole());
LocalApp.AppBegin(loggerFactory.CreateLogger("Hi.Sample"));
LocalProjectService localProjectService = new LocalProjectService(); LocalProjectService localProjectService = new LocalProjectService();
var projectPath = "C:/HiNC-Projects/NewProject/Main.hincproj"; var projectPath = "C:/HiNC-Projects/NewProject/Main.hincproj";
@ -151,6 +152,8 @@ public static class DemoBuildMachiningProject
localProjectService.LoadProject(projectPath); localProjectService.LoadProject(projectPath);
MachiningProject machiningProject = localProjectService.MachiningProject; MachiningProject machiningProject = localProjectService.MachiningProject;
IProgress<object> progress = null;
#region ConfigureMachiningToolHouse #region ConfigureMachiningToolHouse
localProjectService.MachiningToolHouse = new MachiningToolHouse() localProjectService.MachiningToolHouse = new MachiningToolHouse()
{ {
@ -194,16 +197,16 @@ public static class DemoBuildMachiningProject
IdealGeom = null, IdealGeom = null,
WorkpieceGeomToFixtureBuckleTransformer = new StaticTranslation(new Vec3d(0, 0, 0)), WorkpieceGeomToFixtureBuckleTransformer = new StaticTranslation(new Vec3d(0, 0, 0)),
CuttingPara = XFactory.GenByFile<ICuttingPara>( CuttingPara = XFactory.GenByFile<ICuttingPara>(
"Resource/CuttingParameter", "Al6061T6.mp", GenMode.Default), "Resource/CuttingParameter", "Al6061T6.mp", progress),
WorkpieceMaterial = XFactory.GenByFile<WorkpieceMaterial>( WorkpieceMaterial = XFactory.GenByFile<WorkpieceMaterial>(
"Resource/WorkpieceMaterial", "Al6061T6.WorkpieceMaterial", GenMode.Default), "Resource/WorkpieceMaterial", "Al6061T6.WorkpieceMaterial", progress),
}; };
#endregion #endregion
#region ConfigureMachineChain #region ConfigureMachineChain
localProjectService.MachiningChain localProjectService.MachiningChain
= XFactory.GenByFile<CodeXyzabcMachineTool>( = XFactory.GenByFile<CodeXyzabcMachineTool>(
"Resource", "MachineTool/PMC-B1/PMC-B1.mt", GenMode.Default); "Resource", "MachineTool/PMC-B1/PMC-B1.mt", progress);
#endregion #endregion
machiningProject.MakeXmlSourceToFile(projectPath); machiningProject.MakeXmlSourceToFile(projectPath);

View File

@ -21,7 +21,8 @@ public static class DemoUseMachiningProject
{ {
static void Main() static void Main()
{ {
LocalApp.AppBegin(); using var loggerFactory = Microsoft.Extensions.Logging.LoggerFactory.Create(b => b.AddConsole());
LocalApp.AppBegin(loggerFactory.CreateLogger("Hi.Sample"));
LocalProjectService localProjectService = new LocalProjectService(); LocalProjectService localProjectService = new LocalProjectService();
#region ProjectLoading #region ProjectLoading

View File

@ -4,79 +4,78 @@ using Hi.Disp;
using Hi.Geom; using Hi.Geom;
using Hi.Mech.Topo; using Hi.Mech.Topo;
namespace Sample.Mech namespace Sample.Mech;
/// <summary>
/// Demonstrates the creation and visualization of mechanical assemblies with kinematic linkages.
/// Shows how to build coordinate systems, establish kinematic relationships, and capture visual output.
/// </summary>
/// <remarks>
/// ### Source Code
/// [!code-csharp[SampleCode](~/../Hi.Sample/Mech/DemoTopo1.cs)]
/// </remarks>
public static class DemoTopo1
{ {
/// <summary> /// <summary>
/// Demonstrates the creation and visualization of mechanical assemblies with kinematic linkages. /// Creates a demonstration assembly with kinematic linkages.
/// Shows how to build coordinate systems, establish kinematic relationships, and capture visual output. /// Builds a mechanical assembly with multiple anchors and branches, including both static and dynamic transformations.
/// </summary> /// </summary>
/// <remarks> /// <returns>A tuple containing the assembly and root anchor</returns>
/// ### Source Code static (Asmb asmb, Anchor root) GetDemoAsmb()
/// [!code-csharp[SampleCode](~/../Hi.Sample/Mech/DemoTopo1.cs)] {
/// </remarks> #region DocSite.DemoTopo1
public static class DemoTopo1 //build coordinate systems and the assembly.
{ Asmb asmb = new Asmb { Name = "Mech" };
/// <summary> Anchor O = new Anchor(asmb, "O");
/// Creates a demonstration assembly with kinematic linkages. Anchor O1 = new Anchor(asmb, "O1");
/// Builds a mechanical assembly with multiple anchors and branches, including both static and dynamic transformations. Anchor X = new Anchor(asmb, "X");
/// </summary> Anchor Z = new Anchor(asmb, "Z");
/// <returns>A tuple containing the assembly and root anchor</returns> Anchor B = new Anchor(asmb, "B");
static (Asmb asmb,Anchor root) GetDemoAsmb()
{
#region DocSite.DemoTopo1
//build coordinate systems and the assembly.
Asmb asmb = new Asmb { Name = "Mech" };
Anchor O = new Anchor(asmb, "O");
Anchor O1 = new Anchor(asmb, "O1");
Anchor X = new Anchor(asmb, "X");
Anchor Z = new Anchor(asmb, "Z");
Anchor B = new Anchor(asmb, "B");
//build kinematic link //build kinematic link
Branch.Attach(O, O1, new StaticTranslation(new Vec3d(0, 0, 80))); Branch.Attach(O, O1, new StaticTranslation(new Vec3d(0, 0, 80)));
Branch brnX = Branch.Attach(O1, X, new DynamicTranslation(new Vec3d(1, 0, 0))); Branch brnX = Branch.Attach(O1, X, new DynamicTranslation(new Vec3d(1, 0, 0)));
Branch brnZ = Branch.Attach(X, Z, new DynamicTranslation(new Vec3d(0, 0, 1))); Branch brnZ = Branch.Attach(X, Z, new DynamicTranslation(new Vec3d(0, 0, 1)));
Branch brnB = Branch.Attach(Z, B, new DynamicRotation(new Vec3d(0, 1, 0), 0, new Vec3d(-100, 0, 0))); Branch brnB = Branch.Attach(Z, B, new DynamicRotation(new Vec3d(0, 1, 0), 0, new Vec3d(-100, 0, 0)));
//drive the dynamic transformation by single value for each branch. //drive the dynamic transformation by single value for each branch.
brnX.Step = 200; brnX.Step = 200;
brnZ.Step = 100; brnZ.Step = 100;
brnB.Step = MathUtil.ToRad(-60); brnB.Step = MathUtil.ToRad(-60);
//Get and show the transform matrices relative to O. //Get and show the transform matrices relative to O.
Dictionary<Anchor, Mat4d> matMap = asmb.GetMat4dMap(O); Dictionary<Anchor, Mat4d> matMap = asmb.GetMat4dMap(O);
Console.WriteLine("Transform Matrix relative to O:"); Console.WriteLine("Transform Matrix relative to O:");
foreach (KeyValuePair<Anchor, Mat4d> keyValue in matMap) foreach (KeyValuePair<Anchor, Mat4d> keyValue in matMap)
Console.WriteLine($"{keyValue.Key.Name} : {keyValue.Value}"); Console.WriteLine($"{keyValue.Key.Name} : {keyValue.Value}");
#endregion #endregion
return (asmb,O); return (asmb, O);
} }
/// <summary> /// <summary>
/// Captures the assembly visualization and saves it to a file. /// Captures the assembly visualization and saves it to a file.
/// Initializes the display engine, sets up the assembly visualization with an isometric view, and saves a snapshot to a bitmap file. /// Initializes the display engine, sets up the assembly visualization with an isometric view, and saves a snapshot to a bitmap file.
/// </summary> /// </summary>
/// <param name="src">A tuple containing the assembly and root anchor for visualization</param> /// <param name="src">A tuple containing the assembly and root anchor for visualization</param>
static void SnapshotToFile((Asmb asmb, Anchor root) src) static void SnapshotToFile((Asmb asmb, Anchor root) src)
{ {
//all the drawing function has to call DispEngine.Init() before using. //all the drawing function has to call DispEngine.Init() before using.
DispEngine.Init(); DispEngine.Init();
DispEngine.EnableSuppressDefaultLogo = true; DispEngine.EnableSuppressDefaultLogo = true;
using (DispEngine dispEngine = new DispEngine( using (DispEngine dispEngine = new DispEngine(
src.asmb.GetAsmbDraw(src.root))) src.asmb.GetAsmbDraw(src.root)))
{ {
dispEngine.SetViewToIsometricView(); dispEngine.SetViewToIsometricView();
dispEngine.Snapshot("DemoTopo1.bmp", 680, 480); dispEngine.Snapshot("DemoTopo1.bmp", 680, 480);
} }
Console.WriteLine("Snapshot file output."); Console.WriteLine("Snapshot file output.");
DispEngine.FinishDisp(); DispEngine.FinishDisp();
} }
static void Main() static void Main()
{ {
SnapshotToFile(GetDemoAsmb()); SnapshotToFile(GetDemoAsmb());
} }
}
} }