C#ATIA

↑タイトル詐欺 主にFusion360API 偶にCATIA V5 VBA(絶賛ネタ切れ中)

CSVファイルからテーブルをマクロで作成

CATIA V5です。予めお伝えしておくと僕も知ってます。
Drawにこちらのコマンドが有る事を。

次のサンプルは、指定したCSVファイルを読み込み、アクティブなビューの
原点付近にテーブルを作ります。

'vba

Option Explicit


Sub CATMain()

    Dim dDoc As DrawingDocument
    Set dDoc = CATIA.ActiveDocument

    Dim view As DrawingView
    Set view = dDoc.sheets.ActiveSheet.views.ActiveView

    Dim Msg As String
    Msg = "読み取り専用で開くファイルを選択してください"
    
    Dim suffix As String
    suffix = "*.csv"

    Dim path As String
    path = CATIA.FileSelectionBox( _
        Msg, _
        suffix, _
        CatFileSelectionModeOpen _
    )
    If path = vbNullString Then Exit Sub

    Call create_csv_table( _
        view, _
        path _
    )

End Sub


Private Function create_csv_table( _
    ByVal view As DrawingView, _
    ByVal path As String, _
    Optional ByVal position As Variant) _
    As Boolean

    If IsMissing(position) Then
        position = Array(0, 0)
    End If

    create_csv_table = False

    Dim dataAry As Variant
    dataAry = read_file(path)

    If IsEmpty(dataAry) Then Exit Function
    If UBound(dataAry) < 1 Then Exit Function

    Dim rowCount As Long
    rowCount = get_row_count(dataAry)
    If rowCount < 0 Then Exit Function

    Dim columnCount As Long
    columnCount = get_column_count(dataAry)
    
    Dim table As DrawingTable
    Set table = create_table( _
        view, _
        position, _
        rowCount, _
        columnCount _
    )
    
    import_data table, dataAry

End Function


Private Sub import_data( _
    ByVal table As DrawingTable, _
    ByVal dataAry As Variant)

    Dim maxRow As Long
    maxRow = table.NumberOfRows

    Dim rowData As Variant
    Dim idx As Long
    Dim i As Long, j As Long
    For i = 1 To maxRow
        idx = i - 1

        table.SetRowSize i, 0
        rowData = Split(dataAry(idx), ",")

        If UBound(rowData) < 1 Then GoTo continue
        
        For j = 0 To UBound(rowData)
            If rowData(j) = vbNullString Then GoTo continue2
            table.SetCellString i, j + 1, Trim(Str(rowData(j)))
continue2:
        Next
continue:
    Next

    For i = 1 To table.NumberOfColumns
        table.SetColumnSize i, 0
    Next

End Sub

Private Function create_table( _
    ByVal view As DrawingView, _
    ByVal position As Variant, _
    ByVal rowCount As Long, _
    ByVal columnCount As Long) _
    As DrawingTable

    Set create_table = view.Tables.Add( _
        position(0), _
        position(1), _
        rowCount, _
        columnCount, _
        7.5, _
        15 _
    )

End Function


Private Function get_column_count( _
    ByVal ary As Variant) _
    As Long

    Dim maxCount As Long
    maxCount = UBound(Split(ary(0), ","))

    If UBound(ary) < 2 Then
        GoTo continue
    End If

    Dim count As Long
    Dim i As Long
    For i = 1 To UBound(ary)
        count = UBound(Split(ary(i), ","))

        If maxCount < count Then
            maxCount = count
        End If
    Next

continue:
    get_column_count = maxCount + 1

End Function


Private Function get_row_count( _
    ByVal ary As Variant) _
    As Long

    Dim i As Long
    For i = UBound(ary) To 0 Step -1
        If Len(ary(i)) > 0 Then
            get_row_count = i + 1
            Exit Function
        End If
    Next
    
    get_row_count = -1

End Function


Private Function read_file( _
    ByVal path As String) _
    As Variant

    On Error Resume Next
    With get_fso.GetFile(path).OpenAsTextStream
        read_file = Split(.ReadAll, vbNewLine)
        .Close
    End With
    On Error GoTo 0

End Function


Private Function get_fso() _
    As Object

    Set get_fso = CreateObject("Scripting.FileSystemObject")
    
End Function

"アクティブなビュー" なので、一つもビューが無い場合は、メインビューに。
シートの背景で実行すれば、背景にテーブルが作成されます。

左が元のCSVで右がマクロ後に作成されたテーブルです。

・行数はCSVファイルの行数と同じですが、後半から要素が出現するまでの
空白行は無視します。(途中の空白行は出力)
・列数は最大の列数


念の為ですが、標準コマンドでインポートしたものが右です。

見た目も変わらないです。

これ自体が役立つわけでは無いのですが、何かの結果をCSVで出力
させつつテーブルも作成 のような場面で・・・無いか。