from __future__ import annotations
import math
import FreeCAD
FEATURE = "axis"
BACKEND = "ferrocad"
PREFERENCES = "User parameter:BaseApp/Preferences/Mods/%s/%s" % (FEATURE, BACKEND)
LEGACY_GRID_PREFERENCES = "User parameter:BaseApp/Preferences/Mods/grid/ferrocad"
LEGACY_PREFERENCES = "User parameter:BaseApp/Preferences/View"
DEFAULT_ENABLED = True
DEFAULT_COLOR_X = "#f87171"
DEFAULT_COLOR_Y = "#4ade80"
DEFAULT_COLOR_Z = "#38bdf8"
DEFAULT_OPACITY = 0.65
DEFAULT_LENGTH = 80.0
DEFAULT_WIDTH = 1.5
DEFAULT_ORIGIN = True
DEFAULT_LABELS = True
DEFAULT_GHOST_ALPHA = 0.10
SETTINGS_SCHEMA = (
{
"name": "enabled",
"label": "Show axis cross",
"kind": "bool",
"default": DEFAULT_ENABLED,
},
{
"name": "color_x",
"label": "X axis colour",
"kind": "color",
"default": DEFAULT_COLOR_X,
},
{
"name": "color_y",
"label": "Y axis colour",
"kind": "color",
"default": DEFAULT_COLOR_Y,
},
{
"name": "color_z",
"label": "Z axis colour",
"kind": "color",
"default": DEFAULT_COLOR_Z,
},
{
"name": "opacity",
"label": "Opacity",
"kind": "float",
"default": DEFAULT_OPACITY,
"min": 0.0,
"max": 1.0,
},
{
"name": "length",
"label": "Stem length (px)",
"kind": "float",
"default": DEFAULT_LENGTH,
"min": 8.0,
"max": 400.0,
},
{
"name": "width",
"label": "Stem width (px)",
"kind": "float",
"default": DEFAULT_WIDTH,
"min": 0.5,
"max": 6.0,
},
{
"name": "origin",
"label": "Origin node",
"kind": "bool",
"default": DEFAULT_ORIGIN,
},
{
"name": "labels",
"label": "Axis labels",
"kind": "bool",
"default": DEFAULT_LABELS,
},
{
"name": "ghost_alpha",
"label": "Occluded alpha",
"kind": "float",
"default": DEFAULT_GHOST_ALPHA,
"min": 0.0,
"max": 1.0,
},
)
AXIS_SETTINGS_SCHEMA = SETTINGS_SCHEMA
_SPECS = {entry["name"]: entry for entry in SETTINGS_SCHEMA}
_HEX_DIGITS = "0123456789abcdefABCDEF"
def _prefs():
return FreeCAD.ParamGet(PREFERENCES)
def _legacy_grid_prefs():
return FreeCAD.ParamGet(LEGACY_GRID_PREFERENCES)
def _legacy_view_prefs():
return FreeCAD.ParamGet(LEGACY_PREFERENCES)
def _present(group, name: str, kind: str) -> bool:
if kind == "bool":
return name in group.GetBools()
if kind in ("float", "quantity"):
return name in group.GetFloats()
return name in group.GetStrings()
def _store(group, spec: dict, value) -> None:
name = spec["name"]
kind = spec["kind"]
if kind == "bool":
group.SetBool(name, bool(value))
elif kind in ("float", "quantity"):
group.SetFloat(name, float(value))
else:
group.SetString(name, str(value))
def _migrate_enabled(group) -> None:
spec = _SPECS["enabled"]
if _present(group, "enabled", "bool"):
return
value = DEFAULT_ENABLED
for legacy in (_legacy_grid_prefs(), _legacy_view_prefs()):
if _present(legacy, "GridAxisCross", "bool"):
value = legacy.GetBool("GridAxisCross", DEFAULT_ENABLED)
break
_store(group, spec, value)
def _read(spec: dict):
group = _prefs()
if spec["name"] == "enabled":
_migrate_enabled(group)
name = spec["name"]
kind = spec["kind"]
default = spec["default"]
if kind == "bool":
return group.GetBool(name, bool(default))
if kind in ("float", "quantity"):
return group.GetFloat(name, float(default))
return group.GetString(name, str(default))
def _write(spec: dict, value) -> None:
_store(_prefs(), spec, value)
def _colour(value) -> str:
if not isinstance(value, str):
raise ValueError("an axis colour must be a '#rrggbb' string, got %r" % (value,))
text = value.strip()
if not text.startswith("#"):
raise ValueError("an axis colour must start with '#', got %r" % (text,))
digits = text[1:]
if len(digits) == 3:
digits = "".join(digit * 2 for digit in digits)
elif len(digits) != 6:
raise ValueError("an axis colour must be #rgb or #rrggbb, got %r" % (text,))
if any(digit not in _HEX_DIGITS for digit in digits):
raise ValueError("an axis colour has a non-hex digit, got %r" % (text,))
return "#" + digits.lower()
def _number(value, name: str, *, minimum=0.0, maximum=None) -> float:
try:
number = float(value)
except (TypeError, ValueError):
raise ValueError("%s must be a number, got %r" % (name, value)) from None
if not math.isfinite(number):
raise ValueError("%s must be finite, got %r" % (name, value))
if number < minimum:
raise ValueError("%s must be at least %s, got %r" % (name, minimum, value))
if maximum is not None and number > maximum:
raise ValueError("%s must be at most %s, got %r" % (name, maximum, value))
return number
def _measured(spec: dict, value, name: str) -> float:
return _number(
value,
name,
minimum=float(spec.get("min", 0.0)),
maximum=None if "max" not in spec else float(spec["max"]),
)
class _AxisSettings:
def schema(self) -> list:
return [dict(entry) for entry in SETTINGS_SCHEMA]
@property
def enabled(self) -> bool:
return bool(_read(_SPECS["enabled"]))
@enabled.setter
def enabled(self, value) -> None:
_write(_SPECS["enabled"], bool(value))
@property
def color_x(self) -> str:
return _colour(_read(_SPECS["color_x"]))
@color_x.setter
def color_x(self, value) -> None:
_write(_SPECS["color_x"], _colour(value))
@property
def color_y(self) -> str:
return _colour(_read(_SPECS["color_y"]))
@color_y.setter
def color_y(self, value) -> None:
_write(_SPECS["color_y"], _colour(value))
@property
def color_z(self) -> str:
return _colour(_read(_SPECS["color_z"]))
@color_z.setter
def color_z(self, value) -> None:
_write(_SPECS["color_z"], _colour(value))
@property
def opacity(self) -> float:
return _measured(_SPECS["opacity"], _read(_SPECS["opacity"]), "the axis opacity")
@opacity.setter
def opacity(self, value) -> None:
_write(
_SPECS["opacity"],
_measured(_SPECS["opacity"], value, "the axis opacity"),
)
@property
def length(self) -> float:
return _measured(_SPECS["length"], _read(_SPECS["length"]), "the axis length")
@length.setter
def length(self, value) -> None:
_write(
_SPECS["length"],
_measured(_SPECS["length"], value, "the axis length"),
)
@property
def width(self) -> float:
return _measured(_SPECS["width"], _read(_SPECS["width"]), "the axis width")
@width.setter
def width(self, value) -> None:
_write(
_SPECS["width"],
_measured(_SPECS["width"], value, "the axis width"),
)
@property
def origin(self) -> bool:
return bool(_read(_SPECS["origin"]))
@origin.setter
def origin(self, value) -> None:
_write(_SPECS["origin"], bool(value))
@property
def labels(self) -> bool:
return bool(_read(_SPECS["labels"]))
@labels.setter
def labels(self, value) -> None:
_write(_SPECS["labels"], bool(value))
@property
def ghost_alpha(self) -> float:
return _measured(
_SPECS["ghost_alpha"],
_read(_SPECS["ghost_alpha"]),
"the axis ghost alpha",
)
@ghost_alpha.setter
def ghost_alpha(self, value) -> None:
_write(
_SPECS["ghost_alpha"],
_measured(_SPECS["ghost_alpha"], value, "the axis ghost alpha"),
)
def apply(self) -> dict:
return {
"view": {
"axis": {
"enabled": self.enabled,
"color_x": self.color_x,
"color_y": self.color_y,
"color_z": self.color_z,
"opacity": self.opacity,
"length": self.length,
"width": self.width,
"origin": self.origin,
"labels": self.labels,
"ghost_alpha": self.ghost_alpha,
}
}
}
def snapshot(self) -> dict:
return {
"enabled": self.enabled,
"color_x": self.color_x,
"color_y": self.color_y,
"color_z": self.color_z,
"opacity": self.opacity,
"length": self.length,
"width": self.width,
"origin": self.origin,
"labels": self.labels,
"ghost_alpha": self.ghost_alpha,
}
def reset(self) -> None:
_prefs().Clear()
_INSTANCE = None
def axis() -> _AxisSettings:
global _INSTANCE
if _INSTANCE is None:
_INSTANCE = _AxisSettings()
return _INSTANCE