Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to add a constraint so a spline handle?

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
a.g.ashwood
174 Views, 2 Replies

How to add a constraint so a spline handle?

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

2 REPLIES 2
Message 2 of 3
kandennti
in reply to: a.g.ashwood

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

Message 3 of 3
a.g.ashwood
in reply to: a.g.ashwood

That's exactly what I was after!

Thanks a lot.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report