wujingjing
2024-07-24 4eb6092c32df67d752101ab8ae23c9b2236db0dc
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
import { EdgeConfig, NodeConfig } from '@antv/g6';
import { FaultTreeShape } from '/@/projectCom/fault/tree/version/graph/types';
const PORT2ANCHOR = {
    top: [0.5, 0],
    left: [0, 0.5],
    bottom: [0.5, 1],
    right: [1, 0.5],
} as const;
 
export const SHAPE2TYPE = {
    // edge: 'polyline-edge',
    edge: 'polyline-edge',
 
    // [FaultTreeShape.FaultEvent]: 'rect-node',
    [FaultTreeShape.Gate]: 'or-node',
    // [FaultTreeShape.Or]: 'or-node',
    // [FaultTreeShape.And]: 'and-node',
    [FaultTreeShape.MiddleEvent]: 'rect-node',
    [FaultTreeShape.BottomEvent]: 'circle-node',
    [FaultTreeShape.TopEvent]: 'rect-node',
} as const;
 
export const shapeWithInfo = ['rect-node', 'circle-node'];
/**
 * x6 数据转换为 g6 数据
 * @param x6Json
 * @returns
 */
export const x6ToG6 = (x6Json) => {
    const nodes = [] as NodeConfig[];
    const edges = [] as EdgeConfig[];
    const x6DataMap = new Map(
        x6Json.map((item) => {
            return [item.id, item];
        })
    );
    for (let cell of x6Json) {
        if (cell.shape === 'edge') {
            let sourcePort = null;
            let targetPort = null;
            if (cell?.source?.cell) {
                const sourceCell = x6DataMap.get(cell.source?.cell) as any;
                const foundIndex = (sourceCell.ports.items as any[]).findIndex((item) => item.id === cell.source.port);
                if (foundIndex !== -1) {
                    sourcePort = foundIndex;
                }
            }
 
            if (cell?.target?.cell) {
                const targetCell = x6DataMap.get(cell.target?.cell) as any;
                const foundIndex = (targetCell.ports.items as any[]).findIndex((item) => item.id === cell.target.port);
                if (foundIndex !== -1) {
                    targetPort = foundIndex;
                }
            }
            edges.push({
                id: cell.id,
                source: cell.source.cell,
                target: cell.target.cell,
                type: SHAPE2TYPE[cell.shape],
                sourceAnchor: sourcePort,
                targetAnchor: targetPort,
            });
        } else {
            let dx = 0;
            let dy = 0;
            if (cell.shape === FaultTreeShape.Gate) {
                dy = 0;
                dx = -5;
            }
            nodes.push({
                id: cell.id,
                x: cell.position.x + dx,
                y: cell.position.y + dy,
                type: SHAPE2TYPE[cell.shape],
                width: cell.size.width,
                height: cell.size.height,
                label: cell?.attrs?.text?.text,
                anchorPoints: cell.ports?.items?.map((port) => {
                    const group = port.group;
 
                    const portPosition = cell.ports?.groups[group]?.position;
                    return PORT2ANCHOR[portPosition];
                }),
                data: cell.data,
            });
        }
    }
    return { edges, nodes };
};