Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

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

Anonymous

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

Anonymous
Not applicable

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.

 

 

0 Likes
Reply
Accepted solutions (1)
874 Views
2 Replies
Replies (2)

goyals
Autodesk
Autodesk
Accepted solution

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
0 Likes

Anonymous
Not applicable

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

0 Likes