10 Replies Latest reply on Jun 4, 2014 4:00 PM by Adam Hoffman

    How to get if a selected component is transparent or not in vba

    Adam Hoffman

      Hello, I'm wondering how to get whether a selected component is transparent or not. I looked in the Api help and the display state example was a little out of my league. I was wondering how this could be done. I don't need any other info on the display state, just if it's transparent or not. Any help would be appreciated. Thanks.

        • Re: How to get if a selected component is transparent or not in vba
          Matt Martens

          Use the selection manager to get the IComponent2 object of the selected component. Then try IComponent2::GetRenderMaterials and IRenderMaterial::Transparency. I'm not sure if this will get the part level or component level transparency.

            • Re: How to get if a selected component is transparent or not in vba
              Adam Hoffman

              Thanks for the tip, but the value returned from the component level transparency is zero no matter if the component is tranparent or not.

                • Re: How to get if a selected component is transparent or not in vba
                  Matt Martens

                  Apparently, it returns the part level transparency. To verify this, open the part and change it to transparent and then try again. If the macro returns anything other than 0, then this is the case.

                   

                  In the meantime, I will look elsewhere.

                    • Re: How to get if a selected component is transparent or not in vba
                      Adam Hoffman

                      I changed the part to transparent and it still returned zero.

                        • Re: How to get if a selected component is transparent or not in vba
                          Matt Martens

                          I got it. Use IComponent2::GetMaterialPropertyValues2. This will return an array of doubles. The value you are looking for is the 8th one so the array index would be 7. Any double value in any of the array values of -1 indicates that the component level has not been set. So if you check for a value of -1 of the 8th array object you will know if the part is transparent or not.

                            • Re: How to get if a selected component is transparent or not in vba
                              Adam Hoffman

                              I keep getting a type mismatch when I try to get the array of doubles:

                               

                              Dim swApp As SldWorks.SldWorks

                              Dim swModel As SldWorks.ModelDoc2
                              Dim swAssembly As SldWorks.AssemblyDoc

                              Dim boolstatus As Boolean
                              Dim longstatus As Long, longwarnings As Long

                              Dim swSelMgr As SldWorks.SelectionMgr
                              Dim swComp As SldWorks.Component2
                              Dim varMatProp As Variant
                              Dim TValue As Double

                              Sub main()

                              Set swApp = _
                              Application.SldWorks

                              Set swModel = swApp.ActiveDoc

                              Dim ModelType As Integer
                              ModelType = swModel.GetType()

                              If ModelType = swDocumentTypes_e.swDocASSEMBLY Then

                              Set swAssembly = swModel

                              Else

                              MsgBox ("You Must Have An Assembly Document Open To Run This Macro")

                              Exit Sub

                              End If

                              Set swSelMgr = swModel.SelectionManager
                              Dim swEnt As SldWorks.Entity
                              Set swEnt = swSelMgr.GetSelectedObject5(1)

                              If swEnt Is Nothing Then
                              MsgBox ("No Entity Selected!")
                              Exit Sub
                              End If

                              Set swComp = swEnt.GetComponent

                              If swComp Is Nothing Then
                              MsgBox ("No Component Selected, Try Again!")
                              Exit Sub
                              End If

                               

                              'RIGHT HERE IS MY ERROR

                              Set varMatProp = swComp.GetMaterialPropertyValues2(swInConfigurationOpts_e.swThisConfiguration, Nothing)

                               

                               

                              If Not varMatPropArray Is Nothing Then

                              TValue = varMatProp(7)

                              MsgBox (CStr(TValue))

                              End If

                               

                              I've tried a few different things and can't get it to work. Basically all I need it to do is to toggle the transparency on and off like this:

                               

                              If selectedcomponent.IsTransparent Then

                              boolstatus = swAssembly.SetComponentTransparent(False)

                              Else

                              boolstatus = swAssembly.SetComponentTransparent(True)

                              End If

                                • Re: How to get if a selected component is transparent or not in vba
                                  Matt Martens

                                  Don't use the Set at the beginning of the line. The variable varMatProp is not an object so you don't need set.

                                   

                                  The next line will now show an error because this is not a method to check if an array has values. Use "If UBound(varMatProp) > LBound(varMatProp) Then" instead.

                                    • Re: How to get if a selected component is transparent or not in vba
                                      Adam Hoffman

                                      Thanks, I'm still a beginner at programming and started learning in VB.Net using Visual Studio 2013 Express. So alot of VB6 language still eludes me, because I never use it. But I figured writing this one in a .swp file would suffice.The tip you gave me ended up working great, Thanks!!! Here is what I ended up with for the transparency issue:

                                       

                                      varMatProp = swComp.GetMaterialPropertyValues2(swInConfigurationOpts_e.swThisConfiguration, Nothing)

                                      If UBound(varMatProp) > LBound(varMatProp) Then

                                      TValue = varMatProp(7)

                                      End If

                                      If TValue = 0.75 Then

                                      boolstatus = swAssembly.SetComponentTransparent(False)

                                      ElseIf TValue = -1 Then

                                      boolstatus = swAssembly.SetComponentTransparent(True)

                                      End If

                                      swModel.ClearSelection2 True

                                       

                                      I've got it programmed to a button on my 3D Mouse now and it's amazing just how much time it saves when checking large assemblies. Thanks again!