Transforming SketchPoints using a Transformation Matrix

Transforming SketchPoints using a Transformation Matrix

Joshua.mursic
Advocate Advocate
622 Views
2 Replies
Message 1 of 3

Transforming SketchPoints using a Transformation Matrix

Joshua.mursic
Advocate
Advocate

Hello, I am trying to get the sketchpoint with the the lowest Z value, relative to a different coordinate system. I have tried using the solution provided by @BrianEkins here:

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/converting-points-from-alternate-coordinat...

 

I use a construction axes and then try to generate any perpendicular vector. then try and build a coordinate system using the cross products.

here is what I have, but the lowPoint seems to still be defined in the original coordinate system

 

origin = design.activeComponent.constructionPoints.itemByName("PartOrigin").geometry
xAxis:adsk.core.Vector3D = design.activeComponent.constructionAxes.itemByName("partAxis").geometry.getData()[2]
#Get any perpendicular Vector
yAxisZ = ((xAxis.x*1+xAxis.y*1)/xAxis.z*-1)*-1
yAxis = adsk.core.Vector3D.create(1,1,yAxisZ)
zAxis = xAxis.crossProduct(yAxis)
yAxis = xAxis.crossProduct(zAxis)

transformMatrix = adsk.core.Matrix3D.create()
transformMatrix.setWithCoordinateSystem(origin, xAxis, yAxis, zAxis)
transformMatrix.invert()

zLow = 999999
for point in sketchSpline.fitPoints:
    # Transform the point.
    point:adsk.fusion.SketchPoint
    point.geometry.transformBy(transformMatrix) 
    if zLow > point.geometry.z:
        zLow = point.geometry.z
        lowPoint = point.geometry

 

 

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

BrianEkins
Mentor
Mentor
Accepted solution

I couldn't resist playing with this one. Give the code below a try.

 

import adsk.core, adsk.fusion, traceback

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

        # Get the sketch named "Points"
        pointSk = root.sketches.itemByName('Points')

        # Have a construction axis selected.
        axis: adsk.fusion.ConstructionAxis = ui.selectEntity('Select a construction line', 'ConstructionLines').entity
        
        # Create a matrix using the construction line direction as the Z axis.
        origin = axis.geometry.origin
        z = axis.geometry.direction
        x = GetPerpendicularVector(z)
        y = z.crossProduct(x)
        trans = adsk.core.Matrix3D.create()
        trans.setWithCoordinateSystem(origin, x, y, z)
        trans.invert()

        # Get the point coordinates in the world coordinate system and save 
        # them to a list along with the original sketch point.
        points = []
        for skPoint in pointSk.sketchPoints:
            if skPoint != pointSk.originPoint:
                points.append([skPoint.worldGeometry, skPoint])

        # Transform the point coordinates using the new coordinate system.
        pnt: adsk.core.Point3D
        for pnt in points:
            pnt[0].transformBy(trans)

        # Find the point with the smallest Z value.
        lowPnt = None
        for pnt in points:
            if lowPnt is None:
                lowPnt = pnt
            elif pnt[0].z < lowPnt[0].z:
                lowPnt = pnt
    
        # Select the point.
        ui.activeSelections.clear()
        ui.activeSelections.add(lowPnt[1])
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc())) 


def GetPerpendicularVector(vec: adsk.core.Vector3D) -> adsk.core.Vector3D:
    newVec = vec.copy()
    newVec.normalize()
    if newVec.isEqualTo(adsk.core.Vector3D.create(1, 0, 0)):
        temp = adsk.core.Vector3D.create(0, 1, 0)
    else:
        temp =  adsk.core.Vector3D.create(1, 0, 0)

    return newVec.crossProduct(temp)
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 3

Joshua.mursic
Advocate
Advocate

That works perfectly! Thank you

0 Likes