MACRO LIBRARY

Configure dimension according to user input

Free public access. No account or subscription required.

Files

Configure dimension according to user input Associated model

Code

Module: mMain

'For use in webinar "Introduction to the SolidWorks API", June 1, 2012 12:00 EST
    
'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
    
Sub main()
    UserForm1.Show
End Sub

User Form: UserForm1

Private Sub cmdAdd_Click()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swDim As SldWorks.Dimension
    Dim dblDiameter As Double
    
    'connect to SolidWorks and the active document
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
    'make sure active document is a part
    If swModel.GetType <> swDocPART Then
        MsgBox "Please open a part."
        Exit Sub
    End If
    
    'get and test user input
    dblDiameter = txtDiameter.Text / 1000
    If dblDiameter <= 0.002 Or dblDiameter >= 0.025 Then
        MsgBox "Enter a value between 2mm and 25mm."
        Exit Sub
    End If
    
    'add a new configuration
    swModel.AddConfiguration3 dblDiameter * 1000, Empty, Empty, 0
    
    'get the dimension and change its value, then rebuild
    Set swDim = swModel.Parameter("Hole Dia.@Sketch5")
    swDim.SetSystemValue3 dblDiameter, swThisConfiguration, Nothing
    swModel.EditRebuild3
    
    'save the part and save out an eDrawing
    swModel.Save3 swSaveAsOptions_Silent, Empty, Empty
    swModel.Extension.SaveAs Replace(swModel.GetPathName, "SLDPRT", "EPRT"), _
        swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, Empty, Empty
End Sub