Table of Contents

Workflow: NC Optimization

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.

flowchart TD
    Prereq["Prerequisites<br>(simulation with physics,<br>cutting parameters)"]
    Config["Configure optimization options"]
    Simulate["Run simulation"]
    Output["Generate optimized NC files"]
    Verify["Verify optimization results"]

    Prereq --> Config --> Simulate --> Output --> Verify

1. Prerequisites

NC optimization requires a simulation environment with physics enabled and valid cutting parameters:

EnablePhysics = true;
LoadCuttingParaByFile("Material.mp");
Prerequisite Description
Physics enabled EnablePhysics must be true
Cutting parameters Workpiece must have loaded milling coefficients (see Workflow: Milling Force Parameter Training)
Valid tool definitions Tool geometry, flute count, and material properties configured
Note

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.


2. Configure Optimization Options

Feed Rate Control

Property Description Default
OptEnableFeedrate Enable sequential feed rate optimization true
OptEnableInterpolation Re-interpolation for smoother acceleration/deceleration
OptRapidFeed_mmdmin Feed rate for non-cutting regions (mm/min)
OptMinFeedrate_mmdmin Minimum cutting-region feed rate (mm/min)
OptMaxFeedrate_mmdmin Maximum cutting-region feed rate (mm/min)
OptMaxAcceleration_mmds2 Acceleration/deceleration limit (mm/s²)
OptFeedrateAssignmentRatio Re-interpolation trigger threshold

Extended Distance

Property Description
OptExtendedPreDistance_mm Pre-distance for equivalent calculation of cutting regions (mm)
OptExtendedPostDistance_mm Post-distance for equivalent calculation of cutting regions (mm)

Safety Factors (Physics-Based Constraints)

Property Description
OptSpindlePowerSafetyFactor Spindle power safety factor (0 = ignore)
OptSpindleTorqueSafetyFactor Spindle torque safety factor (0 = ignore)
OptThermalYieldSafetyFactor Thermal yield safety factor (0 = ignore)
OptPreferedForce_N Target cutting force (N)
Note

Target value = 100% / Safety factor. For example, a safety factor of 1.5 means the physical quantity targets ~67% of the limit.

Constraint Priority

In cutting regions, constraints are applied in this order:

  1. Direct feed rate constraints (min/max feed rate, min/max feed per tooth from tool settings)
  2. Acceleration/deceleration constraints (OptMaxAcceleration_mmds2)
  3. Physics-based constraints (spindle power, torque, thermal yield, preferred force)

When constraints at the same priority conflict, the lowest feed rate is used.

Script Command Example

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;

XML Configuration (NC Code Inline)

Optimization settings can be embedded in NC code comments:

N0110 X-3.064 Y6.378 (;@OptMaxAcceleration_mmds2=10;)
N0150 G01 X-3.068 Y40.776 (;@OptMaxAcceleration_mmds2=100; OptMaxFeedrate_mmdmin=12000;)

3. Run Simulation

Configuration can be interleaved between NC files. Settings apply to the files that follow:

OptRapidFeed_mmdmin = 4000;
PlayNcFile("NC/file1.nc");

OptRapidFeed_mmdmin = 8000;
PlayNcFile("NC/file2.nc");

Excluding Lines from Optimization

To preserve specific NC lines unchanged:

N0140 G03 X-2.66 Y38.193 I-103.796 J7.172 (;@Preserve();)

To exclude a range:

N0140 G03 X-2.66 Y38.193 (;@BeginPreserve();)
N0150 G01 X-3.068 Y40.776
N0160 X-3.555 Y43.338 (;@EndPreserve();)
Warning

Do not combine UpdateNcOptOption inside the SessionStepBuilt event with NC-embedded optimization commands. This may cause undefined behavior due to parallel computation.


4. Generate Optimized NC Files

OptimizeToFiles writes the optimized NC programs:

OptimizeToFiles("Cache/Opt-[NcName]");

The [NcName] template is replaced with each input NC file name.


5. Verify Optimization Results

Optimization Logs

Enable the per-step log to see which constraint limited each step:

EnableIndividualStepAdjustmentLog = true;

The .IndependentStepAdjustment.log file records per-step calculations including:

  • FrtByPreferedForce_mm — feed per tooth from target force
  • FrtByYieldingStressRatio_mm — feed per tooth from yielding stress
  • FrtBySpindleTorqueRatio_mm — feed per tooth from spindle torque
  • FrtBySpindlePowerRatio_mm — feed per tooth from spindle power
  • FrtByThermalYieldingRatio_mm — feed per tooth from thermal yield

Embedded Log Comments

Control embedded log verbosity with EmbeddedLogMode:

Mode Description
None No log comments
SimpleLog StepIndex on re-interpolated lines; LineNo on last interpolated line per original line
FullLog StepIndex and LineNo on all lines

Example output: G01 X10.0 Y20.0 F500 (src(LineNo: 140, StepIndex: 256))

Tracking Individual Step Constraints

To isolate which physical quantity limits each step, disable smoothing:

OptMaxAcceleration_mmds2 = double.PositiveInfinity;
OptFeedrateAssignmentRatio = 0;
OptExtendedPreDistance_mm = 0;
OptExtendedPostDistance_mm = 0;
EnableIndividualStepAdjustmentLog = true;
EmbeddedLogMode = NcOptimizationEmbeddedLogMode.FullLog;

Post-Optimization Simulation Differences

Optimized feed rates produce different interpolation points, causing:

  • Different simulation mesh errors
  • Surface morphology changes at the surface roughness level (more pronounced at corners)

Simulated physical quantities after optimization may be slightly above target values due to these differences.

Tip

For abnormally low optimized feed rates at corners, refer to Corner Feed Rate Optimization.


Tool Breakage Solutions

If the simulation shows yielding stress ratio, max spindle torque ratio, or max spindle power ratio above 100%, consider:

  1. Modify the toolpath to reduce cutting width/depth
  2. Use HiNC optimization to adjust feed rates, bringing these ratios below 100%

For thermal edge chipping, reduce the spindle speed to allow heat dissipation.


Complete Script Example

EnablePhysics = true;
LoadCuttingParaByFile("Material.mp");

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("NC/file1.nc");

OptimizeToFiles("Cache/Opt-[NcName]");
WriteStepFiles("Output/[NcName].step.csv");

See Also