354 lines
14 KiB
JavaScript
354 lines
14 KiB
JavaScript
import Demo from "../demo";
|
||
import EquipmentTag from "../utils/EquipmentTag";
|
||
/**
|
||
*
|
||
* @param {Mesh} targetPoint
|
||
* @param {String} equType "fan" "sensors"
|
||
*/
|
||
//formInfo需要的信息这里包括了
|
||
//equipmentType
|
||
|
||
function addEquipment(targetPoint, formInfo, fanData) {
|
||
if (targetPoint.hasDevice) {
|
||
return;
|
||
}
|
||
|
||
switch (formInfo.equipmentType) {
|
||
case "frequency":
|
||
handleFanEqu.call(this, targetPoint, formInfo, fanData);
|
||
break;
|
||
// 还没有风压阀的模型,所以先用其他设备传感器实现
|
||
// case "valve":
|
||
// handleValveEqu.call(this, targetPoint, formInfo.chooseEquipment);
|
||
// break;
|
||
case "windPressure":
|
||
case "sensor":
|
||
handleOtherEqu.call(this, targetPoint, formInfo);
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
targetPoint.hasDevice = true;
|
||
// 标识设备信息
|
||
this.clearTagsObj();
|
||
}
|
||
|
||
//判断墙是否已经存在,如果存在的话,就不管,添加则无效嘛(就是说只能添加一面墙,如果存在一面墙,则不管了)
|
||
let hasWall = false;
|
||
let wallHang = 0;
|
||
|
||
// function handleValveEqu(targetPoint, equipmentInfo) {
|
||
|
||
// }
|
||
let allline = new Array();
|
||
let Allhang = new Array();
|
||
// 其他传感器
|
||
function handleOtherEqu(targetPoint, equipmentInfo) {
|
||
const equMesh = this.equMap.get("equ_sensors").clone();
|
||
const worldP = targetPoint.getWorldPosition(new this.THREE.Vector3());
|
||
equMesh.position.copy(worldP);
|
||
|
||
//设备添加标签
|
||
const param = `${equipmentInfo.data || "--"} ${equipmentInfo.unit || "-"}`; //渲染设备参数(data)和单位(unit)
|
||
const tag = EquipmentTag(equipmentInfo.equipmentName,param);
|
||
equMesh.getObjectByName("tag").material = tag;
|
||
if (/tr$/.test(targetPoint.name)) {
|
||
equMesh.rotation.z = -Math.PI / 2;
|
||
equMesh.rotation.y -= Math.PI / 4;
|
||
equMesh.getObjectByName("tag").rotation.z += Math.PI; // 注意在原来的基础上加
|
||
} else if (/tl$/.test(targetPoint.name)) {
|
||
equMesh.rotation.z = Math.PI / 2;
|
||
equMesh.rotation.y += Math.PI / 4;
|
||
equMesh.getObjectByName("tag").rotation.z += Math.PI;
|
||
} else if (/tc$/.test(targetPoint.name)) {
|
||
equMesh.rotation.z = Math.PI;
|
||
equMesh.getObjectByName("tag").rotation.z += Math.PI;
|
||
} else if (/br$/.test(targetPoint.name)) {
|
||
equMesh.rotation.z = -Math.PI / 2;
|
||
equMesh.getObjectByName("tag").rotation.z += Math.PI;
|
||
} else if (/bl$/.test(targetPoint.name)) {
|
||
equMesh.rotation.z = Math.PI / 2;
|
||
equMesh.getObjectByName("tag").rotation.z += Math.PI;
|
||
}
|
||
targetPoint.scale.set(0.03, 0.06, 0.05);
|
||
this.scene.add(equMesh);
|
||
targetPoint.visible = false;
|
||
// targetPoint.hasWall = true;
|
||
// allline.push(targetPoint.name)
|
||
// if (hasWall == false) {
|
||
// let findWallHang = new Array;
|
||
// // console.log(targetPoint.name);
|
||
// console.log(allline);
|
||
// Allhang = allline.map(function (str) {
|
||
// return parseInt(str.match(/\d+/)[0], 10);
|
||
// })
|
||
// console.log(Allhang);
|
||
// //将取出来的所有行数字符串用一个新数组来保存,之后判断是不是存在同一行的(即相同的行数)
|
||
// for (let i = 0; i < Allhang.length; i++) {
|
||
// //判断是不是存在相同的行数,如果存在,则继续判断是不是存在左右的传感器
|
||
// findWallHang.push(Allhang[i - 1])
|
||
// if (findWallHang.includes(Allhang[i])) {
|
||
// console.log(allline[i - 1].slice(-1));
|
||
// console.log(allline[i].slice(-1));
|
||
// wallHang = Allhang[i];
|
||
// let sameHang = findWallHang.indexOf(Allhang[i])
|
||
// if ((allline[sameHang - 1].slice(-1) == 'r' && allline[i].slice(-1) == 'l') || allline[sameHang - 1].slice(-1) == 'l' && allline[i].slice(-1) == 'r') {
|
||
// console.log('进行添加操作');
|
||
// // 将这二个符合添加规则的锚点给设一个属性,这样方便之后我们进行删除操作
|
||
// // this.scene.getObjectByName(allline[i - 1]).hasWall = true
|
||
// // this.scene.getObjectByName(allline[i]).hasWall = true
|
||
// console.log(this.scene.getObjectByName(allline[i - 1]));
|
||
// console.log(this.scene.getObjectByName(allline[i]));
|
||
// //出现满足条件的风压阀了,这时候就设定为已经有墙了,不需要再添加了
|
||
// hasWall = true
|
||
// findWallHang = []
|
||
// //进行添加操作,因为数值问题,所以我们分开进行添加操作
|
||
// for (let line = Allhang[i]; line <= 20; line++) {
|
||
// if (line < 10) {
|
||
// let wall = 'wall_' + '0' + line
|
||
// console.log(wall);
|
||
// //把墙设为可见
|
||
// this.scene.getObjectByName(wall).visible = true
|
||
// let point = 'point_' + '00' + line + '_tc'
|
||
// // console.log(this.scene.getObjectByName(point));
|
||
// //把顶部的附着点设为隐藏!!!,然后设为不可选中
|
||
// this.scene.getObjectByName(point).visible = false
|
||
// this.scene.getObjectByName(point).layers.set(-2)
|
||
// //如果最中间一行有设备,我们则需要将设备进行隐藏!!!
|
||
// if (this.scene.getObjectByName(point).hasDevice == true) {
|
||
// let EquID = this.scene.getObjectByName(point).info.id
|
||
// this.scene.getObjectById(EquID).visible = false
|
||
// }
|
||
// } else if (line >= 10) {
|
||
// let wall = 'wall_' + line
|
||
// this.scene.getObjectByName(wall).visible = true
|
||
// let point = 'point_' + '0' + line + '_tc'
|
||
// this.scene.getObjectByName(point).visible = false
|
||
// this.scene.getObjectByName(point).layers.set(-2)
|
||
// if (this.scene.getObjectByName(point).hasDevice == true) {
|
||
// let EquID = this.scene.getObjectByName(point).info.id
|
||
// this.scene.getObjectById(EquID).visible = false
|
||
// }
|
||
// }
|
||
// //处理21行的设备和附着点
|
||
// let point = 'point_' + '0' + 21 + '_tc'
|
||
// this.scene.getObjectByName(point).visible = false
|
||
// this.scene.getObjectByName(point).layers.set(-2)
|
||
// if (this.scene.getObjectByName(point).hasDevice == true) {
|
||
// let EquID = this.scene.getObjectByName(point).info.id
|
||
// this.scene.getObjectById(EquID).visible = false
|
||
// }
|
||
// }
|
||
// }
|
||
// }
|
||
// }
|
||
// }
|
||
//处理风压阀的代码片段
|
||
// let lineIndex = targetPoint.name.slice(7, 9);
|
||
// if (allline.includes(Number(lineIndex))) {
|
||
// allline.slice(array.indexOf(lineIndex), 1);
|
||
// console.log("dadad");
|
||
// } else {
|
||
// allline.push(Number(lineIndex));
|
||
// }
|
||
// console.log(allline);
|
||
// console.log(allline);
|
||
// allline.forEach((item, index, array) => {
|
||
// if (array.includes(item)) {
|
||
// array.slice(array.indexOf(item), 1)
|
||
// array.slice(array.indexOf(item), 1)
|
||
// console.log(array);
|
||
// }
|
||
// })
|
||
// console.log(this.scene);
|
||
// 保存该设备模型id,后期直接从附附着点进行删除
|
||
targetPoint.info = {
|
||
id: equMesh.id,
|
||
...equipmentInfo,
|
||
};
|
||
//下面进行模拟分压阀的添加效果
|
||
// 1. 不能放在初始化的地方去写,我们最好放在同一行的地方去写,当用户每添加一个模型,就判断是不是该行已经添加过了多个模型了
|
||
// 2.如果是的话,我们必须判断是不是左右同时添加的情况,如果是的话则则可以加一个墙
|
||
|
||
// 0. 首先需要定义一个变量,看墙是否已经添加了,如果已经添加的话,则直接跳出不管(因为只会有二个风压阀生成的墙)
|
||
// 1. 每添加一个则将附着点保存到数组里面,然后对数组里面进行第一遍过滤,将相同行数的提取出来,如果存在左右的话,则进行墙的展示(里面套for循环将后面的墙全部打开,并且将该行上面的附着点全部隐藏,包括设备和层级!!!)
|
||
}
|
||
|
||
// 风机
|
||
function handleFanEqu(
|
||
targetPoint,
|
||
equipmentInfo,
|
||
fanData,
|
||
speed = Math.random().toFixed(1) * 1000
|
||
) {
|
||
// 由于风机比较多,每个风机转速不一直,保存在一个数中遍历
|
||
if (!this.fanSpinArray) {
|
||
this.fanSpinArray = [];
|
||
}
|
||
const equMesh = this.equMap.get("equ_fan").clone();
|
||
// console.log(this.equMap.get("equ_fan"));
|
||
// console.log(this.equMap.get("equ_fan").material.clone());
|
||
// equMesh.material = this.equMap.get("equ_fan").material.clone();
|
||
// console.log(equMesh);
|
||
//深层次克隆
|
||
equMesh.children.map(function (item) {
|
||
// console.log(item);
|
||
if (item.isMesh) {
|
||
item.material = item.material.clone();
|
||
}
|
||
});
|
||
// equMesh.geometry = this.equMap.get("equ_fan").geometry.clone();
|
||
// console.log(equMesh);
|
||
|
||
const worldP = targetPoint.getWorldPosition(new this.THREE.Vector3());
|
||
equMesh.position.copy(worldP);
|
||
equMesh.translateY(-0.6);
|
||
this.scene.add(equMesh);
|
||
targetPoint.visible = false;
|
||
// 放大附着点方便选中风机的范围
|
||
targetPoint.scale.set(0.06, 0.06, 0.06);
|
||
// 保存该设备id,后期直接从附附着点进行删除
|
||
targetPoint.info = {
|
||
id: equMesh.id, //模型在场景id
|
||
...equipmentInfo,
|
||
};
|
||
//我们把风机的附着点挂在原型上,这样应该可以进行处理
|
||
if (targetPoint.info.equipmentName.slice(0, 1) == "1") {
|
||
this.scene.Fan1targetPoint = targetPoint;
|
||
} else {
|
||
this.scene.Fan2targetPoint = targetPoint;
|
||
}
|
||
//这里感觉情况改变风机的颜色
|
||
changeFanColor(fanData, targetPoint, this.scene);
|
||
|
||
// 定义风机旋转
|
||
const fanLeaf = equMesh.getObjectByName("fan_leafs");
|
||
const fanSpin = new this.TWEEN.Tween({ rotation: 0 });
|
||
fanSpin.to({ rotation: 2 * Math.PI }, speed);
|
||
fanSpin.repeat(Infinity);
|
||
fanSpin.start();
|
||
fanSpin.onUpdate((obj) => {
|
||
fanLeaf.rotation.z = obj.rotation;
|
||
});
|
||
this.fanSpinArray.push(fanSpin);
|
||
}
|
||
|
||
// 删除传感器
|
||
function removeEquipment(targetPoint) {
|
||
//删除设备这里需要再进行处理,就是根据附着点(附着点一定保存了当前添加设备的信息了)判断是不是分压阀,然后继续一样的隐藏
|
||
if (!targetPoint.hasDevice) return;
|
||
|
||
//删除我们这里使用行数来判断,即是不是删除到了我们的那一行呢?
|
||
//如果删除到了我们那行的话,我们必须将行的数组从中去掉已存在的数据,然后将附着点列表中也去除这个点,然后将墙设置为false来让下次使用
|
||
|
||
// let lineIndex = targetPoint.name.slice(7, 9)
|
||
// deleteItem(allline, targetPoint.name)
|
||
// if (Number(lineIndex) == wallHang) {
|
||
// wallHang = 0;
|
||
// hasWall = false
|
||
// for (let line = Number(lineIndex); line <= 20; line++) {
|
||
// if (line < 10) {
|
||
// let wall = 'wall_' + '0' + line
|
||
// this.scene.getObjectByName(wall).visible = false
|
||
// let point = 'point_' + '00' + line + '_tc'
|
||
// console.log(this.scene.getObjectByName(point));
|
||
// this.scene.getObjectByName(point).visible = true
|
||
// this.scene.getObjectByName(point).layers.set(0)
|
||
// if (this.scene.getObjectByName(point).hasDevice == true) {
|
||
// this.scene.getObjectByName(point).visible = false
|
||
// let EquID = this.scene.getObjectByName(point).info.id
|
||
// this.scene.getObjectById(EquID).visible = true
|
||
// }
|
||
// } else if (line >= 10) {
|
||
// let wall = 'wall_' + line
|
||
// this.scene.getObjectByName(wall).visible = false
|
||
// let point = 'point_' + '0' + line + '_tc'
|
||
// this.scene.getObjectByName(point).visible = true
|
||
// this.scene.getObjectByName(point).layers.set(0)
|
||
// if (this.scene.getObjectByName(point).hasDevice == true) {
|
||
// this.scene.getObjectByName(point).visible = false
|
||
// let EquID = this.scene.getObjectByName(point).info.id
|
||
// this.scene.getObjectById(EquID).visible = true
|
||
// }
|
||
// }
|
||
// //处理21行的设备和附着点
|
||
// let point = 'point_' + '0' + 21 + '_tc'
|
||
// this.scene.getObjectByName(point).visible = true
|
||
// this.scene.getObjectByName(point).layers.set(0)
|
||
// if (this.scene.getObjectByName(point).hasDevice == true) {
|
||
// let EquID = this.scene.getObjectByName(point).info.id
|
||
// this.scene.getObjectById(EquID).visible = true
|
||
// }
|
||
// }
|
||
// }
|
||
// deleteItem(Allhang, Number(lineIndex))
|
||
|
||
console.log("测试是不是删除了");
|
||
const mesh = this.scene.getObjectById(targetPoint.info.id);
|
||
this.scene.remove(mesh);
|
||
targetPoint.visible = true;
|
||
targetPoint.hasDevice = false;
|
||
targetPoint.scale.set(0.01, 0.01, 0.01);
|
||
delete targetPoint.info; // 清空设备信息
|
||
}
|
||
export { addEquipment, removeEquipment };
|
||
|
||
function deleteItem(array, item) {
|
||
let index = array.indexOf(item);
|
||
if (index !== -1) {
|
||
array.splice(index, 1);
|
||
}
|
||
return array;
|
||
}
|
||
|
||
// 这里是改变风机颜色的代码
|
||
function changeFanColor(fanData, targetPoint, scene) {
|
||
if (fanData !== undefined) {
|
||
//保证是预览模式才进行这里的操作(!=1)
|
||
if (fanData.length > 0) {
|
||
//if保证存在风机(即有长度再进行遍历)
|
||
for (let i = 0; i < fanData.length; i++) {
|
||
//先做匹配操作
|
||
let fanType = fanData[i].equipmentType;
|
||
//有符合条件的风机进来了
|
||
if (targetPoint.info.typeKey == fanType) {
|
||
//下面进行变色需要的逻辑判断了
|
||
if (fanData[i].breakdown == true) {
|
||
//故障状态,变红色
|
||
scene.traverse(function (child) {
|
||
if (child.id === targetPoint.info.id) {
|
||
child.traverse(function (obj) {
|
||
// console.log(obj);
|
||
// 判断子对象是否是物体,如果是,更改其颜色
|
||
if (obj.isMesh) {
|
||
obj.material.color.set(0xff0000);
|
||
}
|
||
});
|
||
}
|
||
});
|
||
} else {
|
||
// 这里再判断是否运行
|
||
if (fanData[i].running == true) {
|
||
//运行状态,绿色 0x008000
|
||
scene.traverse(function (child) {
|
||
if (child.id === targetPoint.info.id) {
|
||
child.traverse(function (obj) {
|
||
// console.log(obj);
|
||
// 判断子对象是否是物体,如果是,更改其颜色
|
||
if (obj.isMesh) {
|
||
obj.material.color.set(0x008000);
|
||
}
|
||
});
|
||
}
|
||
});
|
||
} else {
|
||
//停止状态,不变色
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|