Skip to content

Commit

Permalink
feat: try parsing layer info directly from g-code (#1031)
Browse files Browse the repository at this point in the history
Signed-off-by: Pedro Lamas <pedrolamas@gmail.com>
  • Loading branch information
pedrolamas committed Feb 9, 2023
1 parent 72f56a2 commit cc323b8
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 68 deletions.
34 changes: 21 additions & 13 deletions src/components/settings/GcodePreviewSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

<v-divider />

<app-setting :title="$t('app.setting.label.group_lower_layers')">
<app-setting :title="$t('app.setting.label.draw_background')">
<v-switch
v-model="groupLowerLayers"
v-model="drawBackground"
hide-details
class="mb-5"
@click.native.stop
Expand All @@ -30,12 +30,20 @@

<v-divider />

<app-setting :title="$t('app.setting.label.draw_background')">
<v-switch
v-model="drawBackground"
hide-details
class="mb-5"
@click.native.stop
<app-setting :title="$t('app.setting.label.default_min_layer_height')">
<v-text-field
:value="minLayerHeight"
:rules="[
$rules.required,
$rules.numberValid,
$rules.numberGreaterThanOrEqual(0.1)
]"
filled
dense
single-line
hide-details="auto"
suffix="mm"
@change="setMinLayerHeight"
/>
</app-setting>

Expand Down Expand Up @@ -261,14 +269,14 @@ export default class GcodePreviewSettings extends Vue {
})
}
get groupLowerLayers () {
return this.$store.state.config.uiSettings.gcodePreview.groupLowerLayers
get minLayerHeight () {
return this.$store.state.config.uiSettings.gcodePreview.minLayerHeight
}
set groupLowerLayers (value: boolean) {
setMinLayerHeight (value: number) {
this.$store.dispatch('config/saveByPath', {
path: 'uiSettings.gcodePreview.groupLowerLayers',
value,
path: 'uiSettings.gcodePreview.minLayerHeight',
value: +value,
server: true
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/locales/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ app:
dashed: Dashed
default_extrude_length: Default extrude length
default_extrude_speed: Default extrude speed
default_min_layer_height: Default minimum layer height
default_toolhead_move_length: Default toolhead move length
default_toolhead_xy_speed: Default toolhead XY speed
default_toolhead_z_speed: Default toolhead Z speed
Expand All @@ -512,7 +513,6 @@ app:
fps_idle_target: FPS Target when not in focus
height: Height
gcode_coords: Use GCode Coordinates
group_lower_layers: Group lower layers
icon: Icon
invert_x_control: Invert X control
invert_y_control: Invert Y control
Expand Down
2 changes: 1 addition & 1 deletion src/store/config/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export const defaultState = (): ConfigState => {
retractionIconSize: 0.6,
drawBackground: true,
showAnimations: true,
groupLowerLayers: false,
minLayerHeight: 0.1,
autoLoadOnPrintStart: false,
autoLoadMobileOnPrintStart: false,
autoFollowOnFileLoad: true,
Expand Down
2 changes: 1 addition & 1 deletion src/store/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export interface GcodePreviewConfig {
retractionIconSize: number;
drawBackground: boolean;
showAnimations: boolean;
groupLowerLayers: boolean;
minLayerHeight: number;
autoLoadOnPrintStart: boolean;
autoLoadMobileOnPrintStart: boolean;
autoFollowOnFileLoad: boolean;
Expand Down
4 changes: 3 additions & 1 deletion src/store/gcodePreview/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ export const actions: ActionTree<GcodePreviewState, RootState> = {
break
}

case 'moves': {
case 'result': {
try {
commit('setMoves', data.moves)
commit('setLayers', data.layers)
commit('setParserProgress', payload.file.size ?? payload.gcode.length)
} catch (error) {
consola.error('Parser worker error', error)
Expand All @@ -65,6 +66,7 @@ export const actions: ActionTree<GcodePreviewState, RootState> = {

commit('setParserProgress', 0)
commit('setMoves', [])
commit('setLayers', [])

commit('setFile', payload.file)

Expand Down
39 changes: 21 additions & 18 deletions src/store/gcodePreview/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { RootState } from '../types'
import { AppFile } from '@/store/files/types'
import { binarySearch, moveToSVGPath } from '@/util/gcode-preview'
import { state as configState } from '@/store/config/state'
import IsKeyOf from '@/util/is-key-of'

export const getters: GetterTree<GcodePreviewState, RootState> = {
/**
Expand All @@ -34,40 +34,43 @@ export const getters: GetterTree<GcodePreviewState, RootState> = {
},

getLayers: (state, getters, rootState): Layer[] => {
if (state.layers.length) {
return state.layers
}

const output = []
const moves = getters.getMoves
const moves = getters.getMoves as Move[]

let z = NaN
let zStart = 0
let zLast = NaN
let zNext = NaN

const { uiSettings } = (rootState && rootState.config) ? rootState.config : configState
const groupLowerLayers = uiSettings.gcodePreview.groupLowerLayers
const { minLayerHeight } = rootState.config.uiSettings.gcodePreview

const zCmp = groupLowerLayers
? (a: number, b: number) => Number.isNaN(a) || a < b
: (a: number, b: number) => a !== b

moves.forEach((move: Move, index: number) => {
moves.forEach((move, index) => {
if (move.z !== undefined && z !== move.z) {
z = move.z
zStart = index
}

if (move.e && move.e > 0 && zCmp(zLast, z)) {
zLast = z
if (move.e && move.e > 0 && (Number.isNaN(zLast) || z < zLast || z >= zNext)) {
if (['x', 'y', 'i', 'j'].some(x => IsKeyOf(x, move) && move[x] !== 0)) {
zLast = z
zNext = Math.round((z + minLayerHeight) * 10000) / 10000

output.push({
z,
move: zStart,
filePosition: move.filePosition
})
output.push({
z,
move: zStart,
filePosition: move.filePosition
})
}
}
})

// If moves exist but there are no layers, add a single "default" layer at z=0
// This can happen for gcode that only contains travel moves (eg: 2d plotters without Z or E steppers)
if (output.length === 0 && moves.length > 0) {
if (output.length === 0 && moves.length) {
output.push({
z: 0,
move: 0,
Expand Down Expand Up @@ -241,7 +244,7 @@ export const getters: GetterTree<GcodePreviewState, RootState> = {
return 0
}

return binarySearch(getters.getMoves, (val: Move) => filePosition - (val.filePosition ?? 0), true)
return binarySearch(getters.getMoves, (val: Move) => filePosition - val.filePosition, true)
},

getLayerNrByFilePosition: (state, getters) => (filePosition: number): LayerNr => {
Expand Down
4 changes: 4 additions & 0 deletions src/store/gcodePreview/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export const mutations: MutationTree<GcodePreviewState> = {
Vue.set(state, 'moves', Object.freeze(payload.map(Object.freeze)))
},

setLayers (state, payload) {
Vue.set(state, 'layers', Object.freeze(payload.map(Object.freeze)))
},

setFile (state, file: AppFile) {
state.file = file
},
Expand Down
1 change: 1 addition & 0 deletions src/store/gcodePreview/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { GcodePreviewState } from './types'
export const defaultState = (): GcodePreviewState => {
return {
moves: [],
layers: [],
file: undefined,
parserProgress: 0,
parserWorker: null,
Expand Down
7 changes: 5 additions & 2 deletions src/store/gcodePreview/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export type LayerNr = number

export interface GcodePreviewState {
moves: Move[];
layers: Layer[],
file?: AppFile;
parserProgress: number;
parserWorker: Worker | null;
Expand All @@ -25,12 +26,13 @@ export interface LinearMove {
z?: number;
e?: number;

filePosition?: number;
filePosition: number;
}

export interface ArcMove extends LinearMove {
i?: number;
j?: number;
k?: number;
r?: number;
direction: Rotation;
}
Expand Down Expand Up @@ -84,8 +86,9 @@ export type ParseGcodeWorkerClientMessage = {
action: 'progress',
filePosition: number
} | {
action: 'moves',
action: 'result',
moves: Move[]
layers: Layer[]
}

export type ParseGcodeWorkerServerMessage = {
Expand Down
Loading

0 comments on commit cc323b8

Please sign in to comment.