Create Hole Table Sample

Description

Creates a hole table on the active sheet and updates table title and direction.

Code Samples

"""Create a hole table on the active sheet and update its title."""

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

        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

        sheet = drawing.activeSheet
        hole_tables = sheet.holeTables
        if not hole_tables or not sheet.views:
            return

        # Find a view curve point to use as origin.
        target_view = None
        origin_point = None
        for view in sheet.views:
            if not view or not view.viewCurves or view.viewCurves.count == 0:
                continue
            curve = next(iter(view.viewCurves), None)
            if not curve:
                continue
            curve_type = int(curve.type)
            if curve_type == int(adsk.drawing.ViewCurveTypes.LineViewCurveType):
                line_curve = adsk.drawing.LineViewCurve.cast(curve)
                origin_point = line_curve.startPoint if line_curve else None
            elif curve_type == int(adsk.drawing.ViewCurveTypes.ArcViewCurveType):
                arc_curve = adsk.drawing.ArcViewCurve.cast(curve)
                origin_point = arc_curve.centerPoint if arc_curve else None
            elif curve_type == int(adsk.drawing.ViewCurveTypes.CircleViewCurveType):
                circle_curve = adsk.drawing.CircleViewCurve.cast(curve)
                origin_point = circle_curve.centerPoint if circle_curve else None
            else:
                origin_point = getattr(curve, "startPoint", None) or getattr(curve, "endPoint", None)
            if origin_point:
                target_view = view
                break
        if not target_view or not origin_point:
            return

        table_input = hole_tables.createInput()
        if not table_input:
            return

        table_input.view = target_view
        table_input.originPoint = origin_point
        table_input.title = "Hole Table API Sample"
        table_input.direction = adsk.drawing.TableDirectionTypes.UpTableDirectionType
        table_input.placementPoint = adsk.drawing.DrawingPoint.create(adsk.core.Point2D.create(150.0, 120.0))

        hole_table = hole_tables.add(table_input)
        if not hole_table:
            return

        hole_table.title = "Hole Table API Sample Updated"
        hole_table.direction = adsk.drawing.TableDirectionTypes.DownTableDirectionType

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