wangjuncheng
2025-05-28 4a0b0a87f183abae6aff6174436be2ccbc507be2
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<template>
    <div style="display: flex; justify-content: space-between;">
        <div @click="initPickHandler">
            <img v-if="!isPicking" src="@/assets/img/timeline/断面.png" style="width: 26px;height: 26px;" />
            <img v-else src="@/assets/img/timeline/已断面.png" style="width: 26px;height: 26px;" />
        </div>
        <div @click="confirmPoints">
            <img v-if="!isUploaded" src="@/assets/img/timeline/上传.png" style="width: 26px;height: 26px;" />
            <img v-else src="@/assets/img/timeline/已上传.png" style="width: 26px;height: 26px;" />
        </div>
        <div @click="clearPoints">
            <img src="@/assets/img/timeline/清除.png" style="width: 26px;height: 26px;" />
 
        </div>
    </div>
</template>
 
<script setup>
import { ElMessage } from 'element-plus';
import { ref, onMounted ,defineExpose } from "vue";
 
const viewer = window.viewer;
 
let pickedPointsCross = ref([]);
let pickHandlerCross = null;
let isWallCreated = ref(false); // 新增状态变量,标记墙体是否已创建
let isPicking = ref(false); // 是否正在拾取点
const isUploaded = ref(false); // 控制是否已上传
// 获取断面坐标
function getPickPosition(windowPosition) {
    if (!viewer) return null;
    const cartesian = viewer.scene.pickPosition(windowPosition);
    if (!cartesian) return null;
    const cartographic = Cesium.Cartographic.fromCartesian(cartesian);
    const longitude = Cesium.Math.toDegrees(cartographic.longitude);
    const latitude = Cesium.Math.toDegrees(cartographic.latitude);
    const terrainHeight = viewer.scene.globe.getHeight(cartographic);
    if (!terrainHeight) return null;
    const positionWithTerrainHeight = Cesium.Cartesian3.fromRadians(
        cartographic.longitude,
        cartographic.latitude,
        terrainHeight
    );
    return {
        cartesian: positionWithTerrainHeight,
        longitude,
        latitude
    };
}
 
// 选取两个断面点坐标并绘制断面截面
function addPointToViewer(point) {
    if (pickedPointsCross.value.length >= 2) {
        clearPoints();
    }
    pickedPointsCross.value.push(point);
    drawPointOnMap(point);
    if (pickedPointsCross.value.length === 2) {
        // ElMessage.success('当前两点坐标已选取完成,正在生成断面截面!');
        drawWall(pickedPointsCross.value[0], pickedPointsCross.value[1]);
        isWallCreated.value = true; // 设置为已创建墙体
    }
}
 
let pickedEntitiesIds = ref([]); // 用于存储创建的点和墙的ID
 
// 绘制两个断面点坐标,并记录其ID
function drawPointOnMap(point) {
    const entity = viewer.entities.add({
        position: point.cartesian,
        point: {
            color: Cesium.Color.RED,
            outlineColor: Cesium.Color.YELLOW,
            outlineWidth: 2,
            pixelSize: 8 // 圆点半径大小
        }
    });
    pickedEntitiesIds.value.push(entity.id); // 记录实体ID
}
 
// 创建断面截面,并记录其ID
function drawWall(startPoint, endPoint) {
    const entity = viewer.entities.add({
        wall: {
            positions: [startPoint.cartesian, endPoint.cartesian],
            material: Cesium.Color.YELLOW,
            heightReference: Cesium.HeightReference.RELATIVE_TO_GROUND,
        }
    });
    pickedEntitiesIds.value.push(entity.id); // 记录实体ID
}
 
// 修改后的清除函数,只清除创建的点和墙
function clearPoints() {
    for (const id of pickedEntitiesIds.value) {
        viewer.entities.remove(viewer.entities.getById(id));
    }
    pickedPointsCross.value = [];
    pickedEntitiesIds.value = [];
    isWallCreated.value = false;
    isUploaded.value = false;
}
 
// 拾取点坐标然后画点(简化版)
function initPickHandler() {
    // 切换状态:如果之前在拾取,这次就是取消拾取
    if (isPicking.value) {
        if (pickHandlerCross) {
            pickHandlerCross.destroy();
            pickHandlerCross = null;
        }
        isPicking.value = false;
        isUploaded.value = false;
        ElMessage.info('已关闭--断面截面--拾取点坐标功能!');
        return;
    }
 
    // 进入拾取模式
    ElMessage.success(`开始--断面截面--拾取坐标功能,请点击地图选择点位!选取完请及时关闭,避免影响其他功能!`);
    isPicking.value = true;
 
    if (!viewer?.scene?.canvas) return;
 
    // 销毁旧的 handler
    if (pickHandlerCross) {
        pickHandlerCross.destroy();
        pickHandlerCross = null;
    }
 
    // 创建新 handler
    pickHandlerCross = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
 
    const clickAction = (movement) => {
        const position = getPickPosition(movement.position);
        if (position) {
            addPointToViewer(position);
        }
    };
 
    pickHandlerCross.setInputAction(clickAction, Cesium.ScreenSpaceEventType.LEFT_CLICK);
}
// 确认按钮点击事件,发送请求调用接口
function confirmPoints() {
    if (pickedPointsCross.value.length < 2) {
        ElMessage.warning('请先选择两个点后再进行确认!');
        return;
    }
 
    const point1 = pickedPointsCross.value[0];
    const point2 = pickedPointsCross.value[1];
 
    console.log('第一个点信息:', {
        longitude: point1.longitude,
        latitude: point1.latitude,
        cartesian: point1.cartesian
    });
 
    console.log('第二个点信息:', {
        longitude: point2.longitude,
        latitude: point2.latitude,
        cartesian: point2.cartesian
    });
 
    isUploaded.value = true; // 设置为已上传状态
    ElMessage.success('正在进行--断面截面--数据分析上传,请稍等...');
}
defineExpose({
    clearPoints
});
</script>