Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System.Windows.Forms
Imports System.Drawing

Public Class FileAnalyzer

    Public swApp As SldWorks

    Private Sub FileAnalyzer_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        tvFeatures.SuspendLayout()
        tvFeatures.BeginUpdate()
        tvFeatures.Nodes.Clear()

        Dim openDoc As ModelDoc2 = swApp.ActiveDoc
        openDoc.ClearSelection2(True)
        Dim featMgr As FeatureManager = openDoc.FeatureManager
        Dim configs As String() = openDoc.GetConfigurationNames()
        Dim features As Object() = featMgr.GetFeatures(True)
        Dim config As Configuration = openDoc.ConfigurationManager.ActiveConfiguration
        Dim Owner As Feature
        Dim supStates As Boolean()
        Dim supState As Boolean = False
        For Each feat As Feature In features
            Owner = feat.GetOwnerFeature()
            If Owner Is Nothing Then

                'If Not feat.GetTypeName().ToUpper().EndsWith("FOLDER") AndAlso Not feat.GetTypeName().ToUpper().StartsWith("AEMSIMFEATURE") AndAlso Not feat.GetTypeName().ToUpper().StartsWith("FLATPATTERN") AndAlso Not feat.GetTypeName().ToUpper().StartsWith("SHEETMETAL") AndAlso Not feat.GetTypeName().ToUpper().StartsWith("SMBASEFLANGE") AndAlso Not feat.GetTypeName().ToUpper().StartsWith("ATTRIBUTE") AndAlso Not feat.Name.ToUpper().StartsWith("ANNOTATIONS") AndAlso Not feat.Name.ToUpper().StartsWith("FRONT") AndAlso Not feat.Name.ToUpper().StartsWith("TOP") AndAlso Not feat.Name.ToUpper().StartsWith("RIGHT") AndAlso Not feat.Name.ToUpper().StartsWith("ORIGIN") AndAlso Not feat.Name.ToUpper().StartsWith("MATES") AndAlso Not feat.Name.ToUpper().StartsWith("MATEGROUP") Then
                Dim newNode As New TreeNode(feat.Name)
                newNode.Name = feat.Name
                If openDoc.GetType() = 1 Then
                    supStates = DirectCast(feat.IsSuppressed2(DirectCast(swInConfigurationOpts_e.swAllConfiguration, Int32), configs), Boolean())
                    For ss As Integer = 0 To supStates.Length - 1
                        If Not supStates(ss) Then
                            newNode.Nodes.Add(configs(ss))
                        End If
                    Next
                Else
                    For Each cfgName As String In configs
                        config = openDoc.GetConfigurationByName(cfgName)
                        supState = config.GetComponentSuppressionState(feat.Name)
                        If Not supState Then
                            newNode.Nodes.Add(cfgName)
                        End If
                    Next
                End If
                tvFeatures.Nodes.Add(newNode)
                'End If
            End If
        Next

        For Each tNode As TreeNode In tvFeatures.Nodes
            If tNode.GetNodeCount(True) = 0 Then
                tNode.ForeColor = Color.Red
                'tNode.Selected = True
            End If
        Next

        tvFeatures.EndUpdate()
        tvFeatures.ResumeLayout()

    End Sub

    Private Sub tvFeatures_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tvFeatures.MouseDoubleClick
        Dim thisTree As TreeView = DirectCast(sender, TreeView)
        Dim openDoc As ModelDoc2 = swApp.ActiveDoc
        openDoc.ClearSelection2(True)
        Dim featMgr As FeatureManager = openDoc.FeatureManager
        Dim features As Object() = featMgr.GetFeatures(True)
        For Each feat As Feature In features
            Dim nodeArray As TreeNode() = tvFeatures.Nodes.Find(feat.Name, False)
            If nodeArray.Length > 0 Then
                If nodeArray(0) IsNot Nothing Then
                    If nodeArray(0).IsSelected Then
                        feat.Select2(True, 0)
                    End If
                End If
            End If
        Next
    End Sub
End Class