I want to achieve the same functionality as SolidWorks’ chamfer with angle (distance-angle), but it’s not working correctly. Can someone help me?
here is results of my code
function drawChamferLineWithDegree(angle, distance) {
const store = window.myApp._context.provides.store;
const sketchBody = store.getters.getSelectedSketches[0];
const selectedObjects = store.getters.getSelectedObjects;
const data = {};
data.intersectPoint = calculateIntersection(selectedObjects);
const firstClick = selectedObjects[0].userData.selectCoordinates;
const secondClick = selectedObjects[1].userData.selectCoordinates;
const intersectPoint = calculateIntersection(selectedObjects);
const firstDirection = firstClick.clone().sub(intersectPoint).normalize();
const secondDirection = secondClick.clone().sub(intersectPoint).normalize();
const clicksNormal = [firstClick.clone().sub(intersectPoint).normalize(), secondClick.clone().sub(intersectPoint).normalize()];
const isClockwise = clicksNormal[0].dot(clicksNormal[1]).z < 0;
const angleInRadians = MathUtils.degToRad(isClockwise ? angle : -angle);
const rotatedFirstDirection = new Vector3(
firstDirection.x * Math.cos(angleInRadians) - firstDirection.y * Math.sin(angleInRadians),
firstDirection.x * Math.sin(angleInRadians) + firstDirection.y * Math.cos(angleInRadians),
0
).normalize();
const rotatedSecondDirection = new Vector3(
secondDirection.x * Math.cos(angleInRadians) - secondDirection.y * Math.sin(angleInRadians),
secondDirection.x * Math.sin(angleInRadians) + secondDirection.y * Math.cos(angleInRadians),
0
).normalize();
const startPoint = intersectPoint.clone().add(rotatedFirstDirection.multiplyScalar(distance));
const endPoint = intersectPoint.clone().add(rotatedSecondDirection.multiplyScalar(distance));
const chamferLine = constructorLine([startPoint, endPoint]);
chamferLine.material.color.set(0xff0000);
chamferLine.userData.distance = distance;
chamferLine.userData.intersectPoint = data.intersectPoint;
chamferLine.userData.directions = { firstDirection, secondDirection };
chamferLine.userData.localPoints = [startPoint, endPoint];
chamferLine.userData.points = [startPoint, endPoint];
store.commit(‘setSelectedObjects’, […selectedObjects, chamferLine]);
sketchBody.add(chamferLine);
}
function updateChamferGroupWithDegree(selectedLines, chamferLine, angle, distance) {
const intersectPoint = chamferLine.userData.intersectPoint.clone();
const { firstDirection, secondDirection } = chamferLine.userData;
const angleInRadians = MathUtils.degToRad(angle);
const rotatedFirstDirection = new Vector3(
firstDirection.x * Math.cos(angleInRadians) - firstDirection.y * Math.sin(angleInRadians),
firstDirection.x * Math.sin(angleInRadians) + firstDirection.y * Math.cos(angleInRadians),
0
).normalize();
const rotatedSecondDirection = new Vector3(
secondDirection.x * Math.cos(angleInRadians) - secondDirection.y * Math.sin(angleInRadians),
secondDirection.x * Math.sin(angleInRadians) + secondDirection.y * Math.cos(angleInRadians),
0
).normalize();
const firstChamferPoint = intersectPoint.clone().add(rotatedFirstDirection.multiplyScalar(distance));
const secondChamferPoint = intersectPoint.clone().add(rotatedSecondDirection.multiplyScalar(distance));
chamferLine.geometry.setFromPoints([firstChamferPoint, secondChamferPoint]);
const firstLineIndex = selectedLines.value[0].userData.dinamic.i;
const secondLineIndex = selectedLines.value[1].userData.dinamic.i;
selectedLines.value[0].userData.dinamic.points[firstLineIndex].copy(firstChamferPoint);
selectedLines.value[1].userData.dinamic.points[secondLineIndex].copy(secondChamferPoint);
selectedLines.value[0].geometry.setFromPoints(selectedLines.value[0].userData.dinamic.points);
selectedLines.value[1].geometry.setFromPoints(selectedLines.value[1].userData.dinamic.points);
if (selectedLines.value[0].children[firstLineIndex]) {
selectedLines.value[0].children[firstLineIndex].geometry.setFromPoints([firstChamferPoint]);
}
if (selectedLines.value[1].children[secondLineIndex]) {
selectedLines.value[1].children[secondLineIndex].geometry.setFromPoints([secondChamferPoint]);
}
}