Skip to content

G-code remapping

A remap teaches the LinuxCNC interpreter what to do when it reads a particular G-code or M-code.

For example, a machine configuration could define M400 P25 as a custom operation. When LinuxCNC reads that block, it validates the P word and runs the procedure assigned to M400. The part program uses an ordinary-looking M-code; the machine configuration owns its meaning.

Without a remap, the program would have to call that procedure directly with O-word syntax. Remapping gives it a normal G-code or M-code interface and lets the interpreter check its arguments.

A remap can:

  • give an unallocated G-code or M-code a new meaning; or
  • replace the built-in behavior of one of the existing codes LinuxCNC permits you to redefine.

LinuxCNC can run an NGC procedure or a Python function as the body of a remap. Start with an NGC procedure when normal G-code, O-word flow control, and parameters are enough.

What the code needs to do Start with
Run moves, set outputs, call other O-word procedures, or use simple conditions A simple NGC remap
Inspect interpreter state or implement logic that is awkward in NGC A simple Python remap
Ask machine logic to start an operation and wait for feedback A Python machine-action remap

An NGC remap needs no Python files or [PYTHON] section. Add the Python configuration only when the remap uses python=, prolog=, or epilog=.

Each remapped code has one REMAP entry in the machine’s INI file:

[RS274NGC]
REMAP = M400 modalgroup=10 argspec=Pq ngc=m400

Read it from left to right:

Part Meaning
M400 The code being defined.
modalgroup=10 Where this M-code runs relative to other words in the same block.
argspec=Pq P is required and Q is optional.
ngc=m400 Run the O-word procedure in m400.ngc. The .ngc extension is omitted here.

The body can instead be a Python function:

REMAP = M400 modalgroup=10 argspec=Pq python=m400

Choose either ngc= or python= for the body. LinuxCNC rejects a REMAP entry that uses both or neither.

Start with an unallocated code. Remapping an existing code replaces its built-in behavior. Check Code availability before choosing one.