Files
Code
Module: mMain
'Demonstrates how to find a dimension by its name if the owning feature is unknown.
'Precondition: Model is open and constant values are specified. Searching occurs in
' different contexts depending on whether the IModelDoc2 argument passed in is a
' part or assembly.
'Result: FindDimension returns the **first** IDimension object beginning with
' DIMENSION_NAME or Nothing if not found.
'Notes:
'-If you only want to search a single component, pass in the underlying IModelDoc2
' object of that component, obtained from IComponent2::GetModelDoc2.
'-The topLevelOnly argument in FindDimension only applies to parts and assemblies.
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
Const DIMENSION_NAME As String = "D1"
Const TOP_LEVEL_ONLY As String = False
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDim As SldWorks.Dimension
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swDim = FindDimension(swModel, DIMENSION_NAME, TOP_LEVEL_ONLY)
If swDim Is Nothing Then
MsgBox "Dimension not found"
Else
MsgBox "Dimension found: " & swDim.GetNameForSelection
End If
End Sub
Private Function FindDimension(model As SldWorks.ModelDoc2, dimName As String, topLevelOnly As Boolean) As SldWorks.Dimension
Dim i As Integer
Set FindDimension = Nothing
If TypeOf model Is SldWorks.PartDoc Then
Dim swFeat As SldWorks.Feature
Dim vFeats As Variant
vFeats = model.FeatureManager.GetFeatures(topLevelOnly)
If IsEmpty(vFeats) Then Exit Function
For i = 0 To UBound(vFeats)
Set swFeat = vFeats(i)
Set FindDimension = model.Parameter(dimName & "@" & swFeat.name)
If Not FindDimension Is Nothing Then Exit For
Next i
ElseIf TypeOf model Is SldWorks.AssemblyDoc Then
Dim swAssy As SldWorks.AssemblyDoc
Dim swComp As SldWorks.Component2
Dim swCompModel As SldWorks.ModelDoc2
Dim vComps As Variant
Set swAssy = model
vComps = swAssy.GetComponents(topLevelOnly)
If IsEmpty(vComps) Then Exit Function
For i = 0 To UBound(vComps)
Set swComp = vComps(i)
Set swCompModel = swComp.GetModelDoc2
If Not swCompModel Is Nothing Then
Set FindDimension = FindDimension(swCompModel, dimName, topLevelOnly)
If Not FindDimension Is Nothing Then Exit For
End If
Next i
End If
End Function