MACRO LIBRARY

Change sketch dimensions via user form

Free public access. No account or subscription required.

Files

Change sketch dimensions via user form Associated model

Code

Module: mMain

'Demonstrates how to modify dimension values in a part using a user form.
    
'Preconditions: A part is open containing dimensions specified in the constants below.
    
'Note: This macro contains minimal error handling. For example, it does not include:
'-Check to verify that a model with the specified dimensions is open when the user clicks Update.
'-Check to verify that the user form input is a valid dimension value.
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Public Const DIMENSION_NAME_WIDTH As String = "D1@Sketch1"
Public Const DIMENSION_NAME_DEPTH As String = "D1@Boss-Extrude1"
    
Sub main()
    UserForm1.Show
End Sub

User Form: UserForm1

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDim As SldWorks.Dimension
Dim vValue As Variant
    
Private Sub cmdUpdate_Click()
    'Get Width
    Set swDim = swModel.Parameter(DIMENSION_NAME_WIDTH)
    swDim.SetValue3 CDbl(txtWidth.Text), swThisConfiguration, Empty
    
    'Get Depth
    Set swDim = swModel.Parameter(DIMENSION_NAME_DEPTH)
    swDim.SetValue3 CDbl(txtDepth.Text), swThisConfiguration, Empty
    
    'Rebuild
    swModel.ForceRebuild3 False
End Sub
    
Private Sub UserForm_Initialize()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
    If swModel Is Nothing Then End
    
    'Get Width
    Set swDim = swModel.Parameter(DIMENSION_NAME_WIDTH)
    If swDim Is Nothing Then
        swApp.SendMsgToUser "Couldn't locate " & DIMENSION_NAME_WIDTH
        End
    End If
    vValue = swDim.GetValue3(swThisConfiguration, Empty)
    txtWidth.Text = vValue(0)
    
    'Get Depth
    Set swDim = swModel.Parameter(DIMENSION_NAME_DEPTH)
    If swDim Is Nothing Then
        swApp.SendMsgToUser "Couldn't locate " & DIMENSION_NAME_DEPTH
        End
    End If
    vValue = swDim.GetValue3(swThisConfiguration, Empty)
    txtDepth.Text = vValue(0)
End Sub