Skip to content

Commit 709c792

Browse files
Add Translation Controls (#529)
* i can transform cube * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * use name * transform working but transform axes exaggerates * It's better * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Do something on trannform control change * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Positioning works * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Finally the mess is clear (Atleast) * Use sharedModel for translation * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Dont recenter * A little cleanup * limit to translation for now * Compatible with clipplane * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Refactor transformation logic to allow only when single object is selected * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 4f7c1fc commit 709c792

File tree

1 file changed

+88
-6
lines changed

1 file changed

+88
-6
lines changed

packages/base/src/3dview/mainview.tsx

Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ export class MainView extends React.Component<IProps, IStates> {
400400
this._transformControls.enabled = false;
401401
this._transformControls.visible = false;
402402
this._createViewHelper();
403+
this._updateTransformControls();
403404
}
404405
};
405406

@@ -811,6 +812,7 @@ export class MainView extends React.Component<IProps, IStates> {
811812

812813
this._scene.add(this._clippingPlaneMesh);
813814
this._scene.add(this._meshGroup);
815+
814816
if (this._loadingTimeout) {
815817
clearTimeout(this._loadingTimeout);
816818
this._loadingTimeout = null;
@@ -1017,7 +1019,16 @@ export class MainView extends React.Component<IProps, IStates> {
10171019
return mesh;
10181020
}
10191021

1022+
private _previousSelection: { [key: string]: ISelection } | null = null;
10201023
private _updateSelected(selection: { [key: string]: ISelection }) {
1024+
const selectionChanged =
1025+
JSON.stringify(selection) !== JSON.stringify(this._previousSelection);
1026+
1027+
if (!selectionChanged) {
1028+
return;
1029+
}
1030+
this._previousSelection = { ...selection };
1031+
10211032
// Reset original color and remove bounding boxes for old selection
10221033
for (const selectedMesh of this._selectedMeshes) {
10231034
let originalColor = selectedMesh.userData.originalColor;
@@ -1046,20 +1057,25 @@ export class MainView extends React.Component<IProps, IStates> {
10461057
if (material?.linewidth) {
10471058
material.linewidth = DEFAULT_LINEWIDTH;
10481059
}
1060+
1061+
// Detach TransformControls from the previous selection
1062+
if (!this._clipSettings.enabled) {
1063+
{
1064+
this._transformControls.detach();
1065+
}
1066+
}
10491067
}
10501068

10511069
// Set new selection
10521070
this._selectedMeshes = [];
1053-
for (const selectionName in selection) {
1071+
const selectedNames = Object.keys(selection);
1072+
1073+
for (const selectionName of selectedNames) {
10541074
const selectedMesh = this._meshGroup?.getObjectByName(
10551075
selectionName
10561076
) as BasicMesh;
10571077

1058-
if (!selectedMesh) {
1059-
continue;
1060-
}
1061-
1062-
if (!selectedMesh.visible) {
1078+
if (!selectedMesh || !selectedMesh.visible) {
10631079
continue;
10641080
}
10651081

@@ -1097,6 +1113,70 @@ export class MainView extends React.Component<IProps, IStates> {
10971113
}
10981114
}
10991115
}
1116+
1117+
if (selectedNames.length === 1 && !this._clipSettings.enabled) {
1118+
const selectedMeshName = selectedNames[0];
1119+
const matchingChild = this._meshGroup?.children.find(child =>
1120+
child.name.startsWith(selectedMeshName)
1121+
);
1122+
1123+
if (matchingChild) {
1124+
this._transformControls.attach(matchingChild as BasicMesh);
1125+
1126+
const obj = this._model.sharedModel.getObjectByName(selectedMeshName);
1127+
const positionArray = obj?.parameters?.Placement?.Position;
1128+
1129+
if (positionArray && positionArray.length === 3) {
1130+
const positionVector = new THREE.Vector3(
1131+
positionArray[0],
1132+
positionArray[1],
1133+
positionArray[2]
1134+
);
1135+
this._transformControls.position.copy(positionVector);
1136+
}
1137+
1138+
this._transformControls.setMode('translate');
1139+
this._transformControls.visible = true;
1140+
this._transformControls.enabled = true;
1141+
}
1142+
}
1143+
}
1144+
1145+
private _updateTransformControls() {
1146+
this._transformControls.addEventListener('mouseUp', () => {
1147+
if (this._clipSettings.enabled) {
1148+
return;
1149+
}
1150+
const updatedObject = this._selectedMeshes[0];
1151+
const objectName = updatedObject.name;
1152+
1153+
const updatedPosition = new THREE.Vector3();
1154+
updatedObject.getWorldPosition(updatedPosition);
1155+
1156+
const obj = this._model.sharedModel.getObjectByName(objectName);
1157+
1158+
if (obj && obj.parameters && obj.parameters.Placement) {
1159+
const positionArray = obj?.parameters?.Placement?.Position;
1160+
const newPosition = [
1161+
positionArray[0] + updatedPosition.x,
1162+
positionArray[1] + updatedPosition.y,
1163+
positionArray[2] + updatedPosition.z
1164+
];
1165+
1166+
this._model.sharedModel.updateObjectByName(objectName, {
1167+
data: {
1168+
key: 'parameters',
1169+
value: {
1170+
...obj.parameters,
1171+
Placement: {
1172+
...obj.parameters.Placement,
1173+
Position: newPosition
1174+
}
1175+
}
1176+
}
1177+
});
1178+
}
1179+
});
11001180
}
11011181

11021182
private _onSharedMetadataChanged = (
@@ -1448,6 +1528,8 @@ export class MainView extends React.Component<IProps, IStates> {
14481528
this._renderer.localClippingEnabled = true;
14491529
this._transformControls.enabled = true;
14501530
this._transformControls.visible = true;
1531+
this._transformControls.attach(this._clippingPlaneMeshControl);
1532+
this._transformControls.position.copy(new THREE.Vector3(0, 0, 0));
14511533
this._clippingPlaneMeshControl.visible = this._clipSettings.showClipPlane;
14521534
if (this._clippingPlaneMesh) {
14531535
this._clippingPlaneMesh.visible = true;

0 commit comments

Comments
 (0)