diff --git a/src/utils/coordinateTransform.js b/src/utils/coordinateTransform.js index 0ecd56c..4186ed9 100644 --- a/src/utils/coordinateTransform.js +++ b/src/utils/coordinateTransform.js @@ -18,7 +18,7 @@ export class CoordinateTransformer { */ getConfig() { let canvasSize - + this.paperId = localStorage.getItem('paperId') // 优先使用纸张ID if (this.paperId) { try { @@ -26,13 +26,21 @@ export class CoordinateTransformer { const { default: dynamicPaperConfig } = require('./paperConfigDynamic.js') const paper = dynamicPaperConfig.getPaperById(this.paperId) if (paper) { - const pixelSize = dynamicPaperConfig.calculatePixelSize(paper, 203, this.orientation) + const pixelSize = dynamicPaperConfig.calculatePixelSize(paper, 300, this.orientation) canvasSize = { width: pixelSize.width, height: pixelSize.height, name: paper.name, description: paper.description || '' } + console.log(`坐标转换器 - 纸张配置:`, { + paperId: this.paperId, + paperName: paper.name, + orientation: this.orientation, + originalSize: `${paper.widthMm}×${paper.heightMm}mm`, + pixelSize: `${pixelSize.width}×${pixelSize.height}px`, + dpi: 203 + }) } } catch (error) { console.warn('获取动态纸张配置失败,使用降级方案:', error) @@ -50,8 +58,14 @@ export class CoordinateTransformer { name: '自定义', description: `自定义尺寸 (${this.orientation === 'landscape' ? '横向' : '纵向'})` } + console.log(`坐标转换器 - 自定义尺寸:`, canvasSize) } else { canvasSize = getCanvasSize(this.paperType, this.orientation) + console.log(`坐标转换器 - 降级方案:`, { + paperType: this.paperType, + orientation: this.orientation, + canvasSize + }) } } @@ -67,7 +81,7 @@ export class CoordinateTransformer { canvasSize, scaleX: 1, scaleY: 1, - offsetX: Math.max(750, canvasSize.width * 0.8), + offsetX: 0, offsetY: 0 } } @@ -101,16 +115,19 @@ export class CoordinateTransformer { */ toZPL(canvasX, canvasY) { if (this.orientation === 'portrait') { - return { + // 纵向打印:直接映射 + const result = { x: Math.round(canvasX * this.config.scaleX), y: Math.round(canvasY * this.config.scaleY) - } + }; + return result; } else { - // 横向打印需要坐标旋转变换 - return { - x: Math.round(canvasX * this.config.scaleX), - y: Math.round(canvasY * this.config.scaleY) - } + const { width, height } = this.config.canvasSize; + const result = { + x: Math.round(canvasY * this.config.scaleX), + y: Math.round((width - canvasX-100) * this.config.scaleY) + }; + return result; } } @@ -122,15 +139,18 @@ export class CoordinateTransformer { */ toCanvas(zplX, zplY) { if (this.orientation === 'portrait') { + // 纵向打印:直接映射 return { x: Math.round(zplX / this.config.scaleX), y: Math.round(zplY / this.config.scaleY) } } else { // 横向打印的逆变换 + // 与修正后的 toZPL 方法对应的逆转换 + const { width, height } = this.config.canvasSize return { - x: Math.round(this.config.offsetX - (zplY / this.config.scaleY)), - y: Math.round(zplX / this.config.scaleX) + x: Math.round(zplY / this.config.scaleX), + y: Math.round((height - zplX) / this.config.scaleY) } } } diff --git a/src/utils/zplGenerator.js b/src/utils/zplGenerator.js index 64e771d..0045f51 100644 --- a/src/utils/zplGenerator.js +++ b/src/utils/zplGenerator.js @@ -412,9 +412,9 @@ export class ZPLGenerator { */ generateHorizontalLineZPL(element, x, y) { if (this.orientation === 'landscape') { - // 横向打印时需要特殊处理 - const adjustedY = Math.round(1200 - element.x) - element.width - return `^FO${x},${adjustedY}^FWR^GB${element.height},${element.width},3,B^FS` + // 横向打印时,由于使用了^FWB,横线在旋转后变成竖线 + // 使用标准的GB指令,坐标转换已经在coordinateTransform中处理 + return `^FO${x},${y}^GB${element.height},${element.width},3,B^FS` } else { return `^FO${x},${y}^GB${element.width},${element.height},3,B^FS` } @@ -425,9 +425,11 @@ export class ZPLGenerator { */ generateVerticalLineZPL(element, x, y) { if (this.orientation === 'landscape') { - return `^FO${x},${y}^FWR^GB${element.height},${element.width},3,B^FS` + // 横向打印时,由于使用了^FWB,竖线在旋转后变成横线 + // 使用标准的GB指令,坐标转换已经在coordinateTransform中处理 + return `^FO${x},${y}^GB${element.height},${element.width},3,B^FS` } else { - return `^FO${x},${y}^GB1,${element.height},${element.width},B^FS` + return `^FO${x},${y}^GB${element.width},${element.height},3,B^FS` } } diff --git a/src/views/modules/labelSetting/LabelDesigner.vue b/src/views/modules/labelSetting/LabelDesigner.vue index 636e30b..1c8b2a7 100644 --- a/src/views/modules/labelSetting/LabelDesigner.vue +++ b/src/views/modules/labelSetting/LabelDesigner.vue @@ -6,8 +6,13 @@
@@ -408,9 +413,17 @@ export default { console.log('使用默认DPI: 300') } - if (labelSetting.paperOrientation) { + // 处理打印方向,优先使用 prop 传入的值,然后是路由参数 + if (this.orientation && this.orientation !== 'portrait') { + // 如果组件prop已经指定了方向(如从label_draw2.vue传入的landscape),使用它 + console.log('使用组件prop指定的打印方向:', this.orientation) + } else if (labelSetting.paperOrientation) { this.orientation = labelSetting.paperOrientation - console.log('从路由参数设置打印方向:', labelSetting.paperOrientation) + console.log('从路由参数设置打印方向(paperOrientation):', labelSetting.paperOrientation) + } else if (labelSetting.printDirection) { + // 将中文打印方向转换为英文 + this.orientation = (labelSetting.printDirection === '横向打印' || labelSetting.printDirection === '横向设计') ? 'landscape' : 'portrait' + console.log('从路由参数设置打印方向(printDirection):', labelSetting.printDirection, '-> orientation:', this.orientation) } else { // 没有历史打印方向时,确保使用默认值 portrait this.orientation = 'portrait' @@ -729,6 +742,34 @@ export default { // 预览逻辑由PropertyPanel处理 }, + handleClearCanvas() { + if (this.elements.length === 0) { + this.$message.info('画布已经是空的') + return + } + + this.$confirm('确定要清空画布吗?此操作不可恢复。', '清空画布', { + confirmButtonText: '确定清空', + cancelButtonText: '取消', + type: 'warning', + dangerouslyUseHTMLString: true, + message: '