/*
|
* @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 } })
|
}
|
}
|