mattercontrol/research/06-slicing-system.md

208 lines
6.6 KiB
Markdown

# Slicing System
## Summary
MatterControl integrates the **MatterSlice** slicing engine to convert 3D models into G-code instructions for printing. The system maps printer settings to slicer parameters, handles multi-extruder configurations, and supports features like rafts, supports, and custom start/end G-code.
## Technical Description
### Architecture Overview
```
PrinterConfig.Settings
EngineMappingsMatterSlice (IObjectSlicer)
├── Settings Mapping (Exports dictionary)
MatterSlice Engine
G-code Output File
```
### Slicer Static Class
The `Slicer` static class provides helper methods:
**Key Methods:**
| Method | Purpose |
|--------|---------|
| `SliceItem()` | Entry point for slicing objects |
| `GetExtrudersUsed()` | Determine which extruders are needed |
| `GetSolidsForExtruder()` | Filter objects by extruder assignment |
| `GetAllHoles()` | Find hole/negative objects |
| `T1OrGreaterUsed()` | Check if multi-extruder print |
**Configuration:**
- `RunInProcess` - Run slicer in-process (debug) vs subprocess
### EngineMappingsMatterSlice
Implements `IObjectSlicer` interface to translate MatterControl settings to MatterSlice parameters:
**Export Mapping:**
```csharp
Exports = new Dictionary<string, ExportField>()
{
[SettingsKey.layer_height] = new ExportField("layerThickness"),
[SettingsKey.perimeters] = new ExportField("numberOfPerimeters"),
[SettingsKey.fill_density] = new ExportField("infillPercent", transformer),
// ...
};
```
**Mapping Categories:**
| Category | Sample Settings |
|----------|-----------------|
| Layer | `layer_height`, `first_layer_height` |
| Perimeters | `perimeters`, `external_perimeter_speed` |
| Infill | `fill_density`, `infill_type`, `fill_angle` |
| Speed | `travel_speed`, `perimeter_speed`, `infill_speed` |
| Retraction | `retract_length`, `retract_speed`, `retract_lift` |
| Support | `support_material_spacing`, `support_air_gap` |
| Raft/Brim | `create_raft`, `raft_air_gap`, `skirts` |
| Fan | `min_fan_speed`, `max_fan_speed`, `bridge_fan_speed` |
| Extruder | `filament_diameter`, `nozzle_diameter`, `extrusion_multiplier` |
### Export Field Transformers
Some settings require transformation:
```csharp
// Percentage to decimal conversion
[SettingsKey.fill_density] = new ExportField(
"infillPercent",
(value, settings) => {
if (double.TryParse(value, out double infillRatio))
return $"{infillRatio * 100}";
return "0";
})
// Start G-code processing
[SettingsKey.start_gcode] = new ExportField(
"startCode",
(value, settings) => StartGCodeGenerator.BuildStartGCode(settings, value))
```
### Multi-Extruder Support
Extruder assignment based on `MaterialIndex`:
```csharp
var itemsThisExtruder = meshItemsOnBuildPlate.Where((item) => {
var material = item.WorldMaterialIndex();
return material == extruderIndex
|| (extruderIndex == 0 && material >= extruderCount);
});
```
**Extruder-Specific Settings:**
- `support_material_extruder`
- `support_material_interface_extruder`
- `raft_extruder`
- `brim_extruder`
### Print Output Types
Objects can have different output behaviors:
| Type | Description |
|------|-------------|
| `Default` | Normal solid printing |
| `Solid` | Explicitly solid |
| `Support` | Support material |
| `Hole` | Negative/subtraction |
| `Fuzzy` | Fuzzy skin effect |
### Slicing Process
1. **Collect Printable Items**: Filter scene for printable objects
2. **Determine Extruders**: Calculate which extruders are needed
3. **Map Settings**: Convert settings to slicer parameters
4. **Run Slicer**: Execute MatterSlice engine
5. **Progress Reporting**: Update UI with slice progress
6. **Output G-code**: Write to file
### MatterSlice Integration
MatterSlice is included as a submodule:
- `Submodules/MatterSlice/MatterSlice.csproj`
- `Submodules/MatterSlice/MatterSliceLib/MatterSliceLib.csproj`
**Key Settings Files:**
- `StaticData/SliceSettings/Layouts.txt` - UI layout definitions
- Settings JSON files in printer profiles
### Slicer Settings UI
Settings are organized via `SliceSettingsWidget`:
```
SliceSettingsWidget
├── Category tabs (General, Infill, Support, etc.)
├── Settings rows (SliceSettingsRow)
└── Preset selector (PresetSelectorWidget)
```
### Design Rationale
**Integrated Slicer**: Bundling MatterSlice provides:
- Seamless user experience
- Direct control over slicing behavior
- No external dependencies to manage
- Consistent cross-platform behavior
**Export Field Pattern**: The mapping dictionary allows:
- Declarative settings mapping
- Custom transformers where needed
- Easy addition of new settings
- Clear documentation of mappings
**Extruder Detection**: Pre-calculating used extruders enables:
- Appropriate temperature settings
- Correct tool change G-code
- Optimized print preparation
## Reference
### Core Classes
| Class | Location | Description |
|-------|----------|-------------|
| `Slicer` | [SlicerConfiguration/Slicer.cs](MatterControlLib/SlicerConfiguration/Slicer.cs) | Static slicer helpers |
| `EngineMappingsMatterSlice` | [SlicerConfiguration/EngineMappingsMatterSlice.cs](MatterControlLib/SlicerConfiguration/EngineMappingsMatterSlice.cs) | Settings mapping |
| `ExportField` | [MatterControl.Printing/ExportField.cs](MatterControl.Printing/ExportField.cs) | Field definition |
| `SliceSettingsWidget` | [SlicerConfiguration/SliceSettingsWidget.cs](MatterControlLib/SlicerConfiguration/SliceSettingsWidget.cs) | Settings UI |
### UI Components
| Class | Location | Description |
|-------|----------|-------------|
| `SliceSettingsRow` | [SlicerConfiguration/SliceSettingsRow.cs](MatterControlLib/SlicerConfiguration/SliceSettingsRow.cs) | Individual setting row |
| `PresetSelectorWidget` | [SlicerConfiguration/PresetSelectorWidget.cs](MatterControlLib/SlicerConfiguration/PresetSelectorWidget.cs) | Preset dropdown |
| `SettingsRow` | [SlicerConfiguration/SettingsRow.cs](MatterControlLib/SlicerConfiguration/SettingsRow.cs) | Base row widget |
### Key Enums
| Enum | Values |
|------|--------|
| `PrintOutputTypes` | Default, Solid, Support, Hole, Fuzzy |
| `InfillType` | Grid, Triangles, Gyroid, Lines, etc. |
| `SeamPlacement` | Random, Nearest, Aligned |
| `SupportType` | None, Lines, Grid, FromBed, Everywhere |
### Settings Keys (Sample)
| Key | Description |
|-----|-------------|
| `layer_height` | Layer thickness |
| `perimeters` | Number of perimeter shells |
| `fill_density` | Infill percentage |
| `support_material` | Enable supports |
| `create_raft` | Create print raft |
| `retract_length` | Retraction distance |
| `travel_speed` | Non-printing move speed |