This repository contains a Unity project that reads G-code files, simulates the movement of an end mill (cylindrical cutting tool), and visualizes the toolpath generated by the G-code. The project is implemented using C# and Unity's built-in features like LineRenderer.
- G-code Parsing: Reads and interprets G-code commands to control the movement of the end mill.
- Toolpath Visualization: Displays the trajectory of the end mill using Unity's
LineRenderer. - Coordinate Transformation: Converts between G-code coordinates and Unity's coordinate system.
- Modal State Management: Handles G-code modal states, including absolute/relative positioning, plane selection, and feed rate adjustments.
- Linear and Arc Motion Support: Simulates both linear (G0, G1) and arc (G2, G3) movements.
- Unity Version: Unity 6 or later
- Dependencies:
- A G-code parser library (
GenericGCodeParser) is used to interpret G-code files. Ensure it is included in your project.
- A G-code parser library (
-
Load a G-Code File:
- Specify the path to your G-code file in the
gcodeFilePathvariable (default:Assets/gcode/test.cnc).
- Specify the path to your G-code file in the
-
Simulate End Mill Movement:
- The script reads the G-code file line by line and moves a cylindrical object in Unity according to the parsed commands.
-
Visualize Toolpath:
- The trajectory of the end mill is recorded and displayed using Unity's
LineRenderer.
- The trajectory of the end mill is recorded and displayed using Unity's
-
Coordinate Conversion:
- Converts between Unity's coordinate system (X, Y, Z) and G-code's coordinate system (X, Z, Y) with scaling.
- Clone this repository into your Unity project's
Assetsfolder. - Attach the
EndmillMoverscript to a GameObject representing the end mill (e.g., a cylinder). - Add a
LineRenderercomponent to the same GameObject for toolpath visualization. - Place your G-code file in the specified path (
Assets/gcode/test.cncby default). - Play the scene in Unity to see the end mill move along the toolpath defined by the G-code.
-
EndmillMoverScript:- Core logic for parsing G-code, controlling object movement, and visualizing toolpaths.
-
Coordinate Conversion:
ConvertToUnityCoordinates: Converts G-code coordinates to Unity's coordinate system.ConvertToGCodeCoordinates: Converts Unity coordinates back to G-code format.
-
Motion Handlers:
HandleLinearMove: Simulates linear movements (G0, G1).HandleArcMove: Simulates circular arc movements (G2, G3).
-
Toolpath Recording:
- Positions are recorded during movement and passed to the
LineRendererfor visualization.
- Positions are recorded during movement and passed to the
G21 ; Set units to millimeters
G90 ; Absolute positioning
G0 X10 Y10 Z0 ; Rapid move
G1 X20 Y20 Z-5 F800 ; Linear move with feed rate
G2 X30 Y30 I10 J0 ; Clockwise arc- The cylinder moves along a defined path while leaving a visual trail representing its trajectory.
- Scaling Factor: Adjust the
SCALEvariable to modify how real-world units map to Unity units. - Feed Rate: Modify feed rate behavior by adjusting
lastFeedRate. - Visualization Style: Customize the appearance of the toolpath by tweaking
LineRendererproperties.
This project is licensed under the MIT License. See LICENSE for details.
Special thanks to contributors and libraries used in this project, including any external G-code parsers integrated into this implementation.