Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Converting points from alternate coordinate systems to match Fusion 360

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
bloudraak
335 Views, 4 Replies

Converting points from alternate coordinate systems to match Fusion 360

I have several mechanical drawings where the mounting holes for the device use a different coordinate system. Here's one, taken from below, with the origin be on the right bottom. It's essentially the same as the Fusion 360 coordinate system, except flipped over. 

 

wernersLNPQD_0-1645929733874.png

Some mechanical drawings also require to be rotated and moved.  If for a minute, imagine the above diagram is rotated 90 or 270 degrees.

 

I'm trying to figure out how to transform the points for holes from their coordinate system, to one that Fusion use. 

 

# Hardcoded for now
width = 77.0 # X 
depth = 44.0 # Y
points = [[61.69, 5.69], [61.69, 39.99], [4.24, 21.49]]
face = "bottom" 
# 
# Code Omitted 
#
pts = adsk.core.ObjectCollection.create()
sketchPoints = sketch.sketchPoints
for p in points:
    x = p[0]
    y = p[1]
    app.log(f'Adding hole at ({x}, {y})')
    pt = None
    if face == "bottom":
        pt = sketchPoints.add(adsk.core.Point3D.create(-1*x/10, y/10, 0))
    elif face == "top":
        pt = sketchPoints.add(adsk.core.Point3D.create(x/10, y/10, 0))
    else:
        raise "Not Implemented"
    # Rotate
    pts.add(pt)

 Is there a better approach to converting between coordinate systems in Fusion 360, than writing a ton of code?  

 

Software Engineer
https://wernerstrydom.com
4 REPLIES 4
Message 2 of 5
JeromeBriot
in reply to: bloudraak

Hello,

 

I would do like this:

 

# Hardcoded for now
width = 77.0 # X 
depth = 44.0 # Y
points = [[61.69, 5.69], [61.69, 39.99], [4.24, 21.49]]
face = "bottom" 
# 
# Code Omitted 
#

if face == "bottom":
    f = -1
elif face == "top":
    f = 1
else:
    raise "Not Implemented"
    
pts = adsk.core.ObjectCollection.create()
sketchPoints = sketch.sketchPoints

for p in points:
    x = p[0]
    y = p[1]
    app.log(f'Adding hole at ({x}, {y})')
    pt = None
    pt = sketchPoints.add(adsk.core.Point3D.create(f*x/10, y/10, 0))
    # Rotate
    pts.add(pt)

 

 

Message 3 of 5
bloudraak
in reply to: JeromeBriot

That's essentially what I ended up doing. That being said, I was hoping there's a built-in mechanism to convert between different coordinate systems.

Software Engineer
https://wernerstrydom.com
Message 4 of 5
BrianEkins
in reply to: bloudraak

A transformation matrix will do that for you. You can define a matrix that defines the transformation from one coordinate system to another and then use that to transform each of your points.

 

Here's an example.

Let's assume I have a coordinate system like the one in your original description where positive X is to the left and positive Y is up. I have some points whose coordinates are defined relative to that coordinate system.  I want to create those same points in Fusion but I know that coordinate system is different than the sketch coordinate system.  In this sample, the coordinate system is not only oriented like I described, but the origin is 10 cm in the X direction of the sketch coordinate system.  Here's a picture, where the coordinate system the points are defined in is purple and the sketch coordinate system is shown in green.  

MatrixSample.png

Here's the code. The original points are (1,1,0), (1,2,0), (1,3,0), and (1,4,0) and are defined relative to the purple coordinate system. In the code, the position and orientation of the purple coordinate system is defined. It then uses that to create a transformation matrix to go from that coordinate system to the sketch coordinate system. The points are then transformed and created in the sketch. They are positioned as shown in the picture. The (1,1,0) point now has the coordinate (9,1,0) after the transform has been applied.

 

I also draw some lines in the sketch so you can see where the X and Y axes are because Fusion doesn't show that.

 

def MatrixSample():
    try:
        des: adsk.fusion.Design = app.activeProduct

        # Define the coordinate system the points are currently defined in where it
        # is defined with respect to the sketch coordinate system.
        origin = adsk.core.Point3D.create(10, 0, 0)
        xDir = adsk.core.Vector3D.create(-1, 0, 0)
        yDir = adsk.core.Vector3D.create(0, 1, 0)

        points = []
        points.append(adsk.core.Point3D.create(1, 1, 0))        
        points.append(adsk.core.Point3D.create(1, 2, 0))        
        points.append(adsk.core.Point3D.create(1, 3, 0))        
        points.append(adsk.core.Point3D.create(1, 4, 0))        

        # Define the matrix that describe the transform into sketch space.
        otherCoordSys = adsk.core.Matrix3D.create()
        zDir = xDir.crossProduct(yDir)
        yDir = zDir.crossProduct(xDir)
        otherCoordSys.setWithCoordinateSystem(origin, xDir, yDir, zDir)

        # Invert it to shift it to the sketch coordinate system.
        otherCoordSys.invert()

        if app.activeEditObject.objectType != adsk.fusion.Sketch.classType():
            ui.messageBox('A sketch must be active.')
            return

        sketch: adsk.fusion.Sketch = app.activeEditObject
        for point in points:
            # Transform the point.
            point.transformBy(otherCoordSys)

            # Create the point in the sketch.
            sketch.sketchPoints.add(point)

        # Draw the axes of the sketch coordinate system to be able to visualize it.
        lines = sketch.sketchCurves.sketchLines
        lines.addByTwoPoints(adsk.core.Point3D.create(0,0,0), adsk.core.Point3D.create(10,0,0))
        lines.addByTwoPoints(adsk.core.Point3D.create(0,0,0), adsk.core.Point3D.create(0,5,0))
    except:
        ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

 

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 5 of 5
en9y37
in reply to: BrianEkins

Incredibly useful to me!!!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report