From e134e940e7dbc91b49218d6a7fad58de6c71481d Mon Sep 17 00:00:00 2001 From: Joost De Cock Date: Sat, 18 Apr 2020 11:35:28 +0200 Subject: [PATCH] feat: Added plugin-export-dxf --- config/descriptions.yaml | 121 ++++----- packages/plugin-export-dxf/CHANGELOG.md | 108 ++++++++ packages/plugin-export-dxf/README.md | 185 +++++++++++++ packages/plugin-export-dxf/info.md | 85 ++++++ packages/plugin-export-dxf/package.json | 52 ++++ packages/plugin-export-dxf/rollup.config.js | 41 +++ packages/plugin-export-dxf/src/dxf.js | 243 ++++++++++++++++++ packages/plugin-export-dxf/src/footer.js | 5 + packages/plugin-export-dxf/src/header.js | 13 + packages/plugin-export-dxf/src/index.js | 13 + packages/plugin-export-dxf/tests/bust.test.js | 12 + 11 files changed, 818 insertions(+), 60 deletions(-) create mode 100644 packages/plugin-export-dxf/CHANGELOG.md create mode 100644 packages/plugin-export-dxf/README.md create mode 100644 packages/plugin-export-dxf/info.md create mode 100644 packages/plugin-export-dxf/package.json create mode 100644 packages/plugin-export-dxf/rollup.config.js create mode 100644 packages/plugin-export-dxf/src/dxf.js create mode 100644 packages/plugin-export-dxf/src/footer.js create mode 100644 packages/plugin-export-dxf/src/header.js create mode 100644 packages/plugin-export-dxf/src/index.js create mode 100644 packages/plugin-export-dxf/tests/bust.test.js diff --git a/config/descriptions.yaml b/config/descriptions.yaml index 7fa246b07ed..2aba61c3878 100644 --- a/config/descriptions.yaml +++ b/config/descriptions.yaml @@ -1,62 +1,63 @@ -aaron: "A FreeSewing pattern for a A-shirt or tank top" -benjamin: "A FreeSewing pattern for a bow tie" -bent: "A FreeSewing pattern for a menswear body block with a two-part sleeve" -breanna: "A FreeSewing pattern for a basic body block for womenswear" -brian: "A FreeSewing pattern for a basic body block for menswear" -bruce: "A FreeSewing pattern for boxer briefs" -carlita: "A FreeSewing pattern for Sherlock Holmes cosplay; Or just a nice long coat" -carlton: "A FreeSewing pattern for Sherlock Holmes cosplay; Or just a nice long coat" -cathrin: "A FreeSewing pattern for a underbust corset / waist trainer" -components: "A collection of React components for FreeSewing web UIs" -core: "A library for creating made-to-measure sewing patterns" -create-freesewing-pattern: "Initializer package for FreeSewing patterns: npm init FreeSewing-pattern" -css-theme: "A CSS theme for FreeSewing web UIs" -diana: "A FreeSewing pattern for a top with a draped neck" -examples: "A FreeSewing pattern holding examples for our documentation" -florent: "A FreeSewing pattern for a flat cap" -fu: "A FreeSewing pattern for a face mask" -gatsby-remark-jargon: "A gatsby-transformer-remark sub-plugin for jargon terms" -holmes: "A FreeSewing pattern for a Sherlock Holmes hat" -huey: "A FreeSewing pattern for a zip-up hoodie" -hugo: "A FreeSewing pattern for a hooded jumper with raglan sleeves" -i18n: "Translations for the FreeSewing project" -jaeger: "A FreeSewing pattern for a sport coat style jacket" -models: "Body measurements data for a range of default sizes" -mui-theme: "A Material-UI theme for FreeSewing web UIs" -pattern-info: "Information about available freesewing patterns" -penelope: "A FreeSewing pattern for a pencil skirt" -plugin-banner: "A FreeSewing plugin to repeat text on a path" -plugin-bundle: "An umbrella package of 8 essential FreeSewing build-time plugins" -plugin-bust: "A FreeSewing plugin that helps with bust-adjusting menswear patterns" -plugin-buttons: "A FreeSewing plugin that provides button and buttonhole snippets" -plugin-cutonfold: "A FreeSewing plugin to add cut-on-fold indicators on your patterns" -plugin-debug: "A FreeSewing plugin to log debug info to your browser console" -plugin-designer: "A FreeSewing plugin to facilitate pattern design" -plugin-dimension: "A FreeSewing plugin to add dimensions to your (paperless) pattern" -plugin-flip: "A FreeSewing plugin to flip parts horizontally" -plugin-gore: "A FreeSewing plugin to generate gores for a semi-sphere or dome" -plugin-grainline: "A FreeSewing plugin to add grainline indicators on your patterns" -plugin-i18n: "A FreeSewing plugin for pattern translation" -plugin-logo: "A FreeSewing plugin to add our logo to your patterns" -plugin-round: "A FreeSewing plugin to round corners" -plugin-scalebox: "A FreeSewing plugin to add a scalebox to your pattern" -plugin-sprinkle: "A FreeSewing plugin to bulk-add snippets to your pattern" -plugin-svgattr: "A FreeSewing plugin to set SVG attributes" -plugin-theme: "A FreeSewing plugin that provides a default theme" -plugin-title: "A FreeSewing plugin to add a title to your pattern parts" -plugin-validate: "A FreeSewing plugin that validates aspects of your code" +aaron: 'A FreeSewing pattern for a A-shirt or tank top' +benjamin: 'A FreeSewing pattern for a bow tie' +bent: 'A FreeSewing pattern for a menswear body block with a two-part sleeve' +breanna: 'A FreeSewing pattern for a basic body block for womenswear' +brian: 'A FreeSewing pattern for a basic body block for menswear' +bruce: 'A FreeSewing pattern for boxer briefs' +carlita: 'A FreeSewing pattern for Sherlock Holmes cosplay; Or just a nice long coat' +carlton: 'A FreeSewing pattern for Sherlock Holmes cosplay; Or just a nice long coat' +cathrin: 'A FreeSewing pattern for a underbust corset / waist trainer' +components: 'A collection of React components for FreeSewing web UIs' +core: 'A library for creating made-to-measure sewing patterns' +create-freesewing-pattern: 'Initializer package for FreeSewing patterns: npm init FreeSewing-pattern' +css-theme: 'A CSS theme for FreeSewing web UIs' +diana: 'A FreeSewing pattern for a top with a draped neck' +examples: 'A FreeSewing pattern holding examples for our documentation' +florent: 'A FreeSewing pattern for a flat cap' +fu: 'A FreeSewing pattern for a face mask' +gatsby-remark-jargon: 'A gatsby-transformer-remark sub-plugin for jargon terms' +holmes: 'A FreeSewing pattern for a Sherlock Holmes hat' +huey: 'A FreeSewing pattern for a zip-up hoodie' +hugo: 'A FreeSewing pattern for a hooded jumper with raglan sleeves' +i18n: 'Translations for the FreeSewing project' +jaeger: 'A FreeSewing pattern for a sport coat style jacket' +models: 'Body measurements data for a range of default sizes' +mui-theme: 'A Material-UI theme for FreeSewing web UIs' +pattern-info: 'Information about available freesewing patterns' +penelope: 'A FreeSewing pattern for a pencil skirt' +plugin-banner: 'A FreeSewing plugin to repeat text on a path' +plugin-bundle: 'An umbrella package of 8 essential FreeSewing build-time plugins' +plugin-bust: 'A FreeSewing plugin that helps with bust-adjusting menswear patterns' +plugin-buttons: 'A FreeSewing plugin that provides button and buttonhole snippets' +plugin-cutonfold: 'A FreeSewing plugin to add cut-on-fold indicators on your patterns' +plugin-debug: 'A FreeSewing plugin to log debug info to your browser console' +plugin-designer: 'A FreeSewing plugin to facilitate pattern design' +plugin-dimension: 'A FreeSewing plugin to add dimensions to your (paperless) pattern' +plugin-export-dxf: 'A FreeSewing plugin to export your pattern as DXF-ASTM' +plugin-flip: 'A FreeSewing plugin to flip parts horizontally' +plugin-gore: 'A FreeSewing plugin to generate gores for a semi-sphere or dome' +plugin-grainline: 'A FreeSewing plugin to add grainline indicators on your patterns' +plugin-i18n: 'A FreeSewing plugin for pattern translation' +plugin-logo: 'A FreeSewing plugin to add our logo to your patterns' +plugin-round: 'A FreeSewing plugin to round corners' +plugin-scalebox: 'A FreeSewing plugin to add a scalebox to your pattern' +plugin-sprinkle: 'A FreeSewing plugin to bulk-add snippets to your pattern' +plugin-svgattr: 'A FreeSewing plugin to set SVG attributes' +plugin-theme: 'A FreeSewing plugin that provides a default theme' +plugin-title: 'A FreeSewing plugin to add a title to your pattern parts' +plugin-validate: 'A FreeSewing plugin that validates aspects of your code' prettier-config: "FreeSewing's shared configuration for prettier" -remark-jargon: "A Remark plugin for jargon terms" -rendertest: "A FreeSewing pattern to test (y)our render engine our CSS" -sandy: "A FreeSewing pattern for a circle skirt" -shin: "A FreeSewing pattern for swim trunks" -simon: "A FreeSewing pattern for a button down shirt" -simone: "A FreeSewing pattern for a button down shirt (Simone = Simon for people with breasts)" -sven: "A FreeSewing pattern for a straightforward sweater" -tamiko: "A FreeSewing pattern for a zero-waste top" -theo: "A FreeSewing pattern for classic trousers" -trayvon: "A FreeSewing pattern for a tie" +remark-jargon: 'A Remark plugin for jargon terms' +rendertest: 'A FreeSewing pattern to test (y)our render engine our CSS' +sandy: 'A FreeSewing pattern for a circle skirt' +shin: 'A FreeSewing pattern for swim trunks' +simon: 'A FreeSewing pattern for a button down shirt' +simone: 'A FreeSewing pattern for a button down shirt (Simone = Simon for people with breasts)' +sven: 'A FreeSewing pattern for a straightforward sweater' +tamiko: 'A FreeSewing pattern for a zero-waste top' +theo: 'A FreeSewing pattern for classic trousers' +trayvon: 'A FreeSewing pattern for a tie' tutorial: "A FreeSewing pattern for a baby bib that's used in our tutorial" -utils: "A collection of utilities shared across freesewing frontend projects" -wahid: "A FreeSewing pattern for a classic fitted waistcoat" -waralee: "A FreeSewing pattern for wrap pants" +utils: 'A collection of utilities shared across freesewing frontend projects' +wahid: 'A FreeSewing pattern for a classic fitted waistcoat' +waralee: 'A FreeSewing pattern for wrap pants' diff --git a/packages/plugin-export-dxf/CHANGELOG.md b/packages/plugin-export-dxf/CHANGELOG.md new file mode 100644 index 00000000000..cf992c04312 --- /dev/null +++ b/packages/plugin-export-dxf/CHANGELOG.md @@ -0,0 +1,108 @@ +# Change log for: @freesewing/plugin-export-dxf + +## Unreleased + +**Note:** Version bump only for package plugin-export-dxf + +## 2.5.0 (2020-04-05) + +**Note:** Version bump only for package plugin-export-dxf + +## 2.4.6 (2020-03-23) + +**Note:** Version bump only for package plugin-export-dxf + +## 2.4.5 (2020-03-19) + +**Note:** Version bump only for package plugin-export-dxf + +## 2.4.4 (2020-03-15) + +**Note:** Version bump only for package plugin-export-dxf + +## 2.4.3 (2020-03-12) + +**Note:** Version bump only for package plugin-export-dxf + +## 2.4.2 (2020-03-08) + +**Note:** Version bump only for package plugin-export-dxf + +## 2.4.1 (2020-03-04) + +**Note:** Version bump only for package plugin-export-dxf + +## 2.4.0 (2020-02-29) + +**Note:** Version bump only for package plugin-export-dxf + +## 2.3.0 (2020-02-23) + +**Note:** Version bump only for package plugin-export-dxf + +## 2.2.0 (2020-02-22) + +**Note:** Version bump only for package plugin-export-dxf + +## 2.1.9 (2020-01-18) + +**Note:** Version bump only for package plugin-export-dxf + +## 2.1.8 (2019-12-16) + +**Note:** Version bump only for package plugin-export-dxf + +## 2.1.7 (2019-12-15) + +**Note:** Version bump only for package plugin-export-dxf + +## 2.1.6 (2019-11-24) + +**Note:** Version bump only for package plugin-export-dxf + +## 2.1.5 (2019-11-19) + +**Note:** Version bump only for package plugin-export-dxf + +## 2.1.4 (2019-11-01) + +**Note:** Version bump only for package plugin-export-dxf + +## 2.1.3 (2019-10-18) + +**Note:** Version bump only for package plugin-export-dxf + +## 2.1.2 (2019-10-14) + +**Note:** Version bump only for package plugin-export-dxf + +## 2.1.1 (2019-10-13) + +**Note:** Version bump only for package plugin-export-dxf + +## 2.1.0 (2019-10-06) + +**Note:** Version bump only for package plugin-export-dxf + +## 2.0.4 (2019-09-27) + +**Note:** Version bump only for package plugin-export-dxf + +## 2.0.3 (2019-09-15) + +**Note:** Version bump only for package plugin-export-dxf + +## 2.0.2 (2019-09-06) + +**Note:** Version bump only for package plugin-export-dxf + +## 2.0.1 (2019-09-01) + +**Note:** Version bump only for package plugin-export-dxf + +## 2.0.0 (2019-08-25) + +This is the **initial release**, and the start of this change log. + +Prior to version 2, FreeSewing was not a JavaScript project. +As such, that history is out of scope for this change log. diff --git a/packages/plugin-export-dxf/README.md b/packages/plugin-export-dxf/README.md new file mode 100644 index 00000000000..5a51fb77b52 --- /dev/null +++ b/packages/plugin-export-dxf/README.md @@ -0,0 +1,185 @@ +![FreeSewing](https://freesewing.org/banner.jpg) + +

@freesewing/plugin-export-dxf on NPM + License: MIT + Code quality on DeepScan + Open issues tagged pkg:plugin-export-dxf +

Follow @freesewing_org on Twitter + Chat with us on Gitter + Become a FreeSewing Patron + Follow @freesewing_org on Twitter +

+ +## What am I looking at? 🤔 + +This repository is our _monorepo_ holding [all our NPM packages](https://www.npmjs.com/search?q=keywords:freesewing). +This folder holds **@freesewing/plugin-export-dxf** + +A FreeSewing plugin to export your pattern as DXF-ASTM + +## About + +This plugin adds the ability to export patterns to DXF-ASTM. + +DXF (Drawing interchange format) is a file format developed by Autodesk +(of AutoCAD® fame). +The DXF-ASTM variety is a subset of the format, specifically targetted +at the garment industry. + +ASTM is the _American Society for Testing and Materials_ — a standards body — +that published the format. +DXF-ASTM is the successor of DXF-AAMA which was developed by the _American +Apparel Manufacturers Association_ which reveals the origins of the file format. + +## Usage + +Instantiate your pattern, and use the plugin. +It will add the `exportDxf()` method to the pattern object. +This method will return the DXF-ASTM output. + +```js +const models import '@freesewing/models' +const Aaron import '@freesewing/aaron' +const exportDxfPlugin = import '@freesewing/plugin-export-dxf' + +const settings = { + // Make sure to set complete to false + complete: false, + measurements: models.withoutBreasts.size42 +} + +let dxf = new Aaron(settings) + .use(exportDxfPlugin) + .draft() + .exportDxf() +``` + +## Configuration + +This plugin takes a configuration object as a second parameter to the +`pattern.use()` method. + +### Precision + +The precision property determines the length of the line segments used +to approximate curves. The generated DXF-ASTM output will only contain +straight lines, so curves will be approximated wiht lines segments. + +The `precision` sets the length of those segments in mm. +In the example below, the `precision` is set to `25` resulting in +the use of line segments 25mm (1 inch) to approximate the curve. + +```js +let config = { + precision: 25 +} + +let dxf = new Aaron(settings).use(exportDxfPlugin, config) +``` + +The default `precision` is `1`, giving you 1mm long line segments +to approximate curves. + +## Reasons to use this plugin + +This plugin can export your pattern to DXF-ASTM so you can import it to +your 3D software of choice. + +## Reasons to not use this plugin + +### Because DXF is inferior to SVG in every way + +DXF (and DXF-AAMA and DXF-ASTM with it) are rooted in the world of industrial +manufacturing. And it shows. + +Nowadays, it's easy to think of the DXF file format as a bit of an embarassment. +But it is deliberatly kept dumb so that old industrial CNC milling machines, +plotters, laster cutters and whatnot, can handle the format. + +### Because this plugin does not implement all of DXF + +This plugin does the minimum to allow export of FreeSewing patterns into +3D garment tools. It expexts the input pattern to only include the outlines. + +That means, no seam allowance, or titles, no notches, and so on. + +## About FreeSewing 💀 + +Where the world of makers and developers collide, that's where you'll find FreeSewing. + +Our [core library](https://freesewing.dev/reference/api/) is a _batteries-included_ toolbox +for parametric design of sewing patterns. It's a modular system (check our list +of [plugins](https://freesewing.dev/reference/plugins/) and getting started is as simple as: + +```bash +npm init freesewing-pattern +``` + +The [getting started](https://freesewing.dev/guides/getting-started/) section on [freesewing.dev](https://freesewing.dev/) is a good +entrypoint to our documentation, but you'll find a lot more there, including +our [API reference](https://freesewing.dev/reference/api/), +as well as [our turorial](https://freesewing.dev/tutorials/pattern-design/), +and [howtos](https://freesewing.dev/howtos/). + +If you're a maker, checkout [freesewing.org](https://freesewing/) where you can generate +our sewing patterns adapted to your measurements. + +## Support FreeSewing: Become a patron 🥰 + +FreeSewing is an open source project run by a community, +and financially supported by our patrons. + +If you feel what we do is worthwhile, you too +should [become a patron](https://freesewing.org/patrons/join). + +## Links 👩‍💻 + +- 💻 Makers website: [freesewing.org](https://freesewing.org) +- 💻 Developers website: [freesewing.dev](https://freesewing.dev) +- 💬 Chat: [gitter.im/freesewing](https://gitter.im/freesewing/chat) +- 🐦 Twitter: [@freesewing_org](https://twitter.com/freesewing_org) +- 📷 Instagram: [@freesewing_org](https://instagram.com/freesewing_org) + +## License: MIT 🤓 + +© [Joost De Cock](https://github.com/joostdecock). +See [the license file](https://github.com/freesewing/freesewing/blob/develop/LICENSE) for details. + +## Where to get help 🤯 + +Our [chatroom on Gitter](https://gitter.im/freesewing/chat) is the best place to ask questions, +share your feedback, or just hang out. + +If you want to report a problem, please [create an issue](https://github.com/freesewing/freesewing/issues/new). diff --git a/packages/plugin-export-dxf/info.md b/packages/plugin-export-dxf/info.md new file mode 100644 index 00000000000..1c74efafad8 --- /dev/null +++ b/packages/plugin-export-dxf/info.md @@ -0,0 +1,85 @@ +## About + +This plugin adds the ability to export patterns to DXF-ASTM. + +DXF (Drawing interchange format) is a file format developed by Autodesk +(of AutoCAD® fame). +The DXF-ASTM variety is a subset of the format, specifically targetted +at the garment industry. + +ASTM is the _American Society for Testing and Materials_ — a standards body — +that published the format. +DXF-ASTM is the successor of DXF-AAMA which was developed by the _American +Apparel Manufacturers Association_ which reveals the origins of the file format. + +## Usage + +Instantiate your pattern, and use the plugin. +It will add the `exportDxf()` method to the pattern object. +This method will return the DXF-ASTM output. + +```js +const models import '@freesewing/models' +const Aaron import '@freesewing/aaron' +const exportDxfPlugin = import '@freesewing/plugin-export-dxf' + +const settings = { + // Make sure to set complete to false + complete: false, + measurements: models.withoutBreasts.size42 +} + +let dxf = new Aaron(settings) + .use(exportDxfPlugin) + .draft() + .exportDxf() +``` + +## Configuration + +This plugin takes a configuration object as a second parameter to the +`pattern.use()` method. + +### Precision + +The precision property determines the length of the line segments used +to approximate curves. The generated DXF-ASTM output will only contain +straight lines, so curves will be approximated wiht lines segments. + +The `precision` sets the length of those segments in mm. +In the example below, the `precision` is set to `25` resulting in +the use of line segments 25mm (1 inch) to approximate the curve. + +```js +let config = { + precision: 25 +} + +let dxf = new Aaron(settings).use(exportDxfPlugin, config) +``` + +The default `precision` is `1`, giving you 1mm long line segments +to approximate curves. + +## Reasons to use this plugin + +This plugin can export your pattern to DXF-ASTM so you can import it to +your 3D software of choice. + +## Reasons to not use this plugin + +### Because DXF is inferior to SVG in every way + +DXF (and DXF-AAMA and DXF-ASTM with it) are rooted in the world of industrial +manufacturing. And it shows. + +Nowadays, it's easy to think of the DXF file format as a bit of an embarassment. +But it is deliberatly kept dumb so that old industrial CNC milling machines, +plotters, laster cutters and whatnot, can handle the format. + +### Because this plugin does not implement all of DXF + +This plugin does the minimum to allow export of FreeSewing patterns into +3D garment tools. It expexts the input pattern to only include the outlines. + +That means, no seam allowance, or titles, no notches, and so on. diff --git a/packages/plugin-export-dxf/package.json b/packages/plugin-export-dxf/package.json new file mode 100644 index 00000000000..1dd1bf100e8 --- /dev/null +++ b/packages/plugin-export-dxf/package.json @@ -0,0 +1,52 @@ +{ + "name": "@freesewing/plugin-export-dxf", + "version": "2.5.0", + "description": "A FreeSewing plugin to export your pattern as DXF-ASTM", + "author": "Joost De Cock (https://github.com/joostdecock)", + "homepage": "https://freesewing.org/", + "repository": "github:freesewing/freesewing", + "license": "MIT", + "bugs": { + "url": "https://github.com/freesewing/freesewing/issues" + }, + "keywords": [ + "freesewing", + "plugin", + "sewing pattern", + "sewing", + "design", + "parametric design", + "made to measure", + "diy", + "fashion" + ], + "main": "dist/index.js", + "module": "dist/index.mjs", + "scripts": { + "clean": "rimraf dist", + "build": "npm run clean && rollup -c", + "test": "echo \"plugin-export-dxf: No tests configured. Perhaps you'd like to do this?\" && exit 0", + "pubtest": "npm publish --registry http://localhost:6662", + "pubforce": "npm publish", + "symlink": "mkdir -p ./node_modules/@freesewing && cd ./node_modules/@freesewing && ln -s -f ../../../* . && cd -", + "start": "rollup -c -w" + }, + "peerDependencies": { + "@freesewing/core": "^2.5.0" + }, + "dependencies": {}, + "devDependencies": {}, + "files": [ + "dist/*", + "README.md", + "package.json" + ], + "publishConfig": { + "access": "public", + "tag": "latest" + }, + "engines": { + "node": ">=8.0.0", + "npm": ">=5" + } +} diff --git a/packages/plugin-export-dxf/rollup.config.js b/packages/plugin-export-dxf/rollup.config.js new file mode 100644 index 00000000000..142439a7f29 --- /dev/null +++ b/packages/plugin-export-dxf/rollup.config.js @@ -0,0 +1,41 @@ +import babel from 'rollup-plugin-babel' +import resolve from 'rollup-plugin-node-resolve' +import commonjs from 'rollup-plugin-commonjs' +import json from 'rollup-plugin-json' +import minify from 'rollup-plugin-babel-minify' +import peerDepsExternal from 'rollup-plugin-peer-deps-external' +import { name, version, description, author, license, main, module } from './package.json' + +const output = [ + { + file: main, + format: 'cjs', + sourcemap: true + } +] +if (typeof module !== 'undefined') + output.push({ + file: module, + format: 'es', + sourcemap: true + }) + +export default { + input: 'src/index.js', + output, + plugins: [ + peerDepsExternal(), + resolve({ modulesOnly: true }), + commonjs(), + json(), + babel({ + exclude: 'node_modules/**', + plugins: ['@babel/plugin-proposal-object-rest-spread'] + }), + minify({ + comments: false, + sourceMap: true, + banner: `/**\n * ${name} | v${version}\n * ${description}\n * (c) ${new Date().getFullYear()} ${author}\n * @license ${license}\n */` + }) + ] +} diff --git a/packages/plugin-export-dxf/src/dxf.js b/packages/plugin-export-dxf/src/dxf.js new file mode 100644 index 00000000000..197fa390221 --- /dev/null +++ b/packages/plugin-export-dxf/src/dxf.js @@ -0,0 +1,243 @@ +import { name, version, description, author, license } from '../package.json' + +function Dxf(config) { + this.config = config +} + +// Round to 2 decimals because DXF is stupid +Dxf.prototype.round = function (val) { + return Math.round(val * 100) / 100 +} + +/** Returns DXF code for optional banner */ +Dxf.prototype.banner = function (pattern) { + return `999 +${name.slice(1)} | v${version} +999 +${description} +999 +(c) ${new Date().getFullYear()} ${author} +999 +License: ${license} +999 +Pattern: ${pattern.config.name} | v${pattern.config.version} +999 +Export date: ${new Date().toISOString()}` +} + +/** Returns DXF code for tables */ +Dxf.prototype.tables = function (tables) { + let dxf = ` + 0 +SECTION + 2 +TABLES` + for (let lineType of tables.lineTypes) + dxf += ` + 0 +TABLE + 2 +LTYPE + 0 +LTYPE + 2 +${lineType.name} + 3 +${lineType.description} + 72 +65 + 73 +0 + 40 +0.00 + 0 +ENDTAB` + for (let layer of tables.layers) + dxf += ` + 0 +TABLE + 2 +LAYER + 0 +LAYER + 2 +${layer.name} + 62 +${layer.color} + 6 +${layer.lineType} + 0 +ENDTAB` + dxf += ` + 0 +ENDSEC` + + return dxf +} + +/** Returns DXF code to close/end a DXF file */ +Dxf.prototype.footer = function () { + return ` + 0 +EOF +` +} + +/** Returns DXF code for a line */ +Dxf.prototype.line = function (to, layer) { + return ` + 0 +VERTEX + 8 +${layer} + 10 +${this.round(to.x)} + 20 +${this.round(to.y)}` +} + +/** Returns DXF code for a curve */ +Dxf.prototype.curve = function (from, cp1, cp2, to, layer, part) { + let { Path } = part.shorthand() + let path = new Path().move(from).curve(cp1, cp2, to) + let steps = Math.floor(path.length() / this.config.precision) + let dxf, current + for (let i = 1; i <= steps; i++) { + current = path.shiftAlong(i * this.config.precision) + dxf += this.line(current, layer) + } + if (current.dist(to) > 0.1) dxf += this.line(to, layer) + + return dxf +} + +/** Returns DXF code for a Path object */ +Dxf.prototype.path = function (path, layer, part) { + let dxf = ` + 0 +POLYLINE + 8 +${layer} + 70 +1` + let current, start + for (let op of path.ops) { + switch (op.type) { + case 'move': + start = op.to + dxf += this.line(op.to, layer) + break + case 'line': + dxf += this.line(op.to, layer) + break + case 'curve': + dxf += this.curve(current, op.cp1, op.cp2, op.to, layer, part) + break + case 'close': + dxf += this.line(start, layer) + break + default: + throw new Error(`Unsupported path operation: ${op.type}`) + } + current = op.to + } + dxf += ` + 0 +SEQEND` + + return dxf +} + +/** Returns blocs portion of the DXF code for a Part object */ +Dxf.prototype.partBlocks = function (part, name, layer = 1) { + let dxf = ` + 0 +BLOCK + 8 +${layer} + 2 +${name} + 70 +0 + 10 +0.00 + 20 +0.00` + for (let key in part.paths) { + let path = part.paths[key] + if (path.render) dxf += this.path(path, layer, part) + } + dxf += ` + 0 +ENDBLK` + + return dxf +} + +/** Returns entities portion of the DXF code for a Part object */ +Dxf.prototype.partEntities = function (part, name, layer = 1) { + return ` + 0 +INSERT + 8 +${layer} + 2 +${name} + 10 +0.00 + 20 +0.00` +} + +/** Exports (drafted) pattern as DXF-ASTM */ +Dxf.prototype.render = function (pattern) { + // Ensure pattern layout + pattern.pack() + + // Tables structure + let tables = { + lineTypes: [ + { + name: 'CONTINUOUS', + description: 'SOLIDLINE' + } + ], + layers: [ + { + name: 1, + color: 7, + lineType: 'CONTINIOUS' + } + ] + } + + let dxf = '' + dxf += this.banner(pattern) + dxf += this.tables(tables) + dxf += ` + 0 +SECTION + 2 +BLOCKS` + for (let partId in pattern.parts) { + if (pattern.parts[partId].render) dxf += this.partBlocks(pattern.parts[partId], partId) + } + dxf += ` + 0 +ENDSEC + 0 +SECTION + 2 +ENTITIES` + for (let partId in pattern.parts) { + if (pattern.parts[partId].render) dxf += this.partEntities(pattern.parts[partId], partId) + } + dxf += ` + 0 +ENDSEC` + dxf += this.footer() + + return dxf +} + +export default Dxf diff --git a/packages/plugin-export-dxf/src/footer.js b/packages/plugin-export-dxf/src/footer.js new file mode 100644 index 00000000000..450757d19e9 --- /dev/null +++ b/packages/plugin-export-dxf/src/footer.js @@ -0,0 +1,5 @@ +const footer = ` +0 +EOF +` +export default footer diff --git a/packages/plugin-export-dxf/src/header.js b/packages/plugin-export-dxf/src/header.js new file mode 100644 index 00000000000..629927a4ddc --- /dev/null +++ b/packages/plugin-export-dxf/src/header.js @@ -0,0 +1,13 @@ +import { name, version, description, author, license } from '../package.json' + +const header = `999 +${name.slice(1)} | v${version} +999 +${description} +999 +(c) ${new Date().getFullYear()} ${author} +999 +License: ${license} +999` + +export default header diff --git a/packages/plugin-export-dxf/src/index.js b/packages/plugin-export-dxf/src/index.js new file mode 100644 index 00000000000..9b903cdabd4 --- /dev/null +++ b/packages/plugin-export-dxf/src/index.js @@ -0,0 +1,13 @@ +import { name, version } from '../package.json' + +import Dxf from './dxf' + +export default { + name: name, + version: version, + hooks: { + postDraft: (pattern, config = { precision: 1 }) => { + pattern.exportDxf = () => new Dxf(config).render(pattern) + } + } +} diff --git a/packages/plugin-export-dxf/tests/bust.test.js b/packages/plugin-export-dxf/tests/bust.test.js new file mode 100644 index 00000000000..2fea0f2b7a3 --- /dev/null +++ b/packages/plugin-export-dxf/tests/bust.test.js @@ -0,0 +1,12 @@ +import freesewing from 'freesewing' +import { version } from '../package.json' +let chai = require('chai') +let expect = chai.expect +chai.use(require('chai-string')) +let plugin = require('../dist/index.js') + +it('Should set the plugin name:version attribute', () => { + let pattern = new freesewing.Pattern() + pattern.use(plugin).draft().render() + expect(pattern.svg.attributes.get('freesewing:plugin-buttons')).to.equal(version) +})