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 do you give a name a newly created object in API?

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
rbbauer00
619 Views, 2 Replies

How do you give a name a newly created object in API?

I am a hobby woodworker and build mostly furniture. I have become fairly efficient with Fusion 360 for my furniture design, but I want to be able to automate some of the things I do, such as joinery.  I work in the medical field and have no formal training in programming or CAD, but I have done some simple command line programming using BASIC and .BAT files.  I studied some basic Python tutorials to get a rudimentary grasp on the language, then went through Brian Ekins' videos about how the object tree works and how the API handles code.  I then perused the User Manual and Reference manual for the API.  While I understand the concepts in the manuals, I struggle with making them work. 

 

I start with all this to simply say I am a total noob and need major hand-holding, but I have tried to do my homework. I found my question asked before, but the answer was simply "Modify the Name property of the Sketch object."  I have no idea how do do that.  When I look up "name" under "sketches" in the manual, it says the follwoing, but I don't know how to insert this into my code to get it to work.

# Get the value of the property.
propertyValue = sketch_var.name


# Set the value of the property.
sketch_var.name = propertyValue

 

To try and figure it out, I am going through the Autodesk Design Academy Lab about making a part for a 3D printer.  The first lab is simply to create a sketch and draw some circles (code below). The code works just fine, but the created sketches are just called Sketch1, Sketch2, etc.  In this example, how would I give a different name to the newly created sketch ( or for that matter, any newly created body, component, etc.)?  

 

import adsk.core, adsk.fusion, traceback


def run(context):
    ui = None
    try:
        # Define the radius variables for circle1 and circle2        
        radiusCircle1 = 2
        radiusCircle2 = 0.156
        
        #  Get the User Interface, used for outputting the message box to the user       
        app = adsk.core.Application.get()
        ui  = app.userInterface
        # Get the Root Component of the active design
        design = app.activeProduct
        rootComp = adsk.fusion.Component.cast(design.rootComponent)




        # Create sketch 1 on the xy plane. 
        # First access the sketches collection of the root component
        sketches = rootComp.sketches
        # Get the xy construction plane from the root component
        xyPlane = rootComp.xYConstructionPlane
        # Add sketch 1 to the sketch collection using the xy plane
        sketch = sketches.add(xyPlane)


        # Draw a circle in the sketch. 
        # Access the sketchcurves and sketch circles collections within sketch 1
        circles = sketch.sketchCurves.sketchCircles
        # Add a circle to the sketch circles collection by using addByCenterRadius. 
        # The needed parameters are the center points and radius.
        circle1 = circles.addByCenterRadius(adsk.core.Point3D.create(0, 0, 0), radiusCircle1)


        # Create sketch 2 on the xy plane. 
        # Add sketch 2 to the same sketch collection using the same xy plane
        sketch2 = sketches.add(xyPlane)


        # Draw a circle in the sketch. 
        circles2 = sketch2.sketchCurves.sketchCircles
        # To create the origin of the second circle on the first circle,
        # use the radius of the first circle as the x coordinate when creating the 3Dpoint
        circle2 = circles2.addByCenterRadius(adsk.core.Point3D.create(radiusCircle1, 0, 0), radiusCircle2)






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

 

 

Thank you in advance for your help.

 

 

R. Brad Bauer
2 REPLIES 2
Message 2 of 3
goyals
in reply to: rbbauer00

Sharing below script which you can try

 

app = adsk.core.Application.get()

root = app.activeProduct.rootComponent # give you root component in design, you can use occurrences property to get the child components

sketches = root.sketches # give you all the sketches in a component

sketch = sketches[0] # give the first sketch in the sketches. You can use for loop to iterate on complete list

sketch.name = 'MySketch' # rename the sketch using sketch property.

 

Thanks.

 



Shyam Goyal
Sr. Software Dev. Manager
Message 3 of 3
rbbauer00
in reply to: goyals

That totally worked!!  I just added the last two lines after my sketches.add line.

 

 

 

sketch = sketches[0]
sketch.name = 'MySketch' 

 

 

 

 

I also noticed I could use sketches[-1] instead of sketches[0].  Is it safe to assume that every new object the API created will be at location [-1] until another of the same type is created?

 

Thank you for your help

R. Brad Bauer

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