refactor: plt preview

This commit is contained in:
2026-03-15 01:07:23 +08:00
parent 1da5cfb2ea
commit 7fd53650e3
4 changed files with 500 additions and 42 deletions
@@ -3,12 +3,30 @@ import type { PageData } from './usePDFExport';
import type { CardShape } from '../types';
import { pts2plotter } from '../../../plotcutter';
export interface CardPathData {
points: [number, number][];
centerX: number;
centerY: number;
}
export interface PltExportData {
paths: CardPathData[];
plotterCode: string;
a4Width: number;
a4Height: number;
}
export interface UsePlotterExportReturn {
generatePltData: (pages: PageData[]) => PltExportData | null;
downloadPltFile: (plotterCode: string) => void;
exportToPlt: (pages: PageData[]) => void;
}
/**
* 根据形状生成卡片轮廓点(单位:mm,相对于卡片左下角)
* @param shape 卡片形状
* @param width 卡片宽度(切割尺寸,不含出血)
* @param height 卡片高度(切割尺寸,不含出血)
*/
function getCardShapePoints(
shape: CardShape,
@@ -19,7 +37,6 @@ function getCardShapePoints(
switch (shape) {
case 'circle': {
// 圆形:生成 36 个点近似圆
const radius = Math.min(width, height) / 2;
const centerX = width / 2;
const centerY = height / 2;
@@ -33,14 +50,12 @@ function getCardShapePoints(
break;
}
case 'triangle': {
// 正三角形:顶点向上
points.push([width / 2, 0]);
points.push([0, height]);
points.push([width, height]);
break;
}
case 'hexagon': {
// 六边形:尖顶向上
const halfW = width / 2;
const quarterH = height / 4;
points.push([halfW, 0]);
@@ -53,7 +68,6 @@ function getCardShapePoints(
}
case 'rectangle':
default: {
// 矩形:顺时针方向
points.push([0, 0]);
points.push([width, 0]);
points.push([width, height]);
@@ -65,49 +79,87 @@ function getCardShapePoints(
return points;
}
/**
* 计算多边形的中心点
*/
function calculateCenter(points: [number, number][]): { x: number; y: number } {
let sumX = 0;
let sumY = 0;
for (const [x, y] of points) {
sumX += x;
sumY += y;
}
return {
x: sumX / points.length,
y: sumY / points.length
};
}
/**
* PLT 导出 hook - 生成 HPGL 格式文件并下载
*/
export function usePlotterExport(store: DeckStore): UsePlotterExportReturn {
const exportToPlt = (pages: PageData[]) => {
const cardWidth = store.state.dimensions?.cardWidth || 56;
const cardHeight = store.state.dimensions?.cardHeight || 88;
const shape = store.state.shape;
const bleed = () => store.state.bleed || 1;
const cardWidth = () => store.state.dimensions?.cardWidth || 56;
const cardHeight = () => store.state.dimensions?.cardHeight || 88;
const shape = () => store.state.shape;
const a4Width = 297; // 横向 A4
const a4Height = 210;
// 计算所有页面的总尺寸
const a4Width = 297; // 横向 A4
const a4Height = 210;
/**
* 生成 PLT 数据(不下载,用于预览)
*/
const generatePltData = (pages: PageData[]): PltExportData | null => {
const paths: CardPathData[] = [];
const currentBleed = bleed();
// 收集所有卡片的轮廓点
const allPaths: [number, number][][] = [];
// 计算切割尺寸(排版尺寸减去出血)
const cutWidth = cardWidth() - currentBleed * 2;
const cutHeight = cardHeight() - currentBleed * 2;
for (const page of pages) {
for (const card of page.cards) {
// 只导出正面
if (card.side !== 'front') continue;
// 获取卡片形状点(相对于卡片原点)
const shapePoints = getCardShapePoints(shape, cardWidth, cardHeight);
// 获取卡片形状点(相对于卡片原点,使用切割尺寸
const shapePoints = getCardShapePoints(shape(), cutWidth, cutHeight);
// 转换点到页面坐标Y 轴翻转:SVG Y 向下,plotter Y 向上)
// 转换点到页面坐标
// - X 轴:卡片位置 + 出血偏移
// - Y 轴:翻转(SVG Y 向下,plotter Y 向上)
const pagePoints = shapePoints.map(([x, y]) => [
card.x + x,
a4Height - (card.y + y)
card.x + currentBleed + x,
a4Height - (card.y + currentBleed + y)
] as [number, number]);
allPaths.push(pagePoints);
const center = calculateCenter(pagePoints);
paths.push({
points: pagePoints,
centerX: center.x,
centerY: center.y
});
}
}
if (allPaths.length === 0) {
alert('没有可导出的卡片');
return;
if (paths.length === 0) {
return null;
}
// 生成 HPGL 指令
const allPaths = paths.map(p => p.points);
const plotterCode = pts2plotter(allPaths, a4Width, a4Height, 1);
// 创建 Blob 并下载
return {
paths,
plotterCode,
a4Width,
a4Height
};
};
/**
* 下载 PLT 文件
*/
const downloadPltFile = (plotterCode: string) => {
const blob = new Blob([plotterCode], { type: 'application/vnd.hp-HPGL' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
@@ -119,5 +171,17 @@ export function usePlotterExport(store: DeckStore): UsePlotterExportReturn {
URL.revokeObjectURL(url);
};
return { exportToPlt };
/**
* 直接导出 PLT(兼容旧接口)
*/
const exportToPlt = (pages: PageData[]) => {
const data = generatePltData(pages);
if (!data) {
alert('没有可导出的卡片');
return;
}
downloadPltFile(data.plotterCode);
};
return { generatePltData, downloadPltFile, exportToPlt };
}