• Home
  • Blog
  • AI in SOLIDWORKS: Create Parts Automatically Using Smart Macros!

AI in SOLIDWORKS: Create Parts Automatically Using Smart Macros!

image

AI in SOLIDWORKS: Create Parts Automatically Using Smart Macros!

Unlock the future of CAD automation! In this video, I show you how to integrate AI with SOLIDWORKS to automatically create 3D parts using smart macros.

If you’re still modelling manually in 2025, you’re already behind — AI-powered macros can speed up your workflow, reduce repetitive tasks, and instantly generate code for part creation.

Cope the below macro code:

 

Option Explicit

Sub main()
    Dim swApp As Object, swModel As Object
    Dim swSketchMgr As Object, swFeatMgr As Object, swExt As Object
    
    ' Connect to SolidWorks (Late Binding)
    On Error Resume Next
    Set swApp = GetObject(, "SldWorks.Application")
    If swApp Is Nothing Then
        Set swApp = CreateObject("SldWorks.Application")
        If swApp Is Nothing Then
            MsgBox "Unable to start SolidWorks.", vbCritical
            Exit Sub
        End If
    End If
    On Error GoTo 0
    swApp.Visible = True
    
    ' Template path (update for your version, e.g. 2026)
    Dim templatePath As String
    templatePath = "C:\ProgramData\SolidWorks\SOLIDWORKS 2026\templates\Part.prtdot"
    If Dir(templatePath) = "" Then
        MsgBox "Template file not found at: " & vbCrLf & templatePath, vbCritical
        Exit Sub
    End If
    
    ' Create new part
    Set swModel = swApp.NewDocument(templatePath, 0, 0, 0)
    If swModel Is Nothing Then
        MsgBox "Failed to create new part.", vbCritical
        Exit Sub
    End If
    
    ' ISO bolt data arrays (M1–M36, coarse threads)
    Dim sizes, dVals, sVals, kVals, pVals, lengthOptions
    sizes = Array("M1", "M1.2", "M1.6", "M2", "M2.5", "M3", "M4", "M5", _
                  "M6", "M8", "M10", "M12", "M14", "M16", "M18", "M20", _
                  "M22", "M24", "M27", "M30", "M33", "M36")
    
    dVals = Array(1, 1.2, 1.6, 2, 2.5, 3, 4, 5, _
                  6, 8, 10, 12, 14, 16, 18, 20, _
                  22, 24, 27, 30, 33, 36)
    
    sVals = Array(2, 2.5, 3.2, 4, 5, 5.5, 7, 8, _
                  10, 13, 16, 18, 21, 24, 27, 30, _
                  34, 36, 41, 46, 50, 55)
    
    kVals = Array(0.8, 1, 1.3, 1.6, 2, 2.4, 3.2, 4, _
                  4, 5.3, 6.4, 7.5, 8.8, 10, 11.5, 12.5, _
                  14, 15, 17, 19, 21, 23)
    
    pVals = Array(0.25, 0.25, 0.35, 0.4, 0.45, 0.5, 0.7, 0.8, _
                  1, 1.25, 1.5, 1.75, 2, 2, 2.5, 2.5, _
                  2.5, 3, 3, 3.5, 3.5, 4)
    
    ' Ask user for bolt size
    Dim sizeList As String, i As Integer
    For i = LBound(sizes) To UBound(sizes)
        sizeList = sizeList & sizes(i) & IIf(i < UBound(sizes), ", ", "")
    Next
    Dim boltSize As String
    boltSize = InputBox("Select Bolt Size from: " & vbCrLf & sizeList, "ISO Bolt Generator", "M6")
    boltSize = UCase(Trim(boltSize))
    
    Dim idx As Integer: idx = -1
    For i = LBound(sizes) To UBound(sizes)
        If sizes(i) = boltSize Then idx = i: Exit For
    Next
    If idx = -1 Then
        MsgBox "Invalid bolt size entered.", vbCritical
        Exit Sub
    End If
    
    ' Length options
    lengthOptions = Array(6, 8, 10, 12, 16, 20, 25, 30, 35, 40, 45, 50, _
                          60, 70, 80, 90, 100, 120, 150, 200)
    
    Dim lenList As String
    For i = LBound(lengthOptions) To UBound(lengthOptions)
        lenList = lenList & lengthOptions(i) & IIf(i < UBound(lengthOptions), ", ", "")
    Next
    Dim Lmm As Double
    Lmm = Val(InputBox("Select Bolt Length (mm) from: " & vbCrLf & lenList, "ISO Bolt Generator", lengthOptions(0)))
    If Lmm <= 0 Then
        MsgBox "Invalid length.", vbCritical
        Exit Sub
    End If
    
    ' Convert mm to meters
    Dim d As Double, s As Double, k As Double, L As Double, pitch As Double
    d = dVals(idx) / 1000#
    s = sVals(idx) / 1000#
    k = kVals(idx) / 1000#
    L = Lmm / 1000#
    pitch = pVals(idx) / 1000#
    
    Dim callout As String
    callout = sizes(idx) & "x" & CStr(pVals(idx))
    
    ' Geometry Creation
    Set swSketchMgr = swModel.SketchManager
    Set swFeatMgr = swModel.FeatureManager
    Set swExt = swModel.Extension
    
    ' Shaft
    swExt.SelectByID2 "Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0
    swSketchMgr.InsertSketch True
    swSketchMgr.CreateCircleByRadius 0, 0, 0, d / 2#
    swSketchMgr.InsertSketch True
    swFeatMgr.FeatureExtrusion2 True, False, False, 0, 0, L, 0, False, False, False, False, 0, 0, _
                                 False, False, False, False, True, True, True, 0, 0, False
    
    ' Hex head
    swExt.SelectByID2 "Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0
    swSketchMgr.InsertSketch True
    swSketchMgr.CreatePolygon 0, 0, 0, 0, s / 2#, 0, 6, False
    swSketchMgr.InsertSketch True
    swFeatMgr.FeatureExtrusion2 True, False, False, 0, 0, k, 0, False, False, False, False, 0, 0, _
                                 False, False, False, False, True, True, True, 0, 0, False
    
    ' Cosmetic Thread
    Dim selEdgeOk As Boolean
    selEdgeOk = swExt.SelectByID2("", "EDGE", d / 2#, 0, 0, False, 0, Nothing, 0)
    If Not selEdgeOk Then
        selEdgeOk = swExt.SelectByID2("", "EDGE", d / 2#, 0, L, False, 0, Nothing, 0)
    End If
    If Not selEdgeOk Then
        MsgBox "Could not select shaft edge for cosmetic thread.", vbExclamation
        Exit Sub
    End If
    
    Dim threadDia As Double
    threadDia = d - 0.0002
    
    Dim myFeature As Object
    Set myFeature = swFeatMgr.InsertCosmeticThread2(False, threadDia, L, callout)
    If myFeature Is Nothing Then
        MsgBox "Failed to create cosmetic thread.", vbCritical
        Exit Sub
    End If
    
    ' Make cosmetic threads visible
    swApp.SetUserPreferenceToggle 9, True   ' 9 = swDetailingDisplayCosmeticThreads
    swModel.GraphicsRedraw2
    
    MsgBox "ISO Bolt " & sizes(idx) & " x " & Lmm & " mm with visible cosmetic thread (" & callout & ") created successfully!", vbInformation
End Sub

 

 

Use it, modify it, and automate your workflow!

This is not just a tutorial — it’s the start of smart, automated CAD design.

If you found this helpful, make sure to like, comment, and subscribe for more SOLIDWORKS + AI tutorials, automation hacks, and advanced CAD workflow videos.