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..ac2916b --- /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 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 + 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: + +- `HexNutSize` is the distance across the flats 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 size (`HexNutSize` 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..0726907 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ This is a [FreeCAD PartDesign][fc-partdesign] addon for FFF/FDM 3D-printing desi ![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..5c47272 --- /dev/null +++ b/Resources/icons/ffDesign_HexNut.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Resources/panels/ffDesign_HexNut.ui b/Resources/panels/ffDesign_HexNut.ui new file mode 100644 index 0000000..37576aa --- /dev/null +++ b/Resources/panels/ffDesign_HexNut.ui @@ -0,0 +1,98 @@ + + + HexNutDialog + + + + 0 + 0 + 400 + 190 + + + + Hex Nut Hole + + + + + + Hex Nut Size + + + + + + + mm + + + 0.000000000000000 + + + Distance between two opposite flat sides of the hex nut (spanner size). + + + + + + + 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..912f744 --- /dev/null +++ b/ffDesign_HexNut.py @@ -0,0 +1,264 @@ +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, + size_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 + # 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 + 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) + + 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: str = ""): + 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_size: 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_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 "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") + 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, + size_expr=f"{hole.Name}.HexNutSize", + 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: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() + make_parametric_hex_nut( + hex_sketch, + hole_loc=hole_loc, + size_expr=f"{hole.Name}.HexNutSize", + 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.HexNutSize.setProperty("rawValue", 5.5) + self.form.HexNutDepth.setProperty("rawValue", 3.0) + + def accept(self): + try: + 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 + + Gui.Control.closeDialog() + + try: + App.ActiveDocument.openTransaction("Add hex nut hole") + make_hex_nuts( + self.body, + self.hole, + nut_size=nut_size, + 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..30d91a0 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 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