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

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

msm19b046
Contributor Contributor
552 Views
4 Replies
Message 1 of 5

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

msm19b046
Contributor
Contributor

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 

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

kandennti
Mentor
Mentor
Accepted solution

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()))
0 Likes
Message 3 of 5

msm19b046
Contributor
Contributor

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.

 

0 Likes
Message 4 of 5

msm19b046
Contributor
Contributor

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

0 Likes
Message 5 of 5

kandennti
Mentor
Mentor

@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()))