Skip to content
Snippets Groups Projects
createMesh.py 69.8 KiB
Newer Older
  • Learn to ignore specific revisions
  • Brendon Murphy's avatar
    Brendon Murphy committed
    
    def Nut_Mesh(props, context):
    
        verts = []
        faces = []
        Head_Verts = []
    
        Head_Faces = []
    
    Brendon Murphy's avatar
    Brendon Murphy committed
    
        New_Nut_Height = 5
    
    Brendon Murphy's avatar
    Brendon Murphy committed
        Face_Start = len(verts)
    
        Thread_Verts, Thread_Faces, New_Nut_Height = Create_Internal_Thread(
                                                        props.bf_Minor_Dia, props.bf_Major_Dia,
                                                        props.bf_Pitch, props.bf_Hex_Nut_Height,
                                                        props.bf_Crest_Percent, props.bf_Root_Percent,
                                                        1, props.bf_Div_Count
                                                        )
    
    Brendon Murphy's avatar
    Brendon Murphy committed
        verts.extend(Thread_Verts)
    
        faces.extend(Copy_Faces(Thread_Faces, Face_Start))
    
    Brendon Murphy's avatar
    Brendon Murphy committed
        Face_Start = len(verts)
    
        Head_Verts, Head_Faces, Lock_Nut_Rad = add_Hex_Nut(
                                                    props.bf_Hex_Nut_Flat_Distance,
                                                    props.bf_Major_Dia, New_Nut_Height
                                                    )
    
    Brendon Murphy's avatar
    Brendon Murphy committed
        verts.extend((Head_Verts))
    
        faces.extend(Copy_Faces(Head_Faces, Face_Start))
    
    Brendon Murphy's avatar
    Brendon Murphy committed
        LowZ = 0 - New_Nut_Height
    
    Brendon Murphy's avatar
    Brendon Murphy committed
        if props.bf_Nut_Type == 'bf_Nut_Lock':
            Face_Start = len(verts)
    
            Nylon_Head_Verts, Nylon_Head_faces, LowZ = add_Nylon_Head(
                                                            Lock_Nut_Rad, 0 - New_Nut_Height,
                                                            props.bf_Div_Count
                                                            )
    
    Brendon Murphy's avatar
    Brendon Murphy committed
            verts.extend((Nylon_Head_Verts))
    
            faces.extend(Copy_Faces(Nylon_Head_faces, Face_Start))
    
    Brendon Murphy's avatar
    Brendon Murphy committed
            Face_Start = len(verts)
    
            Nylon_Verts, Nylon_faces, Temp_LowZ = add_Nylon_Part(
                                                            Lock_Nut_Rad, 0 - New_Nut_Height,
                                                            props.bf_Div_Count
                                                            )
    
    Brendon Murphy's avatar
    Brendon Murphy committed
            verts.extend((Nylon_Verts))
    
            faces.extend(Copy_Faces(Nylon_faces, Face_Start))
    
        return Move_Verts_Up_Z(verts, 0 - LowZ), faces
    
    # ####################################################################
    #                    Create Bolt
    # ####################################################################
    
    Brendon Murphy's avatar
    Brendon Murphy committed
    
    def Bolt_Mesh(props, context):
    
        verts = []
        faces = []
        Bit_Verts = []
        Bit_Faces = []
        Bit_Dia = 0.001
        Head_Verts = []
    
        Head_Faces = []
    
    Brendon Murphy's avatar
    Brendon Murphy committed
        Head_Height = 0.0
    
    
        ReSized_Allen_Bit_Flat_Distance = props.bf_Allen_Bit_Flat_Distance  # set default
    
        Head_Height = props.bf_Hex_Head_Height  # will be changed by the Head Functions
    
    Brendon Murphy's avatar
    Brendon Murphy committed
        if props.bf_Bit_Type == 'bf_Bit_Allen' and props.bf_Head_Type == 'bf_Head_Pan':
    
            # need to size Allen bit if it is too big.
            if Allen_Bit_Dia(props.bf_Allen_Bit_Flat_Distance) > Max_Pan_Bit_Dia(props.bf_Pan_Head_Dia):
                ReSized_Allen_Bit_Flat_Distance = Allen_Bit_Dia_To_Flat(
                                                    Max_Pan_Bit_Dia(props.bf_Pan_Head_Dia)
    
                                                    )
                ReSized_Allen_Bit_Flat_Distance -= ReSized_Allen_Bit_Flat_Distance * 0.05   # It looks better if it is just a bit smaller
    
                # print ("Resized Allen Bit Flat Distance to ",ReSized_Allen_Bit_Flat_Distance)
    
        # Bit Mesh
    
    Brendon Murphy's avatar
    Brendon Murphy committed
        if props.bf_Bit_Type == 'bf_Bit_Allen':
    
            Bit_Verts, Bit_Faces, Bit_Dia = Create_Allen_Bit(
                                                    ReSized_Allen_Bit_Flat_Distance,
                                                    props.bf_Allen_Bit_Depth
                                                    )
    
    Brendon Murphy's avatar
    Brendon Murphy committed
        if props.bf_Bit_Type == 'bf_Bit_Philips':
    
            Bit_Verts, Bit_Faces, Bit_Dia = Create_Phillips_Bit(
                                                    props.bf_Philips_Bit_Dia,
                                                    props.bf_Philips_Bit_Dia * (0.5 / 1.82),
                                                    props.bf_Phillips_Bit_Depth
                                                    )
        # Head Mesh
        if props.bf_Head_Type == 'bf_Head_Hex':
            Head_Verts, Head_Faces, Head_Height = Create_Hex_Head(
                                                    props.bf_Hex_Head_Flat_Distance, Bit_Dia,
                                                    props.bf_Shank_Dia, props.bf_Hex_Head_Height
                                                    )
    
        elif props.bf_Head_Type == 'bf_Head_Cap':
    
            Head_Verts, Head_Faces, Head_Height = Create_Cap_Head(
                                                    Bit_Dia, props.bf_Cap_Head_Dia,
                                                    props.bf_Shank_Dia, props.bf_Cap_Head_Height,
                                                    props.bf_Cap_Head_Dia * (1.0 / 19.0),
                                                    props.bf_Cap_Head_Dia * (1.0 / 19.0),
                                                    props.bf_Div_Count
                                                    )
        elif props.bf_Head_Type == 'bf_Head_Dome':
            Head_Verts, Head_Faces, Head_Height = Create_Dome_Head(
                                                    Bit_Dia, props.bf_Dome_Head_Dia,
                                                    props.bf_Shank_Dia, props.bf_Hex_Head_Height,
                                                    1, 1, 0, props.bf_Div_Count
                                                    )
    
        elif props.bf_Head_Type == 'bf_Head_Pan':
    
            Head_Verts, Head_Faces, Head_Height = Create_Pan_Head(
                                                    Bit_Dia, props.bf_Pan_Head_Dia,
                                                    props.bf_Shank_Dia,
                                                    props.bf_Hex_Head_Height, 1, 1, 0,
                                                    props.bf_Div_Count
                                                    )
    
        elif props.bf_Head_Type == 'bf_Head_CounterSink':
    
            Head_Verts, Head_Faces, Head_Height = Create_CounterSink_Head(
                                                    Bit_Dia, props.bf_CounterSink_Head_Dia,
                                                    props.bf_Shank_Dia, props.bf_CounterSink_Head_Dia,
                                                    props.bf_CounterSink_Head_Dia * (0.09 / 6.31),
                                                    props.bf_Div_Count
                                                    )
        """
        Head_Verts, Head_Faces, Head_Height = Create_CounterSink_Head(
                                                    Bit_Dia, props.bf_CounterSink_Head_Dia,
                                                    props.bf_Shank_Dia,
                                                    props.bf_CounterSink_Head_Dia,
                                                    props.bf_CounterSink_Head_Dia * (1.0 / 19.0)
                                                    )
        """
    
    Brendon Murphy's avatar
    Brendon Murphy committed
        Face_Start = len(verts)
    
        verts.extend(Move_Verts_Up_Z(Bit_Verts, Head_Height))
        faces.extend(Copy_Faces(Bit_Faces, Face_Start))
    
    Brendon Murphy's avatar
    Brendon Murphy committed
    
        Face_Start = len(verts)
    
        verts.extend(Move_Verts_Up_Z(Head_Verts, Head_Height))
        faces.extend(Copy_Faces(Head_Faces, Face_Start))
    
    Brendon Murphy's avatar
    Brendon Murphy committed
    
        Face_Start = len(verts)
    
        Thread_Verts, Thread_Faces, Thread_Height = Create_External_Thread(
                                                        props.bf_Shank_Dia, props.bf_Shank_Length,
                                                        props.bf_Minor_Dia, props.bf_Major_Dia,
                                                        props.bf_Pitch, props.bf_Thread_Length,
                                                        props.bf_Crest_Percent,
                                                        props.bf_Root_Percent, props.bf_Div_Count
                                                        )
    
        verts.extend(Move_Verts_Up_Z(Thread_Verts, 00))
        faces.extend(Copy_Faces(Thread_Faces, Face_Start))
    
        return Move_Verts_Up_Z(verts, Thread_Height), faces
    
    Aaron Keith's avatar
    Aaron Keith committed
    def Create_New_Mesh(props, context):
    
    Brendon Murphy's avatar
    Brendon Murphy committed
    
        verts = []
        faces = []
    
    Aaron Keith's avatar
    Aaron Keith committed
        edges = []
    
    Brendon Murphy's avatar
    Brendon Murphy committed
        if props.bf_Model_Type == 'bf_Model_Bolt':
    
            # print('Create Bolt')
    
    Brendon Murphy's avatar
    Brendon Murphy committed
            verts, faces = Bolt_Mesh(props, context)
            sObjName = 'Bolt'
    
    Brendon Murphy's avatar
    Brendon Murphy committed
        if props.bf_Model_Type == 'bf_Model_Nut':
    
            # print('Create Nut')
    
    Brendon Murphy's avatar
    Brendon Murphy committed
            verts, faces = Nut_Mesh(props, context)
            sObjName = 'Nut'
    
        verts, faces = RemoveDoubles(verts, faces)
    
        verts = Scale_Mesh_Verts(verts, GLOBAL_SCALE)
    
    Aaron Keith's avatar
    Aaron Keith committed
        mesh = bpy.data.meshes.new(name=sObjName)
        mesh.from_pydata(verts, edges, faces)
    
        # useful for development when the mesh may be invalid.
        # Fix T51338 : Validate the mesh (the internal thread generator for the Nut
        # should be more reliable now, however there could be other possible errors)
        is_not_mesh_valid = mesh.validate()
    
        if is_not_mesh_valid:
    
            props.report({'INFO'}, "Mesh is not Valid, correcting")