于杰
9 天以前 69a25cd577d5639f2869bcf80f498b373e80137e
修改旋转逻辑,增加辅助旋转功能
2个文件已修改
145 ■■■■■ 已修改文件
north-glass-erp/northglass-erp/src/views/pp/glassOptimize/page/OptimizationRect.vue 141 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
north-glass-erp/src/main/java/com/example/erp/controller/pp/GlassOptimizeController.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
north-glass-erp/northglass-erp/src/views/pp/glassOptimize/page/OptimizationRect.vue
@@ -989,36 +989,153 @@
    }
    adjustGrayRectangles(layoutIndex);
  } else {
    // 旋转失败,恢复原始尺寸
    glassDetail.width = originalState.width;
    glassDetail.height = originalState.height;
    ElMessage.warning('无法旋转,存在重叠或超出边界');
    // 尝试向上移动并旋转
    const upResult = tryMoveAndRotate(layoutIndex, rectIndex, 'up');
    if (!upResult) {
      ElMessage.warning('无法旋转,即使移动后仍存在重叠或超出边界');
    }
  }
};
// 辅助方法:尝试移动并旋转
const tryMoveAndRotate = (layoutIndex, rectIndex, direction) => {
  // 保存原始状态
  const layout = layouts.value[layoutIndex];
  const glassDetail = layout.glassDetails[rectIndex];
  const originalPosition = {
    x: glassDetail.x,
    y: glassDetail.y,
    width: glassDetail.width,
    height: glassDetail.height
  };
  let success = false;
  if (direction === 'up') {
    // 尝试向上移动并旋转
    moveRectAndRotate(layoutIndex, rectIndex, 'down');
    // 检查是否成功
    const otherRects = layout.glassDetails.filter(r => !r.isRemain && r !== glassDetail);
    let hasOverlap = false;
    otherRects.forEach(otherRect => {
      if (checkOverlap(glassDetail, otherRect)) {
        hasOverlap = true;
      }
    });
    if (!hasOverlap &&
        glassDetail.x + glassDetail.width <= layout.width &&
        glassDetail.y + glassDetail.height <= layout.height &&
        glassDetail.x >= 0 && glassDetail.y >= 0) {
      success = true;
      // 如果向上移动并旋转成功,则再向下移动,让矩形靠近底部
      moveRect(layoutIndex, rectIndex, 'up');
    } else {
      // 向上移动并旋转失败,恢复原始状态
      glassDetail.x = originalPosition.x;
      glassDetail.y = originalPosition.y;
      glassDetail.width = originalPosition.width;
      glassDetail.height = originalPosition.height;
    }
  } else if (direction === 'down') {
    // 尝试向下移动并旋转
    moveRectAndRotate(layoutIndex, rectIndex, 'up');
    // 检查是否成功
    const otherRects = layout.glassDetails.filter(r => !r.isRemain && r !== glassDetail);
    let hasOverlap = false;
    otherRects.forEach(otherRect => {
      if (checkOverlap(glassDetail, otherRect)) {
        hasOverlap = true;
      }
    });
    if (!hasOverlap &&
        glassDetail.x + glassDetail.width <= layout.width &&
        glassDetail.y + glassDetail.height <= layout.height &&
        glassDetail.x >= 0 && glassDetail.y >= 0) {
      success = true;
      // 如果向下移动并旋转成功,则再向上移动,让矩形靠近顶部
      moveRect(layoutIndex, rectIndex, 'down');
    } else {
      // 向下移动并旋转失败,恢复原始状态
      glassDetail.x = originalPosition.x;
      glassDetail.y = originalPosition.y;
      glassDetail.width = originalPosition.width;
      glassDetail.height = originalPosition.height;
    }
  }
  return success;
};
//移动旋转方法
const moveRectAndRotate = (layoutIndex, rectIndex, direction) => {
  const layout = layouts.value[layoutIndex];
  const glassDetail = layout.glassDetails[rectIndex];
  const grayRects = layout.glassDetails.filter(r => r.isRemain);
  // 先移动
  moveRect(layoutIndex, rectIndex, direction);
  // 再旋转
  const originalState = { ...glassDetail };
  const temp = glassDetail.width;
  glassDetail.width = glassDetail.height;
  glassDetail.height = temp;
  const canPlace = grayRects.some(grayRect => {
    return grayRect.width >= glassDetail.width && grayRect.height >= glassDetail.height;
  const otherRects = layout.glassDetails.filter(r => !r.isRemain && r !== glassDetail);
  let isValidRotation = true;
  otherRects.forEach(otherRect => {
    if (checkOverlap(glassDetail, otherRect)) {
      isValidRotation = false;
    }
  });
  if (!canPlace) {
    const temp = glassDetail.width;
    glassDetail.width = glassDetail.height;
    glassDetail.height = temp;
    ElMessage.warning('无法旋转,没有足够的空间');
    return;
  if (glassDetail.x + glassDetail.width > layout.width || glassDetail.y + glassDetail.height > layout.height) {
    isValidRotation = false;
  }
  adjustGrayRectangles(layoutIndex);
  moveRect(layoutIndex, rectIndex, direction);
  if (isValidRotation) {
    // 更新glassPoint坐标(如果存在)
    if (glassDetail.glassPoint && Array.isArray(glassDetail.glassPoint)) {
      const originalPoints = JSON.parse(JSON.stringify(glassDetail.glassPoint));
      glassDetail.glassPoint.forEach((point, index) => {
        const relX = originalPoints[index].X - originalState.x;
        const relY = originalPoints[index].Y - originalState.y;
        point.X = originalState.x + relY;
        point.Y = originalState.y + (originalState.width - relX);
        point.X = parseFloat(point.X.toFixed(2));
        point.Y = parseFloat(point.Y.toFixed(2));
      });
    }
    adjustGrayRectangles(layoutIndex);
  } else {
    // 旋转失败,恢复原始尺寸和位置
    glassDetail.width = originalState.width;
    glassDetail.height = originalState.height;
    // 恢复移动前的位置
    moveRect(layoutIndex, rectIndex, getReverseDirection(direction));
    ElMessage.warning('无法旋转,存在重叠或超出边界');
  }
};
// 获取反向方向的辅助函数
const getReverseDirection = (direction) => {
  switch (direction) {
    case 'up': return 'down';
    case 'down': return 'up';
    case 'left': return 'right';
    case 'right': return 'left';
    default: return direction;
  }
};
//移动方法
north-glass-erp/src/main/java/com/example/erp/controller/pp/GlassOptimizeController.java
@@ -228,7 +228,7 @@
    }
    @PostMapping("/calculate")
    @PostMapping("/mesCalculate")
    public ResponseEntity<Map<String, Object>> receiveOptimizeRequest(
            @RequestBody Map<String, Object> requestData) {
@@ -240,7 +240,7 @@
            response.put("msg", "success");
            response.put("data", "");
            // 异步处理计算任务
            // todo 异步处理计算任务
//            glassOptimizeService.processExternalOptimizeRequest(requestData);
            System.out.println(requestData);