igurielidze eb0b346e1a Fix tag paths to match Ignition tag tree exactly
- BCN → Beacon (not Solenoids)
- SOL → Solenoid (not Solenoids)
- Added: Chute, Tipper, Diverter, Extendo, Chute Enable, Package Release

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-30 23:41:35 +04:00

748 lines
32 KiB
TypeScript

import { layout } from './stores/layout.svelte.js';
import { isEpcType, isInductionType, isSpurType, isCurvedType, isRectConveyanceType, isExtendoType, isPhotoeyeType, getSymbolGroup, EPC_CONFIG, INDUCTION_CONFIG, PHOTOEYE_CONFIG, getCurveGeometry } from './symbols.js';
import { serializeSymbol, deserializeSymbol } from './serialization.js';
import type { PlacedSymbol } from './types.js';
/** Parse conveyance label into display lines — same logic as renderer */
function parseConveyanceLabel(label: string): { lines: string[]; stripped: string[] } {
let core = label.replace(/_?VFD\d*$/i, '');
if (core === label) core = label;
const m = core.match(/^([A-Za-z]+)(.*)$/);
if (m) {
const prefix = m[1];
const nums = m[2].replace(/_/g, '-').replace(/^-/, '');
if (nums) return { lines: [prefix, nums], stripped: [nums] };
return { lines: [prefix], stripped: [prefix] };
}
return { lines: [core], stripped: [core] };
}
/** Emit conveyance label text inside a <g> — inherits outer transform from group */
function emitConveyanceLabelInner(lines: string[], sym: PlacedSymbol) {
if (!sym.label) return;
const { lines: textLines } = parseConveyanceLabel(sym.label);
let labelCx: number, labelCy: number, availH: number;
let textRotDeg = 0; // additional rotation for the text
if (isCurvedType(sym.symbolId)) {
const angle = sym.curveAngle || 90;
const { arcCx, arcCy, outerR, innerR, bandW } = getCurveGeometry(sym.symbolId, sym.x, sym.y, sym.w, sym.h);
const midR = (outerR + innerR) / 2;
const midAngleRad = ((angle / 2) * Math.PI) / 180;
labelCx = arcCx + midR * Math.cos(midAngleRad);
labelCy = arcCy - midR * Math.sin(midAngleRad);
availH = bandW - 4;
// Tangent rotation (same as canvas renderer)
let textRotRad = -midAngleRad + Math.PI / 2;
// Check readability with outer rotation
const symRotRad = ((sym.rotation || 0) * Math.PI) / 180;
let worldAngle = (textRotRad + symRotRad) % (2 * Math.PI);
if (worldAngle < 0) worldAngle += 2 * Math.PI;
if (worldAngle > Math.PI / 2 && worldAngle < Math.PI * 3 / 2) textRotRad += Math.PI;
// Mirror: label follows the mirrored shape naturally
textRotDeg = (textRotRad * 180) / Math.PI;
} else if (isSpurType(sym.symbolId)) {
const w2 = sym.w2 ?? sym.w;
const fontSize = 14;
const th = fontSize * textLines.length;
const optCy = Math.min(sym.h - th / 2 - 1, sym.h * 0.55);
const textTop = optCy - th / 2;
const rightEdge = w2 + (Math.max(0, textTop) / sym.h) * (sym.w - w2);
// Estimate text width (~8px per char at 14px bold)
const estTextW = Math.max(...textLines.map(l => l.length * 8));
labelCx = sym.x + Math.max(estTextW / 2, rightEdge - estTextW / 2);
labelCy = sym.y + optCy;
availH = sym.h - 4;
} else if (isInductionType(sym.symbolId)) {
const stripTopY = sym.y + sym.h * INDUCTION_CONFIG.stripTopFrac;
const stripBottomY = sym.y + sym.h * INDUCTION_CONFIG.stripBottomFrac;
labelCx = sym.x + (INDUCTION_CONFIG.headWidth + sym.w) / 2;
labelCy = (stripTopY + stripBottomY) / 2;
availH = stripBottomY - stripTopY - 4;
} else {
labelCx = sym.x + sym.w / 2;
labelCy = sym.y + sym.h / 2;
availH = sym.h - 4;
}
// For non-curved: check readability and flip if needed
let needsMirrorFix = false;
if (!isCurvedType(sym.symbolId)) {
const rot = ((sym.rotation || 0) % 360 + 360) % 360;
const effectiveAngle = sym.mirrored ? ((360 - rot) % 360) : rot;
if (effectiveAngle > 90 && effectiveAngle < 270) textRotDeg = 180;
needsMirrorFix = !!sym.mirrored;
}
const fontSize = Math.min(14, availH / textLines.length);
if (fontSize < 4) return;
const lineH = fontSize;
// Build transform: counter-mirror then rotate for readability
let transformParts: string[] = [];
if (needsMirrorFix) transformParts.push(`translate(${labelCx},${labelCy}) scale(-1,1) translate(${-labelCx},${-labelCy})`);
if (textRotDeg) transformParts.push(`rotate(${textRotDeg.toFixed(1)},${labelCx},${labelCy})`);
const rotAttr = transformParts.length ? ` transform="${transformParts.join(' ')}"` : '';
for (let i = 0; i < textLines.length; i++) {
const dy = -(textLines.length - 1) * lineH / 2 + i * lineH;
const y = labelCy + dy + fontSize * 0.35;
lines.push(` <text x="${labelCx}" y="${y}" text-anchor="middle" style="font-family:Arial;font-weight:bold;font-size:${fontSize}px;fill:#000000"${rotAttr}>${textLines[i]}</text>`);
}
}
function downloadBlob(blob: Blob, filename: string) {
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
a.click();
URL.revokeObjectURL(url);
}
export function exportJSON() {
const data = {
symbols: layout.symbols.map(s => serializeSymbol(s)),
gridSize: layout.gridSize,
minSpacing: layout.minSpacing,
canvasW: layout.canvasW,
canvasH: layout.canvasH,
};
const mcmName = layout.currentMcm || 'export';
downloadBlob(new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' }), `${mcmName}_layout.json`);
}
/** Build Ignition tag path from device label and MCM name.
* Format: System/{MCM}/{Category}/{SubCategory}/{Label} */
function getIgnitionTagPath(label: string, mcm: string): string | null {
if (!label) return null;
const upper = label.toUpperCase();
// VFD drives (conveyors, spurs, curves, inductions)
if (/_VFD\d*$/i.test(label)) return `System/${mcm}/VFD/APF/${label}`;
// EPC controllers
if (/_EPC\d*$/i.test(label)) return `System/${mcm}/VFD/APF/${label}`;
// Network nodes
if (/_FIOM\d*$/i.test(label) || /_FIO\d*$/i.test(label)) return `System/${mcm}/Network_Node/FIO/${label}`;
if (/_FIOH\d*$/i.test(label)) return `System/${mcm}/Network_Node/HUB/${label}`;
if (/_SIO\d*$/i.test(label)) return `System/${mcm}/Network_Node/SIO/${label}`;
if (/_DPM\d*$/i.test(label)) return `System/${mcm}/Network_Node/DPM/${label}`;
// Sensors
if (/_TPE\d*$/i.test(label)) return `System/${mcm}/Sensor/Tracking/${label}`;
if (/_LPE\d*$/i.test(label)) return `System/${mcm}/Sensor/Long_Range/${label}`;
if (/_FPE\d*$/i.test(label)) return `System/${mcm}/Sensor/Full/${label}`;
if (/_JPE\d*$/i.test(label)) return `System/${mcm}/Sensor/Jam/${label}`;
if (/_PS\d*$/i.test(label)) return `System/${mcm}/Sensor/Pressure/${label}`;
// Controls / Station
if (/_JR\d*_PB$/i.test(label) || /_JR\d*$/i.test(label)) return `System/${mcm}/Station/Jam_Reset/${label}`;
if (/_SS\d*_PB$/i.test(label)) return `System/${mcm}/Station/Start_Stop/${label}`;
if (/_S\d*_PB$/i.test(label)) return `System/${mcm}/Station/Start/${label}`;
// Beacon
if (/_BCN\d*$/i.test(label)) return `System/${mcm}/Beacon/${label}`;
// Solenoid
if (/_SOL\d*$/i.test(label)) return `System/${mcm}/Solenoid/${label}`;
// Chute (by suffix)
if (/_CHT\d*$/i.test(label)) return `System/${mcm}/Chute/Conveyance/${label}`;
// Tipper
if (/_TIP\d*$/i.test(label)) return `System/${mcm}/Tipper/${label}`;
// Diverter
if (/_DIV\d*$/i.test(label) || /_LS\d*$/i.test(label)) return `System/${mcm}/Diverter/${label}`;
// Extendo
if (/_EXT\d*$/i.test(label)) return `System/${mcm}/Extendo/${label}`;
// Chute Enable
if (/_EN\d*_PB$/i.test(label)) return `System/${mcm}/Station/Jam_Reset_Chute_Bank/${label}`;
// Package Release
if (/_PR\d*_PB$/i.test(label)) return `System/${mcm}/Station/Emergency_Pull_Cord/${label}`;
// PDP
if (/^PDP/i.test(label)) return `System/${mcm}/PDP/${label}`;
// MCM
if (/^MCM/i.test(label)) return null;
return null;
}
/** Build Ignition data attributes for a symbol group element */
function getIgnitionAttrs(label: string): string {
const mcm = layout.currentMcm || 'MCM01';
const tagPath = getIgnitionTagPath(label, mcm);
let attrs = ` data-color="#000000" data-state="OFF" data-priority="No Alarms"`;
if (tagPath) attrs += ` data-tagpath="${tagPath}"`;
return attrs;
}
/** Serialize child elements of an SVG, stripping xmlns added by XMLSerializer */
function serializeChildren(parent: Element): string {
return Array.from(parent.children)
.map(el => new XMLSerializer().serializeToString(el)
.replace(/ xmlns="http:\/\/www\.w3\.org\/2000\/svg"/g, ''))
.join('\n ');
}
async function buildSvgString(): Promise<string> {
const lines: string[] = [
'<?xml version="1.0" encoding="UTF-8" standalone="no"?>',
`<svg width="${layout.canvasW}" height="${layout.canvasH}" viewBox="0 0 ${layout.canvasW} ${layout.canvasH}" version="1.1"`,
' xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"',
' xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape">',
` <rect width="${layout.canvasW}" height="${layout.canvasH}" fill="#ffffff" />`,
];
// Overlay types render on top of base conveyance
const OVERLAY_IDS = new Set([
'photoeye', 'photoeye_v',
'fio_sio_fioh', 'fio_sio_fioh_v',
'dpm', 'dpm_v',
'pdp', 'pdp_v',
'mcm', 'mcm_v',
]);
const visible = layout.symbols.filter(s => !s.hidden && !layout.hiddenGroups.has(getSymbolGroup(s.symbolId)));
const baseSymbols = visible.filter(s => !OVERLAY_IDS.has(s.symbolId));
const overlaySymbols = visible.filter(s => OVERLAY_IDS.has(s.symbolId));
for (const sym of [...baseSymbols, ...overlaySymbols]) {
const rot = sym.rotation || 0;
const mirrored = sym.mirrored || false;
const cx = sym.x + sym.w / 2;
const cy = sym.y + sym.h / 2;
const label = sym.label || sym.name;
const igAttrs = getIgnitionAttrs(label);
const idAttr = `id="${label}" inkscape:label="${label}"${igAttrs}`;
// Build outer transform (rotation + mirror)
const outerParts: string[] = [];
if (rot) outerParts.push(`rotate(${rot},${cx},${cy})`);
if (mirrored) outerParts.push(`translate(${cx},0) scale(-1,1) translate(${-cx},0)`);
const outerTransform = outerParts.join(' ');
if (isEpcType(sym.symbolId)) {
await emitEpc(lines, sym as PlacedSymbol, label, outerTransform);
} else if (isInductionType(sym.symbolId)) {
const hw = INDUCTION_CONFIG.headWidth;
const stripTopY = sym.y + sym.h * INDUCTION_CONFIG.stripTopFrac;
const stripBottomY = sym.y + sym.h * INDUCTION_CONFIG.stripBottomFrac;
const pts = INDUCTION_CONFIG.arrowPoints.map(([xf, yf]) => [sym.x + xf * hw, sym.y + yf * sym.h] as const);
const d = `M ${sym.x + sym.w},${stripTopY} L ${pts[0][0]},${stripTopY} ${pts.map(([px, py]) => `L ${px},${py}`).join(' ')} L ${pts[5][0]},${stripBottomY} L ${sym.x + sym.w},${stripBottomY} Z`;
lines.push(` <g ${idAttr}${outerTransform ? ` transform="${outerTransform}"` : ''}>`);
lines.push(` <path d="${d}" fill="#ffffff" stroke="#000000" stroke-width="1" />`);
emitConveyanceLabelInner(lines, sym as PlacedSymbol);
lines.push(` </g>`);
} else if (isCurvedType(sym.symbolId)) {
const angle = sym.curveAngle || 90;
const { arcCx, arcCy, outerR, innerR } = getCurveGeometry(sym.symbolId, sym.x, sym.y, sym.w, sym.h);
const sweepRad = (angle * Math.PI) / 180;
const outerEndX = arcCx + outerR * Math.cos(sweepRad);
const outerEndY = arcCy - outerR * Math.sin(sweepRad);
const innerEndX = arcCx + innerR * Math.cos(sweepRad);
const innerEndY = arcCy - innerR * Math.sin(sweepRad);
const largeArc = angle > 180 ? 1 : 0;
const d = [
`M ${arcCx + outerR},${arcCy}`,
`A ${outerR},${outerR} 0 ${largeArc},0 ${outerEndX},${outerEndY}`,
`L ${innerEndX},${innerEndY}`,
`A ${innerR},${innerR} 0 ${largeArc},1 ${arcCx + innerR},${arcCy}`,
'Z',
].join(' ');
lines.push(` <g ${idAttr}${outerTransform ? ` transform="${outerTransform}"` : ''}>`);
lines.push(` <path d="${d}" fill="#ffffff" stroke="#000000" stroke-width="1" />`);
emitConveyanceLabelInner(lines, sym as PlacedSymbol);
lines.push(` </g>`);
} else if (isSpurType(sym.symbolId)) {
const w2 = sym.w2 ?? sym.w;
const d = `M ${sym.x},${sym.y} L ${sym.x + w2},${sym.y} L ${sym.x + sym.w},${sym.y + sym.h} L ${sym.x},${sym.y + sym.h} Z`;
lines.push(` <g ${idAttr}${outerTransform ? ` transform="${outerTransform}"` : ''}>`);
lines.push(` <path d="${d}" fill="#ffffff" stroke="#000000" stroke-width="1" />`);
emitConveyanceLabelInner(lines, sym as PlacedSymbol);
lines.push(` </g>`);
} else if (isRectConveyanceType(sym.symbolId)) {
lines.push(` <g ${idAttr}${outerTransform ? ` transform="${outerTransform}"` : ''}>`);
lines.push(` <rect x="${sym.x}" y="${sym.y}" width="${sym.w}" height="${sym.h}" fill="#ffffff" stroke="#000000" stroke-width="1" />`);
emitConveyanceLabelInner(lines, sym as PlacedSymbol);
lines.push(` </g>`);
} else if (isExtendoType(sym.symbolId)) {
const bracketW = 10.6 / 31.07 * 73;
const x = sym.x, y = sym.y, w = sym.w, h = sym.h;
const pts = [
[x + bracketW * 0.44, y + h * 0.085],
[x + bracketW, y + h * 0.085],
[x + bracketW, y + h * 0.222],
[x + w, y + h * 0.222],
[x + w, y + h * 0.780],
[x + bracketW, y + h * 0.780],
[x + bracketW, y + h * 0.917],
[x + bracketW * 0.44, y + h * 0.916],
[x + bracketW * 0.34, y + h * 0.985],
[x, y + h * 0.980],
[x, y + h * 0.017],
[x + bracketW * 0.34, y + h * 0.016],
];
const d = `M ${pts[0][0]},${pts[0][1]} ` + pts.slice(1).map(p => `L ${p[0]},${p[1]}`).join(' ') + ' Z';
lines.push(` <g ${idAttr}${outerTransform ? ` transform="${outerTransform}"` : ''}>`);
lines.push(` <path d="${d}" fill="#ffffff" stroke="#000000" stroke-width="1" />`);
emitConveyanceLabelInner(lines, sym as PlacedSymbol);
lines.push(` </g>`);
} else if (isPhotoeyeType(sym.symbolId)) {
const { leftCap, rightCap } = PHOTOEYE_CONFIG;
const x = sym.x, y = sym.y, w = sym.w, h = sym.h;
const pts = [
[x + leftCap, y + h * 0.42],
[x + leftCap, y + h * 0.248],
[x, y + h * 0.05],
[x, y + h * 0.948],
[x + leftCap, y + h * 0.744],
[x + leftCap, y + h * 0.585],
[x + w - rightCap, y + h * 0.585],
[x + w - rightCap, y + h * 0.826],
[x + w, y + h * 0.826],
[x + w, y + h * 0.181],
[x + w - rightCap, y + h * 0.181],
[x + w - rightCap, y + h * 0.42],
];
const d = `M ${pts[0][0]},${pts[0][1]} ` + pts.slice(1).map(p => `L ${p[0]},${p[1]}`).join(' ') + ' Z';
lines.push(` <path ${idAttr} d="${d}" fill="#ffffff" stroke="#000000" stroke-width="1"${outerTransform ? ` transform="${outerTransform}"` : ''} />`);
} else {
// Regular SVG symbol
try {
const svgText = await (await fetch(sym.file)).text();
const doc = new DOMParser().parseFromString(svgText, 'image/svg+xml');
const svgEl = doc.documentElement;
if (svgEl.querySelector('parsererror')) {
console.error('SVG parse error for:', sym.file);
continue;
}
const vb = svgEl.getAttribute('viewBox');
const [vbX, vbY, vbW, vbH] = vb
? vb.split(/[\s,]+/).map(Number)
: [0, 0, sym.w, sym.h];
const sx = sym.w / vbW;
const sy = sym.h / vbH;
// Base positioning transform
let baseTransform = `translate(${sym.x},${sym.y}) scale(${sx.toFixed(6)},${sy.toFixed(6)}) translate(${-vbX},${-vbY})`;
if (mirrored) baseTransform = `translate(${cx},0) scale(-1,1) translate(${-cx},0) ${baseTransform}`;
if (rot) baseTransform = `rotate(${rot},${cx},${cy}) ${baseTransform}`;
const children = Array.from(svgEl.children);
if (children.length === 1 && children[0].tagName === 'g') {
// SVG has a <g> wrapper — keep it, put id/label on it, compose transforms
const g = children[0];
const gTransform = g.getAttribute('transform') || '';
const fullTransform = gTransform ? `${baseTransform} ${gTransform}` : baseTransform;
const innerContent = serializeChildren(g);
lines.push(` <g ${idAttr} transform="${fullTransform}">`);
lines.push(` ${innerContent}`);
lines.push(' </g>');
} else if (children.length === 1) {
// Single element, no group — put id/label/transform directly on it
const el = children[0].cloneNode(true) as Element;
const elTransform = el.getAttribute('transform');
el.setAttribute('transform', elTransform ? `${baseTransform} ${elTransform}` : baseTransform);
el.setAttribute('id', label);
let s = new XMLSerializer().serializeToString(el)
.replace(/ xmlns="http:\/\/www\.w3\.org\/2000\/svg"/g, '');
// Inject inkscape:label after the tag name
const firstSpace = s.indexOf(' ');
s = s.slice(0, firstSpace) + ` inkscape:label="${label}"` + s.slice(firstSpace);
lines.push(` ${s}`);
} else {
// Multiple children without a group — wrap in <g> with id/label
const innerContent = serializeChildren(svgEl);
lines.push(` <g ${idAttr} transform="${baseTransform}">`);
lines.push(` ${innerContent}`);
lines.push(' </g>');
}
} catch (err) {
console.error('Failed to embed symbol:', sym.name, err);
}
}
}
// Embed layout data as metadata for re-import
const layoutData = {
symbols: layout.symbols.filter(s => !s.hidden && !layout.hiddenGroups.has(getSymbolGroup(s.symbolId))).map(s => serializeSymbol(s)),
gridSize: layout.gridSize,
minSpacing: layout.minSpacing,
canvasW: layout.canvasW,
canvasH: layout.canvasH,
};
lines.push(` <!-- LAYOUT_DATA:${JSON.stringify(layoutData)}:END_LAYOUT_DATA -->`);
lines.push('</svg>');
return lines.join('\n');
}
export async function exportSVG() {
const svgStr = await buildSvgString();
const mcmName = layout.currentMcm || 'export';
downloadBlob(new Blob([svgStr], { type: 'image/svg+xml' }), `${mcmName}_Detailed_View.svg`);
}
/** Convert SVG element to Ignition JSON format */
function svgElementToIgnition(el: Element): Record<string, any> | null {
const tag = el.tagName.toLowerCase();
if (tag === 'defs' || tag === 'sodipodi:namedview') return null;
const obj: Record<string, any> = {};
if (tag === 'g') {
obj.type = 'group';
obj.name = el.getAttribute('id') || el.getAttribute('inkscape:label') || 'group';
if (el.getAttribute('id')) obj.id = el.getAttribute('id');
if (el.getAttribute('transform')) obj.transform = el.getAttribute('transform');
// Convert children
const children: Record<string, any>[] = [];
for (const child of Array.from(el.children)) {
const c = svgElementToIgnition(child);
if (c) children.push(c);
}
if (children.length) obj.elements = children;
// Ignition metadata from data-* attributes
if (el.getAttribute('data-color')) obj.color = el.getAttribute('data-color');
if (el.getAttribute('data-state')) obj.state = el.getAttribute('data-state');
if (el.getAttribute('data-priority')) obj.priority = el.getAttribute('data-priority');
if (el.getAttribute('data-tagpath')) obj.tagpaths = [el.getAttribute('data-tagpath')];
} else if (tag === 'rect') {
obj.type = 'rect';
obj.name = el.getAttribute('id') || 'rect';
if (el.getAttribute('id')) obj.id = el.getAttribute('id');
for (const attr of ['x', 'y', 'width', 'height', 'rx', 'ry']) {
if (el.getAttribute(attr)) obj[attr] = el.getAttribute(attr);
}
if (el.getAttribute('transform')) obj.transform = el.getAttribute('transform');
addFillStroke(obj, el);
// Ignition metadata
if (el.getAttribute('data-color')) obj.color = el.getAttribute('data-color');
if (el.getAttribute('data-state')) obj.state = el.getAttribute('data-state');
if (el.getAttribute('data-priority')) obj.priority = el.getAttribute('data-priority');
if (el.getAttribute('data-tagpath')) obj.tagpaths = [el.getAttribute('data-tagpath')];
} else if (tag === 'path') {
obj.type = 'path';
obj.name = el.getAttribute('id') || 'path';
if (el.getAttribute('id')) obj.id = el.getAttribute('id');
obj.d = el.getAttribute('d') || '';
if (el.getAttribute('transform')) obj.transform = el.getAttribute('transform');
addFillStroke(obj, el);
if (el.getAttribute('data-color')) obj.color = el.getAttribute('data-color');
if (el.getAttribute('data-state')) obj.state = el.getAttribute('data-state');
if (el.getAttribute('data-priority')) obj.priority = el.getAttribute('data-priority');
if (el.getAttribute('data-tagpath')) obj.tagpaths = [el.getAttribute('data-tagpath')];
} else if (tag === 'text') {
obj.type = 'text';
obj.name = el.getAttribute('id') || 'text';
obj.text = el.textContent || '';
for (const attr of ['x', 'y']) {
if (el.getAttribute(attr)) obj[attr] = el.getAttribute(attr);
}
if (el.getAttribute('text-anchor')) obj.textAnchor = el.getAttribute('text-anchor');
if (el.getAttribute('transform')) obj.transform = el.getAttribute('transform');
// Parse inline style to Ignition style object
const styleStr = el.getAttribute('style');
if (styleStr) {
const styleObj: Record<string, string> = {};
let fillColor = '#000000';
for (const part of styleStr.split(';')) {
const [k, v] = part.split(':').map(s => s.trim());
if (k === 'font-family') styleObj.fontFamily = v;
else if (k === 'font-weight') styleObj.fontWeight = v;
else if (k === 'font-size') styleObj.fontSize = v;
else if (k === 'fill') fillColor = v;
}
if (Object.keys(styleObj).length) obj.style = styleObj;
obj.fill = { paint: fillColor };
} else {
if (el.getAttribute('fill')) obj.fill = { paint: el.getAttribute('fill') };
if (el.getAttribute('font-size')) obj.fontSize = el.getAttribute('font-size');
}
} else if (tag === 'polyline') {
obj.type = 'polyline';
obj.name = el.getAttribute('id') || 'polyline';
if (el.getAttribute('points')) obj.points = el.getAttribute('points');
addFillStroke(obj, el);
if (el.getAttribute('transform')) obj.transform = el.getAttribute('transform');
} else if (tag === 'circle') {
obj.type = 'circle';
obj.name = el.getAttribute('id') || 'circle';
for (const attr of ['cx', 'cy', 'r']) {
if (el.getAttribute(attr)) obj[attr] = el.getAttribute(attr);
}
addFillStroke(obj, el);
if (el.getAttribute('transform')) obj.transform = el.getAttribute('transform');
} else if (tag === 'polygon') {
obj.type = 'polygon';
obj.name = el.getAttribute('id') || 'polygon';
if (el.getAttribute('points')) obj.points = el.getAttribute('points');
addFillStroke(obj, el);
if (el.getAttribute('transform')) obj.transform = el.getAttribute('transform');
} else {
return null; // skip unknown elements
}
return obj;
}
/** Extract fill/stroke from SVG element (handles both attributes and style) */
function addFillStroke(obj: Record<string, any>, el: Element) {
const styleStr = el.getAttribute('style');
if (styleStr) {
const styles: Record<string, string> = {};
for (const part of styleStr.split(';')) {
const [k, v] = part.split(':').map(s => s.trim());
if (k && v) styles[k] = v;
}
const fill: Record<string, string> = {};
if (styles.fill) fill.paint = styles.fill;
else if (el.getAttribute('fill')) fill.paint = el.getAttribute('fill')!;
if (styles['fill-opacity']) fill.opacity = styles['fill-opacity'];
if (fill.paint) obj.fill = fill;
const stroke: Record<string, string> = {};
if (styles.stroke) stroke.paint = styles.stroke;
if (styles['stroke-width']) stroke.width = styles['stroke-width'];
if (styles['stroke-dasharray']) stroke.dasharray = styles['stroke-dasharray'];
if (styles['stroke-opacity']) stroke.opacity = styles['stroke-opacity'];
if (stroke.paint) obj.stroke = stroke;
} else {
if (el.getAttribute('fill')) {
const fill: Record<string, string> = { paint: el.getAttribute('fill')! };
if (el.getAttribute('fill-opacity')) fill.opacity = el.getAttribute('fill-opacity')!;
obj.fill = fill;
}
if (el.getAttribute('stroke')) {
const stroke: Record<string, string> = { paint: el.getAttribute('stroke')! };
if (el.getAttribute('stroke-width')) stroke.width = el.getAttribute('stroke-width')!;
obj.stroke = stroke;
}
}
// fill="none" → paint: "transparent"
if (obj.fill?.paint === 'none') obj.fill.paint = 'transparent';
if (obj.stroke?.paint === 'none') obj.stroke.paint = 'transparent';
}
/** Build Ignition ia.shapes.svg component data */
async function buildIgnitionComponent(): Promise<Record<string, any>> {
const svgStr = await buildSvgString();
const doc = new DOMParser().parseFromString(svgStr, 'image/svg+xml');
const svgEl = doc.documentElement;
const elements: Record<string, any>[] = [];
for (const child of Array.from(svgEl.children)) {
if (child.nodeType === 8) continue;
const converted = svgElementToIgnition(child);
if (converted) elements.push(converted);
}
const mcmName = layout.currentMcm || 'export';
return {
type: 'ia.shapes.svg',
version: 0,
props: {
viewBox: svgEl.getAttribute('viewBox') || `0 0 ${layout.canvasW} ${layout.canvasH}`,
elements,
},
meta: { name: `${mcmName}_Detailed_View` },
position: { width: 1, height: 1 },
custom: {},
};
}
/** Export as Ignition SCADA JSON file (download) */
export async function exportIgnitionJSON() {
const component = await buildIgnitionComponent();
const mcmName = layout.currentMcm || 'export';
downloadBlob(
new Blob([JSON.stringify([component], null, 2)], { type: 'application/json' }),
`${mcmName}_Detailed_View.json`
);
}
/** Deploy directly to Ignition project directory */
export async function deployToIgnition() {
const component = await buildIgnitionComponent();
const projectName = layout.ignitionProject || 'Testing_Project';
const viewName = layout.ignitionViewName || layout.currentMcm || 'MCM01';
const viewJson = JSON.stringify({
custom: {},
params: {},
props: {
defaultSize: { height: layout.canvasH, width: layout.canvasW },
},
root: {
children: [{
meta: { name: viewName },
position: { width: 1, height: 1 },
props: component.props,
type: 'ia.shapes.svg',
}],
meta: { name: 'root' },
props: { mode: 'percent' },
type: 'ia.container.coord',
},
}, null, 2);
// Compute SHA-256 signature of view.json for Ignition resource integrity
const viewBytes = new TextEncoder().encode(viewJson);
const hashBuffer = await crypto.subtle.digest('SHA-256', viewBytes);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const signature = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
const resourceJson = JSON.stringify({
scope: 'G',
version: 1,
restricted: false,
overridable: true,
files: ['view.json'],
attributes: {
lastModificationSignature: signature,
lastModification: {
actor: 'admin',
timestamp: new Date().toISOString().replace(/\.\d{3}Z$/, 'Z'),
},
},
}, null, 2);
try {
const resp = await fetch('/api/deploy-ignition', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ projectName, viewName, viewJson, resourceJson }),
});
const result = await resp.json();
if (result.ok) {
// Trigger project rescan via gateway scan endpoint
try {
await fetch('/api/ignition-scan', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ projectName }),
});
} catch { /* scan is best-effort */ }
alert(`Deployed to Ignition!\n${result.path}\n\nClose and reopen the project in Designer to see the view.`);
} else {
alert(`Deploy failed: ${result.error}`);
}
} catch (err) {
alert(`Deploy failed: ${err instanceof Error ? err.message : String(err)}`);
}
}
/** Emit EPC symbol — polyline + icon + right box, wrapped in <g> with id/label */
async function emitEpc(lines: string[], sym: PlacedSymbol, label: string, outerTransform: string) {
const waypoints = sym.epcWaypoints || EPC_CONFIG.defaultWaypoints;
const ox = sym.x;
const oy = sym.y;
const parts: string[] = [];
const tAttr = outerTransform ? ` transform="${outerTransform}"` : '';
// Polyline
if (waypoints.length >= 2) {
const points = waypoints.map(wp => `${ox + wp.x},${oy + wp.y}`).join(' ');
parts.push(` <polyline points="${points}" fill="none" stroke="#000000" stroke-width="${EPC_CONFIG.lineWidth}" />`);
}
if (waypoints.length >= 2) {
// Left icon
const lb = EPC_CONFIG.leftBox;
const p0x = ox + waypoints[0].x, p0y = oy + waypoints[0].y;
const p1x = ox + waypoints[1].x, p1y = oy + waypoints[1].y;
const lAngle = Math.atan2(p1y - p0y, p1x - p0x) * 180 / Math.PI;
try {
const svgText = await (await fetch(EPC_CONFIG.iconFile)).text();
const doc = new DOMParser().parseFromString(svgText, 'image/svg+xml');
const svgEl = doc.documentElement;
const vb = svgEl.getAttribute('viewBox');
const [vbX, vbY, vbW, vbH] = vb ? vb.split(/[\s,]+/).map(Number) : [0, 0, lb.w, lb.h];
const sx = lb.w / vbW;
const sy = lb.h / vbH;
const iconTransform = `translate(${p0x},${p0y}) rotate(${lAngle.toFixed(2)}) translate(${-lb.w},${-lb.h / 2}) scale(${sx.toFixed(6)},${sy.toFixed(6)}) translate(${-vbX},${-vbY})`;
const children = Array.from(svgEl.children);
if (children.length === 1 && children[0].tagName === 'g') {
const g = children[0];
const gT = g.getAttribute('transform') || '';
const fullT = gT ? `${iconTransform} ${gT}` : iconTransform;
parts.push(` <g transform="${fullT}">`);
parts.push(` ${serializeChildren(g)}`);
parts.push(` </g>`);
} else {
parts.push(` <g transform="${iconTransform}">`);
parts.push(` ${serializeChildren(svgEl)}`);
parts.push(` </g>`);
}
} catch {
parts.push(` <rect x="${-lb.w}" y="${-lb.h / 2}" width="${lb.w}" height="${lb.h}" fill="#aaaaaa" stroke="#000000" stroke-width="${EPC_CONFIG.lineWidth}" transform="translate(${p0x},${p0y}) rotate(${lAngle.toFixed(2)})" />`);
}
// Right box
const last = waypoints[waypoints.length - 1];
const prev = waypoints[waypoints.length - 2];
const plx = ox + last.x, ply = oy + last.y;
const ppx = ox + prev.x, ppy = oy + prev.y;
const rAngle = Math.atan2(ply - ppy, plx - ppx) * 180 / Math.PI;
const rb = EPC_CONFIG.rightBox;
parts.push(` <rect x="${-rb.w}" y="${-rb.h / 2}" width="${rb.w}" height="${rb.h}" fill="#aaaaaa" stroke="#000000" stroke-width="${EPC_CONFIG.lineWidth}" transform="translate(${plx},${ply}) rotate(${rAngle.toFixed(2)})" />`);
}
lines.push(` <g id="${label}" inkscape:label="${label}"${tAttr}>`);
lines.push(parts.join('\n'));
lines.push(' </g>');
}
export function loadLayoutSVG(file: File): Promise<void> {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = (ev) => {
try {
const svgText = ev.target!.result as string;
const match = svgText.match(/<!-- LAYOUT_DATA:(.*?):END_LAYOUT_DATA -->/);
if (!match) throw new Error('No layout data found in SVG. Only SVGs exported from this tool can be imported.');
const data = JSON.parse(match[1]);
layout.pushUndo();
if (data.gridSize) layout.gridSize = data.gridSize;
if (data.minSpacing) layout.minSpacing = data.minSpacing;
if (data.canvasW) layout.canvasW = data.canvasW;
if (data.canvasH) layout.canvasH = data.canvasH;
layout.symbols = [];
layout.nextId = 1;
for (const s of data.symbols) {
layout.symbols.push(deserializeSymbol(s, layout.nextId++));
}
layout.markDirty();
layout.saveMcmState();
resolve();
} catch (err) {
reject(err);
}
};
reader.readAsText(file);
});
}
export function loadLayoutJSON(file: File): Promise<void> {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = (ev) => {
try {
const data = JSON.parse(ev.target!.result as string);
layout.pushUndo();
if (data.gridSize) layout.gridSize = data.gridSize;
if (data.minSpacing) layout.minSpacing = data.minSpacing;
layout.symbols = [];
layout.nextId = 1;
for (const s of data.symbols) {
layout.symbols.push(deserializeSymbol(s, layout.nextId++));
}
layout.markDirty();
layout.saveMcmState();
resolve();
} catch (err) {
reject(err);
}
};
reader.readAsText(file);
});
}