部品表作成マクロに関して教えて下さい

アクティブな図面のモデルから、部品表を作成するマクロ作って使用してますが、部品コンフィグレーションのグループ化を選択するコードが分からず困ってます。テーブル位置(アンカーポイント)、部品表タイプ、部品表テンプレート等、InsertBomTable4で指定し実行後に部品表枠サイズや並び替え(ソート)も問題なく出来てますが、部品コンフィグレーションのグループ化だけどのように指定したらいいのか分からないのでデフォルトの「同一部品のコンフィグレーションを別アイテムとして表示」になってます。コード一部抜粋しますので、教えて頂けると助かります。よろしくお願いします。

'部品表作成
    tableTemplate = template
    config = ""
    
    On Error Resume Next
    
    Set swBOMTableAnnotation =swView.InsertBomTable4(True, 0, 0, anchorType, bomType, config, tableTemplate, False, swNumberingType_Detailed, False)
    Set swTableAnnotation = swBOMTableAnnotation
    
    If Err.Number > 0 Then
        MsgBox "部品表を作成出来ませんでした" & vbCrLf & "最初からやり直して下さい", vbInformation
        On Error GoTo 0
    GoTo Err1
    End If
    On Error GoTo 0
    
'部品表枠変更
    swTableAnnotation.BorderLineWeight = 0.18
    swTableAnnotation.GridLineWeight = 0.18
    
'部品表並び替え

    Dim swSortData As BomTableSortData
    Dim sortSaved As Boolean
    
    Set swSortData = swBOMTableAnnotation.GetBomTableSortData
    
    swSortData.ColumnIndex(0) = 2   'ソート最優先キー3列目
    swSortData.Ascending(0) = True  '1列目ソート昇順
    swSortData.ColumnIndex(1) = 3 'ソート設定2キー4列目
    swSortData.Ascending(1) = True  '2列目ソート昇順
    swSortData.ColumnIndex(2) = -1 'ソート設定3キー無し
    'swSortData.Ascending(2) = True  '2列目ソート昇順
    
     ' 部品番号変更する
    swSortData.DoNotChangeItemNumber = False

    ' 並べ替え順序保存
    sortSaved = swSortData.SaveCurrentSortParameters
    
    '並べ替え実行
    boolstatus = swBOMTableAnnotation.Sort(swSortData)

コメント

コメント表示順: リミット: オートロード:
  • 福谷勇時 1074 日前

    回答頂いた内容参考に下記コードで試してみたところ
    valueの値を変更することで、部品コンフィグレーションのグループ化(3種類)を選択出来ました。
    フォントサイズ、部品表タイプ選択画面Userformに追加して任意選択できるようにしたいと思います。

    Set swBOMTableAnnotation = swView.InsertBomTable4()
    Set swTableAnnotation = swBOMTableAnnotation

    ---------------追加

    Set swBOMFeature = swBOMTableAnnotation.BomFeature
        Set instance = swBOMFeature
        instance.PartConfigurationGrouping = value

    ---------------------------------------

    ちなみに山本さんに教えて頂いたコードも3種類選択できることを確認出来ました。

    Set swBOMTableAnnotation = swView.InsertBomTable4()
    Set swTableAnnotation = swBOMTableAnnotation

    ---------------追加

    '同一部品のコンフィグを別アイテムとして表示
    swBOMFeature.PartConfigurationGrouping = swPartConfigurationGroupingOption_e.swDisplay_AllConfigurationOfSamePart_AsOneItem

    '同一部品の全てのコンフィグを単一アイテムとして表示
    'swBOMFeature.PartConfigurationGrouping = swPartConfigurationGroupingOption_e.swDisplay_ConfigurationOfSamePart_AsSeparateItem

    '同じ名前をもつコンフィグを単一アイテムとして表示
    'swBOMFeature.PartConfigurationGrouping = swPartConfigurationGroupingOption_e.swDisplay_ConfigurationWithSameName_AsOneItem

    ---------------------------------------

    取得swBOMFeatureの内容
    image


    お手数掛けました。

  • 山本誠 1075 日前

    APIヘルプから適当に組み立ててみたので動かないかもですが、
    こんな感じでは?

    ' ***** Set swBOMTableAnnotation =swView.InsertBomTable4()以降に追加
    Set swBOMFeature = swBOMAnnotation.BomFeature
    swBOMFeature.PartConfigurationGrouping = swPartConfigurationGroupingOption_e.swDisplay_ConfigurationWithSameName_AsOneItem
    ' ***** ここまで

  • 辻 直樹 1075 日前

    "PartConfigurationGrouping"というのはAPIライブラリにあるものなので宣言する必要はないのでは?
    宣言とSetで定義が必要なのは”BomFeature"オブジェクトですよね。(やり方は下記の例文を参照)

    https://help.solidworks.com/2019/english/api/sldworksapi/Insert_and_Show_BOM_Table_and_BOM_Balloon_Example_VB.htm

    定義した”BomFeature"を、前レスで示したヘルプにあるセットプロパティの構文の"instance"部分に当てはめるようでしょう。

    instance.PartConfigurationGrouping = value

    >ちなみにこれも似たような感じでしょうか?・・・とあるのはここで”Value"として何を入れるかの定数の説明ですよね。

    まあ私もマクロ書いて走らせて確認してるわけではないので、どう動くかまではわかりませんが、、、

  • 福谷勇時 1075 日前

    私も多分これのことだと思って、自分のマクロに追加してみましたが、
    まず宣言文「Property PartConfigurationGrouping As System.Integer」でエラーが出てしまい
    原因が分からず諦めてしまいました。

    宣言できれば、使用法記述の「value」の値を1~3に変更で切り替わる?かと。。。。
    作成したマクロ載せるので、どのように宣言したらよいか教えて頂けると助かります。

    ちなみにこれも似たような感じでしょうか?

    https://help.solidworks.com/2019/english/api/swconst/SOLIDWORKS.Interop.swconst~SOLIDWORKS.Interop.swconst.swPartConfigurationGroupingOption_e.html

    -------------------------------------------------------

    Option Explicit

    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swDrawing As SldWorks.DrawingDoc
    Dim swView As SldWorks.View
    Dim swBOMTableAnnotation As SldWorks.BomTableAnnotation
    Dim swTableAnnotation As SldWorks.TableAnnotation

    Dim nNumRow As Long
    Dim sRowStr As String

    Dim i As Long
    Dim j As Long

    Dim tableTemplate As String
    Dim config As String
    Dim boolstatus As Boolean

    Public template As String
    Public anchorType As String
    Public bomType As Long
    Public flag As Integer
    ---------------------------------------------
    Sub main()

        Set swApp = Application.SldWorks
        Set swModel = swApp.ActiveDoc
        
    'ファイル確認エラー表示
        If swModel Is Nothing Then
        MsgBox "ファイルが開かれてません" & vbCrLf & "ファイルを開きマクロ実行して下さい", vbCritical
        GoTo Err1
        End If
            
        Dim Doctype As Long
        Doctype = swModel.GetType
        
        If Doctype <> 3 Then
        MsgBox "図面が選択されてません" & vbCrLf & "図面を選択しマクロ実行して下さい", vbCritical
        GoTo Err1
        End If
        
    '部品表作成Vew指定
        Set swDrawing = swModel
        Set swView = swDrawing.GetCurrentSheet.GetViews()(0)
        

    'テンプレート選択ユーザーフォーム
        UserForm1.Show

        If flag = 1 Then
        MsgBox "キャンセルしました", vbInformation
        flag = 0
        GoTo Err1
        End If

    '部品表作成
        tableTemplate = template
        config = ""
        
        On Error Resume Next
        
        Set swBOMTableAnnotation = swView.InsertBomTable4(True, 0, 0, anchorType, bomType, config, tableTemplate, False, swNumberingType_Detailed, False)
        Set swTableAnnotation = swBOMTableAnnotation
        
        If Err.Number > 0 Then
            MsgBox "部品表を作成出来ませんでした" & vbCrLf & "最初からやり直して下さい", vbInformation
            On Error GoTo 0
        GoTo Err1
        End If
        On Error GoTo 0
        
    '部品表枠変更
        swTableAnnotation.BorderLineWeight = 0.18
        swTableAnnotation.GridLineWeight = 0.18
        
    '部品表並び替え

        Dim swSortData As BomTableSortData
        Dim sortSaved As Boolean
        
        Set swSortData = swBOMTableAnnotation.GetBomTableSortData
        
        swSortData.ColumnIndex(0) = 2   'ソート最優先キー3列目
        swSortData.Ascending(0) = True  '1列目ソート昇順
        swSortData.ColumnIndex(1) = 3 'ソート設定2キー4列目
        swSortData.Ascending(1) = True  '2列目ソート昇順
        swSortData.ColumnIndex(2) = -1 'ソート設定3キー無し
        'swSortData.Ascending(2) = True  '2列目ソート昇順
        
         ' 部品番号変更する
        swSortData.DoNotChangeItemNumber = False

        ' 並べ替え順序保存
        sortSaved = swSortData.SaveCurrentSortParameters
        
        '並べ替え実行
        boolstatus = swBOMTableAnnotation.Sort(swSortData)
        
        
    '部品表行数取得
        nNumRow = swTableAnnotation.RowCount


    '指定列の値取得

           j = 8
           
        For i = 1 To nNumRow - 1
            sRowStr = ""

                sRowStr = sRowStr & swTableAnnotation.Text2(i, j, True) & ","

            Debug.Print Left(sRowStr, Len(sRowStr) - 1)
            
           swTableAnnotation.Text2(i, 18, True) = swTableAnnotation.Text2(i, j, True)
            
        Next i


    Err1:

        Set swApp = Nothing
        Set swModel = Nothing
        Set swDrawing = Nothing

    End Sub
    ------------------------------------------

    UserForm1は、使用するテンプレートのフォント大小と部品表タイプを選択します。

    ------------------------------------------
    Private Sub TextBox1_Change()

    End Sub

    Private Sub UserForm_Initialize()
        
    OptionButton1.Caption = "フォント小"
    OptionButton2.Caption = "フォント大"
    OptionButton1.value = True

    'OptionButton3.Caption = "ポイント右上"
    'OptionButton4.Caption = "ポイント右下"
    'OptionButton5.Caption = "ポイントなし"
    'OptionButton3.Value = True

    OptionButton6.Caption = "トップレベルのみ"
    OptionButton7.Caption = "部品のみ"
    OptionButton8.Caption = "インデント"
    OptionButton6.value = True

    End Sub

    Private Sub CommandButton1_Click()

    If OptionButton1.value = True Then
    template = "\\共有\部品表テンプレート文字小.sldbomtbt"
    ElseIf OptionButton2.value = True Then
    template = "\\共有\部品表テンプレート文字大.sldbomtbt"
    End If

    'If OptionButton3.Value = True Then
    anchorType = 2
    'ElseIf OptionButton4.Value = True Then
    'anchorType = 4
    'ElseIf OptionButton5.Value = True Then
    'anchorType = ""
    'End If

    If OptionButton6.value = True Then
    bomType = 2
    ElseIf OptionButton7.value = True Then
    bomType = 1
    ElseIf OptionButton8.value = True Then
    bomType = 3
    End If

    Unload Me

    End Sub

    Private Sub CommandButton2_Click()

    flag = 1

    Unload Me

    End Sub

    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)

    'Xボタンが押されたとき

      If CloseMode = 0 Then
        flag = 1
        Unload Me
      End If

    End Sub