drawModel.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <template>
  2. <div :id="`drawFloor${id}`" class="drawFloor" v-loading="canvasLoading">
  3. <canvas :id="`floorCanvas${id}`" :width="cadWidth" :height="cadHeight" ref="canvas" tabindex="0"></canvas>
  4. <div class="operate" v-show="showTools">
  5. <canvasFun @fit="fit" @savePng="savePng" @saveSvg="saveSvg" @scale="scale" :config="config" ref="canvasFun"></canvasFun>
  6. </div>
  7. </div>
  8. </template>
  9. <script>
  10. import { DivideFloorScene, FloorView } from "@saga-web/cad-engine/lib"
  11. import { SColor, SPoint } from "@sybotan-web/draw/lib";
  12. import canvasFun from "@/components/business_space/newGraphy/canvasFun"
  13. import { floorQuery } from "@/api/scan/request";
  14. export default {
  15. components: {
  16. canvasFun
  17. },
  18. data() {
  19. return {
  20. drawMainScene: null,
  21. view: null,
  22. dataKey: '',
  23. cadWidth: 800,
  24. cadHeight: 600,
  25. canvasLoading: false,
  26. modelId: '',
  27. FloorID: '',
  28. Outline: [],
  29. buttonContent: "",
  30. showTools: false,
  31. config: {
  32. isEdit: false
  33. }
  34. };
  35. },
  36. props: {
  37. isEdit: {
  38. default: false
  39. },
  40. id: {
  41. default: 0
  42. },
  43. dialog: {
  44. default: false
  45. },
  46. CurrentModelId: String
  47. },
  48. created() { },
  49. mounted() {
  50. this.cadWidth = document.getElementById(`drawFloor${this.id}`).offsetWidth;
  51. this.cadHeight = document.getElementById(`drawFloor${this.id}`).offsetHeight;
  52. },
  53. methods: {
  54. initGraphy(ModelId) {
  55. this.modelId = ModelId;
  56. this.clearGraphy()
  57. this.drawMainScene = new DivideFloorScene();
  58. this.canvasLoading = true;
  59. this.drawMainScene.getFloorData('/modelapi/base-graph/query', { ModelId: ModelId }).then(res => {
  60. console.log(this)
  61. this.getGraphtSuc(res);
  62. })
  63. },
  64. getFloorData() {
  65. let pa = {
  66. Filters: `FloorID='${this.FloorID}'`
  67. }
  68. floorQuery(pa, res => {
  69. let newArr = res.Content[0].Outline.map(t => {
  70. return new SPoint(t.X, t.Y);
  71. })
  72. this.drawMainScene.addSceneMark(newArr)
  73. })
  74. },
  75. getSelectedSpaces() {//获取选中区域
  76. if (this.view && this.view.scene) {
  77. let list = this.view.scene.getSelectedSpaces();
  78. if (list.length) {
  79. return list
  80. } else {
  81. return []
  82. }
  83. } else {
  84. return []
  85. }
  86. },
  87. // 清空平面图
  88. clearGraphy() {
  89. if (this.view) {
  90. this.view.scene = null;
  91. return
  92. }
  93. let id = `floorCanvas${this.id}`;
  94. this.view = new FloorView(id)
  95. },
  96. canvasClick(item, eventObj) {//点击平面图事件
  97. this.$emit("changeButtonContent",this.drawMainScene.getSelectedSpaces().length?"通过模型空间创建":"通过模型创建")
  98. },
  99. getGraphtSuc(res) {
  100. this.showTools = true;
  101. this.canvasLoading = false;
  102. if (res.Result == 'failure') {
  103. this.showTools = false;
  104. this.$message.warning(res.Message);
  105. return;
  106. }
  107. this.view.scene = this.drawMainScene;
  108. this.view.fitSceneToView();
  109. this.drawMainScene.click(this, this.canvasClick);
  110. if (this.$refs.canvasFun) {
  111. this.view.maxScale = this.view.scale * 10;
  112. this.view.minScale = this.view.scale;
  113. this.$refs.canvasFun.everyScale = this.view.scale;
  114. }
  115. },
  116. // canvas 获取焦点
  117. focus() {
  118. document.getElementById(`floorCanvas${this.id}`).focus()
  119. },
  120. // 工具栏操作
  121. // 适配底图到窗口
  122. fit() {
  123. this.view.fitSceneToView()
  124. },
  125. // 保存为png
  126. savePng() {
  127. this.view.saveImage(`${this.floor}.png`, 'png');
  128. },
  129. // 保存为svg
  130. saveSvg() {
  131. this.view.saveSceneSvg(`${this.floor}.svg`, 6400, 4800);
  132. },
  133. // 缩放
  134. scale(val) {
  135. if (!this.view) {
  136. return;
  137. }
  138. let scale = this.view.scale;
  139. this.view.scaleByPoint(val / scale, this.cadWidth / 2, this.cadHeight / 2);
  140. }
  141. },
  142. watch: {
  143. CurrentModelId: {
  144. handler(newName, oldName) {
  145. if (newName) {
  146. this.initGraphy(newName)
  147. }
  148. },
  149. immediate: true,
  150. },
  151. "view.scale": {
  152. handler(n) {
  153. if (this.$refs.canvasFun) {
  154. this.$refs.canvasFun.sliderVal = n * 10 / this.view.minScale;
  155. }
  156. }
  157. },
  158. "isEdit": {
  159. handler(n) {
  160. this.config.isEdit = n;
  161. }
  162. }
  163. }
  164. };
  165. </script>
  166. <style scoped lang="less">
  167. .drawFloor {
  168. width: 100%;
  169. height: 100%;
  170. position: relative;
  171. .operate {
  172. position: absolute;
  173. left: 50%;
  174. bottom: 20px;
  175. transform: translateX(-50%);
  176. z-index: 99;
  177. }
  178. }
  179. </style>