User parameter with sketch.offset

User parameter with sketch.offset

Anonymous
Not applicable
994 Views
5 Replies
Message 1 of 6

User parameter with sketch.offset

Anonymous
Not applicable

Hello,

Just started to use the Fusion 360 python API today, and I've been progressing slowly, but I'm now completely stumped. I've been unable to find out how to create an offset in a sketch for a spline that I've created which is based on a user parameter. I've tried the following things:

 

# ...code to create points of curve...
curves = sketch.findConnectedCurves(spline)

# Create the offset
dirPoint = adsk.core.Point3D.create(0, 0.5, 0)

paramRingThickness = userParams.itemByName('ring_thickness')

# All of the below 'ringThickness' attempts failed
# ringThickness = adsk.core.ValueInput.createByString(paramRingThickness.name)
# ringThickness = adsk.core.ValueInput.createByReal(4)
# ringThickness = adsk.core.ValueInput.createByString("40 mm")
ringThickness = adsk.core.ValueInput.createByString("ring_thickness")
offsetCurves = sketch.offset(curves, dirPoint, ringThickness)

 

 

Which all result in the same error about `TypeError: Wrong number or type of arguments for overloaded function 'Sketch_offset'`

 

Thank you for any assistance!

0 Likes
Accepted solutions (1)
995 Views
5 Replies
Replies (5)
Message 2 of 6

prainsberry
Autodesk
Autodesk
# ...code to create points of curve...
curves = sketch.findConnectedCurves(spline)

# Create the offset
dirPoint = adsk.core.Point3D.create(0, 0.5, 0)

paramRingThickness = userParams.itemByName('ring_thickness')

# All of the below 'ringThickness' attempts failed
# ringThickness = adsk.core.ValueInput.createByString(paramRingThickness.name)
# ringThickness = adsk.core.ValueInput.createByReal(4)
# ringThickness = adsk.core.ValueInput.createByString("40 mm")
ringThickness = adsk.core.ValueInput.createByString("ring_thickness")
offsetCurves = sketch.offset(curves, dirPoint, paramRingThickness.value)

 

I think this should work.  paramRingThickness.value will give you the actual value of the parameter.  

 



Patrick Rainsberry
Developer Advocate, Fusion 360
0 Likes
Message 3 of 6

Anonymous
Not applicable

Thanks for replying!
I had indeed found that, but I really need this to be parametric, so when the 'ring_thickness' variable changes in Fusion 360 at any point, so should the offset.

0 Likes
Message 4 of 6

prainsberry
Autodesk
Autodesk
Accepted solution

OK, yah I was wondering about that.  So it is a bit tricky because with the offset you have to set the "expression" of the resulting dimension after the fact. 

 

Here is a full script that does what I believe you are looking for:

#Author-
#Description-

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

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui: adsk.core.UserInterface  = app.userInterface
        
        doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
        design: adsk.fusion.Design = app.activeProduct

        # Get the root component of the active design.
        rootComp = design.rootComponent

        # Create a new sketch on the xy plane.
        sketch = rootComp.sketches.add(rootComp.xYConstructionPlane)

        # Create an object collection for the points.
        points = adsk.core.ObjectCollection.create()

        # Define the points the spline with fit through.
        points.add(adsk.core.Point3D.create(0, 0, 0))
        points.add(adsk.core.Point3D.create(5, 1, 0))
        points.add(adsk.core.Point3D.create(6, 4, 3))
        points.add(adsk.core.Point3D.create(7, 6, 6))
        points.add(adsk.core.Point3D.create(2, 3, 0))
        points.add(adsk.core.Point3D.create(0, 1, 0))

        # Create the spline.
        spline = sketch.sketchCurves.sketchFittedSplines.add(points)

        # ...code to create points of curve...
        curves = sketch.findConnectedCurves(spline)

        # Create the offset
        dirPoint = adsk.core.Point3D.create(0, 9, 0)
        
        ringThickness = adsk.core.ValueInput.createByReal(4)
        ring_name = 'ring_thickness'

        design.userParameters.add(ring_name, ringThickness, 'in', 'My Comment' )
        paramRingThickness = design.userParameters.itemByName(ring_name)
        
        offsetCurves = sketch.offset(curves, dirPoint, paramRingThickness.value)

        curve: adsk.fusion.SketchCurve = offsetCurves.item(0)
        constraint: adsk.fusion.OffsetConstraint = curve.geometricConstraints.item(0)

        constraint.dimension.parameter.expression = ring_name

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

 



Patrick Rainsberry
Developer Advocate, Fusion 360
Message 5 of 6

Anonymous
Not applicable

Apologies for my very late reply! Thank you very much for the full example! If I read it correctly, you create the user parameter through the python script as well, and then afterwards, once the script is run you can adjust the "ring_thickness" parameter within Fusion 360 and the offset will update accordingly?

I actually had redo my design and not use an offset, because I kept on getting errors that the offset wasn't possible on some shapes, so I ended up calculating the offset myself in python as well and adding that as a second spline. I did have to completely give up on the idea of parametric though, because the distance between the original spline and the "offset" spline is now of course hard coded as a variable within python. I hope this won't come back to haunt me later...

0 Likes
Message 6 of 6

prainsberry
Autodesk
Autodesk

No problem,

Yah offsets can be tricky.  Especially if the offset curve would cause itself to have self intersections.  

 

Another thing you could do using similar commands as above would be to write a script that just programmatically adjusts the offset values of your "new" curve.  So if the variable is in your script, then you calculate the new points, you can get the .fitPoints collection on your spline object and move the points to the new locations.  

This would still give you parametric update behavior.  Since the actual spline is the "same spline" other downstream features should maintain their references and update accordingly.  
See here: https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-67c1819f-97fe-40b5-a3e4-0a26a4a5bb95

 



Patrick Rainsberry
Developer Advocate, Fusion 360
0 Likes