Drawing View Curves Sample

Description

Demonstrates how to read drawing view curves by type and extract representative geometry values.

Code Samples

"""Read key geometry values from drawing view curves."""

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 or not drawing.activeSheet.views:
            return

        # Find the first view that contains curves.
        target_view = None
        for candidate in drawing.activeSheet.views:
            if candidate and candidate.viewCurves and candidate.viewCurves.count > 0:
                target_view = candidate
                break
        if not target_view:
            return

        # Loop curves and collect representative geometry values.
        curve_summaries = []
        for curve in target_view.viewCurves:
            if not curve:
                continue

            curve_type = int(curve.type)
            curve_summary = {"type": curve_type, "isSameSelf": curve.isSame(curve)}
            if curve_type == int(adsk.drawing.ViewCurveTypes.LineViewCurveType):
                line = adsk.drawing.LineViewCurve.cast(curve)
                if line:
                    curve_summary.update(
                        {
                            "length": line.length,
                            "start": line.startPoint.coordinate if line.startPoint else None,
                            "end": line.endPoint.coordinate if line.endPoint else None,
                            "mid": line.midPoint.coordinate if line.midPoint else None,
                        }
                    )

            elif curve_type == int(adsk.drawing.ViewCurveTypes.ArcViewCurveType):
                arc = adsk.drawing.ArcViewCurve.cast(curve)
                if arc:
                    curve_summary.update(
                        {
                            "radius": arc.radius,
                            "center": arc.centerPoint.coordinate if arc.centerPoint else None,
                            "start": arc.startPoint.coordinate if arc.startPoint else None,
                            "end": arc.endPoint.coordinate if arc.endPoint else None,
                        }
                    )

            elif curve_type == int(adsk.drawing.ViewCurveTypes.CircleViewCurveType):
                circle = adsk.drawing.CircleViewCurve.cast(curve)
                if circle:
                    curve_summary.update(
                        {
                            "radius": circle.radius,
                            "center": circle.centerPoint.coordinate if circle.centerPoint else None,
                        }
                    )

            elif curve_type == int(adsk.drawing.ViewCurveTypes.SplineViewCurveType):
                spline = adsk.drawing.SplineViewCurve.cast(curve)
                if spline:
                    curve_summary.update(
                        {
                            "start": spline.startPoint.coordinate if spline.startPoint else None,
                            "end": spline.endPoint.coordinate if spline.endPoint else None,
                            "center": spline.centerPoint.coordinate if spline.centerPoint else None,
                        }
                    )

            elif curve_type == int(adsk.drawing.ViewCurveTypes.PolylineViewCurveType):
                polyline = adsk.drawing.PolylineViewCurve.cast(curve)
                if polyline:
                    curve_summary.update(
                        {
                            "start": polyline.startPoint.coordinate if polyline.startPoint else None,
                            "end": polyline.endPoint.coordinate if polyline.endPoint else None,
                        }
                    )

            curve_summaries.append(curve_summary)

        if not curve_summaries:
            return

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