VBA macro to get the value of a dimension in a sketch?

I have a reference to an IComponent2 within the context of the active document. I also have the name of a dimension that I’m trying to get the value from “D1@Dimensions”. Does anyone have a macro that can do this ?

Public Function GetDimensionValue(ByVal swComponent as Component2, ByVal dimension as String) as Double 
' code goes here

End Function 


Close enough :

Public Function GetDimensionValue(ByVal mainAssemblyModelDoc As ModelDoc2, ByVal swComponent As Component2, ByVal dimensionName As String) As Double
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim swDispDim As SldWorks.DisplayDimension
    Dim swDim As SldWorks.dimension     
    Dim dimValue As Variant
    Dim status As Boolean
    Set swSelMgr = mainAssemblyModelDoc.SelectionManager

    status = mainAssemblyModelDoc.Extension.SelectByID2(dimensionName + "@" + swComponent.GetSelectByIDString, "DIMENSION", 0, 0, 0, False, 0, Nothing, 0)

    Set swDispDim = swSelMgr.GetSelectedObject6(1, -1)

    Set swDim = swDispDim.GetDimension
    dimValue = swDim.GetSystemValue2(swComponent.ReferencedConfiguration)
    GetDimensionValue = dimValue

End Function
1 Like

Yes, building the complete string to the dimension seems to be the only way. I get dimension values the exact same way.

1 Like