1
0
Fork 0

add plugin-cutlist to plugin-annotations

This commit is contained in:
Enoch Riese 2023-04-15 17:09:29 -04:00
parent 04bab16cbd
commit adbc817863
27 changed files with 229 additions and 513 deletions

View file

@ -0,0 +1,103 @@
export const cutlistStores = [
['cutlist.addCut', addCut],
['cutlist.removeCut', removeCut],
['cutlist.setGrain', setGrain],
['cutlist.setCutOnFold', setCutOnFold],
['cutlist.getCutFabrics', getCutFabrics],
]
export const cutlistHooks = {
prePartDraft: [
function (pattern) {
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 })
},
],
}
/**
* 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
*/
function addCut(store, so = {}) {
const { cut = 2, material = 'fabric', identical = false, bias = false, ignoreOnFold = false } = so
const partName = store.get('activePart')
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]
const existing = store.get(path, [])
store.set(path, existing.concat({ cut, identical, bias, ignoreOnFold }))
return store
}
/** Method to remove the cut info */
function removeCut(store, material = false) {
return addCut(store, { cut: false, material })
}
/** Method to add the grain info */
function setGrain(store, grain = false) {
const partName = store.get('activePart')
const path = ['cutlist', partName, 'grain']
if (grain === false) return store.unset(path)
if (typeof grain !== 'number') {
store.log.error('Called part.setGrain() with a value that is not a number')
return store
}
return store.set(path, grain)
}
/** Method to add the cutOnFold info */
function setCutOnFold(store, p1, p2) {
const partName = store.get('activePart')
const path = ['cutlist', partName, 'cutOnFold']
if (p1 === false && typeof p2 === 'undefined') {
return store.unset(path)
}
if (!isNaN(p1.x) && !isNaN(p1.y) && !isNaN(p2.x) && !isNaN(p2.y)) {
store.set(path, [p1, p2])
} else
store.log.error('Called part.setCutOnFold() but at least one parameter is not a Point instance')
return store
}
/** Get a list of fabrics used by the pattern for the given settings */
function getCutFabrics(store, settings) {
const cutlist = store.get('cutlist')
const list = settings.only ? [].concat(settings.only) : Object.keys(cutlist)
const fabrics = []
list.forEach((partName) => {
if (!cutlist[partName].materials) {
return
}
for (var m in cutlist[partName].materials) {
if (!fabrics.includes(m)) fabrics.push(m)
}
})
return fabrics
}

View file

@ -24,10 +24,8 @@ export const cutonfoldMacros = {
delete points.cutonfoldVia1
delete points.cutonfoldVia2
delete paths.cutonfoldCutonfold
// setCutOnFold relies on plugin-cutlist
if (typeof store.cutlist?.setCutOnFold === 'function') {
store.cutlist.setCutOnFold(false) // Restore default
}
store.cutlist.setCutOnFold(false) // Restore default
return true
}
so = {
@ -36,10 +34,11 @@ export const cutonfoldMacros = {
prefix: 'cutonfold',
...so,
}
if (typeof store.cutlist?.setCutOnFold === 'function') {
store.cutlist.setCutOnFold(so.from, so.to)
if (so.grainline) store.cutlist.setGrain(so.from.angle(so.to))
}
// store in cutlist
store.cutlist.setCutOnFold(so.from, so.to)
if (so.grainline) store.cutlist.setGrain(so.from.angle(so.to))
if (complete) {
points[so.prefix + 'From'] = so.from.shiftFractionTowards(so.to, so.margin / 100)
points[so.prefix + 'To'] = so.to.shiftFractionTowards(so.from, so.margin / 100)

View file

@ -29,10 +29,10 @@ export const grainlineMacros = {
...dflts,
...so,
}
// setGrain relies on plugin-cutlist
if (typeof store.cutlist?.setGrain === 'function') {
store.cutlist.setGrain(so.from.angle(so.to))
}
// store in cutlist
store.cutlist.setGrain(so.from.angle(so.to))
if (complete) {
points.grainlineFrom = so.from.shiftFractionTowards(so.to, 0.05)
points.grainlineTo = so.to.shiftFractionTowards(so.from, 0.05)

View file

@ -8,6 +8,7 @@ import { bannerMacros } from './banner.mjs'
import { bannerboxMacros } from './bannerbox.mjs'
import { bartackMacros } from './bartack.mjs'
import { crossboxMacros } from './crossbox.mjs'
import { cutlistStores, cutlistHooks } from './cutlist.mjs'
import { scaleboxMacros } from './scalebox.mjs'
import { titleMacros } from './title.mjs'
// Hooks and Macros
@ -31,6 +32,7 @@ export const plugin = {
...pleatHooks.preRender,
...sewtogetherHooks.preRender,
],
prePartDraft: [...cutlistHooks.prePartDraft],
},
macros: {
...bannerMacros,
@ -45,6 +47,7 @@ export const plugin = {
...sewtogetherMacros,
...titleMacros,
},
store: [...cutlistStores],
}
export const annotationsPlugin = plugin