From 3ba7ef3f58d1d38fd4ba564500ebee83aa045e6f Mon Sep 17 00:00:00 2001 From: RazorSh4rk Date: Mon, 2 Nov 2026 09:44:30 -0800 Subject: [PATCH 1/3] Add hex nut hole tool Add a Hex Nut Hole command that cuts a hexagonal recess at the end of a PartDesign Hole feature. Supports a radius, recess depth, entry or opposite face, and a 30 degree rotation. --- Documentation/README.md | 1 + Documentation/ffDesign_HexNut.md | 62 ++++ Documentation/ffDesign_HoleWizard.md | 1 + InitGui.py | 1 + README.md | 8 +- Resources/icons/ffDesign_HexNut.svg | 377 ++++++++++++++++++++++++ Resources/panels/ffDesign_HexNut.ui | 98 ++++++ Resources/panels/ffDesign_HoleWizard.ui | 31 +- ffDesign_HexNut.py | 265 +++++++++++++++++ ffDesign_HoleWizard.py | 6 + ffDesign_Utils.py | 5 + 11 files changed, 851 insertions(+), 4 deletions(-) create mode 100644 Documentation/ffDesign_HexNut.md create mode 100644 Resources/icons/ffDesign_HexNut.svg create mode 100644 Resources/panels/ffDesign_HexNut.ui create mode 100644 ffDesign_HexNut.py diff --git a/Documentation/README.md b/Documentation/README.md index 0a89ff4..c8704b9 100644 --- a/Documentation/README.md +++ b/Documentation/README.md @@ -8,6 +8,7 @@ This is the documentation for the tools included in the _FusedFilamentDesign_ ad | | ![ffDesign_RibThreads](../Resources/icons/ffDesign_RibThreads.svg) | [**Thread Forming Ribs**](./ffDesign_RibThreads.md) | Hole geometry for rib thread forming | | | ![ffDesign_Teardrop](../Resources/icons/ffDesign_Teardrop.svg) | [**Teardrop Shape**](./ffDesign_Teardrop.md) | Teardrop-shaped holes for better horizontal holes and to avoid seam inaccuracy | | | ![ffDesign_RoofBridge](../Resources/icons/ffDesign_RoofBridge.svg) | [**Roof Bridge**](./ffDesign_RoofBridge.md) | Roof bridges for horizontal holes | +| | ![ffDesign_HexNut](../Resources/icons/ffDesign_HexNut.svg) | [**Hex Nut Hole**](./ffDesign_HexNut.md) | Hexagonal recess at the end of a hole to capture a hex nut | | ![ffDesign_ZipTieChannels](../Resources/icons/ffDesign_ZipTieChannels.svg) | | [**Zip Tie Channels**](./ffDesign_ZipTieChannels.md) | Generate parametric zip tie channels on a part's surface | | ![ffDesign_AutoFillet](../Resources/icons/ffDesign_AutoFillet.svg) | | [**Auto Fillet**](./ffDesign_AutoFillet.md) | Automatically add fillets to vertical edges | diff --git a/Documentation/ffDesign_HexNut.md b/Documentation/ffDesign_HexNut.md new file mode 100644 index 0000000..1bbc7a9 --- /dev/null +++ b/Documentation/ffDesign_HexNut.md @@ -0,0 +1,62 @@ +![ffDesign_HexNut](../Resources/icons/ffDesign_HexNut.svg) +## Command: Hex Nut Hole +This command cuts a hexagonal recess at one end of a hole, so that a hex nut +can be captured inside the part. This is useful for embedding nuts in printed +parts without a press fit. + +## Prerequisites +- A [PartDesign Hole][pd-hole] feature must be selected or the tip of the + active body must be a [PartDesign Hole][pd-hole]. + +## Usage +Run this command to generate a hex nut recess for the selected Hole feature. A +dialog will open in the [Task Panel][task-panel] where you can control the +generation. + +- **Hex Nut Radius**: The radius from the hole center to a corner of the hex + nut. This is the radius of the circle around the hexagon, not the distance + across the flats. +- **Hex Hole Depth**: The depth of the hexagonal recess, measured from the + selected face. +- **Start on the opposite face**: When checked, the recess starts on the + opposite face of the part instead of on the hole entry face. The tool + determines the location of this face from the shape of the part. +- **Rotate by 30 degrees**: When checked, the hexagon turns + by 30 degrees. A vertex then points where a flat side faces in the default + orientation. A regular hexagon looks the same after a 60 degree turn, so 30 + degrees is the useful angle. + +Click "OK" to then proceed generating the recess. This will create the +following features: + +- A pocket for the hex nut recesses of all holes in this Hole feature. When + the recess starts on the opposite face, each hole gets its own pocket. + +Additionally, two properties will be added to the original Hole feature which +can be used to parametrically control the recesses: + +- `HexNutRadius` is the radius from the hole center to a corner of the hex nut. +- `HexNutDepth` is the depth of the hexagonal recess. + +## Parametricity +This feature is parametric with respect to the following variables: + +- Hole + * The position of the circles in the supporting sketch of the original Hole feature. + * The position of the supporting sketch itself. +- Hex Nut Properties of the Hole + * Hex nut radius (`HexNutRadius` property) + * Hex nut recess depth (`HexNutDepth` property) + +This feature is **not** parametric with respect to the following variables. +You will need to delete the feature and recreate it to update these variables: + +- Supporting Sketch + * The number of circles in the supporting sketch of the original Hole feature. +- Hole + * The direction of the hole (`Reversed` property). + * The choice between the hole entry face and the opposite face. + * The rotation of the hexagon (vertex up or side up). + +[pd-hole]: https://wiki.freecad.org/PartDesign_Hole +[task-panel]: https://wiki.freecad.org/Task_panel diff --git a/Documentation/ffDesign_HoleWizard.md b/Documentation/ffDesign_HoleWizard.md index dad3c98..959d9da 100644 --- a/Documentation/ffDesign_HoleWizard.md +++ b/Documentation/ffDesign_HoleWizard.md @@ -8,6 +8,7 @@ selecting one of the following tools: - [ Thread Forming Ribs](./ffDesign_RibThreads.md) - [ Teardrop Shape](./ffDesign_Teardrop.md) - [ Roof Bridge](./ffDesign_RoofBridge.md) +- [ Hex Nut Hole](./ffDesign_HexNut.md) ## Prerequisites - A [PartDesign Hole][pd-hole] feature must be selected or the tip of the diff --git a/InitGui.py b/InitGui.py index b34948b..630e82d 100644 --- a/InitGui.py +++ b/InitGui.py @@ -6,6 +6,7 @@ import ffDesign_RibThreads import ffDesign_Teardrop import ffDesign_RoofBridge +import ffDesign_HexNut import ffDesign_ZipTieChannels import ffDesign_AutoFillet diff --git a/README.md b/README.md index 17ee812..1aa9bf9 100644 --- a/README.md +++ b/README.md @@ -3,10 +3,10 @@ This is a [FreeCAD PartDesign][fc-partdesign] addon for FFF/FDM 3D-printing design. It includes various tools to generate geometry for better printability of a part. -![A demo screenshot of the Hole Wizard in action](./Resources/splash.png) +![A demo screenshot of the Hole Wizard in action](./Resources/splash.png). ## Installation -This addon is available through the [FreeCAD Addon-Manager](https://wiki.freecad.org/Std_AddonMgr). +This addon is available through the [FreeCAD Addon-Manager][addon-manager]. ## Usage The addon adds a new toolbar to the [PartDesign][fc-partdesign] workbench where the new tools can be accessed. Check the tooltips for guidance. @@ -26,12 +26,13 @@ To find out more about using the tools included in this addon, please check the | | ![ffDesign_RibThreads](./Resources/icons/ffDesign_RibThreads.svg) | **Thread Forming Ribs** | Hole geometry for [rib thread forming][df3dp-ribthreads] (**R5.4**) | | | ![ffDesign_Teardrop](./Resources/icons/ffDesign_Teardrop.svg) | **Teardrop Shape** | Teardrop-shaped holes for [better horizontal holes][df3dp-horizontal-holes] and to avoid [seam inaccuracy][df3dp-seam] (**R2.2** & **R2.3**) | | | ![ffDesign_RoofBridge](./Resources/icons/ffDesign_RoofBridge.svg) | **Roof Bridge** | Roof bridges for [horizontal holes][df3dp-horizontal-holes] (**R2.2**) | +| | ![ffDesign_HexNut](./Resources/icons/ffDesign_HexNut.svg) | **Hex Nut Hole** | Hexagonal recess at the end of a hole to capture a hex nut | | ![ffDesign_ZipTieChannels](./Resources/icons/ffDesign_ZipTieChannels.svg) | | **Zip Tie Channels** | Generate parametric [zip tie channels][df3dp-zip-ties] on a part's surface (**R4.1**) | | ![ffDesign_AutoFillet](./Resources/icons/ffDesign_AutoFillet.svg) | | **Auto Fillet** | Automatically add [fillets on all vertical edges][df3dp-vertical-fillets] of a part. (**R2.1**) | | _Planned_ | | **Seam Groove** | Generate a seam groove to control [where the slicer will place the perimeter seams][df3dp-seam] (**R2.3**)| | _Planned_ | | **Sacrificial Layer** | Generate a [sacrificial layers][df3dp-sacrificial] for some surface with holes (**R3.4**) | -(The **R#.#** numbers reference the rules from my [Design for 3D-Printing][df3dp-main] guide) +(The **R#.#** numbers reference the rules from my [Design for 3D-Printing][df3dp-main] guide). ## Changelog To follow updates of this addon, please check the [Releases Page](https://github.com/Rahix/FusedFilamentDesign/releases) or the [CHANGELOG](./CHANGELOG.md) file. @@ -39,6 +40,7 @@ To follow updates of this addon, please check the [Releases Page](https://github ## License This addon is licensed under the `LGPL-2.1-or-later` license. See [LICENSE](./LICENSE) for more info. +[addon-manager]: https://wiki.freecad.org/Std_AddonMgr [fc-partdesign]: https://wiki.freecad.org/PartDesign_Workbench [fc-hole]: https://wiki.freecad.org/PartDesign_Hole [fc-additional-workbenches]: https://wiki.freecad.org/How_to_install_additional_workbenches diff --git a/Resources/icons/ffDesign_HexNut.svg b/Resources/icons/ffDesign_HexNut.svg new file mode 100644 index 0000000..7b71f8d --- /dev/null +++ b/Resources/icons/ffDesign_HexNut.svg @@ -0,0 +1,377 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Resources/panels/ffDesign_HexNut.ui b/Resources/panels/ffDesign_HexNut.ui new file mode 100644 index 0000000..5ec9a2d --- /dev/null +++ b/Resources/panels/ffDesign_HexNut.ui @@ -0,0 +1,98 @@ + + + HexNutDialog + + + + 0 + 0 + 400 + 190 + + + + Hex Nut Hole + + + + + + Hex Nut Radius + + + + + + + mm + + + 0.000000000000000 + + + Radius from the hole center to a corner of the hex nut. + + + + + + + Hex Hole Depth + + + + + + + mm + + + 0.000000000000000 + + + Depth of the hexagonal recess, measured from the hole's entry face. + + + + + + + Start the hexagonal recess on the opposite face of the part instead of on the hole entry face. + + + Start on the opposite face + + + + + + + Turn the hexagon by 30 degrees, so don't have to print a bridge. + + + Rotate by 30 degrees + + + + + + + The hexagonal recess extends from the selected face into the part. + + + true + + + + + + + + Gui::QuantitySpinBox + QWidget +
gui::quantityspinbox.h
+
+
+ + +
diff --git a/Resources/panels/ffDesign_HoleWizard.ui b/Resources/panels/ffDesign_HoleWizard.ui index a283592..1b1c78f 100644 --- a/Resources/panels/ffDesign_HoleWizard.ui +++ b/Resources/panels/ffDesign_HoleWizard.ui @@ -7,7 +7,7 @@ 0 0 400 - 320 + 380 @@ -162,6 +162,35 @@ Preconditions: + + + + + 0 + 0 + + + + Add a hexagonal recess to the end of the hole to hold a hex nut. + + + Add Hex Nut Hole + + + + icons:ffDesign_HexNut.svgicons:ffDesign_HexNut.svg + + + + 32 + 32 + + + + Qt::ToolButtonStyle::ToolButtonTextBesideIcon + + + diff --git a/ffDesign_HexNut.py b/ffDesign_HexNut.py new file mode 100644 index 0000000..625899a --- /dev/null +++ b/ffDesign_HexNut.py @@ -0,0 +1,265 @@ +import math + +from PySide import QtCore + +import FreeCADGui as Gui +import FreeCAD as App +import Part +import Sketcher + +import ffDesign_Utils as Utils + + +HEX_SIDES = 6 + + +def make_parametric_hex_nut( + sketch, + *, + hole_loc: Utils.LocationExprSet, + radius_expr: str, + rotation: float = 0.0, +): + Utils.assert_sketch(sketch) + + last_geo_id = len(sketch.Geometry) + points = [] + for i in range(HEX_SIDES): + angle = 2 * math.pi / HEX_SIDES * i + rotation + points.append(App.Vector(math.cos(angle), math.sin(angle), 0)) + + new_geo = [Part.LineSegment(points[i], points[(i + 1) % HEX_SIDES]) for i in range(HEX_SIDES)] + sketch.addGeometry(new_geo, False) + + new_constraints = [] + for i in range(HEX_SIDES): + new_constraints.append( + Sketcher.Constraint("Coincident", last_geo_id + i, 2, last_geo_id + (i + 1) % HEX_SIDES, 1) + ) + sketch.addConstraint(new_constraints) + + # Fix every vertex directly. Equal sides plus points on a circle also + # allows a collapsed hexagon, so all vertices get their own coordinates. + x = hole_loc.x_expr + y = hole_loc.y_expr + r = f"({radius_expr})" + vertex_offsets = [] + for i in range(HEX_SIDES): + angle = 2 * math.pi / HEX_SIDES * i + rotation + vertex_offsets.append((f"{r} * {math.cos(angle)!r}", f"{r} * {math.sin(angle)!r}")) + + expressions = [] + for dx, dy in vertex_offsets: + expressions.append(f"{x} + ({dx})") + expressions.append(f"{y} + ({dy})") + + last_c = len(sketch.Constraints) + new_constraints = [] + for i in range(HEX_SIDES): + new_constraints.append(Sketcher.Constraint("DistanceX", last_geo_id + i, 1, 0)) + new_constraints.append(Sketcher.Constraint("DistanceY", last_geo_id + i, 1, 0)) + sketch.addConstraint(new_constraints) + for i, expr in enumerate(expressions): + sketch.setExpression(f"Constraints[{last_c + i}]", expr) + + sketch.recompute() + + +def _get_far_face_offset(body, hole, profile_sketch, hole_loc): + """ + Distance from the profile plane to the opposite face of the part. + + The result is signed along the profile sketch's local Z axis, so it can be + used as the attachment offset of a sketch on the opposite face. + """ + Utils.assert_body(body) + Utils.assert_hole(hole) + Utils.assert_sketch(profile_sketch) + + if hole_loc.center is None: + raise Utils.ffDesignError("Could not determine the position of a hole.") + + profile_placement = profile_sketch.Placement + normal = profile_placement.Rotation.multVec(App.Vector(0, 0, 1)) + direction = normal if hole.Reversed else normal * -1 + entry = profile_placement.multVec(hole_loc.center) + + # The hole axis itself is empty, so probe the material just next to the + # hole wall and look for the last point that is still inside the part. + radius = hole.Diameter.Value / 2 + radial_a = direction.cross(App.Vector(1, 0, 0)) + if radial_a.Length < 1e-6: + radial_a = direction.cross(App.Vector(0, 1, 0)) + radial_a.normalize() + radial_b = direction.cross(radial_a) + radial_b.normalize() + + bound = body.Shape.BoundBox.DiagonalLength * 2 + 10 + entry_proj = entry.dot(direction) + distances = [] + for radial in (radial_a, radial_a * -1, radial_b, radial_b * -1): + offset = radial * (radius + 0.05) + start = entry + offset - direction * (radius + 3) + end = entry + offset + direction * bound + common = body.Shape.common(Part.makeLine(start, end)) + if not common.Vertexes: + continue + far_proj = max(vertex.Point.dot(direction) for vertex in common.Vertexes) + distances.append(far_proj - entry_proj) + + if not distances or max(distances) <= 0: + raise Utils.ffDesignError( + "Could not find the opposite face of this hole. The hex nut recess was not created." + ) + + distance = max(distances) + return distance if hole.Reversed else -distance + + +def _make_hex_nut_pocket(body, hole, hex_sketch, *, reversed_, suffix): + pocket = body.newObject("PartDesign::Pocket", f"{hole.Name}{suffix}") + pocket.Profile = (hex_sketch, "") + pocket.ReferenceAxis = (hex_sketch, ["N_Axis"]) + pocket.Reversed = reversed_ + hex_sketch.Visibility = False + pocket.setExpression("Length", f"{hole.Name}.HexNutDepth") + pocket.Label = f"{hole.Label}{suffix}" + pocket.recompute() + + +def make_hex_nuts( + body, + hole, + *, + nut_radius: App.Units.Quantity, + nut_depth: App.Units.Quantity, + far_side: bool = False, + vertex_up: bool = False, +): + Utils.assert_body(body) + Utils.assert_hole(hole) + + # A regular hexagon has 6-fold symmetry, so a 60 degree turn looks the + # same. Turning it by 30 degrees is what swaps a flat side and a vertex. + rotation = math.radians(30) if vertex_up else 0.0 + + nut_radius = App.Units.Quantity(nut_radius) + assert nut_radius.Unit.Type == "Length" + nut_depth = App.Units.Quantity(nut_depth) + assert nut_depth.Unit.Type == "Length" + + if "HexNutRadius" not in hole.PropertiesList: + hole.addProperty("App::PropertyLength", "HexNutRadius", group="FusedFilamentDesign") + hole.HexNutRadius = nut_radius + + if "HexNutDepth" not in hole.PropertiesList: + hole.addProperty("App::PropertyLength", "HexNutDepth", group="FusedFilamentDesign") + hole.HexNutDepth = nut_depth + + profile_sketch, profile_partial = Utils.get_hole_profile_sketch(hole) + hole_locations = Utils.get_sketch_locations(profile_sketch, Utils.get_hole_profile_type(hole), profile_partial) + + if not far_side: + hex_sketch = Utils.make_derived_sketch(body, profile_sketch, "_HexNut") + for hole_loc in hole_locations: + make_parametric_hex_nut( + hex_sketch, + hole_loc=hole_loc, + radius_expr=f"{hole.Name}.HexNutRadius", + rotation=rotation, + ) + _make_hex_nut_pocket(body, hole, hex_sketch, reversed_=hole.Reversed, suffix="_HexNut") + return + + # Each hole can exit through a face at a different distance, and a sketch + # is planar. So on the opposite side, every hole gets its own sketch. + offsets = [ + (hole_loc, _get_far_face_offset(body, hole, profile_sketch, hole_loc)) for hole_loc in hole_locations + ] + multiple = len(offsets) > 1 + for i, (hole_loc, offset) in enumerate(offsets): + suffix = f"_HexNut{i + 1}" if multiple else "_HexNut" + hex_sketch = Utils.make_derived_sketch(body, profile_sketch, suffix) + hex_sketch.AttachmentOffset = App.Placement(App.Vector(0, 0, offset), App.Rotation()) + hex_sketch.recompute() + make_parametric_hex_nut( + hex_sketch, + hole_loc=hole_loc, + radius_expr=f"{hole.Name}.HexNutRadius", + rotation=rotation, + ) + _make_hex_nut_pocket(body, hole, hex_sketch, reversed_=not hole.Reversed, suffix=suffix) + + +class HexNutTaskPanel: + def __init__(self, body, hole): + Utils.assert_body(body) + Utils.assert_hole(hole) + + self.body = body + self.hole = hole + self.form = Gui.PySideUic.loadUi(Utils.Resources.get_panel("ffDesign_HexNut.ui")) + + self.form.HexNutRadius.setProperty("rawValue", 3.2) + self.form.HexNutDepth.setProperty("rawValue", 3.0) + + def accept(self): + try: + nut_radius = self.form.HexNutRadius.property("value") + nut_depth = self.form.HexNutDepth.property("value") + far_side = self.form.FarSide.checkState() == QtCore.Qt.CheckState.Checked + vertex_up = self.form.VertexUp.checkState() == QtCore.Qt.CheckState.Checked + + Gui.Control.closeDialog() + + try: + App.ActiveDocument.openTransaction("Add hex nut hole") + make_hex_nuts( + self.body, + self.hole, + nut_radius=nut_radius, + nut_depth=nut_depth, + far_side=far_side, + vertex_up=vertex_up, + ) + App.ActiveDocument.recompute() + except Exception as e: + App.ActiveDocument.abortTransaction() + raise e from None + else: + App.ActiveDocument.commitTransaction() + except Utils.ffDesignError as e: + e.emit_to_user() + + def reject(self): + Gui.Control.closeDialog() + + +class HexNutCommand: + def GetResources(self): + return { + "Pixmap": "icons:ffDesign_HexNut.svg", + "MenuText": App.Qt.translate("ffDesign", "Add hex nut hole"), + "ToolTip": App.Qt.translate( + "ffDesign", + "Add a hexagonal recess to the end of a hole to hold a hex nut.\n" + "1. Select a Hole feature in the active body.\n" + "2. Run this command.\n", + ), + } + + def Activated(self): + try: + hole = Utils.get_selected_hole() + body = Utils.get_active_part_design_body_for_feature(hole) + + dialog = HexNutTaskPanel(body, hole) + Gui.Control.showDialog(dialog) + except Utils.ffDesignError as e: + e.emit_to_user() + + def IsActive(self): + return Utils.check_hole_tool_preconditions() + + +Gui.addCommand("ffDesign_HexNut", HexNutCommand()) diff --git a/ffDesign_HoleWizard.py b/ffDesign_HoleWizard.py index c7047b5..a126d58 100644 --- a/ffDesign_HoleWizard.py +++ b/ffDesign_HoleWizard.py @@ -15,6 +15,7 @@ def __init__(self, hole): self.form.AddRibThreads.clicked.connect(self.addRibThreads) self.form.AddRoofBridge.clicked.connect(self.addRoofBridge) self.form.AddTeardropShape.clicked.connect(self.addTeardropShape) + self.form.AddHexNutHole.clicked.connect(self.addHexNutHole) self.updateCommandAvailability() @@ -28,6 +29,7 @@ def updateCommandAvailability(self): # The following hole tools are always available self.form.AddTeardropShape.setEnabled(True) self.form.AddRoofBridge.setEnabled(True) + self.form.AddHexNutHole.setEnabled(True) def addCounterboreBridges(self): FreeCADGui.Control.closeDialog() @@ -45,6 +47,10 @@ def addTeardropShape(self): FreeCADGui.Control.closeDialog() FreeCADGui.runCommand("ffDesign_Teardrop") + def addHexNutHole(self): + FreeCADGui.Control.closeDialog() + FreeCADGui.runCommand("ffDesign_HexNut") + def accept(self): FreeCADGui.Control.closeDialog() diff --git a/ffDesign_Utils.py b/ffDesign_Utils.py index 6f29b17..3886da9 100644 --- a/ffDesign_Utils.py +++ b/ffDesign_Utils.py @@ -313,6 +313,8 @@ class LocationExprSet: vector_expr: str x_expr: str y_expr: str + # Position of the hole in the sketch's local coordinate system. + center: App.Vector = None def get_sketch_locations(sketch, profile_type, profile_partial=None): @@ -329,6 +331,7 @@ def try_make_loc_expr_set(index, obj, kind): vector_expr=f"vector({x_expr}, {y_expr}, 0)", x_expr=f"{x_expr} * 1mm", y_expr=f"{y_expr} * 1mm", + center=App.Vector(obj.X, obj.Y, 0), ) if (profile_type & MASK_PROFILE_CIRCLES) != 0: if obj.TypeId == "Part::GeomCircle": @@ -337,6 +340,7 @@ def try_make_loc_expr_set(index, obj, kind): vector_expr=center, x_expr=f"{center}.x * 1mm", y_expr=f"{center}.y * 1mm", + center=obj.Center, ) if (profile_type & MASK_PROFILE_ARCS) != 0: if obj.TypeId == "Part::GeomArcOfCircle": @@ -345,6 +349,7 @@ def try_make_loc_expr_set(index, obj, kind): vector_expr=center, x_expr=f"{center}.x * 1mm", y_expr=f"{center}.y * 1mm", + center=obj.Center, ) return None From ee58fd86b28e4236b0b95896bb95016820818137 Mon Sep 17 00:00:00 2001 From: RazorSh4rk Date: Mon, 9 Nov 2026 07:31:24 -0800 Subject: [PATCH 2/3] address the small semantic issues from #61 --- README.md | 2 +- Resources/icons/ffDesign_HexNut.svg | 378 +--------------------------- ffDesign_HexNut.py | 13 +- ffDesign_Utils.py | 2 +- 4 files changed, 8 insertions(+), 387 deletions(-) diff --git a/README.md b/README.md index 1aa9bf9..0726907 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ This is a [FreeCAD PartDesign][fc-partdesign] addon for FFF/FDM 3D-printing design. It includes various tools to generate geometry for better printability of a part. -![A demo screenshot of the Hole Wizard in action](./Resources/splash.png). +![A demo screenshot of the Hole Wizard in action](./Resources/splash.png) ## Installation This addon is available through the [FreeCAD Addon-Manager][addon-manager]. diff --git a/Resources/icons/ffDesign_HexNut.svg b/Resources/icons/ffDesign_HexNut.svg index 7b71f8d..5c47272 100644 --- a/Resources/icons/ffDesign_HexNut.svg +++ b/Resources/icons/ffDesign_HexNut.svg @@ -1,377 +1 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + \ No newline at end of file diff --git a/ffDesign_HexNut.py b/ffDesign_HexNut.py index 625899a..8d1b5ab 100644 --- a/ffDesign_HexNut.py +++ b/ffDesign_HexNut.py @@ -76,9 +76,6 @@ def _get_far_face_offset(body, hole, profile_sketch, hole_loc): Utils.assert_hole(hole) Utils.assert_sketch(profile_sketch) - if hole_loc.center is None: - raise Utils.ffDesignError("Could not determine the position of a hole.") - profile_placement = profile_sketch.Placement normal = profile_placement.Rotation.multVec(App.Vector(0, 0, 1)) direction = normal if hole.Reversed else normal * -1 @@ -116,11 +113,11 @@ def _get_far_face_offset(body, hole, profile_sketch, hole_loc): return distance if hole.Reversed else -distance -def _make_hex_nut_pocket(body, hole, hex_sketch, *, reversed_, suffix): +def _make_hex_nut_pocket(body, hole, hex_sketch, *, reversed, suffix: str = ""): pocket = body.newObject("PartDesign::Pocket", f"{hole.Name}{suffix}") pocket.Profile = (hex_sketch, "") pocket.ReferenceAxis = (hex_sketch, ["N_Axis"]) - pocket.Reversed = reversed_ + pocket.Reversed = reversed hex_sketch.Visibility = False pocket.setExpression("Length", f"{hole.Name}.HexNutDepth") pocket.Label = f"{hole.Label}{suffix}" @@ -168,7 +165,7 @@ def make_hex_nuts( radius_expr=f"{hole.Name}.HexNutRadius", rotation=rotation, ) - _make_hex_nut_pocket(body, hole, hex_sketch, reversed_=hole.Reversed, suffix="_HexNut") + _make_hex_nut_pocket(body, hole, hex_sketch, reversed=hole.Reversed, suffix="_HexNut") return # Each hole can exit through a face at a different distance, and a sketch @@ -178,7 +175,7 @@ def make_hex_nuts( ] multiple = len(offsets) > 1 for i, (hole_loc, offset) in enumerate(offsets): - suffix = f"_HexNut{i + 1}" if multiple else "_HexNut" + suffix = f"_HexNut{i + 1:03}" if multiple else "_HexNut" hex_sketch = Utils.make_derived_sketch(body, profile_sketch, suffix) hex_sketch.AttachmentOffset = App.Placement(App.Vector(0, 0, offset), App.Rotation()) hex_sketch.recompute() @@ -188,7 +185,7 @@ def make_hex_nuts( radius_expr=f"{hole.Name}.HexNutRadius", rotation=rotation, ) - _make_hex_nut_pocket(body, hole, hex_sketch, reversed_=not hole.Reversed, suffix=suffix) + _make_hex_nut_pocket(body, hole, hex_sketch, reversed=not hole.Reversed, suffix=suffix) class HexNutTaskPanel: diff --git a/ffDesign_Utils.py b/ffDesign_Utils.py index 3886da9..30d91a0 100644 --- a/ffDesign_Utils.py +++ b/ffDesign_Utils.py @@ -314,7 +314,7 @@ class LocationExprSet: x_expr: str y_expr: str # Position of the hole in the sketch's local coordinate system. - center: App.Vector = None + center: App.Vector def get_sketch_locations(sketch, profile_type, profile_partial=None): From 4633b8b0db9bd0456058723d9e66ca7ddc7d856d Mon Sep 17 00:00:00 2001 From: RazorSh4rk Date: Mon, 9 Nov 2026 08:34:06 -0800 Subject: [PATCH 3/3] switch to spanner size from diameter #61 --- Documentation/ffDesign_HexNut.md | 10 +++++----- Resources/panels/ffDesign_HexNut.ui | 6 +++--- ffDesign_HexNut.py | 28 +++++++++++++++------------- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/Documentation/ffDesign_HexNut.md b/Documentation/ffDesign_HexNut.md index 1bbc7a9..ac2916b 100644 --- a/Documentation/ffDesign_HexNut.md +++ b/Documentation/ffDesign_HexNut.md @@ -13,9 +13,9 @@ Run this command to generate a hex nut recess for the selected Hole feature. A dialog will open in the [Task Panel][task-panel] where you can control the generation. -- **Hex Nut Radius**: The radius from the hole center to a corner of the hex - nut. This is the radius of the circle around the hexagon, not the distance - across the flats. +- **Hex Nut Size**: The distance between two opposite flat sides of the hex + nut, also known as the spanner size. This is the nominal dimension of a hex + nut, not the radius to a corner. - **Hex Hole Depth**: The depth of the hexagonal recess, measured from the selected face. - **Start on the opposite face**: When checked, the recess starts on the @@ -35,7 +35,7 @@ following features: Additionally, two properties will be added to the original Hole feature which can be used to parametrically control the recesses: -- `HexNutRadius` is the radius from the hole center to a corner of the hex nut. +- `HexNutSize` is the distance across the flats of the hex nut. - `HexNutDepth` is the depth of the hexagonal recess. ## Parametricity @@ -45,7 +45,7 @@ This feature is parametric with respect to the following variables: * The position of the circles in the supporting sketch of the original Hole feature. * The position of the supporting sketch itself. - Hex Nut Properties of the Hole - * Hex nut radius (`HexNutRadius` property) + * Hex nut size (`HexNutSize` property) * Hex nut recess depth (`HexNutDepth` property) This feature is **not** parametric with respect to the following variables. diff --git a/Resources/panels/ffDesign_HexNut.ui b/Resources/panels/ffDesign_HexNut.ui index 5ec9a2d..37576aa 100644 --- a/Resources/panels/ffDesign_HexNut.ui +++ b/Resources/panels/ffDesign_HexNut.ui @@ -17,12 +17,12 @@ - Hex Nut Radius + Hex Nut Size - + mm @@ -30,7 +30,7 @@ 0.000000000000000 - Radius from the hole center to a corner of the hex nut. + Distance between two opposite flat sides of the hex nut (spanner size). diff --git a/ffDesign_HexNut.py b/ffDesign_HexNut.py index 8d1b5ab..912f744 100644 --- a/ffDesign_HexNut.py +++ b/ffDesign_HexNut.py @@ -17,7 +17,7 @@ def make_parametric_hex_nut( sketch, *, hole_loc: Utils.LocationExprSet, - radius_expr: str, + size_expr: str, rotation: float = 0.0, ): Utils.assert_sketch(sketch) @@ -42,7 +42,9 @@ def make_parametric_hex_nut( # allows a collapsed hexagon, so all vertices get their own coordinates. x = hole_loc.x_expr y = hole_loc.y_expr - r = f"({radius_expr})" + # The size is across the flats, so the corners sit on a circle of radius + # half the flat distance divided by cos(30 degrees), i.e. size / sqrt(3). + r = f"({size_expr}) / sqrt(3)" vertex_offsets = [] for i in range(HEX_SIDES): angle = 2 * math.pi / HEX_SIDES * i + rotation @@ -128,7 +130,7 @@ def make_hex_nuts( body, hole, *, - nut_radius: App.Units.Quantity, + nut_size: App.Units.Quantity, nut_depth: App.Units.Quantity, far_side: bool = False, vertex_up: bool = False, @@ -140,14 +142,14 @@ def make_hex_nuts( # same. Turning it by 30 degrees is what swaps a flat side and a vertex. rotation = math.radians(30) if vertex_up else 0.0 - nut_radius = App.Units.Quantity(nut_radius) - assert nut_radius.Unit.Type == "Length" + nut_size = App.Units.Quantity(nut_size) + assert nut_size.Unit.Type == "Length" nut_depth = App.Units.Quantity(nut_depth) assert nut_depth.Unit.Type == "Length" - if "HexNutRadius" not in hole.PropertiesList: - hole.addProperty("App::PropertyLength", "HexNutRadius", group="FusedFilamentDesign") - hole.HexNutRadius = nut_radius + if "HexNutSize" not in hole.PropertiesList: + hole.addProperty("App::PropertyLength", "HexNutSize", group="FusedFilamentDesign") + hole.HexNutSize = nut_size if "HexNutDepth" not in hole.PropertiesList: hole.addProperty("App::PropertyLength", "HexNutDepth", group="FusedFilamentDesign") @@ -162,7 +164,7 @@ def make_hex_nuts( make_parametric_hex_nut( hex_sketch, hole_loc=hole_loc, - radius_expr=f"{hole.Name}.HexNutRadius", + size_expr=f"{hole.Name}.HexNutSize", rotation=rotation, ) _make_hex_nut_pocket(body, hole, hex_sketch, reversed=hole.Reversed, suffix="_HexNut") @@ -182,7 +184,7 @@ def make_hex_nuts( make_parametric_hex_nut( hex_sketch, hole_loc=hole_loc, - radius_expr=f"{hole.Name}.HexNutRadius", + size_expr=f"{hole.Name}.HexNutSize", rotation=rotation, ) _make_hex_nut_pocket(body, hole, hex_sketch, reversed=not hole.Reversed, suffix=suffix) @@ -197,12 +199,12 @@ def __init__(self, body, hole): self.hole = hole self.form = Gui.PySideUic.loadUi(Utils.Resources.get_panel("ffDesign_HexNut.ui")) - self.form.HexNutRadius.setProperty("rawValue", 3.2) + self.form.HexNutSize.setProperty("rawValue", 5.5) self.form.HexNutDepth.setProperty("rawValue", 3.0) def accept(self): try: - nut_radius = self.form.HexNutRadius.property("value") + nut_size = self.form.HexNutSize.property("value") nut_depth = self.form.HexNutDepth.property("value") far_side = self.form.FarSide.checkState() == QtCore.Qt.CheckState.Checked vertex_up = self.form.VertexUp.checkState() == QtCore.Qt.CheckState.Checked @@ -214,7 +216,7 @@ def accept(self): make_hex_nuts( self.body, self.hole, - nut_radius=nut_radius, + nut_size=nut_size, nut_depth=nut_depth, far_side=far_side, vertex_up=vertex_up,