|
| 1 | +""" |
| 2 | +Plugin for standard and v-slot aluminium extrusions in a variety of sizes. |
| 3 | +
|
| 4 | +Extrusion profiles are derived from Matronics ApS datasheets, eg |
| 5 | +http://www.matronics.eu/Data/Longship/Files/Products/vslot-2020_1.DXF |
| 6 | +
|
| 7 | +All used datasheets include in their description the text "Can be used for CAD programs" |
| 8 | +""" |
| 9 | + |
| 10 | +from typing import Callable, TypeVar, Union, Tuple |
| 11 | + |
| 12 | +# import importlib.resources |
| 13 | +import pkg_resources |
| 14 | +import cadquery as cq |
| 15 | +from cadquery.occ_impl import importers |
| 16 | + |
| 17 | +T = TypeVar("T", bound="Workplane") |
| 18 | + |
| 19 | + |
| 20 | +def _extrusion( |
| 21 | + size: Tuple[float, float], |
| 22 | + profile_filename: str, |
| 23 | + extrusion_name: str, |
| 24 | +) -> Callable: |
| 25 | + """ |
| 26 | + Return a function that will in turn generate an extrusion. |
| 27 | + :param size: size of the extrusion, used for centering |
| 28 | + :param profile_filename: filename of the extrusion's profile |
| 29 | + :param extrusion_name: a string that will be inserted into a docstring template and assigned |
| 30 | + to __doc__ on the returned function |
| 31 | + """ |
| 32 | + # importlib.resources is favored over pkg_resources, but not yet available in cadquery, |
| 33 | + # see https://setuptools.readthedocs.io/en/latest/pkg_resources.html |
| 34 | + # When importlib. resources is available, following code should be used instead of pkg_resources |
| 35 | + # file = ( |
| 36 | + # importlib.resources.files("plugins.extrusions") |
| 37 | + # .joinpath("profiles/") |
| 38 | + # .joinpath(profile_filename) |
| 39 | + # ) |
| 40 | + file = pkg_resources.resource_filename(__name__, "profiles/" + profile_filename) |
| 41 | + profile = importers.importDXF(file).wires() |
| 42 | + docstring = f""" |
| 43 | + Return a {extrusion_name} extrusion with the specified length |
| 44 | + :param length: length of the extrusion |
| 45 | + :type length: float > 0 |
| 46 | + :param centered: If True, the extrusion will be centered around the reference point. |
| 47 | + If False, the corner of the extrusion will be on the reference point and it will |
| 48 | + extend in the positive x, y and z directions. Can also use a 3-tuple to |
| 49 | + specify centering along each axis. |
| 50 | + """ |
| 51 | + |
| 52 | + def rv(length: float, centered: Union[bool, Tuple[bool, bool, bool]] = False) -> T: |
| 53 | + if isinstance(centered, bool): |
| 54 | + centered = (centered, centered, centered) |
| 55 | + offset = cq.Vector( |
| 56 | + 0 if centered[0] else size[0] / 2, |
| 57 | + 0 if centered[1] else size[0] / 2, |
| 58 | + -length / 2 if centered[2] else 0, |
| 59 | + ) |
| 60 | + extrusion = profile.toPending()._extrude(length).move(cq.Location(offset)) |
| 61 | + return cq.Workplane("XY").newObject([extrusion]) |
| 62 | + |
| 63 | + rv.__doc__ = docstring |
| 64 | + return rv |
| 65 | + |
| 66 | + |
| 67 | +e2020 = _extrusion([20, 20], "e2020.dxf", "2020") |
| 68 | +e2040 = _extrusion([40, 20], "e2040.dxf", "2040") |
| 69 | +e2080 = _extrusion([80, 20], "e2080.dxf", "2080") |
| 70 | +e4040 = _extrusion([40, 40], "e4040.dxf", "4040") |
| 71 | +v2020 = _extrusion([20, 20], "v2020.dxf", "2020 v-slot") |
| 72 | +v2040 = _extrusion([40, 20], "v2040.dxf", "2040 v-slot") |
| 73 | +v4040 = _extrusion([40, 40], "v4040.dxf", "4040 v-slot") |
0 commit comments