2022-09-15 07:53:35 +02:00
|
|
|
import { name, version } from '../data.mjs'
|
2022-09-07 21:16:44 +02:00
|
|
|
import { Point } from '@freesewing/core'
|
|
|
|
|
|
|
|
export const plugin = {
|
|
|
|
name,
|
|
|
|
version,
|
|
|
|
store: [
|
|
|
|
['addCut', addCut],
|
|
|
|
['removeCut', removeCut],
|
|
|
|
['setGrain', setGrain],
|
|
|
|
['setCutOnFold', setCutOnFold],
|
2022-09-11 21:40:21 +02:00
|
|
|
],
|
2022-09-07 21:16:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// More specifically named exports
|
|
|
|
export const cutlistPlugin = plugin
|
|
|
|
export const pluginCutlist = plugin
|
|
|
|
|
2023-03-09 15:14:58 -06:00
|
|
|
/**
|
|
|
|
* Add a set of cutting instructions for the part
|
|
|
|
* @param {Store} store the Store
|
|
|
|
* @param {string} partName the name of the part
|
|
|
|
* @param {Object} so a set of cutting instructions for a material
|
|
|
|
* @param {number} so.cut = 2 the number of pieces to cut from the specified fabric
|
|
|
|
* @param {string} so.material = fabric the name of the material to cut from
|
|
|
|
* @param {boolean} so.identical = false should even numbers of pieces be cut in the same direction or mirrored
|
|
|
|
* @param {boolean} so.bias = false should the pieces in these cutting instructions be cut on the bias
|
|
|
|
* @param {boolean} so.ignoreOnFold should these cutting instructions ignore any cutOnFold information set by the part
|
|
|
|
*/
|
2023-03-09 11:20:17 -06:00
|
|
|
function addCut(store, partName, so = {}) {
|
|
|
|
const { cut = 2, material = 'fabric', identical = false, bias = false, ignoreOnFold = false } = so
|
2022-09-07 21:16:44 +02:00
|
|
|
if (cut === false) {
|
|
|
|
if (material === false) store.unset(['cutlist', partName, 'materials'])
|
|
|
|
else store.unset(['cutlist', partName, 'materials', material])
|
|
|
|
return store
|
|
|
|
}
|
|
|
|
if (!(Number.isInteger(cut) && cut > -1)) {
|
|
|
|
store.log.error(`Tried to set cut to a value that is not a positive integer`)
|
|
|
|
return store
|
|
|
|
}
|
|
|
|
if (typeof material !== 'string') {
|
|
|
|
store.log.warning(`Tried to set material to a value that is not a string`)
|
|
|
|
return store
|
|
|
|
}
|
|
|
|
const path = ['cutlist', partName, 'materials', material]
|
2023-03-09 11:20:17 -06:00
|
|
|
const existing = store.get(path) || []
|
|
|
|
store.set(path, existing.concat({ cut, identical, bias, ignoreOnFold }))
|
2022-09-07 21:16:44 +02:00
|
|
|
|
|
|
|
return store
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Method to remove the cut info */
|
2022-09-11 21:40:21 +02:00
|
|
|
function removeCut(store, partName, material = false) {
|
2023-03-09 11:20:17 -06:00
|
|
|
return addCut(store, partName, { cut: false, material })
|
2022-09-07 21:16:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Method to add the grain info */
|
2023-03-09 11:20:17 -06:00
|
|
|
function setGrain(store, partName, grain = false) {
|
|
|
|
const path = ['cutlist', partName, 'grain']
|
2022-09-07 21:16:44 +02:00
|
|
|
if (grain === false) return store.unset(path)
|
2023-03-09 11:20:17 -06:00
|
|
|
if (typeof grain !== 'number') {
|
|
|
|
store.log.error('Called part.setGrain() with a value that is not a number')
|
2022-09-07 21:16:44 +02:00
|
|
|
return store
|
|
|
|
}
|
|
|
|
return store.set(path, grain)
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Method to add the cutOnFold info */
|
2023-03-09 11:20:17 -06:00
|
|
|
function setCutOnFold(store, partName, p1, p2) {
|
|
|
|
const path = ['cutlist', partName, 'cutOnFold']
|
2022-09-07 21:16:44 +02:00
|
|
|
if (p1 === false && typeof p2 === 'undefined') {
|
2023-03-09 11:20:17 -06:00
|
|
|
return store.unset(path)
|
2022-09-07 21:16:44 +02:00
|
|
|
}
|
|
|
|
if (p1 instanceof Point && p2 instanceof Point) {
|
|
|
|
store.set(path, [p1, p2])
|
2022-09-11 21:40:21 +02:00
|
|
|
} else
|
|
|
|
store.log.error('Called part.setCutOnFold() but at least one parameter is not a Point instance')
|
2022-09-07 21:16:44 +02:00
|
|
|
|
|
|
|
return store
|
|
|
|
}
|