summaryrefslogtreecommitdiff
path: root/blocks/eda-frontend/src/components/SchematicEditor/Helper/SvgParser.js
blob: 8c0e5ef5802c85773cc4237de85d5668b46dd8dd (plain)
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
101
102
103
104
105
/* eslint new-cap: ["error", {"newIsCapExceptionPattern": "^mx"}] */
import 'mxgraph/javascript/src/css/common.css'

import mxGraphFactory from 'mxgraph'

import { updateMxGraphXML } from '../../../utils/GalleryUtils'

import {
  getPins,
  getPointXY,
  getRotationParameters,
  getSuperBlockDiagram
} from './ToolbarTools'

const {
  mxPoint
} = new mxGraphFactory()

// we need to divide the svg width and height by the same number in order to maintain the aspect ratio.
export const defaultScale = parseFloat(process.env.REACT_APP_BLOCK_SCALE)
export const portSize = parseFloat(process.env.REACT_APP_PORT_SIZE)

export function getParameter (i) {
  if (i < 10) { return 'p00' + i.toString() } else if (i < 100) { return 'p0' + i.toString() } else { return 'p' + i.toString() }
}

export function getSvgMetadata (graph, parent, evt, target, x, y, component) {
  // calls extractData and other MXGRAPH functions
  // initialize information from the svg meta
  // plots pinnumbers and component labels.

  const allowedPart = [0, 1]
  const allowedDmg = [0, 1]

  const blockName = component.block_name
  const parameterCount = component.newblockparameter_set.length
  // make the component images smaller by scaling
  const width = component.block_width / defaultScale
  const height = component.block_height / defaultScale

  const v1 = graph.insertVertex(parent, null, null, x, y, width, height, blockName)
  v1.CellType = 'Component'
  v1.blockprefix = component.blockprefix.name
  v1.displayProperties = {
    display_parameter: component.initial_display_parameter
  }
  const parameterValues = {}
  for (let i = 0; i < parameterCount; i++) {
    const p = getParameter(i) + '_value'
    parameterValues[p] = component.newblockparameter_set[i].p_value_initial
  }
  v1.parameter_values = parameterValues
  v1.errorFields = {}

  v1.setConnectable(false)

  const blockports = component.newblockport_set
  const ports = blockports.length
  v1.explicitInputPorts = 0
  v1.implicitInputPorts = 0
  v1.explicitOutputPorts = 0
  v1.implicitOutputPorts = 0
  v1.controlPorts = 0
  v1.commandPorts = 0
  v1.simulationFunction = component.simulation_function
  v1.SuperBlockDiagram = getSuperBlockDiagram(updateMxGraphXML(component.super_block))
  v1.pins = {
    explicitInputPorts: [],
    implicitInputPorts: [],
    controlPorts: [],
    explicitOutputPorts: [],
    implicitOutputPorts: [],
    commandPorts: []
  }
  for (let i = 0; i < ports; i++) {
    const blockport = blockports[i]

    if (!allowedPart.includes(blockport.port_part)) { continue }
    if (!allowedDmg.includes(blockport.port_dmg)) { continue }
    if (blockport.port_name === 'NC') { continue }

    const xPos = 1 / 2 + blockport.port_x / defaultScale / width
    const yPos = 1 / 2 + blockport.port_y / defaultScale / height

    const portOrientation = blockport.port_orientation
    const rotationParameters = getRotationParameters(portOrientation)

    const pins = getPins(portOrientation, v1)

    const pointXY = getPointXY(rotationParameters, blockName)
    const pointX = pointXY.pointX
    const pointY = pointXY.pointY

    const point = new mxPoint(pointX, pointY)

    const vp = graph.insertVertex(v1, null, null, xPos, yPos, portSize, portSize, portOrientation)
    vp.geometry.relative = true
    vp.geometry.offset = point
    vp.CellType = 'Pin'
    vp.ParentComponent = v1.id
    if (pins != null) {
      pins.push(vp)
    }
  }
}