Skip to content

Commit

Permalink
make guidata an internal attribute in FCStd class
Browse files Browse the repository at this point in the history
  • Loading branch information
arjxn-py committed Sep 25, 2024
1 parent 2bc1bfd commit 01d3734
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions jupytercad_freecad/freecad/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def __init__(self) -> None:
self._metadata = {}
self._id = None
self._visible = True
self._guidata = {}
self._prop_handlers: Dict[str, Type[BaseProp]] = {}
for Cls in Props.__dict__.values():
if isinstance(Cls, type) and issubclass(Cls, BaseProp):
Expand Down Expand Up @@ -132,8 +133,8 @@ def load(self, base64_content: str) -> None:
# Get metadata
self._metadata = fc_file.Meta

# Get GuiData (metadata from the GuiDocument.xml file)
self._options["guidata"] = _guidata_to_options(
# Get GuiData and assign it to the internal attribute
self._guidata = _guidata_to_options(
OfflineRenderingUtils.getGuiData(tmp.name)
)

Expand All @@ -144,16 +145,16 @@ def load(self, base64_content: str) -> None:

obj_data = self._fc_to_jcad_obj(obj)

if obj_name in self._options["guidata"]:
if "color" in self._options["guidata"][obj_name]:
if obj_name in self._guidata:
if "color" in self._guidata[obj_name]:
default_color = "#808080"
gui_data_color = self._options["guidata"][obj_name]["color"]
gui_data_color = self._guidata[obj_name]["color"]

obj_data["parameters"]["Color"] = (
gui_data_color if gui_data_color else default_color
)
if "visible" in self._options["guidata"][obj_name]:
gui_data_visible = self._options["guidata"][obj_name]["visible"]
if "visible" in self._guidata[obj_name]:
gui_data_visible = self._guidata[obj_name]["visible"]
obj_data["visible"] = (
gui_data_visible if gui_data_visible is not None else True
)
Expand Down Expand Up @@ -185,11 +186,8 @@ def save(self, objects: List, options: Dict, metadata: Dict) -> None:
fc_file.addObject(py_obj["shape"], py_obj["name"])
to_update = [x for x in new_objs if x in current_objs] + to_add

guidata = options.get("guidata", {})

for obj_name in to_update:
py_obj = new_objs[obj_name]

fc_obj = fc_file.getObject(py_obj["name"])

for prop, jcad_prop_value in py_obj["parameters"].items():
Expand All @@ -216,21 +214,20 @@ def save(self, objects: List, options: Dict, metadata: Dict) -> None:
f"Property '{prop}' does not exist on object '{fc_obj.Name}' and is not handled"
)

# Handle updating the color in guidata
if "Color" in py_obj["parameters"]:
new_hex_color = py_obj["parameters"]["Color"]
else:
new_hex_color = "#808080" # Default to gray if no color is provided

if obj_name in guidata:
guidata[obj_name]["color"] = new_hex_color
if obj_name in self._guidata:
self._guidata[obj_name]["color"] = new_hex_color
else:
guidata[obj_name] = {"color": new_hex_color}

options["guidata"] = guidata
self._guidata[obj_name] = {"color": new_hex_color}

OfflineRenderingUtils.save(
fc_file,
guidata=_options_to_guidata(guidata),
guidata=_options_to_guidata(self._guidata),
)

fc_file.recompute()
Expand Down

0 comments on commit 01d3734

Please sign in to comment.