Matrix of Occurrences.addNewComponent

Matrix of Occurrences.addNewComponent

kandennti
Mentor Mentor
446 Views
2 Replies
Message 1 of 3

Matrix of Occurrences.addNewComponent

kandennti
Mentor
Mentor

Hi there.
This is a question that was recently asked in the Japanese forum.
I answered it, but it seemed very complicated to me.

 

First, create an occurrence (Part_A:1) in the root component.
Then, we will move and rotate the created occurrence.

1.png
The attached file looks like this, although I made a sketch to check it later.

 

The goal is to create a new occurrence in Part_A with the origin at the position moved X+100mm.
2.png

 

I thought I could create a new one by doing the following process, since it uses the component.occurrences of Part_A.

# Fusion360API Python script

import traceback
import adsk.fusion
import adsk.core

def run(context):
    ui: adsk.core.UserInterface = None
    try:
        app: adsk.core.Application = adsk.core.Application.get()
        ui = app.userInterface
        des: adsk.fusion.Design = app.activeProduct
        root: adsk.fusion.Component = des.rootComponent

        # get Occurrence Part_A:1
        occPartA: adsk.fusion.Occurrence = root.occurrences[0]

        # Origin for the new Occurrence
        newOri: adsk.core.Point3D = adsk.core.Point3D.create(10,0,0)

        # Create Matrix
        mat: adsk.core.Matrix3D = adsk.core.Matrix3D.create()
        mat.setWithCoordinateSystem(
            newOri,
            adsk.core.Vector3D.create(1,0,0),
            adsk.core.Vector3D.create(0,1,0),
            adsk.core.Vector3D.create(0,0,1)
        )

        # Create Occurrence
        comp: adsk.fusion.Component = occPartA.component
        occs: adsk.fusion.Occurrences = comp.occurrences
        newOcc: adsk.fusion.Occurrence = occs.addNewComponent(mat)

        # Light on Origin
        newOcc.component.isOriginFolderLightBulbOn = True

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

 

When you actually run the script, the occurrence is created in a different position than expected.

3.png

 

This is what it looks like from the Top orientation.

4.png

 

The orientation of the XYZ axis is the same as that of Part_A, but the position of the origin is clearly created with the orientation of the root component.

5.png

 

I can't tell if this is a bug or a specification, but it seems to be making it very difficult to understand.
Is this really the right way to handle it?

 

In case you're wondering, here's a sample to help you get to the goal of this.

def run(context):
    ui: adsk.core.UserInterface = None
    try:
        app: adsk.core.Application = adsk.core.Application.get()
        ui = app.userInterface
        des: adsk.fusion.Design = app.activeProduct
        root: adsk.fusion.Component = des.rootComponent

        # get Occurrence Part_A:1
        occPartA: adsk.fusion.Occurrence = root.occurrences[0]

        # Origin for the new Occurrence
        newOri: adsk.core.Point3D = adsk.core.Point3D.create(10,0,0)

        # *** Adding ***
        # Get only the orientation of Occurrence.transform
        matPartA: adsk.core.Matrix3D = occPartA.transform
        matPartA.translation = adsk.core.Vector3D.create(0,0,0)

        # Align with the orientation of part_A
        newOri.transformBy(matPartA)
        # ******

        # Create Matrix
        mat: adsk.core.Matrix3D = adsk.core.Matrix3D.create()
        mat.setWithCoordinateSystem(
            newOri,
            adsk.core.Vector3D.create(1,0,0),
            adsk.core.Vector3D.create(0,1,0),
            adsk.core.Vector3D.create(0,0,1)
        )

        # Create Occurrence
        comp: adsk.fusion.Component = occPartA.component
        occs: adsk.fusion.Occurrences = comp.occurrences
        newOcc: adsk.fusion.Occurrence = occs.addNewComponent(mat)

        # Light on Origin
        newOcc.component.isOriginFolderLightBulbOn = True

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

 

However, if the process is to be performed at a deeper occurrence, additional calculations are required.

0 Likes
Accepted solutions (1)
447 Views
2 Replies
Replies (2)
Message 2 of 3

MichaelT_123
Advisor
Advisor
Accepted solution

Hi Kandennti-san,
Matrixes multiplications (transformations) are not commutative, so the order in wich they are performed matters.
In your particular case where you want Part_A to inherit Component2_occ place it into Part_A (and move it accordingly) before commiting its transformation of Part_A. Time line trick should be useful here.
When both transformations are completed check the resulting matrices of native occs and their proxies.
This should give you answer how to properly place Component2 in the workflow you described when “component inheritance" must be made “on the fly”.
Regards
MichaelT
PS. I am posting this explanation from my mobile, so it is brief. However I hope that it put you and your colleagues on the right path to conquer the issue.

 

MichaelT
Message 3 of 3

kandennti
Mentor
Mentor

@MichaelT_123 Thank you for your reply.

 

I understand that matrix multiplication is not commutative.
However, I feel that my inability to understand more than that is due to my lack of mathematical skills.

 

As it happens, the result of the process I presented is correct, and it seems to have been understood by the person who asked the question.

0 Likes