Skip to content

Commit 4048066

Browse files
authored
Allow transforming objects when clipping is enabled (#565)
1 parent 9cfb913 commit 4048066

File tree

1 file changed

+74
-63
lines changed

1 file changed

+74
-63
lines changed

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

Lines changed: 74 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,8 @@ export class MainView extends React.Component<IProps, IStates> {
355355
}, 100)
356356
);
357357

358-
// Setting up the transform controls
359-
this._transformControls = new TransformControls(
358+
// Setting up the clip plane transform controls
359+
this._clipPlaneTransformControls = new TransformControls(
360360
this._camera,
361361
this._renderer.domElement
362362
);
@@ -389,12 +389,15 @@ export class MainView extends React.Component<IProps, IStates> {
389389
this._scene.add(this._clippingPlaneMeshControl);
390390

391391
// Disable the orbit control whenever we do transformation
392-
this._transformControls.addEventListener('dragging-changed', event => {
393-
this._controls.enabled = !event.value;
394-
});
392+
this._clipPlaneTransformControls.addEventListener(
393+
'dragging-changed',
394+
event => {
395+
this._controls.enabled = !event.value;
396+
}
397+
);
395398

396399
// Update the clipping plane whenever the transform UI move
397-
this._transformControls.addEventListener('change', () => {
400+
this._clipPlaneTransformControls.addEventListener('change', () => {
398401
let normal = new THREE.Vector3(0, 0, 1);
399402
normal = normal.applyEuler(this._clippingPlaneMeshControl.rotation);
400403

@@ -415,13 +418,59 @@ export class MainView extends React.Component<IProps, IStates> {
415418
)
416419
);
417420
});
418-
this._transformControls.attach(this._clippingPlaneMeshControl);
419-
this._scene.add(this._transformControls);
420421

422+
this._clipPlaneTransformControls.attach(this._clippingPlaneMeshControl);
423+
this._scene.add(this._clipPlaneTransformControls);
424+
425+
this._clipPlaneTransformControls.enabled = false;
426+
this._clipPlaneTransformControls.visible = false;
427+
428+
this._transformControls = new TransformControls(
429+
this._camera,
430+
this._renderer.domElement
431+
);
432+
// Disable the orbit control whenever we do transformation
433+
this._transformControls.addEventListener('dragging-changed', event => {
434+
this._controls.enabled = !event.value;
435+
});
436+
// Update the currently transformed object in the shared model once finished moving
437+
this._transformControls.addEventListener('mouseUp', () => {
438+
const updatedObject = this._selectedMeshes[0];
439+
const objectName = updatedObject.name;
440+
441+
const updatedPosition = new THREE.Vector3();
442+
updatedObject.getWorldPosition(updatedPosition);
443+
444+
const obj = this._model.sharedModel.getObjectByName(objectName);
445+
446+
if (obj && obj.parameters && obj.parameters.Placement) {
447+
const positionArray = obj?.parameters?.Placement?.Position;
448+
const newPosition = [
449+
positionArray[0] + updatedPosition.x,
450+
positionArray[1] + updatedPosition.y,
451+
positionArray[2] + updatedPosition.z
452+
];
453+
454+
this._model.sharedModel.updateObjectByName(objectName, {
455+
data: {
456+
key: 'parameters',
457+
value: {
458+
...obj.parameters,
459+
Placement: {
460+
...obj.parameters.Placement,
461+
Position: newPosition
462+
}
463+
}
464+
}
465+
});
466+
}
467+
});
468+
this._scene.add(this._transformControls);
469+
this._transformControls.setMode('translate');
421470
this._transformControls.enabled = false;
422471
this._transformControls.visible = false;
472+
423473
this._createViewHelper();
424-
this._updateTransformControls();
425474
}
426475
};
427476

@@ -701,10 +750,10 @@ export class MainView extends React.Component<IProps, IStates> {
701750
event.preventDefault();
702751
event.stopPropagation();
703752

704-
if (this._transformControls.mode === 'rotate') {
705-
this._transformControls.setMode('translate');
753+
if (this._clipPlaneTransformControls.mode === 'rotate') {
754+
this._clipPlaneTransformControls.setMode('translate');
706755
} else {
707-
this._transformControls.setMode('rotate');
756+
this._clipPlaneTransformControls.setMode('rotate');
708757
}
709758
break;
710759
}
@@ -1086,11 +1135,7 @@ export class MainView extends React.Component<IProps, IStates> {
10861135
}
10871136

10881137
// Detach TransformControls from the previous selection
1089-
if (!this._clipSettings.enabled) {
1090-
{
1091-
this._transformControls.detach();
1092-
}
1093-
}
1138+
this._transformControls.detach();
10941139
}
10951140

10961141
// Set new selection
@@ -1141,7 +1186,7 @@ export class MainView extends React.Component<IProps, IStates> {
11411186
}
11421187
}
11431188

1144-
if (selectedNames.length === 1 && !this._clipSettings.enabled) {
1189+
if (selectedNames.length === 1) {
11451190
const selectedMeshName = selectedNames[0];
11461191
const matchingChild = this._meshGroup?.children.find(child =>
11471192
child.name.startsWith(selectedMeshName)
@@ -1162,50 +1207,12 @@ export class MainView extends React.Component<IProps, IStates> {
11621207
this._transformControls.position.copy(positionVector);
11631208
}
11641209

1165-
this._transformControls.setMode('translate');
11661210
this._transformControls.visible = true;
11671211
this._transformControls.enabled = true;
11681212
}
11691213
}
11701214
}
11711215

1172-
private _updateTransformControls() {
1173-
this._transformControls.addEventListener('mouseUp', () => {
1174-
if (this._clipSettings.enabled) {
1175-
return;
1176-
}
1177-
const updatedObject = this._selectedMeshes[0];
1178-
const objectName = updatedObject.name;
1179-
1180-
const updatedPosition = new THREE.Vector3();
1181-
updatedObject.getWorldPosition(updatedPosition);
1182-
1183-
const obj = this._model.sharedModel.getObjectByName(objectName);
1184-
1185-
if (obj && obj.parameters && obj.parameters.Placement) {
1186-
const positionArray = obj?.parameters?.Placement?.Position;
1187-
const newPosition = [
1188-
positionArray[0] + updatedPosition.x,
1189-
positionArray[1] + updatedPosition.y,
1190-
positionArray[2] + updatedPosition.z
1191-
];
1192-
1193-
this._model.sharedModel.updateObjectByName(objectName, {
1194-
data: {
1195-
key: 'parameters',
1196-
value: {
1197-
...obj.parameters,
1198-
Placement: {
1199-
...obj.parameters.Placement,
1200-
Position: newPosition
1201-
}
1202-
}
1203-
}
1204-
});
1205-
}
1206-
});
1207-
}
1208-
12091216
private _onSharedMetadataChanged = (
12101217
_: IJupyterCadDoc,
12111218
changes: MapChange
@@ -1545,6 +1552,7 @@ export class MainView extends React.Component<IProps, IStates> {
15451552
this._camera.up.copy(up);
15461553

15471554
this._transformControls.camera = this._camera;
1555+
this._clipPlaneTransformControls.camera = this._camera;
15481556

15491557
const resizeEvent = new Event('resize');
15501558
window.dispatchEvent(resizeEvent);
@@ -1553,18 +1561,20 @@ export class MainView extends React.Component<IProps, IStates> {
15531561
private _updateClipping() {
15541562
if (this._clipSettings.enabled) {
15551563
this._renderer.localClippingEnabled = true;
1556-
this._transformControls.enabled = true;
1557-
this._transformControls.visible = true;
1558-
this._transformControls.attach(this._clippingPlaneMeshControl);
1559-
this._transformControls.position.copy(new THREE.Vector3(0, 0, 0));
1564+
this._clipPlaneTransformControls.enabled = true;
1565+
this._clipPlaneTransformControls.visible = true;
1566+
this._clipPlaneTransformControls.attach(this._clippingPlaneMeshControl);
1567+
this._clipPlaneTransformControls.position.copy(
1568+
new THREE.Vector3(0, 0, 0)
1569+
);
15601570
this._clippingPlaneMeshControl.visible = this._clipSettings.showClipPlane;
15611571
if (this._clippingPlaneMesh) {
15621572
this._clippingPlaneMesh.visible = true;
15631573
}
15641574
} else {
15651575
this._renderer.localClippingEnabled = false;
1566-
this._transformControls.enabled = false;
1567-
this._transformControls.visible = false;
1576+
this._clipPlaneTransformControls.enabled = false;
1577+
this._clipPlaneTransformControls.visible = false;
15681578
this._clippingPlaneMeshControl.visible = false;
15691579
if (this._clippingPlaneMesh) {
15701580
this._clippingPlaneMesh.visible = false;
@@ -1704,7 +1714,8 @@ export class MainView extends React.Component<IProps, IStates> {
17041714
private _controls: OrbitControls; // Mouse controls
17051715
private _hasOrbited = false; // Whether the last orbit control run has actually orbited
17061716
private _disabledNextClick = false; // We set this when we stop orbiting, to prevent the next click event
1707-
private _transformControls: TransformControls; // Mesh position/rotation controls
1717+
private _clipPlaneTransformControls: TransformControls; // Clip plane position/rotation controls
1718+
private _transformControls: TransformControls; // Mesh position controls
17081719
private _pointer3D: IPointer | null = null;
17091720
private _clock: THREE.Clock;
17101721
private _targetPosition: THREE.Vector3 | null = null;

0 commit comments

Comments
 (0)