SBaseEditScene.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. * *********************************************************************************************************************
  3. *
  4. * !!
  5. * .F88X
  6. * X8888Y
  7. * .}888888N;
  8. * i888888N; .:! .I$WI:
  9. * R888888I .'N88~ i8}+8Y&8"l8i$8>8W~'>W8}8]KW+8IIN"8&
  10. * .R888888I .;N8888~ .X8' "8I.!,/8" !%NY8`"8I8~~8>,88I
  11. * +888888N; .8888888Y "&&8Y.}8,
  12. * ./888888N; .R888888Y .'}~ .>}'.`+> i}! "i' +/' .'i~ !11,.:">, .~]! .i}i
  13. * ~888888%: .I888888l .]88~`1/iY88Ii+1'.R$8$8]"888888888> Y8$ W8E X8E W8888'188Il}Y88$*
  14. * 18888888 E8888881 .]W%8$`R8X'&8%++N8i,8N%N8+l8%` .}8N:.R$RE%N88N%N$K$R 188,FE$8%~Y88I
  15. * .E888888I .i8888888' .:$8I;88+`E8R:/8N,.>881.`$8E/1/]N8X.Y8N`"KF&&FK!'88*."88K./$88%RN888+~
  16. * 8888888I .,N888888~ ~88i"8W,!N8*.I88.}888%F,i$88"F88" 888:E8X.>88!i88>`888*.}Fl1]*}1YKi'
  17. * i888888N' I888Y ]88;/EX*IFKFK88X K8R .l8W 88Y ~88}'88E&%8W.X8N``]88!.$8K .:W8I
  18. * .i888888N; I8Y .&8$ .X88! i881.:%888>I88 ;88] +88+.';;;;:.Y88X 18N.,88l .+88/
  19. * .:R888888I
  20. * .&888888I Copyright (c) 2016-2020. 博锐尚格科技股份有限公司
  21. * ~8888'
  22. * .!88~ All rights reserved.
  23. *
  24. * *********************************************************************************************************************
  25. */
  26. import { SGraphDeleteListCommand, SGraphEditScene, SGraphEdit } from "@persagy-web/edit"
  27. import { SMouseEvent } from "@persagy-web/base/lib";
  28. import { SArrowStyleType, SPoint, SRect } from '@persagy-web/draw';
  29. import { SItemStatus, SRectSelectItem } from '@persagy-web/big/lib';
  30. import { SGraphItem, SGraphSelectContainer, SOrderSetType } from "@persagy-web/graph/lib/";
  31. import { uuid } from "./until";
  32. import { SBaseArrow, SBaseEquipment, SBaseExpainEdit, SBasePipe } from "./"
  33. import { SBaseArrowPolyEdit } from './items/SBaseArrowPolyEdit';
  34. import { SGraphAddListCommand } from '@persagy-web/edit/lib/commands/SGraphAddListCommand';
  35. /**
  36. * big-edit 场景
  37. *
  38. * @author 韩耀龙 <han_yao_long@163.com>
  39. */
  40. export class SBaseEditScene extends SGraphEditScene {
  41. /** 图例节点 */
  42. Nodes: any = []; // 图例节点,所有与工程信息化相关的图例(图标类型与区域)
  43. /** 图例节点 */ // 与工程信息无关的标识对象(增加文本注释,图上的图片说明)
  44. Markers: any = [];
  45. /** 管线对象 */
  46. Relations: any = [];
  47. /** 复制的对象 */
  48. copyString: any[] = [];
  49. /** 选中的对象 */
  50. grabItem: null | SGraphEdit | SGraphItem = null;
  51. /**
  52. * 构造函数
  53. *
  54. */
  55. constructor() {
  56. super()
  57. } // Constructor
  58. /**
  59. * 新增基础类注释
  60. *
  61. * @param event 鼠标事件参数
  62. */
  63. addExplainItem(event: SMouseEvent): void {
  64. const data = {
  65. /** 名称 */
  66. name: '基础注释文本',
  67. /** 图标 */
  68. type: "Text",
  69. /** 位置 */
  70. pos: { x: event.x, y: event.y },
  71. /** 由应用自己定义 */
  72. properties: {
  73. type: "BaseExplain",
  74. },
  75. style: {
  76. default: {
  77. text: '请在右侧属性栏输入文字!',
  78. color: "#646c73",
  79. font: 14,
  80. backgroundcolor: "#f7f9facc",
  81. }
  82. }
  83. };
  84. const item = new SBaseExpainEdit(null, data);
  85. item.moveTo(event.x, event.y);
  86. item.selectable = true;
  87. item.moveable = true;
  88. this.addItem(item);
  89. this.grabItem = null;
  90. item.connect("onContextMenu", this, this.getItem);
  91. this.finishCreated(item);
  92. }
  93. /**
  94. * 新增基础箭头(折线)
  95. *
  96. * @param event 鼠标事件
  97. */
  98. addPolyLineArrow(event: SMouseEvent): void {
  99. const data = {
  100. name: '基础箭头',
  101. type: "Arrow",
  102. pos: { x: 0, y: 0 },
  103. properties: {
  104. type: "BaseArrow",
  105. },
  106. style: {
  107. outLine: [{ x: event.x, y: event.y }],
  108. begin: SArrowStyleType.None, //开端箭头样式
  109. end: SArrowStyleType.None, //结尾箭头样式
  110. isMove: true, //是否可以移动
  111. default: {}
  112. }
  113. };
  114. const item = new SBaseArrow(null, data);
  115. item.status = SItemStatus.Create;
  116. item.selectable = true;
  117. this.addItem(item);
  118. this.grabItem = item;
  119. item.connect("finishCreated", this, this.finishCreated);
  120. item.connect("onContextMenu", this, this.getItem);
  121. if (this.view) {
  122. this.view.update();
  123. }
  124. }
  125. /**
  126. * 新增基础箭头(多边形)
  127. *
  128. * @param event 鼠标事件
  129. */
  130. addPolygonArrow(event: SMouseEvent): void {
  131. const data = {
  132. /** 名称 */
  133. name: '多边形箭头',
  134. /** 图标(Image),线类型(Line) */
  135. type: "ArrowPolygon",
  136. /** 缩放比例(Image),线类型(Line) */
  137. scale: { x: 1, y: 1, z: 1 },
  138. /** 缩放比例(Image),线类型(Line) */
  139. rolate: { x: 0, y: 0, z: 0 },
  140. /** 位置 */
  141. pos: { x: 0, y: 0 },
  142. /** 由应用自己定义 */
  143. properties: {
  144. type: "BaseArrowPolygon" // 自定义类型用于区分mark与node
  145. },
  146. style: {
  147. line: [{ x: event.x, y: event.y }],
  148. default: {
  149. }
  150. }
  151. };
  152. const item = new SBaseArrowPolyEdit(null, data);
  153. item.status = SItemStatus.Create;
  154. item.selectable = true;
  155. this.addItem(item);
  156. this.grabItem = item;
  157. item.connect("finishCreated", this, this.finishCreated);
  158. item.connect("onContextMenu", this, this.getItem);
  159. if (this.view) {
  160. this.view.update();
  161. }
  162. }
  163. // /**
  164. // * 添加基本设备 item
  165. // *
  166. // * @param event 鼠标事件
  167. // * @param legendObj 图例样式
  168. // */
  169. // addEuqipment(event: SMouseEvent, legendObj: any): void {
  170. // const data = {
  171. // nodeId: uuid(),
  172. // /** 名称 */
  173. // name: '基础设备',
  174. // /** 返回物理世界对象 ID 列表 */
  175. // attachObjectIds: [],
  176. // size: { width: 50, height: 50 },
  177. // /** 图标 (Image),线类型 (Line) */
  178. // type: "Image",
  179. // /** 位置 */
  180. // pos: { x: event.x, y: event.y },
  181. // /** 由应用自己定义 */
  182. // properties: {
  183. // type: "BaseEquipment",
  184. // infoMsgList: [{
  185. // }]
  186. // },
  187. // style: {
  188. // default: {
  189. // strokecolor: "#c0ccda",
  190. // url: require('./../../../assets/images/equip/' + legendObj.url),
  191. // }
  192. // }
  193. // }
  194. // const item = new SBaseEquipment(null, data);
  195. // item.status = SItemStatus.Create;
  196. // this.addItem(item);
  197. // item.selectable = true;
  198. // item.moveable = true;
  199. // this.grabItem = item;
  200. // this.finishCreated(item)
  201. // item.connect("onContextMenu", this, this.getItem);
  202. // if (this.view) {
  203. // this.view.update();
  204. // }
  205. // }
  206. /**
  207. * 新增管道
  208. *
  209. * @param event 鼠标事件
  210. */
  211. addBasePipe(event: SMouseEvent, legendObj: any) {
  212. const anc = this.clickIsAnchor(event);
  213. if (anc) {
  214. const p = anc.mapToScene(0, 0)
  215. anc.isConnected = true;
  216. event.x = p.x;
  217. event.y = p.y;
  218. };
  219. const data = {
  220. name: legendObj.name,
  221. graphElementId: legendObj.id,
  222. lineType: '',
  223. node1Id: '',
  224. node2Id: '',
  225. anchor1Id: '',
  226. anchor2Id: '',
  227. pointList: [{ x: event.x, y: event.y }],
  228. properties: {
  229. type: "BasePipe",
  230. },
  231. style: {
  232. default: {
  233. strokeColor: legendObj.color,
  234. lineWidth: 4
  235. }
  236. }
  237. };
  238. const item = new SBasePipe(null, data);
  239. item.status = SItemStatus.Create;
  240. item.selectable = true;
  241. this.addItem(item);
  242. this.grabItem = item;
  243. // 起始锚点
  244. item.startAnchor = anc;
  245. if (anc) {
  246. anc.parent?.connect('changePos', item, item.changePos)
  247. item.anchor1Id = anc.id;
  248. item.node1Id = anc.parent ? anc.parent.nodeId : '';
  249. }
  250. console.log('anc', anc)
  251. item.connect("finishCreated", this, this.finishCreated);
  252. item.connect("onContextMenu", this, this.getItem);
  253. if (this.view) {
  254. this.view.update();
  255. }
  256. }
  257. /**
  258. * 重做
  259. */
  260. redo(): void {
  261. if (this.grabItem && this.grabItem instanceof SGraphEdit) {
  262. this.grabItem.redo()
  263. } else {
  264. this.undoStack.redo();
  265. }
  266. }
  267. /**
  268. * 撤销
  269. */
  270. undo(): void {
  271. if (this.grabItem && this.grabItem instanceof SGraphEdit) {
  272. this.grabItem.undo()
  273. } else {
  274. this.undoStack.undo();
  275. }
  276. }
  277. /**
  278. * 删除
  279. *
  280. * @return 删除的图例
  281. */
  282. deleteItem(itemArr: SGraphItem[] = []): any {
  283. let itemList = null;
  284. if (itemArr && itemArr.length && itemArr[0]) {
  285. itemList = [...itemArr]
  286. } else {
  287. if (this.selectContainer.count == 0) {
  288. return []
  289. }
  290. itemList = this.selectContainer.itemList;
  291. this.undoStack.push(new SGraphDeleteListCommand(this, [...itemList]))
  292. }
  293. // 推入 undo/redo 栈
  294. itemList.forEach((element: any) => {
  295. this.removeItem(element)
  296. });
  297. if (this.view) {
  298. this.view.update()
  299. }
  300. return itemList
  301. }
  302. /**
  303. * 框选
  304. */
  305. addRectSelect(event: SMouseEvent): void {
  306. let point = new SPoint(event.x, event.y);
  307. let rect = new SRectSelectItem(null, point);
  308. this.addItem(rect);
  309. this.grabItem = rect;
  310. }
  311. /**
  312. * 计算框选交集
  313. *
  314. * @param ctrl 是否点击ctrl
  315. */
  316. groupSelect(ctrl: boolean) {
  317. if (this.grabItem instanceof SRectSelectItem) {
  318. const rect = this.grabItem.boundingRect();
  319. this.arrToSelect(this.root.children, rect)
  320. }
  321. }
  322. /**
  323. * 选中item:框选
  324. *
  325. * @param arr 实例数组
  326. * @param rect 框选矩形区域
  327. */
  328. private arrToSelect(arr: SGraphItem[], rect: SRect) {
  329. if (Array.isArray(arr) && arr.length) {
  330. arr.forEach(t => {
  331. if (!(t instanceof SGraphSelectContainer) && t.parent) {
  332. let temp = t.boundingRect();
  333. let lefttop = t.mapToScene(temp.left, temp.top)
  334. let rightbottom = t.mapToScene(temp.right, temp.bottom)
  335. let r = new SRect(lefttop, rightbottom)
  336. if (rect.isIn(r)) {
  337. this.selectContainer.toggleItem(t)
  338. }
  339. }
  340. })
  341. }
  342. }
  343. /////////////////////////////////////////////////////////////////////
  344. // 鼠标事件
  345. /**
  346. * 鼠标左键按下
  347. *
  348. * @param event 鼠标事件参数
  349. */
  350. onMouseDown(event: SMouseEvent): any {
  351. if (!super.onMouseDown(event) && 1 == event.buttons) {
  352. this.addRectSelect(event);
  353. }
  354. }
  355. /**
  356. * 鼠标抬起
  357. *
  358. * @param event 鼠标事件参数
  359. */
  360. onMouseUp(event: SMouseEvent): boolean {
  361. if (this.grabItem) {
  362. // 鼠标抬起时,如果grabItem为框选则删除框选item
  363. if (this.grabItem instanceof SRectSelectItem) {
  364. this.removeItem(this.grabItem);
  365. this.groupSelect(false);
  366. this.grabItem = null;
  367. if (this.view) {
  368. this.view.update()
  369. }
  370. return true;
  371. }
  372. return this.grabItem.onMouseUp(event);
  373. }
  374. return super.onMouseUp(event)
  375. }
  376. /**
  377. * 复制
  378. */
  379. copy(): void {
  380. const itemList = this.save(false);
  381. if (itemList) {
  382. // 删除对应得id
  383. itemList.markers.map((item: any) => {
  384. delete item.id;
  385. delete item.graphId;
  386. delete item.markerId;
  387. return item
  388. });
  389. itemList.nodes.map((item: any) => {
  390. delete item.id;
  391. delete item.graphId;
  392. delete item.markerId;
  393. return item
  394. });
  395. itemList.relations.map((item: any) => {
  396. delete item.id;
  397. delete item.graphId;
  398. delete item.markerId;
  399. return item
  400. });
  401. this.copyString = itemList
  402. // 获取input dom
  403. const input = document.createElement('input');
  404. input.setAttribute('id', 'COPYINPUT')
  405. input.value = JSON.stringify(this.copyString)
  406. sessionStorage.setItem("copyString", input.value);
  407. document.body.appendChild(input);
  408. input.select()
  409. document.execCommand('copy');
  410. input.style.display = 'none';
  411. console.log(input.value, Date.now());
  412. document.body.removeChild(input)
  413. }
  414. }
  415. /**
  416. * 添加多个item命令
  417. *
  418. * @param itemList 鼠标事件对象
  419. */
  420. addListCommand(itemList: SGraphEdit[]): void {
  421. this.undoStack.push(new SGraphAddListCommand(this, itemList));
  422. }
  423. /**
  424. * 图例置顶、置底
  425. *
  426. * @parm type 设置类型
  427. */
  428. setOrder(type: string) {
  429. this.selectContainer.setOrder(SOrderSetType[type])
  430. }
  431. /**
  432. * 设置管线结束锚点
  433. */
  434. setTipeEndanchor(event: SMouseEvent): void {
  435. if (this.grabItem instanceof SBasePipe) {
  436. const anc = this.clickIsAnchor(event);
  437. if (anc) {
  438. const p = anc.mapToScene(0, 0)
  439. anc.isConnected = true;
  440. event.x = p.x;
  441. event.y = p.y;
  442. if (this.grabItem.status == SItemStatus.Create) {
  443. if (this.grabItem.pointList.length) {
  444. this.grabItem.endAnchor = anc;
  445. anc.parent?.connect('changePos', this.grabItem, this.grabItem.changePos)
  446. }
  447. this.grabItem.anchor2Id = anc.id
  448. this.grabItem.node2Id = anc.parent.nodeId
  449. this.grabItem.onMouseDown(event)
  450. this.grabItem.status = SItemStatus.Normal;
  451. this.finishCreated(this.grabItem)
  452. return
  453. }
  454. }
  455. this.grabItem.onMouseDown(event)
  456. }
  457. }
  458. /**
  459. * 获取item (常用与场景外的调用F)
  460. *
  461. * @params isAll 是否为全部item数据
  462. * @return obj 返回保存的数据接口
  463. */
  464. save(isAll: boolean = true): any {
  465. }
  466. }