How to determine the distance between center point of cylindrical surfaces?

How to determine the distance between center point of cylindrical surfaces?

rwillardphil
Enthusiast Enthusiast
1,664 Views
5 Replies
Message 1 of 6

How to determine the distance between center point of cylindrical surfaces?

rwillardphil
Enthusiast
Enthusiast

I am new to the API and working through it. 

 

I have the code below, but it determines the distance between the points picked on the cylindrical surface, not the actual centerline of the cylindrical feature.  How do you drill down and get the center line  (or center point) of the cylindrical surfaces that are selected?  (Similar to how the "Measure" dialog box does it)

 

Do you need to change how the item is selected?  Or can you manipulate the selection and find the center?

 

# Start of Code

 

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

def run(context😞
    ui = None
    try:
        app = adsk.core.Application.get()
        design = app.activeProduct
        ui  = app.userInterface
        # ui.messageBox('Hey Phil')
        rootComp = adsk.fusion.Component.cast(design.rootComponent)    
        ui.messageBox('Select two clynidrical faces')
        selectedItem1 = ui.selectEntity("Select a Cylinder""CylindricalFaces")
        selectedItem1Value = selectedItem1.point
        selectedItem2 = ui.selectEntity("Select another Cylinder""CylindricalFaces")
        selectedItem2Value = selectedItem2.point
        lengthBetweenPoints = selectedItem1Value.distanceTo(selectedItem2Value)

        unitsMgr = design.unitsManager
        dispLengthBetweenPoints = unitsMgr.formatInternalValue(lengthBetweenPoints, unitsMgr.defaultLengthUnits, True)
        
        ui.messageBox('Length is' +dispLengthBetweenPoints)
0 Likes
Accepted solutions (1)
1,665 Views
5 Replies
  • API
Replies (5)
Message 2 of 6

BrianEkins
Mentor
Mentor

The BRepFace object supports the geometry method.  This will return various types of objects depending on the shape of the face.  In your case, since you set the selection filter to "CylindricalFaces" those faces will always return a Cylinder object as their geometry.  From a Cylinder you can get it's origin point, a vector defining the direction of the axis, and the radius.  In your case, you want the origin point.

 

Depending on your specific case, you might need to do more work than just getting the distance between two origin points. It's possible, depending on the geometry of your part, that the two points might not be on the same plane.  For example, if I have a point at 0, 0, 0 and another at 5, 0, 1 and I want to measure the 2D distance relative to the Z plane, I want to ignore the Z value so I get an answer of 5 instead of 5.1.  An easy way to do that is to set the Z value of the two points to 0 and then get the distance.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes
Message 3 of 6

rwillardphil
Enthusiast
Enthusiast

Brian,

 

Thanks for replying.  I really am trying to use the help files, but I cannot find how to extract the origin point from my selection (I am going to assume they are co-planar for this).

 

so my code of 

        selectedItem1 = ui.selectEntity("Select a Cylinder""CylindricalFaces")
That should give me a Cylindrical Face object in "selectedItem1".   So if my next line is this:
        originPoint1 = selectedItem1.????
How do you get the Origin Point?  I know that ".point" returns the point that was selected on the surface itself.   I don't see in the help files where to get the origin, or let alone what is available to grab at this point.    I want to be able to use the hlep files to follow this down, but seems like there are things that are not shown..... for "ui" "selectEntity" with a filter down to "CylindricalFaces" there should be a document showing what is avaiable in this object.... I can't seem to find that information.
 
Again thanks for helping.
 
Ryan.
 
 
 
 
0 Likes
Message 4 of 6

rwillardphil
Enthusiast
Enthusiast

@BrianEkins 

 

I found the chm file that allows you to dig deeper into the API information, and was able to get the following to almost work perfect for me.

 

selectedItem1Value = selectedItem1.entity.centroid
 
"selectedItem1" alone returns a value "selection"  ( why isn't this a BRepFace?)
adding "entity" returned a value of "BRepFace", so I was able to use the "centroid" property.  This works perfectly if the cylinders are both started from the same plane and have the same height.  If one is taller, it doesn't accurately show the center distance.
 
I have continued to look but cannot find the origin point. Do you have to manipulate the BRepFace object to get this?
 
Thanks,
 
Ryan
 
0 Likes
Message 5 of 6

BrianEkins
Mentor
Mentor
Accepted solution

Hopefully, this overview topic will be of some help.

http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-1C3FFADB-52C4-49BB-8981-4A448FFE4442

 

When you do a selection a Selection object is returned.  It provides the point where the selection occurred and the entity that was selected.  In your case the entity will be a BRepFace object.  You'll see in the topic I referenced above that faces can be many different shapes but because of the filter you specified for the selection, only BRepFaces that have a cylindrical shape can be selected.  This means that if you use the geometry property of the returned BRepFace you'll get back a Cylinder object.  It's from this object you can get the specific geometry information about the selected cylinder.

 

Here's an example of what I believe you're trying to do.  I'm assuming in this example that the axes of the cylinders are parallel to the Z axis so I'm ignoring any difference in the Z value.

def run(context):
    app = adsk.core.Application.get()
    ui  = app.userInterface

    try:
        # Have the first cylindrical face selected.
        cyl1Sel = ui.selectEntity('Select the first cylinder.', 'CylindricalFaces')
        cylFace1 : adsk.fusion.BRepFace = cyl1Sel.entity
        cyl1 : adsk.core.Cylinder = cylFace1.geometry

        # Have the second cylindrical face selected.
        cyl2Sel = ui.selectEntity('Select the second cylinder.', 'CylindricalFaces')
        cylFace2 : adsk.fusion.BRepFace = cyl2Sel.entity
        cyl2 : adsk.core.Cylinder = cylFace2.geometry

        # Get points along the axes of the cylinders.
        cyl1Point = cyl1.origin
        cyl2Point = cyl2.origin

        # Set the Z to zero so that eliminate any Z differences.
        cyl1Point.z = 0
        cyl2Point.z = 0

        dist = cyl1Point.distanceTo(cyl2Point)

        # Create a formatted string in the current units to display.
        des : adsk.fusion.Design = app.activeProduct
        result = des.unitsManager.formatInternalValue(dist, 
                                        des.unitsManager.defaultLengthUnits, True)
        ui.messageBox('Distance: ' + result)
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))    
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes
Message 6 of 6

rwillardphil
Enthusiast
Enthusiast

Thank you, that helps tremendously.

0 Likes