Load.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.create(
  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. }
  33. } catch (e) {}
  34. }
  35. if (dataLocal.circles) {
  36. for (let key in dataLocal.circles) {
  37. let circle = circleService.create(
  38. dataLocal.circles[key].center,
  39. dataLocal.circles[key].radius,
  40. key
  41. );
  42. circle.setPoints(dataLocal.circles[key].points);
  43. circle.setColor(dataLocal.circles[key].color);
  44. circle.setDisplay(dataLocal.circles[key].display);
  45. }
  46. }
  47. if (dataLocal.magnifiers) {
  48. for (let key in dataLocal.magnifiers) {
  49. let magnifier = magnifierService.create(
  50. dataLocal.magnifiers[key].position,
  51. key
  52. );
  53. magnifier.setSrc(dataLocal.magnifiers[key].photoUrl);
  54. magnifier.setDisplay(dataLocal.magnifiers[key].display);
  55. try {
  56. if (dataLocal.magnifiers[key].photoUrl) {
  57. await magnifier.setImageData();
  58. }
  59. } catch (e) {}
  60. }
  61. }
  62. if (dataLocal.texts) {
  63. for (let key in dataLocal.texts) {
  64. let text = textService.create(dataLocal.texts[key].center, key);
  65. text.setValue(dataLocal.texts[key].value);
  66. text.setFontSize(dataLocal.texts[key].fontSize);
  67. text.setColor(dataLocal.texts[key].color);
  68. text.setDisplay(dataLocal.texts[key].display);
  69. }
  70. }
  71. if (dataLocal.points) {
  72. for (let key in dataLocal.points) {
  73. let point = pointService.create(dataLocal.points[key], key);
  74. point.setCategory(dataLocal.points[key].category);
  75. point.setParent(
  76. JSON.parse(JSON.stringify(dataLocal.points[key].parent))
  77. );
  78. point.setDisplay(dataLocal.points[key].display);
  79. }
  80. }
  81. if (dataLocal.lines) {
  82. for (let key in dataLocal.lines) {
  83. let line = lineService.createByPointId(
  84. dataLocal.lines[key].startId,
  85. dataLocal.lines[key].endId,
  86. dataLocal.lines[key].category,
  87. key
  88. );
  89. if (dataLocal.lines[key].color) {
  90. line.setColor(dataLocal.lines[key].color);
  91. }
  92. if (dataLocal.lines[key].value) {
  93. line.setValue(dataLocal.lines[key].value);
  94. }
  95. line.setDisplay(dataLocal.lines[key].display);
  96. if (line.getCategory() == VectorCategory.Line.BaseLine) {
  97. Settings.baseLineId = key;
  98. }
  99. }
  100. }
  101. if (dataLocal.hasOwnProperty("currentId")) {
  102. dataService.setCurrentId(dataLocal.currentId);
  103. }
  104. if (dataLocal.hasOwnProperty("res")) {
  105. coordinate.setRes(dataLocal.res);
  106. }
  107. } else if (data3d) {
  108. if (data3d.backImage) {
  109. let bgImg = imageService.create(data3d.backImage, data3d.vectorId);
  110. bgImg.setCenter(coordinate.center);
  111. try {
  112. if (bgImg.src) {
  113. await bgImg.setImageData();
  114. }
  115. } catch (e) {}
  116. if (data3d.meterPerPixel) {
  117. coordinate.setRes(data3d.meterPerPixel);
  118. }
  119. const width = bgImg.imageData.width;
  120. const height = bgImg.imageData.height;
  121. if (data3d.baseLines) {
  122. for (let i = 0; i < data3d.baseLines.length; ++i) {
  123. //理论上基准线只能有一条
  124. let baseLine = lineService.create(
  125. this.getXY(width, height, data3d.baseLines[i][0]),
  126. this.getXY(width, height, data3d.baseLines[i][1]),
  127. VectorCategory.Line.BaseLine
  128. );
  129. Settings.baseLineId = baseLine.vectorId;
  130. }
  131. }
  132. if (data3d.measures) {
  133. for (let i = 0; i < data3d.measures.length; ++i) {
  134. //理论上基准线只能有一条
  135. //
  136. const line = lineService.create(
  137. this.getXY(width, height, data3d.measures[i].pos[0]),
  138. this.getXY(width, height, data3d.measures[i].pos[1]),
  139. VectorCategory.Line.MeasureLine
  140. );
  141. line.value = data3d.measures[i].dis;
  142. }
  143. }
  144. if (data3d.basePoints) {
  145. for (let i = 0; i < data3d.basePoints.length; ++i) {
  146. let point = pointService.create(
  147. this.getXY(width, height, data3d.basePoints[i])
  148. );
  149. point.setCategory(VectorCategory.Point.BasePoint);
  150. }
  151. }
  152. if (data3d.fixPoints) {
  153. for (let i = 0; i < data3d.fixPoints.length; ++i) {
  154. let point = pointService.create(
  155. // data3d.fixPoints[i].text
  156. this.getXY(width, height, data3d.fixPoints[i].pos)
  157. );
  158. point.setCategory(VectorCategory.Point.FixPoint);
  159. }
  160. }
  161. }
  162. }
  163. if (Settings.baseLineId) {
  164. this.layer.updateForLocation();
  165. }
  166. this.layer.history.init();
  167. this.layer.renderer.autoRedraw();
  168. }
  169. getXY(width, height, position) {
  170. const point = {};
  171. point.x = position.x - width / 2;
  172. point.y = height / 2 - position.y;
  173. return point;
  174. }
  175. save() {
  176. const res = coordinate.getRes();
  177. dataService.setRes(res);
  178. const scale = res / (coordinate.zoom / coordinate.defaultZoom);
  179. dataService.setScale(scale);
  180. return dataService.vectorData;
  181. }
  182. // 退出页面清除缓存及其他操作
  183. clear() {
  184. this.layer.uiControl.menu_clear();
  185. console.warn("clear");
  186. }
  187. }