refactor: move code to plotcutter

This commit is contained in:
2026-03-15 09:29:18 +08:00
parent 025f5a46b0
commit 28794fd9f0
8 changed files with 737 additions and 314 deletions
+45 -114
View File
@@ -1,69 +1,30 @@
import type { DeckStore } from './deckStore';
import type { PageData } from './usePDFExport';
import type { CardShape } from '../types';
import {
getCardShapePoints,
calculateCenter
} from '../../../plotcutter/contour';
import { pts2plotter } from '../../../plotcutter/plotter';
export interface CardPathData {
points: [number, number][];
centerX: number;
centerY: number;
startPoint: [number, number];
endPoint: [number, number];
}
import { calculateSinglePageLayout, generateTravelPaths, pts2plotter } from '../../../plotcutter';
export interface PltExportData {
paths: CardPathData[];
travelPaths: [number, number][][];
plotterCode: string;
/** 单页满排时的 PLT 代码 */
pltCode: string;
/** A4 宽度 (mm) */
a4Width: number;
/** A4 高度 (mm) */
a4Height: number;
/** 每页卡片数 */
cardsPerPage: number;
}
export interface UsePlotterExportReturn {
generatePltData: (pages: PageData[]) => PltExportData | null;
downloadPltFile: (plotterCode: string) => void;
exportToPlt: (pages: PageData[]) => void;
/** 生成单页满排时的 PLT 数据 */
generatePltData: () => PltExportData | null;
/** 下载 PLT 文件 */
downloadPltFile: (pltCode: string) => void;
/** 直接导出 PLT(打开下载) */
exportToPlt: () => void;
}
/**
* 生成空走路径(抬刀移动路径)
* 从左上角出发,连接所有卡片的起点/终点,最后返回左上角
*/
function generateTravelPaths(
cardPaths: CardPathData[],
a4Height: number
): [number, number][][] {
const travelPaths: [number, number][][] = [];
// 起点:左上角 (0, a4Height) - 注意 SVG 坐标 Y 向下,plotter 坐标 Y 向上
const startPoint: [number, number] = [0, a4Height];
if (cardPaths.length === 0) {
return travelPaths;
}
// 从起点到第一张卡的起点
travelPaths.push([startPoint, cardPaths[0].startPoint]);
// 卡片之间的移动
for (let i = 0; i < cardPaths.length - 1; i++) {
const currentEnd = cardPaths[i].endPoint;
const nextStart = cardPaths[i + 1].startPoint;
travelPaths.push([currentEnd, nextStart]);
}
// 从最后一张卡返回起点
travelPaths.push([cardPaths[cardPaths.length - 1].endPoint, startPoint]);
return travelPaths;
}
/**
* PLT 导出 hook - 生成 HPGL 格式文件并下载
* PLT 导出 hook - 生成单页满排时的 HPGL 格式文件
*
* 刀路只关心单页排满的情况,不考虑实际牌组的张数。
*/
export function usePlotterExport(store: DeckStore): UsePlotterExportReturn {
const bleed = () => store.state.bleed || 1;
@@ -71,81 +32,51 @@ export function usePlotterExport(store: DeckStore): UsePlotterExportReturn {
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 orientation = () => store.state.printOrientation || 'landscape';
/**
* 生成 PLT 数据(不下载,用于预览)
* 生成单页满排时的 PLT 数据
*/
const generatePltData = (pages: PageData[]): PltExportData | null => {
const paths: CardPathData[] = [];
const currentBleed = bleed();
const currentCornerRadius = cornerRadius();
const generatePltData = (): PltExportData | null => {
const layout = calculateSinglePageLayout({
cardWidth: cardWidth(),
cardHeight: cardHeight(),
shape: shape(),
bleed: bleed(),
cornerRadius: cornerRadius(),
orientation: orientation()
});
// 计算切割尺寸(排版尺寸减去出血)
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(), cutWidth, cutHeight, currentCornerRadius);
// 转换点到页面坐标:
// - X 轴:卡片位置 + 出血偏移
// - Y 轴:翻转(SVG Y 向下,plotter Y 向上)
const pagePoints = shapePoints.map(([x, y]) => [
card.x + currentBleed + x,
a4Height - (card.y + currentBleed + y)
] as [number, number]);
const center = calculateCenter(pagePoints);
const startPoint = pagePoints[0];
const endPoint = pagePoints[pagePoints.length - 1];
paths.push({
points: pagePoints,
centerX: center.x,
centerY: center.y,
startPoint,
endPoint
});
}
}
if (paths.length === 0) {
if (layout.cardPaths.length === 0) {
return null;
}
// 生成空走路径
const travelPaths = generateTravelPaths(paths, a4Height);
const travelPaths = generateTravelPaths(layout.cardPaths, layout.a4Height);
// 生成 HPGL 代码(包含空走路径,从左上角出发并返回)
const allPaths = paths.map(p => p.points);
const startPoint: [number, number] = [0, a4Height]; // 左上角
const endPoint: [number, number] = [0, a4Height]; // 返回左上角
const plotterCode = pts2plotter(allPaths, a4Width, a4Height, 1, startPoint, endPoint);
const allPaths = layout.cardPaths.map(p => p.points);
const startPoint: [number, number] = [0, layout.a4Height];
const endPoint: [number, number] = [0, layout.a4Height];
const plotterCode = pts2plotter(allPaths, layout.a4Width, layout.a4Height, 1, startPoint, endPoint);
return {
paths,
travelPaths,
plotterCode,
a4Width,
a4Height
pltCode: plotterCode,
a4Width: layout.a4Width,
a4Height: layout.a4Height,
cardsPerPage: layout.cardsPerPage
};
};
/**
* 下载 PLT 文件
*/
const downloadPltFile = (plotterCode: string) => {
const blob = new Blob([plotterCode], { type: 'application/vnd.hp-HPGL' });
const downloadPltFile = (pltCode: string) => {
const blob = new Blob([pltCode], { type: 'application/vnd.hp-HPGL' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = `deck-export-${new Date().toISOString().slice(0, 19).replace(/:/g, '-')}.plt`;
link.download = `deck-plt-${new Date().toISOString().slice(0, 19).replace(/:/g, '-')}.plt`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
@@ -153,15 +84,15 @@ export function usePlotterExport(store: DeckStore): UsePlotterExportReturn {
};
/**
* 直接导出 PLT兼容旧接口
* 直接导出 PLT打开下载
*/
const exportToPlt = (pages: PageData[]) => {
const data = generatePltData(pages);
const exportToPlt = () => {
const data = generatePltData();
if (!data) {
alert('没有可导出的卡片');
return;
}
downloadPltFile(data.plotterCode);
downloadPltFile(data.pltCode);
};
return { generatePltData, downloadPltFile, exportToPlt };