1
wangjuncheng
2025-06-10 6d989ac3e9902f7dfbc1d8472270929a04ddb517
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<template>
  <div class="custom-panel">
    <div class="panel-content">
      <!-- 开挖深度设置 -->
      <el-form :model="form" label-width="80px">
        <el-form-item label="开挖深度">
          <el-input v-model="form.digDepth" placeholder="请输入开挖深度(m)" />
        </el-form-item>
      </el-form>
      <!-- 按钮区域 -->
      <div class="button-group">
        <el-button type="primary" @click="handleDraw">绘制</el-button>
        <el-button type="success" @click="handleConfirm">确认</el-button>
        <el-button type="danger" @click="handleClear">清除</el-button>
      </div>
    </div>
  </div>
</template>
 
<script setup>
import { ref, reactive, watch, defineEmits } from 'vue';
import { ElForm, ElFormItem, ElInput, ElButton, ElMessage } from 'element-plus';
const emit = defineEmits(['update-excavation-data']);
const form = reactive({
  digDepth: '100'
});
let excavationInstance = null;
const currentPoints = reactive([]);
const isDrawing = ref(false);
let clickHandler = null;
const handleDraw = () => {
  if (!window.viewer) {
    console.error('Cesium Viewer 尚未初始化');
    return;
  }
 
  const viewer = window.viewer;
 
  // 清空之前的数据
  currentPoints.splice(0, currentPoints.length);
  isDrawing.value = true;
 
  // 销毁旧的监听器
  if (clickHandler) {
    clickHandler.destroy();
  }
 
  // 立即创建开挖对象(不依赖初始点)
  createExcavation();
 
  // 开始监听点击事件
  clickHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
  clickHandler.setInputAction((movement) => {
    const cartesian = viewer.camera.pickEllipsoid(movement.position, viewer.scene.globe.ellipsoid);
    if (cartesian) {
      currentPoints.push(cartesian.clone());
    }
  }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
};
 
const createExcavation = () => {
  const depth = parseFloat(form.digDepth);
  if (!isNaN(depth)) {
    excavationInstance = window.earthCtrl.analysis.createTerrainExcavation({
      height: depth,
      callback: function (a) {
      },
    });
  }else{
    ElMessage.warning('请输入有效的开挖深度!');
  }
};
 
watch(
  () => form.digDepth,
  (newVal) => {
    const num = parseFloat(newVal);
    if (!isNaN(num) && excavationInstance) {
      excavationInstance.setDeepth?.(num);
    }
  }
);
// 点击确认按钮(打印当前坐标点集合和深度)
const handleConfirm = () => {
  if (currentPoints.length === 0) {
    ElMessage.warning('请选取开沟有效的坐标点!');
    return;
  }
  const depth = parseFloat(form.digDepth);
  // 转换为普通对象数组
  const plainPoints = currentPoints.map(point => ({
    x: point.x,
    y: point.y,
    z: point.z
  }));
  const result = {
    points: plainPoints,
    depth: depth
  };
  emit('update-excavation-data', result);
  ElMessage.success('实现开挖功能!');
 
};
const handleClear = () => {
  isDrawing.value = false;
  // 销毁点击事件监听器
  if (clickHandler) {
    clickHandler.destroy();
    clickHandler = null;
  }
  // 清空临时点
  currentPoints.splice(0, currentPoints.length);
  // 移除开挖对象
  if (excavationInstance) {
    excavationInstance.removeFromMap?.();
    excavationInstance = null;
  }
};
</script>
 
<style scoped>
.custom-panel {
  padding: 20px;
  width: 290px;
  background: url("@/assets/img/tools/plotting_new.png") no-repeat;
  filter: opacity(83%);
  background-size: 100% 100%;
  box-sizing: border-box;
}
 
.panel-content {
  display: flex;
  flex-direction: column;
}
 
.button-group {
  display: flex;
  justify-content: space-around;
}
</style>