Measuring Volume of ALL bodies in the root component and listing them separately

Measuring Volume of ALL bodies in the root component and listing them separately

ChapmanM605
Participant Participant
547 Views
2 Replies
Message 1 of 3

Measuring Volume of ALL bodies in the root component and listing them separately

ChapmanM605
Participant
Participant

Hi,

 

I have multiple bodies in a model all in the root component. I want to know the volume of each body. Display wise i am using the ui.messageBox but would like to export to excel or even just as text.

 

I have no problem getting and displaying the volume of any one body referenced individually:

 

body1 = rootComp.bRepBodies.item(0)
volume1 = body1.volume
v1 = str(volume1)
ui.messageBox('The volume is: ' + v1)
 
My struggle is trying to make a loop that iterates through all bodies to get each volume and display all on one message box (and later export to excel). I have tried multiple ways but this is an idea what i am trying below. Annoyingly the example code in the F360 reference document is really close but summates all the bodies together so i think avoids the need to store separately as a list maybe. I seem to keep come form trying to iterate with floats but struggling to convert to int like i have done before. 
 
Hope that makes sense and thanks in advnace
 
Matt
 
    for j in range(0, rootComp.bRepBodies.count😞
            body = rootComp.bRepBodies.item(j)
            volumelist = [body.volume]

      # Format a string to display the volume using the default distance units.
        result = design.unitsManager.formatValue(volumelist, design.unitsManager.defaultLengthUnits + '^3')
        ui.messageBox('The volume list is: ' + result)
0 Likes
Accepted solutions (1)
548 Views
2 Replies
Replies (2)
Message 2 of 3

Jorge_Jaramillo
Collaborator
Collaborator
Accepted solution

Hi,

 

Instead of the ui.messageBox() method you can use app.log() which use the Text Commands output view to display the results (I used it oftenly for my debugs).

Here you have a script that loops over root.bRepBodies to display every body's volume:

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

app = adsk.core.Application.get()
doc = app.activeDocument
des: adsk.fusion.Design = adsk.fusion.Design.cast(app.activeProduct)
root = des.rootComponent


def run(context) -> None:

    try:
        for body in root.bRepBodies:
            app.log(f'{body.name}, {body.volume} cm^3')
    except:
        app.log('Failed:\n{}'.format(traceback.format_exc()))
    adsk.terminate()

 

And this is a test I made:

Jorge_Jaramillo_0-1725630184730.png

 

I hope this can help.

 

Regards,

Jorge Jaramillo

 

Message 3 of 3

ChapmanM605
Participant
Participant

2 lines of code...Amazing - thank you! I knew i was missing something.

 

Thanks again

 

Matt