This macro was created to quickly create washer shaped objects, but a variety of shapes are possible. With the right settings you could quickly create rods, spheres, o-rings, square profile o-rings, spacers, toroids (donuts), and tubes. To create a sphere, leave either the inner or outer diameter box empty and set the radius to half the diameter of the desired part. For a perfect sphere the diameter and length values will be equal while the radius value will be half the diameter value.

CreateWasher.bmp
CreateWasher.swp

  1.  
  2. 'precondition: Adjust line 23 of the code below to point to your version of solidWorks' Part.prtdot file.
  3.  
  4. Dim swApp As Object
  5. Dim part As Object
  6. Dim boolstatus As Boolean
  7. Dim longstatus As Long, longwarnings As Long
  8. Dim swModelDocExt As SldWorks.ModelDocExtension
  9. Public firstEnter As Boolean
  10. Public firstEnter1 As Boolean
  11. Public firstEnter2 As Boolean
  12. Public firstEnter3 As Boolean
  13. Public firstEnter4 As Boolean
  14. Public isChamfer As Boolean
  15.  
  16. Sub main()
  17. firstEnter = True
  18. firstEnter1 = True
  19. firstEnter2 = True
  20. firstEnter3 = True
  21. firstEnter4 = True
  22. Set swApp = Application.SldWorks
  23. Set part = swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2017\templates\Part.prtdot", 0, 0, 0) 'This line opens up SolidWorks' default part document. If you don't have SW 2017, or you've installed SolidWorks to a different location, this line will need to be changed.
  24. 'Set part = swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2016\templates\Part.prtdot", 0, 0, 0)
  25. 'Set part = swApp.NewDocument("C:\ProgramData\SolidWorks\SolidWorks 2013\templates\Part.prtdot", 0, 0, 0)
  26. Set part = swApp.ActiveDoc
  27. UserForm1.Show
  28. End Sub
  29.  
  30. Sub createShape(inner, outer, length, chamfer)
  31. boolstatus = part.Extension.SelectByID2("Top Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
  32. part.SketchManager.InsertSketch True
  33. part.ClearSelection2 True
  34. part.SketchManager.AddToDB = True 'Without this line close diameters merge. Example: a 1 inch circle and a 1.01 inch circle snap together.
  35. Dim skSegment As Object
  36. If inner <> 0 Then
  37. Set skSegment = part.SketchManager.CreateCircle(0#, 0#, 0#, inner, 0#, 0#)
  38. End If
  39. If outer <> 0 Then
  40. Set skSegment = part.SketchManager.CreateCircle(0#, 0#, 0#, outer, 0#, 0#)
  41. End If
  42. part.SketchManager.AddToDB = False
  43. Dim myFeature As Object
  44. Set myFeature = part.FeatureManager.FeatureExtrusion2(True, False, False, 0, 0, length, chamfer, False, False, False, False, 1.74532925199433E-02, 1.74532925199433E-02, False, False, False, False, True, True, True, 0, 0, False)
  45. part.SelectionManager.EnableContourSelection = False
  46. Set swModelDocExt = part.Extension
  47. swModelDocExt.SelectAll
  48. If isChamfer Then
  49. Set myFeature = part.FeatureManager.InsertFeatureChamfer(6, 1, chamfer, 0.78539816339745, 0, 0, 0, 0)
  50. Else
  51. Set myFeature = part.FeatureManager.FeatureFillet3(195, chamfer, 0.01, 0, 0, 0, 0, (radiiArray0), (dist2Array0), (conicRhosArray0), (setBackArray0), (pointArray0), (pointDist2Array0), (pointRhoArray0))
  52. End If
  53. End
  54. End Sub
  55.  

      Most of the UserForm1 code controls how the text boxes behave when you click on them. Instead of labels, the text in each text box acts as a label and is removed the first time a user clicks in the text box. If no chamfer or radius is desired, leave the word chamfer in the Chamfer text box or remove the word chamfer and leave the Chamfer text box empty. Likewise if you wish to create a rod shape rather than a tube shape you can leave either the Inner Diameter or Outer Diameter text boxes empty or with their original text (Inner Diameter Outer Diameter).

UserForm1 Code:


  1.  
  2. Private Sub Create_Click()
  3. Dim inner As Double
  4. Dim outer As Double
  5. Dim length As Double
  6. Dim chamfer As Double
  7. If TextBox1.Text = "Inner Diameter" Or TextBox1.Text = "" Then
  8. inner = 0
  9. Else
  10. inner = TextBox1.Text * 0.0254 * 0.5
  11. End If
  12. If TextBox2.Text = "Outer Diameter" Or TextBox2.Text = "" Then
  13. outer = 0
  14. Else
  15. outer = TextBox2.Text * 0.0254 * 0.5
  16. End If
  17. length = TextBox3.Text * 0.0254
  18. If TextBox4.Text = "Chamfer" Or TextBox4.Text = "" Then
  19. chamfer = 0
  20. Else: chamfer = TextBox4.Text * 0.0254
  21. End If
  22. Call createShape(inner, outer, length, chamfer)
  23. End Sub
  24.  
  25. Private Sub OptionButton1_Click()
  26. isChamfer = True
  27. If firstEnter4 Then TextBox4.Text = "Chamfer"
  28. End Sub
  29.  
  30. Private Sub OptionButton2_Click()
  31. isChamfer = False
  32. If firstEnter4 Then TextBox4.Text = "Radius"
  33. End Sub
  34.  
  35. Private Sub TextBox1_Enter()
  36. If (firstEnter = True) Then
  37. firstEnter = False
  38. TextBox1.TextAlign = fmTextAlignLeft
  39. TextBox1.Text = ""
  40. End If
  41. End Sub
  42.  
  43. Private Sub TextBox2_Enter()
  44. If firstEnter2 Then
  45. TextBox2.Text = ""
  46. TextBox2.TextAlign = fmTextAlignLeft
  47. firstEnter2 = False
  48. End If
  49. End Sub
  50.  
  51. Private Sub TextBox3_Enter()
  52. If firstEnter3 Then
  53. TextBox3.TextAlign = fmTextAlignLeft
  54. TextBox3.Text = ""
  55. firstEnter3 = False
  56. End If
  57. End Sub
  58.  
  59. Private Sub TextBox4_Enter()
  60. If firstEnter4 Then
  61. TextBox4.TextAlign = fmTextAlignLeft
  62. TextBox4.Text = ""
  63. firstEnter4 = False
  64. End If
  65. End Sub
  66.  
  67. Private Sub TextBox5_Enter()
  68. TextBox5.Visible = False
  69. End Sub
  70.  
  71. Private Sub UserForm_Initialize()
  72. isChamfer = True
  73. OptionButton1.Value = True
  74. End Sub
  75.  

Notes:


A "compile error: Expected variable or procedure, not project" was triggered when the subroutine createShape was named createWasher. A subroutine name cannot match the macro file name. CreateWasher.swp cannot have a subroutine inside of it called createWasher, so it seems.

When the value in the Inner Diameter text box was almost the same size as the value in the Outer Diameter text box, the sketch would appear to draw only one circle. Because of snapping, the second circle was never drawn. The solution to this issue is found on or around line 32 of the main code:
part.SketchManager.AddToDB = True
This line of code some how bypasses the snapping portion of the drawing process.