SBaseEditScene.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 { SGraphEditScene, SGraphAddCommand } from "../edit"
  27. import { SMouseEvent } from "@persagy-web/base/lib";
  28. import { SPoint, SFont, SColor, SRect, SArrowStyleType } from '@persagy-web/draw/lib';
  29. import { SRectSelectItem, SItemStatus } from '@persagy-web/big/lib';
  30. import { SGraphItem, SGraphSelectContainer } from "@persagy-web/graph/";
  31. import { uuid } from "./until";
  32. import { SBaseExpainEdit, SBaseArrow, SBaseEquipment, SBasePipeUninTool } from "./"
  33. /**
  34. * big-edit 场景
  35. *
  36. * @author 韩耀龙 <han_yao_long@163.com>
  37. */
  38. export class SBaseEditScene extends SGraphEditScene {
  39. /**图例节点 */
  40. Nodes: any = []; // 图例节点,所有与工程信息化相关的图例(图标类型与区域)
  41. /**图例节点 */ // 与工程信息无关的标识对象(增加文本注释,图上的图片说明)
  42. Markers: any = [];
  43. /** 管线对象 */
  44. Relations: any = [];
  45. copyString: any[] = []
  46. constructor() {
  47. super()
  48. }
  49. /**
  50. * 新增基础类注释
  51. *
  52. * @param event 鼠标事件参数
  53. */
  54. addExplainItem(event: SMouseEvent): void {
  55. const data = {
  56. /** 名称 */
  57. name: '基础注释文本',
  58. /** 图标 */
  59. type: "Text",
  60. /** 位置 */
  61. pos: { x: event.x, y: event.y },
  62. /** 由应用自己定义 */
  63. properties: {
  64. type: "BaseExplain",
  65. },
  66. style: {
  67. default: {
  68. text: '请在右侧属性栏输入文字!',
  69. color: "#646c73",
  70. font: 14,
  71. backgroundcolor: "#f7f9facc",
  72. }
  73. }
  74. }
  75. const item = new SBaseExpainEdit(null, data);
  76. item.moveTo(event.x, event.y);
  77. item.selectable = true;
  78. item.moveable = true;
  79. this.addItem(item);
  80. this.undoStack.push(new SGraphAddCommand(this, item));
  81. this.grabItem = null;
  82. this.finishCreated(item);
  83. }
  84. /**
  85. * 新增基础箭头(折线)
  86. *
  87. * @param event 鼠标事件
  88. */
  89. addPolyLineArrow(event: SMouseEvent): void {
  90. const data = {
  91. name: '基础箭头',
  92. type: "Arrow",
  93. pos: { x: 0, y: 0 },
  94. properties: {
  95. type: "BaseArrow",
  96. },
  97. style: {
  98. outLine: [{ x: event.x, y: event.y }],
  99. begin: SArrowStyleType.None, //开端箭头样式
  100. end: SArrowStyleType.None, //结尾箭头样式
  101. isMove: true, //是否可以移动
  102. default: {
  103. }
  104. }
  105. }
  106. const item = new SBaseArrow(null, data);
  107. item.status = SItemStatus.Create;
  108. item.selectable = true;
  109. this.addItem(item);
  110. this.undoStack.push(new SGraphAddCommand(this, item));
  111. this.grabItem = item;
  112. item.connect("finishCreated", this, this.finishCreated);
  113. item.connect("onContextMenu", this, this.getItem);
  114. if (this.view) {
  115. this.view.update();
  116. }
  117. }
  118. /**
  119. * 添加基本设备item
  120. *
  121. * @param event 鼠标事件
  122. * @param legendObj 图例样式
  123. */
  124. addEuqipment(event: SMouseEvent, legendObj: any): void {
  125. const data = {
  126. /** 名称 */
  127. name: '基础设备',
  128. num: 1,
  129. size: { width: 50, height: 50 },
  130. /** 图标(Image),线类型(Line) */
  131. type: "Image",
  132. /** 位置 */
  133. pos: { x: event.x, y: event.y },
  134. /** 由应用自己定义 */
  135. properties: {
  136. type: "baseEquipment",
  137. },
  138. style: {
  139. default: {
  140. strokecolor: "#c0ccda",
  141. url: require('./../../../assets/images/equip/' + legendObj.url),
  142. }
  143. }
  144. }
  145. const item = new SBaseEquipment(null, data);
  146. item.status = SItemStatus.Create;
  147. this.addItem(item);
  148. this.undoStack.push(new SGraphAddCommand(this, item));
  149. item.selectable = true;
  150. item.moveable = true;
  151. this.grabItem = item;
  152. item.connect("finishCreated", this, this.finishCreated);
  153. item.connect("onContextMenu", this, this.getItem);
  154. if (this.view) {
  155. this.view.update();
  156. }
  157. } // Function addEuqipment()
  158. /**
  159. * 新增管道
  160. *
  161. * @param event 鼠标事件
  162. */
  163. addBasePipe(event: SMouseEvent) {
  164. const data = {
  165. name: '管道',
  166. type: "line",
  167. pos: { x: 0, y: 0 },
  168. properties: {
  169. type: "BasePolyline",
  170. },
  171. style: {
  172. outLine: [{ x: event.x, y: event.y }],
  173. default: {
  174. }
  175. }
  176. }
  177. const item = new SBasePipe(null, data);
  178. item.status = SItemStatus.Create;
  179. item.selectable = true;
  180. this.addItem(item);
  181. this.undoStack.push(new SGraphAddCommand(this, item));
  182. this.grabItem = item;
  183. item.connect("finishCreated", this, this.finishCreated);
  184. item.connect("onContextMenu", this, this.getItem);
  185. if (this.view) {
  186. this.view.update();
  187. }
  188. }
  189. /**
  190. * 添加基本管道联通器
  191. *
  192. * @param event 鼠标事件
  193. * @param cmd 命令
  194. */
  195. addPipeUninTool(event: SMouseEvent, cmd: string): void {
  196. const cmdList = {
  197. 'wantou': 2,
  198. 'santong': 3,
  199. 'sitong': 4,
  200. }
  201. const data = {
  202. /** 名称 */
  203. name: '基础管道接头',
  204. /** 图标(Image),线类型(Line) */
  205. type: "Image",
  206. /** 位置 */
  207. pos: { x: event.x, y: event.y },
  208. /** 由应用自己定义 */
  209. properties: {
  210. type: "BasePipeUninTool",
  211. },
  212. style: {
  213. uninToolType: cmdList[cmd] ? cmdList[cmd] : 2, //2,3,4 分别分二头连接器、三头连接器、四头连接器
  214. default: {
  215. strokecolor: "#c0ccda",
  216. }
  217. }
  218. }
  219. const item = new SBasePipeUninTool(null, data);
  220. item.status = SItemStatus.Create;
  221. item.selectable = true;
  222. item.moveable = true;
  223. this.addItem(item);
  224. this.undoStack.push(new SGraphAddCommand(this, item));
  225. this.grabItem = item;
  226. item.connect("finishCreated", this, this.finishCreated);
  227. item.connect("onContextMenu", this, this.getItem);
  228. if (this.view) {
  229. this.view.update();
  230. }
  231. }
  232. /**
  233. * 重做
  234. *
  235. * @return any
  236. */
  237. redo(): void {
  238. if (this.grabItem && this.grabItem.redo) {
  239. this.grabItem.redo()
  240. } else {
  241. this.undoStack.redo();
  242. }
  243. }
  244. /**
  245. * 撤销
  246. *
  247. * @return any
  248. */
  249. undo(): void {
  250. if (this.grabItem && this.grabItem.undo) {
  251. this.grabItem.undo()
  252. } else {
  253. this.undoStack.undo();
  254. }
  255. }
  256. /**
  257. * 删除
  258. *
  259. * @return item[]
  260. */
  261. deleteItem(): any {
  262. if (this.selectContainer.count == 0) {
  263. return []
  264. }
  265. let itemList = this.selectContainer.itemList;
  266. itemList.forEach((element: any) => {
  267. this.removeItem(element)
  268. });
  269. if (this.view) {
  270. this.view.update()
  271. }
  272. return itemList
  273. }
  274. /**
  275. * 框选
  276. *
  277. */
  278. addRectSelect(event: SMouseEvent): void {
  279. let point = new SPoint(event.x, event.y);
  280. let rect = new SRectSelectItem(null, point);
  281. this.addItem(rect);
  282. this.grabItem = rect;
  283. }
  284. /**
  285. * 计算框选交集
  286. */
  287. groupSelect(ctrl: boolean) {
  288. // if (!ctrl) {
  289. // this.selectContainer.clear()
  290. // }
  291. if (this.grabItem instanceof SRectSelectItem) {
  292. const rect = this.grabItem.boundingRect();
  293. this.arrToSelect(this.root.children, rect)
  294. }
  295. }
  296. /**
  297. * 选中item:框选
  298. */
  299. private arrToSelect(arr: SGraphItem[], rect: SRect) {
  300. if (Array.isArray(arr) && arr.length) {
  301. arr.forEach(t => {
  302. if (!(t instanceof SGraphSelectContainer) && t.parent) {
  303. let temp = t.boundingRect();
  304. let lefttop = t.mapToScene(temp.left, temp.top)
  305. let rightbottom = t.mapToScene(temp.right, temp.bottom)
  306. let r = new SRect(lefttop, rightbottom)
  307. if (rect.isIn(r)) {
  308. this.selectContainer.toggleItem(t)
  309. }
  310. }
  311. })
  312. }
  313. }
  314. /////////////////////////////////////////////////////////////////////
  315. // 鼠标事件
  316. /**
  317. * 鼠标左键按下
  318. *
  319. * @param event 鼠标事件参数
  320. */
  321. onMouseDown(event: SMouseEvent): any {
  322. if (!super.onMouseDown(event) && 1 == event.buttons) {
  323. this.addRectSelect(event);
  324. }
  325. }
  326. /**
  327. * 鼠标抬起
  328. *
  329. * @param event 鼠标事件参数
  330. */
  331. onMouseUp(event: SMouseEvent): boolean {
  332. if (this.grabItem) {
  333. // 鼠标抬起时,如果grabItem为框选则删除框选item
  334. if (this.grabItem instanceof SRectSelectItem) {
  335. this.removeItem(this.grabItem);
  336. this.groupSelect(false);
  337. this.grabItem = null;
  338. if (this.view) {
  339. this.view.update()
  340. }
  341. return true;
  342. }
  343. return this.grabItem.onMouseUp(event);
  344. }
  345. return super.onMouseUp(event)
  346. }
  347. /**
  348. * 复制
  349. *
  350. */
  351. copy() {
  352. const itemList = this.selectContainer.itemList
  353. if (itemList.length) {
  354. itemList.forEach(t => {
  355. const data = JSON.parse(JSON.stringify(t.toData()))
  356. data.ID = uuid()
  357. this.copyString.push(data)
  358. })
  359. // 生成复制字符串
  360. console.log(this.copyString)
  361. // 获取input dom
  362. const input = document.createElement('input');
  363. input.setAttribute('id', 'COPYINPUT')
  364. input.value = JSON.stringify(this.copyString)
  365. sessionStorage.setItem("copyString", input.value);
  366. document.body.appendChild(input);
  367. input.select()
  368. document.execCommand('copy');
  369. input.style.display = 'none';
  370. console.log(input.value, Date.now());
  371. document.body.removeChild(input)
  372. }
  373. }
  374. /**
  375. * 粘贴
  376. *
  377. */
  378. paste() {
  379. const copyList = JSON.parse(JSON.stringify(this.copyString));
  380. copyList.forEach((t: any) => {
  381. if (this.view) {
  382. t.Pos.X += 10,
  383. t.Pos.Y += 10
  384. }
  385. t.moveable = true;
  386. console.log('t', t)
  387. this.addItem(t)
  388. });
  389. this.view ? this.view.update() : ''
  390. console.log(this.view)
  391. }
  392. }