Example of how to use sketchCurves.sketchFixedSplines.addByNurbsCurve

Example of how to use sketchCurves.sketchFixedSplines.addByNurbsCurve

OceanHydroAU
Collaborator Collaborator
1,305 Views
5 Replies
Message 1 of 6

Example of how to use sketchCurves.sketchFixedSplines.addByNurbsCurve

OceanHydroAU
Collaborator
Collaborator

Hopefully this will save folks in future from som hair-pulling!!!  Here's some code that reproduces how the UI inserts control-point-splines.

 

Autodesk: You have my permission to reproduce this code as a working example in the relevant help documentation here (hint hint!):-

 

http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-EA5FBD51-ED6F-4D04-83B2-D72A384BC6DA

 

import adsk.core, adsk.fusion, adsk.cam, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        design = app.activeProduct
        rootComp = design.rootComponent
        sketches = rootComp.sketches
        sketch = sketches.add(rootComp.xYConstructionPlane)
        sketchCurves = sketch.sketchCurves

        # Any arbitrary number of 4 or more points (cannot have degree-3 splines with less than 4 points)
        points= [   [  0,  0,  0 ],
                    [ -4,  3,  0 ],
                    [ -9,  3,  0 ],
                    [ -8, -2,  0 ],
                    [ -5,  2,  0 ],
                    [  0,  0,  0 ] ]

        # Create knots to weight all the points equally (matching how the UI control-point-spline feature works)
        knots=[0,0,0,0]
        for i in range(1,len(points)-3): knots.append(i)
        knots=[*knots,knots[-1]+1,knots[-1]+1,knots[-1]+1,knots[-1]+1]

        # Create the arrays for the nurbs
        controlPoints=[]
        for p in points:
            point = adsk.core.Point3D.create(*p)
            controlPoints.append(point)
            sketch.sketchPoints.add( point )    # optional - show our control points in the sketch

        # Create and insert the curve
        degree=3
        nurbsCurve = adsk.core.NurbsCurve3D.createNonRational(controlPoints, degree, knots, False)
        sketchCurves.sketchFixedSplines.addByNurbsCurve( nurbsCurve )

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

 

Which produces:-

 

Screen Shot 2020-11-11 at 10.07.23 am.png

p.s. If you're trying to create airfoils - my add-in is worth considering: 

1,306 Views
5 Replies
Replies (5)
Message 2 of 6

OceanHydroAU
Collaborator
Collaborator

this

 

knots = [ *knots, knots[-1]+1, knots[-1]+1, knots[-1]+1, knots[-1]+1 ]

 

might need to be this

 

knots = [ *knots[0:], knots[-1]+1, knots[-1]+1, knots[-1]+1, knots[-1]+1 ]

 

sometimes (I had intermittent failures on the first one)

Message 3 of 6

MichaelT_123
Advisor
Advisor

Hi Mr OceanHydroAU,

 

The code:

    knots = [ *knots[0:], knots[-1]+1, knots[-1]+1, knots[-1]+1, knots[-1]+1 ]

can be confusing... masochistic for the writer and sadistic for a reader  😵... but it could be as simple as building a flail with the benefit of avoiding costly mistakes. For example:

     j = i + 1

    knots.extend( [i, j, j, j, j ] )

There is some flexibility on how knot vectors are constructed, but there are some rules also. On top of this, there are some strategies of optimizing it for particular cases. The knowledge on the subject (going centuries back) is quite wide and deep and easily available.  The whole procedure you have presented as simple as does not guarantee a trouble-free outcome for general cases.

 

Regards

MichaelT

P.S.

I hope that you will not find this post as too harsh, as I made it in good faith.

 

MichaelT
Message 4 of 6

OceanHydroAU
Collaborator
Collaborator

This is for python coders - by definition, they're all sadistic, if not masochistic themselves.  Camels beat Pythons any day.

 

As colourful (in both ways) as your comment was, it is worse than mine.  Either post a working example, or bite your tongue my man.

 

Unlike the rest of the internet - these awesome autodesk forums have almost no trolls - just accomplished engineers and learners, all helping each other as best they can.  Might I suggest you head over to Quora - you'll find much more familiar company there.

Message 5 of 6

nnikbin
Collaborator
Collaborator

Thank you @OceanHydroAU for sharing this sample.

 

What changes should be made to the code if we need a periodic Control Point Spline (in addition to calling createNonRational method with isPeriodic parameter set to True instead of false)?

 

Is it possible to create periodic Control Point Splines in UI?

0 Likes
Message 6 of 6

2022282210229
Participant
Participant

hello,your curve cannot modify through the control points,do you know how to solve it?

0 Likes