Script to retrieve the volume a certain body

Script to retrieve the volume a certain body

Anonymous
Not applicable
1,349 Views
4 Replies
Message 1 of 5

Script to retrieve the volume a certain body

Anonymous
Not applicable

 

Hello,

 

I have a design with several bodies, one of these bodies being each boundary fill generated by a variable position plan (parametric) .

For each position of that plan, I need to get the volume a  (certain) body. So far I saved each instance as a new file (script generated),  then opened it and interrogated for the volume of the body of interest. 

 

The generated number of bodies I need to interrogate is huge (more than a thousand), therefore a very tedious job.

 

Can anybody help the with a script to get these volumes (volumes of body generated by each position of that plan), preferably output to a cvs file ? 

 

Thank  you,

Gabi

 

0 Likes
Accepted solutions (1)
1,350 Views
4 Replies
Replies (4)
Message 2 of 5

marshaltu
Autodesk
Autodesk

Hello,

 

I don't know what your design looked like. I would like to give sample codes how to get volumes of cell bodies by boundary fill command and hope it can help you.

 

I also attached the file the sample worked well with. There were a box and a work plane. I changed the position of plane after I got volume of cell bodies created by boundary fill. 

 

Thanks,

Marshal

 

#Author-
#Description-

import adsk.core, adsk.fusion, adsk.cam, 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

        box = root.bRepBodies.item(0)
        plane = root.constructionPlanes.item(0)
        tools = adsk.core.ObjectCollection.create()
        tools.add(box)
        tools.add(plane)
        
        boxlength = root.modelParameters.itemByName('d1').value # 9 cm
        planedistanceparam = root.modelParameters.itemByName('d6') 
        planedistance = planedistanceparam.value # 0 cm
        
        # The position of the plan will be changed from 0 to the length of box.
        boundaryFills = root.features.boundaryFillFeatures
        planedistancestep = 1 #cm
        results = ''
        while planedistance > boxlength * -1:
            planedistanceparam.value = planedistance - planedistancestep
            boundaryFillInput = boundaryFills.createInput(tools, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)  
            resultVolumes = ''
            i = 0
            for cell in boundaryFillInput.bRepCells:
                cell.isSelected = True
                body = cell.cellBody
                i = i + 1
                resultVolumes += 'cellbody{} volume {}, '.format(i, body.volume)
            boundaryFillFeature = boundaryFills.add(boundaryFillInput)
            planedistance = planedistanceparam.value
            results += 'plane position {}, {}\n'.format(planedistance, resultVolumes)
            boundaryFillFeature.deleteMe()
            
        ui.messageBox(results)
        
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))



Marshal Tu
Fusion Developer
>
0 Likes
Message 3 of 5

Anonymous
Not applicable

Hello Marshal, 

 

Thank you for your help but still I was not able to get any result (my scripting knowledge are too limited). I attached an example with 2 bodies: the variable red body (B) being created from the full body A. The volume I need to extract, for various positions of the cutting plan is body B. Can these values be exported to a csv file ?

 

Thank you,

Gabriel

0 Likes
Message 4 of 5

marshaltu
Autodesk
Autodesk

Hello,

 

The following sample demos how to change the position of cutting plane and get the volumes of all cells after cutting plane cuts "A" body. The path of csv need be changed if you want to play the sample in your side. 

 

PS: if you want to get the volume of one of cellbodies(e.g. you mentioned "B"), you have to define criteria(e.g. by bounding box of cell body) to filter it out. 

 

Thanks,

Marshal

 

#Author-
#Description-

import adsk.core, adsk.fusion, adsk.cam, 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

        A = root.bRepBodies.item(0)
        plane = root.constructionPlanes.item(0)
        tools = adsk.core.ObjectCollection.create()
        tools.add(A)
        tools.add(plane)

        planeposparam = root.modelParameters.itemByName('d5') 
        planepos = planeposparam.value # 0 deg
        
        # The position of the plan will be changed from 0 to the length of box.
        boundaryFills = root.features.boundaryFillFeatures
        planeposstep = design.unitsManager.convert(10, 'deg', 'rad') #10 deg
        poslimit = design.unitsManager.convert(180, 'deg', 'rad') #180 deg
        results = 'PlanePos Cell1 Volume Cell2 Volume Cell3 Volume\n'
        while planepos < poslimit:
            boundaryFillInput = boundaryFills.createInput(tools, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)  
            resultVolumes = ''
            i = 0
            for cell in boundaryFillInput.bRepCells:
                cell.isSelected = True
                body = cell.cellBody
                i = i + 1
                resultVolumes += '{} {} '.format(i, body.volume)
            boundaryFillFeature = boundaryFills.add(boundaryFillInput)
            boundaryFillFeature.deleteMe()
            boundaryFillInput = None
            planepos = planeposparam.value
            results += '{} {}\n'.format(design.unitsManager.convert(planepos, 'rad', 'deg'), resultVolumes)
            planeposparam.value = planepos + planeposstep
            
        with open('/Users/tum/Downloads/1.csv', 'w') as f:
            f.write(results)
            
        ui.messageBox('Ok')
        
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


Marshal Tu
Fusion Developer
>
Message 5 of 5

Anonymous
Not applicable
Accepted solution

Thanks Marshal  (and sorry for the delayed reply)! 

0 Likes