drawFloor.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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-if="showTools">
  5. <canvasFun @fit="fit" @savePng="savePng" @saveSvg="saveSvg" @divide="divide" @clearDivide="clearDivide" @scale="scale"
  6. @changeAbsorb="changeAbsorb" :config="config" ref="canvasFun" @saveJson="saveJson"></canvasFun>
  7. </div>
  8. </div>
  9. </template>
  10. <script>
  11. import { DivideFloorScene, FloorView, Opt } from "@saga-web/cad-engine/lib"
  12. import { SColor, SPoint } from "@saga-web/draw/lib";
  13. import canvasFun from "@/components/old-adm/business_space/newGraphy/canvasFun"
  14. import ScanController from "@/controller/old-adm/ScanController";
  15. import { EditLineItem } from "@saga-web/cad-engine"
  16. import { mapGetters } from "vuex";
  17. export default {
  18. components: {
  19. canvasFun
  20. },
  21. data() {
  22. return {
  23. drawMainScene: null,
  24. view: null,
  25. dataKey: '',
  26. cadWidth: 800,
  27. cadHeight: 600,
  28. canvasLoading: false,
  29. FloorID: '',
  30. Outline: [],
  31. floorData: {},
  32. config: {
  33. isEdit: false,
  34. divide: true
  35. },
  36. shadeList: [],
  37. type: 1, // 当前图展示类型
  38. scaleItem: null, // 比例尺item
  39. urlModelId: '', // url中传入的modelid
  40. };
  41. },
  42. props: {
  43. isEdit: {
  44. default: false
  45. },
  46. showTools: {
  47. default: false
  48. },
  49. id: {
  50. default: 0
  51. },
  52. dialog: {
  53. default: false
  54. }
  55. },
  56. computed: {
  57. ...mapGetters("layout", ["projectId", "projects"]),
  58. projectName() {
  59. let projectObj = this.projects.find(item => {
  60. return item.id == this.projectId
  61. })
  62. return projectObj ? projectObj.name ? projectObj.name : projectObj.id : this.projectId
  63. }
  64. },
  65. created() {
  66. this.FloorID = this.$route.query.FloorID;
  67. this.urlModelId = this.$route.query.modelId
  68. Opt.sceneMarkColor = new SColor('#00000080');
  69. if (!this.dialog) {
  70. this.init();
  71. }
  72. },
  73. mounted() {
  74. this.cadWidth = document.getElementById(`drawFloor${this.id}`).offsetWidth;
  75. this.cadHeight = document.getElementById(`drawFloor${this.id}`).offsetHeight;
  76. },
  77. methods: {
  78. //
  79. init() {
  80. this.getFloorData();
  81. },
  82. // 初始化canvas
  83. initGraphy(Id, type) {
  84. // type=1 => id:模型id
  85. // type=2 => id:floormap
  86. // type=3 => id:图片的key
  87. let that = this;
  88. that.type = type;
  89. that.clearGraphy()
  90. that.drawMainScene = new DivideFloorScene();
  91. that.canvasLoading = true;
  92. if (type == 1) {
  93. that.drawMainScene.getFloorData(`${window.__systemConf.baseServiceUrl}/revit-algorithm/base-graph/query`, { ModelId: Id }).then(res => {
  94. that.getGraphtSuc(res)
  95. })
  96. } else if(type==2){
  97. that.drawMainScene.loadUrl(`${window.__systemConf.baseServiceUrl}/image-service/common/file_get?systemId=revit&key=${Id}`).then(res => {
  98. that.getGraphtSuc(res)
  99. })
  100. }else if(type==3){
  101. that.drawMainScene.loadImg(`${window.__systemConf.baseServiceUrl}/image-service/common/image_get?systemId=dataPlatform&key=${Id}`, res=>{
  102. that.getGraphtSuc(res)
  103. })
  104. }
  105. },
  106. // 获取底图成功
  107. getGraphtSuc(res) {
  108. this.canvasLoading = false;
  109. if (res == 'error') {
  110. this.FloorMap = '';
  111. console.log('数据解析异常');
  112. return;
  113. }
  114. if (res.Result == 'failure') {
  115. this.showTools = false;
  116. this.$message.warning(res.Message);
  117. return;
  118. }
  119. this.view.scene = this.drawMainScene;
  120. this.view.fitSceneToView();
  121. this.drawMainScene.isSpaceSelectable = false;
  122. if (this.type == 3) {
  123. this.addScaleItem(this.floorData.properties)
  124. this.$emit('getGraphSuc');
  125. if (this.floorData.infos && this.floorData.infos.FloorMapShift) {
  126. const struct = JSON.parse(this.floorData.infos.FloorMapShift)
  127. const x = struct.x;
  128. const y = struct.y;
  129. if (x != 0 || y != 0) {
  130. this.drawMainScene.imgList[0].moveTo(x, y);
  131. }
  132. }
  133. }
  134. if (this.$refs.canvasFun) {
  135. this.view.minScale = this.view.scale;
  136. this.$refs.canvasFun.everyScale = this.view.scale;
  137. }
  138. if (this.floorData.outline && this.floorData.outline.length) {
  139. let newArr = this.floorData.outline.map(t => {
  140. return new SPoint(t.X, t.Y);
  141. })
  142. this.drawMainScene.addSceneMark(newArr);
  143. }
  144. this.$emit('handled', this.floorData.outline && this.floorData.outline.length > 0)
  145. },
  146. // 获取楼层数据
  147. getFloorData() {
  148. let pa = {
  149. filters: `id='${this.FloorID}'`,
  150. // 根据接口调整
  151. // cascade: [{ name: 'floorOutline' }]
  152. }
  153. // 先用 floorQuery 级联 floorOutline 以后与 floorQueryOutline 一块使用
  154. const temp = this.urlModelId && this.urlModelId.split('.')[1]
  155. if (temp && (temp.toLowerCase() == 'png' || temp.toLowerCase() == 'jpg')) { //底图为图片且已经绑定过图片
  156. ScanController.floorQueryAndSign(pa).then(res => {
  157. this.floorData = res.content[0];
  158. if (this.floorData.infos && this.floorData.infos.floorMap) {
  159. let floorMap = this.floorData.infos.floorMap
  160. this.initGraphy(floorMap, 3)
  161. }
  162. })
  163. } else {
  164. // floorQuery
  165. ScanController.floorQueryOutline(pa).then(res => {
  166. this.floorData = res.content[0];
  167. if (this.floorData.infos && this.floorData.infos.floorMap) {
  168. this.getOtherFloorOutLine();
  169. let floorMap = this.floorData.infos.floorMap
  170. this.initGraphy(floorMap, 2)
  171. }
  172. })
  173. }
  174. },
  175. // 获取绑定该模型id的其他楼层轮廓线
  176. getOtherFloorOutLine() {
  177. let modelid = this.floorData.modelId;
  178. if (modelid) {
  179. let pa = {
  180. filters: `modelId='${modelid}'`,
  181. cascade: [{ name: 'building' }]
  182. }
  183. this.shadeList = [];
  184. ScanController.floorQueryOutline(pa).then(res => {
  185. const temp = res.content.map(t => {
  186. if (t.id != this.FloorID) {
  187. if ( t.outline && t.outline.length) {
  188. let line = t.outline.map(item => {
  189. return new SPoint(item.X, item.Y);
  190. })
  191. this.shadeList.push(line);
  192. }
  193. return t
  194. }
  195. }).filter(item => item);
  196. this.drawMainScene.addAllShade(this.shadeList);
  197. this.$emit('changeSign', res.content.length > 1, temp);
  198. });
  199. }
  200. },
  201. // 添加比例尺item 当楼层图为图片类型时 回显比例尺
  202. addScaleItem(data) {
  203. this.scaleItem = new EditLineItem(null)
  204. this.scaleItem.strokeColor = SColor.Red;
  205. this.drawMainScene.addItem(this.scaleItem)
  206. if (data && data.line && data.line.length == 2) {
  207. this.scaleItem.line = data.line;
  208. this.scaleItem.text = data.text;
  209. // todo
  210. this.scaleItem.pointChange();
  211. }
  212. },
  213. // 清空平面图
  214. clearGraphy() {
  215. if (this.view) {
  216. this.view.scene = null;
  217. return
  218. }
  219. let id = `floorCanvas${this.id}`;
  220. this.view = new FloorView(id)
  221. },
  222. // canvas 获取焦点
  223. focus() {
  224. document.getElementById(`floorCanvas${this.id}`).focus()
  225. },
  226. // 工具栏操作
  227. // 适配底图到窗口
  228. fit() {
  229. this.view.fitSceneToView()
  230. },
  231. // 保存为png
  232. savePng() {
  233. this.view.saveImage(`${this.projectName}-${this.buildFloorName}.png`, 'png');
  234. },
  235. // 保存为svg
  236. saveSvg() {
  237. this.view.saveSceneSvg(`${this.projectName}-${this.buildFloorName}.svg`, 6400, 4800);
  238. },
  239. // 保存json
  240. saveJson() {
  241. this.view.saveFloorJson(`${this.projectName}-${this.buildFloorName}.json`)
  242. },
  243. // 切割划分
  244. divide() {
  245. this.drawMainScene.isMarking = true;
  246. },
  247. // 清除切割划分
  248. clearDivide() {
  249. this.drawMainScene.clearSceneMark()
  250. },
  251. // 吸附
  252. changeAbsorb(isAbsorbing) {
  253. this.drawMainScene.isAbsorbing = isAbsorbing;
  254. },
  255. // 缩放
  256. scale(val) {
  257. if (!this.view) {
  258. return;
  259. }
  260. let scale = this.view.scale;
  261. this.view.scaleByPoint(val / scale, this.cadWidth / 2, this.cadHeight / 2)
  262. },
  263. },
  264. watch: {
  265. "view.scale": {
  266. handler(n) {
  267. if (this.$refs.canvasFun) {
  268. let s = n * 10 / this.view.minScale
  269. this.$refs.canvasFun.sliderVal = s > 1000 ? 1000 : s;
  270. }
  271. }
  272. },
  273. "isEdit": {
  274. handler(n) {
  275. this.config.isEdit = n;
  276. }
  277. }
  278. }
  279. };
  280. </script>
  281. <style scoped lang="less">
  282. .drawFloor {
  283. width: 100%;
  284. height: 100%;
  285. position: relative;
  286. .operate {
  287. position: absolute;
  288. left: 50%;
  289. bottom: 20px;
  290. transform: translateX(-50%);
  291. z-index: 99;
  292. }
  293. }
  294. </style>