pythoncadquery

I'm trying to model a bolt in CadQuery python


I'm trying to build a CAD model of a bolt, but I can't figure out how to cut off the tops of the corners at the top of the head at a 45-degree angle.

I want to get this result

I want to get this result

What did I do for this?

At first I tried this:

import cadquery as cq
from math import sqrt, tan, radians


head_diameter = 10.0  
head_height = 5.0     
shaft_diameter = 5.0 
shaft_length = 20.0   


R = head_diameter / 2                 
r = R * sqrt(3)/2                    
chamfer_size = (R - r) / tan(45)     

bolt_head = (
    cq.Workplane("XY")
    .polygon(6, 2*R)                 
    .extrude(head_height)              
    .translate((0, 0, -1 * (head_height/2)))
)

bolt_head = bolt_head.edges("Z").chamfer(1)

bolt_shaft = (
    cq.Workplane("XY")
    .circle(shaft_diameter/2)
    .extrude(-shaft_length)
)

bolt = bolt_head.union(bolt_shaft)

This code generates the following CAD: The result is here

Then so:

import cadquery as cq
from math import sqrt, tan, radians


HEAD_DIAMETER = 20.0    
HEAD_HEIGHT = 10.0      
CUT_ANGLE = 45.0        
SHAFT_DIAMETER = 8.0    
SHAFT_LENGTH = 30.0    


R = HEAD_DIAMETER / 2
r = R * sqrt(3)/2  
cut_depth = (R - r) / tan(radians(CUT_ANGLE))


hexagon = (
    cq.Workplane("XY")
    .polygon(6, HEAD_DIAMETER)
    .extrude(HEAD_HEIGHT + cut_depth)
)


cutter = (
    cq.Workplane("XY")
    .workplane(offset=HEAD_HEIGHT)
    .circle(r)  
    .workplane(offset=cut_depth)
    .circle(R * 1.1)  
    .loft(combine=True)
)


result = hexagon.cut(cutter)


result = (
    result.faces(">Z")  
    .workplane(centerOption="CenterOfMass")  
    .circle(r)
    .cutBlind(-cut_depth)
)


show_object(result)

But it outputs an error:

ValueError: If multiple objects selected, they all must be planar faces.

This code generates the following CAD: The result is here

Does anyone know how to cut off the upper parts of the corners of the bolt head at a 45 degree angle?


Solution

  • You can cut the upper edge by using a solid of revolution: create a triangle with the required angle and revolve it around the bolt axis to create the cutter.

    cutter = (
        cq.Workplane("XZ")
        .workplane(offset=HEAD_HEIGHT)
        .move(r, 0)
        .lineTo(R*1.1, 0).lineTo(R*1.1, -cut_depth).lineTo(r, 0)
        .wire()
        .revolve()
    )