Skip to content

Commit 2fca476

Browse files
Add save and load methods to CadDocument (#772)
* Add save and open methods to CadDocument * Tidy * Tidy * add test + load_simplified * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Use constant * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Rename load -> import_from_file, tidy * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Support pathlib.Path * Undo add_step_file update --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent cc5469f commit 2fca476

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

python/jupytercad_lab/jupytercad_lab/notebook/cad_document.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from pathlib import Path
77
from typing import Any, Dict, List, Optional, Union
88

9-
from pycrdt import Array, Doc, Map
9+
from pycrdt import Array, Doc, Map, Text
1010
from pydantic import BaseModel
1111
from ypywidgets.comm import CommWidget
1212

@@ -27,6 +27,7 @@
2727
Parts,
2828
ShapeMetadata,
2929
IAny,
30+
SCHEMA_VERSION,
3031
)
3132

3233
logger = logging.getLogger(__file__)
@@ -50,6 +51,7 @@ def __init__(self, path: Optional[str] = None):
5051
ydoc=ydoc,
5152
)
5253

54+
self.ydoc["schemaVersion"] = self._schemaVersion = Text(SCHEMA_VERSION)
5355
self.ydoc["objects"] = self._objects_array = Array()
5456
self.ydoc["metadata"] = self._metadata = Map()
5557
self.ydoc["outputs"] = self._outputs = Map()
@@ -64,6 +66,49 @@ def objects(self) -> List[str]:
6466
return [x["name"] for x in self._objects_array]
6567
return []
6668

69+
@classmethod
70+
def import_from_file(cls, path: str | Path) -> CadDocument:
71+
"""
72+
Import a CadDocument from a .jcad file.
73+
74+
:param path: The path to the file.
75+
:return: A new CadDocument instance.
76+
"""
77+
instance = cls()
78+
with open(path, "r") as f:
79+
jcad_content = json.load(f)
80+
81+
instance.ydoc["objects"] = instance._objects_array = Array(
82+
[Map(obj) for obj in jcad_content.get("objects", [])]
83+
)
84+
instance.ydoc["options"] = instance._options = Map(
85+
jcad_content.get("options", {})
86+
)
87+
instance.ydoc["metadata"] = instance._metadata = Map(
88+
jcad_content.get("metadata", {})
89+
)
90+
instance.ydoc["outputs"] = instance._outputs = Map(
91+
jcad_content.get("outputs", {})
92+
)
93+
94+
return instance
95+
96+
def save(self, path: str | Path) -> None:
97+
"""
98+
Save the CadDocument to a .jcad file on the local filesystem.
99+
100+
:param path: The path to the file.
101+
"""
102+
content = {
103+
"schemaVersion": SCHEMA_VERSION,
104+
"objects": self._objects_array.to_py(),
105+
"options": self._options.to_py(),
106+
"metadata": self._metadata.to_py(),
107+
"outputs": self._outputs.to_py(),
108+
}
109+
with open(path, "w") as f:
110+
json.dump(content, f, indent=4)
111+
67112
@classmethod
68113
def _path_to_comm(cls, filePath: Optional[str]) -> Dict:
69114
path = None

0 commit comments

Comments
 (0)