From aec27952f88eb1eb127eb97cfde185a936d68ada Mon Sep 17 00:00:00 2001 From: Chen Kasirer Date: Wed, 24 Jul 2024 17:42:33 +0200 Subject: [PATCH] added parameters class --- src/compas_timber/_fabrication/__init__.py | 3 +- .../_fabrication/btlx_process.py | 65 +++++++++++++++++-- src/compas_timber/_fabrication/jack_cut.py | 40 ++++++++++++ tests/compas_timber/test_btlx.py | 19 ++++++ 4 files changed, 120 insertions(+), 7 deletions(-) diff --git a/src/compas_timber/_fabrication/__init__.py b/src/compas_timber/_fabrication/__init__.py index 2f1beedbe..6ee04ee8c 100644 --- a/src/compas_timber/_fabrication/__init__.py +++ b/src/compas_timber/_fabrication/__init__.py @@ -3,6 +3,7 @@ from .btlx_process import BTLxProcess from .btlx_process import OrientationType from .jack_cut import JackRafterCut +from .jack_cut import JackRafterCutParams -__all__ = ["JackRafterCut", "BTLxProcess", "OrientationType"] +__all__ = ["JackRafterCut", "BTLxProcess", "OrientationType", "JackRafterCutParams"] diff --git a/src/compas_timber/_fabrication/btlx_process.py b/src/compas_timber/_fabrication/btlx_process.py index bd2f16b1b..d8673a300 100644 --- a/src/compas_timber/_fabrication/btlx_process.py +++ b/src/compas_timber/_fabrication/btlx_process.py @@ -1,3 +1,5 @@ +from collections import OrderedDict + from compas.data import Data @@ -8,15 +10,66 @@ class BTLxProcess(Data): ---------- ref_side_index : int The reference side, zero-based, index of the beam to be cut. 0-5 correspond to RS1-RS6. + priority : int + The priority of the process. + process_id : int + The process ID. + PROCESS_NAME : str + The name of the process. + """ @property def __data__(self): - return {"ref_side_index": self.ref_side_index} + return {"ref_side_index": self.ref_side_index, "priority": self.priority, "process_id": self.process_id} - def __init__(self, ref_side_index): + def __init__(self, ref_side_index, priority=0, process_id=0): super(BTLxProcess, self).__init__() self.ref_side_index = ref_side_index + self._priority = priority + self._process_id = process_id + + @property + def priority(self): + return self._priority + + @property + def process_id(self): + return self._process_id + + @property + def PROCESS_NAME(self): + raise NotImplementedError("PROCESS_NAME must be implemented as class attribute in subclasses!") + + +class BTLxProcessParams(object): + """Base class for BTLx process parameters. This creates the dictionary of key-value pairs for the process as expected by the BTLx file format. + + Parameters + ---------- + instance : :class:`BTLxProcess` + The instance of the process to create parameters for. + + """ + + def __init__(self, instance): + self._instance = instance + + def as_dict(self): + """Returns the process parameters as a dictionary. + + Returns + ------- + dict + The process parameters as a dictionary. + """ + result = OrderedDict() + result["Name"] = self._instance.PROCESS_NAME + result["Process"] = "yes" + result["Priority"] = str(self._instance.priority) + result["ProcessID"] = str(self._instance.process_id) + result["ReferencePlaneID"] = str(self._instance.ref_side_index + 1) + return result class OrientationType(object): @@ -24,11 +77,11 @@ class OrientationType(object): Attributes ---------- - START : int + START : literal("start") The start of the beam is cut away. - END : int + END : literal("end") The end of the beam is cut away. """ - START = 0 - END = 1 + START = "start" + END = "end" diff --git a/src/compas_timber/_fabrication/jack_cut.py b/src/compas_timber/_fabrication/jack_cut.py index bf74cb755..1e9ef3237 100644 --- a/src/compas_timber/_fabrication/jack_cut.py +++ b/src/compas_timber/_fabrication/jack_cut.py @@ -10,10 +10,12 @@ from compas.geometry import distance_point_point from compas.geometry import intersection_line_plane from compas.geometry import is_point_behind_plane +from compas.tolerance import TOL from compas_timber.elements import FeatureApplicationError from .btlx_process import BTLxProcess +from .btlx_process import BTLxProcessParams from .btlx_process import OrientationType @@ -37,6 +39,8 @@ class JackRafterCut(BTLxProcess): """ + PROCESS_NAME = "JackRafterCut" # type: ignore + @property def __data__(self): data = super(JackRafterCut, self).__data__ @@ -68,6 +72,10 @@ def __init__(self, orientation, start_x=0.0, start_y=0.0, start_depth=0.0, angle # Properties ######################################################################## + @property + def params_dict(self): + return JackRafterCutParams(self).as_dict() + @property def orientation(self): return self._orientation @@ -272,3 +280,35 @@ def plane_from_params_and_beam(self, beam): else: plane_normal = -cutting_plane.xaxis return Plane(cutting_plane.point, plane_normal) + + +class JackRafterCutParams(BTLxProcessParams): + """A class to store the parameters of a Jack Rafter Cut feature. + + Parameters + ---------- + instance : :class:`~compas_timber._fabrication.JackRafterCut` + The instance of the Jack Rafter Cut feature. + """ + + def __init__(self, instance): + # type: (JackRafterCut) -> None + super(JackRafterCutParams, self).__init__(instance) + + def as_dict(self): + """Returns the parameters of the Jack Rafter Cut feature as a dictionary. + + Returns + ------- + dict + The parameters of the Jack Rafter Cut feature as a dictionary. + """ + # type: () -> OrderedDict + result = super(JackRafterCutParams, self).as_dict() + result["Orientation"] = self._instance.orientation + result["StartX"] = "{:.{prec}f}".format(self._instance.start_x, prec=TOL.precision) + result["StartY"] = "{:.{prec}f}".format(self._instance.start_y, prec=TOL.precision) + result["StartDepth"] = "{:.{prec}f}".format(self._instance.start_depth, prec=TOL.precision) + result["Angle"] = "{:.{prec}f}".format(self._instance.angle, prec=TOL.precision) + result["Inclination"] = "{:.{prec}f}".format(self._instance.inclination, prec=TOL.precision) + return result diff --git a/tests/compas_timber/test_btlx.py b/tests/compas_timber/test_btlx.py index 21180ec5c..011a1d9ed 100644 --- a/tests/compas_timber/test_btlx.py +++ b/tests/compas_timber/test_btlx.py @@ -187,3 +187,22 @@ def test_jack_rafter_cut_data(tol): assert copied_instance.angle == instance.angle assert copied_instance.inclination == instance.inclination assert copied_instance.ref_side_index == instance.ref_side_index + + +def test_jack_rafter_params_obj(): + instance = JackRafterCut(OrientationType.START, 14.23, 0.22, 42, 123.555, 95.2, ref_side_index=3) + + params = instance.params_dict + + assert params["Name"] == "JackRafterCut" + assert params["Process"] == "yes" + assert params["Priority"] == "0" + assert params["ProcessID"] == "0" + assert params["ReferencePlaneID"] == "4" + + assert params["Orientation"] == "start" + assert params["StartX"] == "14.230" + assert params["StartY"] == "0.220" + assert params["StartDepth"] == "42.000" + assert params["Angle"] == "123.555" + assert params["Inclination"] == "95.200"