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: 

Fusion API Modify and Export Parameters to File

1 REPLY 1
Reply
Message 1 of 2
r.palmerRBN3J
884 Views, 1 Reply

Fusion API Modify and Export Parameters to File

Hello,

 

I am trying to write a Python script to modify certain vehicle parameters, and then output a mass and center of gravity for each parameter combination. I'd like these to output to a single txt or csv file. I've attempted to use the Parameter I/O Add-In to help me write this, but I'm not really sure what I'm doing so the process has been quite difficult. I'm hoping I can get an experts help as I imagine this is a fairly trivial task. I have also tried to incorporate some tips from @ekinsb particularly from This Post, though I'm not sure of how to modify from STL export to inputting data to a text file.

 

The code I have written thus far is below:

halfbody_range=[4,5,6]
radius_range=[0.4,0.5,0.6]

filename = ‘parameters output.csv’

def writeParametersToFile(filePath):
    app = adsk.core.Application.get()
    design = app.activeProduct
                      
    with open(filePath, 'w', newline='') as csvFile:
        csvWriter = csv.writer(csvFile, dialect=csv.excel)
        for param in design.allParameters:
            try:
                paramUnit = param.unit
            except:
                paramUnit = ""
            
            csvWriter.writerow([param.name, paramUnit, param.expression, param.comment]) 
    
    # get the name of the file without the path    
    partsOfFilePath = filePath.split("/")
    ui  = app.userInterface
    ui.messageBox('Parameters written to ' + partsOfFilePath[-1])       

def run(context):
	ui = None
	try:
		app = adsk.core.Application.get()
		ui = app.userInterface
		design = adsk.fusion.Design.cast(app.activeProduct)

		for angle in halfbody_range:
			userParams.itemByName(‘angle_halfbody’).expression = angle

			for radius in radius_range:
				userParams.itemByName(‘radius_range’).expression = radius

				# Let the view have a chance to paint just so you can watch the progress
				adsk.doEvents()


				propertyValue = Assembly.mass
				propertyValue = Assembly.centerOfMass
				writeParametersToFile

 

Basically, I want to cycle through each value in the halfbody_range and radius_range, having the CG and mass of the entire 'assembly' (Forgive me, I typically use Inventor) for that combination of user parameters output to the txt or csv file. In this case I should have 9 separate outputs.

 

I haven't attempted to run this script yet as I'm not sure I even have all of the required components. Any help you can offer me would be greatly appreciated. Thank you in advance!

1 REPLY 1
Message 2 of 2
BrianEkins
in reply to: r.palmerRBN3J

Here's a simpler version that I believe is close to what you described.  I'm sure you'll need to make some changes to get the specific output format you need but hopefully this will be a good start.

 

import adsk.core, adsk.fusion, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface
        design = adsk.fusion.Design.cast(app.activeProduct)
        root = design.rootComponent
        userParams = design.userParameters

        halfbody_range=['4', '5', '6']
        radius_range=['0.4', '0.5', '0.6']

        filename = 'C:/Temp/ParamResults.csv'
        f = open(filename, 'w')

        # Write the headers for the CSV file.
        f.write('HalfBody,Radius,Mass,Center Of Mass X,Center Of Mass Y, Center Of Mass Z\n')

        for angle in halfbody_range:
            userParams.itemByName('angle_halfbody').expression = angle

            for radius in radius_range:
                userParams.itemByName('radius_range').expression = radius

                # Let the view have a chance to paint just so you can watch the progress
                adsk.doEvents()

                # Get the mass and center of mass.
                physProps = adsk.fusion.PhysicalProperties.cast(root.physicalProperties)
                mass = str(physProps.mass)
                centerOfMass = physProps.centerOfMass

                f.write(angle + ',' + radius + ',' + mass + ',' + 
                        str(centerOfMass.x) + ',' + 
                        str(centerOfMass.y) + ',' + 
                        str(centerOfMass.z) + '\n')

        f.close()

        ui.messageBox('Finished. Results written to: ' + filename)
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com

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