4 Replies Latest reply on Jan 2, 2017 8:54 AM by Norbert Gal

    GetType of suppressed virtual component in assembly?

    Norbert Gal

      Hi,

       

      I have to check some "properties" on all components in an opened assembly (loop through all sub-assembly level).

      I created a loop for it, and first of all I have to check the name and if current component is assembly or not?

      This seem easy at first sight but I stucked, because if the "inspected" component is virtual and suppressed, I have no idea how to get these informations.

       

      This is a shoerened code with "looper" function:

       

      Option Explicit

      Public swApp As SldWorks.SldWorks

      Public swModel As ModelDoc2

       

      Sub Main()

      Set swApp = Application.SldWorks

      Set swModel = swApp.ActiveDoc

      LevelLoop swModel     'first call of "looper" with currently opened assembly

       

      End Sub

       

      Function LevelLoop(swModelBase As ModelDoc2)

          Dim vComps As Variant

          Dim swAssy As SldWorks.AssemblyDoc

          Dim swComp As SldWorks.Component2

          Dim swModel As ModelDoc2

          Dim i As Integer

          Dim name As String

          Dim compType as String


          If swModelBase.GetType = swDocASSEMBLY Then

              Set swAssy = swModelBase

              vComps = swAssy.GetComponents(True)

              If Not IsEmpty(vComps) Then

                  For i = 0 To UBound(vComps)     'loop through all of current asm's components

                      Set swComp = vComps(i)    

                      Set swModel = swComp.GetModelDoc2

       

                      'get component name and type..

                      If swComp.GetSuppression <> 0 Then

      'if component is RESOLVED or VIRTUAL, "swComp.Name" and "swModel.GetType" works 

                          name = swComp.Name    

                          If swModel.GetType = swDocASSEMBLY Then compType = "ASM" Else compType = ""

                      Else

      'if component is SUPPRESSED, "swComp.Name" and "swModel.GetType" doesn't works,

      'therefore that was my idea to decide component asm or not, but VIRTUAL components has no path :/

                          name = swComp.GetPathName    

                          If UCase(Right(swComp.GetPathName, 6)) = "SLDASM" Then compType = "ASM" Else compType = ""

                      End If

       

       

                      If compType = "ASM" Then

                          If swComp.GetSuppression <> 0 Then

                              DoSomething

                              LevelLoop swModel     'the looper function "goes inside" of the current assembly

                          End If

                      End If

       

                  Next i

              End If

          End If

      End Function

       

      Anybody has any idea?

       

      Thank you

        • Re: GetType of suppressed virtual component in assembly?
          John Alexander

          Can you elaborate on what happens with swModel when the component is suppressed? That doesn't seem right to me. Is it returning an empty string or is it throwing an error?

           

          Set swModel = swComp.GetModelDoc2: debug.assert Not swModel is Nothing

           

          In my experience, when this assertion fails there is a broken reference (file moved or deleted, if you try to unsuporess it manually, it will prompt you to find it on the system).

           

          • Re: GetType of suppressed virtual component in assembly?
            Luke Malpass

            You are getting that error because the swModel is Nothing then you try and access it.

             

            There is no issue accessing the information you need, however you want to change how you are iterating.

             

            Instead, take a look at the top level model do model.FeatureByPositionReverse(model.GetFeatureCount() - 1), 0). This will get you a Feature. On that feature loop it. Here is some C# code I write that you can read to convert to VBA, that accepst the Feature from the above call as the start and iterates through all items in the assembly:

             

            private void TraverseFeatures(Feature firstfeature, int indentLevel)
            {
                Feature feat = firstfeature;

             

                while (feat != null)
                {
                    if (string.Equals(feat.GetTypeName2(), "Reference", StringComparison.InvariantCultureIgnoreCase))
                    {
                        Component2 tempComp = (Component2)feat.GetSpecificFeature2();

             

                        ProcessComponent(tempComp, indentLevel);

             

                        TraverseFeatures((Feature)tempComp.FirstFeature(), indentLevel + 1);
                     }
                           
                    feat = (Feature)feat.GetNextFeature();
                }
            }

             

            Then in the ProcessComponent you can call tempComp.IsSuppressed() to get the suppression state. You can do tempComp.GetPathName() to get the path, and anything else thats in the Component model