Skip to content

Commit

Permalink
Update shape metadata logic
Browse files Browse the repository at this point in the history
  • Loading branch information
trungleduc committed Jul 29, 2024
1 parent 09f22d8 commit f6c5c9d
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 22 deletions.
12 changes: 0 additions & 12 deletions packages/base/src/3dview/mainviewmodel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { IWorkerMessage } from '@jupytercad/occ-worker';
import {
IAnnotation,
IDict,
IDisplayShape,
IJcadObjectDocChange,
IJCadWorker,
IJCadWorkerRegistry,
Expand Down Expand Up @@ -106,8 +105,6 @@ export class MainViewModel implements IDisposable {
}
});

this._saveMeta(result);

if (this._firstRender) {
const postShapes = this._jcadModel.sharedModel
.outputs as any as IDict<IPostResult>;
Expand Down Expand Up @@ -231,15 +228,6 @@ export class MainViewModel implements IDisposable {
}
}

private _saveMeta(payload: IDisplayShape['payload']['result']) {
if (!this._jcadModel) {
return;
}
Object.entries(payload).forEach(([objName, data]) => {
this._jcadModel.sharedModel.setShapeMeta(objName, data.meta);
});
}

private async _onSharedObjectsChanged(
_: IJupyterCadDoc,
change: IJcadObjectDocChange
Expand Down
29 changes: 28 additions & 1 deletion packages/base/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ export async function executeOperator(
}

// Everything's good, we can apply the change to the shared model

const objMeta = dryRunResult.shapeMetadata?.[objectModel.name];
if (objMeta) {
objectModel.shapeMetadata = objMeta;
}
sharedModel.transact(() => {
transaction(sharedModel);
});
Expand Down Expand Up @@ -1046,7 +1051,7 @@ namespace Private {
title: value.title,
sourceData: value.default(current.context.model),
schema: FORM_SCHEMA[value.shape],
syncData: (props: IDict) => {
syncData: async (props: IDict) => {
const { Name, ...parameters } = props;
const objectModel: IJCadObject = {
shape: value.shape as Parts,
Expand All @@ -1056,8 +1061,30 @@ namespace Private {
};

const sharedModel = current.context.model.sharedModel;

if (sharedModel) {
if (!sharedModel.objectExists(objectModel.name)) {
// Try a dry run with the update content to verify its feasibility
const currentJcadContent = current.context.model.getContent();
const updatedContent: IJCadContent = {
...currentJcadContent,
objects: [...currentJcadContent.objects, objectModel]
};
const dryRunResult =
await current.content.currentViewModel.dryRun(updatedContent);
if (dryRunResult.status === 'error') {
showErrorMessage(
`Failed to create the ${value.shape} shape`,
`The ${value.shape} tool was unable to create the desired shape due to invalid parameter values. The values you entered may not be compatible with the dimensions of your piece.`
);

return;
}

const objMeta = dryRunResult.shapeMetadata?.[objectModel.name];
if (objMeta) {
objectModel.shapeMetadata = objMeta;
}
sharedModel.addObject(objectModel);
} else {
showErrorMessage(
Expand Down
13 changes: 10 additions & 3 deletions packages/base/src/panelview/objectproperties.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,18 @@ class ObjectPropertiesReact extends React.Component<IProps, IStates> {
}

// Dry run was successful, ready to apply the update now
const meta: IDict = dryRunResult.shapeMetadata?.[objectName] ?? {};
const obj = model.sharedModel.getObjectByName(objectName);
if (obj) {
model.sharedModel.updateObjectByName(objectName, 'parameters', {
...obj['parameters'],
...properties
model.sharedModel.updateObjectByName(objectName, {
data: {
key: 'parameters',
value: {
...obj['parameters'],
...properties
}
},
meta
});
}
}
Expand Down
11 changes: 8 additions & 3 deletions packages/occ-worker/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ self.onmessage = async (event: MessageEvent): Promise<void> => {
break;
}
case WorkerAction.DRY_RUN: {
let response: IDict = {};
try {
WorkerHandler[WorkerAction.DRY_RUN](message.payload);
response = WorkerHandler[WorkerAction.DRY_RUN](message.payload);
} catch (e) {
let msg = '';

Expand All @@ -80,13 +81,17 @@ self.onmessage = async (event: MessageEvent): Promise<void> => {
);
return;
}

const shapeMetadata: IDict = {};
Object.entries(response.result ?? {}).forEach(([objName, data]) => {
shapeMetadata[objName] = (data as any)?.['meta'];
});
sendToMain(
{
action: MainAction.DRY_RUN_RESPONSE,
payload: {
id: message.payload.id,
status: 'ok'
status: 'ok',
shapeMetadata
}
},
id
Expand Down
10 changes: 8 additions & 2 deletions packages/schema/src/doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,18 @@ export class JupyterCadDoc
});
}

updateObjectByName(name: string, key: string, value: any): void {
updateObjectByName(
name: string,
payload: { data: { key: string; value: any }; meta?: IDict }
): void {
const obj = this._getObjectAsYMapByName(name);
if (!obj) {
return;
}
const { key, value } = payload.data;

this.transact(() => {
// Special case for changing parameters, we may need to update dependencies
console.log('update ', key);
if (key === 'parameters') {
switch (obj.get('shape')) {
case 'Part::Cut': {
Expand All @@ -190,6 +193,9 @@ export class JupyterCadDoc
}

obj.set(key, value);
if (payload.meta) {
obj.set('shapeMetadata', payload.meta);
}
});
}

Expand Down
6 changes: 5 additions & 1 deletion packages/schema/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ export interface IJupyterCadDoc extends YDocument<IJupyterCadDocChange> {
removeObjectByName(name: string): void;
addObject(value: IJCadObject): void;
addObjects(value: Array<IJCadObject>): void;
updateObjectByName(name: string, key: string, value: any): void;
updateObjectByName(
name: string,
payload: { data: { key: string; value: any }; meta?: IDict }
): void;
getDependants(name: string): string[];

getOption(key: keyof IJCadOptions): IDict | undefined;
Expand Down Expand Up @@ -258,6 +261,7 @@ export interface IDryRunResponsePayload {
id: string;
status: 'ok' | 'error';
message?: string;
shapeMetadata?: IDict;
}

export interface IDryRunResponse extends IMainMessageBase {
Expand Down
1 change: 1 addition & 0 deletions python/jupytercad_core/jupytercad_core/jcad_ydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def get(self) -> str:
return json.dumps(
dict(objects=objects, options=options, metadata=meta, outputs=outputs),
indent=2,
sort_keys=True,
)

def set(self, value: str) -> None:
Expand Down

0 comments on commit f6c5c9d

Please sign in to comment.