add the DXF import button

This commit is contained in:
Val Erastov 2022-12-04 14:33:18 -08:00
parent 2fa2866296
commit f62fb8aa86
4 changed files with 82 additions and 32 deletions

View file

@ -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);
}

28
package-lock.json generated
View file

@ -9,7 +9,8 @@
"version": "0.1.0", "version": "0.1.0",
"license": "SEE LICENSE IN LICENSE", "license": "SEE LICENSE IN LICENSE",
"dependencies": { "dependencies": {
"@tarikjabiri/dxf": "^2.3.2", "@dxfjs/parser": "^0.0.2",
"@tarikjabiri/dxf": "^2.6.1",
"@types/three": "^0.143.0", "@types/three": "^0.143.0",
"browser-xml2js": "^0.4.19", "browser-xml2js": "^0.4.19",
"classnames": "2.2.5", "classnames": "2.2.5",
@ -2487,6 +2488,14 @@
"node": ">=10.0.0" "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": { "node_modules/@electron/get": {
"version": "1.14.1", "version": "1.14.1",
"resolved": "https://registry.npmjs.org/@electron/get/-/get-1.14.1.tgz", "resolved": "https://registry.npmjs.org/@electron/get/-/get-1.14.1.tgz",
@ -3044,9 +3053,9 @@
} }
}, },
"node_modules/@tarikjabiri/dxf": { "node_modules/@tarikjabiri/dxf": {
"version": "2.3.4", "version": "2.6.1",
"resolved": "https://registry.npmjs.org/@tarikjabiri/dxf/-/dxf-2.3.4.tgz", "resolved": "https://registry.npmjs.org/@tarikjabiri/dxf/-/dxf-2.6.1.tgz",
"integrity": "sha512-ChgsslM6XI3EAYxSwt2BBsqoIZ1RRO/DEuiAVHyaRohXZoeE36iyGnrwPa63ILw7OJLirwO1nimBtCfAimBOhw==", "integrity": "sha512-/1APUzakzUQp7uJdg3qJwz0Pffx1fQq1plLyuqMFGO643iWqoqba8d5Y+B5jpEqwNiuX+/BQUZFwMHDbH3LRhQ==",
"funding": { "funding": {
"url": "https://github.com/sponsors/dxfjs" "url": "https://github.com/sponsors/dxfjs"
} }
@ -17650,6 +17659,11 @@
"integrity": "sha512-dBVuXR082gk3jsFp7Rd/JI4kytwGHecnCoTtXFb7DB6CNHp4rg5k1bhg0nWdLGLnOV71lmDzGQaLMy8iPLY0pw==", "integrity": "sha512-dBVuXR082gk3jsFp7Rd/JI4kytwGHecnCoTtXFb7DB6CNHp4rg5k1bhg0nWdLGLnOV71lmDzGQaLMy8iPLY0pw==",
"dev": true "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": { "@electron/get": {
"version": "1.14.1", "version": "1.14.1",
"resolved": "https://registry.npmjs.org/@electron/get/-/get-1.14.1.tgz", "resolved": "https://registry.npmjs.org/@electron/get/-/get-1.14.1.tgz",
@ -18084,9 +18098,9 @@
} }
}, },
"@tarikjabiri/dxf": { "@tarikjabiri/dxf": {
"version": "2.3.4", "version": "2.6.1",
"resolved": "https://registry.npmjs.org/@tarikjabiri/dxf/-/dxf-2.3.4.tgz", "resolved": "https://registry.npmjs.org/@tarikjabiri/dxf/-/dxf-2.6.1.tgz",
"integrity": "sha512-ChgsslM6XI3EAYxSwt2BBsqoIZ1RRO/DEuiAVHyaRohXZoeE36iyGnrwPa63ILw7OJLirwO1nimBtCfAimBOhw==" "integrity": "sha512-/1APUzakzUQp7uJdg3qJwz0Pffx1fQq1plLyuqMFGO643iWqoqba8d5Y+B5jpEqwNiuX+/BQUZFwMHDbH3LRhQ=="
}, },
"@tootallnate/once": { "@tootallnate/once": {
"version": "2.0.0", "version": "2.0.0",

View file

@ -5,35 +5,21 @@ import {SketchFormat_V3} from "sketcher/io";
import {ApplicationContext} from "cad/context"; import {ApplicationContext} from "cad/context";
import {OperationRequest} from "../craft/craftBundle"; import {OperationRequest} from "../craft/craftBundle";
import {AssemblyConstraintDefinition} from "cad/assembly/assemblyConstraint"; import {AssemblyConstraintDefinition} from "cad/assembly/assemblyConstraint";
import {uploadFile} from "ui/fileUploader";
export function activate(ctx: ApplicationContext) { export function activate(ctx: ApplicationContext) {
function importProjectImpl(getId, onDone) { function importProjectImpl(getId, onDone) {
const uploader = document.createElement('input');
uploader.setAttribute('type', 'file');
uploader.style.display = 'none';
document.body.appendChild(uploader); uploadFile((fileName, content) => {
uploader.click(); const bundle = JSON.parse(content as string);
function read() { const projectId = getId(fileName, bundle);
const reader = new FileReader();
reader.onload = () => {
try {
const bundle = JSON.parse(reader.result as string); if (projectId) {
const projectId = getId(uploader.value, bundle); importBundle(projectId, bundle);
onDone(projectId);
if (projectId) { }
importBundle(projectId, bundle); });
onDone(projectId);
}
} finally {
document.body.removeChild(uploader);
}
};
reader.readAsText(uploader.files[0]);
}
uploader.addEventListener('change', read, false);
} }
function importBundle(projectId: string, bundle: ModelBundle) { function importBundle(projectId: string, bundle: ModelBundle) {

View file

@ -1,6 +1,16 @@
import {MdZoomOutMap} from "react-icons/md"; 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 {NoIcon} from "../icons/NoIcon";
import {uploadFile} from "ui/fileUploader";
import {DxfParserAdapter} from "sketcher/dxf";
import {SketchFormat_V3} from "sketcher/io";
export default [ 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', id: 'Save',
shortName: 'Save', shortName: 'Save',