FloorScene.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * ********************************************************************************************************************
  3. *
  4. * :*$@@%$*: ;: ;; ;;
  5. * :@@%! :!@@%: %! ;%%@@%$ =@@@@@@@%; @%@@@%%%%@@@@@
  6. * :@%; :$= %%$$$%$$ ;$$ ;$@= !@$
  7. * =@! %! @ $=;% !@@@%: !$$$$$$$$$$$$$$=
  8. * =@* %! @ $= % %@= =%@! %=
  9. * *$%%! @@= ;=$%%%$*: %! @ $= % =%%%%%%@$ *%: =%
  10. * %@@!: !@@@%=$@@@@%! :*@@$: %! @ $= % $* ;@ @* :%*
  11. * ;@@! ;!!!;: ;@%: =======@%========* @ $$ % $%*****$@ :@$=*********=@$
  12. * $@* ;@@@%=!: *@*
  13. * =@$ ;;;!=%@@@@=! =@!
  14. * %@$: =@%: :*@@@* %@= Copyright (c) 2016-2019. 北京上格云技术有限公司
  15. * ;%@@$=$@@%* *@@@$=%@@%;
  16. * ::;:: ::;:: All rights reserved.
  17. *
  18. * ********************************************************************************************************************
  19. */
  20. import { SGraphyScene } from "@saga-web/graphy/lib";
  21. // @ts-ignore
  22. import pako from "pako";
  23. import { FloorData } from "./types/FloorData";
  24. import { Space } from "./types/Space";
  25. import { Column } from "./types/Column";
  26. import { Wall } from "./types/Wall";
  27. import { VirtualWall } from "./types/VirtualWall";
  28. import { Door } from "./types/Door";
  29. import { Casement } from "./types/Casement";
  30. import Axios from "axios";
  31. import { SpaceItem } from "./items/SpaceItem";
  32. import { WallItem } from "./items/WallItem";
  33. import { ColumnItem } from "./items/ColumnItem";
  34. import { VirtualWallItem } from "./items/VirtualWallItem";
  35. import { DoorItem } from "./items/DoorItem";
  36. import { WindowItem } from "./items/WindowItem";
  37. /**
  38. * 楼层场景
  39. *
  40. * @author 郝建龙
  41. */
  42. export class FloorScene extends SGraphyScene {
  43. /** item数据 */
  44. data: FloorData | null = null;
  45. /** 是否显示空间 */
  46. private _isShowSpace: boolean = true;
  47. get isShowSpace(): boolean {
  48. return this._isShowSpace;
  49. } // Get isShowSpace
  50. set isShowSpace(v: boolean) {
  51. if (this._isShowSpace === v) {
  52. return;
  53. }
  54. this._isShowSpace = v;
  55. this.spaceList.map((t: SpaceItem) => {
  56. t.visible = this._isShowSpace;
  57. return t;
  58. });
  59. } // Set isShowSpace
  60. /** 是否显示柱子 */
  61. private _isShowColumn: boolean = true;
  62. get isShowColumn(): boolean {
  63. return this._isShowColumn;
  64. } // Get isShowColumn
  65. set isShowColumn(v: boolean) {
  66. if (this._isShowColumn === v) {
  67. return;
  68. }
  69. this._isShowColumn = v;
  70. this.columnList.map((t: ColumnItem) => {
  71. t.visible = this._isShowColumn;
  72. return t;
  73. });
  74. } // Set isShowColumn
  75. /** 是否展示墙体 */
  76. private _isShowWall: boolean = true;
  77. get isShowWall(): boolean {
  78. return this._isShowWall;
  79. } // Get isShowWall
  80. set isShowWall(v: boolean) {
  81. if (this._isShowWall === v) {
  82. return;
  83. }
  84. this._isShowWall = v;
  85. this.wallList.map((t: WallItem) => {
  86. t.visible = this._isShowWall;
  87. return t;
  88. });
  89. } // Set isShowWall
  90. /** 是否展示虚拟墙 */
  91. _isShowVirtualWall: boolean = true;
  92. get isShowVirtualWall(): boolean {
  93. return this._isShowVirtualWall;
  94. } // Get isShowVrtualWall
  95. set isShowVirtualWall(v: boolean) {
  96. if (this._isShowVirtualWall === v) {
  97. return;
  98. }
  99. this._isShowVirtualWall = v;
  100. this.virtualWallList.map((t: VirtualWallItem) => {
  101. t.visible = this._isShowVirtualWall;
  102. return t;
  103. });
  104. } // Set isShowVrtualWall
  105. /** 是否展示门 */
  106. _isShowDoor: boolean = true;
  107. get isShowDoor(): boolean {
  108. return this._isShowDoor;
  109. } // Get isShowDoor
  110. set isShowDoor(v: boolean) {
  111. if (this._isShowDoor === v) {
  112. return;
  113. }
  114. this._isShowDoor = v;
  115. this.doorList.map((t: DoorItem) => {
  116. t.visible = this._isShowDoor;
  117. return t;
  118. });
  119. } // Set isShowDoor
  120. /** 是否展示窗户 */
  121. _isShowWindow: boolean = true;
  122. get isShowWindow(): boolean {
  123. return this._isShowWindow;
  124. } // Get isShowWindow
  125. set isShowWindow(v: boolean) {
  126. if (this._isShowWindow === v) {
  127. return;
  128. }
  129. this._isShowWindow = v;
  130. this.casementList.map((t: WindowItem) => {
  131. t.visible = this._isShowWindow;
  132. return t;
  133. });
  134. } // Set isShowWindow
  135. /** 空间是否可选 */
  136. _isSpaceSelectable: boolean = true;
  137. get isSpaceSelectable(): boolean {
  138. return this._isSpaceSelectable;
  139. } // Get isSpaceSelectable
  140. set isSpaceSelectable(v: boolean) {
  141. if (this._isSpaceSelectable === v) {
  142. return;
  143. }
  144. this._isSpaceSelectable = v;
  145. this.spaceList.map((t: SpaceItem) => {
  146. t.selectable = this._isSpaceSelectable;
  147. return t;
  148. });
  149. } // Set isSpaceSelectable
  150. /** 墙list */
  151. wallList: WallItem[] = [];
  152. /** 柱子list */
  153. columnList: ColumnItem[] = [];
  154. /** 门list */
  155. doorList: DoorItem[] = [];
  156. /** 窗list */
  157. casementList: WindowItem[] = [];
  158. /** 虚拟墙list */
  159. virtualWallList: VirtualWallItem[] = [];
  160. /** 空间list */
  161. spaceList: SpaceItem[] = [];
  162. /**
  163. * @param data 绘制空间地图得所有参数
  164. */
  165. constructor() {
  166. super();
  167. } // Constructor()
  168. /**
  169. * 获取底图压缩文件
  170. *
  171. * @param url 请求数据文件路径
  172. */
  173. loadUrl(url: string) {
  174. let that = this;
  175. return new Promise((relove, reject) => {
  176. Axios({
  177. method: "get",
  178. url: url,
  179. data: {},
  180. responseType: "blob"
  181. })
  182. .then((res: any) => {
  183. let blob = res.data;
  184. this.unzip(blob)
  185. .then((jsonData: any) => {
  186. that.addBaseMapItem(jsonData);
  187. relove(jsonData);
  188. })
  189. .catch((error: any) => {
  190. console.log(error);
  191. });
  192. })
  193. .catch((res: any) => {
  194. console.log(res);
  195. });
  196. });
  197. } // Function loadUrl
  198. /**
  199. * 解压数据
  200. *
  201. * @param blob 文件
  202. */
  203. private unzip(blob: any): any {
  204. let reader = new FileReader();
  205. reader.readAsBinaryString(blob);
  206. let that = this;
  207. return new Promise(resolve => {
  208. reader.onload = (readerEvt: any) => {
  209. let binaryString = readerEvt.target.result;
  210. //解压数据
  211. let base64Data = btoa(binaryString);
  212. let unGzipData = that.unzipBase64(base64Data);
  213. try {
  214. that.data = unGzipData.entityList
  215. ? unGzipData.entityList[0].Elements
  216. : unGzipData.EntityList[0].Elements;
  217. resolve(that.data);
  218. } catch (e) {
  219. console.log(e);
  220. console.log(unGzipData);
  221. resolve("error");
  222. }
  223. };
  224. });
  225. } // Function unzip
  226. /**
  227. * 获取楼层未压缩数据
  228. *
  229. * @param url 请求路径
  230. */
  231. getFloorData(url: string, data: { ModelId: string }) {
  232. let that = this;
  233. return new Promise((resolve, reject) => {
  234. Axios({
  235. method: "post",
  236. url: url,
  237. data: data
  238. })
  239. .then((res: any) => {
  240. if (res.data.Result == "success") {
  241. let floordata = res.data.EntityList[0].Elements;
  242. that.data = floordata;
  243. that.addBaseMapItem(floordata);
  244. }
  245. resolve(res.data);
  246. })
  247. .catch((err: any) => {
  248. console.log(err);
  249. });
  250. });
  251. } // Function getFloorData
  252. /**
  253. * 增添所有底图item;
  254. *
  255. * @param data itemList对象
  256. */
  257. private addBaseMapItem(data: FloorData) {
  258. if (data.Walls) {
  259. data.Walls.map((t: Wall) => {
  260. this.addWall(t);
  261. });
  262. }
  263. if (data.Columns) {
  264. data.Columns.map((t: Column) => {
  265. this.addColumn(t);
  266. });
  267. }
  268. if (data.Windows) {
  269. data.Windows.map((t: Casement) => {
  270. this.addCasement(t);
  271. });
  272. }
  273. if (data.VirtualWalls) {
  274. data.VirtualWalls.map((t: VirtualWall) => {
  275. this.addVirtualWall(t);
  276. });
  277. }
  278. if (data.Doors) {
  279. data.Doors.map((t: Door) => {
  280. this.addDoor(t);
  281. });
  282. }
  283. if (data.Spaces) {
  284. data.Spaces.map((t: Space) => {
  285. this.addSpace(t);
  286. });
  287. }
  288. } // Function addBaseMapItem
  289. /**
  290. * 添加空间到scene 中
  291. *
  292. * @param space 空间item
  293. */
  294. addSpace(space: Space): void {
  295. let item = new SpaceItem(null, space);
  296. item.zOrder = 2;
  297. item.visible = this.isShowSpace;
  298. item.selectable = this.isSpaceSelectable;
  299. this.spaceList.push(item);
  300. this.addItem(item);
  301. } // Function addSpace
  302. /**
  303. * 添加柱子到scene 中
  304. *
  305. * @param column 柱子item
  306. */
  307. addColumn(column: Column): void {
  308. let item = new ColumnItem(null, column);
  309. item.visible = this.isShowColumn;
  310. this.columnList.push(item);
  311. this.addItem(item);
  312. } // Function addColumn
  313. /**
  314. * 添加墙到scene 中
  315. *
  316. * @param wall 墙item
  317. */
  318. addWall(wall: Wall): void {
  319. let item = new WallItem(null, wall);
  320. item.visible = this.isShowWall;
  321. this.wallList.push(item);
  322. this.addItem(item);
  323. } // Function addWall
  324. /**
  325. * 添加所有虚拟墙到scene中
  326. *
  327. * @param virtualWall 虚拟墙item
  328. */
  329. addVirtualWall(virtualWall: VirtualWall): void {
  330. let item = new VirtualWallItem(null, virtualWall);
  331. item.visible = this.isShowVirtualWall;
  332. this.virtualWallList.push(item);
  333. this.addItem(item);
  334. } // Function addVirtualWall
  335. /**
  336. * 添加门到scene 中
  337. *
  338. * @param doors 门item
  339. */
  340. addDoor(door: Door): void {
  341. let item = new DoorItem(null, door);
  342. item.zOrder = 99;
  343. item.visible = this.isShowDoor;
  344. this.doorList.push(item);
  345. this.addItem(item);
  346. } // Function addDoor
  347. /**
  348. * 添加窗户到scene 中
  349. *
  350. * @param windows 窗户item
  351. */
  352. addCasement(casement: Casement): void {
  353. let item = new WindowItem(null, casement);
  354. item.zOrder = 1;
  355. item.visible = this.isShowWindow;
  356. this.casementList.push(item);
  357. this.addItem(item);
  358. } // Function addCasement
  359. /**
  360. * 扩大数组中相应字段一定倍数
  361. *
  362. * @param arr itemList
  363. * @param k 倍数
  364. * @param name 字段key值
  365. */
  366. changeMap(arr: any, k: any, name: any) {
  367. let data = arr.map((items: any) => {
  368. if (items[name] && items[name].length) {
  369. items[name].map((children: any) => {
  370. if (Array.isArray(children)) {
  371. return children.map(res => {
  372. res.Y = res.Y * k;
  373. return res;
  374. });
  375. } else {
  376. children.Y = children.Y * k;
  377. return children;
  378. }
  379. });
  380. }
  381. });
  382. return data;
  383. } // Function changeMap
  384. /**
  385. * 解压文件
  386. *
  387. * @param b64Data base64数据
  388. */
  389. private unzipBase64(b64Data: any) {
  390. let strData = atob(b64Data);
  391. let charData = strData.split("").map(function(x) {
  392. return x.charCodeAt(0);
  393. });
  394. let binData = new Uint8Array(charData);
  395. let data = pako.inflate(binData, { to: "string" });
  396. return eval("(" + data + ")");
  397. } // Function unzipBase64
  398. /**
  399. * 压缩文件
  400. *
  401. * @param str 被压缩的数据
  402. */
  403. private zip(str: any) {
  404. //escape(str) --->压缩前编码,防止中午乱码
  405. let binaryString = pako.gzip(escape(str), { to: "string" });
  406. return binaryString;
  407. } // Function zip
  408. /**
  409. * 获取选中空间的列表
  410. *
  411. * @return 被选中的空间列表
  412. */
  413. getSelectedSpaces(): SpaceItem[] {
  414. let arr: SpaceItem[] = [];
  415. this.spaceList.map(sp => {
  416. if (sp.selected) {
  417. arr.push(sp);
  418. }
  419. });
  420. return arr;
  421. } // Function getSelectedSpaces
  422. /**
  423. * 清除选中的空间
  424. *
  425. */
  426. clearSelectedSpaces(): void {
  427. this.spaceList.map(sp => {
  428. sp.selected = false;
  429. return sp;
  430. });
  431. } // Function clearSelectedSpaces
  432. } // Class FloorScene