This was the first serious SolidWorks API macro I (Keith) ever wrote. The code is fairly sloppy so I only use this macro for demonstrating the capabilities of the SolidWorks API, and not for training others. Nevertheless you are welcome to download and look at it. Also note that splines are used to create the involute profile, so don't assume that mathematically they are perfect involutes.
Files
Code
Module: GearCreator1
Sub main()
UserForm1.cboUnits.AddItem "in"
UserForm1.cboUnits.AddItem "mm"
UserForm1.cmdCalculate.SetFocus
UserForm1.Show 'open up form
End Sub
User Form: UserForm1
Option Explicit
'used by SW API
Dim swApp As SldWorks.SldWorks
Dim swDoc As ModelDoc2
Dim SelMgr As Object
Dim longstatus As Long, longwarnings As Long
Dim Feature As Object
Dim template As String
Dim dimvaluepreset As Integer
'gear specifications
Dim GR As Single
Dim pd As Single
Dim D1 As Single
Dim D2 As Single
Dim N1 As Single
Dim N2 As Single
Private Sub cmdCalculate_Click()
Dim i As Integer
If IsError() = True Then Exit Sub 'error handling
GR = Val(txt1.Text) 'grab values from text boxes
D1 = Val(txt2.Text)
D2 = Val(txt3.Text)
pd = Val(txt4.Text)
N1 = Val(txt5.Text)
N2 = Val(txt6.Text)
'The solver places highest priority on the GR, then the diams, then the diam pitch, then the teeth.
'loop around twice, in the event that GR had to be calculated from other values
For i = 0 To 1
If GR = 0 Then 'no GR provided; make sure it can be calculated
If D1 <> 0 And D2 <> 0 Then GR = D1 / D2
If N2 <> 0 And N1 <> 0 Then GR = N1 / N2
If GR = 0 Then
MsgBox "More values needed to calculate gear ratio.", vbExclamation, "Error"
Exit Sub
End If
ElseIf D1 <> 0 And D2 <> 0 And N1 <> 0 And N2 <> 0 And GR > 0 And GR < 1 Then 'in case user changes just GR, with all other values present
If D1 <> 0 And D2 <> 0 Then GR = D1 / D2
If N2 <> 0 And N1 <> 0 Then GR = N1 / N2
If GR < 1 Then
MsgBox "Gear ratio cannot be less than 1.", vbExclamation, "Error"
Exit Sub
End If
If GR <> 0 And D2 <> 0 Then D1 = GR * D2
If pd <> 0 And N1 <> 0 Then D1 = N1 / pd
If GR <> 0 And D1 <> 0 Then D2 = D1 / GR
If pd <> 0 And N2 <> 0 Then D2 = N2 / pd
If GR <> 0 And N2 <> 0 Then N1 = GR * N2
If D1 <> 0 And pd <> 0 Then N1 = D1 * pd
If GR <> 0 And N1 <> 0 Then N2 = N1 / GR
If D2 <> 0 And pd <> 0 Then N2 = D2 * pd
If D1 <> 0 And N1 <> 0 Then pd = N1 / D1
If N2 <> 0 And D2 <> 0 Then pd = N2 / D2
Else 'GR has been provided, several values missing
If GR <> 0 And N1 <> 0 Then N2 = N1 / GR
If GR <> 0 And N2 <> 0 Then N1 = GR * N2
If GR <> 0 And D1 <> 0 Then D2 = D1 / GR
If GR <> 0 And D2 <> 0 Then D1 = GR * D2
If D1 <> 0 And pd <> 0 Then N1 = D1 * pd
If D2 <> 0 And pd <> 0 Then N2 = D2 * pd
If pd <> 0 And N2 <> 0 Then D2 = N2 / pd
If pd <> 0 And N1 <> 0 Then D1 = N1 / pd
If D1 <> 0 And N1 <> 0 Then pd = N1 / D1
If N2 <> 0 And D2 <> 0 Then pd = N2 / D2
End If
Next i
If N1 = 0 Or N2 = 0 Or D1 = 0 Or D2 = 0 Or pd = 0 Then
MsgBox "More values needed for calculations.", vbExclamation, "Error"
Exit Sub
End If
'teeth must be a whole number; make them whole and then recalculate other values
N1 = Round(N1, 0)
N2 = Round(N2, 0)
If N2 = 0 Or N1 = 0 Then
MsgBox "Number of teeth cannot equal 0.", vbExclamation, "Error"
Exit Sub
End If
GR = N1 / N2
D1 = D2 * N1 / N2
D2 = D1 * N2 / N1
pd = N1 / D1
txt1.Text = GR
txt2.Text = D1
txt3.Text = D2
txt4.Text = pd
txt5.Text = N1
txt6.Text = N2
'test for interference
If (N1 > 1309 And N2 < 17) Or (N1 > 101 And N2 < 16) Or (N1 > 45 And N2 < 15) Or (N1 > 26 And N2 < 14) Or (N1 > 16 And N2 < 13) Or N2 < 13 Then
MsgBox "Interference likely. Prevent by decreasing gear ratio or increasing # of pinion teeth.", vbExclamation, "Alert"
End If
cmdCreate.Enabled = True
End Sub
Private Sub cmdCreate_Click()
Dim rootcirc As Single
Dim teethnum As Integer
Dim add As Single
Dim ded As Single
Dim thickness As Single
Dim topland As Single
Dim depth As Single
If IsNumeric(txtDepth.Text) = False Or txtDepth <= 0 Then 'error handling
MsgBox "Width must be a number greater than 0.", , "Error"
Exit Sub
End If
'tooth specifications
add = 1 / pd
ded = 1.25 / pd
thickness = 1.571 / pd
topland = 0.25 / pd
'width of the gear
depth = txtDepth.Text
'convert to mm if units were given in inches
If cboUnits.Text = "in" Then
rootcirc = rootcirc * 25.4
add = add * 25.4
ded = ded * 25.4
thickness = thickness * 25.4
topland = topland * 25.4
depth = depth * 25.4
D1 = D1 * 25.4
D2 = D2 * 25.4
End If
UserForm1.Hide
Set swApp = Application.SldWorks
'find location of default template
template = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swDefaultTemplatePart)
If template = "" Then
MsgBox "No default part template found. Cannot proceed."
Exit Sub
End If
'make 0, 1, or 2 gears depending on what user selected
If chkGear.Value = True Then
rootcirc = D1 - 2 * ded
teethnum = N1
Call swAPI(rootcirc, teethnum, add, ded, depth, thickness, topland)
End If
If chkPin.Value = True Then
rootcirc = D2 - 2 * ded
teethnum = N2
Call swAPI(rootcirc, teethnum, add, ded, depth, thickness, topland)
End If
If chkEnd.Value = True Then
End 'quit macro once finished
Else
UserForm1.Show
End If
End Sub
'====================API======================
Private Sub swAPI(rootcirc, teethnum, add, ded, depth, thickness, topland)
'creat new part document from default template
Set swDoc = swApp.NewDocument(template, 0, 0#, 0#)
'save user's prefs for inputting dim values, then toggle off so prompts don't pop up
dimvaluepreset = swApp.GetUserPreferenceToggle(swInputDimValOnCreate)
swApp.SetUserPreferenceToggle swInputDimValOnCreate, False
'change units to inches if inches were chosen
If cboUnits.Text = "in" Then swDoc.SetUserPreferenceIntegerValue swUnitsLinear, swINCHES
'selects the front plane
swDoc.Extension.SelectByID2 "front plane", "plane", 0, 0, 0, False, 0, Nothing, 0
'creates circle
swDoc.SketchManager.CreateCircle 0, 0, 0, 0.0282843, 0.0282843, 0
'adds a dimension (descriptive)
swDoc.AddDimension2 0.1, 0.1, 0
'changes the dimension
swDoc.Parameter("D1@Sketch1").SystemValue = 0.1
'next line contains depth of extrude
swDoc.FeatureManager.FeatureExtrusion2 True, False, False, 0, 0, depth / 1000, depth / 1000, False, False, False, False, 0.01, 0.01, False, False, False, False, 1, 1, 1, 0, 0, False
'draw center lines
swDoc.SketchManager.InsertSketch True
swDoc.Extension.SelectByID2 "Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0
swDoc.SketchManager.CreateCenterLine 0, 0, 0, 0, 0.04287068927272, 0
swDoc.SketchManager.CreateCenterLine 0, 0.04287068927272, 0, 0, 0.04742570000795, 0
swDoc.SketchManager.CreateCenterLine 0, 0.04742570000795, 0, 0, 0.05318644887897, 0
swDoc.SketchManager.CreateCenterLine 0, 0.05318644887897, 0, 0, 0.05739313526385, 0
'dimension from origin to end of 2nd center line
swDoc.Extension.SelectByID2 "Point1", "SKETCHPOINT", 0, 0, 0, False, 0, Nothing, 0
swDoc.Extension.SelectByID2 "Point4", "SKETCHPOINT", 0, 0.04742570000795, 0, True, 0, Nothing, 0
swDoc.AddDimension2 0.0162796, 0.0249603, 0
swDoc.Parameter("D1@Sketch2").SystemValue = 0.0474257
swDoc.Extension.SelectByID2 "Line2", "SKETCHSEGMENT", -0.004940102247032, 0.03768371805194, -0.0148975354874, False, 0, Nothing, 0
swDoc.AddDimension2 -0.0335335, 0.044907, 0
swDoc.Parameter("D2@Sketch2").SystemValue = 0.0045
swDoc.Extension.SelectByID2 "Line3", "SKETCHSEGMENT", -0.004980747191583, 0.04258816330296, -0.01718682723604, False, 0, Nothing, 0
swDoc.AddDimension2 0.0303254, 0.0509849, 0
swDoc.Parameter("D3@Sketch2").SystemValue = 0.005
swDoc.Extension.SelectByID2 "Line4", "SKETCHSEGMENT", -0.00583702285527, 0.04563111986872, -0.01835177625225, False, 0, Nothing, 0
swDoc.AddDimension2 -0.0164688, 0.0550186, 0
swDoc.Parameter("D4@Sketch2").SystemValue = 0.004
'add top landing, tooth thickness, root thickness
swDoc.SketchManager.CreateLine -0.002517731915248, 0.0564257, 0, 0.002654983169762, 0.0564257, 0
swDoc.Extension.SelectByID2 "Point6", "SKETCHPOINT", 0, 0.0564257, 0, True, 0, Nothing, 0
swDoc.SketchAddConstraints "sgATMIDDLE"
swDoc.ClearSelection2 True 'had to be kept since the contraints are still selected
swDoc.Extension.SelectByID2 "Line5", "SKETCHSEGMENT", -0.001996948007191, 0.05656115292476, -0.001922590469939, False, 0, Nothing, 0
swDoc.AddDimension2 0.000641405, 0.0635143, 0
swDoc.Parameter("D5@Sketch2").SystemValue = 0.005
'root arc
swDoc.SketchManager.CreateArc 0, 0, 0, 0.003835194946916, 0.03887228576205, 0, -0.004170121776003, 0.03883778321456, 0, 1
'tooth arc
swDoc.SketchManager.CreateArc 0, 0, 0, 0.006156213938993, 0.03417500342898, 0, -0.008667935067397, 0.03362583428112, 0, 1
'midpoint constraints
swDoc.Extension.SelectByID2 "Arc1", "SKETCHSEGMENT", -0.001154545730884, 0.0391762425732, 0.002539999999897, False, 0, Nothing, 0
swDoc.Extension.SelectByID2 "Point5", "SKETCHPOINT", 0, 0.0524257, 0, True, 0, Nothing, 0
swDoc.SketchAddConstraints "sgATMIDDLE"
'midpoint constraint
swDoc.Extension.SelectByID2 "Arc2", "SKETCHSEGMENT", 0.002036174726427, 0.03519823371213, 0.00254000000001, False, 0, Nothing, 0
swDoc.Extension.SelectByID2 "Point2", "SKETCHPOINT", 0, 0.0429257, 0, True, 0, Nothing, 0
swDoc.SketchAddConstraints "sgATMIDDLE"
'select 2 end points and tooth arc itself, add dim
swDoc.Extension.SelectByID2 "Point10", "SKETCHPOINT", -0.005596923749727, 0.05212608238713, 0, False, 0, Nothing, 0
swDoc.Extension.SelectByID2 "Point9", "SKETCHPOINT", 0.005596923749727, 0.05212608238713, 0, True, 0, Nothing, 0
swDoc.Extension.SelectByID2 "Arc1", "SKETCHSEGMENT", 0.001507436744187, 0.05232386534007, -0.002426444593793, True, 0, Nothing, 0
swDoc.AddDimension2 0.000576383, 0.0682822, 0
swDoc.Parameter("D6@Sketch2").SystemValue = 0.01
'select 2 end points and root arc itself, add dim
swDoc.Extension.SelectByID2 "Point12", "SKETCHPOINT", -0.01071494691579, 0.04156688144523, 0, False, 0, Nothing, 0
swDoc.Extension.SelectByID2 "Point11", "SKETCHPOINT", 0.01071494691579, 0.04156688144523, 0, True, 0, Nothing, 0
swDoc.Extension.SelectByID2 "Arc2", "SKETCHSEGMENT", 0.004707286101129, 0.04286122086851, -0.002527043759002, True, 0, Nothing, 0
swDoc.AddDimension2 -0.00297319, 0.0247553, 0
swDoc.Parameter("D7@Sketch2").SystemValue = 0.015
'spline
Dim vPoints As Variant
Dim pointArray() As Double
ReDim pointArray(0 To 8) As Double
pointArray(0) = 0.0025
pointArray(1) = 0.0564257
pointArray(2) = 0
pointArray(3) = 0.004992423427787
pointArray(4) = 0.05218744800053
pointArray(5) = 0
pointArray(6) = 0.007461899133492
pointArray(7) = 0.04227216320241
pointArray(8) = 0
vPoints = pointArray
swDoc.SketchManager.CreateSpline (vPoints)
ReDim pointArray(0 To 8) As Double
pointArray(0) = -0.0025
pointArray(1) = 0.0564257
pointArray(2) = 0
pointArray(3) = -0.004992423427787
pointArray(4) = 0.05218744800053
pointArray(5) = 0
pointArray(6) = -0.007461899133492
pointArray(7) = 0.04227216320241
pointArray(8) = 0
vPoints = pointArray
swDoc.SketchManager.CreateSpline (vPoints)
'get rid of dimension used for tooth thickness (will make extrude simpler)
swDoc.Extension.SelectByID2 "Arc1", "SKETCHSEGMENT", -0.01874777700611, 0.02922827323733, -0.01623724348762, False, 0, Nothing, 0
swDoc.EditDelete
'dimension the spline
swDoc.Extension.SelectByID2 "Point14", "SKETCHPOINT", -0.004992423427787, 0.05218744800053, 0, False, 0, Nothing, 0
swDoc.Extension.SelectByID2 "Point6", "SKETCHPOINT", 0, 0.0564257, 0, True, 0, Nothing, 0
swDoc.AddDimension2 -0.0025699, 0.0671196, 0
swDoc.Extension.SelectByID2 "Point13", "SKETCHPOINT", 0.004992423427787, 0.05218744800053, 0, False, 0, Nothing, 0
swDoc.Extension.SelectByID2 "Point8", "SKETCHPOINT", 0.0025, 0.0564257, 0, True, 0, Nothing, 0
swDoc.SetPickMode 'necessary
swDoc.Extension.SelectByID2 "Point13", "SKETCHPOINT", 0.004992423427787, 0.05218744800053, 0, False, 0, Nothing, 0
swDoc.Extension.SelectByID2 "Point6", "SKETCHPOINT", 0, 0.0564257, 0, True, 0, Nothing, 0
swDoc.AddDimension2 0.00274532, 0.0671466, 0
swDoc.Extension.SelectByID2 "Point13", "SKETCHPOINT", 0.004992423427787, 0.05218744800053, 0, False, 0, Nothing, 0
swDoc.Extension.SelectByID2 "Point4", "SKETCHPOINT", 0, 0.0474257, 0, True, 0, Nothing, 0
swDoc.AddDimension2 0.0106855, 0.0497722, 0
swDoc.Extension.SelectByID2 "D8@Sketch2@Part18.SLDPRT", "DIMENSION", 0, 0, 0, False, 0, Nothing, 0
swDoc.Extension.SelectByID2 "Point14", "SKETCHPOINT", -0.004992423427787, 0.05218744800053, 0, False, 0, Nothing, 0
swDoc.Extension.SelectByID2 "Point4", "SKETCHPOINT", 0, 0.0474257, 0, True, 0, Nothing, 0
swDoc.AddDimension2 -0.0166218, 0.0495298, 0
'extrude tooth
swDoc.Extension.SelectByID2 "Sketch2", "SKETCH", 0, 0, 0, False, 0, Nothing, 0
swDoc.FeatureManager.FeatureExtrusion True, False, False, 0, 0, depth / 1000, depth / 1000, False, False, False, False, 0.01745329251994, 0.01745329251994, False, False, False, False, 1, 1, 1
'circular pattern
Dim swPart As SldWorks.PartDoc
Dim vBodies As Variant
Dim swBody As SldWorks.Body2
Dim vFaces As Variant
Dim swFace As SldWorks.Face2
Dim swSurface As SldWorks.Surface
Dim swEnt As SldWorks.Entity
Dim swSelMgr As SldWorks.SelectionMgr
Dim swSelData As SldWorks.SelectData
Dim swFeat As SldWorks.Feature
Dim i As Integer
Set swApp = Application.SldWorks
Set swDoc = swApp.ActiveDoc
Set swPart = swDoc
Set swSelMgr = swDoc.SelectionManager
Set swSelData = swSelMgr.CreateSelectData
vBodies = swPart.GetBodies2(SwConst.swBodyType_e.swSolidBody, True)
Set swBody = vBodies(0)
vFaces = swBody.GetFaces
For i = 0 To UBound(vFaces)
Set swFace = vFaces(i)
Set swSurface = swFace.GetSurface
If swSurface.IsCylinder Then
Set swEnt = swFace
swEnt.Select4 False, swSelData
End If
Next i
swDoc.InsertAxis2 False
swDoc.Extension.SelectByID2 "Boss-Extrude2", "BODYFEATURE", 0, 0, 0, False, 4, Nothing, 0
swDoc.Extension.SelectByID2 "Axis1", "AXIS", 0, 0, 0, True, 1, Nothing, 0
swDoc.FeatureManager.FeatureCircularPattern2 teethnum, 2 * 3.14159 / teethnum, False, "NULL", False
'change dimensions of center lines, then rebuild
swDoc.Parameter("D1@Sketch1").SystemValue = rootcirc / 1000 'diameter
swDoc.Parameter("D1@Sketch2").SystemValue = rootcirc / 2000 'radius
swDoc.Parameter("D2@Sketch2").SystemValue = add / 1000 'its not necessary to use the addendum here, but it will work fine so we'll use it
swDoc.Parameter("D3@Sketch2").SystemValue = ded / 1000
swDoc.Parameter("D4@Sketch2").SystemValue = add / 1000
swDoc.Parameter("D8@Sketch2").SystemValue = Sqr(add ^ 2 + (thickness / 2) ^ 2) / 1000
swDoc.Parameter("D9@Sketch2").SystemValue = Sqr(ded ^ 2 + (thickness / 2) ^ 2) / 1000
swDoc.Parameter("D10@Sketch2").SystemValue = Sqr(ded ^ 2 + (thickness / 2) ^ 2) / 1000
swDoc.Parameter("D6@Sketch2").SystemValue = Sqr(add ^ 2 + (thickness / 2) ^ 2) / 1000
swDoc.Parameter("D5@Sketch2").SystemValue = topland / 1000
swDoc.Parameter("D7@sketch2").SystemValue = (thickness * 0.1 + thickness) / 1000 'root arc, make is slightly larger than tooth thickness
swDoc.ForceRebuild3 True
swDoc.GraphicsRedraw2
swDoc.ShowNamedView2 "*Isometric", 7
swDoc.ViewZoomtofit
'restore user's prefs for inputting dim values
swApp.SetUserPreferenceToggle swInputDimValOnCreate, dimvaluepreset
End Sub
'===================ERROR HANDLING====================
Function IsError() As Boolean
'make sure the textboxes only include numbers
If txt1.Text <> "" And IsNumeric(txt1.Text) = False Then
MsgBox "Text boxes can only contain numbers.", vbExclamation, "Error"
IsError = True
End If
If txt4.Text <> "" And IsNumeric(txt4.Text) = False Then
MsgBox "Text boxes can only contain numbers.", vbExclamation, "Error"
IsError = True
End If
If txt3.Text <> "" And IsNumeric(txt3.Text) = False Then
MsgBox "Text boxes can only contain numbers.", vbExclamation, "Error"
IsError = True
End If
If txt2.Text <> "" And IsNumeric(txt2.Text) = False Then
MsgBox "Text boxes can only contain numbers.", vbExclamation, "Error"
IsError = True
End If
If txt6.Text <> "" And IsNumeric(txt6.Text) = False Then
MsgBox "Text boxes can only contain numbers.", vbExclamation, "Error"
IsError = True
End If
If txt5.Text <> "" And IsNumeric(txt5.Text) = False Then
MsgBox "Text boxes can only contain numbers.", vbExclamation, "Error"
IsError = True
End If
If Val(txt1.Text) < 0 Or Val(txt2.Text) < 0 Or Val(txt3.Text) < 0 Or Val(txt4.Text) < 0 Or Val(txt5.Text) < 0 Or Val(txt6.Text) < 0 Then
MsgBox "Text boxes must contain postive numbers.", vbExclamation, "Error"
IsError = True
End If
End Function
'if the user changes anything in the text box then don't allow them to create gear, they need to recalculate
Private Sub txt4_Change()
cmdCreate.Enabled = False
End Sub
Private Sub txt2_Change()
cmdCreate.Enabled = False
End Sub
Private Sub txt5_Change()
cmdCreate.Enabled = False
End Sub
Private Sub txt1_Change()
cmdCreate.Enabled = False
End Sub
Private Sub txt3_Change()
cmdCreate.Enabled = False
End Sub
Private Sub txt6_Change()
cmdCreate.Enabled = False
End Sub
'==================MISC=================
Private Sub cmdHelp_Click()
MsgBox "To create a gear:" & vbCrLf & "1. Enter specs." & vbCrLf & "2. Click Calculate." & vbCrLf & _
"3. If errors, modify specs. Otherwise, click Create." & vbCrLf & vbCrLf & _
"FAQ:" & vbCrLf & "Q: Why do I have to enter exactly 3 values?" & vbCrLf & _
"A: GC needs the gear ratio (or a way to calculate it) as well as at least" & vbCrLf & _
"one piece of info about each gear (e.g., diameter or teeth)." & vbCrLf & vbCrLf & _
"Q: Why am I getting ""interference likely""?" & vbCrLf & _
"A: Given your current specifications, undercutting will probably occur." & vbCrLf & _
"Gear Creator doesn't prevent you from using such specs, however." & vbCrLf & "It simply warns you." & vbCrLf & vbCrLf & _
"Q: Why can't I get the exact gear ratio I want?" & vbCrLf & _
"A: # of teeth must be a whole number, and this controls the gear ratio." & vbCrLf & vbCrLf & _
"Q: How do I create a gear train?" & vbCrLf & _
"A: Turn off ""Quit upon completion"". After the first gear and pinion are made," & vbCrLf & _
"changing only the pinion teeth and diameter. De-select Gear and then click Create." & vbCrLf & vbCrLf & _
"Q: What gear standards does GC follow?" & vbCrLf & _
"A: GC uses AGMA standards for a 20 deg pressure angle." & vbCrLf & _
"The profile is not a perfect involute, but with backlash any difference is negligible.", , "Help"
End Sub
Private Sub cmdCancel_Click()
End 'close the user form and macro if user cancels
End Sub
'clear spec boxes
Private Sub cmdClear_Click()
txt1.Text = ""
txt2.Text = ""
txt3.Text = ""
txt4.Text = ""
txt5.Text = ""
txt6.Text = ""
End Sub
'info
Private Sub UserForm_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
MsgBox "Spur Gear Creator v1.0" & vbCrLf & "1/19/09 4:20PM" & vbCrLf & "Author: Keith Rice" & vbCrLf & "keith@cadsharp.com", , "About"
End Sub