How to transform sketches correctly?

How to transform sketches correctly?

Joshua.mursic
Advocate Advocate
730 Views
3 Replies
Message 1 of 4

How to transform sketches correctly?

Joshua.mursic
Advocate
Advocate

Hello, I am struggling with a problem. I have a situation like this: A component with a mesh body and several sketches. I am trying to align the mesh and the sketches so that they are aligned with the occurrences origin. I have a sketch name "Axis" with a single sketch line that I would like to align with my occurrences Z axis, and a sketch named Center, that I would like to align with the origin of the occurrence. Then after I move the mesh and the sketches I want to transform the occurrence back to where it was originally.  I am able to get the mesh to align correctly, but I am struggling with getting the sketches to position correctly. 

 

Joshuamursic_2-1704212799756.png

 

Here is what I have so far.

 

import adsk.core, adsk.fusion, adsk.cam, traceback

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

        design:adsk.fusion.Design = app.activeDocument.products.itemByProductType("DesignProductType")
        root = design.rootComponent
        occurrence = root.occurrences.item(0)
        component = occurrence.component

        originalPosition = occurrence.transform2

        for sketch in component.sketches:
            if "Center" in sketch.name:
                center = sketch
            if "Axis" in sketch.name:
                axis = sketch

        #align the axis vector with the occurrence Z axis
        zVector = axis.sketchCurves.sketchLines.item(0).worldGeometry.asInfiniteLine().direction
        transformationMatrix = adsk.core.Matrix3D.create()
        transformationMatrix.setToRotateTo(zVector,adsk.core.Vector3D.create(0,0,1))
        #get the new location of the center after the rotation has been applied
        center = center.sketchPoints.item(1).worldGeometry
        center.transformBy(transformationMatrix)
        #apply the translation so the center sketch point is on the occurence origin
        translationVector = center.vectorTo(adsk.core.Point3D.create(0,0,0))
        transformationMatrix.translation = translationVector

        #align the mesh
        moveFeatures = component.features.moveFeatures
        objectsToMove = adsk.core.ObjectCollection.create()
        for body in occurrence.bRepBodies:
            objectsToMove.add(body)
        objectsToMove.add(component.meshBodies.item(0))
        moveMesh = moveFeatures.createInput2(objectsToMove)
        moveMesh.defineAsFreeMove(transformationMatrix)
        moveFeatures.add(moveMesh)

        #align the sketches
        for sketch in component.sketches:
            mat = sketch.transform
            #get the sketch in world space
            mat.transformBy(occurrence.transform2)
            #transform the sketch using the same matrix we used to transform the mesh
            mat.transformBy(transformationMatrix)
            sketch.transform = mat
        
        #move the occurrence back to where all the items originally where
        transformationMatrix.invert()
        transformationMatrix.transformBy(originalPosition)
        occurrence.transform2 = transformationMatrix


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

 

I have also attached a test project I have been using.

 

Any help is greatly appreciated.

 

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

BrianEkins
Mentor
Mentor

I think there is a bug when setting the sketch.transform property. I expected it to behave the same as the occurrence.transform property, which is when you set it, the transformation matrix you provide overrides the existing transform. However, I found when setting the sketch transform, it multiplies the existing sketch transform with the new transform, which results in it modifying, rather than replacing, the existing transform.

 

I thought a simple workaround would be to get the current sketch transform, invert it, and then assign it to the sketch. This results in the current transform of the sketch being an identity matrix. Then, transform it with the matrix you want. I added the following three lines before the call to set the transform using your matrix.

            t = sketch.transform
            t.invert
            sketch.transform = t
            sketch.transform = mat

However, I don't think the result is correct, but I also don't understand exactly what it is that you're trying to do, so I don't know what to look for. Hopefully, having this information is enough for you to figure it out.

 

I'll see if a bug can be logged. I think there's a risk to fixing the existing property since it would possibly break any existing programs where they've already figured this out. Instead, it's likely a new transform2 property might be added that behaves correctly.

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

Joshua.mursic
Advocate
Advocate

I see, thanks Brian. 

 

I am importing STL data into Fusion 360 from other software. When I import these meshes into fusion, they have positions in space that are not related to the orientation of the mesh. So I am trying to move the mesh, and sketches inside the occurrence so that the meshes and sketches relate to its origin point in fusion more accurately. One use case for this is the bounding boxes around meshes are relative to the origins position, so in the example document of the cube, its bounding box does not match the shape of the mesh well, until I align the mesh using some of the sketch lines as vectors. There are other meshes that are part of an assembly as well so its important the the position of the mesh not change in global space. I am basically trying to change the position of the occurrences origin position only.

 

Joshuamursic_0-1704328333552.png

 

0 Likes
Message 4 of 4

Joshua.mursic
Advocate
Advocate

Hello @BrianEkins, I have tried many ways to see if i can get the desired result and have been unable to do so. From what I saw, the issue is when the transformation matrix has any rotational component, the sketch's position is incorrect. But transformations to sketches parent occurrence produce the expected results. Could you please notify me when this bug has been resolved?

0 Likes