How to addNewComponenet using Transformation Matrix

Joshua.mursic
Advocate

How to addNewComponenet using Transformation Matrix

Joshua.mursic
Advocate
Advocate

Hello,

I am trying to add a component using a transformation matrix. 

 

newComponent = root.occurrences.addNewComponent(partMatrix)

 

 

When I try to add a new component using the partmatrix I get the error:

RuntimeError: 3 : invalid argument transform

 

I have the part Matrix in a dictionary called constructionData with the key "PartMatrix".  I tried to create a new matrix and then set it with the values of the partMatrix but this also errors.

 

 

partMatrix:adsk.core.Matrix3D = constructionData["PartMatrix"]
mat= adsk.core.Matrix3D.create()
mat.setWithCoordinateSystem(partMatrix.getAsCoordinateSystem()[0],
                                        partMatrix.getAsCoordinateSystem()[1],                                                                                                                            
                                        partMatrix.getAsCoordinateSystem()[2],
                                        partMatrix.getAsCoordinateSystem()[3])
newComponent = root.occurrences.addNewComponent(mat)

 

 

 

But when I do this, 

 

 

partMatrix:adsk.core.Matrix3D = constructionData["PartMatrix"]
mat= adsk.core.Matrix3D.create()
transformMatrix.setToRotateTo(adsk.core.Vector3D.create(1,0,0),partMatrix.getAsCoordinateSystem()[1],adsk.core.Vector3D.create(1,0,0))
transformMatrix.setToRotateTo(adsk.core.Vector3D.create(0,1,0),partMatrix.getAsCoordinateSystem()[2],adsk.core.Vector3D.create(0,1,0))
transformMatrix.setToRotateTo(adsk.core.Vector3D.create(0,0,1),partMatrix.getAsCoordinateSystem()[3],adsk.core.Vector3D.create(0,0,1))
newComponent = root.occurrences.addNewComponent(mat)

 

 

I am able to rotate the matrix. However because I am doing it in steps, the rotation is wrong.

 

I am not sure why I am getting an error.

 

here is the matrix data as an array:

(-0.04609723612371068, 0.08869538091126858, 0.0028591374116374167, 0.0, 0.08869538091126858, 0.046153189469425746, -0.0017357773488455935, 0.0, -0.002859137411637416, 0.0017357773488455943, -0.09994404367405274, 0.0, 1.9448515944543634, 0.9751591343287863, 1.9185717820420838, 1.0)

0 Likes
Reply
Accepted solutions (1)
511 Views
6 Replies
Replies (6)

MichaelT_123
Advisor
Advisor

Hi Mr JoshuaMursic,

 

... haven't analyzed all bits of the code but ... think about:

transformMatrix.setToRotateTo(adsk.core.Vector3D.create(0,0,1),partMatrix.getAsCoordinateSystem()[3],adsk.core.Vector3D.create(0,0,1))

 

Regards

MichaelT

 

MichaelT
1 Like

kandennti
Mentor
Mentor

Hi @Joshua.mursic .

 

A simple test was performed.
This one creates an occurrence without error.

# Fusion360API Python script

import traceback
import adsk
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

        mat: core.Matrix3D = adsk.core.Matrix3D.create()

        # https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-957d3c2d-294a-4fdb-8301-26ec31234655
        origin: core.Point3D = None
        xAxis: core.Vector3D = None
        yAxis: core.Vector3D = None
        zAxis: core.Vector3D = None
        origin, xAxis, yAxis, zAxis = mat.getAsCoordinateSystem()

        # https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-9a8c6abe-1b88-425a-b64c-cb0500554a5f
        mat.setWithCoordinateSystem(origin, xAxis, yAxis, zAxis)

        root.occurrences.addNewComponent(mat)

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

 

Fix the size of xAxis so that it is half the size.

This will cause an error when executed.

・・・
        origin, xAxis, yAxis, zAxis = mat.getAsCoordinateSystem()

        xAxis.scaleBy(0.5)
・・・

 

Next, the orientation of xAxis is corrected.

This also causes an error when executed.

・・・
        origin, xAxis, yAxis, zAxis = mat.getAsCoordinateSystem()

        xAxis.y += 1
        xAxis.normalize()
・・・

 

So we see that the xyz vector is a unit vector and each axis must be perpendicular to the other.

0 Likes

Joshua.mursic
Advocate
Advocate

I created the axis in the CAD environment using the matrix above.

Joshuamursic_0-1681475750844.png

as you can see, all of the angles are perpendicular. So I do not know why the 

root.occurrences.addNewComponent(partMatrix) is giving errors.
 
I am trying to import the part in at its position and then align it to the global coordinate system.

 

0 Likes

Joshua.mursic
Advocate
Advocate

Joshuamursic_0-1681488496358.png

I calculated the Determinant and it = 1.000000015592180721

@kandennti was correct and the matrix is not Orthogonal. 

 

I tried to normalize each axis:

partCoordinate = partMatrix.getAsCoordinateSystem()
origin = partCoordinate[0]
xAxis = partCoordinate[1]
yAxis = partCoordinate[2]
zAxis = partCoordinate[3]

xAxis.normalize()
yAxis.normalize()
zAxis.normalize()

testMatrix = adsk.core.Matrix3D.create()
testMatrix.setWithCoordinateSystem(origin,xAxis,yAxis,zAxis)
newComponent = root.occurrences.addNewComponent(testMatrix)

But this also fails.

 

Here is the original data from the CAD output xml.

<_00>-0.460972361237106780</_00>
<_01>0.886953809112685840</_01>
<_02>0.028591374116374167</_02>
<_03>0</_03>
<_10>0.886953809112685840</_10>
<_11>0.461531894694257480</_11>
<_12>-0.017357773488455935</_12>
<_13>0</_13>
<_20>-0.028591374116374160</_20>
<_21>0.017357773488455942</_21>
<_22>-0.999440436740527400</_22>
<_23>0</_23>
<_30>19.44851594454363300</_30>
<_31>9.751591343287863400</_31>
<_32>19.18571782042083700</_32>
<_33>1</_33>

 

I assumed that the xml elements were _00 for column 0 row 0 in the matrix. but none of the resulting vectors are normal. 

1 Like

BrianEkins
Mentor
Mentor
Accepted solution

If the matrix isn't orthogonal, the problem is with the code extracting the matrix information from the CAD model. One of your previous pictures shows the axes are perpendicular to each other but maybe it's being interpreted incorrectly to write out the matrix definition.

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

Joshua.mursic
Advocate
Advocate

Yes, I think the CAD software outputting the matrix is slightly wrong. The determinant is  1.000000015592180721 when it should be 1 if it was Orthoginal (from my basic understanding). Thanks you everyone for your help.

0 Likes