Files
cut-abstractions/tests/dev1/dataHandle/common/LayoutEngine/DisposeModelInBoardBorder.ts
2025-07-22 18:22:31 +08:00

322 lines
8.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { BlockSizePlus } from './BlockSizePlus.js'
import { BlockHelper } from './BlockHelper.js'
import { BlockModel,PlaceBlock,PlaceBoard,PlaceMaterial } from '../../confClass.js'
/**
* 大板长边的两侧多少范围内 避免造型 ,如果出现,则那片小板掉头放置
* @param pm
* @param 大板边缘范围
* @face 大板面 反面 1 正面 0 ;双面 2
* @forCNC 针对的是cnc数据
* @isReverse 反转: 大板边缘尽量 安排造型
*/
export function DisPoseModelInBoardBorder(pm: PlaceMaterial, width: number, face: number, forCNC: number, isReverse: boolean)
{
if (width == 0)
return
for (const pb of pm.boardList)
{
if (pb.isAdnormal())
continue
if (pb.isLocked || pb.cutedType != 0)
continue
if (isReverse == false)
{
// 避免大板边出现 造型
DisPoseModelInBoardBorder_pb(pb, width, face, forCNC)
}
else
{
// 尽量 安排 造型在大板边
needModelInBoardBorder_pb(pb, width, face, forCNC)
}
}
}
/**
* 大板边缘 避免造型 如果出现,则那片小板掉头放置
*
* @param pb
* @param width
* @param face
* @param forCNC 针对的是cnc的加工
*/
function DisPoseModelInBoardBorder_pb(pb: PlaceBoard, width: number, face: number, forCNC: number)
{
const bw = pb.width
for (const block of pb.blockList)
{
if (block.isUnRegular)
continue
if (block.placeX + block.placeWidth < width)
continue // 小板整片板都在边缘, 转不转没意义
if (block.placeX > bw - width)
continue
// 如果内部有小板, 也不能掉头
if (hasChildBlock(block, pb.blockList))
continue
let isInBorder = false
const models = getModels(block, face, forCNC)
for (const model of models)
{
isInBorder = isInBoardBorder(block, model, bw, width)
if (isInBorder)
break
}
if (isInBorder == false)
continue
turnBlock(block)
}
}
/**
* 大板边缘 尽量安排造型
*
* @param pb
* @param width
* @param face
* @param forCNC
*/
function needModelInBoardBorder_pb(pb: PlaceBoard, width: number, face: number, forCNC: number)
{
const blocks_turn: any = []
const bw = pb.width
// 大板左边
const blocks_z = pb.blockList.filter(t => t.isUnRegular == false && t.placeX < width)
for (const block of blocks_z)
{
// 如果内部有小板, 也不能掉头
if (hasChildBlock(block, pb.blockList))
continue
// 造型
const models = getModels(block, face, forCNC)
let hasModelOnLeft = false // 左边有造型
let hasModelOnRight = false // 右边有造型
for (const model of models)
{
if (hasModelOnLeft == false && isInBoardLeft(block, model, width, false))
hasModelOnLeft = true
if (hasModelOnRight == false && isInBoardLeft(block, model, width, true))
hasModelOnRight = true
}
// 左边没有造型, 右边有造型,则掉头
if (hasModelOnLeft == false && hasModelOnRight)
{
blocks_turn.push(block)
}
}
// 大板右边
const blocks_y = pb.blockList.filter(t => t.isUnRegular == false && t.placeX + t.placeWidth > bw - width)
for (const block of blocks_y)
{
// 如果内部有小板, 也不能掉头
if (hasChildBlock(block, pb.blockList))
continue
// 造型
const models = getModels(block, face, forCNC)
let hasModelOnLeft = false // 小板左边有造型
let hasModelOnRight = false // 小板右边有造型
for (const model of models)
{
if (hasModelOnLeft == false && isInBoardRight(block, model, bw, width, true))
hasModelOnLeft = true
if (hasModelOnRight == false && isInBoardRight(block, model, bw, width, false))
hasModelOnRight = true
}
// 右边没有造型, 左边有造型,则掉头
if (hasModelOnLeft && hasModelOnRight == false)
{
blocks_turn.push(block)
}
}
// 翻转小板
for (const block of blocks_turn)
{
turnBlock(block)
}
}
/** 获取小板指定面造型 */
function getModels(block: PlaceBlock, face: number, forCNC: number): BlockModel[]
{
let models: any = []
if (face == 0) // 正面
{
models = block.modelListFaceA
if (forCNC == 1)
{
const ms_A = block.isTurnOver() == false ? block.modelListOrgFaceA() : block.modelListOrgFaceB()
models = ms_A.filter(t => t.isCutting == false).concat(block.modelListOrgTrough().filter(t => t.isCutting == false))
}
}
else if (face == 1) // 反面
{
models = block.modelListFaceB
if (forCNC == 1)
{
const ms_B = block.isTurnOver() == false ? block.modelListOrgFaceB() : block.modelListOrgFaceA()
models = ms_B.filter(t => t.isCutting == false)
}
}
else // 随意面
{
models = block.models().filter(t => t.isCutting != (forCNC == 1))
}
return models
}
/** 判断是否有造型出现在无法加工的区域 */
export function hasModelInBoardBorder(pb: PlaceBoard, width: number, face: number, forCNC: number)
{
pb.hasModelOnLeft = false
pb.hasModelOnRight = false
const bw = pb.width
for (const block of pb.blockList)
{
if (block.isUnRegular)
continue
if (block.placeX + block.placeWidth < width)
continue // 小板整片板都在边缘, 转不转没意义
if (block.placeX > bw - width)
continue
const models = getModels(block, face, forCNC)
if (pb.hasModelOnLeft == false)
{
for (const model of models)
{
let isLeft = false
for (const p of model.pointList)
{
const rp = BlockHelper.getPlaceXYInBoard(block, p.pointX, p.pointY)
if (rp.x < width)
{
isLeft = true
break
}
}
if (isLeft)
{
pb.hasModelOnLeft = true
break
}
}
}
if (pb.hasModelOnRight == false)
{
for (const model of models)
{
let isRight = false
for (const p of model.pointList)
{
const rp = BlockHelper.getPlaceXYInBoard(block, p.pointX, p.pointY)
if (rp.x > bw - width)
{
isRight = true
break
}
}
if (isRight)
{
pb.hasModelOnRight = true
break
}
}
}
if (pb.hasModelOnLeft && pb.hasModelOnRight)
return // 已经有结果了
}
}
/** 判断造型是否在大板的边缘 */
function isInBoardBorder(block: PlaceBlock, model: BlockModel, bw: number, width: number): boolean
{
for (const p of model.pointList)
{
const rp = BlockHelper.getPlaceXYInBoard(block, p.pointX, p.pointY)
if (rp.x < width || rp.x > bw - width)
return true
}
return false
}
/** 造型是否在大板左边 toTurn 翻转后 */
function isInBoardLeft(block: PlaceBlock, model: BlockModel, width: number, toTurn: boolean)
{
for (const p of model.pointList)
{
const rp = BlockHelper.getPlaceXYInBlock(block, p.pointX, p.pointY, false, false)
let rx = toTurn ? block.placeWidth - rp.x : rp.x
rx = rx + block.placeX
if (rx < width)
return true
}
return false
}
/** 造型 是否在大板 右边 toTurn 翻转后 */
function isInBoardRight(block: PlaceBlock, model: BlockModel, boardWidth: number, width: number, toTurn: boolean)
{
for (const p of model.pointList)
{
const rp = BlockHelper.getPlaceXYInBlock(block, p.pointX, p.pointY, false, false)
let rx = toTurn ? block.placeWidth - rp.x : rp.x
rx = rx + block.placeX
if (rx > boardWidth - width)
return true
}
return false
}
/** 判断block里有没有小板 */
function hasChildBlock(block: PlaceBlock, blocks: PlaceBlock[]): boolean
{
for (const b of blocks)
{
if (b.blockNo == block.blockNo)
continue
if (b.placeX > block.placeX && b.placeX + b.placeWidth < block.placeX + block.placeWidth && b.placeY > block.placeY && b.placeY + b.placeLength < block.placeY + block.placeLength)
return true
}
return false
}
function turnBlock(block: PlaceBlock)
{
// 有造型在大板边
const orgStyle = block.placeStyle
const newStyle = BlockHelper.getTurnedPlaceStyle(orgStyle, 2)
const orgPlaceX = block.placeX - block.placeOffX
const orgPlaceY = block.placeY - block.placeOffY
const offset = BlockSizePlus.getOffDis(block, newStyle)
// 左右上下,外扩尺寸不一样, 翻转有可能会导致 小板与其他小板 干涉。
if (offset.left != offset.right || offset.top != offset.bottom)
return
const newPlaceX = orgPlaceX + offset.x
const newPlaceY = orgPlaceY + offset.y
block.placeX = newPlaceX
block.placeY = newPlaceY
block.placeOffX = offset.x
block.placeOffY = offset.y
// 修改小板的放置方式
BlockHelper.resetPlaceStyle(block, newStyle)
}