Как да използвате Aspose.CAD в TypeScript
Предварителни условия
- Visual Code + Live Server
- Node.js
Преобразуване на dgn изображение в png и показване в браузъра
В този пример ще създадете проста програма за преобразуване, която преобразува чертеж и го запазва като изображение.
Създаване на JavaScript
Създайте package.json в папката на проекта
npm init -y
Променете package.json, добавете зависимости на aspose-cad
"dependencies": { "aspose-cad": "^23.1.0" }, "devDependencies": { "live-server": "^1.2.1", "typescript": "^3.3.3333", "yarn": "^1.22.19" }
Създайте index.ts
import { PngOptions, Image } from './node_modules/aspose-cad/es2015/index.js'; declare let window: any; window.processDrawing = async function processDrawing(array: Uint8Array): Promise<any> { //ВЗЕМИ_ФОРМАТ_НА_ФАЙЛА var fileFormat = Image.getFileFormat(array); console.log(fileFormat); // ЗАРЕЖДАНЕ var file = Image.load(array); console.log(file); // ЗАПАЗВАНЕ var exportedFilePromise = Image.save(array, new PngOptions()); return await exportedFilePromise.then(exportedFile => { console.log(exportedFile); return exportedFile; }); }
Използвайте npm командата за създаване на index.js
tsc
Създайте index.html
<!DOCTYPE html> Отворете конзолата (Ctrl+Shift+I), за да видите изхода. <script src="./node_modules/aspose-cad/dotnet.js"></script> <script type="module" src="./node_modules/aspose-cad/es2015/index-js.js"></script> <body> <input id="file" type="file"> <img id="image" /> </body> <script> window.onload = async function () { document.querySelector('input').addEventListener('change', function() { var reader = new FileReader(); reader.onload = function() { var arrayBuffer = this.result; var array = new Uint8Array(arrayBuffer); //ВЗЕМИ_ФОРМАТ_НА_ФАЙЛА fileFormat = Aspose.CAD.Image.getFileFormat(array); console.log(fileFormat); // ЗАРЕЖДАНЕ file = Aspose.CAD.Image.load(array); console.log(file); // ЗАПАЗВАНЕ exportedFilePromise = Aspose.CAD.Image.save(array, new Aspose.CAD.PngOptions()); exportedFilePromise.then(exportedFile => { console.log(exportedFile); var urlCreator = window.URL || window.webkitURL; var blob = new Blob([exportedFile], { type: 'application/octet-stream' }); var imageUrl = urlCreator.createObjectURL(blob); document.querySelector("#image").src = imageUrl; }); } reader.readAsArrayBuffer(this.files[0]); }, false); }; </script>
Инсталирайте пакетите, използвайки npm командата
npm install
Стартирайте приложението с Live Server или yarn
npm run serve
Пример за изпълнение
- Изберете файл.
- Изберете всякакъв DXF, DWG, DGN, DWF, DWFX, IFC, STL, DWT, IGES, PLT, CF2, OBJ, HPGL, IGS, PCL, FBX, PDF, SVG файл.
- Ако отговорът е успешен, файлът ще бъде показан на екрана и ще предложи да го изтеглите.