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: 

Move sketch coordinate system

9 REPLIES 9
SOLVED
Reply
Message 1 of 10
brad.bylls
537 Views, 9 Replies

Move sketch coordinate system

I have some code where the user selects a face for the sketch plane, and a point is created where the user selected that face.

 if changed_input.id == 'face':
            selection_input = inputs.itemById('face')
            selection = selection_input.selection(0)
            xyPlane = selection.entity
            clickPnt :adsk.core.Point3D = selection.point
            clickPoint = clickPnt
            _inputSelectNominalDiam.isVisible = True
            strResult2 = ' '
 
What I want to do is move the coordinate system to the point that the user selected (clickPoint) so I can create the sketches from that point. Centers of the circles shown below (could be anywhere on face)
It could be any face of the body/component.
Error.png
Brad Bylls
9 REPLIES 9
Message 2 of 10
BrianEkins
in reply to: brad.bylls

That's not currently possible.  When a sketch is created, Fusion uses internal logic to determine the origin and orientation of the sketch coordinate system.  It's not possible to control this logic or redefine the result.  The only thing possible is to move the sketch to a new plane, which causes Fusion to redo this logic for the new plane.  The ability to redefine the orientation of the sketch coordinate system has come up a few times in the past but Fusion doesn't currently support that capability internally so it's not possible to provide that capability through the API.  

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 10
kandennti
in reply to: brad.bylls

Hi @brad.bylls .

 

I don't think Fusion360 can create a sketch with an arbitrary position as the coordinate system.

The only way I can think of is to create an occurrence with the click point as the origin, and create a sketch in the XY plane.

I have created a sample to do that.

# Fusion360API Python script
import adsk.core, adsk.fusion, traceback

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

        # select PlanarFace
        msg :str = 'Select PlanarFace'
        selFiltter :str = 'PlanarFaces'
        sel :adsk.core.Selection = selectEnt(msg ,selFiltter)
        if not sel: return

        # get matrix3d from click point
        pnt :adsk.core.Point3D = sel.point
        eva :adsk.core.SurfaceEvaluator = sel.entity.evaluator
        _, vec = eva.getNormalAtPoint(pnt)
        mat :adsk.core.Matrix3D = adsk.core.Matrix3D.create()
        mat.setToRotateTo(adsk.core.Vector3D.create(0, 0, 1), vec)
        _, xAxis, yAxis, zAxis = mat.getAsCoordinateSystem()
        mat.setWithCoordinateSystem(pnt, xAxis, yAxis, zAxis)

        # create occurrence
        occ :adsk.fusion.Occurrence = root.occurrences.addNewComponent(mat)
        targetComp :adsk.fusion.Component = occ.component
        targetComp.isOriginFolderLightBulbOn = True

        # create Sketch
        skt :adsk.fusion.Sketch = targetComp.sketches.add(
            targetComp.xYConstructionPlane)
        skt.sketchCurves.sketchCircles.addByCenterRadius(
            adsk.core.Point3D.create(0, 0, 0), 1)

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

def selectEnt(
    msg :str,
    filtterStr :str) -> adsk.core.Selection :

    try:
        app = adsk.core.Application.get()
        ui = app.userInterface
        sel = ui.selectEntity(msg, filtterStr)
        return sel
    except:
        return None
Message 4 of 10
brad.bylls
in reply to: BrianEkins

Then how can you create a sketch on the plane with geometry based on that point?

Brad Bylls
Message 5 of 10
brad.bylls
in reply to: kandennti

Thanks kandennti,

 

Looks very confusing.

Your syntax is different than most with all of the :'s not sure I understand 

occ :adsk.fusion.Occurrence = root.occurrences.addNewComponent(mat) 
targetComp :adsk.fusion.Component = occ.component

etc.

I'll try to figure it out thanks.

Brad Bylls
Message 6 of 10
kandennti
in reply to: brad.bylls

@brad.bylls .

 

For more information about Occurrence, click here.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-88A4DB43-CFDD-4CFF-B124-7EE67915A07A 

The "Component" shown in the GUI is the "Occurrence" in the API.

 

This is a sample that creates a sketch at the clicked position.

# Fusion360API Python script
import adsk.core, adsk.fusion, traceback

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

        # select PlanarFace
        msg :str = 'Select PlanarFace'
        selFiltter :str = 'PlanarFaces'
        sel :adsk.core.Selection = selectEnt(msg ,selFiltter)
        if not sel: return

        # get click point & surface
        pnt :adsk.core.Point3D = sel.point
        ent :adsk.fusion.BRepFace = sel.entity

        # get component
        targetComp :adsk.fusion.Component = ent.body.parentComponent

        # create Sketch
        skt :adsk.fusion.Sketch = targetComp.sketches.addWithoutEdges(ent)
        skt.sketchCurves.sketchCircles.addByCenterRadius(
            skt.modelToSketchSpace(pnt) , 1)

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

def selectEnt(
    msg :str,
    filtterStr :str) -> adsk.core.Selection :

    try:
        app = adsk.core.Application.get()
        ui = app.userInterface
        sel = ui.selectEntity(msg, filtterStr)
        return sel
    except:
        return None
Message 7 of 10
BrianEkins
in reply to: brad.bylls

@brad.bylls, I'm not sure I understand your last question about creating a sketch on a plane with geometry based on a point.

 

What kandennti suggested is a reasonable workaround.  Here's an overview of what's happening in that case.  When you create a new component, Fusion creates a new component which is the container for all of the modeling content (sketches, construction geometry, features, etc.) and also creates an occurrence that references that component.  What you see in the browser and in the graphics window is the occurrence which is a reflection of the component.  The component has its own coordinate system that never changes and all the geometry in the component is built relative to that coordinate system.  The occurrence exists in the assembly and can be moved and rotated.  When you move or rotate an occurrence you're editing the occurrence and not the component it references.  You can have multiple occurrences of a component and each occurrence will typically be in a different location.

 

For example, I might have one bolt component but 50 instances of that bolt in an assembly. Each of these 50 instances of the bolt is an occurrence of the component that contains the actual bolt geometry.

 

A simple case is to create a component that contains a single sketch.  To make things easy, that sketch can be created on the X-Y base plane of the component.  There's an occurrence in the assembly that represents this component.  The component can be repositioned within the assembly, which has the effect of also moving the sketch.  The sketch doesn't actually move within the component but the occurrence moves which results in moving its representation of the component.

 

Hopefully, that helps and doesn't add to the confusion.  Here's a topic from the help that talks more about components and occurrences.  

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-88A4DB43-CFDD-4CFF-B124-7EE67915A07A

 

And here's a link to a presentation I did from Autodesk University that also covers this topic. I noticed it only has the video and is missing the paper and sample code, so I've attached it.  

https://www.autodesk.com/autodesk-university/class/Components-Components-Components-Depth-Look-Inter...

 

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 8 of 10
brad.bylls
in reply to: BrianEkins

Thanks Brian.
Very helpful.
Brad Bylls
Message 9 of 10
brad.bylls
in reply to: kandennti

I got it.

Seems to be all in the Matrix3D.

Thanks to all for the help.

Brad Bylls
Message 10 of 10
brad.bylls
in reply to: BrianEkins

I watched the video which made everything clear about occurrence vs. component.

Thanks for the help. 

Brad Bylls

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