diff --git a/packages/core/src/part.mjs b/packages/core/src/part.mjs index 7161f7cfb25..86744cb236f 100644 --- a/packages/core/src/part.mjs +++ b/packages/core/src/part.mjs @@ -359,7 +359,7 @@ Part.prototype.__macroClosure = function (props) { const self = this const method = function (key, args) { const macro = utils.__macroName(key) - if (typeof self[macro] === 'function') self[macro](args, props) + if (typeof self[macro] === 'function') return self[macro](args, props) else if ('context' in self) self.context.store.log.warn('Unknown macro `' + key + '` used in ' + self.name) } diff --git a/plugins/core-plugins/src/index.mjs b/plugins/core-plugins/src/index.mjs index dbed6e11807..f3fc1a4d279 100644 --- a/plugins/core-plugins/src/index.mjs +++ b/plugins/core-plugins/src/index.mjs @@ -45,9 +45,9 @@ for (const plugin of bundledPlugins) { export const plugin = { name, version, + store, hooks, macros, - store, } // Specific named export diff --git a/plugins/plugin-annotations/src/banner.mjs b/plugins/plugin-annotations/src/banner.mjs index 67e0ca61f93..d6432342344 100644 --- a/plugins/plugin-annotations/src/banner.mjs +++ b/plugins/plugin-annotations/src/banner.mjs @@ -58,6 +58,8 @@ const banner = function (config, { part, paths, store, complete }) { * Store all IDs in the store so we can remove this macro with rmbanner */ store.set(['parts', part.name, 'macros', 'banner', 'ids', mc.id, 'paths'], ids) + + return store.getMacroIds(config.id, 'banner') } // Export macros diff --git a/plugins/plugin-annotations/src/bannerbox.mjs b/plugins/plugin-annotations/src/bannerbox.mjs index f5dfac20aa0..60d3892a670 100644 --- a/plugins/plugin-annotations/src/bannerbox.mjs +++ b/plugins/plugin-annotations/src/bannerbox.mjs @@ -100,6 +100,8 @@ const bannerbox = function (config, { Point, paths, Path, part, macro, log, stor * Store all IDs in the store so we can remove this macro with rmtitle */ store.set(['parts', part.name, 'macros', 'bannerbox', 'ids', mc.id, 'paths'], ids) + + return store.getMacroIds(mc.id, 'bannerbox') } export const bannerboxMacros = { bannerbox, rmbannerbox } diff --git a/plugins/plugin-annotations/src/bartack.mjs b/plugins/plugin-annotations/src/bartack.mjs index dc03ba4beca..0f762732aff 100644 --- a/plugins/plugin-annotations/src/bartack.mjs +++ b/plugins/plugin-annotations/src/bartack.mjs @@ -129,6 +129,8 @@ function createBartack(config, props) { * Store all IDs in the store so we can remove this macro with rm[name] */ props.store.set(['parts', props.part.name, 'macros', name, 'ids', mc.id, 'paths'], ids) + + return props.store.getMacroIds(mc.id, name) } /* diff --git a/plugins/plugin-annotations/src/crossbox.mjs b/plugins/plugin-annotations/src/crossbox.mjs index 706c9ef56e3..6df9648a0a1 100644 --- a/plugins/plugin-annotations/src/crossbox.mjs +++ b/plugins/plugin-annotations/src/crossbox.mjs @@ -119,6 +119,8 @@ const crossbox = function (config, { points, Point, paths, Path, complete, store * Just make sure to keep points and paths apart */ store.set(['parts', part.name, 'macros', 'title', 'ids', mc.id], ids) + + return store.getMacroIds(mc.id, 'crossbox') } // Export macros diff --git a/plugins/plugin-annotations/src/cutonfold.mjs b/plugins/plugin-annotations/src/cutonfold.mjs index 8d05fe11232..3658f48e8f5 100644 --- a/plugins/plugin-annotations/src/cutonfold.mjs +++ b/plugins/plugin-annotations/src/cutonfold.mjs @@ -107,6 +107,8 @@ const cutonfold = function (config, { paths, Path, complete, store, scale, log, * Store all IDs in the store so we can remove this macro with rmcutonfold */ store.set(['parts', part.name, 'macros', 'cutonfold', 'ids', mc.id, 'paths'], ids) + + return store.getMacroIds(mc.id, 'cutonfold') } // Export macros diff --git a/plugins/plugin-annotations/src/dimensions.mjs b/plugins/plugin-annotations/src/dimensions.mjs index 616d67eeb1c..c3f997e1b03 100644 --- a/plugins/plugin-annotations/src/dimensions.mjs +++ b/plugins/plugin-annotations/src/dimensions.mjs @@ -151,6 +151,8 @@ const addDimension = (config, props, type) => { * Store all IDs in the store so we can remove this macro with rm variants */ props.store.set(['parts', props.part.name, 'macros', type, 'ids', mc.id, 'paths'], ids) + + return props.store.getMacroIds(mc.id, type) } /* diff --git a/plugins/plugin-annotations/src/grainline.mjs b/plugins/plugin-annotations/src/grainline.mjs index 564c8445dd5..c1b06e2efcf 100644 --- a/plugins/plugin-annotations/src/grainline.mjs +++ b/plugins/plugin-annotations/src/grainline.mjs @@ -99,6 +99,8 @@ const grainline = function (config = {}, { paths, Path, Point, complete, store, * Store all IDs in the store so we can remove this macro with rmgrainline */ store.set(['parts', part.name, 'macros', 'grainline', 'ids', mc.id, 'paths'], ids) + + return store.getMacroIds(mc.id, 'grainline') } // Export macros diff --git a/plugins/plugin-annotations/src/index.mjs b/plugins/plugin-annotations/src/index.mjs index aecfadcddbe..f654133c72e 100644 --- a/plugins/plugin-annotations/src/index.mjs +++ b/plugins/plugin-annotations/src/index.mjs @@ -21,6 +21,7 @@ import { pleatMacros, pleatDefs } from './pleat.mjs' import { sewtogetherMacros, sewtogetherDefs } from './sewtogether.mjs' // Only stores import { flagStores } from './flag.mjs' +import { utilsStores } from './utils.mjs' export const plugin = { name, @@ -61,7 +62,7 @@ export const plugin = { ...sewtogetherMacros, ...titleMacros, }, - store: [...cutlistStores, ...flagStores], + store: [...cutlistStores, ...flagStores, ...utilsStores], } export const annotationsPlugin = plugin diff --git a/plugins/plugin-annotations/src/pleat.mjs b/plugins/plugin-annotations/src/pleat.mjs index 19ea582be29..57f7ed047b1 100644 --- a/plugins/plugin-annotations/src/pleat.mjs +++ b/plugins/plugin-annotations/src/pleat.mjs @@ -110,6 +110,8 @@ const pleat = function (config, { paths, Path, log, Point, complete, scale, stor * Store all IDs in the store so we can remove this macro with rmpleat */ store.set(['parts', part.name, 'macros', 'pleat', 'ids', mc.id, 'paths'], ids) + + return store.getMacroIds(mc.id, 'pleat') } // Export macros diff --git a/plugins/plugin-annotations/src/scalebox.mjs b/plugins/plugin-annotations/src/scalebox.mjs index bbe96e16379..4ee6e26438b 100644 --- a/plugins/plugin-annotations/src/scalebox.mjs +++ b/plugins/plugin-annotations/src/scalebox.mjs @@ -259,6 +259,8 @@ const scalebox = function ( imperial: ids.imperial, }, }) + + return store.getMacroIds(mc.id, 'scalebox') } /* diff --git a/plugins/plugin-annotations/src/sewtogether.mjs b/plugins/plugin-annotations/src/sewtogether.mjs index 827b7fdcaaa..d34682c0e50 100644 --- a/plugins/plugin-annotations/src/sewtogether.mjs +++ b/plugins/plugin-annotations/src/sewtogether.mjs @@ -125,6 +125,8 @@ const sewtogether = function (config, { paths, Path, log, Point, complete, sa, s * Store all IDs in the store so we can remove this macro with rmsewtogether */ store.set(['parts', part.name, 'macros', 'sewtogether', 'ids', mc.id, 'paths'], ids) + + return store.getMacroIds(mc.id, 'sewtogether') } // Export macros diff --git a/plugins/plugin-annotations/src/title.mjs b/plugins/plugin-annotations/src/title.mjs index efcb18a2185..628d3d3fbf8 100644 --- a/plugins/plugin-annotations/src/title.mjs +++ b/plugins/plugin-annotations/src/title.mjs @@ -230,6 +230,8 @@ const addTitleMacro = function ( * Store all IDs in the store so we can remove this macro with rmtitle */ store.set(['parts', part.name, 'macros', 'title', 'ids', mc.id, 'points'], ids) + + return store.getMacroIds(mc.id, 'title') } // Export macros diff --git a/plugins/plugin-annotations/src/utils.mjs b/plugins/plugin-annotations/src/utils.mjs index f513c13fca8..8f7ec92a279 100644 --- a/plugins/plugin-annotations/src/utils.mjs +++ b/plugins/plugin-annotations/src/utils.mjs @@ -7,3 +7,21 @@ export const getIds = (keys, id, macroName) => { return ids } + +/* + * Helper method to get an existing macro id + */ +const getIdsFromStore = (store, id, macroName, partName = false) => { + if (!partName) partName = store.activePart + const data = store.get(['parts', partName, 'macros', macroName, 'ids', id]) + + return data ? data : false +} + +/* + * Add these to the store + */ +export const utilsStores = [ + ['createMacroIds', (store, keys, id, macroName) => getIds(keys, id, macroName)], + ['getMacroIds', getIdsFromStore], +] diff --git a/plugins/plugin-round/src/index.mjs b/plugins/plugin-round/src/index.mjs index 72cf7668d8a..92c367d08ac 100644 --- a/plugins/plugin-round/src/index.mjs +++ b/plugins/plugin-round/src/index.mjs @@ -1,39 +1,56 @@ import { name, version } from '../data.mjs' +import { getIds } from './utils.mjs' + +/* + * These are the keys for macro IDs + */ +const pointKeys = ['start', 'cp1', 'cp2', 'end'] +const pathKeys = ['path'] export const plugin = { name, version, macros: { - round: function (so) { + round: function (mc, { points, paths, Point, Path, store, part }) { const C = 0.55191502449 - const { hide = true } = so - // Find angle between points - let from = so.from - let to = so.to - let via = so.via - let radius = so.radius - let prefix = so.prefix || 'round' - //let angle1 = from.angle(via) - //let angle2 = via.angle(to) - //if ((Math.round(angle1) - Math.round(angle2)) % 90 !== 0) - // console.log('Warning: The round macro only handles 90 degree angles correctly.') - let fd = from.dist(via) - let td = to.dist(via) + const { + from = new Point(0, 0), + to = new Point(666, 666), + via = new Point(666, 0), + id = 'round', + classes = '', + hide = true, + } = mc + let { radius = 66.6 } = mc + const ids = getIds([...pointKeys, ...pathKeys], id, name) + + const fd = from.dist(via) + const td = to.dist(via) if (radius > fd || radius > td || typeof radius === 'undefined') radius = fd > td ? td : fd - this.points[prefix + 'Start'] = via.shiftTowards(from, radius) - this.points[prefix + 'Cp1'] = via.shiftTowards(from, radius * (1 - C)) - this.points[prefix + 'Cp2'] = via.shiftTowards(to, radius * (1 - C)) - this.points[prefix + 'End'] = via.shiftTowards(to, radius) - this.paths[prefix + 'Rounded'] = new this.Path() - .move(this.points[prefix + 'Start']) - .curve( - this.points[prefix + 'Cp1'], - this.points[prefix + 'Cp2'], - this.points[prefix + 'End'] - ) - .attr('class', so.class ? so.class : '') - if (hide) this.paths[prefix + 'Rounded'].hide() - else this.paths[prefix + 'Rounded'].unhide() + points[ids.start] = via.shiftTowards(from, radius) + points[ids.cp1] = via.shiftTowards(from, radius * (1 - C)) + points[ids.cp2] = via.shiftTowards(to, radius * (1 - C)) + points[ids.end] = via.shiftTowards(to, radius) + paths[ids.path] = new Path() + .move(this.points[ids.start]) + .curve(points[ids.cp1], points[ids.cp2], points[ids.end]) + .addClass(classes) + if (hide) paths[ids.path].hide() + else paths[ids.path].unhide() + + /* + * Store all IDs in the store so we can remove this macro with rmtitle + */ + store.set( + ['parts', part.name, 'macros', 'round', 'ids', mc.id, 'points'], + getIds(pointKeys, id, name) + ) + store.set( + ['parts', part.name, 'macros', 'round', 'ids', mc.id, 'paths'], + getIds(pathKeys, id, name) + ) + + return store.getMacroIds(id, 'round') }, }, } diff --git a/plugins/plugin-round/src/utils.mjs b/plugins/plugin-round/src/utils.mjs new file mode 100644 index 00000000000..f513c13fca8 --- /dev/null +++ b/plugins/plugin-round/src/utils.mjs @@ -0,0 +1,9 @@ +/* + * Helper method to get the various IDs for a macro + */ +export const getIds = (keys, id, macroName) => { + const ids = {} + for (const key of keys) ids[key] = `__macro_${macroName}_${id}_${key}` + + return ids +}