This commit is contained in:
marco committed 2026-10-26 14:56:24 +01:00
1 parent 367c93c93e
commit 4bfd5d9bf7
34 files changed
+558 -94

No files matched your search

+67
View File
@@ -0,0 +1,67 @@
import { describe, expect, it } from '@jest/globals';
import { DrawPfeil } from './drawPfeil';
import { parsePfeilRequest } from '../../_dto/pfeil.dto';
import { createXamlFiles } from '../../anlagen/visu-aggregate/procon-xaml-export';
describe('Pfeilgenerator', () => {
it('exportiert frei kombinierte Namen mit zugehöriger Farbe und umgekehrter Richtung', () => {
const request = parsePfeilRequest({ config: { points: [{ x: 6, y: 6 }, { x: 86, y: 6 }], variants: [
{ name: 'activeRight', state: 'active' }, { name: 'activeLeft', state: 'active', reverse: true },
{ name: 'InactivePos1', state: 'inactive' }, { name: 'InactivePos2', state: 'inactive' },
{ name: 'errorRightPos2', state: 'error' },
] }, options: { colorActive: '#123456', colorInactive: '#654321', colorError: '#ff0000' } });
const result = new DrawPfeil().getObject(request.config, request.options);
expect(result.varianten.map(v => v.state)).toEqual(['activeRight', 'activeLeft', 'InactivePos1', 'InactivePos2', 'errorRightPos2']);
expect(result.varianten[0].svg).toContain('#123456');
expect(result.varianten[1].svg).not.toEqual(result.varianten[0].svg);
expect(result.varianten[2].svg).toContain('#654321');
expect(result.varianten[4].svg).toContain('#ff0000');
const exported = createXamlFiles([{ componentId: 'c1', viewName: 'Band', elemente: [{
key: 'pfeil_1', name: '', typ: 'pfeil', darstellung: 'xaml', posX: 0, posY: 0, width: result.width, height: result.height, zIndex: 0, show: true,
varianten: result.varianten.map(v => ({ zustand: v.state, xaml: v.xaml, xamlBase64: v.xamlBase64 })),
}] }], { anlageId: 'a1', auswahl: [{ componentId: 'c1', elementKeys: ['pfeil_1'] }] });
expect(exported.files.map(file => file.name)).toEqual(request.config.variants!.map(v => `Band_pfeil_1_${v.name}.xaml`));
});
it.each([
[], [{ name: '../active', state: 'active' }], [{ name: 'a', state: 'unknown' }],
[{ name: 'ActivePos1', state: 'active' }, { name: 'activepos1', state: 'inactive' }],
[{ name: 'active', state: 'active', reverse: 'true' }],
])('weist ungültige Varianten zurück: %j', (...variants) => {
expect(() => parsePfeilRequest({ config: { points: [{ x: 0, y: 0 }, { x: 10, y: 10 }], variants } })).toThrow();
});
it.each([
[{ x: 0, y: 0 }, { x: 100, y: 0 }],
[{ x: -100, y: -50 }, { x: 0, y: -50 }, { x: 20, y: 20 }, { x: 20, y: 20 }],
[{ x: 0, y: 0 }, { x: 0, y: -100 }],
])('rendert Punkte %j in allen Zuständen ohne ungültige Geometrie', (...points) => {
const request = parsePfeilRequest({ config: { points }, options: { colorActive: '#123456' } });
const result = new DrawPfeil().getObject(request.config, request.options);
expect(result.width).toBeGreaterThan(0); expect(result.height).toBeGreaterThan(0);
expect(result.varianten.map(v => v.state)).toEqual(['inactive', 'active', 'error']);
for (const variant of result.varianten) {
expect(variant.svg).toContain('<svg'); expect(variant.xaml).toContain('<Path');
expect(variant.svg + variant.xaml).not.toMatch(/NaN|Infinity/);
expect(Buffer.from(variant.xamlBase64, 'base64').toString('utf8')).toBe(variant.xaml);
}
expect(result.varianten[1].svg).toContain('#123456');
});
it.each([[], [{ x: 0, y: 0 }], [{ x: 1, y: 1 }, { x: 1, y: 1 }], [{ x: NaN, y: 0 }, { x: 1, y: 1 }]])(
'weist unbrauchbare Punktlisten zurück: %j', (...points) => {
expect(() => parsePfeilRequest({ config: { points } })).toThrow();
});
it('exportiert mehrere Pfeile des Aggregats mit eindeutigen Zustandsdateien', () => {
const result = new DrawPfeil().getObject({ points: [{ x: 6, y: 6 }, { x: 86, y: 46 }] });
const elemente = ['pfeil_1', 'pfeil_2'].map(key => ({
key, name: key, typ: 'pfeil', darstellung: 'xaml' as const, width: result.width, height: result.height,
posX: 0, posY: 0, zIndex: 1, show: true,
varianten: result.varianten.map(v => ({ zustand: v.state, xaml: v.xaml, xamlBase64: v.xamlBase64 })),
}));
const exported = createXamlFiles([{ componentId: 'c1', viewName: 'Band', elemente }],
{ anlageId: 'a1', auswahl: [{ componentId: 'c1', elementKeys: ['pfeil_1', 'pfeil_2'] }] });
expect(exported.files).toHaveLength(6);
expect(exported.files[0].name).toBe('Band_pfeil_1_inactive.xaml');
expect(exported.files.every(file => file.data.includes('<Path'))).toBe(true);
});
});
+34
View File
@@ -9,6 +9,8 @@ import { Injectable } from "@nestjs/common";
import { DrawDto, DrawShapeOptionenDto } from "src/_dto/draw.dto";
import { DrawHeader } from "../lib/drawHeader";
import { LineDto } from "src/_dto/visuElement.dto";
import { DrawDataDto } from '../../_dto/draw.dto';
import type { PfeilConfig, PfeilOptions } from '../../_dto/pfeil.dto';
interface Para {
scale?: number,
@@ -17,6 +19,38 @@ interface Para {
@Injectable()
export class DrawPfeil {
getObject(config: PfeilConfig, options: PfeilOptions = {}): DrawDataDto {
const sourcePoints = config.points.filter((p, i, all) => !i || p.x !== all[i - 1].x || p.y !== all[i - 1].y);
const minX = Math.min(0, ...sourcePoints.map(p => p.x - 6)), minY = Math.min(0, ...sourcePoints.map(p => p.y - 6));
const width = Math.max(...sourcePoints.map(p => p.x + 6)) - minX;
const height = Math.max(...sourcePoints.map(p => p.y + 6)) - minY;
const variants = config.variants ?? (['inactive', 'active', 'error'] as const).map(state => ({ name: state, state, reverse: false }));
const varianten = variants.map(variant => {
const points = variant.reverse ? [...sourcePoints].reverse() : sourcePoints;
const end = points[points.length - 1], previous = points[points.length - 2];
const distance = Math.hypot(end.x - previous.x, end.y - previous.y);
const ux = (end.x - previous.x) / distance, uy = (end.y - previous.y) / distance;
const size = Math.min(8, distance);
const base = { x: end.x - ux * size, y: end.y - uy * size };
const state = variant.state;
const color = state === 'active' ? options.colorActive ?? this.gemisch
: state === 'error' ? options.colorError ?? this.red : options.colorInactive ?? this.normal;
const group = new DrawGroup({});
const line = new DrawPath(points.map((p, i) => ({ type: i ? 'L' : 'M', x: p.x - minX, y: p.y - minY })));
line.setAtrr({ stroke: color, strokeWidth: 3 });
const head = new DrawPath([
{ type: 'M', x: base.x - uy * 4 - minX, y: base.y + ux * 4 - minY },
{ type: 'L', x: end.x - minX, y: end.y - minY },
{ type: 'L', x: base.x + uy * 4 - minX, y: base.y - ux * 4 - minY }, { type: 'Z' },
]);
head.setAtrr({ fill: color, stroke: color, strokeWidth: 1 });
group.add(head); group.add(line);
const header = new DrawHeader({ lenght: width, height }); header.add(group);
const svg = header.getChildString(false, 0), xaml = header.getChildString(true, 0);
return { state: variant.name, svg, xaml, xamlBase64: Buffer.from(xaml, 'utf8').toString('base64') };
});
return { name: 'pfeil', width, height, varianten };
}
scale = 0;
winkel = 0;
raster = 4;