1
0
Fork 0
freesewing/plugins/plugin-flip/src/index.mjs

52 lines
2 KiB
JavaScript
Raw Normal View History

import { name, version } from '../data.mjs'
2018-12-22 14:16:53 +01:00
2022-08-28 12:33:06 +02:00
export const plugin = {
name,
version,
2018-12-22 14:16:53 +01:00
macros: {
flip: function (so, { paths, points, snippets }) {
2022-01-19 16:23:40 +01:00
const axis = so?.axis === 'y' ? 'y' : 'x'
2019-08-03 15:03:33 +02:00
let flipped = null
const ops = ['from', 'to', 'cp1', 'cp2']
for (const id in points) {
// Keep track of the amount of flips
// (needed to allow flipping twice, but also avoid double flips in paths below)
2019-07-06 13:57:38 +02:00
if (flipped === null) {
flipped = points[id].attributes.get('flipped')
2019-08-03 15:03:33 +02:00
if (flipped === false) flipped = 1
else flipped += 1
2019-07-06 13:57:38 +02:00
}
points[id][axis] = points[id][axis] * -1
points[id].attributes.set('flipped', flipped)
2019-07-06 13:57:38 +02:00
}
for (let id of Object.keys(paths)) {
for (let op in paths[id].ops) {
2018-12-22 14:16:53 +01:00
for (let type of ops) {
if (typeof paths[id].ops[op][type] !== 'undefined') {
2019-07-06 13:57:38 +02:00
// Path ops can use points not listed in part.points. We should only flip those here
// and not double flip the points flipped above
let wasFlipped = paths[id].ops[op][type].attributes.get('flipped')
2019-08-03 15:03:33 +02:00
if (wasFlipped !== false) wasFlipped = parseInt(wasFlipped)
2019-07-06 13:57:38 +02:00
if (wasFlipped !== flipped) {
paths[id].ops[op][type][axis] = paths[id].ops[op][type][axis] * -1
paths[id].ops[op][type].attributes.set('flipped', flipped)
2019-07-06 13:57:38 +02:00
}
}
2018-12-22 14:16:53 +01:00
}
}
}
for (let id of Object.keys(snippets)) {
2019-07-06 13:57:38 +02:00
// Snippets use points not listed in part.points. We should only flip those here
// and not double flip the points flipped above
let wasFlipped = snippets[id].anchor.attributes.get('flipped')
2019-08-03 15:03:33 +02:00
if (wasFlipped !== false) wasFlipped = parseInt(wasFlipped)
if (wasFlipped !== flipped) snippets[id].anchor[axis] = snippets[id].anchor[axis] * -1
2019-07-06 13:57:38 +02:00
}
2022-01-19 16:23:40 +01:00
},
},
2019-08-03 15:03:33 +02:00
}
2022-08-28 12:33:06 +02:00
// More specifically named exports
export const flipPlugin = plugin
export const pluginFlip = plugin