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

dewaine50
Advocate

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

dewaine50
Advocate
Advocate

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.

0 Likes
Reply
654 Views
4 Replies
Replies (4)

BrianEkins
Mentor
Mentor

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

dewaine50
Advocate
Advocate

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

0 Likes

dewaine50
Advocate
Advocate

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

0 Likes

p.seem
Advocate
Advocate

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.

0 Likes