1
0
Fork 0

feat(core): Added new cutting methods. Closes #2470

This commit is contained in:
joostdecock 2022-07-30 20:02:42 +02:00
parent 8ec441389b
commit bc567874c1
5 changed files with 196 additions and 28 deletions

View file

@ -18,7 +18,7 @@ function Part() {
this.render = true this.render = true
this.utils = utils this.utils = utils
this.layout = { move: { x: 0, y: 0 } } this.layout = { move: { x: 0, y: 0 } }
this.cut = { grain: 90, materials: {} }
this.Point = Point this.Point = Point
this.Path = Path this.Path = Path
this.Snippet = Snippet this.Snippet = Snippet
@ -357,20 +357,68 @@ Part.prototype.generateTransform = function(transforms) {
} }
} }
/** Chainable way to set the grain property */ /** Chainable way to add the cut info */
Part.prototype.setGrain = function (grain = 90) { Part.prototype.addCut = function (cut=2, material='fabric', identical=false) {
this.attributes.set('data-grain', grain) if (cut === false) {
if (this.cut.materials[material]) delete this.cut.materials[material]
else this.context.raise.error(`Tried to remove a material that is not set`)
return this
}
if (typeof this.cut.materials[material] === 'undefined') this.cut.materials[material] = {}
if (typeof cut !== 'number') {
this.context.raise.error(`Tried to set cut to a value that is not a number`)
return this
}
if (typeof material !== 'string') {
this.context.raise.error(`Tried to set material to a value that is not a string`)
return this
}
this.cut.materials[material].cut = cut
this.cut.materials[material].identical = identical
return this return this
} }
/** Chainable way to set the grain property */ /** Chainable way to remove (some) cut info */
Part.prototype.setCut = function (cut = { count: 2, mirror: true, onFold: false }) { Part.prototype.removeCut = function (material=false) {
this.attributes.set('data-cut', cut) if (!material) {
this.context.raise.warning('Called part.removeCut() without any parameters. Not removing anything')
return this
}
if (typeof material !== 'string') {
this.context.raise.error(`Tried to set material to a value that is not a string`)
return this
}
if (this.cut.materials[material]) delete this.cut.materials[material]
else this.context.raise.error(`Tried to remove a material that is not set`)
return this return this
} }
/** Chainable way to add the grain info */
Part.prototype.setGrain = function (grain=false) {
if (grain === false) {
this.context.raise.warning('Called part.setGrain() without any parameters. Not changing anything')
return this
}
if (typeof grain !== 'number') {
this.context.raise.error('Called part.setGrain() with a value that is not a number')
return this
}
this.cut.grain = grain
return this
}
/** Chainable way to add the cutOnFold info */
Part.prototype.setCutOnFold = function (p1=false, p2=false) {
if (p1 instanceof Point && p2 instanceof Point) {
this.cut.cutOnFold = [p1, p2]
}
else this.context.raise.error('Called part.setCutOnFold() but at least one parameter is not a Point instance')
return this
}
export default Part export default Part

View file

@ -273,33 +273,137 @@ it("Should generate the part transforms", () => {
expect(part.attributes.list['transform-origin'][0]).to.equal('10.5 39') expect(part.attributes.list['transform-origin'][0]).to.equal('10.5 39')
}); });
it("Should set the part grain", () => { it("Should add the part cut", () => {
let pattern = new freesewing.Pattern();
let part = new pattern.Part();
part.addCut(4, 'fabric', true)
expect(part.cut.materials.fabric.cut).to.equal(4)
expect(part.cut.materials.fabric.identical).to.equal(true)
part.addCut()
expect(part.cut.materials.fabric.cut).to.equal(2)
expect(part.cut.materials.fabric.identical).to.equal(false)
});
it("Should generate an error if cut is not a number", () => {
let pattern = new freesewing.Pattern();
let part = new pattern.Part();
part.addCut('a', 'fabric', true)
expect(pattern.events.error.length).to.equal(1)
expect(pattern.events.error[0]).to.equal('Tried to set cut to a value that is not a number')
});
it("Should generate an error if material is not a string", () => {
let pattern = new freesewing.Pattern();
let part = new pattern.Part();
part.addCut(3, 4)
expect(pattern.events.error.length).to.equal(1)
expect(pattern.events.error[0]).to.equal('Tried to set material to a value that is not a string')
});
it("Should generate an error when removing a material that is not set (through addCut)", () => {
let pattern = new freesewing.Pattern();
let part = new pattern.Part();
part.addCut(4, 'fabric', true)
part.addCut(false, 'lining')
expect(pattern.events.error.length).to.equal(1)
expect(pattern.events.error[0]).to.equal('Tried to remove a material that is not set')
});
it("Should remove the part cut through addCut", () => {
let pattern = new freesewing.Pattern();
let part = new pattern.Part();
part.addCut(4, 'fabric', true)
part.addCut(false, 'fabric')
expect(typeof part.cut.materials.fabric).to.equal('undefined')
});
it("Should remove the part cut through removeCut", () => {
let pattern = new freesewing.Pattern();
let part = new pattern.Part();
part.addCut(4, 'fabric', true)
part.removeCut('fabric')
expect(typeof part.cut.materials.fabric).to.equal('undefined')
});
it("Should generate an error when removing a material that is not set (through removeCut)", () => {
let pattern = new freesewing.Pattern();
let part = new pattern.Part();
part.addCut(4, 'fabric', true)
part.removeCut('lining')
expect(pattern.events.error.length).to.equal(1)
expect(pattern.events.error[0]).to.equal('Tried to remove a material that is not set')
});
it("Should generate an error when removing a material that is not set (through removeCut)", () => {
let pattern = new freesewing.Pattern();
let part = new pattern.Part();
part.addCut(4, 'fabric', true)
part.removeCut(23)
expect(pattern.events.error.length).to.equal(1)
expect(pattern.events.error[0]).to.equal('Tried to set material to a value that is not a string')
});
it("Should generate a warning when calling removeCut without parameters", () => {
let pattern = new freesewing.Pattern();
let part = new pattern.Part();
part.addCut(4, 'fabric', true)
part.removeCut()
expect(pattern.events.warning.length).to.equal(1)
expect(pattern.events.warning[0]).to.equal('Called part.removeCut() without any parameters. Not removing anything')
});
it("Should generate a warning when calling removeCut without parameters", () => {
let pattern = new freesewing.Pattern();
let part = new pattern.Part();
part.addCut(4, 'fabric', true)
part.removeCut()
expect(pattern.events.warning.length).to.equal(1)
expect(pattern.events.warning[0]).to.equal('Called part.removeCut() without any parameters. Not removing anything')
});
it("Should set the part grainline", () => {
let pattern = new freesewing.Pattern();
let part = new pattern.Part();
expect(part.cut.grain).to.equal(90)
part.setGrain(123)
expect(part.cut.grain).to.equal(123)
});
it("Should raise a warning when calling part.setGrain() without any parameters", () => {
let pattern = new freesewing.Pattern(); let pattern = new freesewing.Pattern();
pattern.settings.mode = "draft";
let part = new pattern.Part(); let part = new pattern.Part();
part.setGrain(666)
expect(part.attributes.list['data-grain'][0]).to.equal(666)
part.setGrain() part.setGrain()
expect(part.attributes.list['data-grain'][0]).to.equal(90) expect(part.cut.grain).to.equal(90)
expect(pattern.events.warning.length).to.equal(1)
expect(pattern.events.warning[0]).to.equal('Called part.setGrain() without any parameters. Not changing anything')
}); });
it("Should set the part cut", () => { it("Should raise an error when calling part.setGrain() with a value that is not a number", () => {
let pattern = new freesewing.Pattern(); let pattern = new freesewing.Pattern();
pattern.settings.mode = "draft";
let part = new pattern.Part(); let part = new pattern.Part();
const cut = { part.setGrain('a')
count: 4, expect(part.cut.grain).to.equal(90)
mirror: false, expect(pattern.events.error.length).to.equal(1)
onFold: true expect(pattern.events.error[0]).to.equal('Called part.setGrain() with a value that is not a number')
} });
const dflt = {
count: 2, it("Should set the cutOnFold", () => {
mirror: true, let pattern = new freesewing.Pattern();
onFold: false let part = new pattern.Part();
} const { Point } = part.shorthand()
part.setCut(cut) part.setCutOnFold(new Point(2,3), new Point(100,200))
expect(JSON.stringify(part.attributes.list['data-cut'][0])).to.equal(JSON.stringify(cut)) expect(part.cut.cutOnFold[0].x).to.equal(2)
part.setCut() expect(part.cut.cutOnFold[0].y).to.equal(3)
expect(JSON.stringify(part.attributes.list['data-cut'][0])).to.equal(JSON.stringify(dflt)) expect(part.cut.cutOnFold[1].x).to.equal(100)
expect(part.cut.cutOnFold[1].y).to.equal(200)
});
it("Should raise an error when setting the cutOnFold with a non-Point value", () => {
let pattern = new freesewing.Pattern();
let part = new pattern.Part();
const { Point } = part.shorthand()
part.setCutOnFold(new Point(2,3), 12)
expect(pattern.events.error.length).to.equal(1)
expect(pattern.events.error[0]).to.equal('Called part.setCutOnFold() but at least one parameter is not a Point instance')
}); });

View file

@ -26,3 +26,4 @@ it("Should set an attribute", () => {
s.attr("class", "less", true); s.attr("class", "less", true);
expect(s.attributes.get("class")).to.equal("less"); expect(s.attributes.get("class")).to.equal("less");
}); });

View file

@ -18,3 +18,10 @@ it("Should set a store value only if unset", () => {
store.setIfUnset("few", "schmoo"); store.setIfUnset("few", "schmoo");
expect(store.get("few")).to.equal("baz"); expect(store.get("few")).to.equal("baz");
}); });
it("Should raise a warning when retrieving a invalid key", () => {
store.get('nope')
expect(pattern.events.warning.length).to.equal(1)
expect(pattern.events.warning[0]).to.equal('Tried to access `nope` in the `store` but it is not set')
});

View file

@ -267,4 +267,12 @@ it("Should run postRender hook", () => {
expect(pattern.render()).to.equal("test"); expect(pattern.render()).to.equal("test");
}); });
it("Should tab in and out", () => {
let pattern = new freesewing.Pattern()
pattern.render()
const svg = pattern.svg
svg.tabs = 2
expect(svg.tab()).to.equal(' ')
});