8 Replies Latest reply on Jun 18, 2016 6:40 AM by Yong Ning

    How to select bomtable in Feature.

    Yong Ning

      b1.jpg

      Select bomtable code  result is

      *****************

      ITEM NO.

      PART NUMBER

      QTY.

      ******************

       

      Sub main()

          Dim swApp                   As SldWorks.SldWorks

          Dim SwModel                 As SldWorks.ModelDoc2

          Dim swSelMgr                As SldWorks.SelectionMgr

          Dim swTable                 As SldWorks.TableAnnotation

          Dim swBomTable              As SldWorks.BomTableAnnotation

          Dim i                       As Long

          ''

          Set swApp = Application.SldWorks

          Set SwModel = swApp.ActiveDoc

          Set swSelMgr = SwModel.SelectionManager

          Set swTable = swSelMgr.GetSelectedObject5(1)  

          ''

          For i = 0 To swTable.ColumnCount

              Debug.Print swTable.GetColumnTitle(i)

          Next i

      End Sub

      ******************

       

      Help. know bomtable name. How to select bomtable?

       

      b2.jpg

       

       

      for example. Know  bomtable name of Bill of Materials1

        

      'boolstatus = SwModel.Extension.SelectByID2("Bill of Materials1", "BOMFEATURE", 0, 0, 0, False, 0, Nothing, 0)

       

       

          Dim swBomTable              As SldWorks.BomTableAnnotation

          Set swTable = ????????????

       

      Thanks

          • Re: How to select bom in Feature.
            Yong Ning

            Thank your code

             

            Dim swApp As SldWorks.SldWorks

            Dim sBOMname As String

            Dim swView As SldWorks.View

            Dim swSheetView As SldWorks.View

            Dim swModel As SldWorks.ModelDoc2

            Dim swDrawing As SldWorks.DrawingDoc

            Dim swBomFeat   As SldWorks.BomFeature

            Dim swFeat As SldWorks.Feature

            Dim swBOM As SldWorks.Feature

            Sub Main()

                Dim swBomTable              As SldWorks.BomTableAnnotation

                Set swApp = Application.SldWorks

                Set swModel = swApp.ActiveDoc

                Dim swSelMgr As SelectionMgr

                Dim swTable As TableAnnotation

                Set swDrawing = swModel

                Set swSelMgr = swDrawing.SelectionManager

                Set swFeat = swDrawing.FirstFeature   

                'Get BOM name

                Do While Not swFeat Is Nothing

                    If "BomFeat" = swFeat.GetTypeName Then

                        Set swBomFeat = swFeat.GetSpecificFeature2

                        Set swBOM = swBomFeat.GetFeature

                        sBOMname = swBOM.Name

                        swModel.Extension.SelectByID2 sBOMname, "BOMFEATURE", 0, 0, 0, False, 0, Nothing, 0

                        Debug.Print sBOMname           

                        Stop

                        Set swTable = swSelMgr.GetSelectedObject5(1)    → error is Type mismatch (error 13)       

                        Exit Do

                    End If

                    Set swFeat = swFeat.GetNextFeature

                Loop       

            End Sub

            **************************************

             

            Help me

            Dim swTable As TableAnnotation

            Set swTable = swSelMgr.GetSelectedObject5(1)

             

            the code error is Type mismatch (error 13)

             

            Set swTable = ????????

              • Re: How to select bom in Feature.
                Simon Turner

                swModel.Extension.SelectByID2 sBOMname, "BOMFEATURE", 0, 0, 0, False, 0, Nothing, 0

                will return a BomFeature object.

                So, declare a BomFeature variable:

                 

                Dim myBom as BomFeature

                Set myBom = swSelMgr.GetSelectedObject5(1)

                 

                Then use myBom.GetTableAnnotations

                • Re: How to select bomtable in Feature.
                  Yong Ning

                  Sub del20160617()

                      Dim Xls As Excel.Application, Rng As Range

                          Set Xls = GetObject(, "Excel.Application")

                      Dim Sht As Worksheet

                          Set Sht = Xls.Worksheets("Drawing")

                      

                      Dim BomRng As Range, WidthFactorRng As Range, BomXyRng As Range

                      ''

                      Dim ColWidthRng As Range, RowHightRng As Range, tRng As Range

                           With Sht

                                Set BomRng = .Range(.Cells(5, 2).Formula)

                                Set WidthFactorRng = .Range(.Cells(5, 3).Formula)

                                Set BomXyRng = .Range(.Cells(5, 4).Formula)

                                Set ColWidthRng = .Range(.Cells(5, 5).Formula)

                                Set RowHightRng = .Range(.Cells(5, 6).Formula)

                                ''Debug.Print BomRng.Address, WidthFactorRng.Address, BomXyRng.Address, ColWidthRng.Address, RowHightRng.Address

                           End With

                      Dim SwApp As SldWorks.SldWorks, SwModel As ModelDoc2

                           Set SwApp = Application.SldWorks

                           Set SwModel = SwApp.ActiveDoc

                      Dim SwSelMgr As SelectionMgr

                           Set SwSelMgr = SwModel.SelectionManager

                      Dim SwTabAnn As TableAnnotation, SwTextFormat As TextFormat, SwAnn As Annotation

                           Set SwTabAnn = SwSelMgr.GetSelectedObject5(1)

                           ''

                           With SwTabAnn

                               For ii = 0 To .RowCount - 1

                                  For jj = 0 To .ColumnCount - 1

                                      Set SwTextFormat = .GetCellTextFormat(ii, jj)

                                      If ii = .RowCount - 1 Then

                                             .Text(ii, jj) = BomRng(1, jj + 1)

                                             Set SwTextFormat = WidthFactorTextFormat(SwTextFormat, BomRng(1, jj + 1), WidthFactorRng(1, jj + 1), 0.05)

                                             .SetColumnWidth jj, ColWidthRng(1, jj + 1) / 1000, 0

                                             .SetRowHeight ii, RowHightRng(1, 1) / 1000, 0

                                      Else

                                             .SetRowHeight ii, RowHightRng(2, 1) / 1000, 0

                                             Set SwTextFormat = WidthFactorTextFormat(SwTextFormat, BomRng(2, jj + 1), WidthFactorRng(2, jj + 1), 0.05)

                                      End If

                                      .SetCellTextFormat ii, jj, False, SwTextFormat

                                  Next jj

                               Next ii

                               Set SwAnn = .GetAnnotation

                               .AnchorType = BomXyRng(1, 3)

                           End With

                           With SwAnn

                                .SetPosition BomXyRng(1, 1) / 1000, BomXyRng(1, 2) / 1000, 0

                                .Layer = "文本"

                               

                           End With

                     

                  End Sub

                   

                   

                  ''

                  Function WidthFactorTextFormat(TextFormat As TextFormat, Rng As Range, wFactor, LineSpec) As TextFormat

                    ''

                    With TextFormat

                           '.Italic = Rng.Font.Italic

                           .Bold = Rng.Font.Bold

                           .LineSpacing = LineSpec / 1000

                           .CharHeight = (Rng.Font.Size - 6) / 1000

                        

                           .TypeFaceName = Rng.Font.Name

                           .WidthFactor = wFactor

                    End With

                    Set WidthFactorTextFormat = TextFormat

                  End Function

                    • Re: How to select bomtable in Feature.
                      Yong Ning

                      ''

                      Function MainPlateBOM(SwBomTabAnn As BomTableAnnotation, SwTabAnn As TableAnnotation, cArr, Arr, wArr, Xx, Yy, oAnchorType)

                             Xx = Xx / 1000

                             Yy = Yy / 1000

                             ''

                             With SwTabAnn

                                For jj = 0 To UBound(cArr)  ' .ColumnCount - 1

                                   .SetColumnWidth jj, wArr(jj) / 1000, 0

                                   .SetColumnTitle jj, cArr(jj)

                                   SwBomTabAnn.SetColumnCustomProperty jj, Arr(jj)

                                Next jj

                                For ii = 0 To .RowCount

                                   If ii = .RowCount Then

                                      .SetRowHeight ii, 0.01, 0

                                   Else

                                      .SetRowHeight ii, 0.005, 0

                                   End If

                                Next ii

                                .AnchorType = oAnchorType

                                Set SwAnn = .GetAnnotation

                                SwAnn.SetPosition Xx, Yy, 0

                             End With

                      End Function

                      Private Sub ChangeBOM()

                          Dim Arr(1), cArr(1), wArr(1)

                            cArr(0) = Array("序号", "标 准 号", "名        称", "数量", "材  料", "质量" & Chr(13) & "(单)", "质量" & Chr(13) & "(合)", "备  注")

                            Arr(0) = Array("", "标准号", "名称", "", "材料", "质量", "", "备注")

                            wArr(0) = Array(10, 25, 50, 12, 25, 15, 18, 25)

                            ''

                            cArr(1) = Array("件号", "名    称", "模型质量", "下料尺寸", "下料质量", "数量", "材料", "δ", "下料质量-模型质量", "备  注")

                            Arr(1) = Array("件号", "名称", "质量", "下料尺寸", "下料质量", "", "材料", "δ", "", "")

                            wArr(1) = Array(10, 40, 15, 35, 15, 10, 20, 8, 40, 15)

                       

                       

                          Dim SwApp As SldWorks.SldWorks, SwModel As ModelDoc2

                              Set SwApp = Application.SldWorks

                              Set SwModel = SwApp.ActiveDoc

                          Dim SwSelMgr As SelectionMgr

                              Set SwSelMgr = SwModel.SelectionManager

                          Dim SwFeat As Feature, SwBomFeat As BomFeature

                       

                          Dim SwBomTabAnn As BomTableAnnotation, SwTabAnn As TableAnnotation

                              Set SwFeat = SwModel.FirstFeature

                              Do While Not SwFeat Is Nothing

                                 '''

                                 If UCase(SwFeat.Name) Like "*BOM*" And SwFeat.GetTypeName = "BomFeat" Then

                                    SwFeat.Select True

                                    Set SwBomFeat = SwSelMgr.GetSelectedObject5(1)

                                    Set SwBomTabAnn = SwBomFeat.GetTableAnnotations(0)

                                    Set SwTabAnn = SwBomTabAnn

                                    'Debug.Print SwFeat.Name, SwFeat.GetTypeName

                                    If UCase(SwFeat.Name) Like "*MAIN*" Then

                                       MainPlateBOM SwBomTabAnn, SwTabAnn, cArr(0), Arr(0), wArr(0), 420 - 5, 30, swBOMConfigurationAnchor_BottomRight

                                         ''

                                    ElseIf UCase(SwFeat.Name) Like "*PLATE*" Then

                                       MainPlateBOM SwBomTabAnn, SwTabAnn, cArr(1), Arr(1), wArr(1), 25, 5, swBOMConfigurationAnchor_BottomLeft

                                          With SwTabAnn '

                                             For ii = .RowCount To 0 Step -1

                                               If .Text(ii, 3) Like "*板材*" Then

                                                  Debug.Print .Text(ii, 6)

                                               Else

                                                 .DeleteRow (ii)

                                               End If

                                             Next ii

                                          End With

                       

                                    End If

                                 

                                 End If

                                 Set SwFeat = SwFeat.GetNextFeature

                                 SwModel.ClearSelection2 True

                              Loop

                      End Sub

                       

                       

                      Set SwBomFeat = SwFeat.GetSpecificFeature2  ' SwSelMgr.GetSelectedObject5(1)

                        • Re: How to select bomtable in Feature.
                          Yong Ning

                          Private Sub ll1()

                             Dim SwApp As SldWorks.SldWorks, SwModel As ModelDoc2

                                Set SwApp = Application.SldWorks

                                Set SwModel = SwApp.ActiveDoc

                             Dim SwSelMgr As SelectionMgr

                                Set SwSelMgr = SwModel.SelectionManager

                             Dim Visible

                             Dim SwBomFeat As BomFeature, SwBomTabAnn As BomTableAnnotation

                                Set SwBomTabAnn = SwSelMgr.GetSelectedObject5(1)

                                With SwBomTabAnn

                                    Ss = .GetComponents(1)

                                End With

                            Dim SwTabAnn As TableAnnotation

                                Set SwTabAnn = SwBomTabAnn

                            

                            Dim SwComp As Component2

                                With SwTabAnn

                                   For ii = 1 To .RowCount - 1

                                      Ss = SwTabAnn.GetComponents(ii)

                                      Set SwComp = Ss(0)

                                      Debug.Print SwComp.Name2, SwComp.GetModelDoc.GetTitle,

                                      Debug.Print SwComp.ReferencedConfiguration

                                   Next ii

                                End With

                                Stop

                          End S

                           

                           

                          **************************************

                           

                          Private Sub ll()

                             Dim SwApp As SldWorks.SldWorks, SwModel As ModelDoc2

                                 Set SwApp = Application.SldWorks

                                 Set SwModel = SwApp.ActiveDoc

                             Dim SwSelMgr As SelectionMgr

                                 Set SwSelMgr = SwModel.SelectionManager

                             Dim SwDraw As DrawingDoc

                                 Set SwDraw = SwModel

                             Dim vSheet, SwSheet As Sheet, SwView As View

                                 vSheet = SwDraw.GetSheetNames

                             Dim SwBomFeat As BomFeature, SwBomTabAnn As BomTableAnnotation

                             Dim sConfig

                                 ''

                                 For ii = 0 To UBound(vSheet)

                                    SwDraw.ActivateSheet (vSheet(ii))

                                    Set SwSheet = SwDraw.GetCurrentSheet

                                    Set SwView = SwDraw.GetFirstView

                                    Set SwView = SwView.GetNextView

                                    sConfig = SwView.ReferencedConfiguration

                                    tmp = SwModel.Extension.SelectByID2(vSheet(ii) & "BOM", "BOMFEATURE", 0, 0, 0, False, 0, Nothing, 0)

                                  

                                    If tmp Then

                                        Set SwBomFeat = SwSelMgr.GetSelectedObject5(1)

                                        BOMConfiguration SwBomFeat, sConfig

                                      

                                    End If

                                 Next ii

                          End Sub

                           

                           

                           

                           

                          Function BOMConfiguration(SwBomFeat As BomFeature, sConfig)

                              Dim Names, Visibles

                                        With SwBomFeat

                                           Names = .GetConfigurations(False, Visibles)

                                           '''

                                           For jj = 0 To UBound(Names)

                                               Visibles(jj) = False

                                               If sConfig = Names(jj) Then

                                                   Visibles(jj) = True

                                               End If

                                           Next jj

                                           .SetConfigurations False, Visibles, Names

                                        End With

                           

                           

                          End Function