-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bac86b2
commit 35451f6
Showing
6 changed files
with
155 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,53 @@ | ||
import json | ||
from pathlib import Path | ||
|
||
from jupyter_server.base.handlers import APIHandler | ||
from jupyter_server.utils import url_path_join | ||
from jupyter_server.utils import url_path_join, ApiPath, to_os_path | ||
import tornado | ||
|
||
class RouteHandler(APIHandler): | ||
# The following decorator should be present on all verb methods (head, get, post, | ||
# patch, put, delete, options) to ensure only authorized user can request the | ||
# Jupyter server | ||
from jupytercad_openvsp.loader import vsp3_to_stl | ||
|
||
|
||
class JCadExportHandler(APIHandler): | ||
@tornado.web.authenticated | ||
def get(self): | ||
self.finish(json.dumps({ | ||
"data": "This is /jupytercad-openvsp/get-example endpoint!" | ||
})) | ||
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() | ||
|
||
stl_content = vsp3_to_stl(file_content) | ||
jcad = dict( | ||
objects=[ | ||
dict( | ||
name=Path(export_name).stem, | ||
visible=True, | ||
shape="Part::Any", | ||
parameters=dict(Content=stl_content, Type="STL"), | ||
) | ||
], | ||
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-openvsp", "get-example") | ||
handlers = [(route_pattern, RouteHandler)] | ||
route_pattern = url_path_join(base_url, "jupytercad", "export-ovsp") | ||
handlers = [(route_pattern, JCadExportHandler)] | ||
web_app.add_handlers(host_pattern, handlers) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { | ||
IJCadObject, | ||
IJcadObjectDocChange, | ||
IJupyterCadDoc, | ||
JupyterCadDoc | ||
} from '@jupytercad/schema'; | ||
import { JSONExt } from '@lumino/coreutils'; | ||
import { ISignal, Signal } from '@lumino/signaling'; | ||
import * as Y from 'yjs'; | ||
|
||
export class JupyterCadOVSPDoc extends JupyterCadDoc { | ||
constructor() { | ||
super(); | ||
|
||
this._source = this.ydoc.getText('source'); | ||
|
||
this._source.observeDeep(this._sourceObserver); | ||
} | ||
|
||
get version(): string { | ||
return '0.1.0'; | ||
} | ||
|
||
get objectsChanged(): ISignal<IJupyterCadDoc, IJcadObjectDocChange> { | ||
return this._objectChanged; | ||
} | ||
|
||
get objects(): Array<IJCadObject> { | ||
const source = this._source.toJSON(); | ||
|
||
if (!source) { | ||
return []; | ||
} | ||
|
||
return [ | ||
{ | ||
name: 'OpenVSP File', // TODO get file name? | ||
visible: true, | ||
shape: 'Part::Any', | ||
parameters: { | ||
Content: this._source.toJSON(), | ||
Type: 'STL' | ||
} | ||
} | ||
]; | ||
} | ||
|
||
static create(): JupyterCadOVSPDoc { | ||
return new JupyterCadOVSPDoc(); | ||
} | ||
|
||
editable = false; | ||
toJcadEndpoint = 'jupytercad/export-ovsp'; | ||
|
||
private _sourceObserver = (events: Y.YEvent<any>[]): void => { | ||
const changes: Array<{ | ||
name: string; | ||
key: keyof IJCadObject; | ||
newValue: IJCadObject; | ||
}> = []; | ||
events.forEach(event => { | ||
event.keys.forEach((change, key) => { | ||
changes.push({ | ||
name: 'OpenVSP File', | ||
key: key as any, | ||
newValue: JSONExt.deepCopy(event.target.toJSON()) | ||
}); | ||
}); | ||
}); | ||
this._objectChanged.emit({ objectChange: changes }); | ||
this._changed.emit({ objectChange: changes }); | ||
}; | ||
|
||
private _source: Y.Text; | ||
private _objectChanged = new Signal<IJupyterCadDoc, IJcadObjectDocChange>( | ||
this | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters