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 to find mass and cost of a Part/Model?

9 REPLIES 9
SOLVED
Reply
Message 1 of 10
ph0en1x01
507 Views, 9 Replies

How to find mass and cost of a Part/Model?

Suppose I created a 3D model using an API and assigned it material XYZ. After modifying its dimensions multiple times using the GUI, I now want to determine its mass and associated cost, preferably in an Excel file. Can this be achieved using the API?

If yes, please provide a universal script that can run on any 3D Model just by copy pasting it into the script.

 

This link contains chassis code which has 9 bodies:

https://github.com/purushottamnawale/cad-customization-of-ev-chassis/blob/main/Chassis.py

Tags (2)
Labels (1)
  • API
9 REPLIES 9
Message 2 of 10
BrianEkins
in reply to: ph0en1x01

You can get the volume and mass from the BRepBody and the Component object. You can't get the cost because Fusion does not know that. You might be able to combine the volume with some external material information to calculate a cost, but there are other factors to consider, such as the manufacturing process. For example, if you're 3D printing the model, the volume of a body may not reflect the actual volume of material used depending on the type of infill you use.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 10
ph0en1x01
in reply to: ph0en1x01

Thank you for your response. Would it be possible for you to provide me with a sample script?
My goal is to find the mass of all the bodies as shown in the image.
Screenshot (121).png

Message 4 of 10
kandennti
in reply to: ph0en1x01

Hi @ph0en1x01 .

 

It can be obtained with the physicalProperties property.

# Fusion360API Python script

import traceback
import adsk.core as core
import adsk.fusion as fusion

def run(context):
    ui: core.UserInterface = None
    try:
        app: core.Application = core.Application.get()
        ui = app.userInterface
        des: fusion.Design = app.activeProduct
        root: fusion.Component = des.rootComponent

        occ: fusion.Occurrence = root.occurrences[-1]
        show_physicalProperties(occ)

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


def show_physicalProperties(
    occ: fusion.Occurrence,
):

    props: fusion.PhysicalProperties = occ.physicalProperties
    origin, _, _, _ = occ.transform2.getAsCoordinateSystem()

    infos = [
        f'Mass \t\t{props.mass}',
        f'Volume \t\t{props.volume}',
        f'Density \t\t{props.density}',
        f'Area \t\t{props.area}',
        f'World X,Y,Z \t\t{origin.asArray()}',
        f'CenterOfMass \t{props.centerOfMass.asArray()}',
    ]

    app: core.Application = core.Application.get()
    ui: core.UserInterface = app.userInterface
    ui.messageBox('\n'.join(infos))

 

Message 5 of 10
ph0en1x01
in reply to: kandennti

It gives an error.
occ: fusion.Occurrence = root.occurrences[-1]
raise IndexError("The index (%d) is out of range." % i)
IndexError: The index (-1) is out of range.
Message 6 of 10
kandennti
in reply to: ph0en1x01

@ph0en1x01 .

 

I think it's because I ran it with data that has no occurrence in the root component.

If you don't know what that error means, you may have a lot of trouble in the future.

Message 7 of 10
ph0en1x01
in reply to: kandennti

Can you modify the same code, so that it can run for any 3D Model, just by copy pasting it into the script.

Message 8 of 10
kandennti
in reply to: ph0en1x01

@ph0en1x01 .

 

What is this "3D Model" you speak of?
Body? Components? Ocurrence?

Message 9 of 10
ph0en1x01
in reply to: kandennti

Message 10 of 10
kandennti
in reply to: ph0en1x01

@ph0en1x01 .

 

Select any of the body, occurrence or root components.

# Fusion360API Python script

import traceback
import adsk.core as core
import adsk.fusion as fusion

def run(context):
    ui: core.UserInterface = None
    try:
        app: core.Application = core.Application.get()
        ui = app.userInterface

        msg: str = 'Select Entity'
        selFilter: str = 'Bodies,Occurrences,RootComponents'
        sel: core.Selection = selectEnt(msg, selFilter)
        if not sel:
            return

        show_physicalProperties(sel.entity)
        a=1

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


def show_physicalProperties(
    entity: core.Base,
):

    if not hasattr(entity, 'physicalProperties'):
        return

    props: fusion.PhysicalProperties = entity.physicalProperties
    if hasattr(entity, 'transform2'):
        origin, _, _, _ = entity.transform2.getAsCoordinateSystem()
        worldXYZ = origin.asArray()
    else:
        worldXYZ = '-'

    infos = [
        f'Mass \t\t{props.mass}',
        f'Volume \t\t{props.volume}',
        f'Density \t\t{props.density}',
        f'Area \t\t{props.area}',
        f'World X,Y,Z \t\t{worldXYZ}',
        f'CenterOfMass \t{props.centerOfMass.asArray()}',
    ]

    app: core.Application = core.Application.get()
    ui: core.UserInterface = app.userInterface
    ui.messageBox('\n'.join(infos))


def selectEnt(
    msg: str,
    filterStr: str
) -> core.Selection:

    try:
        app: core.Application = core.Application.get()
        ui: core.UserInterface = app.userInterface
        sel = ui.selectEntity(msg, filterStr)
        return sel
    except:
        return None

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