| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | import json |
| | import Path |
| | from typing import Mapping, List, Optional, cast |
| | import FreeCAD |
| | from ...assets import Asset, AssetUri, AssetSerializer |
| | from ...shape import ToolBitShape |
| | from ..models.base import ToolBit |
| |
|
| |
|
| | if False: |
| | Path.Log.setLevel(Path.Log.Level.DEBUG, Path.Log.thisModule()) |
| | Path.Log.trackModule(Path.Log.thisModule()) |
| | else: |
| | Path.Log.setLevel(Path.Log.Level.INFO, Path.Log.thisModule()) |
| |
|
| |
|
| | class FCTBSerializer(AssetSerializer): |
| | for_class = ToolBit |
| | mime_type = "application/x-freecad-toolbit" |
| | extensions = (".fctb",) |
| |
|
| | @classmethod |
| | def get_label(cls) -> str: |
| | return FreeCAD.Qt.translate("CAM", "FreeCAD Tool") |
| |
|
| | @classmethod |
| | def extract_dependencies(cls, data: bytes) -> List[AssetUri]: |
| | """Extracts URIs of dependencies from serialized data.""" |
| | Path.Log.debug(f"FCTBSerializer.extract_dependencies: raw data = {data!r}") |
| | data_dict = json.loads(data.decode("utf-8")) |
| | shape = data_dict["shape"] |
| | return [ToolBitShape.resolve_name(shape)] |
| |
|
| | @classmethod |
| | def serialize(cls, asset: Asset) -> bytes: |
| | |
| | if not isinstance(asset, ToolBit): |
| | raise TypeError(f"Expected ToolBit instance, got {type(asset).__name__}") |
| | attrs = asset.to_dict() |
| | return json.dumps(attrs, sort_keys=True, indent=2).encode("utf-8") |
| |
|
| | @classmethod |
| | def deserialize( |
| | cls, |
| | data: bytes, |
| | id: str, |
| | dependencies: Optional[Mapping[AssetUri, Asset]], |
| | ) -> ToolBit: |
| | """ |
| | Creates a ToolBit instance from serialized data and resolved |
| | dependencies. |
| | """ |
| | attrs = json.loads(data.decode("utf-8", "ignore")) |
| | attrs["id"] = id |
| |
|
| | if dependencies is None: |
| | |
| | |
| | Path.Log.debug(f"FCTBSerializer.deserialize: shallow. id = {id!r}, attrs = {attrs!r}") |
| | return ToolBit.from_dict(attrs, shallow=True) |
| |
|
| | |
| | |
| | shape_id = attrs.get("shape") |
| | if not shape_id: |
| | Path.Log.warning("ToolBit data is missing 'shape' key, defaulting to 'endmill'") |
| | shape_id = "endmill" |
| |
|
| | shape_uri = ToolBitShape.resolve_name(shape_id) |
| | shape = dependencies.get(shape_uri) |
| |
|
| | if shape is None: |
| | raise ValueError( |
| | f"Dependency for shape '{shape_id}' not found by uri {shape_uri}" f" {dependencies}" |
| | ) |
| | elif not isinstance(shape, ToolBitShape): |
| | raise ValueError( |
| | f"Dependency for shape '{shape_id}' found by uri {shape_uri} " |
| | f"is not a ToolBitShape instance. {dependencies}" |
| | ) |
| |
|
| | return ToolBit.from_shape(shape, attrs, id) |
| |
|
| | @classmethod |
| | def deep_deserialize(cls, data: bytes) -> ToolBit: |
| | """Deep deserialize preserving the original toolbit ID.""" |
| |
|
| | attrs_map = json.loads(data) |
| | original_id = attrs_map.get("id") |
| |
|
| | asset_class = cast(ToolBit, cls.for_class) |
| | toolbit = asset_class.from_dict(attrs_map) |
| |
|
| | if original_id: |
| | toolbit.id = original_id |
| |
|
| | return toolbit |
| |
|