0 Replies Latest reply on Apr 15, 2015 2:30 AM by Andreas Killer

    VBA: GetSelectedObjectCount2 = 0 after double click something?

    Andreas Killer

      I've developed a "QuickEdit" tool (a Userform in VBA), when I e.g. select a face my tool detects the selected feature, get all dimensions related to that feature and allows to edit them (set a fit, modify tolerances, calculate the middle of the tolerances to create a CAM model, etc.), works pretty good.


      But when I double click e.g. a face, I can see the dimensions in SW and obvious something is selected:



      Unfortunately it seems not possible to detect what is selected, GetSelectedObjectCount2 is always 0, below is my test code. Any ideas?




      Option Explicit


      Enum swMarkType
        swMarkAll = -1 'All selections regardless of marks
        swNoMarks = 0  'Only the selections without marks
        'Any other value = Value that was used to mark and select an object
      End Enum


      Sub Test_SelectedFeature()
        Dim swApp As SldWorks.SldWorks
        Dim swModel As SldWorks.ModelDoc2
        Dim swSelMgr As SldWorks.SelectionMgr
        Dim swSelObj As Object
        Dim i As Long
        Dim Data, Msg
        Dim InSketch As Boolean
        Dim swFeat As SldWorks.Feature
        Set swApp = Application.SldWorks
        Set swModel = swApp.ActiveDoc
        Set swSelMgr = swModel.SelectionManager
        'If we are in a sketch, the feature is the sktech itself
        If Not swModel.GetActiveSketch2 Is Nothing Then
          Set swFeat = swModel.GetActiveSketch2
          InSketch = True
          'Check the selected things
          For i = 1 To swSelMgr.GetSelectedObjectCount2(swMarkAll)
            Set swSelObj = swSelMgr.GetSelectedObject6(i, swMarkAll)
            Select Case swSelMgr.GetSelectedObjectType3(i, swMarkAll)
              Case swSelEDGES
                Data = swSelObj.GetTwoAdjacentFaces2
                Set swFeat = Data(0).GetFeature
              Case swSelectType_e.swSelFACES
                Set swFeat = swSelObj.GetFeature
              Case swSelVERTICES
                Data = swSelObj.GetAdjacentFaces
                Set swFeat = Data(0).GetFeature
              Case swSelDIMENSIONS
                Dim swDim As SldWorks.Dimension
                Set swDim = swSelObj.GetDimension
                Set swFeat = swDim.GetFeatureOwner()
              Case swSelEXTSKETCHPOINTS
                Msg = "Sketchpoint"
              Case swSelDATUMPLANES
                Msg = "Plane"
              Case Else
                Debug.Print "swSelectType_e." & swSelMgr.GetSelectedObjectType2(i)
            End Select
            If Not swFeat Is Nothing Then Exit For
        End If
        If Not swFeat Is Nothing Then
          MsgBox "Selected: " & swFeat.Description
        ElseIf Not IsEmpty(Msg) Then
          MsgBox Msg & " selected. Can't find the corresponding feature"
          MsgBox "Number of selected items: " & swSelMgr.GetSelectedObjectCount2(swMarkAll)
        End If
      End Sub