T-Spline Body API Sample

Description

Demonstrates creating a box T-Spline body in a Form feature, transforming a face, reading the face's smooth-surface area, creasing edges, finishing the form edit, and getting the resulting B-Rep body.

Code Samples

import adsk.core, adsk.fusion, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface

        # Create a new Fusion design document.
        doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
        design = adsk.fusion.Design.cast(app.activeProduct)

        # Form features (and the T-Spline bodies they contain) are timeline features,
        # so the design must be parametric.
        design.designType = adsk.fusion.DesignTypes.ParametricDesignType

        rootComp = design.rootComponent

        # Create an empty Form feature and start editing it. T-Spline bodies are
        # created and edited within a Form feature's edit session.
        formFeature = rootComp.features.formFeatures.add()
        formFeature.startEdit()

        tsplineBodies = formFeature.tSplineBodies

        # Define a box-shaped T-Spline body. The origin is the box corner; xDirection and
        # yDirection define the base plane (the height is along their cross product). The
        # last three arguments are the size in X, Y and Z (in centimeters).
        boxInput = tsplineBodies.createBoxInput(
            adsk.core.Point3D.create(0, 0, 0),
            adsk.core.Vector3D.create(1, 0, 0),
            adsk.core.Vector3D.create(0, 1, 0),
            4.0, 3.0, 2.0)

        # Optional settings on the input (number of faces along each direction).
        boxInput.xFaceCount = 3
        boxInput.yFaceCount = 3
        boxInput.zFaceCount = 3

        # Create the T-Spline body from the input.
        tsplineBody = tsplineBodies.addByShapeInput(boxInput)

        # Transform (translate) the first control-frame face 1 cm along Z.
        firstFace = tsplineBody.faces.item(0)
        moveUp = adsk.core.Matrix3D.create()
        moveUp.translation = adsk.core.Vector3D.create(0, 0, 1.0)
        firstFace.transform(moveUp)

        # Read some values from the face's smooth surface. A T-Spline face is represented by one
        # or more NURBS patches; here we inspect the first patch's NURBS structure.
        patches = firstFace.patches
        firstPatch = patches[0]
        patchInfo = 'first of {} patches: degree {}x{}, {}x{} control points'.format(
            len(patches),
            firstPatch.degreeU, firstPatch.degreeV,
            firstPatch.controlPointCountU, firstPatch.controlPointCountV)

        # Crease up to four edges of the body.
        edges = tsplineBody.edges
        creaseCount = min(4, edges.count)
        for i in range(creaseCount):
            edges.item(i).isCreased = True

        # Finish the Form feature edit. This converts the (now edited) T-Spline body into a
        # B-Rep body owned by the Form feature.
        formFeature.finishEdit()

        # Get the resulting B-Rep body from the T-Spline body.
        brepBody = tsplineBody.bRepBody

        ui.messageBox(
            'Created T-Spline body "{}" with {} faces, {} edges and {} vertices.\n'
            'Moved face smooth surface: {}.\n'
            'Creased {} edges.\n'
            'Resulting B-Rep body has {} faces.'.format(
                tsplineBody.name,
                tsplineBody.faces.count, tsplineBody.edges.count, tsplineBody.vertices.count,
                patchInfo, creaseCount,
                brepBody.faces.count if brepBody else 0))

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