Table of Contents

Class DemoMillingByCutterLocation

Namespace
Sample.Machining
Assembly
Hi.Sample.Wpf.dll
public static class DemoMillingByCutterLocation
Inheritance
DemoMillingByCutterLocation
Inherited Members

Remarks

Source Code

using Hi.Common;
using Hi.Disp;
using Hi.Geom;
using Hi.Geom.Resolution;
using Hi.HiNcKits;
using Hi.MachiningProcs;
using Hi.Numerical.Acts;
using Hi.Wpf.Disp;
using HiMachining.Milling;
using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows;

namespace Sample.Machining
{
    /// <remarks>
    /// ### Source Code
    /// [!code-csharp[SampleCode](~/../Hi.Sample.Wpf/Machining/DemoMillingByCutterLocation.cs)]
    /// </remarks>
    public static class DemoMillingByCutterLocation
    {
        [STAThread]
        static void Main()
        {
            int h = 2;
            string stlFile_CylinderR20 = $"Cache/CylinderH{h}R20.stl";
            string stlFile_CylinderR19 = $"Cache/CylinderH{h}R19.stl";
            string stlFile_CylinderR18 = $"Cache/CylinderH{h}R18.stl";
            new Cylindroid(new PairZr(-h, 20), new PairZr(0, 20))
                .GenStl(new PolarResolution2d(1, MathUtil.ToRad(15))).WriteBin(stlFile_CylinderR20);
            new Cylindroid(new PairZr(-h, 19), new PairZr(0, 19))
                .GenStl(new PolarResolution2d(1, MathUtil.ToRad(15))).WriteBin(stlFile_CylinderR19);
            new Cylindroid(new PairZr(-h, 18), new PairZr(0, 18))
                .GenStl(new PolarResolution2d(1, MathUtil.ToRad(15))).WriteBin(stlFile_CylinderR18);

            LocalApp.AppBegin();

            LocalProjectService localProjectService = new LocalProjectService();

            #region ProjectLoading
            //var projectPath = "C:/HiNC-Projects/DemoStandardPath/Main.hincproj";
            var projectPath = "C:/HiNC-Projects/demo-test-1/Main.hincproj";
            Console.WriteLine($"Load Project: {projectPath}");
            localProjectService.LoadProject(projectPath).ShowIfCatched(null);
            var project = localProjectService.MachiningProject;
            #endregion

            var projectDisplayee = new MachiningProjectDisplayee(()=> localProjectService.MachiningProject);
            var device = new ClMillingDevice();
            project.MachiningEquipment.MachiningChain = device;
            project.ClStrip.IsShowDot = true;
            double resolution_mm = 0.5;
            var projectDir = Path.GetDirectoryName(projectPath);
            project.Workpiece.InitGeom = new StlFile(stlFile_CylinderR20, projectDir);
            project.Workpiece.IdealGeom = new StlFile(stlFile_CylinderR19, projectDir);
            project.Workpiece.InitResolution = resolution_mm;

            RuntimeApi runtimeApi = new RuntimeApi(localProjectService);
            runtimeApi.SetNcResolutionFixed(9999, 15);
            runtimeApi.EnableCollisionDetection = false;
            runtimeApi.EnablePhysics = false;
            runtimeApi.MachiningResolution_mm = resolution_mm;

            //RunConsole(project, projectDisplayee, resolution_mm);
            RunWindow(project, projectDisplayee, resolution_mm);
        }
        static void RunConsole(MachiningProject project,
            MachiningProjectDisplayee projectDisplayee, double resolution_mm)
        {
            RunSession(project, resolution_mm);
            DispEngine dispEngine = new DispEngine(projectDisplayee);
            dispEngine.SetViewToIsometricView();
            dispEngine.SketchView = project.ClStrip.GetFittingView(dispEngine.SketchView);
            dispEngine.SketchView = dispEngine.SketchView.Scale(0.5);
            dispEngine.Snapshot($"Cache/result.bmp", 1000, 1000);

            project.Dispose();
            LocalApp.AppEnd();
            Console.WriteLine($"App exit.");
        }
        static void RunWindow(MachiningProject project,
            MachiningProjectDisplayee projectDisplayee, double resolution_mm)
        {
            var task = Task.Run(() =>
            {
                RunSession(project, resolution_mm);
                Console.WriteLine($"task done.");
            }).ShowIfCatched(null);

            #region Create and Run WPF Application
            Application app = new Application
            {
                ShutdownMode = ShutdownMode.OnMainWindowClose
            };
            app.Exit += (o, e) =>
            {
                project.Dispose();
                LocalApp.AppEnd();
                Console.WriteLine($"App exit.");
            };
            app.Run(new RenderingWindow()
            {
                Title = "Demo",
                Displayee = projectDisplayee
            });
            #endregion
        }
        static void RunSession(MachiningProject project, double resolution_mm)
        {
            RuntimeApi runtimeApi = new RuntimeApi(project);

            project.BeginSession();
            double radius = 20;
            double z = -1;
            project.Act(new ActToolingTeleport(1));//equip tool
            project.Act(new ActClTeleport(new DVec3d(radius, 0, z, 0, 0, 1)));//goto initial position
                                                                              //run an arbitrary contour.
            int divNum = 36;
            for (int i = 0; i <= divNum; i++)
            {
                var cl = new DVec3d(
                    radius * Math.Cos(i * Math.PI * 2 / divNum),
                    radius * Math.Sin(i * Math.PI * 2 / divNum), z,
                    0, 0, 1);
                Console.WriteLine($"{cl.Point.CsvText},{cl.Normal.CsvText},");
                project.Act(new ActClStep(cl));
            }
            double diffLimit_mm = resolution_mm * 2;
            runtimeApi.Diff(resolution_mm * 2);
            Console.WriteLine($"IsDifferenceAceptable: {IsDifferenceAceptable(project, diffLimit_mm)}");
            project.EndSession();
        }
        static bool IsDifferenceAceptable(MachiningProject project, double diffLimit)
        {
            foreach (var attachment in project.Workpiece.DiffAttachmentBag)
            {
                if (double.IsNaN(attachment.Diff) || Math.Abs(attachment.Diff) > diffLimit)
                {
                    Console.WriteLine($"Diff: {attachment.Diff}");
                    return false;
                }
            }
            return true;
        }
    }
}