Skip to content

Commit

Permalink
Step file export to .jcad (#238)
Browse files Browse the repository at this point in the history
* .jcad file export

* Put another timeout

* Nevermind
  • Loading branch information
martinRenou authored Feb 9, 2024
1 parent 732ea7c commit 349e04f
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 1 deletion.
69 changes: 69 additions & 0 deletions packages/base/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ import {
explodedViewIcon,
extrusionIcon,
intersectionIcon,
requestAPI,
sphereIcon,
torusIcon,
unionIcon
} from './tools';
import { JupyterCadPanel, JupyterCadWidget } from './widget';
import { DocumentRegistry } from '@jupyterlab/docregistry';
import { PathExt } from '@jupyterlab/coreutils';

export function newName(type: string, model: IJupyterCadModel): string {
const sharedModel = model.sharedModel;
Expand Down Expand Up @@ -404,6 +407,43 @@ const CAMERA_FORM = {
}
};

const EXPORT_FORM = {
title: 'Export to .jcad',
schema: {
type: 'object',
required: ['Name'],
additionalProperties: false,
properties: {
Name: {
title: 'File name',
description: 'The exported file name',
type: 'string'
}
}
},
default: (context: DocumentRegistry.IContext<IJupyterCadModel>) => {
return {
Name: PathExt.basename(context.path).replace(
PathExt.extname(context.path),
'.jcad'
)
};
},
syncData: (context: DocumentRegistry.IContext<IJupyterCadModel>) => {
return (props: IDict) => {
const { Name } = props;
console.log(`export to ${Name}`);
requestAPI<{ done: boolean }>('jupytercad/export', {
method: 'POST',
body: JSON.stringify({
path: context.path,
newName: Name
})
});
};
}
};

/**
* Add the FreeCAD commands to the application's command registry.
*/
Expand Down Expand Up @@ -645,6 +685,33 @@ export function addCommands(
await dialog.launch();
}
});

commands.addCommand(CommandIDs.exportJcad, {
label: trans.__('Export to .jcad'),
isEnabled: () => {
return tracker.currentWidget
? tracker.currentWidget.context.model.sharedModel.exportable
: false;
},
iconClass: 'fa fa-file-export',
execute: async () => {
const current = tracker.currentWidget;

if (!current) {
return;
}

const dialog = new FormDialog({
context: current.context,
title: EXPORT_FORM.title,
schema: EXPORT_FORM.schema,
sourceData: EXPORT_FORM.default(tracker.currentWidget?.context),
syncData: EXPORT_FORM.syncData(tracker.currentWidget?.context),
cancelButton: true
});
await dialog.launch();
}
});
}

/**
Expand All @@ -670,6 +737,8 @@ export namespace CommandIDs {
export const updateAxes = 'jupytercad:updateAxes';
export const updateExplodedView = 'jupytercad:updateExplodedView';
export const updateCameraSettings = 'jupytercad:updateCameraSettings';

export const exportJcad = 'jupytercad:exportJcad';
}

namespace Private {
Expand Down
1 change: 1 addition & 0 deletions packages/schema/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export interface IJupyterCadDoc extends YDocument<IJupyterCadDocChange> {
metadata: JSONObject;
outputs: JSONObject;
readonly editable: boolean;
readonly exportable: boolean;

objectExists(name: string): boolean;
getObjectByName(name: string): IJCadObject | undefined;
Expand Down
2 changes: 2 additions & 0 deletions packages/schema/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ export class JupyterCadDoc
}

editable = true;
exportable = false;

private _getObjectAsYMapByName(name: string): Y.Map<any> | undefined {
for (const obj of this._objects) {
Expand Down Expand Up @@ -547,6 +548,7 @@ export class JupyterCadStepDoc extends JupyterCadDoc {
}

editable = false;
exportable = true;

private _sourceObserver = (events: Y.YEvent<any>[]): void => {
const changes: Array<{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"ServerApp": {
"jpserver_extensions": {
"jupytercad_core": true
}
}
}
14 changes: 14 additions & 0 deletions python/jupytercad_core/jupytercad_core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@
warnings.warn("Importing 'jupytercad_core' outside a proper installation.")
__version__ = "dev"

from .handlers import setup_handlers


def _jupyter_labextension_paths():
return [{"src": "labextension", "dest": "@jupytercad/jupytercad-core"}]


def _load_jupyter_server_extension(server_app):
"""Registers the API handler to receive HTTP requests from the frontend extension.
Parameters
----------
server_app: jupyterlab.labapp.LabApp
JupyterLab application instance
"""
setup_handlers(server_app.web_app)
server_app.log.info("Registered jupytercad server extension")
52 changes: 52 additions & 0 deletions python/jupytercad_core/jupytercad_core/handlers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import json
from pathlib import Path

from jupyter_server.base.handlers import APIHandler
from jupyter_server.utils import url_path_join, ApiPath, to_os_path
import tornado


class JCadExportHandler(APIHandler):
@tornado.web.authenticated
def post(self):
body = self.get_json_body()

# Get filename removing the drive prefix
file_name = body["path"].split(":")[1]
export_name = body["newName"]

root_dir = Path(self.contents_manager.root_dir).resolve()
file_name = Path(to_os_path(ApiPath(file_name), str(root_dir)))

with open(file_name, "r") as fobj:
file_content = fobj.read()

jcad = dict(
objects=[
dict(
name=Path(export_name).stem,
visible=True,
shape="Part::Any",
parameters=dict(
Content=file_content, Type=str(Path(Path(file_name).suffix[1:]))
),
)
],
metadata={},
options={},
outputs={},
)

with open(Path(file_name).parents[0] / export_name, "w") as fobj:
fobj.write(json.dumps(jcad, indent=2))

self.finish(json.dumps({"done": True}))


def setup_handlers(web_app):
host_pattern = ".*$"

base_url = web_app.settings["base_url"]
route_pattern = url_path_join(base_url, "jupytercad", "export")
handlers = [(route_pattern, JCadExportHandler)]
web_app.add_handlers(host_pattern, handlers)
1 change: 1 addition & 0 deletions python/jupytercad_core/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ exclude = [".github", "binder"]
[tool.hatch.build.targets.wheel.shared-data]
"install.json" = "share/jupyter/labextensions/@jupytercad/jupytercad-core/install.json"
"jupytercad_core/labextension" = "share/jupyter/labextensions/@jupytercad/jupytercad-core"
"jupyter-config/server-config" = "etc/jupyter/jupyter_server_config.d"

[tool.hatch.build.hooks.version]
path = "jupytercad_core/_version.py"
Expand Down
3 changes: 3 additions & 0 deletions python/jupytercad_lab/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ const controlPanel: JupyterFrontEndPlugin<void> = {
* Populates the application menus for the notebook.
*/
function populateMenus(mainMenu: IMainMenu, isEnabled: () => boolean): void {
mainMenu.fileMenu.addItem({
command: CommandIDs.exportJcad
});
// Add undo/redo hooks to the edit menu.
mainMenu.editMenu.undoers.redo.add({
id: CommandIDs.redo,
Expand Down
4 changes: 4 additions & 0 deletions scripts/dev-install.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def install_dev():
execute(f"pip uninstall {py_package} -y")
execute("jlpm clean:all", cwd=root_path / "python" / py_package)
execute(f"pip install -e {python_package_prefix}/{py_package}")

if py_package == "jupytercad_core":
execute("jupyter server extension enable jupytercad_core")

if py_package != "jupytercad_app":
execute(
f"jupyter labextension develop {python_package_prefix}/{py_package} --overwrite"
Expand Down
2 changes: 1 addition & 1 deletion ui-tests/tests/ui.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ test.describe('UI Test', () => {
});
});

test.describe('File rendering test', () => {
test.describe('File operations', () => {
test.beforeAll(async ({ request }) => {
const content = galata.newContentsHelper(request);
await content.deleteDirectory('/examples');
Expand Down

0 comments on commit 349e04f

Please sign in to comment.