Create Custom Table Sample

Description

Creates a custom table on the active sheet and updates cell text.

Code Samples

"""Create a simple custom table on the active drawing sheet."""

import adsk.core
import adsk.drawing
import traceback


def run(context):
    app = None
    ui = None

    try:
        app = adsk.core.Application.get()
        ui = app.userInterface if app else None

        # Resolve active drawing + sheet.
        drawing = adsk.drawing.Drawing.cast(app.activeProduct) if app else None
        if not drawing:
            drawing_doc = adsk.drawing.DrawingDocument.cast(app.activeDocument) if app else None
            drawing = drawing_doc.drawing if drawing_doc else None
        if not drawing or not drawing.activeSheet:
            return

        custom_tables = drawing.activeSheet.customTables
        if not custom_tables:
            return

        # Create table input with a fixed position and size.
        table_input = custom_tables.createInput()
        if not table_input:
            return

        table_input.rowCount = 2
        table_input.columnCount = 2
        table_input.direction = adsk.drawing.TableDirectionTypes.DownTableDirectionType
        table_input.placementPoint = adsk.drawing.DrawingPoint.create(adsk.core.Point2D.create(120.0, 120.0))

        table = custom_tables.add(table_input)
        if not table:
            return

        # Fill cells and read values back.
        table.setCellData(0, 0, "Item")
        table.setCellData(0, 1, "Qty")
        table.setCellData(1, 0, "Bracket")
        table.setCellData(1, 1, "4")

        # Update one display setting after creation.
        table.direction = adsk.drawing.TableDirectionTypes.UpTableDirectionType

    except:
        if app:
            app.log(traceback.format_exc())
        if ui:
            ui.messageBox("Failed:\n{}".format(traceback.format_exc()))