2 Replies Latest reply on Oct 1, 2016 3:09 AM by Paolo Comand

    How to retrieve a specific named dimension in model

    Paolo Comand

      I need to retrieve a dimension with a specific name but I don't know the owner feature.
      EG I need to find all dimensions that have name "LPX@some feature@mypart.sldprt" without know what is "some feature". I have done it traversing the entire part for each single feature examining any single dimension. Is there a direct method without traversing the entire part or assembly?

      Thanks in advance

        • Re: How to retrieve a specific named dimension in model
          Keith Rice

          Unfortunately, no, you will always have to traverse the part or assembly. Here I created a function called FindDimension that might make that simpler for you.

           

          '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
          

           

          Keith

          SolidWorks API Training and Services