HiNC-2025-win-desktop-Deploy/App/wwwroot/HiAPI-docsite/brief/sample/Sample.Machining.DemoMillingByCutterLocation.html
2025-10-18 10:01:54 +08:00

327 lines
13 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Class DemoMillingByCutterLocation | HiAPI-C# 2025 </title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="title" content="Class DemoMillingByCutterLocation | HiAPI-C# 2025 ">
<link rel="icon" href="../../img/HiAPI.favicon.ico">
<link rel="stylesheet" href="../../public/docfx.min.css">
<link rel="stylesheet" href="../../public/main.css">
<meta name="docfx:navrel" content="../../toc.html">
<meta name="docfx:tocrel" content="toc.html">
<meta name="docfx:rel" content="../../">
<meta name="loc:inThisArticle" content="In this article">
<meta name="loc:searchResultsCount" content="{count} results for &quot;{query}&quot;">
<meta name="loc:searchNoResults" content="No results for &quot;{query}&quot;">
<meta name="loc:tocFilter" content="Filter by title">
<meta name="loc:nextArticle" content="Next">
<meta name="loc:prevArticle" content="Previous">
<meta name="loc:themeLight" content="Light">
<meta name="loc:themeDark" content="Dark">
<meta name="loc:themeAuto" content="Auto">
<meta name="loc:changeTheme" content="Change theme">
<meta name="loc:copy" content="Copy">
<meta name="loc:downloadPdf" content="Download PDF">
<script type="module" src="./../../public/docfx.min.js"></script>
<script>
const theme = localStorage.getItem('theme') || 'auto'
document.documentElement.setAttribute('data-bs-theme', theme === 'auto' ? (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light') : theme)
</script>
</head>
<body class="tex2jax_ignore" data-layout="" data-yaml-mime="ManagedReference">
<header class="bg-body border-bottom">
<nav id="autocollapse" class="navbar navbar-expand-md" role="navigation">
<div class="container-xxl flex-nowrap">
<a class="navbar-brand" href="../../index.html">
<img id="logo" class="svg" src="../../img/HiAPI.logo.png" alt="">
</a>
<button class="btn btn-lg d-md-none border-0" type="button" data-bs-toggle="collapse" data-bs-target="#navpanel" aria-controls="navpanel" aria-expanded="false" aria-label="Toggle navigation">
<i class="bi bi-three-dots"></i>
</button>
<div class="collapse navbar-collapse" id="navpanel">
<div id="navbar">
<form class="search" role="search" id="search">
<i class="bi bi-search"></i>
<input class="form-control" id="search-query" type="search" disabled placeholder="Search" autocomplete="off" aria-label="Search">
</form>
</div>
</div>
</div>
</nav>
</header>
<main class="container-xxl">
<div class="toc-offcanvas">
<div class="offcanvas-md offcanvas-start" tabindex="-1" id="tocOffcanvas" aria-labelledby="tocOffcanvasLabel">
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="tocOffcanvasLabel">Table of Contents</h5>
<button type="button" class="btn-close" data-bs-dismiss="offcanvas" data-bs-target="#tocOffcanvas" aria-label="Close"></button>
</div>
<div class="offcanvas-body">
<nav class="toc" id="toc"></nav>
</div>
</div>
</div>
<div class="content">
<div class="actionbar">
<button class="btn btn-lg border-0 d-md-none" type="button" data-bs-toggle="offcanvas" data-bs-target="#tocOffcanvas" aria-controls="tocOffcanvas" aria-expanded="false" aria-label="Show table of contents">
<i class="bi bi-list"></i>
</button>
<nav id="breadcrumb"></nav>
</div>
<article data-uid="Sample.Machining.DemoMillingByCutterLocation">
<h1 id="Sample_Machining_DemoMillingByCutterLocation" data-uid="Sample.Machining.DemoMillingByCutterLocation" class="text-break">
Class DemoMillingByCutterLocation
</h1>
<div class="facts text-secondary">
<dl><dt>Namespace</dt><dd><a class="xref" href="Sample.html">Sample</a>.<a class="xref" href="Sample.Machining.html">Machining</a></dd></dl>
<dl><dt>Assembly</dt><dd>Hi.Sample.Wpf.dll</dd></dl>
</div>
<div class="markdown summary"></div>
<div class="markdown conceptual"></div>
<div class="codewrapper">
<pre><code class="lang-csharp hljs">public static class DemoMillingByCutterLocation</code></pre>
</div>
<dl class="typelist inheritance">
<dt>Inheritance</dt>
<dd>
<div><a class="xref" href="https://learn.microsoft.com/dotnet/api/system.object">object</a></div>
<div><span class="xref">DemoMillingByCutterLocation</span></div>
</dd>
</dl>
<dl class="typelist inheritedMembers">
<dt>Inherited Members</dt>
<dd>
<div>
<a class="xref" href="https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object)">object.Equals(object)</a>
</div>
<div>
<a class="xref" href="https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object-system-object)">object.Equals(object, object)</a>
</div>
<div>
<a class="xref" href="https://learn.microsoft.com/dotnet/api/system.object.gethashcode">object.GetHashCode()</a>
</div>
<div>
<a class="xref" href="https://learn.microsoft.com/dotnet/api/system.object.gettype">object.GetType()</a>
</div>
<div>
<a class="xref" href="https://learn.microsoft.com/dotnet/api/system.object.memberwiseclone">object.MemberwiseClone()</a>
</div>
<div>
<a class="xref" href="https://learn.microsoft.com/dotnet/api/system.object.referenceequals">object.ReferenceEquals(object, object)</a>
</div>
<div>
<a class="xref" href="https://learn.microsoft.com/dotnet/api/system.object.tostring">object.ToString()</a>
</div>
</dd></dl>
<h2 id="Sample_Machining_DemoMillingByCutterLocation_remarks">Remarks</h2>
<div class="markdown level0 remarks"><h3 id="source-code">Source Code</h3>
<pre><code class="lang-csharp" name="SampleCode">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
{
/// &lt;remarks&gt;
/// ### Source Code
/// [!code-csharp[SampleCode](~/../Hi.Sample.Wpf/Machining/DemoMillingByCutterLocation.cs)]
/// &lt;/remarks&gt;
public static class DemoMillingByCutterLocation
{
[STAThread]
static void Main()
{
int h = 2;
string stlFile_CylinderR20 = $&quot;Cache/CylinderH{h}R20.stl&quot;;
string stlFile_CylinderR19 = $&quot;Cache/CylinderH{h}R19.stl&quot;;
string stlFile_CylinderR18 = $&quot;Cache/CylinderH{h}R18.stl&quot;;
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 = &quot;C:/HiNC-Projects/DemoStandardPath/Main.hincproj&quot;;
var projectPath = &quot;C:/HiNC-Projects/demo-test-1/Main.hincproj&quot;;
Console.WriteLine($&quot;Load Project: {projectPath}&quot;);
localProjectService.LoadProject(projectPath).ShowIfCatched(null);
var project = localProjectService.MachiningProject;
#endregion
var projectDisplayee = new MachiningProjectDisplayee(()=&gt; 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 = project.RuntimeApi;
runtimeApi.SetNcResolutionFixed(9999, 15);
runtimeApi.EnableCollisionDetection = false;
runtimeApi.EnablePhysics = false;
runtimeApi.MachiningResolution_mm = resolution_mm;
//RunConsole(project, projectDisplayee, resolution_mm);
RunWindow(localProjectService, projectDisplayee, resolution_mm);
}
static void RunConsole(IProjectService projectService,
MachiningProjectDisplayee projectDisplayee, double resolution_mm)
{
RunSession(projectService, resolution_mm);
DispEngine dispEngine = new DispEngine(projectDisplayee);
dispEngine.SetViewToIsometricView();
dispEngine.SketchView = projectService.MachiningProject.ClStrip.GetFittingView(dispEngine.SketchView);
dispEngine.SketchView = dispEngine.SketchView.Scale(0.5);
dispEngine.Snapshot($&quot;Cache/result.bmp&quot;, 1000, 1000);
projectService.GetLocalProjectService().CloseProject();
LocalApp.AppEnd();
Console.WriteLine($&quot;App exit.&quot;);
}
static void RunWindow(IProjectService projectService,
MachiningProjectDisplayee projectDisplayee, double resolution_mm)
{
var task = Task.Run(() =&gt;
{
RunSession(projectService, resolution_mm);
Console.WriteLine($&quot;task done.&quot;);
}).ShowIfCatched(null);
#region Create and Run WPF Application
Application app = new Application
{
ShutdownMode = ShutdownMode.OnMainWindowClose
};
app.Exit += (o, e) =&gt;
{
projectService.GetLocalProjectService().CloseProject();
LocalApp.AppEnd();
Console.WriteLine($&quot;App exit.&quot;);
};
app.Run(new RenderingWindow()
{
Title = &quot;Demo&quot;,
Displayee = projectDisplayee
});
#endregion
}
static void RunSession(IProjectService projectService, double resolution_mm)
{
var project=projectService.MachiningProject;
RuntimeApi runtimeApi = project.RuntimeApi;
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 &lt;= 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($&quot;{cl.Point.CsvText},{cl.Normal.CsvText},&quot;);
project.Act(new ActClStep(cl));
}
double diffLimit_mm = resolution_mm * 2;
runtimeApi.Diff(resolution_mm * 2);
Console.WriteLine($&quot;IsDifferenceAceptable: {IsDifferenceAceptable(project, diffLimit_mm)}&quot;);
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) &gt; diffLimit)
{
Console.WriteLine($&quot;Diff: {attachment.Diff}&quot;);
return false;
}
}
return true;
}
}
}
</code></pre></div>
</article>
<div class="contribution d-print-none">
</div>
</div>
<div class="affix">
<nav id="affix"></nav>
</div>
</main>
<div class="container-xxl search-results" id="search-results"></div>
<footer class="border-top text-secondary">
<div class="container-xxl">
<div class="flex-fill">
<span> Copyright © 2025 <a href='https://superhightech.com.tw'>Tech Coordinate</a>. All rights reserved. <a href='https://superhightech.com.tw'>超級高科技股份有限公司</a> © 2025 版權所有 </span>
</div>
</div>
</footer>
</body>
</html>