Skip to content

Commit b7f3687

Browse files
authored
Perform a dry run for transforms (#575)
1 parent af996e5 commit b7f3687

File tree

3 files changed

+56
-48
lines changed

3 files changed

+56
-48
lines changed

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -429,16 +429,11 @@ export class MainView extends React.Component<IProps, IStates> {
429429
positionArray[2] + updatedPosition.z
430430
];
431431

432-
this._model.sharedModel.updateObjectByName(objectName, {
433-
data: {
434-
key: 'parameters',
435-
value: {
436-
...obj.parameters,
437-
Placement: {
438-
...obj.parameters.Placement,
439-
Position: newPosition
440-
}
441-
}
432+
this._mainViewModel.maybeUpdateObjectParameters(objectName, {
433+
...obj.parameters,
434+
Placement: {
435+
...obj.parameters.Placement,
436+
Position: newPosition
442437
}
443438
});
444439
}

packages/base/src/3dview/mainviewmodel.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
WorkerAction,
1717
IJCadContent
1818
} from '@jupytercad/schema';
19+
import { showErrorMessage } from '@jupyterlab/apputils';
1920
import { ObservableMap } from '@jupyterlab/observables';
2021
import { JSONValue, PromiseDelegate, UUID } from '@lumino/coreutils';
2122
import { IDisposable } from '@lumino/disposable';
@@ -171,6 +172,51 @@ export class MainViewModel implements IDisposable {
171172
});
172173
}
173174

175+
/**
176+
* Try to update an object, performing a dry run first to make sure it's feasible.
177+
*/
178+
async maybeUpdateObjectParameters(
179+
name: string,
180+
properties: { [key: string]: any }
181+
): Promise<void> {
182+
// getContent already returns a deep copy of the content, we can change it safely here
183+
const updatedContent = this.jcadModel.getContent();
184+
for (const object of updatedContent.objects) {
185+
if (object.name === name) {
186+
object.parameters = {
187+
...object.parameters,
188+
...properties
189+
};
190+
}
191+
}
192+
193+
// Try a dry run
194+
const dryRunResult = await this.dryRun(updatedContent);
195+
if (dryRunResult.status === 'error') {
196+
showErrorMessage(
197+
'Failed to update the desired shape',
198+
'The tool was unable to update the desired shape due to invalid parameter values. The values you entered may not be compatible with the dimensions of your piece.'
199+
);
200+
return;
201+
}
202+
203+
// Dry run was successful, ready to apply the update now
204+
const meta: IDict = dryRunResult.shapeMetadata?.[name] ?? {};
205+
const obj = this.jcadModel.sharedModel.getObjectByName(name);
206+
if (obj) {
207+
this.jcadModel.sharedModel.updateObjectByName(name, {
208+
data: {
209+
key: 'parameters',
210+
value: {
211+
...obj.parameters,
212+
...properties
213+
}
214+
},
215+
meta
216+
});
217+
}
218+
}
219+
174220
/**
175221
* Send a payload to the worker to test its feasibility.
176222
*

packages/base/src/panelview/objectproperties.tsx

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
IJupyterCadTracker,
1010
ISelection
1111
} from '@jupytercad/schema';
12-
import { ReactWidget, showErrorMessage } from '@jupyterlab/apputils';
12+
import { ReactWidget } from '@jupyterlab/apputils';
1313
import { PanelWithToolbar } from '@jupyterlab/ui-components';
1414
import { Panel } from '@lumino/widgets';
1515
import * as React from 'react';
@@ -193,43 +193,10 @@ class ObjectPropertiesReact extends React.Component<IProps, IStates> {
193193
return;
194194
}
195195

196-
// getContent already returns a deep copy of the content, we can change it safely here
197-
const updatedContent = model.getContent();
198-
for (const object of updatedContent.objects) {
199-
if (object.name === objectName) {
200-
object.parameters = {
201-
...object.parameters,
202-
...properties
203-
};
204-
}
205-
}
206-
207-
// Try a dry run
208-
const dryRunResult =
209-
await currentWidget.content.currentViewModel.dryRun(updatedContent);
210-
if (dryRunResult.status === 'error') {
211-
showErrorMessage(
212-
'Failed to update the shape',
213-
'The tool was unable to update the desired shape due to invalid parameter values. The values you entered may not be compatible with the dimensions of your piece.'
214-
);
215-
return;
216-
}
217-
218-
// Dry run was successful, ready to apply the update now
219-
const meta: IDict = dryRunResult.shapeMetadata?.[objectName] ?? {};
220-
const obj = model.sharedModel.getObjectByName(objectName);
221-
if (obj) {
222-
model.sharedModel.updateObjectByName(objectName, {
223-
data: {
224-
key: 'parameters',
225-
value: {
226-
...obj['parameters'],
227-
...properties
228-
}
229-
},
230-
meta
231-
});
232-
}
196+
currentWidget.content.currentViewModel.maybeUpdateObjectParameters(
197+
objectName,
198+
properties
199+
);
233200
}
234201

235202
syncSelectedField = (

0 commit comments

Comments
 (0)