Announcements

The Autodesk Community Forums has a new look. Read more about what's changed on the Community Announcements board.

Find the transform from one occurence to it's parent

danielWNW3M
Contributor

Find the transform from one occurence to it's parent

danielWNW3M
Contributor
Contributor

More or less as the title says. I have several nested components in my design, and I need to find the transform from the parent component to it's child. I can do this easily for top level components to the root with occurence.transform2, how can I get a similar output for the nested ones to their parents?

0 Likes
Reply
Accepted solutions (1)
420 Views
4 Replies
Replies (4)

BrianEkinsADSK
Autodesk
Autodesk
Accepted solution

Let's say you have the following assembly and want to know the transformation between Subassembly1:1 and Part2:1. First; you need to have proxies of the two occurrences so when you get their current transformation using transform2; it will be relative to world space (or the base coordinate system in root). 

 

Root

     SubAssembly1:1

          Part1:1

          Subassembly2:1

               Part2:1

 

# Get the transforms of the two occurrences.
subAssembly1Trans = subAssembly1Occ.transform2
part2Trans = part2Occ.transform2

# Invert the transform of the occurrence higher up in the tree. This results
# in defining a matrix that if applied to the subassembly, will result in 
# an identity matrix which would position the assembly to the origin and
# aligned with the model X-Y-Z axes.
subAssembly1Trans.invert()

# Transform the other occurrence matrix using the inverted matrix. After
# this, part2Trans will define the transformation between the two
# occurrences.
part2Trans.transformBy(subAssembly1Trans)

 

1 Like

kandennti
Mentor
Mentor

Hi @danielWNW3M -San.

 

We created and tested simple data.

The "ooc1" was moved X10mmY20mmZ30mm,
ooc2" is the data with X10mmY20mmZ30mm shifted further.

1.png

The following script was created and executed
The "transform" property is obsolete, but is still available in the current version 2.0.18950.

# 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
        root: fusion.Component = des.rootComponent

        occ1: fusion.Occurrence = root.occurrences[0]
        occ2: fusion.Occurrence = occ1.component.occurrences[0]

        dump(f"Fusion360 Ver{app.version}")
        dump_matrix(occ1.transform2,"occ1.transform2")
        dump_matrix(occ1.transform,"occ1.transform")
        dump_matrix(occ2.transform2,"occ2.transform2")
        dump_matrix(occ2.transform,"occ2.transform")

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


def dump_matrix(
        mat: core.Matrix3D,
        msg: str = ""
) -> None:
    origin, vecX, vecY, vecZ = mat.getAsCoordinateSystem()
    info = "".join(
        [
            f"{msg} : ",
            f"origin{origin.asArray()}_",
            f"vecX{vecX.asArray()}_",
            f"vecY{vecY.asArray()}_",
            f"vecZ{vecZ.asArray()}",
        ]
    )
    dump(info)


def dump(
    msg: str,
) -> None:

    print(msg)
    core.Application.get().log(msg)

 

Here is the result of the execution.

Fusion360 Ver2.0.18950
occ1.transform2 : origin(1.0, 2.0, 3.0)_vecX(1.0, 0.0, 0.0)_vecY(0.0, 1.0, 0.0)_vecZ(0.0, 0.0, 1.0)
occ1.transform : origin(1.0, 2.0, 3.0)_vecX(1.0, 0.0, 0.0)_vecY(0.0, 1.0, 0.0)_vecZ(0.0, 0.0, 1.0)
occ2.transform2 : origin(0.0, 0.0, 0.0)_vecX(1.0, 0.0, 0.0)_vecY(0.0, 1.0, 0.0)_vecZ(0.0, 0.0, 1.0)
occ2.transform : origin(1.0, 2.0, 3.0)_vecX(1.0, 0.0, 0.0)_vecY(0.0, 1.0, 0.0)_vecZ(0.0, 0.0, 1.0)

 

Here is the result I expected from occ2.transform2.

occ2.transform2 : origin(2.0, 4.0, 6.0)_vecX(1.0, 0.0, 0.0)_vecY(0.0, 1.0, 0.0)_vecZ(0.0, 0.0, 1.0)

It is probably broken now.

 

I think what you are looking for is the "transform" property.
I don't think "transform" property should be obsolete because "transform" property and "transform2" property have different meanings.

0 Likes

kandennti
Mentor
Mentor

Sorry I made a mistake.

・・・
        occ1: fusion.Occurrence = root.occurrences[0]
        occ2: fusion.Occurrence = occ1.component.occurrences[0]
        occ2 = occ2.createForAssemblyContext(occ1)
・・・
Fusion360 Ver2.0.18950
occ1.transform2 : origin(1.0, 2.0, 3.0)_vecX(1.0, 0.0, 0.0)_vecY(0.0, 1.0, 0.0)_vecZ(0.0, 0.0, 1.0)
occ1.transform : origin(1.0, 2.0, 3.0)_vecX(1.0, 0.0, 0.0)_vecY(0.0, 1.0, 0.0)_vecZ(0.0, 0.0, 1.0)
occ2.transform2 : origin(2.0, 4.0, 6.0)_vecX(1.0, 0.0, 0.0)_vecY(0.0, 1.0, 0.0)_vecZ(0.0, 0.0, 1.0)
occ2.transform : origin(1.0, 2.0, 3.0)_vecX(1.0, 0.0, 0.0)_vecY(0.0, 1.0, 0.0)_vecZ(0.0, 0.0, 1.0)


The transform2 property is working correctly.

However, what you need is the transform property.

1 Like

danielWNW3M
Contributor
Contributor

Thanks for the help! I ended up using transform 2 on the object and it's parent, inverting the parent, and using transformBy to get the transform between the two, as suggested by @BrianEkinsADSK. and it's working just as I'd hoped.

1 Like