FloorScene.ts 14 KB

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