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 I itteratively export bodies, with slight alteration to sketch each time

4 REPLIES 4
Reply
Message 1 of 5
dewaine50
441 Views, 4 Replies

How do I itteratively export bodies, with slight alteration to sketch each time

Okay, so I have this big complex object (for now lets imagine it as a center point rectangle of 20mm x, 40mm y sketch on the xy plane, extruded up by 10mm in z), and my goal is to automate the process of exporting this object with varying length in y (20x50x10.stl, 20x60x10, 20x70x10, etc)

 

I'm pretty new to this, but here's my attempt to learn. 

 

#Author-
#Description-

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

def run(context):
    ui = None
    try:
        # Define what we are selecting
        app = adsk.core.Application.get()
        ui  = app.userInterface
        des = adsk.fusion.Design.cast(app.activeProduct)
        sk = des.rootComponent.sketches.itemByName('Sketch1')
        rootComp = design.rootComponent
        
        output = 'C:/Desktop/'
        
        # Select the length? Y axis
        lengthParam = des.allParameters.itemByName('Length')        
        
        
        for i in range(1,5)
            length = length + 10 * i
            
            lengthParam.expression = str(length)
            
            filename = output + length + '.stl'
            
            # Save the file as STL.
            exportMgr = adsk.fusion.ExportManager.cast(des.exportManager)
            stlOptions = exportMgr.createSTLExportOptions(rootComp)
            stlOptions.meshRefinement = adsk.fusion.MeshRefinementSettings.MeshRefinementMedium
            stlOptions.filename = filename
            exportMgr.execute(stlOptions)
       
       
       # object_methods = [method_name for method_name in dir(object) if callable(getattr(object, method_name))]
        
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

I have no idea what I'm doing, and can't help that I have something grossly wrong here.

Tags (3)
4 REPLIES 4
Message 2 of 5
BrianEkins
in reply to: dewaine50

You're very close.  Actually, it looks like all of the Fusion logic is OK but there are a couple of Python issues.  Here's your code with a couple of highlighted changes.

 

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

def run(context):
    ui = None
    try:
        # Define what we are selecting
        app = adsk.core.Application.get()
        ui  = app.userInterface
        des = adsk.fusion.Design.cast(app.activeProduct)
        sk = des.rootComponent.sketches.itemByName('Sketch1')
        rootComp = design.rootComponent
        
        output = 'C:/Desktop/'
        
        # Select the length? Y axis
        lengthParam = des.allParameters.itemByName('Length')        
        
length = 0 for i in range(1,5) length = length + 10 * i lengthParam.expression = str(length) filename = output + str(length) + '.stl' # Save the file as STL. exportMgr = adsk.fusion.ExportManager.cast(des.exportManager) stlOptions = exportMgr.createSTLExportOptions(rootComp) stlOptions.meshRefinement = adsk.fusion.MeshRefinementSettings.MeshRefinementMedium stlOptions.filename = filename exportMgr.execute(stlOptions) except: if ui: ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 5
dewaine50
in reply to: BrianEkins

It's saying expression is a nonetype object has no attribute expression? How do I use this expression part, and assign a new value to the expression class/method/object

Message 4 of 5
dewaine50
in reply to: BrianEkins

Ok, I think I might have figured out what is wrong. what is 

 

.itemByName('Length') ?

 

It's always been unclear to me how you're even able to select the specific sides of sketch, esp for complex non rectangular shapes, and fusion just "knows" which one you mean. Is every corner and every vertecies in the sketch assigned something that I can call upon? How do I know the full list of things I can call upon, and how do I access the sketch's names for all its individual pieces/lines?

 

I suspect 'Length' is something I must define, but I have nothing for which in the design history referred to as 'Length'. Or is this the wrong line of thinking

Message 5 of 5
p.seem
in reply to: dewaine50

Spinning through all the parameters is probably the harder way to do what you're after.  I didn't want to post this in two places, so have a look at my answer to your newer question and see if that helps at all.

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