409 lines
17 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Workflow: NC Optimization | HiAPI-C# 2025 </title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="title" content="Workflow: NC Optimization | 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="">
<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="Workflow-NcOptimization">
<h1 id="workflow-nc-optimization">Workflow: NC Optimization</h1>
<p>This workflow describes how to generate optimized NC files from a physics-based simulation. The optimizer adjusts feed rates to keep physical quantities (spindle power, torque, thermal stress, cutting force) within specified safety limits while maximizing machining efficiency.</p>
<pre><code class="lang-mermaid">flowchart TD
Prereq[&quot;Prerequisites&lt;br&gt;(simulation with physics,&lt;br&gt;cutting parameters)&quot;]
Config[&quot;Configure optimization options&quot;]
Simulate[&quot;Run simulation&quot;]
Output[&quot;Generate optimized NC files&quot;]
Verify[&quot;Verify optimization results&quot;]
Prereq --&gt; Config --&gt; Simulate --&gt; Output --&gt; Verify
</code></pre>
<h2 id="1-prerequisites">1. Prerequisites</h2>
<p>NC optimization requires a simulation environment with <strong>physics enabled</strong> and valid <strong>cutting parameters</strong>:</p>
<pre><code class="lang-csharp">EnablePhysics = true;
LoadCuttingParaByFile(&quot;Material.mp&quot;);
</code></pre>
<table>
<thead>
<tr>
<th>Prerequisite</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>Physics enabled</td>
<td><a class="xref" href="../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_EnablePhysics">EnablePhysics</a> must be <code>true</code></td>
</tr>
<tr>
<td>Cutting parameters</td>
<td>Workpiece must have loaded milling coefficients (see <a class="xref" href="force-training.html">Workflow: Milling Force Parameter Training</a>)</td>
</tr>
<tr>
<td>Valid tool definitions</td>
<td>Tool geometry, flute count, and material properties configured</td>
</tr>
</tbody>
</table>
<div class="NOTE">
<h5>Note</h5>
<p>Optimization is based on an ideal geometric model. If the workpiece is a casting or has installation errors, configure a conservatively larger workpiece geometry to prevent misidentification of cutting vs. non-cutting regions.</p>
</div>
<hr>
<h2 id="2-configure-optimization-options">2. Configure Optimization Options</h2>
<h3 id="feed-rate-control">Feed Rate Control</h3>
<table>
<thead>
<tr>
<th>Property</th>
<th>Description</th>
<th>Default</th>
</tr>
</thead>
<tbody>
<tr>
<td><a class="xref" href="../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptEnableFeedrate">OptEnableFeedrate</a></td>
<td>Enable sequential feed rate optimization</td>
<td><code>true</code></td>
</tr>
<tr>
<td><a class="xref" href="../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptEnableInterpolation">OptEnableInterpolation</a></td>
<td>Re-interpolation for smoother acceleration/deceleration</td>
<td></td>
</tr>
<tr>
<td><a class="xref" href="../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptRapidFeed_mmdmin">OptRapidFeed_mmdmin</a></td>
<td>Feed rate for non-cutting regions (mm/min)</td>
<td></td>
</tr>
<tr>
<td><a class="xref" href="../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptMinFeedrate_mmdmin">OptMinFeedrate_mmdmin</a></td>
<td>Minimum cutting-region feed rate (mm/min)</td>
<td></td>
</tr>
<tr>
<td><a class="xref" href="../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptMaxFeedrate_mmdmin">OptMaxFeedrate_mmdmin</a></td>
<td>Maximum cutting-region feed rate (mm/min)</td>
<td></td>
</tr>
<tr>
<td><a class="xref" href="../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptMaxAcceleration_mmds2">OptMaxAcceleration_mmds2</a></td>
<td>Acceleration/deceleration limit (mm/s²)</td>
<td></td>
</tr>
<tr>
<td><a class="xref" href="../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptFeedrateAssignmentRatio">OptFeedrateAssignmentRatio</a></td>
<td>Re-interpolation trigger threshold</td>
<td></td>
</tr>
</tbody>
</table>
<h3 id="extended-distance">Extended Distance</h3>
<table>
<thead>
<tr>
<th>Property</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><a class="xref" href="../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptExtendedPreDistance_mm">OptExtendedPreDistance_mm</a></td>
<td>Pre-distance for equivalent calculation of cutting regions (mm)</td>
</tr>
<tr>
<td><a class="xref" href="../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptExtendedPostDistance_mm">OptExtendedPostDistance_mm</a></td>
<td>Post-distance for equivalent calculation of cutting regions (mm)</td>
</tr>
</tbody>
</table>
<h3 id="safety-factors-physics-based-constraints">Safety Factors (Physics-Based Constraints)</h3>
<table>
<thead>
<tr>
<th>Property</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><a class="xref" href="../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptSpindlePowerSafetyFactor">OptSpindlePowerSafetyFactor</a></td>
<td>Spindle power safety factor (0 = ignore)</td>
</tr>
<tr>
<td><a class="xref" href="../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptSpindleTorqueSafetyFactor">OptSpindleTorqueSafetyFactor</a></td>
<td>Spindle torque safety factor (0 = ignore)</td>
</tr>
<tr>
<td><a class="xref" href="../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptThermalYieldSafetyFactor">OptThermalYieldSafetyFactor</a></td>
<td>Thermal yield safety factor (0 = ignore)</td>
</tr>
<tr>
<td><a class="xref" href="../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptPreferedForce_N">OptPreferedForce_N</a></td>
<td>Target cutting force (N)</td>
</tr>
</tbody>
</table>
<div class="NOTE">
<h5>Note</h5>
<p><strong>Target value</strong> = 100% / Safety factor. For example, a safety factor of 1.5 means the physical quantity targets ~67% of the limit.</p>
</div>
<h3 id="constraint-priority">Constraint Priority</h3>
<p>In cutting regions, constraints are applied in this order:</p>
<ol>
<li><strong>Direct feed rate constraints</strong> (min/max feed rate, min/max feed per tooth from tool settings)</li>
<li><strong>Acceleration/deceleration constraints</strong> (<code>OptMaxAcceleration_mmds2</code>)</li>
<li><strong>Physics-based constraints</strong> (spindle power, torque, thermal yield, preferred force)</li>
</ol>
<p>When constraints at the same priority conflict, the lowest feed rate is used.</p>
<h3 id="script-command-example">Script Command Example</h3>
<pre><code class="lang-csharp">OptEnableFeedrate = true;
OptEnableInterpolation = true;
OptRapidFeed_mmdmin = 4000;
OptMinFeedrate_mmdmin = 100;
OptMaxFeedrate_mmdmin = 4000;
OptMaxAcceleration_mmds2 = 10;
OptExtendedPreDistance_mm = 3;
OptExtendedPostDistance_mm = 2;
OptSpindlePowerSafetyFactor = 1.5;
OptSpindleTorqueSafetyFactor = 1.5;
OptThermalYieldSafetyFactor = 0;
OptPreferedForce_N = double.PositiveInfinity;
</code></pre>
<h3 id="xml-configuration-nc-code-inline">XML Configuration (NC Code Inline)</h3>
<p>Optimization settings can be embedded in NC code comments:</p>
<pre><code class="lang-nc">N0110 X-3.064 Y6.378 (;@OptMaxAcceleration_mmds2=10;)
N0150 G01 X-3.068 Y40.776 (;@OptMaxAcceleration_mmds2=100; OptMaxFeedrate_mmdmin=12000;)
</code></pre>
<hr>
<h2 id="3-run-simulation">3. Run Simulation</h2>
<p>Configuration can be interleaved between NC files. Settings apply to the files that follow:</p>
<pre><code class="lang-csharp">OptRapidFeed_mmdmin = 4000;
PlayNcFile(&quot;NC/file1.nc&quot;);
OptRapidFeed_mmdmin = 8000;
PlayNcFile(&quot;NC/file2.nc&quot;);
</code></pre>
<h3 id="excluding-lines-from-optimization">Excluding Lines from Optimization</h3>
<p>To preserve specific NC lines unchanged:</p>
<pre><code class="lang-nc">N0140 G03 X-2.66 Y38.193 I-103.796 J7.172 (;@Preserve();)
</code></pre>
<p>To exclude a range:</p>
<pre><code class="lang-nc">N0140 G03 X-2.66 Y38.193 (;@BeginPreserve();)
N0150 G01 X-3.068 Y40.776
N0160 X-3.555 Y43.338 (;@EndPreserve();)
</code></pre>
<div class="WARNING">
<h5>Warning</h5>
<p>Do not combine <a class="xref" href="../api/Hi.MachiningSteps.MachiningStep.html#Hi_MachiningSteps_MachiningStep_UpdateNcOptOption_">UpdateNcOptOption</a> inside the <a class="xref" href="../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_SessionStepBuilt">SessionStepBuilt</a> event with NC-embedded optimization commands. This may cause undefined behavior due to parallel computation.</p>
</div>
<hr>
<h2 id="4-generate-optimized-nc-files">4. Generate Optimized NC Files</h2>
<p><a class="xref" href="../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_OptimizeToFiles_">OptimizeToFiles</a> writes the optimized NC programs:</p>
<pre><code class="lang-csharp">OptimizeToFiles(&quot;Cache/Opt-[NcName]&quot;);
</code></pre>
<p>The <code>[NcName]</code> template is replaced with each input NC file name.</p>
<hr>
<h2 id="5-verify-optimization-results">5. Verify Optimization Results</h2>
<h3 id="optimization-logs">Optimization Logs</h3>
<p>Enable the per-step log to see which constraint limited each step:</p>
<pre><code class="lang-csharp">EnableIndividualStepAdjustmentLog = true;
</code></pre>
<p>The <code>.IndependentStepAdjustment.log</code> file records per-step calculations including:</p>
<ul>
<li><code>FrtByPreferedForce_mm</code> — feed per tooth from target force</li>
<li><code>FrtByYieldingStressRatio_mm</code> — feed per tooth from yielding stress</li>
<li><code>FrtBySpindleTorqueRatio_mm</code> — feed per tooth from spindle torque</li>
<li><code>FrtBySpindlePowerRatio_mm</code> — feed per tooth from spindle power</li>
<li><code>FrtByThermalYieldingRatio_mm</code> — feed per tooth from thermal yield</li>
</ul>
<h3 id="embedded-log-comments">Embedded Log Comments</h3>
<p>Control embedded log verbosity with <a class="xref" href="../api/Hi.MachiningProcs.RuntimeApi.html#Hi_MachiningProcs_RuntimeApi_EmbeddedLogMode_">EmbeddedLogMode</a>:</p>
<table>
<thead>
<tr>
<th>Mode</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>None</code></td>
<td>No log comments</td>
</tr>
<tr>
<td><code>SimpleLog</code></td>
<td><code>StepIndex</code> on re-interpolated lines; <code>LineNo</code> on last interpolated line per original line</td>
</tr>
<tr>
<td><code>FullLog</code></td>
<td><code>StepIndex</code> and <code>LineNo</code> on all lines</td>
</tr>
</tbody>
</table>
<p>Example output: <code>G01 X10.0 Y20.0 F500 (src(LineNo: 140, StepIndex: 256))</code></p>
<h3 id="tracking-individual-step-constraints">Tracking Individual Step Constraints</h3>
<p>To isolate which physical quantity limits each step, disable smoothing:</p>
<pre><code class="lang-csharp">OptMaxAcceleration_mmds2 = double.PositiveInfinity;
OptFeedrateAssignmentRatio = 0;
OptExtendedPreDistance_mm = 0;
OptExtendedPostDistance_mm = 0;
EnableIndividualStepAdjustmentLog = true;
EmbeddedLogMode = NcOptimizationEmbeddedLogMode.FullLog;
</code></pre>
<h3 id="post-optimization-simulation-differences">Post-Optimization Simulation Differences</h3>
<p>Optimized feed rates produce different interpolation points, causing:</p>
<ul>
<li>Different simulation mesh errors</li>
<li>Surface morphology changes at the surface roughness level (more pronounced at corners)</li>
</ul>
<p>Simulated physical quantities after optimization may be slightly above target values due to these differences.</p>
<div class="TIP">
<h5>Tip</h5>
<p>For abnormally low optimized feed rates at corners, refer to <a href="../manual/analysis/corner-behavior.html">Corner Feed Rate Optimization</a>.</p>
</div>
<hr>
<h2 id="tool-breakage-solutions">Tool Breakage Solutions</h2>
<p>If the simulation shows yielding stress ratio, max spindle torque ratio, or max spindle power ratio above 100%, consider:</p>
<ol>
<li>Modify the toolpath to reduce cutting width/depth</li>
<li>Use HiNC optimization to adjust feed rates, bringing these ratios below 100%</li>
</ol>
<p>For thermal edge chipping, reduce the spindle speed to allow heat dissipation.</p>
<hr>
<h2 id="complete-script-example">Complete Script Example</h2>
<pre><code class="lang-csharp">EnablePhysics = true;
LoadCuttingParaByFile(&quot;Material.mp&quot;);
OptEnableFeedrate = true;
OptEnableInterpolation = true;
OptRapidFeed_mmdmin = 4000;
OptMinFeedrate_mmdmin = 100;
OptMaxFeedrate_mmdmin = 4000;
OptMaxAcceleration_mmds2 = 10;
OptExtendedPreDistance_mm = 3;
OptExtendedPostDistance_mm = 2;
OptSpindlePowerSafetyFactor = 1.5;
OptSpindleTorqueSafetyFactor = 1.5;
OptThermalYieldSafetyFactor = 0;
OptPreferedForce_N = double.PositiveInfinity;
PlayNcFile(&quot;NC/file1.nc&quot;);
OptimizeToFiles(&quot;Cache/Opt-[NcName]&quot;);
WriteStepFiles(&quot;Output/[NcName].step.csv&quot;);
</code></pre>
<h2 id="see-also">See Also</h2>
<ul>
<li><a href="../manual/analysis/nc-optimization-principles.html">NC Optimization (Concepts)</a> — theory and objectives</li>
<li><a href="../manual/analysis/corner-behavior.html">Corner Feed Rate Optimization</a></li>
<li><a class="xref" href="force-training.html">Workflow: Milling Force Parameter Training</a> — prerequisite: training cutting parameters</li>
<li><a class="xref" href="basic-simulation.html">Workflow: Basic Machining Simulation</a> — basic simulation setup</li>
<li><a class="xref" href="../manual/runtime/runtime-api.html">Glossary: RuntimeApi Quick-Reference</a> — RuntimeApi quick-reference</li>
</ul>
</article>
<div class="contribution d-print-none">
</div>
<div class="next-article d-print-none border-top" id="nextArticle"></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>