10 Replies Latest reply on Apr 13, 2017 3:53 AM by Ross John

    How to rewrite this line from VBA into VB.NET?

    Ross John

      Hi Forum,

       

      I am following the example from Ivana Kolin to save the selections and then reuse them. Saving Assembly selection set

       

      Her code was in VBA. I would like to rewrite it into VB.NET. I could actually do the most job on myself. Except one line. I would like to ask, how to "translate" swComponent(j).Select True from VBA into VB.NET.

       

      Hier I would like to paste code from Inana Kolin from the link above, with my question...

      Sub main()

      Dim swApp As SldWorks.SldWorks

      Dim Part As SldWorks.ModelDoc2

      Dim SelMgr As SldWorks.SelectionMgr

      Dim swComponent() As SldWorks.Component2

      Dim i As Integer

      Dim j As Integer

      Set swApp = GetObject(, "SldWorks.Application")

       

      Set Part = swApp.ActiveDoc

      Set SelMgr = Part.SelectionManager

      i = SelMgr.GetSelectedObjectCount

      ReDim swComponent(i)

      For j = 0 To i

      Set swComponent(j) = SelMgr.GetSelectedObjectsComponent3(j, -1)

      Next j

       

      Part.ClearSelection

       

      For j = 0 To i

      If Not swComponent(j) Is Nothing Then

      swComponent(j).Select True ' Question: How to rewrite this line into VB.NET?

      End If

      Next j

      End Sub

        • Re: How to rewrite this line from VBA into VB.NET?
          VIGNESH NAGARAJAN

          Hi Ross,

          I have tried the code in VB.NET. It works Fine. Just try this out.

           

                  Dim swApp As SldWorks.SldWorks

                  Dim Part As SldWorks.ModelDoc2

                  Dim SelMgr As SldWorks.SelectionMgr

                  Dim swComponent() As SldWorks.Component2

                  Dim i As Integer

                  Dim j As Integer

           

           

                  swApp = CreateObject("sldworks.application")

                  Part = swApp.ActiveDoc

                  SelMgr = Part.SelectionManager

                  i = SelMgr.GetSelectedObjectCount

                  ReDim swComponent(i)

                  For j = 0 To i

                      swComponent(j) = SelMgr.GetSelectedObjectsComponent3(j, -1)

                  Next j

                  Part.ClearSelection()

           

           

                  For j = 0 To i

                      If Not swComponent(j) Is Nothing Then

                          swComponent(j).Select(True)

                      End If

                  Next j

           

           

              End Sub

            • Re: How to rewrite this line from VBA into VB.NET?
              Ross John

              Thanks Vignesh! I've never thought of adding the parenthesis. So easy is it! 

               

              But honestly speaking, it seems I have another very stupid question... What does the "component" exactly mean in API language? Does it literally only mean a part in an assembly? Or a component could be anything that I can select, such as an edge, a face, or even a part, or, anything?

               

              I ask so, because I have a surface in the assembly selected. After running your code, the part is highlighted, which, in my point of view, means that the surface was deleted from selection but the part was selected.

               

              Does it also means, that the GetSelectedObjectsComponent3 could ONLY select the part?

               

              Thanks again!

              Ross

            • Re: How to rewrite this line from VBA into VB.NET?
              Patrik MacHalec

              Hi Ross,

              try IComponent::Select4 method. It should be what you are looking for.