3 Replies Latest reply on Jan 20, 2015 5:58 PM by Mark Olsen

    Component is selected, but doesn't become visible

    Mark Olsen

      I've been working on code to show all envelopes for selected sub-assemblies.  This code gets the sub-components that are envelopes, but the line to make them visible doesn't work.  Anyone have an idea why?

       

       

      Dim swApp As SldWorks.SldWorks
      Dim swPart As SldWorks.ModelDoc2
      Dim swSelMgr As SelectionMgr
      Dim myComp As Object
      Dim mySubPart As Object
      Dim boolstatus As Boolean
      Dim c As Integer
      Dim i As Integer

       

      Sub main()

      Set swApp = Application.SldWorks
      Set swPart = swApp.ActiveDoc
      Set swSelMgr = swPart.SelectionManager
      c = swSelMgr.GetSelectedObjectCount


      For i = 1 To c
          Set myComp = swSelMgr.GetSelectedObject6(i, -1)
          Set mySubPart = myComp.GetModelDoc2
          If mySubPart.GetType = 2 Then
         
                  Dim swSubAssy As SldWorks.AssemblyDoc
                  Dim vComps As Variant
                  Dim swComp As SldWorks.Component2
                  Dim j As Integer
                 
                  Set swSubAssy = mySubPart
                  vComps = swSubAssy.GetComponents(False)
                  For j = 0 To UBound(vComps)
                      Set swComp = vComps(j)
                      Debug.Print swComp.Visible
                      Debug.Print swComp.IsEnvelope
                      If swComp.IsEnvelope Then swComp.Visible = True
                  Next
          End If
      Next

      End Sub

       

      Thanks,

      Mark

        • Re: Component is selected, but doesn't become visible
          Keith Rice

          Mark,

           

          The problem is that you are making the component in the underlying assembly visible, not the active assembly. This is because IComponent2::GetModelDoc2 returns the underlying model document. So what you need to do is bring the component pointer from the underlying context to the active model context. This is accomplished using IComponent2::GetCorresponding. The following code gets an arbitrary features in the component, brings it into the active model context, and then gets the component:

           

          Set swFeat = swChildComp.FirstFeature
          Set swFeat = swComp.GetCorresponding(swFeat)
          Set swChildComp = swFeat.GetComponent
          

           

          Here is the full code:

           

          Dim swApp As SldWorks.SldWorks
          Dim swModel As SldWorks.ModelDoc2
          Dim swSelMgr As SelectionMgr
          Dim swComp As SldWorks.Component2
          Dim swChildComp As SldWorks.Component2
          Dim swCompModel As SldWorks.ModelDoc2
          Dim swCompAssy As SldWorks.AssemblyDoc
          Dim swFeat As SldWorks.Entity
          Dim vComps As Variant
          Dim i As Integer, j As Integer
          
          Sub main()
              Set swApp = Application.SldWorks
              Set swModel = swApp.ActiveDoc
              Set swSelMgr = swModel.SelectionManager
             
              For i = 1 To swSelMgr.GetSelectedObjectCount
                  If swSelMgr.GetSelectedObjectType3(i, -1) = swSelCOMPONENTS Then
                      Set swComp = swSelMgr.GetSelectedObject6(i, -1)
                      Set swCompModel = swComp.GetModelDoc2
                      If swCompModel.GetType = swDocASSEMBLY Then
                          Set swCompAssy = swCompModel
                          vComps = swCompAssy.GetComponents(False)
                          If IsEmpty(vComps) = False Then
                              For j = 0 To UBound(vComps)
                                  Set swChildComp = vComps(j)
                                  If swChildComp.IsEnvelope Then
                                      Set swFeat = swChildComp.FirstFeature
                                      Set swFeat = swComp.GetCorresponding(swFeat)
                                      Set swChildComp = swFeat.GetComponent
                                      swChildComp.Visible = True
                                  End If
                              Next
                          End If
                      End If
                  End If
              Next
          
          End Sub
          

           

          Keith

          SolidWorks API Training

            • Re: Component is selected, but doesn't become visible
              Mark Olsen

              Works perfectly.  Thanks for your help.

               

              Mark

              • Re: Component is selected, but doesn't become visible
                Mark Olsen

                Well, I spoke too soon.  The macro from Keith works only as long as there are no configurations in the component, or the 'in-use' component matches the 'referenced' component in the assembly.  I modified the macro as follows (added lines in bold):

                 

                Dim swApp As SldWorks.SldWorks
                Dim swModel As SldWorks.ModelDoc2
                Dim swSelMgr As SelectionMgr
                Dim swComp As SldWorks.Component2
                Dim swChildComp As SldWorks.Component2
                Dim swCompModel As SldWorks.ModelDoc2
                Dim swCompAssy As SldWorks.AssemblyDoc
                Dim swFeat As SldWorks.Entity
                Dim vComps As Variant
                Dim i As Integer, j As Integer

                Sub main()
                    Set swApp = Application.SldWorks
                    Set swModel = swApp.ActiveDoc
                    Set swSelMgr = swModel.SelectionManager

                    For i = 1 To swSelMgr.GetSelectedObjectCount
                        If swSelMgr.GetSelectedObjectType3(i, -1) = swSelCOMPONENTS Then
                            Set swComp = swSelMgr.GetSelectedObject6(i, -1)
                            Set swCompModel = swComp.GetModelDoc2
                            If swCompModel.GetType = swDocASSEMBLY Then
                                Set swCompAssy = swCompModel
                                vComps = swCompAssy.GetComponents(False)
                                If IsEmpty(vComps) = False Then
                                    For j = 0 To UBound(vComps)
                                        Set swChildComp = vComps(j)
                                        If swChildComp.IsEnvelope Then
                                            Set swFeat = swChildComp.FirstFeature
                                            Dim strOrigConfig As String
                                            strOrigConfig = swComp.ReferencedConfiguration
                                            Dim strNewConfig As String
                                            strNewConfig = swChildComp.ReferencedConfiguration
                                            swComp.ReferencedConfiguration = strNewConfig
                                            Set swFeat = swComp.GetCorresponding(swFeat)
                                            Set swChildComp = swFeat.GetComponent
                                            swChildComp.Visible = True
                                            swComp.ReferencedConfiguration = strOrigConfig
                                        End If
                                    Next
                                End If
                            End If
                        End If
                    Next

                End Sub

                 

                Basically, it now gets both the in use and referenced configuration names, changes the component to the 'in use' configuration, makes the sub component (the envelope) visible, then changes the component back to the 'referenced' configuration.  Kind of a klunky workaround, but it seems to work.

                 

                Mark