2023西安数博会CIM演示-【前端】-Web
AdaKing88
2023-08-21 bc03b832caa49bbcd2674fe4cae3701b5059bf95
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<template>
    <div class="modelTool">
        <div style="color: #fff;">
            显示批注
            <el-switch v-model="isShowComments" @change="showCommentsChange" size="mini"></el-switch>
        </div>
        <div class="modelTool_item" @click="newannotation">
            <div class="modelTool_item_top">
                <img class="modelTool_item_img" src="../../../../assets/img/appUseCenter/icon_mxgj_9.png" alt=""
                v-if="!marginalia" />
                <img class="modelTool_item_img" src="../../../../assets/img/appUseCenter/icon_mxgj_10.png" alt=""
                v-if="marginalia" />
            </div>
            <div class="modelTool_item_foot">
                <span>新建批注</span>
            </div>
        </div>
        <div class="modelTool_item" @click="removeAll">
            <div class="modelTool_item_top">
                <img class="modelTool_item_img" src="../../../../assets/img/appUseCenter/icon_mxgj_11.png" alt="" v-if="true" />
            </div>
            <div class="modelTool_item_foot">
                <span>清除所有</span>
            </div>
        </div>
    </div>
</template>
 
<script>
import {saveOrUpdateLable,getLabelList,deleteLable } from "@/api/datacenter"
import * as SmartEarth from "../../../../../public/CimSDK/index.js"
export default {
    data() {
      return {
        marginalia: false, //批注
        numberValidateForm: {
          name: "",
        },
        longitude: "",
        latitude: "",
        height: null,
        isShowComments: false,
        commentList: [],
        textPiZhu:"",
        piZhu:false,
        open:false,
        lablePiZhu:true
      };
    },
    methods: {
        showCommentsChange() {
            this.loadLable()
            if (this.isShowComments) {
              this.commentList.forEach((element) => {
                element.show = true;
              });
            } else {
                this.marginalia = false;
                if (this.marginalia === false) {
                //再次点击取消标注功能,释放handler
                if (window.PiZhuHandler) {
                    window.PiZhuHandler.destroy();
                    window.PiZhuHandler = undefined;
                  }
                }
                this.commentList.forEach((element) => {
                  element.show = false;
                });
            }
        },
        //添加批注
        piZhuAdd(lon,lat,height,pickedObject){
          this.longitude = lon;
          this.latitude = lat;
          this.height = height;
          if(this.piZhu){
            if(this.open) window.closePopouPiZhu();
            //查看批注样式
            let htmldiv = 
              `<div style="z-index: 9999;width: auto;height: auto;background-color: white;border-radius: 15px;">
                  <header style="height: 40px;line-height: 40px;"><div style="padding-left: 20px;padding-top: 5px;">查看批注</div>
                    <a style="padding:2px;text-align:center;font:16px/14px Tahoma,Verdana,sans-serif;color:#c3c3c3;
                      text-decoration:none;cursor:pointer;font-weight: bold;background:transparent;position:absolute;right:18px;top:16px;"
                      class="closebtn" href="#" onclick=" window.closePopouPiZhu()">×
                    </a>
                  </header>
                  <main style="padding: 0px;">
                      <textarea disabled="disabled" id="piZhuText" style="width:370px;padding: 0px;margin: 10px 20px;height:140px;border: 1px solid #C0C4CC;"></textarea>
                  </main>
                  <footer style="height: 35px;line-height: 35px;">
                      <button style="display: flex;margin: 0px auto;width: auto;border: 1px solid rgb(192, 196, 204);padding: 2px 15px;background: #F2F6FC;" onclick="window.closePopouPiZhu()">   取 消   </button>
                  </footer>
                </div>`;
            htmldiv += 
              `<div style="background:white; position:absolute; top:-3px; left:0px; width:2px; height:10px; 
                padding:1px; margin:0px auto 0; -webkit-transform: rotate(-45deg); -moz-transform: rotate(-45deg);
                -ms-transform: rotate(-45deg); -o-transform: rotate(-45deg); transform: rotate(-45deg);"></div>`;
            //根据事件位置新增查看批注窗体
            window.divPointPiZhu = window.sgworld.view.createScreenDialog({
                html: htmldiv,
                lon: lon,
                lat: lat,
                height: height,
            });
            document.getElementById("piZhuText").value = pickedObject.id._name
            this.piZhu = false
            this.open = true
          }else{
            if(this.open) window.closePopouPiZhu();
            //新增批注样式
            let htmldiv = 
            ` <div style="z-index: 9999;width: auto;height: auto;background-color: white;border-radius: 15px;">
                  <header style="height: 40px;line-height: 40px;"><div style="padding-left: 20px;padding-top: 5px;">新建批注</div>
                    <a style="padding:2px;text-align:center;font:16px/14px Tahoma,Verdana,sans-serif;color:#c3c3c3;
                    text-decoration:none;cursor:pointer;font-weight: bold;background:transparent;position:absolute;right:18px;top:16px;"
                    class="closebtn" href="#" onclick=" window.closePopouPiZhu()">×</a>
                  </header>
                  <main style="padding: 0px;">
                    <textarea id="piZhuText" style="width:370px;padding: 0px;margin: 10px 20px;height:140px;border: 1px solid #C0C4CC;"></textarea>
                  </main>
                  <footer style="height: 35px;line-height: 35px;">
                    <button style="display: flex;margin: 0px auto;width: auto;border: 1px solid rgb(192, 196, 204);padding: 2px 15px;background: #F2F6FC;" onclick="window.textPiZhu()">   确 定   </button>
                  </footer>
                </div>`;
              htmldiv += 
              `<div style="background:white; position:absolute; top:-3px; left:0px; width:2px; height:10px; 
                padding:1px; margin:0px auto 0; -webkit-transform: rotate(-45deg); -moz-transform: rotate(-45deg);
                -ms-transform: rotate(-45deg); -o-transform: rotate(-45deg); transform: rotate(-45deg);"></div>`;
            //根据事件位置新增批注窗体
            window.divPointPiZhu = window.sgworld.view.createScreenDialog({
                  html: htmldiv,
                  lon: lon,
                  lat: lat,
                  height: height,
              });
            this.open= true
          }
        },
        //开启批注
        newannotation() {
          let that = this
          if(this.marginalia)
          {
            this.marginalia = false;
            window.PiZhuHandler.destroy();
            window.PiZhuHandler = undefined;
            return
          }
          this.marginalia = true;
          //注册弹窗事件
          window.textPiZhu = () => {
              this.numberValidateForm.name = document.getElementById("piZhuText").value
              this.submitpizhu()
              window.closePopouPiZhu()
          }
          //注册一个关闭弹框的事件
          if (!window.closePopouPiZhu) {
            window.closePopouPiZhu = () => {
              if (window.divPointPiZhu) {
                window.divPointPiZhu.removeFromMap();
                window.divPointPiZhu = undefined;
                this.open = false
              }
            };
          }
          //注册左键点击事件,获取点击位置
          window.PiZhuHandler = new Cesium.ScreenSpaceEventHandler(sgworld._Viewer.canvas);
          window.PiZhuHandler.setInputAction(function onLeftClick(movement) {
            let lon,lat,height
            let pickBim = true
            window.BIM.PickId(movement.position.x, movement.position.y).then(idArr => {
              let pickEntity  = null
              let pickedObject = sgworld._Viewer.scene.pick(movement.position)
              if(pickedObject != undefined) that.piZhu = true
              if(idArr[0] == 0 && idArr[1] == 0 && idArr[2] == 0 && idArr[3] == 0 ){
                pickBim = false
              }
              if(pickBim){
                sgworld._Viewer.scene.bim.PickWorldPosition(movement.position.x,movement.position.y).then((position)=>{
                  let cartographic = Cesium.Cartographic.fromCartesian({"x":position[0],"y":position[1],"z":position[2]});
                  lon = Cesium.Math.toDegrees(cartographic.longitude);
                  lat = Cesium.Math.toDegrees(cartographic.latitude);
                  height = cartographic.height;// 高度
                  that.piZhuAdd(lon,lat,height,pickedObject)
                })
              }else{
                var wp = movement.position;
                var ray = sgworld._Viewer.scene.camera.getPickRay(wp);
                var cartesian = sgworld._Viewer.scene.globe.pick(ray, sgworld._Viewer.scene);
                var cartographic = Cesium.Cartographic.fromCartesian(cartesian);
                  lat = Cesium.Math.toDegrees(cartographic.latitude);
                  lon = Cesium.Math.toDegrees(cartographic.longitude);
                  height = sgworld._Viewer.scene.globe.getHeight(cartographic);;
                that.piZhuAdd(lon,lat,height,pickedObject)
              }
            })
          }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
        },
        //移除所有批注
        removeAll() {
            deleteLable("1").then((res)=>{
                if(res.code == 200 ){
                this.marginalia = false;
                  window.sgworld.entities.removeAll();
                if (window.PiZhuHandler) {
                    window.PiZhuHandler.destroy();
                }
                  window.PiZhuHandler = undefined;
                }
            })
        },
        //加载批注
        loadLable(){
          let earthCtrl = window.sgworld
          if(this.lablePiZhu){
            getLabelList("1").then((res)=>{
              if(res.code == 200){
                res.data.forEach((item)=>{
                  item.image = require('../../../../assets/img/img.png');
                  var billboardEntity = earthCtrl.factory.createBillboard(item);
                  if (this.isShowComments) {
                    billboardEntity.show = true;
                  } else {
                    billboardEntity.show = false;
                  }
                  this.commentList.push(billboardEntity);
                })
              this.lablePiZhu = false
              }
            })
          }
        },
        //上传批注
        submitpizhu() {
            if (!this.numberValidateForm.name || this.numberValidateForm.name === "" || this.numberValidateForm.name.trim() === "") {
              this.$message.error("批注内容不能为空或空格!");
              return;
            }
            
            let longitude = this.longitude;
            let latitude = this.latitude;
            let height = this.height;
            var param = {
              name: this.numberValidateForm.name,
              image: '../../../../assets/img/img.png',
              lon: longitude,
              lat: latitude,
              alt: height,
              scale: 2,
              content: this.numberValidateForm.name,
              userId: '1'
            };
            saveOrUpdateLable(param).then((res)=>{
              if(res.code == 200){
                param.id = sgworld.factory.createUUID()
                param.image = require('../../../../assets/img/img.png');
                var billboardEntity = sgworld.factory.createBillboard(param);
                if (this.isShowComments) {
                  billboardEntity.show = true;
                } else {
                  billboardEntity.show = false;
                }
                this.commentList.push(billboardEntity);
              }
            })
        }
    }
}
</script>
 
<style scoped lang="less">
  .modelTool {
      padding:10px;
      .modelTool_item {
        width: 56px;
        margin: 0 10px;
        color:aliceblue;
        margin-bottom: 10px;
        cursor: pointer;
        display: inline-block;
        .modelTool_item_top {
          width: 56px;
          height: 44px;
          text-align: center;
          line-height: 65px;
          margin-top:10px;
          .modelTool_item_img {
            width: 32px;
            height: 32px;
          }
        }
        .modelTool_item_foot {
          text-align: center;
          font-size: 14px;
          color: #ffffff;
          margin-top:5px;
        }
      }
      /deep/.el-switch.is-checked .el-switch__core {
        border-color: #3384c6;
        background-color: #3384c6;
      }
  }
</style>