2 Replies Latest reply on May 6, 2016 10:56 PM by Sanya Shmidt

    How to get a solidbody object from an array based on the description from ComboBox?

    Sanya Shmidt

      Hello Guys!

       

      Below is the code I`m using to get a list of solidbodies. I`m adding Structural Members only into my Combobox.

       

      I need to show that body on the drawing when user chooses it from drop down list. I`m having problems referencing solidbodies from the array based on user choice.

      Can someone point me into the right direction please?

       

      Thanks,

      Alex.

       

      '#################################################################################

      Option Explicit

       

      Dim swApp As SldWorks.SldWorks

      Dim swModel As SldWorks.ModelDoc2

      Dim swSelMgr As SldWorks.SelectionMgr

      Dim swView As SldWorks.View

      Dim nbrBodies As Long

      Dim arrBody As Variant

      Dim swBody As SldWorks.Body2

      Dim swFace As SldWorks.Face2

      Dim swEnt As SldWorks.Entity

      Dim swSelData As SldWorks.SelectData

      Dim bool As Boolean

      Dim arrBodiesIn As Variant

      Dim Bodies(0) As Object

      Dim i As Long

      Dim objType As Long

       

      Sub main()

       

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

      Set swModel = swApp.ActiveDoc

      Set swSelMgr = swModel.SelectionManager

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

       

      If (swView Is Nothing) Then

          MsgBox "View not selected."

          Exit Sub

      End If

       

      nbrBodies = swView.GetBodiesCount

          Debug.Print "Number of bodies: " & nbrBodies

          If (nbrBodies < 1) Then

              MsgBox "No bodies in selected view."

              Exit Sub

          End If

      arrBody = swView.Bodies

       

      For i = 0 To UBound(arrBody)

          Set swBody = arrBody(i)

       

          Set swSelData = swSelMgr.CreateSelectData

          swSelData.View = swView

          bool = swBody.Select2(False, swSelData)

        

              ' Object type 76 is a solid body

              objType = swSelMgr.GetSelectedObjectType3(1, -1)

             

      '        If (objType = 76) Then

      '            Debug.Print " Object type: solid body"

      '        End If

             

              If (Not (swSelSOLIDBODIES = swSelMgr.GetSelectedObjectType3(1, -1))) Then

                  MsgBox "Solid body not found."

              End If

             

              Set swFace = swBody.GetFirstFace

             

              Do While Not swFace Is Nothing

                  Set swEnt = swFace

                  ' Select using IEntity

                  bool = swEnt.Select4(True, swSelData): Debug.Assert bool

                  Set swFace = swFace.GetNextFace

              Loop

              Debug.Print "   Name of body: " & swBody.GetSelectionId

          

          ' adding Descriptions on to the combo box

          With frm_main.ComboBox_Bodies

              If Mid(swBody.GetSelectionId, 1, 17) = "Structural Member" Then

                  .AddItem (swBody.GetSelectionId)

              End If

          End With

         

          Next i

       

      swModel.ClearSelection2 True

      '#################################################################################

       

      So here when user chooses the Item from drop down I have to reference it and show:

       

      Private Sub ComboBox_Bodies_Change()

      ' Get the bodies from referenced model

      Set swModel = swView.ReferencedDocument

      arrBody = swModel.GetBodies2(swSolidBody, True)

      If (nbrBodies = 1) Then

         swView.Bodies = (arrBody)

      Else

       

       

         ' Set the body to view, HOW TO REFERENCE IT TO swBody.GetSelectionId AND MY COMBOBOX?

         Set Bodies(0) = arrBody(0)

         arrBodiesIn = Bodies

        

         swView.Bodies = (arrBodiesIn)

      End If

       

       

      swModel.ClearSelection2 True

      End Sub

       

      Any help is appreciated

        • Re: How to get a solidbody object from an array based on the description from ComboBox?
          Michael Spens

          Cool idea Sanya!  Edit your form code like the following.  You may also need to make swView and Bodies both declared Public in your module.

           

          Private Sub ComboBox_Bodies_Change()

          ' Get the bodies from referenced model

          Dim swModel As ModelDoc2

          Set swModel = swView.ReferencedDocument

          arrBody = swModel.GetBodies2(swSolidBody, True)

          If (nbrBodies = 1) Then

             swView.Bodies = (arrBody)

          Else

           

           

             ' Set the body to view, HOW TO REFERENCE IT TO swBody.GetSelectionId AND MY COMBOBOX?

             Set Bodies(0) = GetSpecificBody(ComboBox_Bodies.Text, swModel)

             arrBodiesIn = Bodies

           

             swView.Bodies = (arrBodiesIn)

          End If

           

           

          swModel.ClearSelection2 True

          End Sub

           

           

          Function GetSpecificBody(bodyName As String, swModel As ModelDoc2) As Body2

              Dim mBodies As Variant

              Dim part As PartDoc

              Set part = swModel

              mBodies = part.GetBodies2(swSolidBody, True)

              Dim b As Body2

              Dim i As Integer

              For i = 0 To UBound(mBodies)

                  Set b = mBodies(i)

                  If InStr(1, bodyName, b.Name, vbTextCompare) <> 0 Then

                      Set GetSpecificBody = b

                      Exit Function

                  End If

              Next i

             

          End Function