Load.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import { dataService } from "./Service/DataService.js";
  2. import { lineService } from "./Service/LineService.js";
  3. import { pointService } from "./Service/PointService.js";
  4. import { imageService } from "./Service/ImageService.js";
  5. import VectorCategory from "./enum/VectorCategory.js";
  6. import { coordinate } from "./Coordinate.js";
  7. import Settings from "./Settings";
  8. import { circleService } from "./Service/CircleService.js";
  9. import { magnifierService } from "./Service/MagnifierService.js";
  10. import { textService } from "./Service/TextService.js";
  11. export default class Load {
  12. constructor(layer) {
  13. this.layer = layer;
  14. this.version = "v1.0";
  15. this.vectorsJson = null;
  16. this.newVectorId = null;
  17. }
  18. async load(dataLocal, data3d) {
  19. this.layer.initLocation();
  20. if (dataLocal) {
  21. if (dataLocal.backgroundImg) {
  22. let bgImg = imageService.createBackgroundImg(
  23. dataLocal.backgroundImg.src,
  24. dataLocal.backgroundImg.vectorId
  25. );
  26. bgImg.setCenter(dataLocal.backgroundImg.center);
  27. bgImg.setDisplay(dataLocal.backgroundImg.display);
  28. bgImg.setAngle(dataLocal.backgroundImg.angle);
  29. try {
  30. if (dataLocal.backgroundImg.src) {
  31. await bgImg.setImageData();
  32. bgImg.setBounding();
  33. }
  34. } catch (e) {}
  35. }
  36. if (dataLocal.circles) {
  37. for (let key in dataLocal.circles) {
  38. let circle = circleService.create(
  39. dataLocal.circles[key].center,
  40. dataLocal.circles[key].radius,
  41. key
  42. );
  43. circle.setPoints(dataLocal.circles[key].points);
  44. circle.setColor(dataLocal.circles[key].color);
  45. circle.setDisplay(dataLocal.circles[key].display);
  46. }
  47. }
  48. if (dataLocal.magnifiers) {
  49. for (let key in dataLocal.magnifiers) {
  50. let magnifier = magnifierService.create(
  51. dataLocal.magnifiers[key].position,
  52. key
  53. );
  54. magnifier.setSrc(dataLocal.magnifiers[key].photoUrl);
  55. magnifier.setDisplay(dataLocal.magnifiers[key].display);
  56. try {
  57. if (dataLocal.magnifiers[key].photoUrl) {
  58. await magnifier.setImageData();
  59. }
  60. } catch (e) {}
  61. }
  62. }
  63. if (dataLocal.points) {
  64. for (let key in dataLocal.points) {
  65. let point = pointService.create(dataLocal.points[key], key);
  66. point.setCategory(dataLocal.points[key].category);
  67. point.setParent(
  68. JSON.parse(JSON.stringify(dataLocal.points[key].parent))
  69. );
  70. point.setDisplay(dataLocal.points[key].display);
  71. }
  72. }
  73. if (dataLocal.lines) {
  74. for (let key in dataLocal.lines) {
  75. let line = lineService.createByPointId(
  76. dataLocal.lines[key].startId,
  77. dataLocal.lines[key].endId,
  78. dataLocal.lines[key].category,
  79. key
  80. );
  81. if (dataLocal.lines[key].color) {
  82. line.setColor(dataLocal.lines[key].color);
  83. }
  84. if (dataLocal.lines[key].value) {
  85. line.setValue(dataLocal.lines[key].value);
  86. }
  87. line.setDisplay(dataLocal.lines[key].display);
  88. if (line.getCategory() == VectorCategory.Line.BaseLine) {
  89. Settings.baseLineId = key;
  90. }
  91. }
  92. }
  93. if (dataLocal.texts) {
  94. for (let key in dataLocal.texts) {
  95. let text = textService.create(dataLocal.texts[key].center, key);
  96. text.setValue(dataLocal.texts[key].value);
  97. text.setFontSize(dataLocal.texts[key].fontSize);
  98. text.setColor(dataLocal.texts[key].color);
  99. text.setDisplay(dataLocal.texts[key].display);
  100. text.setAngle(dataLocal.texts[key].angle || 0);
  101. }
  102. }
  103. if (dataLocal.hasOwnProperty("currentId")) {
  104. dataService.setCurrentId(dataLocal.currentId);
  105. }
  106. if (dataLocal.hasOwnProperty("res")) {
  107. coordinate.setRes(dataLocal.res);
  108. }
  109. } else if (data3d) {
  110. if (data3d.backImage) {
  111. let bgImg = imageService.createBackgroundImg(
  112. data3d.backImage,
  113. data3d.vectorId
  114. );
  115. bgImg.setCenter(coordinate.center);
  116. try {
  117. if (bgImg.src) {
  118. await bgImg.setImageData();
  119. bgImg.setBounding();
  120. }
  121. } catch (e) {}
  122. if (data3d.meterPerPixel) {
  123. coordinate.setRes(data3d.meterPerPixel);
  124. }
  125. const width = bgImg.imageData.width;
  126. const height = bgImg.imageData.height;
  127. let angle = 0;
  128. if (data3d.baseLines) {
  129. for (let i = 0; i < data3d.baseLines.length; ++i) {
  130. //理论上基准线只能有一条
  131. let baseLine = lineService.create(
  132. this.getXY(width, height, data3d.baseLines[i][0]),
  133. this.getXY(width, height, data3d.baseLines[i][1]),
  134. VectorCategory.Line.BaseLine
  135. );
  136. Settings.baseLineId = baseLine.vectorId;
  137. //文字要和基准线的方向一致
  138. if (typeof baseLine.a != "undefined") {
  139. angle = Math.atan(baseLine.a);
  140. } else if (baseLine.hasOwnProperty("x")) {
  141. angle = Math.PI / 2;
  142. } else {
  143. angle = 0;
  144. }
  145. }
  146. }
  147. if (data3d.measures) {
  148. for (let i = 0; i < data3d.measures.length; ++i) {
  149. //理论上基准线只能有一条
  150. //
  151. const line = lineService.create(
  152. this.getXY(width, height, data3d.measures[i].pos[0]),
  153. this.getXY(width, height, data3d.measures[i].pos[1]),
  154. VectorCategory.Line.MeasureLine
  155. );
  156. line.value = data3d.measures[i].dis;
  157. }
  158. }
  159. if (data3d.basePoints) {
  160. for (let i = 0; i < data3d.basePoints.length; ++i) {
  161. let point = pointService.create(
  162. this.getXY(width, height, data3d.basePoints[i])
  163. );
  164. point.setCategory(VectorCategory.Point.BasePoint);
  165. }
  166. }
  167. if (data3d.fixPoints) {
  168. for (let i = 0; i < data3d.fixPoints.length; ++i) {
  169. // let point = pointService.create(
  170. // this.getXY(width, height, data3d.fixPoints[i].pos)
  171. // );
  172. // point.setCategory(VectorCategory.Point.FixPoint);
  173. let text = textService.create(data3d.fixPoints[i].pos);
  174. text.setValue(data3d.fixPoints[i].text);
  175. text.setAngle(data3d.fixPoints[i].angle || 0);
  176. }
  177. }
  178. }
  179. }
  180. if (Settings.baseLineId) {
  181. this.layer.updateForLocation();
  182. }
  183. this.layer.history.init();
  184. this.layer.renderer.autoRedraw();
  185. }
  186. getXY(width, height, position) {
  187. const point = {};
  188. point.x = position.x - width / 2;
  189. point.y = height / 2 - position.y;
  190. return point;
  191. }
  192. save() {
  193. const res = coordinate.getRes();
  194. dataService.setRes(res);
  195. const scale = res / (coordinate.zoom / coordinate.defaultZoom);
  196. dataService.setScale(scale);
  197. return dataService.vectorData;
  198. }
  199. // 退出页面清除缓存及其他操作
  200. clear() {
  201. this.layer.uiControl.menu_clear();
  202. console.warn("clear");
  203. }
  204. }