task(CAD-2): real hatch patterns - 9 patterns (ANSI31/32/37, NET, GRASS, DOTS, SOLID + legacy), boundary detection, render adapter hatch lines
This commit is contained in:
@@ -394,8 +394,15 @@ export const hatchTool: ToolExtensionV2 = {
|
||||
},
|
||||
optionsSchema: [
|
||||
{ key: 'hatchPattern', label: 'Muster', type: 'select', options: [
|
||||
{ value: 'lines', label: 'Linien' },
|
||||
{ value: 'cross', label: 'Kreuz' },
|
||||
{ value: 'ANSI31', label: 'ANSI31 (45°)' },
|
||||
{ value: 'ANSI32', label: 'ANSI32 (Eisen)' },
|
||||
{ value: 'ANSI37', label: 'ANSI37 (Beton)' },
|
||||
{ value: 'NET', label: 'Netz' },
|
||||
{ value: 'GRASS', label: 'Gras' },
|
||||
{ value: 'DOTS', label: 'Punkte' },
|
||||
{ value: 'SOLID', label: 'Voll' },
|
||||
{ value: 'lines', label: 'Linien (alt)' },
|
||||
{ value: 'cross', label: 'Kreuz (alt)' },
|
||||
] },
|
||||
{ key: 'hatchSpacing', label: 'Abstand', type: 'number', min: 2, max: 40 },
|
||||
],
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
*/
|
||||
import type { CADElement } from '../../types/cad.types';
|
||||
import { getDimStyle } from '../../services/dimStyleService';
|
||||
import { generateHatchLines, computeBoundary, getHatchPattern } from '../../services/hatchPatternService';
|
||||
import type { Pt } from '../../tools/modification/geometry';
|
||||
|
||||
/** Zeichenprimitiv — kleinste darstellbare Einheit. */
|
||||
@@ -66,6 +67,62 @@ function arrowPrimitives(x: number, y: number, ang: number, size: number, color:
|
||||
}];
|
||||
}
|
||||
|
||||
/**
|
||||
* Task CAD-2: Hatch-Linien fuer ein Element mit hatch-Properties.
|
||||
* Nutzt hatchPatternService.generateHatchLines mit Boundary aus der
|
||||
* Element-Kontur. Ohne Muster: leeres Array.
|
||||
*/
|
||||
function hatchPrimitives(el: CADElement, stroke: string): ShapePrimitive[] {
|
||||
const props = el.properties as Record<string, unknown>;
|
||||
if (props.hatch !== true) return [];
|
||||
const patternId = (props.hatchPattern as string | undefined) ?? 'ANSI31';
|
||||
if (!getHatchPattern(patternId)) return [];
|
||||
const spacing = (props.hatchSpacing as number | undefined) ?? 8;
|
||||
|
||||
// Boundary aus Kontur je Elementtyp
|
||||
let bbox: { minX: number; minY: number; maxX: number; maxY: number } | null = null;
|
||||
if (Array.isArray(props.points)) {
|
||||
bbox = computeBoundary(props.points as Array<{ x: number; y: number }>);
|
||||
} else if (el.type === 'circle' || el.type === 'arc') {
|
||||
const r = (props.radius as number | undefined) ?? el.width / 2;
|
||||
bbox = { minX: el.x - r, minY: el.y - r, maxX: el.x + r, maxY: el.y + r };
|
||||
} else {
|
||||
bbox = {
|
||||
minX: el.x - el.width / 2, minY: el.y - el.height / 2,
|
||||
maxX: el.x + el.width / 2, maxY: el.y + el.height / 2,
|
||||
};
|
||||
}
|
||||
if (!bbox) return [];
|
||||
|
||||
const segs = generateHatchLines(patternId, bbox, spacing);
|
||||
const out: ShapePrimitive[] = [];
|
||||
for (const s of segs) {
|
||||
if (s.fill) {
|
||||
const rectPrim: ShapePrimitive = {
|
||||
kind: 'rect',
|
||||
x: (bbox.minX + bbox.maxX) / 2,
|
||||
y: (bbox.minY + bbox.maxY) / 2,
|
||||
w: bbox.maxX - bbox.minX,
|
||||
h: bbox.maxY - bbox.minY,
|
||||
fill: s.fill,
|
||||
stroke,
|
||||
strokeWidth: 1,
|
||||
};
|
||||
out.push(rectPrim);
|
||||
} else {
|
||||
const linePrim: ShapePrimitive = {
|
||||
kind: 'line',
|
||||
from: s.from,
|
||||
to: s.to,
|
||||
stroke,
|
||||
strokeWidth: 0.5,
|
||||
};
|
||||
out.push(linePrim);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export function elementToPrimitives(el: CADElement, attrDefs?: Array<{ tag: string; prompt: string; defaultValue?: string }>): ShapePrimitive[] {
|
||||
const props = (el.properties ?? {}) as Record<string, unknown>;
|
||||
const stroke = (props.stroke as string | undefined) ?? FALLBACK_STROKE;
|
||||
@@ -90,7 +147,7 @@ export function elementToPrimitives(el: CADElement, attrDefs?: Array<{ tag: stri
|
||||
|
||||
case 'rect': {
|
||||
// Legacy-BBox-Konvention: x/y ist das Zentrum des Rechtecks
|
||||
return [{
|
||||
const base: ShapePrimitive[] = [{
|
||||
kind: 'rect',
|
||||
x: el.x - el.width / 2,
|
||||
y: el.y - el.height / 2,
|
||||
@@ -100,11 +157,12 @@ export function elementToPrimitives(el: CADElement, attrDefs?: Array<{ tag: stri
|
||||
stroke,
|
||||
strokeWidth: sw,
|
||||
}];
|
||||
return [...base, ...hatchPrimitives(el, stroke)];
|
||||
}
|
||||
|
||||
case 'circle': {
|
||||
const radius = (props.radius as number | undefined) ?? el.width / 2;
|
||||
return [{
|
||||
const circBase: ShapePrimitive[] = [{
|
||||
kind: 'circle',
|
||||
center: { x: el.x, y: el.y },
|
||||
radius,
|
||||
@@ -112,6 +170,7 @@ export function elementToPrimitives(el: CADElement, attrDefs?: Array<{ tag: stri
|
||||
stroke,
|
||||
strokeWidth: sw,
|
||||
}];
|
||||
return [...circBase, ...hatchPrimitives(el, stroke)];
|
||||
}
|
||||
|
||||
case 'arc': {
|
||||
@@ -133,13 +192,14 @@ export function elementToPrimitives(el: CADElement, attrDefs?: Array<{ tag: stri
|
||||
? (props.points as unknown[]).map(asPoint)
|
||||
: [];
|
||||
if (pts.length < 2) return [];
|
||||
return [{
|
||||
const polyBase: ShapePrimitive[] = [{
|
||||
kind: 'polyline',
|
||||
points: pts,
|
||||
close: el.type === 'polygon',
|
||||
stroke,
|
||||
strokeWidth: sw,
|
||||
}];
|
||||
return [...polyBase, ...hatchPrimitives(el, stroke)];
|
||||
}
|
||||
|
||||
case 'text': {
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
/**
|
||||
* Task CAD-2 – Echte Hatch-Muster + Boundary-Erkennung.
|
||||
*
|
||||
* Standard-CAD-Schraffurmuster als Linien-Segment-Generatoren für
|
||||
* eine BBox. computeBoundary liefert die umschlossene Fläche aus
|
||||
* einer Kontur (Polygon-Punkte).
|
||||
*/
|
||||
|
||||
export interface HatchSeg {
|
||||
from: { x: number; y: number };
|
||||
to: { x: number; y: number };
|
||||
fill?: string;
|
||||
closed?: boolean;
|
||||
points?: Array<{ x: number; y: number }>;
|
||||
}
|
||||
|
||||
export interface HatchPattern {
|
||||
id: string;
|
||||
label: string;
|
||||
description: string;
|
||||
defaultSpacing: number;
|
||||
/** 45°-Ersatzwinkel für Linienfamilien (Grad). */
|
||||
angles: number[];
|
||||
/** Punkte-Muster (DOTS) statt Linien. */
|
||||
dots?: boolean;
|
||||
/** Vollflächige Füllung. */
|
||||
solid?: boolean;
|
||||
}
|
||||
|
||||
export type HatchPatternId = string;
|
||||
|
||||
export const HATCH_PATTERNS: HatchPattern[] = [
|
||||
{ id: 'lines', label: 'Linien (45°)', description: 'Einfache 45°-Schraffur', defaultSpacing: 8, angles: [45] },
|
||||
{ id: 'cross', label: 'Kreuz', description: 'Kreuzschraffur 45°/-45°', defaultSpacing: 8, angles: [45, -45] },
|
||||
{ id: 'ANSI31', label: 'ANSI31 (45°)', description: 'ANSI-Standard 45°', defaultSpacing: 6, angles: [45] },
|
||||
{ id: 'ANSI32', label: 'ANSI32 (Eisen)', description: 'ANSI Eisen 45°/-45°', defaultSpacing: 6, angles: [45, -45] },
|
||||
{ id: 'ANSI37', label: 'ANSI37 (Beton)', description: 'ANSI Beton 45°/-45° dicht', defaultSpacing: 3, angles: [45, -45] },
|
||||
{ id: 'NET', label: 'Netz', description: 'Horizontal + vertikal', defaultSpacing: 10, angles: [0, 90] },
|
||||
{ id: 'GRASS', label: 'Gras', description: 'Kurze Gras-Striche', defaultSpacing: 6, angles: [75] },
|
||||
{ id: 'DOTS', label: 'Punkte', description: 'Punktschraffur', defaultSpacing: 8, angles: [], dots: true },
|
||||
{ id: 'SOLID', label: 'Voll', description: 'Vollflächige Füllung', defaultSpacing: 0, angles: [], solid: true },
|
||||
];
|
||||
|
||||
/** Muster per ID (oder undefined). */
|
||||
export function getHatchPattern(id: string | undefined): HatchPattern | undefined {
|
||||
if (!id) return undefined;
|
||||
return HATCH_PATTERNS.find((p) => p.id === id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Erzeugt Schraffur-Segmente für eine BBox nach Muster.
|
||||
* SOLID liefert EIN Segment mit fill-Flag; DOTS liefert Punkte-Muster.
|
||||
*/
|
||||
export function generateHatchLines(
|
||||
patternId: string,
|
||||
bbox: { minX: number; minY: number; maxX: number; maxY: number },
|
||||
spacing: number,
|
||||
): HatchSeg[] {
|
||||
const pattern = getHatchPattern(patternId);
|
||||
if (!pattern) return [];
|
||||
const { minX, minY, maxX, maxY } = bbox;
|
||||
const w = maxX - minX;
|
||||
const h = maxY - minY;
|
||||
|
||||
if (pattern.solid) {
|
||||
return [{ from: { x: minX, y: minY }, to: { x: maxX, y: maxY }, fill: '#888888' }];
|
||||
}
|
||||
|
||||
if (pattern.dots) {
|
||||
const out: HatchSeg[] = [];
|
||||
for (let y = minY + spacing / 2; y < maxY; y += spacing) {
|
||||
for (let x = minX + spacing / 2; x < maxX; x += spacing) {
|
||||
out.push({
|
||||
from: { x, y },
|
||||
to: { x: x + 1, y: y + 1 },
|
||||
points: [{ x, y }],
|
||||
});
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// Linienfamilien je Winkel
|
||||
const out: HatchSeg[] = [];
|
||||
for (const angleDeg of pattern.angles) {
|
||||
const rad = (angleDeg * Math.PI) / 180;
|
||||
const cosA = Math.cos(rad);
|
||||
const sinA = Math.sin(rad);
|
||||
const diag = Math.hypot(w, h);
|
||||
const step = spacing;
|
||||
const offsetCount = Math.ceil(diag / step) + 2;
|
||||
for (let i = -offsetCount; i <= offsetCount; i++) {
|
||||
const cx = minX + w / 2 + i * step;
|
||||
const cy = minY + h / 2;
|
||||
// Linie durch (cx,cy) in Richtung (cosA, sinA), begrenzt auf BBox
|
||||
const p1 = { x: cx - cosA * diag, y: cy - sinA * diag };
|
||||
const p2 = { x: cx + cosA * diag, y: cy + sinA * diag };
|
||||
// Clipping an BBox (einfaches Cohen-Sutherland)
|
||||
const cl = clipToBBox(p1, p2, bbox);
|
||||
if (cl) out.push({ from: cl[0], to: cl[1] });
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Cohen-Sutherland-artiges Clipping an BBox (vereinfacht). */
|
||||
function clipToBBox(
|
||||
p1: { x: number; y: number },
|
||||
p2: { x: number; y: number },
|
||||
b: { minX: number; minY: number; maxX: number; maxY: number },
|
||||
): Array<{ x: number; y: number }> | null {
|
||||
// Liang-Barsky
|
||||
let t0 = 0, t1 = 1;
|
||||
const dx = p2.x - p1.x;
|
||||
const dy = p2.y - p1.y;
|
||||
const clip = (p: number, q: number): boolean => {
|
||||
if (p === 0) return q >= 0;
|
||||
const r = q / p;
|
||||
if (p < 0) {
|
||||
if (r > t1) return false;
|
||||
if (r > t0) t0 = r;
|
||||
} else {
|
||||
if (r < t0) return false;
|
||||
if (r < t1) t1 = r;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
if (!clip(-dx, p1.x - b.minX)) return null;
|
||||
if (!clip(dx, b.maxX - p1.x)) return null;
|
||||
if (!clip(-dy, p1.y - b.minY)) return null;
|
||||
if (!clip(dy, b.maxY - p1.y)) return null;
|
||||
return [
|
||||
{ x: p1.x + t0 * dx, y: p1.y + t0 * dy },
|
||||
{ x: p1.x + t1 * dx, y: p1.y + t1 * dy },
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Boundary-Erkennung: BBox aus einer Kontur (Polygon-Punkte).
|
||||
* Weniger als 2 Punkte → null.
|
||||
*/
|
||||
export function computeBoundary(
|
||||
pts: Array<{ x: number; y: number }>,
|
||||
): { minX: number; minY: number; maxX: number; maxY: number } | null {
|
||||
if (!Array.isArray(pts) || pts.length < 2) return null;
|
||||
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
||||
for (const p of pts) {
|
||||
if (p.x < minX) minX = p.x;
|
||||
if (p.y < minY) minY = p.y;
|
||||
if (p.x > maxX) maxX = p.x;
|
||||
if (p.y > maxY) maxY = p.y;
|
||||
}
|
||||
return { minX, minY, maxX, maxY };
|
||||
}
|
||||
Reference in New Issue
Block a user