Get Position of Body in Occurrence

Get Position of Body in Occurrence

rocco.mayr
Enthusiast Enthusiast
1,906 Views
8 Replies
Message 1 of 9

Get Position of Body in Occurrence

rocco.mayr
Enthusiast
Enthusiast

In this post is mentioned that you can get the position of an occurrence from its transform as Matrix3D:

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/component-s-origin-point/m-p/5805863#M725 

 

In the docs of Matrix3D I don't understand which methods give the position. Is it getAsCoordinateSystem() ? But how to use it? In the end I want the position values of the pivot from a body in a occurrence x,y,z.

 

 

# Get the root component of the active design.
rootComp = design.rootComponent

# Get the first sub component
occs = rootComp.occurrences
mat = occs.item(0).transform
subComp1 = occs.item(0).component
        
# Get the first body in sub component 1
baseBody = subComp1.bRepBodies.item(0)

...

baseBodyPosition = ...

 

 

 

0 Likes
Accepted solutions (1)
1,907 Views
8 Replies
Replies (8)
Message 2 of 9

BrianEkins
Mentor
Mentor

If all you care about is the position and not the orientation, the translation property will return a vector that is the offset of the occurrence from the origin.  Getting the matrix as a coordinate system will return this same point as the origin but will also return the direction of the X, Y, and Z axes of the occurrence.

 

A body doesn't have the concept of an "origin".  It's a grouping of geometry that exists in space.  It's like asking for the position of a line.  Do you mean the start or end, or even the midpoint of the line?  For a body, it's the same idea but much more complex.

 

An occurrence does have a distinct position.  An occurrence is a way to instance the contents of a component into an assembly.  A component has a distinct coordinate system, including the origin, which you can see in the browser under the Origin folder for each component.  An occurrence references a component and displays the contents of that component in the assembly.  The transform associated with an occurrence defines the position and orientation of the occurrence with respect to the coordinate system of the component the occurrence exists in.  Hopefully, some of that makes sense.  It's difficult to describe in a few sentences.  This article in the online help discusses occurrences in more detail but doesn't go into the transformations associated with them.

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

rocco.mayr
Enthusiast
Enthusiast

Yes I understood


@BrianEkins  schrieb:

If all you care about is the position and not the orientation, the translation property will return a vector that is the offset of the occurrence from the origin.  Getting the matrix as a coordinate system will return this same point as the origin but will also return the direction of the X, Y, and Z axes of the occurrence.

 

You mean the transform property? I don't find translation in occurrence. Or is it tranform.translation?

 

Would it be possible that you share a code snippet how to get the position of the occurrence?

0 Likes
Message 4 of 9

BrianEkins
Mentor
Mentor
Accepted solution

Here's a simple script that displays the position of the selected occurrence.

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

        occSel = ui.selectEntity('Select an occurrence.', 'Occurrences')
        occ: adsk.fusion.Occurrence = occSel.entity
        transVec = occ.transform.translation

        ui.messageBox(f'Position: {transVec.x:.5f}, {transVec.y:.5f}, {transVec.z:.5f}')
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 5 of 9

jackie.lin.945
Participant
Participant

for this line code

occ: adsk.fusion.Occurrence = occSel.entity

What does it mean for start occ:  

Jackie Lin

0 Likes
Message 6 of 9

JeromeBriot
Mentor
Mentor

Hello,

 

The line is equivalent to:

occ = occSel.entity

: adsk.fusion.Occurrence is a type annotation and it just informs that the occ variable is of type adsk.fusion.Occurrence.

 

Message 7 of 9

jackie.lin.945
Participant
Participant

Thank you.
Does Python syntax has this kind of usage?

0 Likes
Message 8 of 9

JeromeBriot
Mentor
Mentor

@jackie.lin.945  a écrit :

Does Python syntax has this kind of usage?


Yes. These annotations are optional and they only make the code easier to read.

0 Likes
Message 9 of 9

BrianEkins
Mentor
Mentor

This is discussed in more detail in the API documentation here: https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-743C88FB-CA3F-44B0-B0B9-FCC378D0D782#CodeHin...

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com