import { dataService } from "./Service/DataService.js"; import { lineService } from "./Service/LineService.js"; import { pointService } from "./Service/PointService.js"; import { imageService } from "./Service/ImageService.js"; import VectorCategory from "./enum/VectorCategory.js"; import { coordinate } from "./Coordinate.js"; import Settings from "./Settings"; import { circleService } from "./Service/CircleService.js"; import { magnifierService } from "./Service/MagnifierService.js"; import { textService } from "./Service/TextService.js"; export default class Load { constructor(layer) { this.layer = layer; this.version = "v1.0"; this.vectorsJson = null; this.newVectorId = null; } async load(dataLocal, data3d) { this.layer.initLocation(); if (dataLocal) { if (dataLocal.backgroundImg) { let bgImg = imageService.create( dataLocal.backgroundImg.src, dataLocal.backgroundImg.vectorId ); bgImg.setCenter(dataLocal.backgroundImg.center); bgImg.setDisplay(dataLocal.backgroundImg.display); bgImg.setAngle(dataLocal.backgroundImg.angle); try { if (dataLocal.backgroundImg.src) { await bgImg.setImageData(); } } catch (e) {} } if (dataLocal.circles) { for (let key in dataLocal.circles) { let circle = circleService.create( dataLocal.circles[key].center, dataLocal.circles[key].radius, key ); circle.setPoints(dataLocal.circles[key].points); circle.setColor(dataLocal.circles[key].color); circle.setDisplay(dataLocal.circles[key].display); } } if (dataLocal.magnifiers) { for (let key in dataLocal.magnifiers) { let magnifier = magnifierService.create( dataLocal.magnifiers[key].position, key ); magnifier.setSrc(dataLocal.magnifiers[key].photoUrl); magnifier.setDisplay(dataLocal.magnifiers[key].display); try { if (dataLocal.magnifiers[key].photoUrl) { await magnifier.setImageData(); } } catch (e) {} } } if (dataLocal.texts) { for (let key in dataLocal.texts) { let text = textService.create(dataLocal.texts[key].center, key); text.setValue(dataLocal.texts[key].value); text.setFontSize(dataLocal.texts[key].fontSize); text.setColor(dataLocal.texts[key].color); text.setDisplay(dataLocal.texts[key].display); } } if (dataLocal.points) { for (let key in dataLocal.points) { let point = pointService.create(dataLocal.points[key], key); point.setCategory(dataLocal.points[key].category); point.setParent( JSON.parse(JSON.stringify(dataLocal.points[key].parent)) ); point.setDisplay(dataLocal.points[key].display); } } if (dataLocal.lines) { for (let key in dataLocal.lines) { let line = lineService.createByPointId( dataLocal.lines[key].startId, dataLocal.lines[key].endId, dataLocal.lines[key].category, key ); if (dataLocal.lines[key].color) { line.setColor(dataLocal.lines[key].color); } if (dataLocal.lines[key].value) { line.setValue(dataLocal.lines[key].value); } line.setDisplay(dataLocal.lines[key].display); if (line.getCategory() == VectorCategory.Line.BaseLine) { Settings.baseLineId = key; } } } if (dataLocal.hasOwnProperty("currentId")) { dataService.setCurrentId(dataLocal.currentId); } if (dataLocal.hasOwnProperty("res")) { coordinate.setRes(dataLocal.res); } } else if (data3d) { if (data3d.backImage) { let bgImg = imageService.create(data3d.backImage, data3d.vectorId); bgImg.setCenter(coordinate.center); try { if (bgImg.src) { await bgImg.setImageData(); } } catch (e) {} if (data3d.meterPerPixel) { coordinate.setRes(data3d.meterPerPixel); } const width = bgImg.imageData.width; const height = bgImg.imageData.height; if (data3d.baseLines) { for (let i = 0; i < data3d.baseLines.length; ++i) { //理论上基准线只能有一条 let baseLine = lineService.create( this.getXY(width, height, data3d.baseLines[i][0]), this.getXY(width, height, data3d.baseLines[i][1]), VectorCategory.Line.BaseLine ); Settings.baseLineId = baseLine.vectorId; } } if (data3d.measures) { for (let i = 0; i < data3d.measures.length; ++i) { //理论上基准线只能有一条 // const line = lineService.create( this.getXY(width, height, data3d.measures[i].pos[0]), this.getXY(width, height, data3d.measures[i].pos[1]), VectorCategory.Line.MeasureLine ); line.value = data3d.measures[i].dis; } } if (data3d.basePoints) { for (let i = 0; i < data3d.basePoints.length; ++i) { let point = pointService.create( this.getXY(width, height, data3d.basePoints[i]) ); point.setCategory(VectorCategory.Point.BasePoint); } } if (data3d.fixPoints) { for (let i = 0; i < data3d.fixPoints.length; ++i) { let point = pointService.create( this.getXY(width, height, data3d.fixPoints[i]) ); point.setCategory(VectorCategory.Point.FixPoint); } } } } if (Settings.baseLineId) { this.layer.updateForLocation(); } this.layer.history.init(); this.layer.renderer.autoRedraw(); } getXY(width, height, position) { const point = {}; point.x = position.x - width / 2; point.y = height / 2 - position.y; return point; } save() { dataService.vectorData.res = coordinate.getRes(); dataService.vectorData.scale = dataService.vectorData.res / (coordinate.zoom / coordinate.defaultZoom); return dataService.vectorData; } // 退出页面清除缓存及其他操作 clear() { this.layer.uiControl.menu_clear(); console.warn("clear"); } }