Moving assembly components with an API script

Moving assembly components with an API script

greenmica
Explorer Explorer
320 Views
1 Reply
Message 1 of 2

Moving assembly components with an API script

greenmica
Explorer
Explorer

Hello, 

I am creating a biomimetic tail in Fusion360, and I want to model the motion of a cable passing through the ends of the tail segments. 

 

I have printed the tail out using TPU have constrained it to create a helical shape. I want to model this helical shape using Fusion's API, and I have written an AI-assisted code to demonstrate that the math works. 

 

Primary Issue: 

When the script is run, there is a new sketch created that models the mathematical motion I wanted, however none of the segments in the assembly move with the points. I have suppressed all constraints and joints before running the script, but I have not placed all of the segments at one origin. 

 

Any suggestions on how to get the pieces to move according to the API script?

0 Likes
321 Views
1 Reply
Reply (1)
Message 2 of 2

kandennti
Mentor
Mentor

Hi @greenmica -san.

If possible, I would have liked to see a sample.

I think calling `fusion.Design.computeAll` will solve the problem.

# Fusion360API Python script

import traceback
import adsk
import adsk.core as core
import adsk.fusion as fusion

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

        # Create a new document with parametric (hybrid) design mode
        app.documents.add(core.DocumentTypes.FusionDesignDocumentType)
        des: fusion.Design = app.activeProduct
        des.designType = fusion.DesignTypes.ParametricDesignType
        root: fusion.Component = des.rootComponent

        # Create a sketch on the XY plane
        skt: fusion.Sketch = root.sketches.add(root.xYConstructionPlane)

        # Add 5 control points and create a fitted spline
        pts: core.ObjectCollection = core.ObjectCollection.create()
        pts.add(core.Point3D.create(-5, 0, 0))
        pts.add(core.Point3D.create(2, 3, 0))
        pts.add(core.Point3D.create(4, 1, 0))
        pts.add(core.Point3D.create(6, 4, 0))
        pts.add(core.Point3D.create(8, 0, 0))
        spline: fusion.SketchFittedSpline = skt.sketchCurves.sketchFittedSplines.add(pts)

        # Create cylinder components jointed to control points 2, 3, 4 (index 1, 2, 3)
        add_cylinder_component(root, spline, 1)
        add_cylinder_component(root, spline, 2)
        add_cylinder_component(root, spline, 3)

        # Refresh the viewport and notify that components were created
        adsk.doEvents()
        app.activeViewport.refresh()
        ui.messageBox("3 cylinder components created and jointed to control points 2, 3, 4.")

        # Move spline control points 2, 3, 4
        offsets: list[tuple[float, float, float]] = [
            (0, 3, 0),
            (0, -3, 0),
            (0, 3, 0),
        ]
        for i, (dx, dy, dz) in enumerate(offsets):
            spline.fitPoints.item(i + 1).move(core.Vector3D.create(dx, dy, dz))

        # Refresh the viewport and notify that control points were moved
        adsk.doEvents()
        app.activeViewport.refresh()
        ui.messageBox("Spline control points 2, 3, 4 moved.")

        # Recompute all features to update joint positions after moving spline control points
        des.computeAll()
        ui.messageBox("Computing all features to update component positions...")


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


def add_cylinder_component(
    root: fusion.Component,
    spline: fusion.SketchFittedSpline,
    fit_pt_idx: int,
) -> fusion.Joint:
    """Create a cylinder component and rigidly joint it to a spline fit point."""
    occ: fusion.Occurrence = root.occurrences.addNewComponent(core.Matrix3D.create())
    occ.isGroundToParent = False
    comp: fusion.Component = occ.component

    cyl_skt: fusion.Sketch = comp.sketches.add(comp.xYConstructionPlane)
    cyl_skt.sketchCurves.sketchCircles.addByCenterRadius(
        core.Point3D.create(0, 0, 0), 1.0
    )
    prof: fusion.Profile = cyl_skt.profiles.item(0)
    ext_input: fusion.ExtrudeFeatureInput = comp.features.extrudeFeatures.createInput(
        prof, fusion.FeatureOperations.NewBodyFeatureOperation
    )
    ext_input.setDistanceExtent(False, core.ValueInput.createByReal(2.0))
    comp.features.extrudeFeatures.add(ext_input)

    fit_pt: fusion.SketchPoint = spline.fitPoints.item(fit_pt_idx)
    geo1: fusion.JointGeometry = fusion.JointGeometry.createByPoint(comp.originConstructionPoint)
    geo2: fusion.JointGeometry = fusion.JointGeometry.createByPoint(fit_pt)
    joint_input: fusion.JointInput = root.joints.createInput(geo1, geo2)
    joint_input.setAsRigidJointMotion()
    return root.joints.add(joint_input)