diff --git a/CHANGELOG.md b/CHANGELOG.md index a64b573c9..52990a8cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -103,9 +103,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - u_iso/v_iso: Returns the u-iso/v-iso curve of the surface. - Plane3D/CylindricalSurface/ConicalSurface/SphericalSurface3D : normal_at_point +#### core_compiled +- Add custom Schemas for Vectors & Points + #### shells.py - first version boolean operations with any faces. + #### global - Add reference_path to a handful of classes diff --git a/volmdlr/__init__.py b/volmdlr/__init__.py index 27aa345eb..a669bc99d 100644 --- a/volmdlr/__init__.py +++ b/volmdlr/__init__.py @@ -13,7 +13,7 @@ linesegment2d_point_distance, LineSegment3DDistance, linesegment3d_point_distance, get_minimum_distance_points_lines, Matrix22, Matrix33, Point2D, Point3D, Vector2D, - Vector3D) + Vector3D, Vector) TWO_PI = 2 * math.pi diff --git a/volmdlr/core.py b/volmdlr/core.py index faaafb10f..c1d992047 100644 --- a/volmdlr/core.py +++ b/volmdlr/core.py @@ -13,6 +13,7 @@ from numpy.typing import NDArray import dessia_common.core as dc +from dessia_common.schemas.core import SchemaAttribute, Schema import volmdlr import volmdlr.templates @@ -999,3 +1000,55 @@ def __init__(self, primitives: List[Primitive3D], name: str = ''): def __getattr__(self, name): return getattr(self.instance, name) + + +class VectorSchema(Schema): + def __init__(self, class_, annotations, attributes): + self.classname = f"{class_.__module__}.{class_.__name__}" + + super().__init__(annotations=annotations, attributes=attributes) + + def to_dict(self, **kwargs): + dict_ = super().to_dict() + dict_.update({"standalone_in_db": False, "classes": [self.classname]}) + return dict_ + + +class Vector2Schema(VectorSchema): + def __init__(self, class_): + annotations = {"x": float, "y": float, "name": str} + attributes = [SchemaAttribute("x"), SchemaAttribute("y"), SchemaAttribute("name", default_value="")] + super().__init__(class_=class_, annotations=annotations, attributes=attributes) + + +class Vector3Schema(VectorSchema): + def __init__(self, class_): + annotations = {"x": float, "y": float, "z": float, "name": str} + attributes = [SchemaAttribute("x"), SchemaAttribute("y"), + SchemaAttribute("z"), SchemaAttribute("name", default_value="")] + super().__init__(class_=class_, annotations=annotations, attributes=attributes) + + +class VectorBlueprint(volmdlr.Vector): + @classmethod + def raw_schema(cls): + raise NotImplementedError("Cannot compute schema for base class 'Vector'. Should be one of its children class") + + @classmethod + def schema(cls): + """ Write raw schema as a dict in order to make it jsonable. """ + return cls.raw_schema().to_dict() + + +class Vector2DBlueprint(volmdlr.Vector2D): + @classmethod + def raw_schema(cls): + """ Custom Schema implementation in order to allow vectors and points to be used in forms. """ + return Vector2Schema(cls) + + +class Vector3DBlueprint(volmdlr.Vector3D): + @classmethod + def raw_schema(cls): + """ Custom Schema implementation in order to allow vectors and points to be used in forms. """ + return Vector3Schema(cls)