Skip to content

Commit

Permalink
Remove guidata from cad_document
Browse files Browse the repository at this point in the history
  • Loading branch information
arjxn-py committed Sep 13, 2024
1 parent cdb509c commit 6e68f87
Showing 1 changed file with 39 additions and 17 deletions.
56 changes: 39 additions & 17 deletions python/jupytercad_lab/jupytercad_lab/notebook/cad_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,31 +166,53 @@ def remove_annotation(self, annotation_id: str) -> None:
with self.ydoc.transaction() as t:
self._metadata.pop(t, annotation_id, None)

def get_object_by_name(self, object_name: str) -> Optional[Dict]:
"""
Retrieve an object by its name.
:param object_name: Name of the object.
:return: The object if it exists, otherwise None.
"""
for obj in self._options.get("objects", []):
if obj["name"] == object_name:
return obj
return None

def update_object_by_name(self, transaction, object_name: str, updated_obj: Dict) -> None:
"""
Update an object by its name within a transaction.
:param transaction: The YDoc transaction.
:param object_name: Name of the object to update.
:param updated_obj: The updated object data.
"""
objects = self._options.get("objects", [])

for i, obj in enumerate(objects):
if obj["name"] == object_name:
objects[i] = updated_obj
break

self._options.set(transaction, "objects", objects)

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:
if self.check_exist(object_name):
obj = self.get_object_by_name(object_name)

if obj:
with self.ydoc.transaction() as t:
self._options.set(t, "guidata", new_gui)
if color is not None:
obj['color'] = color
else:
obj.pop('color', None)

self.update_object_by_name(t, object_name, obj)

def add_step_file(
self,
Expand Down

0 comments on commit 6e68f87

Please sign in to comment.