Files
Hi.Sample/Machining/DemoBuildMachiningProject.cs
T
iambossandClaude Opus 5 7beb671352 refactor(demos): build sample machine tools as GeneralXyzabcMachineTool
Follows the CodeXyzabcMachineTool retirement in HiMech. DemoBuildMachineTool
drops IGetCodeXyzabcMachineTool, which no longer exists, and spells out the
doc comments the inherited interface used to supply.

The two GenByFile<CodeXyzabcMachineTool> call sites are fixed rather than
merely renamed: they read Resource machine tools rooted at
<GeneralXyzabcMachineTool>, so the factory's `as` cast handed back null and
the demo project got no machining chain at all.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-28 19:57:54 +08:00

220 lines
6.3 KiB
C#

using System;
using Hi.Milling.Apts;
using Hi.Common.XmlUtils;
using Hi.Geom;
using Hi.Machining;
using Hi.Mech.Topo;
using Hi.Milling.Cutters;
using Hi.Milling.FluteContours;
using Hi.MillingForces.Fittings;
using Hi.NcMech.Fixtures;
using Hi.NcMech.Holders;
using Hi.NcMech.Workpieces;
using Hi.NcMech.Xyzabc;
using Hi.NcOpt;
using Hi.Physics;
using Hi.MachiningProcs;
using System.IO;
using Hi.HiNcKits;
using Hi.Milling.MillingTools;
using Microsoft.Extensions.Logging;
namespace Sample.Machining;
/// <summary>
/// Demonstrates how to create and configure a <see cref="MachiningProject"/> programmatically.
/// This sample shows how to set up <see cref="MillingCutter"/>, <see cref="CylindroidHolder"/>,
/// <see cref="MillingTool"/>, <see cref="Fixture"/>, <see cref="Workpiece"/>,
/// and save the project configuration to a file.
/// </summary>
/// <remarks>
/// ### Source Code
/// [!code-csharp[SampleCode](~/../Hi.Sample/Machining/DemoBuildMachiningProject.cs)]
/// </remarks>
public static class DemoBuildMachiningProject
{
internal static CylindroidHolder CreateCylindroidHolder()
{
#region ConfigureHolder
CylindroidHolder cylindroidHolder = new CylindroidHolder()
{
Note = "BT40",
Cylindroid = new Cylindroid([ new PairZr(0,12),new PairZr(20,12),
new PairZr(20,16),new PairZr(30,16)])
};
#endregion
return cylindroidHolder;
}
#region CreateMillingCutter1
internal static MillingCutter CreateMillingCutter1()
{
double diameter_mm = 12;
double roundRadius_mm = 0.5;
MillingCutter millingCutter = new MillingCutter()
{
ShaperProfile = new AptProfile(
new ColumnApt()
{
Diameter_mm = diameter_mm,
Rc_mm = roundRadius_mm,
FluteHeight_mm = 40
}),
UpperBeamGeom = new ExtendedCylinder(90),
InnerBeamProfile = new FluteDependentRatioProfile(),
MillingCutterOptLimit = new MillingCutterOptOption(),
};
//build Fluting property
double helixAngle_deg = 50;
double radialRakeAngle_deg = 15;
double bottomOuterRadius_mm = diameter_mm / 2 - roundRadius_mm;
millingCutter.Fluting = new UniformFluting()
{
TrackNum = 3,
BaselineOneContour = new FluteContour()
{
BaseSideContour = new ConstHelixSideContour(millingCutter)
{
Helix_deg = helixAngle_deg,
RadialRakeAngle_deg = radialRakeAngle_deg
},
BaseBottomContour = new SlideBottomContour()
{
//In most case, helix angle from side equal to rake angle from bottom.
AxialRakeAngle_deg = MathUtil.ToDeg(helixAngle_deg),
DiskAngle_deg = 1.5,
//In most case, rake angle from side equal to eccentric angle from bottom.
EccentricAngle_deg = radialRakeAngle_deg,
CutterLengthOnBottomProjection_mm = bottomOuterRadius_mm * 0.9,
OuterRadius_mm = bottomOuterRadius_mm
}
}
};
return millingCutter;
}
#endregion
#region CreateMillingCutter2
internal static MillingCutter CreateMillingCutter2()
{
MillingCutter millingCutter = new MillingCutter()
{
ShaperProfile = new AptProfile(new ColumnApt()
{
Diameter_mm = 12,
FluteHeight_mm = 40
}),
UpperBeamGeom = new Cylindroid(
[new PairZr(40, 6), new PairZr(90, 6)]),
InnerBeamProfile = new FluteDependentRatioProfile(),
MillingCutterOptLimit = new MillingCutterOptOption()
};
//build Fluting property
double helixAngle_deg = 50;
double radialRakeAngle_deg = 15;
millingCutter.Fluting = new UniformFluting()
{
TrackNum = 3,
BaselineOneContour = new FluteContour()
{
BaseSideContour = new ConstHelixSideContour(millingCutter)
{
Helix_deg = helixAngle_deg,
RadialRakeAngle_deg = radialRakeAngle_deg
},
//since no bottom cut, set bottom contour to null.
BaseBottomContour = null
}
};
return millingCutter;
}
#endregion
internal static CustomSpinningProfile CreateCustomSpinningProfile()
{
return new CustomSpinningProfile(
new Cylindroid(new PairZr(0, 4), new PairZr(50, 4)));
}
#region TestMethod
[STAThread]
static void Main()
{
using var loggerFactory = Microsoft.Extensions.Logging.LoggerFactory.Create(b => b.AddConsole());
LocalApp.AppBegin(loggerFactory.CreateLogger("Hi.Sample"));
LocalProjectService localProjectService = new LocalProjectService();
var projectPath = "C:/HiNC-Projects/NewProject/Main.hincproj";
var projectDirectory = Path.GetDirectoryName(projectPath);
Console.WriteLine($"Directory of the New Project: {projectDirectory}");
localProjectService.LoadProject(projectPath);
MachiningProject machiningProject = localProjectService.MachiningProject;
IProgress<object> progress = null;
#region ConfigureMachiningToolHouse
localProjectService.MachiningToolHouse = new MachiningToolHouse()
{
[1] = new MillingTool()
{
Note = "T1",
PreservedDistanceBetweenFluteAndSpindleNose_mm = 8,
Holder = CreateCylindroidHolder(),
Cutter = CreateMillingCutter1()
},
[2] = new MillingTool()
{
Note = "T2",
PreservedDistanceBetweenFluteAndSpindleNose_mm = 8,
Holder = CreateCylindroidHolder(),
Cutter = CreateMillingCutter2()
},
[3] = new MillingTool()
{
Note = "T3",
PreservedDistanceBetweenFluteAndSpindleNose_mm = 8,
Holder = CreateCylindroidHolder(),
Cutter = CreateMillingCutter1()
},
};
#endregion
#region ConfigureFixture
localProjectService.Fixture = new Fixture()
{
Geom = new Box3d(new Vec3d(-40, -40, 0), new Vec3d(40, 40, 10)),
GeomToWorkpieceTransformer = new StaticTranslation(new Vec3d(0, 0, 10)),
};
#endregion
#region ConfigureWorkpiece
localProjectService.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)),
CuttingPara = XFactory.GenByFile<ICuttingPara>(
"Resource/CuttingParameter", "Al6061T6.default.mp", progress),
WorkpieceMaterial = XFactory.GenByFile<WorkpieceMaterial>(
"Resource/WorkpieceMaterial", "Al6061T6.default.WorkpieceMaterial", progress),
};
#endregion
#region ConfigureMachineChain
localProjectService.MachiningChain
= XFactory.GenByFile<GeneralXyzabcMachineTool>(
"Resource", "MachineTool/Table-B1.default/Table-B1.mt", progress);
#endregion
machiningProject.MakeXmlSourceToFile(projectPath);
machiningProject.Dispose();
LocalApp.AppEnd();
}
#endregion
}