Table of Contents

Class NcOptMixedPatchConverter

Namespace
Hi.NcParsers.NcWriteback
Assembly
HiMech.dll

Mixed-mode INcPieceConverter for the NC-optimization output path: per-piece dispatch between the 1:1 patch semantics of NcPatchWriter (whose per-piece logic it reuses through ApplyRegisteredEdits(SyntaxPiece, NcDiagnosticProgress, ref HashSet<int>) — the public NcPatchWriter 1:1 contract stays untouched) and the 1:n splition fragment rewrite: a source piece whose SentenceIndex is keyed in SplitionPlansBySentenceIndex emits one destination piece per EMITTED fragment of its NcOptSplitionPlan, each produced by replaying the plan's recorded text operations onto a fresh copy of the source BlockText.

public class NcOptMixedPatchConverter : INcPieceConverter
Inheritance
NcOptMixedPatchConverter
Implements
Inherited Members
Extension Methods

Remarks

The fragment rewrite is the SoftNc dual of the HardNc oracle's splition output branch (frozen baseline, HiNc/NcOpt/NcOptProc.cs lines 2165-2401): every fragment starts from the source line text (oracle line 2214) and the operations are applied in the oracle's order — feedrate write/delete (lines 2223-2228), endpoint coordinate writes followed by the leading-whitespace trim and end-of-line comment relocation that close RebuildPosSyntaxToNcLine (lines 1913-1964 via 2313; see TrimStartRelocatingComment(string, Func<string, IReadOnlyList<(int Start, int Length)>>)), modal-token and comment strip for non-first emitted fragments (RebuildFlagSyntaxToNcLine lines 1965-1993 via 2323, comment removal lines 2325-2327), the file-first-feedrate write (lines 2331-2342), the arc R→IJK conversion and per-fragment I/J/K writes (lines 2346-2383), and finally the embedded-log note (lines 2385-2397). All decisions (values, tolerances, filters) were made by the plan builder (SoftNcOptProc); this converter is deliberately dumb.

Deliberate divergences from the oracle, all saner-than-oracle and documented at their sources: comment spans protect every edit (the oracle edits raw text and can bite comment content), token deletion keeps both separator spaces exactly like the oracle (absorbSeparatorSpaceOnRemove: false — see SetTagNumber(ref string, string, double, string, string, IReadOnlyList<(int Start, int Length)>, IReadOnlyList<string>, bool, bool)), and destination sentence indices are allocated only for emitted fragments (the oracle burns an index on every pre-built fragment, dropped ones included — invisible in the output text; see NcOptSplitionFragmentPlan).

Constructors

NcOptMixedPatchConverter(NcPatchWriter, Dictionary<int, NcOptSplitionPlan>)

Creates the mixed converter over an existing patch writer (which carries the brand configuration and the registered 1:1 F edits) and an optional splition plan map.

public NcOptMixedPatchConverter(NcPatchWriter patchWriter, Dictionary<int, NcOptSplitionPlan> splitionPlansBySentenceIndex = null)

Parameters

patchWriter NcPatchWriter

The patch writer whose per-piece semantics (and EditsBySentenceIndex) apply to every non-splition piece.

splitionPlansBySentenceIndex Dictionary<int, NcOptSplitionPlan>

Splition plans keyed by source SentenceIndex; null starts empty.

Properties

PatchWriter

The wrapped patch writer: brand configuration (variable prefix, comment spans, keyword values, formats) and the 1:1 edit map used for non-splition pieces.

public NcPatchWriter PatchWriter { get; }

Property Value

NcPatchWriter

SplitionPlansBySentenceIndex

Splition rewrite plans keyed by source SentenceIndex. A keyed piece emits one destination piece per emitted fragment; an unhit key is reported by the same stream-drain that covers unmatched patch edits (Writeback-Splition–PlanUnmatched).

public Dictionary<int, NcOptSplitionPlan> SplitionPlansBySentenceIndex { get; }

Property Value

Dictionary<int, NcOptSplitionPlan>

TargetBrand

Target dialect token; one of the CncBrandDependency brand constants.

public string TargetBrand { get; }

Property Value

string

Methods

ComposeFragmentText(string, NcOptSplitionPlan, NcOptSplitionFragmentPlan, SyntaxPiece, NcDiagnosticProgress)

Replays one fragment's planned text operations onto a copy of the source block text, in the oracle's operation order (see class remarks). Comment spans are recomputed before every splice.

public string ComposeFragmentText(string srcBlockText, NcOptSplitionPlan plan, NcOptSplitionFragmentPlan fragment, SyntaxPiece piece, NcDiagnosticProgress conversionProgress)

Parameters

srcBlockText string

The source piece's verbatim block text.

plan NcOptSplitionPlan

The pack-level plan (strip token list).

fragment NcOptSplitionFragmentPlan

The fragment spec to apply.

piece SyntaxPiece

The source piece anchoring refusal diagnostics.

conversionProgress NcDiagnosticProgress

Diagnostic sink; may be null.

Returns

string

The fragment's NC line text.

Convert(IEnumerable<SyntaxPiece>, NcDstPieceFactory, NcDiagnosticProgress)

Converts the executed source piece stream into the destination piece stream plus the src↔dst map. Pieces must come from a single source file, in execution order. Problems are reported through conversionProgress with the source piece as the sentence anchor.

public NcConversion Convert(IEnumerable<SyntaxPiece> syntaxPieces, NcDstPieceFactory dstPieceFactory, NcDiagnosticProgress conversionProgress)

Parameters

syntaxPieces IEnumerable<SyntaxPiece>

Finished source pieces (final pipeline layer), execution order.

dstPieceFactory NcDstPieceFactory

Factory stamping identity (file path / file index / fresh SentenceIndex) onto the destination pieces.

conversionProgress NcDiagnosticProgress

Diagnostic sink for the manipulation scenario (conversion/optimization) — keep it separate from the NC play diagnostic home; may be null.

Returns

NcConversion

The destination stream and mapping; never null.

RemoveCommentText(string, Func<string, IReadOnlyList<(int Start, int Length)>>)

Removes the comment text (the brand's comment spans) from blockText, keeping every non-comment byte — including any separator whitespace around the spans, mirroring the oracle's GrabComment-based removal (the space before a trailing (comment) survives as a trailing space). Shared with the plan builder's simple-feedrate-assignability probe.

public static string RemoveCommentText(string blockText, Func<string, IReadOnlyList<(int Start, int Length)>> commentSpanProvider)

Parameters

blockText string

The block text; null counts as empty.

commentSpanProvider Func<string, IReadOnlyList<(int Start, int Length)>>

The brand comment-span provider (see CommentSpanProvider); may be null.

Returns

string

StripModalTokensAndComment(string, IReadOnlyList<string>, Func<string, IReadOnlyList<(int Start, int Length)>>)

Applies the strip operations of a non-first emitted fragment (modal tokens, then comment) to a copy of the source text — the text state the oracle's per-fragment IsSimpleFeedrateAssignableNcLine probe sees (lines 2331-2342 run after lines 2323-2327). Used by the plan builder to evaluate the file-first-feedrate gate without emitting anything.

public static string StripModalTokensAndComment(string srcBlockText, IReadOnlyList<string> stripTokens, Func<string, IReadOnlyList<(int Start, int Length)>> commentSpanProvider)

Parameters

srcBlockText string

The source block text; null counts as empty.

stripTokens IReadOnlyList<string>

The pack's modal strip tokens.

commentSpanProvider Func<string, IReadOnlyList<(int Start, int Length)>>

The brand comment-span provider.

Returns

string

TrimStartRelocatingComment(string, Func<string, IReadOnlyList<(int Start, int Length)>>)

Rebuilds a fragment draft the way the oracle's RebuildPosSyntaxToNcLine does (HiNc/NcOpt/NcOptProc.cs:1917, 1962-1963): the comment is cut out, the remaining code text is TrimStart()ed, and the comment is re-appended at end-of-line with no separator inserted. A line whose comment already trails the code and that carries no leading whitespace is returned unchanged.

public static string TrimStartRelocatingComment(string blockText, Func<string, IReadOnlyList<(int Start, int Length)>> commentSpanProvider)

Parameters

blockText string

The fragment draft; null counts as empty.

commentSpanProvider Func<string, IReadOnlyList<(int Start, int Length)>>

The brand comment-span provider (see CommentSpanProvider); may be null.

Returns

string

Remarks

The oracle re-appends HardNcComment.FullText, which re-spells the comment from its mark and content (HiUniNc/Numerical/HardNcComment.cs:34-52: (content), ;content, //content, %content) — byte-identical to the source span the brand comment-span provider reports, so re-appending the span text reproduces it without needing a mark taxonomy here. Multiple (or overlapping, Heidenhain-union) spans are re-appended in source order as one block; the oracle's GrabComment grabs a single comment per line, so the multi-span case is a SoftNc-only generalization.

Splition fragments only: the 1:1 patch path (ApplyRegisteredEdits(SyntaxPiece, NcDiagnosticProgress, ref HashSet<int>)) never calls this and keeps its byte-stability contract — a patched line differs from its source only inside the edited token's span.