FloorScene.ts 13 KB

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