G51 is a G-code function that allows the CNC programmer to scale or mirror the coordinates of a part program. This can be useful for creating symmetrical or proportional shapes, or for adjusting the size of a part without changing the original program. G51 can also be used to flip the direction of a contour or a hole pattern.

## Usage format

The general format of G51 is:

`G51 X_ Y_ Z_ I_ J_ K_`

where:

- X, Y, Z are the scaling factors for the X, Y, and Z axes respectively. They can be positive or negative values. A value of 1 means no scaling, a value of -1 means mirroring, and a value of 0 means no movement along that axis.
- I, J, K are the coordinates of the scaling center point. They can be absolute or incremental values. If omitted, the scaling center point is assumed to be the current position of the tool.

G51 can be cancelled by using G50 or G92.1.

## Explanation

G51 works by multiplying the coordinates of the part program by the scaling factors specified by the programmer. For example, if the scaling factor for X is 0.5, then every X coordinate in the program will be halved. If the scaling factor for Y is -1, then every Y coordinate in the program will be mirrored along the Y axis.

The scaling center point determines the origin of the scaling or mirroring operation. For example, if the scaling center point is (10, 10, 0), then the point (10, 10, 0) will remain unchanged, while the point (20, 20, 0) will be scaled or mirrored to (15, 5, 0).

## Example

Suppose we have the following part program that draws a square with a side length of 20 units:

`G90 G00 X0 Y0 Z0`

`G01 X20 F100`

`G01 Y20`

`G01 X0`

`G01 Y0`

`G00 Z10`

If we want to scale the square by 50% and mirror it along the X axis, we can use G51 as follows:

`G90 G00 X0 Y0 Z0`

`G51 X-0.5 Y0.5 Z1 I10 J10 K0`

`G01 X20 F100`

`G01 Y20`

`G01 X0`

`G01 Y0`

`G50`

`G00 Z10`

The resulting shape will be a rectangle with dimensions of 10 x 20 units, flipped along the X axis. The text diagram below shows the original square (in blue) and the scaled and mirrored rectangle (in red):

```
Y
^
| +-----+ +-----+
| | | | |
| | | | |
| | | | |
| +-----+ +-----+
| 0 10 20 X
+------------------------>
```