"""Create a bend table on the active sheet using a flat-pattern view."""
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
bend_tables = sheet.bendTables
if not bend_tables or not sheet.views:
return
# Bend tables need a flat-pattern view.
flat_pattern_view = None
for view in sheet.views:
if view and int(view.type) == int(adsk.drawing.ViewTypes.FlatPatternViewType):
flat_pattern_view = view
break
if not flat_pattern_view:
return
table_input = bend_tables.createInput()
if not table_input:
return
table_input.view = flat_pattern_view
table_input.direction = adsk.drawing.TableDirectionTypes.UpTableDirectionType
table_input.placementPoint = adsk.drawing.DrawingPoint.create(adsk.core.Point2D.create(130.0, 130.0))
bend_table = bend_tables.add(table_input)
if not bend_table:
return
# Update one display setting after table creation.
bend_table.direction = adsk.drawing.TableDirectionTypes.DownTableDirectionType
except:
if app:
app.log(traceback.format_exc())
if ui:
ui.messageBox("Failed:\n{}".format(traceback.format_exc()))