How do I select a sketch and edit it? How do I know what all the properties are?

How do I select a sketch and edit it? How do I know what all the properties are?

dewaine50
Advocate Advocate
2,517 Views
5 Replies
Message 1 of 6

How do I select a sketch and edit it? How do I know what all the properties are?

dewaine50
Advocate
Advocate

I'm trying to edit a simple sketch, and I can't seem to get the ropes if it all to work. 

All I'm trying to do is target vertecies of a rectangle sketch and change their distance, and there's little basic documentation on how to do this right. 

 

 

#call application
app = adsk.core.Application.get()
#target the active component and sketch
activeSelection = adsk.fusion.Design.cast(app.activeProduct)
MySketch = activeSelection.rootComponent.sketches.itemByName('NameOfMySketch')
TargetParameter = MySketch.allParameters.....??????
print(MySketch.sketchDimensions )?????
print(MySketch.sketchDimensions.list )??????


 

I'm really not sure wh

2,518 Views
5 Replies
Replies (5)
Message 2 of 6

p.seem
Advocate
Advocate

dewaine,

 

I sympathize - this is one of those places where the documentation really falls down.  It's great as a reference manual but until you know the right name or the right object to look up, it's really hard to get started.

 

As an example, here's some code that will (when run in a design with a single sketch called 'Sketch1' which contains a single rectangle) change one length of the rectangle.

 

        #call application
        app = adsk.core.Application.get()
        #target the active component and sketch
        activeSelection = adsk.fusion.Design.cast(app.activeProduct)
        MySketch = activeSelection.rootComponent.sketches.itemByName('Sketch1')
        
        allParms = activeSelection.allParameters
        
        firstLine = MySketch.sketchCurves.sketchLines.item(0)
        firstLine.endSketchPoint.move(adsk.core.Vector3D.create(10.0, 0, 0))

 

In your other question you're looking into the allParameters list (the unused allParms in my sample code).  What that actually gets you is a list of every parameter in the design -- everything you see in the Model space -> Modify ribbon -> Change Parameters.  If your design is parametric, that is going to be every number you've ever typed in while generating the model.  So if you know in advance the dimension you want, then you can give it a meaningful name and find it that way.  But in general I would suggest that, if you're trying to programatically find some particular feature, parsing it from that list is going to be... difficult.

 

In general fusion has this hierarchical model where it expects you to chose the sketch you want, then the sketch object/curve you want, and so on down the tree to the arc/line/point in question. The parameters list (and to a lesser extent, the Dimension list) flattens all of that hierarchy, so unless you know that the dimension you care about was called d746 you're probably never going to find it there.  But you can usually make more meaningful guesses about what object you're after by working through the tree.  You still need to do all the logic to work out which sketch object your after, but things like 'I want the shorter leg of the only rectangle in this sketch' are at least tractable.  Then when it comes time to actually make changes, you want to be thinking in terms of modifying the object, not editing the dimension.

 

If you start drilling into the documentation on the 'profiles' and 'sketchCurves' methods here then that might start to make more sense in this context.

 

Good luck!

 

Message 3 of 6

therealsamchaney
Advocate
Advocate

Thanks so much. The method itemByName was exactly what I was looking for. I feel like that should really be in one of the first articles in the API documentation pages. It's like they assume you already memorized all of the objects and methods somehow. Of course people are going to want to reference specific sketches or other items by name!

0 Likes
Message 4 of 6

pezlar.vojtech
Explorer
Explorer

Hello, 

thank you for your explanation. But let's say I have a sketch of a circle and I want to access the existing sketch of a circle and make the circle bigger. Is that a possibility? 

 

Or even more complicated. My dissertation is based on airfoil shapes. I want to draw a benchmark airfoil using control vertex splines (8 control vertex points). I then wait for some external software to give me new coordinates for those control vertex points. When I have these new coordinates I want to change the existing sketch using those coordinates. This will ideally run continually.  

0 Likes
Message 5 of 6

BrianEkins
Mentor
Mentor

Here's a completely different approach to the problem you described that takes advantage of the parametric capabilities of Fusion.

  1. Create an airfoil curve, either using a program or interactively. It doesn't have to be precise at this point but should be close and have the correct number of points.
  2. Add dimensions to each of the points to control the X and Y positions. This will require two dimensions for each point.
  3. Edit the names of the dimensions so it indicates which point and whether it's controlling the X or Y position.
  4. Create the extrusion using this sketch.
  5. Now, you can write a program that modifies the values of the parameters controlling the points. Changing these values will change the shape of the curve and the rest of the model will update automatically. After you've changed the parameters controlling the points, you can export the new modified body.

Here's a picture illustrating what I've tried to explain.

Airfoil.png

 

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 6 of 6

pezlar.vojtech
Explorer
Explorer

Dear Mr. Ekins, 

 

Thank you, this simplified it extremely. It was a good suggestion to think of it more in terms of how I would do it manually, rather than trying to think of it purely as a coding problem. 

 

Here I am altering a shape of a control vertex spline defined aerofoil: 

 

pezlarvojtech_0-1654855515362.png

pezlarvojtech_1-1654855594926.png

pezlarvojtech_2-1654855634862.png

 

Code:

 

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

def run(context):
    ui = None
    try:
        # Getting the object Application (All of Fusion)
        app = adsk.core.Application.get()
        #Getting UI if needed (dialog box, etc.)
        ui = app.userInterface          
 
        #get active object design
        design = app.activeProduct
        # Get the root component of the active design (the main component that encompasses all)
        rootComp = design.rootComponent
        # Get object features
        features = rootComp.features  
        # Get object sketches            
        sketches = rootComp.sketches        
       
        # Take existing sketch (only one present)
        aerofoil = sketches.item(0)

        # Get parameters from object design
        parameters = design.allParameters

        # Obtain dimensions
        X1 = parameters.itemByName('X1')
        Z1 = parameters.itemByName('Z1')
        X2 = parameters.itemByName('X2')
        Z2 = parameters.itemByName('Z2')
        X3 = parameters.itemByName('X3')
        Z3 = parameters.itemByName('Z3')
        X4 = parameters.itemByName('X4')
        Z4 = parameters.itemByName('Z4')
        X5 = parameters.itemByName('X5')
        Z5 = parameters.itemByName('Z5')
        X6 = parameters.itemByName('X6')
        Z6 = parameters.itemByName('Z6')

        # Alter Dimensions
        #X1.expression = str(20)
        #Z1.expression = str(30)
        #X2.expression = str(20)
        #Z2.expression = str(30)
        X3.expression = str(95)
        Z3.expression = str(95)
        X4.expression = str(250)
        Z4.expression = str(33)
        #X5.expression = str(20)
        #Z5.expression = str(30)
        #X6.expression = str(20)
        #Z6.expression = str(30)


        # Take a profile (Enclosed curves or line (sketch) creates a profile)
        #prof = aerofoil.profiles.item(0)        
        ## Create an extrusion from profile
        # Get object extrudeFeatures
        #extrudes = features.extrudeFeatures
        #Create set of instructions for the extrusion operation
        #extInputs = extrudes.createInput(prof, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
        #extInputs.setDistanceExtent(False, adsk.core.ValueInput.createByReal(-1))
        #extrudeFeature = extrudes.add(extInputs)

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