What's new

#2 Script for generating and saving Metric Hex Nuts

oldfox

Alibre Super User
This script will allow you to select a nut size in the Mxx Series, from M2 through M20. Instructions are in the
script. If you have any problems or questions, please ask me in this forum. Also coming soon will be a script
for Metric screws. Again, M2 through M20.

Dimensions are correct for a standard hex nut. However they are intended for "EYE CANDY" only.

Thanks go to Stefan, aka IDSLK for the script help he offered. Thanks Stefan.

Scripts for Metric Screws to follow soon. :D

Code:
# Author:  C. Line aka OLDFOX with much appreciated help and guidance from Stefan aka IDSLK (thanks Stefan)
# Version: 2.0
# Date:    27 May 2019

# This script will generate a metric hex nut in the range of M2 thru M20. No standards used. It's only eye candy.

#***************************************************************************************
#                                                                                      *
#                                   INSTRUCTIONS                                       *
#                                                                                      *
#  1. Change SAVE option by commenting or uncommenting lines 147, 148 and/or 149       *
#     147 will save to a "HARDWARE" subfolder of the main project folder               *
#     148 will save to a library subfolder on the desktop (use your own USERNAME       *
#     149 will allow you to BROWSE for a folder of your choice                         *
#  2. RUN this script. Right clicking on the script name will open a menu              *
#  3. Select nut size using the dropdown and then click APPLY and then click CLOSE     *
#  4. That's it. The nut is made and saved. For more nuts, just RUN script again       *
#                                                                                      *
#***************************************************************************************

#**********************************************************************************************************
def SaveLocation():
   Options = []
   Options.append(['Choose Folder', WindowsInputTypes.Folder, 'Folder Path here'])
   Values = Win.OptionsDialog('Save Location', Options)
   if Values == None:
      sys.exit('User cancelled')
   global OutputFolder
   OutputFolder = Values[0]

#**********************************************************************************************************
def HexNut():
   Part = 'Hex Nut'
   global Nut
    
   Body = Nut.AddSketch("Body", Nut.YZPlane)
   Body.AddPolygon(0.0, 0.0, AcrossCorners, 6, False)
   Body.AddCircle(0.0, 0.0, ScrewDiameter, False)
   Nut.AddExtrudeBoss('Hex Nut', Body, Thickness, False)
  
   Nut.AddFillet('Hex Corners', [Nut.GetEdge('Edge<2>'), Nut.GetEdge('Edge<4>'), Nut.GetEdge('Edge<6>'), Nut.GetEdge('Edge<9>'), Nut.GetEdge('Edge<12>'), Nut.GetEdge('Edge<15>')], HexEdgeFillet, False)
   Chamfer = Nut.AddSketch('Face Chamfer', Nut.XYPlane)
  
   Chamfer.AddLine(0.0, (AcrossFlats / 2), 0.0, ((AcrossCorners / 2) + .01), False)
   Chamfer.AddLine(0.0, ((AcrossCorners / 2) + 0.01), ((AcrossCorners/2 - AcrossFlats / 2) / 2), ((AcrossCorners / 2) + 0.01), False)
   Chamfer.AddLine(((AcrossCorners/2 - AcrossFlats / 2) / 2), ((AcrossCorners / 2) + 0.01), 0.0, (AcrossFlats / 2), False)
  
   Chamfer.AddLine(Thickness, (AcrossFlats / 2), Thickness, ((AcrossCorners / 2) + .01), False)
   Chamfer.AddLine(Thickness, ((AcrossCorners / 2) + 0.01), (Thickness - ((AcrossCorners/2 - AcrossFlats / 2) / 2)), ((AcrossCorners / 2) + 0.01), False)
   Chamfer.AddLine((Thickness - ((AcrossCorners/2 - AcrossFlats / 2) / 2)), ((AcrossCorners / 2) + 0.01), Thickness, (AcrossFlats / 2), False)
  
   Nut.AddRevolveCut('Face Chamfer', Chamfer, Nut.XAxis, 360.0)
  
#***************************************************************************************
#                                      Main                                            *
#***************************************************************************************

Units.Current = UnitTypes.Millimeters
DefaultSize = 'None'
Win = Windows()
global Size
global Nut

MetricSizes = ['None', 'M2', 'M2.5', 'M3', 'M3.5', 'M4', 'M5', 'M6', 'M8', 'M10', 'M12', 'M14', 'M16', 'M20']

def InputChanged(Index, Value):
   if Index == 0:
      Size = MetricSizes[Value]
      print Size

def SelectionMade(Values):
   global Size
   Size = MetricSizes[Values[0]]
   try:
      # if a selection was made the Name will be valid
      print Size
   except:
      # if 'close' is choosen and no selection was made, set the value of 'Size' to the Value of 'DefaultSize'
      Size = DefaultSize

   if Size == 'None':
      Choice = 0
      print 'your Choice was invalid...'
      return
   if Size == 'M2':
      Choice = 1
   elif Size == 'M2.5':
      Choice = 2
   elif Size == 'M3':
      Choice = 3
   elif Size == 'M3.5':
      Choice = 4
   elif Size == 'M4':
      Choice = 5
   elif Size == 'M5':
      Choice = 6
   elif Size == 'M6':
      Choice = 7
   elif Size == 'M8':
      Choice = 8
   elif Size == 'M10':
      Choice = 9
   elif Size == 'M12':
      Choice = 10
   elif Size == 'M14':
      Choice = 11
   elif Size == 'M16':
      Choice = 12
   elif Size == 'M20':
      Choice = 13

   MetricSizeData = []#      0     1     2     3      4     5
   MetricSizeData.append (['None', 0,   0.0,  0.0,   0.0,  0.0])
   MetricSizeData.append (['M2',   2,   1.6,  4.0,  4.62,  3.1])
   MetricSizeData.append (['M2.5', 2.5, 2.0,  5.0,  5.77,  4.1])
   MetricSizeData.append (['M3',   3,   2.4,  5.5,  6.35,  4.6])
   MetricSizeData.append (['M3.5', 3.5, 2.8,  6.0,  6.93,  5.1])
   MetricSizeData.append (['M4',   4,   3.2,  7.0,  8.08,  6.0])
   MetricSizeData.append (['M5',   5,   4.7,  8.0,  9.24,  7.0])
   MetricSizeData.append (['M6',   6,   5.2, 10.0, 11.55,  8.9])
   MetricSizeData.append (['M8',   8,   6.8, 13.0, 15.01, 11.6])
   MetricSizeData.append (['M10', 10,   8.4, 16.0, 18.48, 14.6])
   MetricSizeData.append (['M12', 12,  10.8, 18.0, 20.78, 16.6])
   MetricSizeData.append (['M14', 14,  12.8, 21.0, 24.25, 19.4])
   MetricSizeData.append (['M16', 16,  14.8, 24.0, 27.71, 22.4])
   MetricSizeData.append (['M20', 20,  18.0, 30.0, 36.64, 27.9])

   global Size
   global ScrewDiameter
   global Thickness
   global AcrossFlats
   global AcrossCorners
   global BearingFaceDia
   global HexEdgeFillet
   global FaceChamferLength
   global FaceChamferDepth

   Size = MetricSizeData[Choice][0]
   ScrewDiameter = MetricSizeData[Choice][1]
   Thickness = MetricSizeData[Choice][2]
   AcrossFlats = MetricSizeData[Choice][3]
   AcrossCorners = MetricSizeData[Choice][4]
   BearingFaceDia = MetricSizeData[Choice][5]

   HexEdgeFillet = AcrossCorners * 0.02
   FaceChamferLength = AcrossCorners - AcrossFlats
   FaceChamferDepth = (AcrossCorners - AcrossFlats / 2) / 2

#OutputFolder = 'D:/Alibre/Projects/Engines/Engine Name/Hardware'     #Use your own Engine Name
#OutputFolder = 'C:/Users/UserName/Desktop/Hex Nuts'     #Use your own UserName
#SaveLocation()      # This option will allow SELECTING your own path

Options = []
Options.append(['Size', WindowsInputTypes.StringList, MetricSizes, DefaultSize])
DialogWidth = 200
Values = Win.UtilityDialog('Metric Nut Sizes', 'Apply', SelectionMade, InputChanged, Options, DialogWidth)

Nut = Part("%s Hex Nut" % Size)

HexNut()

Nut.Save(OutputFolder)
Nut.Close()
 

Attachments

  • Metric Series Nuts V2.0 5-27-2019.py
    6.7 KB · Views: 14
Top