Getting the right position and rotation of an occurrence

Getting the right position and rotation of an occurrence

xenia.pie
Explorer Explorer
724 Views
3 Replies
Message 1 of 4

Getting the right position and rotation of an occurrence

xenia.pie
Explorer
Explorer

I have a similar problem as this one:

"Background: I'm working on a script that will let me export STLs for all the unique components in my Fusion 360 assembly along with a JSON file containing the position and rotation data info for all the occurrences (aka occurrence.transform.asArray()) .  I ultimately want to use this model data to recreate my project on the web using Three.js."

 

First problem was getting the right transformation form nested parts. Thanks to this post: https://forums.autodesk.com/t5/fusion-360-api-and-scripts/incorrect-transformation-from-occurrence-t... this is solved. But not quite.


The resultant assembly is not showing the right location of the parts since these parts have a "Rigid Joint" and therefore, their location is not the same as the transformation matrix. (see picture below)

Top view: Left-> Fusion; Right-> ViewerTop view: Left-> Fusion; Right-> Viewer

 

The positioning of the parts directly relates to their respective origins, but I can't get the information remaining, which is the location of the rigid joint of each leg assembly to the top table so I can calculate the resultant final location.

 

legs offsetlegs offset

 

So I guess my question is if there is a way of getting the Occurrence/BRepBody position or transformation matrix based on the overall origin and not its own origin?

0 Likes
725 Views
3 Replies
Replies (3)
Message 2 of 4

kandennti
Mentor
Mentor

Hi @xenia.pie .

 

At the time, I did not have enough knowledge about proxies.

 

Now I understand that it is not possible to get the matrix 3D from the component.

 

Can't I get the correct matrix this way?

def getRootMatrix(
        occ: adsk.fusion.Occurrence) -> adsk.core.Matrix3D:

    des: adsk.fusion.Design = occ.component.parentDesign
    root: adsk.fusion.Component = des.rootComponent

    occ_names = occ.fullPathName.split('+')
    occs = [root.allOccurrences.itemByName(name)
            for name in occ_names]

    mat: adsk.core.Matrix3D = adsk.core.Matrix3D.create()
    mat3ds: list = [occ.transform for occ in occs]
    mat3ds.reverse()  # important!!

    for mat3d in mat3ds:
        mat.transformBy(mat3d)

    return mat
0 Likes
Message 3 of 4

BrianEkins
Mentor
Mentor

It doesn't matter if an occurrence is positioned with a joint or moved to a location by the user.  It's current location is independent of how it got there.  There does seem to be a problem with the transform being returned by an occurrence.  In my testing, it seems it's always returning a transform that is relative to the assembly it's directly in.  If the occurrence is a proxy that defines the full path to the occurrence, the transform should be with respect to that full path.

 

Here's a workaround that builds the full transform using the proxy path.  You'll get occurrence proxies if the user selects an occurrence, if you use the allOccurrences property on the root component, or if you traverse the assembly structure by starting with the occurrences in the root component and use the childOccurrences property of the Occurrence object to step down.  You can pass in an occurrence obtained in any of these ways into this function and it should return the transform relative to root.

 

# Give an occurrence, this will combine all the transforms in it's
# assembly structure to return a transform for the occurrence that
# is with respect to the root assembly.
def getFullTransform(occ: adsk.fusion.Occurrence) -> adsk.core.Matrix3D:
    trans = occ.transform

    while occ.assemblyContext is not None:
        contextOcc = occ.assemblyContext
        trans.transformBy(contextOcc.transform)
        occ = occ.assemblyContext

    return trans

 

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

xenia.pie
Explorer
Explorer

Hi there @BrianEkins,

 

I just implemented what you suggest and what I get is not quite the transform matrix relative to root. I'm passing the occurrences one by one to this method "getFullTransform", but the resultant matrix is not right.

(Image attached of the resultant full assembly.)

xeniapie_0-1639491717014.png

 

Please see 3D model attached.

Thank you,

Xenia

 

0 Likes