4 Replies Latest reply on May 2, 2018 1:43 PM by Tyler Nichol

    Macro to insert general table and populate with configuration-specific custom properties

    Tyler Nichol

      Would anyone be able to help me write a macro to do the following:

       

      *********

      Preconditions: In Drawing file, with a Drawing View selected

       

      Commands:

      1) Insert General Table from a template, attached to anchor

      2) Add row for each configuration present in referenced part file that has a boolean Configuration-Specific Custom Property value set to Yes.

      3) Populate table with Configuration-Specific Custom Property values from each included configuration

      *********

       

      Even without a macro I don't seem able to link table values to Configuration-Specific Custom Properties belonging to a configuration that is not referenced by a drawing view in my drawing. So is it even possible to create a macro to do what I want?

        • Re: Macro to insert general table and populate with configuration-specific custom properties
          Solid Air

          Macro is below.

           

          Preconditions: Drawing Active and Drawing View selected.

           

          Because you did not mention names of custom properties you are interested in, macro uses

           

          ShowInTable:  Set to Yes, row is create in table and populated with configuration name, part number and description

           

          PartNumber:  part number assigned to configuration

           

          Description: description assigned to configuration

           

          You will need to modify to your needs.

           

          macro also shows header row and assigns "My Title" to it.  Omit code if you do not want this.

           

          Example part has 4 configurations.  Configuration 003 was set to not show in table.

           

          Option Explicit

           

          Dim swApp As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2

          Dim swSelectionMgr As SldWorks.SelectionMgr

          Dim swView As SldWorks.View

          Dim swViewDoc As SldWorks.ModelDoc2

          Dim swTableAnnotation As SldWorks.TableAnnotation

          Dim swCustPropMgr As SldWorks.CustomPropertyManager

          Dim swDrawing As SldWorks.DrawingDoc

          Dim TableRows As Long

          Dim ConfigNames As Variant

          Dim ConfigName As Variant

          Dim ValOut As String

          Dim ResValOut As String

          Dim WasRes As Boolean

           

          Sub InsertGeneralTable()

           

              Set swApp = Application.SldWorks

             

              Set swModel = swApp.ActiveDoc

             

              Set swSelectionMgr = swModel.SelectionManager

             

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

             

              Set swViewDoc = swView.ReferencedDocument

             

              ConfigNames = swViewDoc.GetConfigurationNames

             

              TableRows = 1

             

              For Each ConfigName In ConfigNames

             

                  Set swCustPropMgr = swViewDoc.Extension.CustomPropertyManager(ConfigName)

                 

                  swCustPropMgr.Get5 "ShowInTable", False, ValOut, ResValOut, WasRes

                 

                  If ValOut = "Yes" Then

                 

                      TableRows = TableRows + 1

                     

                  End If

                 

              Next

             

              Set swDrawing = swModel

             

              Set swTableAnnotation = swDrawing.InsertTableAnnotation2(True, 0, 0, swBOMConfigurationAnchorType_e.swBOMConfigurationAnchor_TopRight, "", TableRows, 3)

             

              swTableAnnotation.BorderLineWeight = 1

             

              swTableAnnotation.SetHeader swTableHeaderPosition_e.swTableHeader_Top, 1

             

              swTableAnnotation.Title = "My Title"

             

              swTableAnnotation.TitleVisible = True

             

              swTableAnnotation.Text(1, 0) = "Config Name"

             

              swTableAnnotation.Text(1, 1) = "Part Number"

             

              swTableAnnotation.Text(1, 2) = "Description"

             

              TableRows = 2

             

              For Each ConfigName In ConfigNames

             

                  Debug.Print ConfigName

                 

                  Set swCustPropMgr = swViewDoc.Extension.CustomPropertyManager(ConfigName)

                 

                  swCustPropMgr.Get5 "ShowInTable", False, ValOut, ResValOut, WasRes

                 

                  If ValOut = "Yes" Then

                 

                      swTableAnnotation.Text(TableRows, 0) = ConfigName

                     

                      swCustPropMgr.Get5 "PartNumber", False, ValOut, ResValOut, False

                     

                      swTableAnnotation.Text(TableRows, 1) = ValOut

                     

                      swCustPropMgr.Get5 "Description", False, ValOut, ResValOut, False

                     

                      swTableAnnotation.Text(TableRows, 2) = ValOut

                     

                      TableRows = TableRows + 1

                     

                  End If

                 

              Next

             

              swModel.EditRebuild3

             

          End Sub