# Bulge G-Code Builder ๐ ๏ธ

[](https://opensource.org/licenses/MIT)
A high-performance library for converting geometry defined as Polylines (with **Bulge** parameters) into CNC-compatible G-Code. Written in Rust and available for WebAssembly.
## โ Why Bulge?
In CAD systems like **AutoCAD (DXF)**, arcs are often stored not as centers and radii, but as a **Bulge** value on a polyline vertex.
- Calculating the center, radius, and start/end angles manually is tedious and error-prone.
- **This library handles all the math for you.** It converts Bulge-defined segments directly into standard `G02/G03` (CW/CCW) circular interpolation commands, saving you hours of geometry implementation.
## ๐ Installation
### For Rust Projects
Add this to your `Cargo.toml`:
```toml
[dependencies]
bulge_gcode = { git = "https://github.com/sagittaracc/bulge-gcode" }
```
### For Web / Node.js (WASM)
Download the latest archive from the **[Releases](https://github.com/sagittaracc/bulge-gcode/releases)** page.
```javascript
import init, { generate_gcode } from './pkg/web/bulge_gcode.js';
```
## ๐ป Usage
### Rust Example
```rust
use bulge_gcode::{Vertex, Polyline, GCodeBuilder, CoordinateMode};
fn main() {
let mut pline = Polyline::new();
// Circle centered at (0,0) with radius 5.0
// Parameters: x, y, bulge
// First vertex: upper point of the circle
// Bulge -1.0 creates a clockwise semicircle
pline.add_vertex(Vertex::new(0.0, 5.0, -1.0));
// Second vertex: lower point of the circle
// Bulge -1.0 completes the clockwise circle
pline.add_vertex(Vertex::new(0.0, -5.0, -1.0));
// Ensure the polyline closes back to the first vertex
pline.closed = true;
let builder = GCodeBuilder::new(pline)
.with_mode(CoordinateMode::Absolute);
match builder.build() {
Some(code) => println!("{}", code),
None => println!(""),
}
}
```
### WASM / JavaScript Example
```javascript
// Data format: [x, y, bulge, x, y, bulge, ...]
const data = new Float64Array([0, 5, -1, 0, -5, -1]);
const places = 3; // Decimal places
const scale = 1.0; // Multiplier (e.g., 1000 for microns)
const gcode = generate_gcode(data, true, true, places, scale);
console.log(gcode);
```
## ๐ Features
- **Arc Support**: Full support for circular interpolation via Bulge.
- **Precision Control**: Configurable decimal places and scaling (microns/mm/inches).
- **Dialects**: Support for both `G0/G1` and `G00/G01` command styles.
- **Cross-Platform**: Works in native Rust, Browser, and Node.js.
## ๐งช Development
Run tests: `cargo test`
Build all WASM targets: `./build.sh`
## ๐ References
This project implements geometric algorithms based on the following resources:
* **[Lee Mac: Bulge Conversion Functions](https://www.lee-mac.com/bulgeconversion.html)** โ A comprehensive guide to working with Bulge geometry in CAD systems.
* **[DXF Reference - Polyline Bulge](https://www.autodesk.com)** โ Official Autodesk documentation on the Bulge parameter.
## โ๏ธ License
This project is licensed under either of:
* **[MIT License](LICENSE-MIT)** (see [LICENSE-MIT](LICENSE-MIT))
* **[Apache License, Version 2.0](LICENSE-APACHE)** (see [LICENSE-APACHE](LICENSE-APACHE))
at your option.
---
*Copyright ยฉ 2024-present, Yuriy Arutyunyan*