From f62fb8aa86929a501e93e9db4f019bbd181ed937 Mon Sep 17 00:00:00 2001 From: Val Erastov Date: Sun, 4 Dec 2022 14:33:18 -0800 Subject: [PATCH] add the DXF import button --- modules/ui/fileUploader.ts | 21 ++++++++++++ package-lock.json | 28 +++++++++++---- .../projectManager/projectManagerBundle.ts | 34 ++++++------------- web/app/sketcher/actions/commonActions.js | 31 ++++++++++++++++- 4 files changed, 82 insertions(+), 32 deletions(-) create mode 100644 modules/ui/fileUploader.ts diff --git a/modules/ui/fileUploader.ts b/modules/ui/fileUploader.ts new file mode 100644 index 00000000..699060fd --- /dev/null +++ b/modules/ui/fileUploader.ts @@ -0,0 +1,21 @@ + +export function uploadFile(cb: ((fileName: string, text: string) => void)) { + const uploader = document.createElement('input'); + uploader.setAttribute('type', 'file'); + uploader.style.display = 'none'; + + document.body.appendChild(uploader); + uploader.click(); + function read() { + const reader = new FileReader(); + reader.onload = () => { + try { + cb(uploader.value, reader.result as string); + } finally { + document.body.removeChild(uploader); + } + }; + reader.readAsText(uploader.files[0]); + } + uploader.addEventListener('change', read, false); +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 73f73232..34d302f2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,8 @@ "version": "0.1.0", "license": "SEE LICENSE IN LICENSE", "dependencies": { - "@tarikjabiri/dxf": "^2.3.2", + "@dxfjs/parser": "^0.0.2", + "@tarikjabiri/dxf": "^2.6.1", "@types/three": "^0.143.0", "browser-xml2js": "^0.4.19", "classnames": "2.2.5", @@ -2487,6 +2488,14 @@ "node": ">=10.0.0" } }, + "node_modules/@dxfjs/parser": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/@dxfjs/parser/-/parser-0.0.2.tgz", + "integrity": "sha512-CpWIzTV+t89MCtsLVeqRL4dNhioWjRFxVyPX0sgPKuIGr9VbXUZ89772WgdrFiSZqPXWGaZ4DaKyt78StgTunQ==", + "funding": { + "url": "https://github.com/sponsors/dxfjs" + } + }, "node_modules/@electron/get": { "version": "1.14.1", "resolved": "https://registry.npmjs.org/@electron/get/-/get-1.14.1.tgz", @@ -3044,9 +3053,9 @@ } }, "node_modules/@tarikjabiri/dxf": { - "version": "2.3.4", - "resolved": "https://registry.npmjs.org/@tarikjabiri/dxf/-/dxf-2.3.4.tgz", - "integrity": "sha512-ChgsslM6XI3EAYxSwt2BBsqoIZ1RRO/DEuiAVHyaRohXZoeE36iyGnrwPa63ILw7OJLirwO1nimBtCfAimBOhw==", + "version": "2.6.1", + "resolved": "https://registry.npmjs.org/@tarikjabiri/dxf/-/dxf-2.6.1.tgz", + "integrity": "sha512-/1APUzakzUQp7uJdg3qJwz0Pffx1fQq1plLyuqMFGO643iWqoqba8d5Y+B5jpEqwNiuX+/BQUZFwMHDbH3LRhQ==", "funding": { "url": "https://github.com/sponsors/dxfjs" } @@ -17650,6 +17659,11 @@ "integrity": "sha512-dBVuXR082gk3jsFp7Rd/JI4kytwGHecnCoTtXFb7DB6CNHp4rg5k1bhg0nWdLGLnOV71lmDzGQaLMy8iPLY0pw==", "dev": true }, + "@dxfjs/parser": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/@dxfjs/parser/-/parser-0.0.2.tgz", + "integrity": "sha512-CpWIzTV+t89MCtsLVeqRL4dNhioWjRFxVyPX0sgPKuIGr9VbXUZ89772WgdrFiSZqPXWGaZ4DaKyt78StgTunQ==" + }, "@electron/get": { "version": "1.14.1", "resolved": "https://registry.npmjs.org/@electron/get/-/get-1.14.1.tgz", @@ -18084,9 +18098,9 @@ } }, "@tarikjabiri/dxf": { - "version": "2.3.4", - "resolved": "https://registry.npmjs.org/@tarikjabiri/dxf/-/dxf-2.3.4.tgz", - "integrity": "sha512-ChgsslM6XI3EAYxSwt2BBsqoIZ1RRO/DEuiAVHyaRohXZoeE36iyGnrwPa63ILw7OJLirwO1nimBtCfAimBOhw==" + "version": "2.6.1", + "resolved": "https://registry.npmjs.org/@tarikjabiri/dxf/-/dxf-2.6.1.tgz", + "integrity": "sha512-/1APUzakzUQp7uJdg3qJwz0Pffx1fQq1plLyuqMFGO643iWqoqba8d5Y+B5jpEqwNiuX+/BQUZFwMHDbH3LRhQ==" }, "@tootallnate/once": { "version": "2.0.0", diff --git a/web/app/cad/projectManager/projectManagerBundle.ts b/web/app/cad/projectManager/projectManagerBundle.ts index 2f75409b..56784837 100644 --- a/web/app/cad/projectManager/projectManagerBundle.ts +++ b/web/app/cad/projectManager/projectManagerBundle.ts @@ -5,35 +5,21 @@ import {SketchFormat_V3} from "sketcher/io"; import {ApplicationContext} from "cad/context"; import {OperationRequest} from "../craft/craftBundle"; import {AssemblyConstraintDefinition} from "cad/assembly/assemblyConstraint"; +import {uploadFile} from "ui/fileUploader"; export function activate(ctx: ApplicationContext) { function importProjectImpl(getId, onDone) { - const uploader = document.createElement('input'); - uploader.setAttribute('type', 'file'); - uploader.style.display = 'none'; - - document.body.appendChild(uploader); - uploader.click(); - function read() { - const reader = new FileReader(); - reader.onload = () => { - try { - const bundle = JSON.parse(reader.result as string); - const projectId = getId(uploader.value, bundle); - - if (projectId) { - importBundle(projectId, bundle); - onDone(projectId); - } - } finally { - document.body.removeChild(uploader); - } - }; - reader.readAsText(uploader.files[0]); - } - uploader.addEventListener('change', read, false); + uploadFile((fileName, content) => { + const bundle = JSON.parse(content as string); + const projectId = getId(fileName, bundle); + + if (projectId) { + importBundle(projectId, bundle); + onDone(projectId); + } + }); } function importBundle(projectId: string, bundle: ModelBundle) { diff --git a/web/app/sketcher/actions/commonActions.js b/web/app/sketcher/actions/commonActions.js index 66f4bb77..e1b0c47d 100644 --- a/web/app/sketcher/actions/commonActions.js +++ b/web/app/sketcher/actions/commonActions.js @@ -1,6 +1,16 @@ import {MdZoomOutMap} from "react-icons/md"; -import {AiOutlineCopy, AiOutlineExport, AiOutlineFile, AiOutlineFolderOpen, AiOutlineSave} from "react-icons/ai"; +import { + AiOutlineCopy, + AiOutlineExport, + AiOutlineFile, + AiOutlineFolderOpen, + AiOutlineImport, + AiOutlineSave +} from "react-icons/ai"; import {NoIcon} from "../icons/NoIcon"; +import {uploadFile} from "ui/fileUploader"; +import {DxfParserAdapter} from "sketcher/dxf"; +import {SketchFormat_V3} from "sketcher/io"; export default [ @@ -43,6 +53,25 @@ export default [ } }, + { + id: 'Import', + shortName: 'Import', + kind: 'Common', + description: 'Import from other formats', + icon: AiOutlineImport, + + invoke: async (ctx, e) => { + uploadFile((fileName, content) => { + + + const dxfParserAdapter = new DxfParserAdapter(); + dxfParserAdapter.parse(content).then(sketch => { + ctx.viewer.io._loadSketch(sketch); + }).catch(console.error); + }); + } + }, + { id: 'Save', shortName: 'Save',