clip-plane.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // 引入依赖
  2. import { watch, reactive, toRefs, onBeforeUnmount } from "vue";
  3. import { initHandler, handlerDrawing, clearHandlerDrawing } from "../../../js/common/drawHandler.js" //公共handler.js
  4. import { Edit, clearEditHandler } from "../../../js/common/editHandler.js" //编辑
  5. import tool from '../../../js/tool/tool.js' //提示等工具
  6. import resource from '../../../js/local/lang.js' //语言资源
  7. import createTooltip from '../../../js/tool/tooltip2.js';
  8. import { storeState } from '../../../js/store/store.js' //简单局部状态管理
  9. import { setAllModelClipPlane, clearAllModelClip, isClippingPlaneSupported } from '../../../js/common/clip-model.js'
  10. function clipPlaneAnalysis(props) {
  11. // 设置默认值数据
  12. let state = reactive({
  13. isEdit: true,//是否编辑
  14. isEditZ: false,
  15. lineVisible: true,//是否显示绘制线
  16. PlanePositions: []
  17. });
  18. // 传入props改变默认值
  19. if (props) {
  20. for (let key in props) {
  21. if (state.hasOwnProperty(key)) {
  22. state[key] = props[key]
  23. } else {
  24. tool.Message.errorMsg(resource.AttributeError + key);
  25. }
  26. }
  27. }
  28. // 初始化数据
  29. let layers;
  30. let planePosition = [];
  31. /*
  32. ***平面分析模块***
  33. */
  34. //初始化分析区域 (后面有需要可以添加监听)
  35. if (props && props.PlanePositions) {
  36. clipPolygonUpdate(props.PlanePositions);
  37. }
  38. if (storeState.isViewer) {
  39. layers = viewer.scene.layers.layerQueue;
  40. if (!window.tooltip) {
  41. window.tooltip = createTooltip(viewer._element);
  42. }
  43. }
  44. //viewer 初始化完成的监听
  45. watch(() => storeState.isViewer, val => {
  46. if (val) {
  47. layers = viewer.scene.layers.layerQueue;
  48. if (!window.tooltip) {
  49. window.tooltip = createTooltip(viewer._element);
  50. }
  51. }
  52. })
  53. // 分析
  54. function clipPlaneStart(e) {
  55. console.log(1)
  56. e.preventDefault();
  57. tooltip.setVisible(false);
  58. tooltip.showAt(' <p>点击鼠标左键开始绘制</p><p>绘制三点确定一个平面</p><p>点击鼠标右键结束绘制</p>', '230px');
  59. if (!layers ) {
  60. layers = viewer.scene.layers.layerQueue;
  61. }
  62. for (let layer of layers) {
  63. layer.selectEnabled = false;
  64. // 设置被裁剪对象的颜色
  65. layer.clipLineColor = new Cesium.Color(1, 1, 1, 0);
  66. }
  67. if (!window.handlerPolygon) {
  68. initHandler("Polygon");
  69. }
  70. handlerDrawing("Polygon", state.lineVisible).then(
  71. res => {
  72. let position = res.result.object.positions;
  73. clipPlaneUpdate(position);
  74. // handlerPolygon.polygon.show = false;
  75. // handlerPolygon.polyline.show = false;
  76. window.handlerPolygon.deactivate();
  77. tooltip.setVisible(false);
  78. if (true) {
  79. Edit(planePosition, state.isEditZ, clipPlaneUpdate);
  80. }
  81. },
  82. err => {
  83. console.log(err);
  84. }
  85. );
  86. window.handlerPolygon.activate();
  87. if (!scene.pickPositionSupported) {
  88. alert("不支持深度纹理,无法绘制多边形,裁剪功能无法使用!");
  89. }
  90. };
  91. // 更新
  92. function clipPlaneUpdate(p) {
  93. planePosition = p;
  94. for (let layer of layers) {
  95. // layer.clearCustomClipBox();
  96. layer.setCustomClipPlane(
  97. p[0],
  98. p[1],
  99. p[2]
  100. );
  101. }
  102. // 同时裁剪场景中的 glTF Model(Entity 加载的 .glb)
  103. // ClippingPlaneCollection 用三点确定一个平面进行裁剪
  104. if (isClippingPlaneSupported()) {
  105. setAllModelClipPlane(viewer, p)
  106. }
  107. };
  108. // 清除
  109. function clearClipPlane(e) {
  110. e.preventDefault();
  111. tooltip.setVisible(false);
  112. planePosition = [];
  113. if (!window.handlerPolygon) return;
  114. clearHandlerDrawing("Polygon");
  115. for (let layer of layers) {
  116. layer.clearCustomClipBox();
  117. }
  118. // 同时清除 glTF Model 的裁剪
  119. clearAllModelClip(viewer)
  120. };
  121. // 监听
  122. watch(() => state.isEdit, val => {
  123. if (val || window.handlerPolygon) {
  124. Edit(planePosition, state.isEditZ, clipPlaneUpdate);
  125. } else {
  126. clearEditHandler("Polygon");
  127. if (window.handlerPolygon && window.handlerPolygon.polygon) {
  128. window.handlerPolygon.polygon.show = false;
  129. }
  130. }
  131. });
  132. watch(() => state.isEditZ, val => {
  133. if (window.editHandler) {
  134. window.editHandler.isEditZ = val;
  135. }
  136. });
  137. // 销毁
  138. onBeforeUnmount(() => {
  139. layers = undefined;
  140. })
  141. return {
  142. ...toRefs(state),
  143. clipPlaneStart,
  144. clearClipPlane,
  145. planePosition
  146. };
  147. };
  148. export default clipPlaneAnalysis