========== mMain ===========

'Add Config v1.0.0

'Creates a new configuration in support.SLDPRT for a certain mounting hole diameter.

'Preconditions: support.SLDPRT is open
'Results: configuration is added, part is saved, eDrawing is saved out

'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com

Public Const DIAMETER_MIN As Double = 1
Public Const DIAMETER_MAX As Double = 30
Public Const DIAMETER_DEFAULT As Double = 10

Public Const DEBUG_MODE As Boolean = False

Sub main()
    UserForm1.Show vbModeless
End Sub


========== UserForm1 ===========


Private Sub cmdAdd_Click()
    Process cboDiameter.Text
End Sub

Private Sub UserForm_Initialize()
    Dim i As Integer
    For i = DIAMETER_MIN To DIAMETER_MAX
        cboDiameter.AddItem (i)
    Next i
    
    cboDiameter.ListIndex = DIAMETER_DEFAULT - 1
End Sub


======== mSolidWorks =======

Sub Process(dblDiameter As Double)
    On Error GoTo ErrHandler
    
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swDim As SldWorks.Dimension
    
    'connect to SolidWorks and the active document
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
    'make sure active document is a part
    If swModel Is Nothing Then
        MsgBox "Please open a part."
        Exit Sub
    End If
    
    If swModel.GetType <> swDocPART Then
        MsgBox "Please open a part."
        Exit Sub
    End If
    
    'add a new configuration
    swModel.AddConfiguration3 dblDiameter & "mm", Empty, Empty, Empty
    
    'get the dimension and change its value, then rebuild
    Set swDim = swModel.Parameter("Hole Dia.@Sketch5")
    swDim.SetSystemValue3 dblDiameter / 1000, swThisConfiguration, Nothing
    swModel.ForceRebuild3 True
    
    'save the part and save out an eDrawing
    If DEBUG_MODE = False Then
        swModel.Save3 swSaveAsOptions_Silent, Empty, Empty
        swModel.Extension.SaveAs Replace(swModel.GetPathName, ".SLDPRT", ".EPRT"), _
            swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, Empty, Empty
    End If
        
    Exit Sub

ErrHandler:
    MsgBox "Unhandled exception in Process(): " & Err.Description
End Sub
