Skip to content

Commit

Permalink
Handle color property for fcstd files
Browse files Browse the repository at this point in the history
  • Loading branch information
arjxn-py committed Sep 18, 2024
1 parent a4ef2f7 commit 31f2fc7
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions jupytercad_freecad/freecad/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,21 +160,35 @@ def save(self, objects: List, options: Dict, metadata: Dict) -> None:
fc_obj = fc_file.getObject(py_obj["name"])

for prop, jcad_prop_value in py_obj["parameters"].items():
prop_type = fc_obj.getTypeIdOfProperty(prop)
prop_handler = self._prop_handlers.get(prop_type, None)
if prop_handler is not None:
fc_value = prop_handler.jcad_to_fc(
jcad_prop_value,
jcad_object=objects,
fc_prop=getattr(fc_obj, prop),
fc_object=fc_obj,
fc_file=fc_file,
)
if fc_value:
if hasattr(fc_obj, prop):
prop_type = fc_obj.getTypeIdOfProperty(prop)
prop_handler = self._prop_handlers.get(prop_type, None)
if prop_handler is not None:
fc_value = prop_handler.jcad_to_fc(
jcad_prop_value,
jcad_object=objects,
fc_prop=getattr(fc_obj, prop),
fc_object=fc_obj,
fc_file=fc_file,
)
if fc_value:
try:
setattr(fc_obj, prop, fc_value)
except Exception:
pass
else:
# Add the Color property if it doesn't exist
if prop == "Color":
try:
setattr(fc_obj, prop, fc_value)
except Exception:
pass
color_value = tuple(jcad_prop_value) if isinstance(jcad_prop_value, (list, tuple)) else (0.5, 0.5, 0.5)
if all(isinstance(x, (int, float)) for x in color_value):
fc_obj.addProperty("App::PropertyColor", "Color", "Base", "A custom color property")
setattr(fc_obj, "Color", color_value)
logger.info(f"Added Color property to object '{obj_name}'.")
else:
logger.error(f"Invalid color value: {jcad_prop_value} for object '{obj_name}'")
except Exception as e:
logger.error(f"Failed to add Color property to object '{obj_name}': {e}")

OfflineRenderingUtils.save(
fc_file,
Expand Down

0 comments on commit 31f2fc7

Please sign in to comment.