How to Use Parameters in F360 API script/add-in to Create a Model in the API then Adjust it Parametrically Afterwards?

How to Use Parameters in F360 API script/add-in to Create a Model in the API then Adjust it Parametrically Afterwards?

jakedbirk
Enthusiast Enthusiast
3,365 Views
5 Replies
Message 1 of 6

How to Use Parameters in F360 API script/add-in to Create a Model in the API then Adjust it Parametrically Afterwards?

jakedbirk
Enthusiast
Enthusiast

Hello, 

I'd like to have my program take in user input, create an F360 Parameter from that input, use that parameter to create a sketch (all of this is already done) then give the user the ability to change the parameters retroactively and have the model update to the new values as they are modified. 

 

The simplest example of this in action would be a script that asks for the radius of a circle then creates a sketch where that parameter and updates drive the radius as the parameter is changed. The ability to use parameters when making sketches is a fundamental feature of F360 when using the typical GUI, so it would make sense to me that the API would allow it. I've written an example script like this, but the radius of the circle does not change when the user retroactively changes the radius parameter (like it would if a user parameter was created and then used to define the radius of a circle in the GUI). 

 

#Author - Jake Birkmaier 
#Description- Example for forum of an API created Parametric sketch
#Goal - To have the radius change after the script is ran when the user changes the 'radius' param

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

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        design = adsk.fusion.Design.cast(app.activeProduct)

        #Create an Input Circle Variable and Pull the input 
        inputCircleRadius = ui.inputBox('Enter Radius','Radius Input Circle','5 mm')
        inputCircleRadiusValue = inputCircleRadius[0]
        
        #Creating variables for the parameter
        paramRadiusName = 'Radius'
        paramRadiusUnit = 'mm'
        paramValueInput = adsk.core.ValueInput.createByReal(5)
        #Creating the actual parameter
        paramRadius = design.userParameters.add(paramRadiusName,paramValueInput,paramRadiusUnit,'')

        #Grabbing the Root Component 
        compRoot = design.rootComponent

        #Create a new sketch on the Xy Plane 
        sketches = compRoot.sketches
        xy_plane = compRoot.xYConstructionPlane
        sketch = sketches.add(xy_plane)
    
        #Create the circle trying to use the parameter. 
        sketch.sketchCurves.sketchCircles.addByCenterRadius(adsk.core.Point3D.create(0,0,0),paramRadius.value)

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

 

Use case: have been working on an Add-In for the past few years that takes user inputs and creates various stair flights (see accompanying picture). This has worked very well for me and has helped me save a ton of time for my business and hopefully will help me scale in the future as I can have other employees use the tool. I initially designed the whole add-in to work in a direct modeling environment, since it seemed easier to code for at the time. After testing the program extensively in the field, I realize the huge advantages of parametric modeling, and would really like to integrate it into the tool that I've created.

My Eventual goal is to group the entire make-flight operation into a singular base feature with the ability to retroactively change parameters just like you would be able to for any other tool in F360.

 

If I could just understand how to make a cylinder with a parameter-based radius in the API, I think I should be able to apply that concept to what I currently have. 

 

I would greatly appreciate any help!

 

StairGram Example for Forum.jpg

 

0 Likes
Accepted solutions (1)
3,366 Views
5 Replies
Replies (5)
Message 2 of 6

kandennti
Mentor
Mentor
Accepted solution

Hi @jakedbirk -San.

 

To be able to change the diameter of the circle with user parameters, a SketchDiameterDimension must be added to the circle.
This is the same as the GUI operation.

・・・
        #Create the circle
        circle: adsk.fusion.SketchCircle = sketch.sketchCurves.sketchCircles.addByCenterRadius(
            adsk.core.Point3D.create(0,0,0),
            1,
        )

        # Dimension text point
        textPoint: adsk.core.Point3D = circle.centerSketchPoint.geometry.copy()
        textPoint.translateBy(
            adsk.core.Vector3D.create(0,1,0)
        )

        # create Diameter Dimension
        dimensions: adsk.fusion.SketchDimensions = sketch.sketchDimensions
        diameterDim: adsk.fusion.SketchDiameterDimension = dimensions.addDiameterDimension(
            circle,
            textPoint,
            True,
        )

        # get ModelParameter
        modelPrm: adsk.fusion.ModelParameter = diameterDim.parameter

        # Set user parameter name in ModelParameter
        modelPrm.expression = paramRadius.name
・・・
0 Likes
Message 3 of 6

jakedbirk
Enthusiast
Enthusiast

Hi Kandennti, 

 

Thank you so much for the answer and I apologize for my own delayed response. 

Using your code as a guide, I have been able to get a good base-level understanding of creating and using dimensions and model parameters and made a few scripts that create other parametric models (like a cube) based on the principles shown in your example script. 

 

I do have a few questions about this script and how dimensions/parameters work in the API: 
1. Why does the centerSketchPoint of the circle need to be translated in order for the code to run properly? Without that translation, I received a runtime error 5.

2. In VS Code I am able to see that the model parameter object has a 'name' attribute (modelPrm.name) which is settable. but the documentation does not show a name property for the model parameter object. Is this an indication that parameters are not well supported by the API/its documentation? 

3. Is creating complex, parameter-driven models a good use case for the API? Or would it be best for me to stick to a direct modeling approach and just delete

Thanks again, 

Jake 

0 Likes
Message 4 of 6

kandennti
Mentor
Mentor

@jakedbirk -San.

 

1)This is because the second argument of the addDiameterDimension method requires a Point3D object.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-9e164ddb-fc7f-403e-8189-dce0d397fc83 
Of course, the following object will work, but it is more natural to have the dimension value near the circle.

 

adsk.core.Point3D.create(0,0,0)

 

 

2)ModelParameter.name property can be found here.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-7731f210-0b5f-4cb5-95b3-67c210262e61 

 

3)I am not sure about this. I think the right answer is what is easy for all script/add-in users to use.

0 Likes
Message 5 of 6

jakedbirk
Enthusiast
Enthusiast

Thank you again for the thoughtful reply. The documentation I was referencing was from a "Fusion 360 API" CHM file that I downloaded a while ago. Perhaps I should update it or reference the web version from now on; my outdated .chm only has four properties for modelParameter!

 

I am not sure I understand the textpoint concept completely, but your replies continue to push me in the right direction and I am sure that I will come to understand it in due time. 

 

Thanks from Alaska, 
Jake 

Message 6 of 6

Jorge_Jaramillo
Collaborator
Collaborator

@Jorge_Jaramillo - this post is being edited to remove PII.

 

Hi,

 

The textpoint contains the x,y,z coordinates to indicate where to place the text of the dimension.

When it was defined as:

 

textPoint: adsk.core.Point3D = circle.centerSketchPoint.geometry.copy()
textPoint.translateBy(
     adsk.core.Vector3D.create(0,1,0)
)

 

means "take the circle center and move 1 cm on the Y axis". (dimension values in the scripts are expressed in cm)

 

Regards,

Jorge Jaramillo

 

0 Likes