Announcements
Autodesk Community will be read-only between April 26 and April 27 as we complete essential maintenance. We will remove this banner once completed. Thanks for your understanding

How to add a constraint so a spline handle?

a.g.ashwood
Contributor

How to add a constraint so a spline handle?

a.g.ashwood
Contributor
Contributor

Hi All,

I am still learning the API so please be kind 🙂

 

I am trying to write a script which generates pathing information and I have come across a problem joining two lines in a consistent way. If we take two lines and put a spline between them then I get something like this:

 

1.jpg

Now, in the GUI I would grab the handles of these two points and make them colinear with the incoming and outgoing lines like this:

2.jpg

 

 

Which results in a lovely curving path:

3.jpg

However, when I try and do this via the API using this code:

def makeSpline(sketch : adsk.fusion.Sketch,startPoint : adsk.fusion.SketchPoint, endPoint : adsk.fusion.SketchPoint, startLine : adsk.fusion.SketchLine, endLine : adsk.fusion.SketchLine) :
    points = adsk.core.ObjectCollection.create()
    points.add(startPoint)
    points.add(endPoint)

    splines = sketch.sketchCurves.sketchFittedSplines
    spline = splines.add(points)
    handle = spline.activateCurvatureHandle(startPoint)
    sketch.geometricConstraints.addCollinear(startLine, handle)
    handle = spline.activateCurvatureHandle(endPoint)
    sketch.geometricConstraints.addCollinear(endLine, handle)
    return spline

 

I get an error:

agashwood_0-1687529175333.png

What am I doing wrong here?

 

TIA

Andy

0 Likes
Reply
Accepted solutions (1)
452 Views
2 Replies
Replies (2)

kandennti
Mentor
Mentor
Accepted solution

Hi @a.g.ashwood -San.

 

I had never tried it before, so I gave it a shot.
I have rewritten the makeSpline function significantly, but this does not cause any errors.
Is this the desired result?

# Fusion360API Python script

import traceback
import adsk.core
import adsk.fusion

def run(context):
    ui: adsk.core.UserInterface = None
    try:
        app: adsk.core.Application = adsk.core.Application.get()
        ui = app.userInterface
        des: adsk.fusion.Design = app.activeProduct
        root: adsk.fusion.Component = des.rootComponent

        skt: adsk.fusion.Sketch = root.sketches.add(
            root.xYConstructionPlane
        )

        startLine: adsk.fusion.SketchLine = create_sketch_line(
            skt,
            [0,1,0],
            [1,1,0],
        )

        endLine: adsk.fusion.SketchLine = create_sketch_line(
            skt,
            [3,3,0],
            [4,3,0],
        )

        makeSpline(
            skt,
            startLine.endSketchPoint,
            endLine.startSketchPoint,
            startLine,
            endLine,
        )

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


def create_sketch_line(
    skt: adsk.fusion.Sketch,
    sPos: list[float],
    ePos: list[float],
) -> adsk.fusion.SketchLine:

    line: adsk.fusion.SketchLine = skt.sketchCurves.sketchLines.addByTwoPoints(
        adsk.core.Point3D.create(sPos[0], sPos[1], sPos[2],),
        adsk.core.Point3D.create(ePos[0], ePos[1], ePos[2],),
    )
    line.isFixed = True

    return line


def makeSpline(
    sketch: adsk.fusion.Sketch,
    startPoint: adsk.fusion.SketchPoint,
    endPoint: adsk.fusion.SketchPoint,
    startLine: adsk.fusion.SketchLine,
    endLine: adsk.fusion.SketchLine
) -> adsk.fusion.SketchFittedSpline:

    points: adsk.core.ObjectCollection = adsk.core.ObjectCollection.createWithArray(
        [startPoint, endPoint]
    )

    # create spline
    splines: adsk.fusion.SketchFittedSplines = sketch.sketchCurves.sketchFittedSplines
    spline: adsk.fusion.SketchFittedSpline = splines.add(points)
    fitPoints = list(spline.fitPoints)

    # create Collinear startline - spline start handle
    sFitPoint: adsk.fusion.SketchPoint = fitPoints[0]
    spline.activateCurvatureHandle(sFitPoint)

    curvatureHandle: adsk.fusion.SketchArc = spline.getCurvatureHandle(sFitPoint)
    curvatureHandle.deleteMe()

    tangentHandle: adsk.fusion.SketchLine = spline.getTangentHandle(sFitPoint)
    sketch.geometricConstraints.addCollinear(tangentHandle, startLine)

    # create Collinear endline - spline end handle
    eFitPoint: adsk.fusion.SketchPoint = fitPoints[-1]
    spline.activateCurvatureHandle(eFitPoint)

    curvatureHandle: adsk.fusion.SketchArc = spline.getCurvatureHandle(eFitPoint)
    curvatureHandle.deleteMe()

    tangentHandle: adsk.fusion.SketchLine = spline.getTangentHandle(eFitPoint)
    sketch.geometricConstraints.addCollinear(tangentHandle, endLine)

    return spline

1.png

0 Likes

a.g.ashwood
Contributor
Contributor

That's exactly what I was after!

Thanks a lot.

0 Likes