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 OUT DISTANCE BETWEEN TWO FACES IN MY 3D DESIGN USING PYTHON SCRIPT

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
msm19b046
167 Views, 4 Replies

HOW TO FIND OUT DISTANCE BETWEEN TWO FACES IN MY 3D DESIGN USING PYTHON SCRIPT

I created a 3D DESIGN USING PYTHON SCRIPT NOW I WANT TO DO VALIDATION SO FOR VALIDATION I WANT TO MEASURE DISTENCE BETWEEN TWO FACES HOW TO DO USING PYTHON SCRIPT IT IS VERY IMPORTANT T STUCK FROM LAST FOUR DAY

THANKS  IN ADVANCE 

4 REPLIES 4
Message 2 of 5
kandennti
in reply to: msm19b046

Hi @msm19b046 -San.

 

It is possible to measure the minimum distance by using MeasureManager.measureMinimumDistance.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-DDA91B87-6A18-40A1-A9D3-8094E34807C7 


Run the following sample to select the surface to be measured twice.

# 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

        # select first face
        selFilter: str = 'Faces'
        msg: str = 'Select First Face'
        sel: core.Selection = selectEnt(msg, selFilter)
        if not sel: return
        firstFace: fusion.BRepFace = sel.entity

        # select second face
        msg: str = 'Select Second Face'
        sel: core.Selection = selectEnt(msg, selFilter)
        if not sel: return
        secondFace: fusion.BRepFace = sel.entity

        # get minimum distance
        measMgr: core.MeasureManager = app.measureManager
        res: core.MeasureResults = measMgr.measureMinimumDistance(
            firstFace,
            secondFace,
        )

        # get units manager
        unitsMgr: core.UnitsManager = des.unitsManager

        # show result
        ui.messageBox(unitsMgr.formatInternalValue(res.value))

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Message 3 of 5
msm19b046
in reply to: kandennti

I WANT TO MEASURE DISTANCE NOT BY SELECTING I WANT TO DO THIS PROCESS AUTOMATE MEANS FUSION SELECT TOP FACE AND BOTTOM FACE AND PRINT THE DISTANCE WHILE I RUN THE SCRIPT 

PLEASE HELP IN THIS 

THANKS IN ADVANCE.

 

Message 4 of 5
msm19b046
in reply to: msm19b046

To measure the minimum distance between the top face and its opposite face without manual selection, you can use the following modified code. This code will automatically select the top face and its opposite face and then measure the minimum distance between them.

```python
import adsk.core
import adsk.fusion
import os
import traceback

def run(context):
ui: adsk.core.UserInterface = adsk.core.UserInterface.cast(None)

try:
stpFileName = r'C:/Test_step/350B1350.stp'

app: adsk.core.Application = adsk.core.Application.get()
ui = app.userInterface
app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
des: adsk.fusion.Design = app.activeProduct
root: adsk.fusion.Component = des.rootComponent
importManager: adsk.core.ImportManager = app.importManager
stpOptions: adsk.core.STEPImportOptions = importManager.createSTEPImportOptions(stpFileName)
importManager.importToTarget(stpOptions, root)

# Find the top face
topFace = None
for comp in root.allComponents:
for face in comp.bRepFaces:
if topFace is None or face.boundingBox.maxPoint.z > topFace.boundingBox.maxPoint.z:
topFace = face

if topFace is None:
ui.messageBox('No top face found.')
return

# Find the opposite face (lowest face)
oppositeFace = None
for comp in root.allComponents:
for face in comp.bRepFaces:
if oppositeFace is None or face.boundingBox.minPoint.z < oppositeFace.boundingBox.minPoint.z:
oppositeFace = face

if oppositeFace is None:
ui.messageBox('No opposite face found.')
return

measMgr: adsk.core.MeasureManager = app.measureManager
res: adsk.core.MeasureResults = measMgr.measureMinimumDistance(
topFace,
oppositeFace,
)

unitsMgr: adsk.core.UnitsManager = des.unitsManager

ui.messageBox('Minimum Distance: {}'.format(unitsMgr.formatInternalValue(res.value)))

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

i wrote this code for automate but is showing error if you can help me please help

Message 5 of 5
kandennti
in reply to: msm19b046

@msm19b046 -San.

 

If you use this when writing code, it will be displayed nicely.
Python is a language where indentation is important.

1.png


The way you get all the aspects is a bit different, but I think this will give you the process you want.

# Fusion360API Python script
import adsk.core
import adsk.fusion
import traceback
import sys

def run(context):
    ui: adsk.core.UserInterface = adsk.core.UserInterface.cast(None)

    try:
        stpFileName = r'C:/Test_step/350B1350.stp'

        app: adsk.core.Application = adsk.core.Application.get()
        ui = app.userInterface
        app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
        des: adsk.fusion.Design = app.activeProduct
        root: adsk.fusion.Component = des.rootComponent

        # import step file
        importManager: adsk.core.ImportManager = app.importManager
        stpOptions: adsk.core.STEPImportOptions = importManager.createSTEPImportOptions(stpFileName)
        importManager.importToTarget(stpOptions, root)

        # get all face
        faces = list(
            root.findBRepUsingPoint(
                adsk.core.Point3D.create(0,0,0),
                adsk.fusion.BRepEntityTypes.BRepFaceEntityType,
                sys.float_info.max,
                False,
            )
        )
        if len(faces) < 1:
            ui.messageBox("No surface found.")
            return

        # get top face
        topFace: adsk.fusion.BRepFace = max(faces, key=lambda f: f.boundingBox.maxPoint.z)

        # get buttom face
        buttomFace: adsk.fusion.BRepFace = min(faces, key=lambda f: f.boundingBox.minPoint.z)

        # get minimum distance
        measMgr: adsk.core.MeasureManager = app.measureManager
        res: adsk.core.MeasureResults = measMgr.measureMinimumDistance(
            topFace,
            buttomFace,
        )

        # get units manager
        unitsMgr: adsk.core.UnitsManager = des.unitsManager

        # show result
        ui.messageBox('Minimum Distance: {}'.format(unitsMgr.formatInternalValue(res.value)))

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

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