Skip to content

KasHir/GCodeVisualizer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

G-code Visualizer for Unity

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.


Features

  • 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.

Requirements

  • 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.

How It Works

  1. Load a G-Code File:

    • Specify the path to your G-code file in the gcodeFilePath variable (default: Assets/gcode/test.cnc).
  2. 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.
  3. Visualize Toolpath:

    • The trajectory of the end mill is recorded and displayed using Unity's LineRenderer.
  4. Coordinate Conversion:

    • Converts between Unity's coordinate system (X, Y, Z) and G-code's coordinate system (X, Z, Y) with scaling.

Usage

  1. Clone this repository into your Unity project's Assets folder.
  2. Attach the EndmillMover script to a GameObject representing the end mill (e.g., a cylinder).
  3. Add a LineRenderer component to the same GameObject for toolpath visualization.
  4. Place your G-code file in the specified path (Assets/gcode/test.cnc by default).
  5. Play the scene in Unity to see the end mill move along the toolpath defined by the G-code.

Code Overview

Key Components

  • EndmillMover Script:

    • 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 LineRenderer for visualization.

Example

Sample G-Code Input (test.cnc)

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

Result in Unity

  • The cylinder moves along a defined path while leaving a visual trail representing its trajectory.

Customization

  • Scaling Factor: Adjust the SCALE variable 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 LineRenderer properties.

License

This project is licensed under the MIT License. See LICENSE for details.


Acknowledgments

Special thanks to contributors and libraries used in this project, including any external G-code parsers integrated into this implementation.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published