Skip to content

Commit

Permalink
add turning for closed contours
Browse files Browse the repository at this point in the history
  • Loading branch information
staniska committed Dec 26, 2023
1 parent 936c81a commit b4f4036
Showing 1 changed file with 49 additions and 22 deletions.
71 changes: 49 additions & 22 deletions lib/contourEdit/tools/turning.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,27 @@ export const createTurning = () => {
if (processingData.startPoint[y] < bottom) create_btn('vertical', 1)

if (View.sinumerikView.modalWindow.directions.children.length < 2) {

while (View.sinumerikView.modalWindow.directions.children.length) {
View.sinumerikView.modalWindow.directions.removeChild(View.sinumerikView.modalWindow.directions.lastChild)
}
View.sinumerikView.modalWindow.directions.innerText = 'Directions can\'t be determined for start point'
if (confirmDialog('Directions can\'t be determined for open corner turning. Create closed contour turing cycle?')) {
closedContourTurning = true
if (processingData.startPoint[x] > left && processingData.startPoint[x] < right) {

create_btn('horizontal', -1)
create_btn('horizontal', 1)
}
if (processingData.startPoint[y] > bottom && processingData.startPoint[y] < top) {
create_btn('vertical', -1)
create_btn('vertical', 1)
}
} else {
return
}
}
View.sinumerikView.modalWindow.cycleName = create_element(['contourEdit_modalWindow_cycleName'],View.sinumerikView.modalWindow)
View.sinumerikView.modalWindow.cycleName = create_element(['contourEdit_modalWindow_cycleName'], View.sinumerikView.modalWindow)
View.sinumerikView.modalWindow.cycleName.text = create_element(['contourEdit_modalWindow_cycleName_header'], View.sinumerikView.modalWindow.cycleName, 'cycleName:')
View.sinumerikView.modalWindow.cycleName.input = create_element(['native-key-bindings'],View.sinumerikView.modalWindow.cycleName, '', 'input')
View.sinumerikView.modalWindow.cycleName.input = create_element(['native-key-bindings'], View.sinumerikView.modalWindow.cycleName, '', 'input')
View.sinumerikView.modalWindow.cycleName.input.value = 'JOPA'


Expand All @@ -105,7 +117,7 @@ export const createTurning = () => {
}

detectProcessingPoints(burnedContourRange, direction, ax, contour)
const programText = generateProgramText(direction, ax, burnedContourRange, contour, x, y, View.sinumerikView.modalWindow.cycleName.input.value)
const programText = generateProgramText(direction, ax, burnedContourRange, contour, x, y, View.sinumerikView.modalWindow.cycleName.input.value, closedContourTurning)

const Editor = atom.workspace.getActiveTextEditor()
View.sinumerikView.contourEditData.inserter = Editor.onDidChangeCursorPosition(() => {
Expand Down Expand Up @@ -209,10 +221,10 @@ const detectProcessingPoints = (cRange, direction, ax, contour) => {
pts[2] !== undefined
) {
let i = 1
while (pts[i+1] !== undefined && getDistance(pts[i].coords, pts[i+1].coords) < PRECISION) {
i ++
while (pts[i + 1] !== undefined && getDistance(pts[i].coords, pts[i + 1].coords) < PRECISION) {
i++
}
endPoints.push(pts[i+1])
endPoints.push(pts[i + 1])
} else {
endPoints.push(pts[1])
}
Expand Down Expand Up @@ -295,7 +307,7 @@ const findChangePoints = (points, direction, contour, ax, x, y, cRange) => {
return changePoints
}

const generateProgramText = (direction, ax, cRange, contour, x, y, cycleName) => {
const generateProgramText = (direction, ax, cRange, contour, x, y, cycleName, closedContour) => {
// console.log('generate program text')
// console.log(direction.direction, typeof direction.direction)
const {diamonAx} = getFrame()
Expand All @@ -313,8 +325,7 @@ const generateProgramText = (direction, ax, cRange, contour, x, y, cycleName) =>
const points = View.sinumerikView.contourEditData.points
const contourName = View.sinumerikView.contourEditRightContainer.querySelector('.contourEditInput').value
const programText = ['\n']
programText.push(`;---- generated cycle ${contourName.length ? (' from contour ' + contourName): ''} -----`)

programText.push(`;---- generated cycle ${contourName.length ? (' from contour ' + contourName) : ''} -----`)

let values

Expand All @@ -331,7 +342,7 @@ const generateProgramText = (direction, ax, cRange, contour, x, y, cycleName) =>
programText.push(`R${targetVarNum}=${(values[1] * (diamonAx === ax2 ? 2 : 1)).toFixed(3)} ; end`)
programText.push(`R${depthVarNum}=${depthValue} ; depth`)
programText.push(`R${safetyVarNum}=1 ;safety distance`)
programText.push(`R${averageDepthVarNum}=ABS(R${startVarNum}-R${targetVarNum})/(TRUNC(ABS((R${startVarNum}-R${targetVarNum})${diamonAx === ax2 ? '/2': ''})/(R${depthVarNum}*1.03))+1)-1/10000`)
programText.push(`R${averageDepthVarNum}=ABS(R${startVarNum}-R${targetVarNum})/(TRUNC(ABS((R${startVarNum}-R${targetVarNum})${diamonAx === ax2 ? '/2' : ''})/(R${depthVarNum}*1.03))+1)-1/10000`)
programText.push('')
programText.push(`R${startVarNum}=R${startVarNum}${reverse ? '-' : '+'}R${averageDepthVarNum}`)
programText.push('')
Expand All @@ -344,23 +355,38 @@ const generateProgramText = (direction, ax, cRange, contour, x, y, cycleName) =>

let PArr = JSON.parse(JSON.stringify(points.processingChangeStart))
if (reverse) PArr = PArr.reverse()
let insert_1 = closedContour ? `${direction.direction === 1 ? '-' : '+'}(R${averageDepthVarNum}+R${safetyVarNum}*2)` : ''
let insert_2 = closedContour ? `${direction.direction === 1 ? '-' : '+'}R${averageDepthVarNum}+R${safetyVarNum}` : ''

if (PArr.length > 2) {
for (let i = 0; i < PArr.length - 2; i++) {
if (Math.min(...values.map(value => Math.abs(value - PArr[i+1].coords[ax2]))) < 1e-2) {
if (Math.min(...values.map(value => Math.abs(value - PArr[i + 1].coords[ax2]))) < 1e-2) {
continue
}
programText.push(`IF R${startVarNum}${reverse ? '>' : '<'}${(PArr[i + 1].coords[ax2] * (diamonAx === ax2 ? 2 : 1)).toFixed(3)}`)
programText.push(` G0 ${ax2}=R${startVarNum} ` + getEquationByAx(

programText.push(` G0 ${ax2}=R${startVarNum}${insert_1} ` + getEquationByAx(
getElBetweenPoints(PArr, points.processingStart, i),
ax, ax2, PArr[i + 1], startVarNum, reverse) + `${(direction.direction === '1')? '-': '+'}R${safetyVarNum}${diamonAx === ax ? '*2': ''}`)
ax, ax2, PArr[i + 1], startVarNum, reverse, closedContour, direction, averageDepthVarNum, safetyVarNum) + (closedContour ? `${(direction.direction === '1') ? '-' : '+'}R${safetyVarNum}${diamonAx === ax ? '*2' : ''}` : ''))
if (closedContour) {
programText.push(` G1 ${ax2}=R${startVarNum}${insert_2}`)
programText.push(` G1 ${ax2}=R${startVarNum} ` + getEquationByAx(
getElBetweenPoints(PArr, points.processingStart, i),
ax, ax2, PArr[i + 1], startVarNum, reverse) + (closedContour ? `${(direction.direction === '1') ? '-' : '+'}R${safetyVarNum}${diamonAx === ax ? '*2' : ''}` : ''))
}
programText.push(` GOTOF ${cycleName}_START`)
programText.push(`ENDIF`)
}
}
programText.push(`G0 ${ax2}=R${startVarNum} ` + getEquationByAx(
programText.push(`G0 ${ax2}=R${startVarNum}${insert_1} ` + getEquationByAx(
getElBetweenPoints(PArr, points.processingStart, PArr.length - 2),
ax, ax2, PArr[PArr.length - 1], startVarNum, reverse) + `${(direction.direction === '1')? '-': '+'}R${safetyVarNum}${diamonAx === ax ? '*2': ''}`)
ax, ax2, PArr[PArr.length - 1], startVarNum, reverse, closedContour, direction, averageDepthVarNum, safetyVarNum) + (closedContour ? '' : `${(direction.direction === '1') ? '-' : '+'}R${safetyVarNum}${diamonAx === ax ? '*2' : ''}`))
if (closedContour) {
programText.push(` G1 ${ax2}=R${startVarNum}${insert_2}`)
programText.push(` G1 ${ax2}=R${startVarNum} ` + getEquationByAx(
getElBetweenPoints(PArr, points.processingStart, PArr.length - 2),
ax, ax2, PArr[PArr.length - 1], startVarNum, reverse) + (closedContour ? '' : `${(direction.direction === '1') ? '-' : '+'}R${safetyVarNum}${diamonAx === ax ? '*2' : ''}`))
}

programText.push(`${cycleName}_START:`)

Expand All @@ -372,7 +398,7 @@ const generateProgramText = (direction, ax, cRange, contour, x, y, cycleName) =>

if (PArr.length > 2) {
for (let i = 0; i < PArr.length - 2; i++) {
if (Math.min(...values.map(value => Math.abs(value - PArr[i+1].coords[ax2]))) < 1e-2) {
if (Math.min(...values.map(value => Math.abs(value - PArr[i + 1].coords[ax2]))) < 1e-2) {
continue
}
programText.push(`IF R${startVarNum}${reverse ? '>' : '<'}${(PArr[i + 1].coords[ax2] * (diamonAx === ax2 ? 2 : 1)).toFixed(3)}`)
Expand All @@ -394,7 +420,7 @@ const generateProgramText = (direction, ax, cRange, contour, x, y, cycleName) =>
programText.push(` ;--- last cycle pass ---`)
programText.push(`ENDIF`)

programText.push(`G1 ${ax}=IC(${direction.direction === '1' ? '-':''}R${safetyVarNum}) ${ax2}=IC(${reverse ? '':'-'}R${safetyVarNum})`)
programText.push(`G1 ${ax}=IC(${direction.direction === '1' ? '-' : ''}R${safetyVarNum}) ${ax2}=IC(${reverse ? '' : '-'}R${safetyVarNum})`)
programText.push(`G0 ${x}=${processing.startPoint[x].toFixed(3)} ${y}=${processing.startPoint[y] * (diamonAx === y ? 2 : 1).toFixed(3)}`)

programText.push(`IF R${startVarNum}${reverse ? '>=' : '<='}R${targetVarNum} GOTOB ${cycleName}`)
Expand Down Expand Up @@ -460,7 +486,6 @@ const filterProgramText = (programText) => {
})



programText = (programText.filter((str, idx) => !stringsForDelete.includes(idx)))

return programText
Expand All @@ -475,16 +500,18 @@ const getElBetweenPoints = (pointsArr, allPointsArr, id1, id2) => {
].parentId)
}

const getEquationByAx = (el, ax, ax2, p, rNum, reverse) => {
const getEquationByAx = (el, ax, ax2, p, rNum, reverse, closedContour, direction, averageDepthVarNum, safetyVarNum) => {
const {diamonAx, x, y} = getFrame()
if (el.type === 'line') {
const {a, b} = getEquationOfLine(el)
let coords = p.coords
let insert_1 = closedContour ? `${direction.direction === 1 ? '-' : '+'}(R${averageDepthVarNum}+R${safetyVarNum})` : ''
// if (p.break) {
// // console.log('jopa!!!')
// coords = p.secondP.coords
// }
return `${ax}=${(coords[ax] * (diamonAx === ax ? 2 : 1)).toFixed(3)}+(R${rNum}-${(coords[ax2] * (diamonAx === ax2 ? 2 : 1)).toFixed(3)})${diamonAx === ax2 ? '/2' : ''}${(ax === x ? '/' : '*')}TAN(${(-Math.atan2(a / b, 1) / Math.PI * 180).toFixed(3)})${diamonAx === ax ? '*2' : ''}`

return `${ax}=${(coords[ax] * (diamonAx === ax ? 2 : 1)).toFixed(3)}+(R${rNum}${insert_1}-${(coords[ax2] * (diamonAx === ax2 ? 2 : 1)).toFixed(3)})${diamonAx === ax2 ? '/2' : ''}${(ax === x ? '/' : '*')}TAN(${(-Math.atan2(a / b, 1) / Math.PI * 180).toFixed(3)})${diamonAx === ax ? '*2' : ''}`
}
if (el.type === 'arc') {

Expand Down

0 comments on commit b4f4036

Please sign in to comment.