Skip to content

Commit

Permalink
Fix python api (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
trungleduc committed Sep 16, 2024
1 parent 1867479 commit 91940ba
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 72 deletions.
42 changes: 0 additions & 42 deletions examples/Notebook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -136,28 +136,6 @@
"doc"
]
},
{
"cell_type": "markdown",
"id": "25cf26d3-de48-42f2-9c79-89d2eb40948f",
"metadata": {},
"source": [
"#### Load an existing FCSTD file"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f89aa5cd-961c-4d74-9a7c-ebe260e081dc",
"metadata": {},
"outputs": [],
"source": [
"from jupytercad import CadDocument\n",
"\n",
"doc = CadDocument(\"example3.FCStd\")\n",
"\n",
"doc"
]
},
{
"cell_type": "markdown",
"id": "a2874026-07eb-4d92-b660-cc68606c0664",
Expand Down Expand Up @@ -248,26 +226,6 @@
"source": [
"doc.remove_annotation(id)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3afc5f3c-cc33-4c8a-8904-98726a3ae118",
"metadata": {},
"outputs": [],
"source": [
"doc.set_color('box2', [0.5,0.4,0.2])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "855133ee-8cd2-432b-933a-d72f1a9f6c7c",
"metadata": {},
"outputs": [],
"source": [
"doc.set_color('box2',None)"
]
}
],
"metadata": {
Expand Down
33 changes: 3 additions & 30 deletions python/jupytercad_lab/jupytercad_lab/notebook/cad_document.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from __future__ import annotations
from copy import deepcopy

import json
import logging
Expand Down Expand Up @@ -93,7 +92,7 @@ def _path_to_comm(cls, filePath: Optional[str]) -> Dict:

def get_object(self, name: str) -> Optional["PythonJcadObject"]:
if self.check_exist(name):
data = json.loads(self._get_yobject_by_name(name).to_py())
data = self._get_yobject_by_name(name).to_py()
return OBJECT_FACTORY.create_object(data, self)

def remove(self, name: str) -> CadDocument:
Expand All @@ -104,7 +103,7 @@ def remove(self, name: str) -> CadDocument:

def add_object(self, new_object: "PythonJcadObject") -> CadDocument:
if self._objects_array is not None and not self.check_exist(new_object.name):
obj_dict = json.loads(new_object.json())
obj_dict = json.loads(new_object.model_dump_json())
obj_dict["visible"] = True
new_map = Map(obj_dict)
self._objects_array.append(new_map)
Expand Down Expand Up @@ -166,32 +165,6 @@ def remove_annotation(self, annotation_id: str) -> None:
with self.ydoc.transaction() as t:
self._metadata.pop(t, annotation_id, None)

def set_color(self, object_name: str, color: Optional[List]) -> None:
"""
Set object color.
:param object_name: Object name.
:param color: Color value, set it to `None` to remove color.
"""
if self._options and self.check_exist(object_name):
current_gui = self._options.get("guidata")
new_gui = None
if current_gui is not None:
new_gui = deepcopy(current_gui)
current_data: Dict = new_gui.get(object_name, {})
if color is not None:
current_data["color"] = color
else:
current_data.pop("color", None)

new_gui[object_name] = current_data
else:
if color is not None:
new_gui = {object_name: {"color": color}}
if new_gui is not None:
with self.ydoc.transaction() as t:
self._options.set(t, "guidata", new_gui)

def add_step_file(
self,
path: str,
Expand Down Expand Up @@ -791,7 +764,7 @@ def create_object(
Model = self._factories[object_type]
args = {}
params = data["parameters"]
for field in Model.__fields__:
for field in Model.model_fields:
args[field] = params.get(field, None)
obj_params = Model(**args)
return PythonJcadObject(
Expand Down

0 comments on commit 91940ba

Please sign in to comment.