File size: 7,412 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | # SPDX-License-Identifier: LGPL-2.1-or-later
# ***************************************************************************
# * Copyright (c) 2025 Samuel Abels <knipknap@gmail.com> *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU Lesser General Public License (LGPL) *
# * as published by the Free Software Foundation; either version 2 of *
# * the License, or (at your option) any later version. *
# * for detail see the LICENCE text file. *
# * *
# * This program is distributed in the hope that it will be useful, *
# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
# * GNU Library General Public License for more details. *
# * *
# * You should have received a copy of the GNU Library General Public *
# * License along with this program; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
# * USA *
# * *
# ***************************************************************************
import uuid
import json
from typing import Mapping, List, Optional, Type
import FreeCAD
from ...assets import Asset, AssetUri, AssetSerializer
from ...toolbit import ToolBit
from ...toolbit.mixins import RotaryToolBitMixin
from ...shape import ToolBitShape, ToolBitShapeEndmill
from ..models.library import Library
SHAPEMAP = {
"ballend": "Ballnose",
"endmill": "Cylindrical",
"v-bit": "Conical",
"vbit": "Conical",
"chamfer": "Snubnose",
}
SHAPEMAP_REVERSE = dict((v, k) for k, v in SHAPEMAP.items())
tooltemplate = {
"units": "metric",
"shape": "Cylindrical",
"length": 10,
"diameter": 3.125,
"description": "",
}
class CamoticsLibrarySerializer(AssetSerializer):
for_class: Type[Asset] = Library
extensions: tuple[str] = (".json",)
mime_type: str = "application/json"
@classmethod
def get_label(cls) -> str:
return FreeCAD.Qt.translate("CAM", "Camotics Tool Library")
@classmethod
def extract_dependencies(cls, data: bytes) -> List[AssetUri]:
return []
@classmethod
def serialize(cls, asset: Asset) -> bytes:
if not isinstance(asset, Library):
raise TypeError("Asset must be a Library instance")
toollist = {}
for tool_no, tool in asset._bit_nos.items():
assert isinstance(tool, RotaryToolBitMixin)
toolitem = tooltemplate.copy()
diameter_value = tool.get_diameter()
# Ensure diameter is a float, handle Quantity and other types
diameter_serializable = 2.0 # Default value as float
if isinstance(diameter_value, FreeCAD.Units.Quantity):
try:
val_mm = diameter_value.getValueAs("mm")
if val_mm is not None:
diameter_serializable = float(val_mm)
except ValueError:
# Fallback to raw value if unit conversion fails
raw_val = diameter_value.Value if hasattr(diameter_value, "Value") else None
if isinstance(raw_val, (int, float)):
diameter_serializable = float(raw_val)
elif isinstance(diameter_value, (int, float)):
diameter_serializable = float(diameter_value) if diameter_value is not None else 2.0
toolitem["diameter"] = diameter_serializable
toolitem["description"] = tool.label
length_value = tool.get_length()
# Ensure length is a float, handle Quantity and other types
length_serializable = 10.0 # Default value as float
if isinstance(length_value, FreeCAD.Units.Quantity):
try:
val_mm = length_value.getValueAs("mm")
if val_mm is not None:
length_serializable = float(val_mm)
except ValueError:
# Fallback to raw value if unit conversion fails
raw_val = length_value.Value if hasattr(length_value, "Value") else None
if isinstance(raw_val, (int, float)):
length_serializable = float(raw_val)
elif isinstance(length_value, (int, float)):
length_serializable = float(length_value) if length_value is not None else 10.0
toolitem["length"] = length_serializable
toolitem["shape"] = SHAPEMAP.get(tool._tool_bit_shape.name.lower(), "Cylindrical")
toollist[str(tool_no)] = toolitem
return json.dumps(toollist, indent=2).encode("utf-8")
@classmethod
def deserialize(
cls,
data: bytes,
id: str,
dependencies: Optional[Mapping[AssetUri, Asset]],
) -> Library:
try:
data_dict = json.loads(data.decode("utf-8"))
except json.JSONDecodeError as e:
raise ValueError(f"Failed to decode JSON data: {e}") from e
library = Library(id, id=id)
for tool_no_str, toolitem in data_dict.items():
try:
tool_no = int(tool_no_str)
except ValueError:
print(f"Warning: Skipping invalid tool number: {tool_no_str}")
continue
# Find the shape class to use
shape_name_str = SHAPEMAP_REVERSE.get(toolitem.get("shape", "Cylindrical"), "endmill")
shape_class = ToolBitShape.get_subclass_by_name(shape_name_str)
if not shape_class:
print(f"Warning: Unknown shape name '{shape_name_str}', defaulting to endmill")
shape_class = ToolBitShapeEndmill
# Translate parameters to FreeCAD types
params = {}
try:
diameter = float(toolitem.get("diameter", 2))
params["Diameter"] = FreeCAD.Units.Quantity(f"{diameter} mm")
except (ValueError, TypeError):
print(f"Warning: Invalid diameter for tool {tool_no_str}, skipping.")
try:
length = float(toolitem.get("length", 10))
params["Length"] = FreeCAD.Units.Quantity(f"{length} mm")
except (ValueError, TypeError):
print(f"Warning: Invalid length for tool {tool_no_str}, skipping.")
# Create the shape
shape_id = shape_name_str.lower()
tool_bit_shape = shape_class(shape_id, **params)
# Create the toolbit
tool = ToolBit(tool_bit_shape, id=f"camotics_tool_{tool_no_str}")
tool.label = toolitem.get("description", "")
library.add_bit(tool, tool_no)
return library
@classmethod
def deep_deserialize(cls, data: bytes) -> Library:
# TODO: Build tools here
return cls.deserialize(data, str(uuid.uuid4()), {})
|