#
luxiaotao1123
2024-10-17 4f32ade76be015062d305c6b0eecf1b7fbedd331
zy-acs-flow/src/map/insight/agv/AgvMain.jsx
@@ -1,81 +1,70 @@
import React, { useRef, useEffect, useState } from 'react';
import { useTranslate } from 'react-admin';
import {
    Box,
    CircularProgress,
    Grid,
    Typography,
    Paper,
    Button,
    Divider,
    Stack,
} from '@mui/material';
import ShelfThree from './AgvThree';
import * as THREE from 'three';
import { Box, CircularProgress } from '@mui/material';
import AgvThree from './AgvThree';
import { getAgvInfo } from '../../http';
import { grey } from '@mui/material/colors';
let shelfThree;
let three;
const startThree = (dom) => {
    shelfThree = new ShelfThree(dom);
    shelfThree.startup();
    three = new AgvThree(dom);
    three.startup();
}
const endThree = () => {
    if (shelfThree) {
        shelfThree.destroy();
        shelfThree = null;
    if (three) {
        three.destroy();
        three = null;
    }
}
const renderThree = (info, curAgvNo) => {
    if (info && shelfThree) {
        shelfThree.generateMesh((loader, addObject) => {
            const promises = [];
            promises.push(new Promise((resolve) => {
                loader.load('model/agv.fbx', (mesh) => {
                    mesh.position.set(0, singleHeight * (lev - 1), 0);
                    mesh.scale.set(5, 5, 5);
                    mesh.name = item.locNo;
                    mesh.traverse((child) => {
                        if (child.isMesh) {
                            switch (child.name) {
                                case '货架':
                                    child.material.color.set(0x4680BF);
                                    break;
                                case '托盘':
                                    child.visible = item.locStsEl === 'STOCK' || item.locStsEl === 'PAKOUT';
                                    child.material.color.set(0xBEBEBE);
                                    break;
                                case '不规则':
                                    child.visible = item.locStsEl === 'STOCK' || item.locStsEl === 'PAKOUT';
                                    child.material.color.set(0xE8B67E);
                                    break;
                                default:
                                    break;
                            }
                            child.castShadow = true;
                            child.receiveShadow = true;
                        }
    if (info && three) {
        three.generateMesh((loader, addObject) => {
            const loadModel = (path) => {
                return new Promise((resolve, reject) => {
                    loader.load(path, (mesh) => {
                        resolve(mesh);
                    }, undefined, (error) => {
                        reject(error);
                    });
                    addObject(mesh);
                    resolve();
                })
            }));
                });
            };
            Promise.all(promises).then(() => {
                shelfThree.setNewSelectedMesh(curLocNo);
                shelfThree.rePerspective(singleHeight * info.length, 500);
            }).catch(error => {
            Promise.all([
                loadModel('model/agv/body.fbx'),
                loadModel('model/agv/loader.fbx'),
                loadModel('model/agv/fork.fbx'),
            ]).then(([bodyMesh, loaderMesh, forkMesh]) => {
                const scaleVal = 1;
                bodyMesh.scale.set(scaleVal, scaleVal, scaleVal);
                loaderMesh.scale.set(scaleVal, scaleVal, scaleVal);
                forkMesh.scale.set(scaleVal, scaleVal, scaleVal);
                loaderMesh.position.set(0, 150, 0);;
                forkMesh.position.set(0, 165, 0);;
                const agvGroup = new THREE.Group();
                agvGroup.add(bodyMesh);
                agvGroup.add(loaderMesh);
                agvGroup.add(forkMesh);
                agvGroup.name = curAgvNo;
                addObject(agvGroup);
                three.setNewSelectedMesh(curAgvNo);
                three.rePerspective(350, 550);
            }).catch((error) => {
                console.error(error);
            });
        })
        });
    }
}
const AgvMain = (props) => {
    const { data, curAgvNo, setCurAgvNo } = props;
    const translate = useTranslate();
    const containerRef = useRef();
    const [loading, setLoading] = useState(true);
    const [info, setInfo] = useState(null);
@@ -95,12 +84,12 @@
            setLoading(true);
            setTimeout(() => {
                startThree(containerRef.current);
                shelfThree.handleClick = (objName) => {
                three.handleClick = (objName) => {
                    setCurAgvNo(objName);
                };
                renderThree(info, curAgvNo);
                setLoading(false);
            }, 300)
            }, 300);
        }
        return endThree;
    }, [info]);
@@ -121,7 +110,7 @@
                        left="50%"
                        style={{ transform: 'translate(-50%, -50%)' }}
                    >
                        <CircularProgress sx={{ color: grey[50] }} />
                        <CircularProgress />
                    </Box>
                )}
            </Box>
@@ -131,4 +120,4 @@
    );
}
export default AgvMain;
export default AgvMain;