一些技术路线测试,增加git,方便代码还原
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
/*
 * @Author       : yuan
 * @Date         : 2025-11-20 09:42:29
 * @LastEditors  : yuan
 * @LastEditTime : 2025-11-21 10:08:32
 * @FilePath     : \js\services\entity-factory.js
 * @Description  : 
 * Copyright 2025 OBKoro1, All Rights Reserved. 
 * 2025-11-20 09:42:29
 */
/**
 * 实体工厂
 * 职责:统一封装 Cesium 实体的创建、查询与删除工具方法
 */
export class EntityFactory {
  /**
   * 构造函数
   * @param {Cesium.Viewer} viewer Cesium Viewer 实例
   */
  constructor(viewer) { this.viewer = viewer }
 
  /**
   * 根据实体名称的包含关系进行模糊查询
   * @param {string} name 关键字(若为空返回空数组)
   * @returns {Cesium.Entity[]} 名称包含关键字的实体列表
   */
  getEntityLikeName (name) {
    if (!name) return []
    const { entities } = this.viewer
    const result = []
    for (let i = 0; i < entities.values.length; i++) {
      const entity = entities.values[i]
      if (entity.name && entity.name.indexOf(name) !== -1) { result.push(entity) }
    }
    return result
  }
 
  /**
   * 根据名称包含关系批量移除实体(安全删除)
   * @param {string} name 关键字(为空不执行)
   */
  removeEntityLikeName (name) {
    if (!name) return
    const list = this.getEntityLikeName(name)
    list.forEach(e => this.viewer.entities.removeById(e.id))
  }
 
  /**
   * 根据前缀移除实体(匹配字符串型 ID 的前缀)
   * @param {string} prefix 字符串前缀
   */
  removeEntityByPrefix (prefix) {
    const list = this.viewer.entities.values
    for (let i = list.length - 1; i >= 0; i--) {
      const id = list[i].id
      if (typeof id === 'string' && id.slice(0, prefix.length) === prefix) {
        this.viewer.entities.remove(list[i])
      }
    }
  }
 
  /**
   * 创建点实体(默认青色且像素大小 10)
   * @param {Cesium.Cartesian3} position 点位置
   * @returns {Cesium.Entity} 新建实体
   */
  createPoint (position) {
    return this.viewer.entities.add({ position, point: { color: new Cesium.Color(0, 1, 1, 0.9), pixelSize: 10 } })
  }
 
  /**
   * 绘制折线(默认宽度 2、材质蓝色、航线类型 RHUMB)
   * @param {Cesium.Cartesian3[]|Cesium.CallbackProperty} positions 位置数组或回调属性
   * @returns {Cesium.Entity} 新建实体
   */
  drawLine (positions) {
    return this.viewer.entities.add({ polyline: { positions, width: 2, material: Cesium.Color.fromCssColorString('#2987f0'), arcType: Cesium.ArcType.RHUMB, clampToGround: false } })
  }
 
  /**
   * 绘制虚线(默认宽度 1、白色虚线材质),初始不显示
   * @param {Cesium.Cartesian3[]|Cesium.CallbackProperty} positions 位置数组或回调属性
   * @param {string} id 实体 ID(用于区分 dashX/dashY/dashZ)
   * @returns {Cesium.Entity} 新建实体
   */
  drawDashLine (positions, id) {
    const colorMap = { dashX: '#9ed81c', dashY: '#ec4346', dashZ: '#47dce4' }
    const color = Cesium.Color.fromCssColorString(colorMap[id] || '#ffffff')
    return this.viewer.entities.add({ name: 'dashLine', id, show: false, polyline: { positions, width: 1, material: color, arcType: Cesium.ArcType.NONE, clampToGround: false } })
  }
 
  /**
   * 绘制面实体(默认半透明蓝色,带描边,每顶点高度独立)
   * @param {Cesium.Cartesian3[]|Cesium.PolygonHierarchy} positions 面的顶点或层级结构
   * @returns {Cesium.Entity} 新建实体
   */
  drawShape (positions) {
    return this.viewer.entities.add({ id: 'polygon', polygon: { hierarchy: positions, fill: true, material: Cesium.Color.fromCssColorString('#2987f0').withAlpha(0.2), outline: true, outlineColor: Cesium.Color.fromCssColorString('#2987f0'), perPositionHeight: true } })
  }
}