2023-04-15 17:09:29 -04:00
|
|
|
export const cutlistStores = [
|
|
|
|
['cutlist.addCut', addCut],
|
|
|
|
['cutlist.removeCut', removeCut],
|
|
|
|
['cutlist.setGrain', setGrain],
|
|
|
|
['cutlist.setCutOnFold', setCutOnFold],
|
|
|
|
['cutlist.getCutFabrics', getCutFabrics],
|
|
|
|
]
|
2022-09-07 21:16:44 +02:00
|
|
|
|
2023-04-15 17:09:29 -04:00
|
|
|
export const cutlistHooks = {
|
|
|
|
prePartDraft: [
|
|
|
|
function (pattern) {
|
2023-03-20 09:45:37 -05:00
|
|
|
const injectedPart = pattern.config.inject[pattern.activePart]
|
|
|
|
if (!injectedPart) return
|
|
|
|
|
|
|
|
const store = pattern.setStores[pattern.activeSet]
|
|
|
|
const injectedCutlist = store.get(['cutlist', injectedPart], {})
|
|
|
|
store.set(['cutlist', pattern.activePart], { ...injectedCutlist })
|
|
|
|
},
|
2023-04-15 17:09:29 -04:00
|
|
|
],
|
2022-09-07 21:16:44 +02:00
|
|
|
}
|
|
|
|
|
2023-03-09 15:14:58 -06:00
|
|
|
/**
|
|
|
|
* Add a set of cutting instructions for the part
|
|
|
|
* @param {Store} store the Store
|
|
|
|
* @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-12 09:19:50 -05:00
|
|
|
function addCut(store, so = {}) {
|
2023-09-07 10:29:19 +02:00
|
|
|
const { cut = 2, identical = false, bias = false, ignoreOnFold = false } = so
|
|
|
|
// Make 'from' an alias for material
|
|
|
|
let { material = 'fabric' } = so
|
|
|
|
if (so.from) material = so.from
|
|
|
|
|
2023-03-12 09:19:50 -05:00
|
|
|
const partName = store.get('activePart')
|
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') {
|
2023-09-06 08:29:59 +02:00
|
|
|
store.log.warn(`Tried to set material to a value that is not a string`)
|
2022-09-07 21:16:44 +02:00
|
|
|
return store
|
|
|
|
}
|
|
|
|
const path = ['cutlist', partName, 'materials', material]
|
2023-03-19 05:44:57 -07:00
|
|
|
const existing = store.get(path, [])
|
2023-03-09 11:20:17 -06:00
|
|
|
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 */
|
2023-03-12 09:19:50 -05:00
|
|
|
function removeCut(store, material = false) {
|
|
|
|
return addCut(store, { cut: false, material })
|
2022-09-07 21:16:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Method to add the grain info */
|
2023-03-12 09:19:50 -05:00
|
|
|
function setGrain(store, grain = false) {
|
|
|
|
const partName = store.get('activePart')
|
2023-03-09 11:20:17 -06:00
|
|
|
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-12 09:19:50 -05:00
|
|
|
function setCutOnFold(store, p1, p2) {
|
|
|
|
const partName = store.get('activePart')
|
2023-03-09 11:20:17 -06:00
|
|
|
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
|
|
|
}
|
2023-03-15 20:33:48 -05:00
|
|
|
if (!isNaN(p1.x) && !isNaN(p1.y) && !isNaN(p2.x) && !isNaN(p2.y)) {
|
2022-09-07 21:16:44 +02:00
|
|
|
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
|
|
|
|
}
|
2023-03-15 12:48:46 -05:00
|
|
|
|
2023-03-19 20:39:31 -05:00
|
|
|
/** Get a list of fabrics used by the pattern for the given settings */
|
2023-03-15 12:48:46 -05:00
|
|
|
function getCutFabrics(store, settings) {
|
|
|
|
const cutlist = store.get('cutlist')
|
|
|
|
const list = settings.only ? [].concat(settings.only) : Object.keys(cutlist)
|
|
|
|
|
|
|
|
const fabrics = []
|
|
|
|
list.forEach((partName) => {
|
2023-04-19 10:11:05 -04:00
|
|
|
if (!cutlist[partName]?.materials) {
|
2023-03-15 12:48:46 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for (var m in cutlist[partName].materials) {
|
|
|
|
if (!fabrics.includes(m)) fabrics.push(m)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return fabrics
|
|
|
|
}
|