Sketch Orientation

Sketch Orientation

apocalip
Contributor Contributor
2,779 Views
12 Replies
Message 1 of 13

Sketch Orientation

apocalip
Contributor
Contributor

Hey!

 

I'm quite confused here about the Sketch Orientations.

I'm building an AddIn to create Parametric BayonetLocks and have serious Problems creating the Sketches.

The main issue i have is, that the "orientation" of the sketches i create seems, kinda confusing or "random".

 

Take this Code for example where i create a Sketch on the root y/z Construction Plane and add lines that show the x/y/z directions:

 

app = adsk.core.Application.get()
ui = app.userInterface
design = adsk.fusion.Design.cast(app.activeProduct)
root = design.rootComponent
sketch = root.sketches.add(root.yZConstructionPlane)

length = 3
lines = sketch.sketchCurves.sketchLines
lines.addByTwoPoints(adsk.core.Point3D.create(0,0,0), adsk.core.Point3D.create(length,0,0))
lines.addByTwoPoints(adsk.core.Point3D.create(0,0,0), adsk.core.Point3D.create(0,length,0))
lines.addByTwoPoints(adsk.core.Point3D.create(0,0,0), adsk.core.Point3D.create(0,0,length))

 

 

This Results in a Sketch that looks like this: 

 

8nspD83yCY1c6T3vkBIw6xJg.png

 

The Problem is, that i want/need to "flip" the "z" axis. So the Line that currently goes down needs to go up.

But i'm seriously lost on how to achieve this. I've already spent ~4-5h trying to google, read and reread the API Documentation and tried Various ways like creating my own 3Point ConstructionPlane, trying to modify the sketch.xDirection/sketch.yDirection, tried to fiddle around with the sketch.origin/sketch.originPoint.geometry, etc.

 

But nothing i try gives me a sketch that i can work with the "z" direction going up.

 

If you ask yourself why i need this, i'm trying to code this AddIn properly parametrically and so i need to work with sketchDimensions, which sadly by Design *can not* be negative, so i can't simply move my sketch contents "up" by using negative dimensions.

 

I hope someone here might now a way to get this working, i'd love to build some AddIns, with parametrics build in but i'm kinda stuck on this issue right now...

0 Likes
2,780 Views
12 Replies
Replies (12)
Message 2 of 13

MichaelT_123
Advisor
Advisor

Hi Mr Apocalip,

 

Consider to check <Preferences><General><Default Modeling Orientation> in the personal settings of F360 as it establishes mother of all coordinate systems of your designs. I am not sure if the later is exposed in API,... just explore this area to find out.

 

Regards

MichaelT

 

 

 

MichaelT
0 Likes
Message 3 of 13

kandennti
Mentor
Mentor

I think there is no other way but to let it transform.

        # -- non transform --
        app = adsk.core.Application.get()
        ui = app.userInterface
        design = adsk.fusion.Design.cast(app.activeProduct)
        root = design.rootComponent
        sketch = root.sketches.add(root.yZConstructionPlane)
        sketch.name = 'non transform'

        length = 3
        lines = sketch.sketchCurves.sketchLines
        lines.addByTwoPoints(adsk.core.Point3D.create(0,0,0), adsk.core.Point3D.create(length,0,0))
        lines.addByTwoPoints(adsk.core.Point3D.create(0,0,0), adsk.core.Point3D.create(0,length,0))
        lines.addByTwoPoints(adsk.core.Point3D.create(0,0,0), adsk.core.Point3D.create(0,0,length))


        # -- transform --
        # init sketch
        skt= root.sketches.add(root.yZConstructionPlane)
        skt.name = 'transform'

        # get Matrix3D
        sktMat = skt.transform.copy()
        mat = adsk.core.Matrix3D.create()
        sktMat.transformBy(mat)
        sktMat.invert()

        # init point3D
        pnt3D = adsk.core.Point3D
        pntX = pnt3D.create(length,0,0)
        pntY = pnt3D.create(0,length,0)
        pntZ = pnt3D.create(0,0,length)

        # exec transform
        pntX.transformBy(sktMat)
        pntY.transformBy(sktMat)
        pntZ.transformBy(sktMat)

        # init skttch lines
        lines = skt.sketchCurves.sketchLines
        lines.addByTwoPoints(skt.originPoint, pntX)
        lines.addByTwoPoints(skt.originPoint, pntY)
        lines.addByTwoPoints(skt.originPoint, pntZ)

 

However, this is only valid for RootComponent sketches, and for Occurrence sketches, you will need to do more transforms.

0 Likes
Message 4 of 13

BrianEkins
Mentor
Mentor

I made a slight modification to your code to better visualize the sketch coordinate system.

app = adsk.core.Application.get()
ui = app.userInterface
design = adsk.fusion.Design.cast(app.activeProduct)
root = design.rootComponent
sketch = root.sketches.add(root.yZConstructionPlane)

length = 3
lines = sketch.sketchCurves.sketchLines
lines.addByTwoPoints(adsk.core.Point3D.create(0,0,0), adsk.core.Point3D.create(length,0,0))
length -= 0.5
lines.addByTwoPoints(adsk.core.Point3D.create(0,0,0), adsk.core.Point3D.create(0,length,0))
length -= 0.5
lines.addByTwoPoints(adsk.core.Point3D.create(0,0,0), adsk.core.Point3D.create(0,0,length))

 

When I run this, I get the results shown below.  Now it's obvious the X is in the -Z direction, Y is in the positive Y direction and the Z is in the positive X direction.  
SketchCoordNew.png

 

You commented that you wanted to flip the Z-axis, which is pointing down, but that's actually the X-axis.  The X and Y axes lie on the YZ plane as expected and the Z-axis is normal to the plane.  When you create a new sketch, Fusion has an internal algorithm it uses to define the orientation of the sketch on the selected plane.  The X and Y axes of the sketch will always lie on the selected plane but their orientation is determined by Fusion.  Currently, Fusion does not support any way to modify the sketch coordinate system.  Because this capability doesn't exist in Fusion the API can't provide it either.  Also, it's not possible to change one of the axes without affecting the others because it needs to follow right-hand coordinate system rules.

 

There is an exception to this and it's when working with a non-parametric sketch.  If you have a design that is set to not capture design history or if you've created a sketch within a base feature in a parametric design, you can use the Move feature to move and rotate the sketch any way you want. Unfortunately, there's an incorrect check in the API for the Move feature that doesn't allow you to provide a sketch as the entity you want to move so this isn't currently possible through the API.

 

A workaround in a parametric design is to create a new component and add the sketch to it, probably on the X-Y plane of the component.  You can now modify the position and orientation of the component any way you want, which will also change the sketch with respect to the root coordinate system.

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

DokH
Community Visitor
Community Visitor

Appologies, but I missing the logic. And why UI created sketches beahve differently from the API created sketches.

I had to reread this explanation several times before I understood the difference between sketch coordinate and model coordinate system.

I therefor added a bit of extra code to try and understand (see below).

Allow me to add some pictures.

XY.png

 XY Plane is logical.

 

 

XZ.png

XZ is not logical.

IMHO, the sketch's +Y should align with the model's +Z.

When editing a UI created sketch, the models X and Z  are graphically properly oriented for the view, that is horizontal to the rigth for X and vertical and up for the Z. So you would expect the sketch X and Y to follow that, but no, Y is negative. The other planes do follow a correct orientation.

At least UI and API created orientations are the same.

 

YZ.png

YZ is also not logical and here UI and API created sketches follow different rules.

Sketch +X should aling to model +Y and sketch +Y should follow model +Z .

 

These differences make it hard to program something thru API as it differs from UI interactions.

 

(Edit: Forgot the code I used)

# Fusion360API Python script

from logging import root
import traceback
import adsk.cam
import adsk.fusion
import adsk.core

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

        # Draw on predefined sketches
        predefinedSketches = ['preXY','preYZ','preXZ']

        for predefinedSketch in predefinedSketches:
            # select sketch
            sketch = sketches.itemByName(predefinedSketch)
            # draw lines
            lines = sketch.sketchCurves.sketchLines
            # draw along SKETCH x-axis
            length = 3
            lines.addByTwoPoints(adsk.core.Point3D.create(0,0,0), adsk.core.Point3D.create(length,0,0))
            # draw along SKETCH y-axis
            length = 5
            lines.addByTwoPoints(adsk.core.Point3D.create(0,0,0), adsk.core.Point3D.create(0,length,0))
            # draw along SKETCH z-axis
            length = 10
            lines.addByTwoPoints(adsk.core.Point3D.create(0,0,0), adsk.core.Point3D.create(0,0,length))

       
        # Create sketch on each origin plane thru API
        planes = [
            rootComp.xYConstructionPlane,
            rootComp.yZConstructionPlane,
            rootComp.xZConstructionPlane
        ]

        plane: adsk.fusion.ConstructionPlane
        for plane in planes:
            # create sketch
            sketch = sketches.add(plane)
            sketch.name = plane.name
            # draw lines
            lines = sketch.sketchCurves.sketchLines
            # draw along SKETCH x-axis
            length = 3
            lines.addByTwoPoints(adsk.core.Point3D.create(0,0,0), adsk.core.Point3D.create(length,0,0))
            # draw along SKETCH y-axis
            length = 5
            lines.addByTwoPoints(adsk.core.Point3D.create(0,0,0), adsk.core.Point3D.create(0,length,0))
            # draw along SKETCH z-axis
            length = 10
            lines.addByTwoPoints(adsk.core.Point3D.create(0,0,0), adsk.core.Point3D.create(0,0,length))

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

 

 

 

 

 

 

0 Likes
Message 6 of 13

apocalip
Contributor
Contributor

Yea, i absolutely agree with you, this is far from easy to understand or logical.

I put quite some effort to build a plugin to easily create 3D printable bayonet locks, but the Fusion360 API threw me off so many times i completely lost interest in it.

The API often feels like you're trying to guide a monkey that's operating the UI, sometimes it works, sometimes it just throws feces in your face and calls it a day.

 

I really Love Fusion360 for so many things, i've been using it almost since the release and i'm a paying custom since ~2 years, but some things about it are just frustrating.

 

In a perfect world i'd love the API to be way more like OpenSCAD, i know that the methodology of how they work internally is completely different, but the API Interface is just stupid and way to overly complex.

 

0 Likes
Message 7 of 13

Jorge_Jaramillo
Collaborator
Collaborator

Hi @apocalip ,

 

Why don't you sketch all your parts over root.xYConstructionPlane which is the plane that have all axis in the direction you need them to be?  If you need to make many sketches, define all of them on the same XY plane, and you won't need to deal with the orientation of the other planes.  Doesn't it work for you?

 

 

And regarding the behavior of the API, I believe Fusion360 needs to meet the requirements of a graphical/design tool (which all of us see easier to use with GUI) and also an engineering tool where a lot of linear and matrix algebra defines the different operations under the API functionality.  In my case, the use of the API made me reminded and reviewed many concepts I learned in the University, specially from the linear algebra course.

There are a lot of resources out there in internet; I used Khan Academy's in my reviews.

 

 

Regards,
Jorge

0 Likes
Message 8 of 13

apocalip
Contributor
Contributor

Hey Jorge,

 

no, sadly simply sketching everything on basically a single 2D Plane does not work.

In my case for the Bayonet Lock i created a cylinder and then needed to create a sketch plane to make a circular revolving cut inside that cylinder. And that's where i smashed my head against so many walls and barely comprehensible internal API behavior with those sketch planes that it completely lost me.

 

I absolutely agree, that the GUI should be that way, but the API in it's current form makes a lot of stuff extremely difficult and leaves to much potential in the mud that it makes me quite sad.

 

Cheers,

Marc

0 Likes
Message 9 of 13

Jorge_Jaramillo
Collaborator
Collaborator

Hi Marc,

 


no, sadly simply sketching everything on basically a single 2D Plane does not work.

No, it isn't just a 2D plane. Any point you use to sketch something on it will have the X, Y  and Z components, and you will get a 3D model.

Look at the Point3d.create() method , it has the 3 measurements to position it on the 3D space.

I do this way without any complication.  Give it a try and check yourself.

 

Regards,
Jorge

0 Likes
Message 10 of 13

apocalip
Contributor
Contributor

Right now the Codebase is almost 2 Years old, i can't just get it running again, so i can't exactly confirm/deny your advice.

I thing i tried that back and and failed as well, because, as i mentioned in the initial post at the top, that i had issues, that my points were "posititioned" in the wrong direction and i did not find a single working way to go the other way, because negative values *do not work*.

Maybe i'll manage to get enough interest build up again some day to give this another real try, but with the current state of the API i'm out to be honest, makes me sad as i said, but that's how i look at it currently.

0 Likes
Message 11 of 13

BrianEkins
Mentor
Mentor

I agree that the orientation of sketches created in Fusion (both in the UI and with the API) is not always logical. When working in the UI, it usually doesn't matter because you're not using a coordinate system other than when creating vertical and horizontal constraints. If the API creates a sketch oriented differently than the UI, that is a bug with the API. But even if it isn't the desired behavior, I don't know if it should be fixed because it's likely that programs have been written expecting the current behavior, and fixing it would break them.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes
Message 12 of 13

markusbarnes
Advocate
Advocate

I'm having a hard time understanding the logic behind Fusion's API sketch orientation. I am able to 2D sketch on the end face of a rectangular body, but when I repeat the same sketch on the opposite end face, the sketch plane orientation is wrong. It's sometimes perpendicular and or flipped horizontally or vertically to the end face, leaving me confused. After reading this thread several times, I'm even more clueless. Are there any articles explaining it in detail? One post here suggested taking a linear algebra course. If that's the case, I guess I need to consider other options. 

0 Likes
Message 13 of 13

BrianEkins
Mentor
Mentor

Unfortunately, this is the behavior of Fusion, and is not something specific to the API. If you interactively create sketches on the faces you described, they should be the same as if they were created using the API. When a sketch is created on a plane, Fusion uses an internal algorithm to determine the plane's origin and orientation. If you use the code in message 4 of this thread, it will draw lines showing the coordinate system of the sketch to verify this. Fusion doesn't currently support any way to change the origin or orientation of a sketch. The API has these same limitations.

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