Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.8

0.11.8 / src / sketch_tools.luc

## The Sketch editor's tools, as Fusion 360 groups them in its CREATE,
## MODIFY and CONSTRAINTS menus (docs/research/FUSION-SKETCH-STUDY.md):
## each with its shortcut, its icon and whether it works yet. Tools not
## built yet show greyed in their menu, so the menus read as Fusion's do.
from luce_window.input import Key

pub class SketchTool:
    pub let name: str
    ## A single key that picks it, or "".
    pub let key: str
    pub let icon: str
    pub let ready: bool
    ## A separator above it in its menu.
    pub let group_start: bool
    ## Its kinds, in a submenu (Fusion's ▸): the row itself is no tool then.
    pub let variants: list[SketchTool]

    pub func init(self, name: str, key: str, icon: str, ready: bool, group_start: bool = false, variants: list[SketchTool] = []):
        self.name = name
        self.key = key
        self.icon = icon
        self.ready = ready
        self.group_start = group_start
        self.variants = variants

pub func menu_names() -> list[str]:
    return ["CREATE", "MODIFY", "CONSTRAINTS"]

## The tools of menu `menu` (menu_names' order).
pub func tools(menu: int) -> list[SketchTool]:
    if menu == 0:
        return [SketchTool("Line", "L", "line-tool", true), SketchTool("Midpoint Line", "", "midpoint-line", true),
                SketchTool("Rectangle", "", "rectangle-tool", true, variants = [SketchTool("2-Point Rectangle", "R", "rectangle-tool", true),
                    SketchTool("3-Point Rectangle", "", "rectangle-3-point", true), SketchTool("Center Rectangle", "", "rectangle-center", true)]),
                SketchTool("Circle", "", "circle-center", true, variants = [SketchTool("Center Diameter Circle", "C", "circle-center", true),
                    SketchTool("2-Point Circle", "", "circle-2-point", true), SketchTool("3-Point Circle", "", "circle-3-point", true),
                    SketchTool("2-Tangent Circle", "", "circle-2-tangent", true), SketchTool("3-Tangent Circle", "", "circle-3-tangent", true)]),
                SketchTool("Arc", "", "arc-3-point", true, variants = [SketchTool("3-Point Arc", "", "arc-3-point", true),
                    SketchTool("Center Point Arc", "", "arc-center", true), SketchTool("Tangent Arc", "", "arc-tangent", true)]),
                SketchTool("Polygon", "", "polygon-select-tool", true, variants = [SketchTool("Circumscribed Polygon", "", "polygon-circumscribed", true),
                    SketchTool("Inscribed Polygon", "", "polygon-inscribed", true), SketchTool("Edge Polygon", "", "polygon-edge", true)]),
                SketchTool("Ellipse", "", "ellipse-tool", true),
                SketchTool("Slot", "", "slot-center-to-center", true, variants = [SketchTool("Center to Center Slot", "", "slot-center-to-center", true),
                    SketchTool("Overall Slot", "", "slot-overall", true), SketchTool("Center Point Slot", "", "slot-center-point", true),
                    SketchTool("Three Point Arc Slot", "", "slot-arc-3-point", true), SketchTool("Center Point Arc Slot", "", "slot-arc-center", true)]),
                SketchTool("Spline", "", "ep-curve", true, variants = [SketchTool("Fit Point Spline", "", "ep-curve", true), SketchTool("Control Point Spline", "", "cv-curve", true)]),
                SketchTool("Conic Curve", "", "conic-curve", true),
                SketchTool("Point", "P", "sketch-point", true), SketchTool("Text", "", "text-tool", true),
                SketchTool("Mirror", "", "mirror-modifier", true, true), SketchTool("Circular Pattern", "", "pattern-circular", true),
                SketchTool("Rectangular Pattern", "", "pattern-rectangular", true),
                SketchTool("Project / Include", "", "project-geometry", true, true, variants = [SketchTool("Project", "", "project-geometry", true), SketchTool("Intersect", "", "sketch-intersect", false),
                    SketchTool("Include 3D Geometry", "", "include-3d", false), SketchTool("Project To Surface", "", "project-curve", false)]),
                SketchTool("Sketch Dimension", "", "sketch-dimension", true, true)]
    if menu == 1:
        return [SketchTool("Fillet", "", "sketch-fillet", true), SketchTool("Chamfer", "", "sketch-chamfer", true), SketchTool("Blend Curve", "", "blend-curve", true, variants = [SketchTool("Blend Curve G1", "", "blend-curve", true),
                    SketchTool("Blend Curve G2", "", "blend-curve", true), SketchTool("Blend Curve G3", "", "blend-curve", true)]),
                SketchTool("Offset", "O", "offset-curve", true), SketchTool("Trim", "T", "sketch-trim", true, true), SketchTool("Extend", "", "sketch-extend", true),
                SketchTool("Break", "", "sketch-break", true), SketchTool("Sketch Scale", "", "scale-tool", true),
                SketchTool("Move/Copy", "M", "move-tool", true), SketchTool("Change Parameters", "", "change-parameters", true, true)]
    return [SketchTool("Horizontal/Vertical", "", "constraint-horizontal-vertical", true), SketchTool("Coincident", "", "constraint-coincident", true),
            SketchTool("Tangent", "", "constraint-tangent", true), SketchTool("Equal", "", "constraint-equal", true),
            SketchTool("Parallel", "", "constraint-parallel", true), SketchTool("Perpendicular", "", "constraint-perpendicular", true),
            SketchTool("Fix/UnFix", "", "lock", true), SketchTool("MidPoint", "", "constraint-midpoint", true),
            SketchTool("Concentric", "", "constraint-concentric", true), SketchTool("Collinear", "", "constraint-collinear", true),
            SketchTool("Symmetry", "", "constraint-symmetry", true), SketchTool("Curvature", "", "constraint-curvature", true),
            SketchTool("Polygon", "", "constraint-polygon", false)]

## The tool a key picks, or "".
pub func tool_for_key(key: str) -> str:
    for menu in 0..<3:
        for tool in tools(menu) + variants_of(menu):
            if tool.key != "" and tool.key == key and tool.ready:
                return tool.name
    return ""

## Every variant of menu `menu`'s tools.
func variants_of(menu: int) -> list[SketchTool]:
    let found: list[SketchTool] = []
    for tool in tools(menu):
        for variant in tool.variants:
            found.append(variant)
    return found

## Whether `name` draws (a CREATE tool placing geometry click by click).
pub func draws(name: str) -> bool:
    return name in ["Line", "Midpoint Line", "2-Point Rectangle", "3-Point Rectangle", "Center Rectangle", "Center Diameter Circle", "2-Point Circle",
                    "3-Point Circle", "2-Tangent Circle", "3-Tangent Circle", "3-Point Arc", "Center Point Arc", "Tangent Arc", "Circumscribed Polygon",
                    "Inscribed Polygon", "Edge Polygon", "Center to Center Slot", "Overall Slot", "Center Point Slot", "Three Point Arc Slot",
                    "Center Point Arc Slot", "Fit Point Spline", "Control Point Spline", "Ellipse", "Conic Curve", "Text", "Point"]

## Whether `name` places points as it is clicked (snapping where it
## does): the drawing tools, Move/Copy, Sketch Scale and the Patterns.
pub func places(name: str) -> bool:
    return draws(name) or name in ["Move/Copy", "Sketch Scale", "Circular Pattern", "Rectangular Pattern"]

## Whether `name` picks curves only (not the points on them).
pub func picks_curves(name: str) -> bool:
    return name in ["Trim", "Extend", "Break", "Offset"] or name.starts_with("Blend Curve")

## Whether `name` takes as many clicks as it is given (Enter, or the last
## point again, ends it).
pub func open_ended(name: str) -> bool:
    return name in ["Fit Point Spline", "Control Point Spline"]

## Whether `name` copies the selection (Mirror and the Patterns).
pub func copies(name: str) -> bool:
    return name in ["Mirror", "Circular Pattern", "Rectangular Pattern"]

## How many clicks drawing tool `name` takes.
pub func clicks_for(name: str) -> int:
    if name == "Point":
        return 1
    if name in ["Three Point Arc Slot", "Center Point Arc Slot"]:
        return 4
    if name in ["3-Point Rectangle", "3-Point Circle", "2-Tangent Circle", "3-Tangent Circle", "3-Point Arc", "Center Point Arc", "Center to Center Slot",
                "Overall Slot", "Center Point Slot", "Ellipse", "Conic Curve"]:
        return 3
    return 2

## Whether `name` is a MODIFY tool.
pub func modifies(name: str) -> bool:
    return in_menu(name, 1)

## Whether `name` is a constraint tool.
pub func constrains(name: str) -> bool:
    return in_menu(name, 2)

## Whether tool `name` is in menu `menu` (or a submenu of it).
pub func in_menu(name: str, menu: int) -> bool:
    for tool in tools(menu):
        if tool.name == name:
            return true
        for variant in tool.variants:
            if variant.name == name:
                return true
    return false

## Tool `name`'s icon, or "" when it is no sketch tool.
pub func icon_of(name: str) -> str:
    for menu in 0..<3:
        for tool in tools(menu) + variants_of(menu):
            if tool.name == name:
                return tool.icon
    return ""

## The status line's hint for tool `tool` in the Sketch editor.
pub func hint(tool: str) -> str:
    if tool == "Select":
        return "Sketch: hold A, S or D for its tools, or drag points (Delete removes, X makes construction)"
    if tool == "Project":
        return "Project: click an edge of the input body, or inside a face for all its loops (they follow the body); right-click: Project body for its outline"
    if draws(tool):
        return f"{tool}: click to place (Cmd places freely), Esc ends"
    if tool in ["Trim", "Extend", "Break"]:
        return f"{tool}: click a curve where it should change"
    if tool in ["Fillet", "Chamfer"]:
        return f"{tool}: click two lines meeting at a corner, or the corner"
    if tool == "Offset":
        return "Offset: click a curve (its chain goes), then to its side"
    if tool == "Move/Copy":
        return "Move/Copy: pick what moves, then click from and to (Shift copies)"
    if tool == "Sketch Scale":
        return "Sketch Scale: pick what scales, then click the base point, a point, and where it goes"
    if tool == "Sketch Dimension":
        return "Sketch Dimension: click a line, circle or arc, or two entities, then click where the dimension goes"
    return f"{tool}: pick the entities to constrain, the reference first"

## The letter key `key` types, upper case, or "".
pub func key_letter(key: Key) -> str:
    let keys = [Key.a, Key.b, Key.c, Key.d, Key.e, Key.f, Key.g, Key.h, Key.i, Key.j, Key.k, Key.l, Key.m, Key.n, Key.o, Key.p, Key.q, Key.r, Key.s, Key.t, Key.u, Key.v, Key.w, Key.x, Key.y, Key.z]
    let letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    for at in 0..<keys.length:
        if keys[at] == key:
            return letters[at..<at + 1]
    return ""