create a sketch on a selected face

create a sketch on a selected face

Anonymous
Not applicable
2,592 Views
7 Replies
Message 1 of 8

create a sketch on a selected face

Anonymous
Not applicable

I'm new to the API and am trying to create a sketch on a user selected face of a body.  I cannot find sample code anywhere and trying to use the manual and object references is confusing to me.  Can anyone point me in the right direction.  If possible provide a sample code.  Thank you.

0 Likes
Accepted solutions (1)
2,593 Views
7 Replies
Replies (7)
Message 2 of 8

BrianEkins
Mentor
Mentor
Accepted solution

Here's a very simple example and I agree that there definitely can be more information in the online help about how to work with sketches.  An overview of sketches has never been published.  You'll likely have some more questions about it after this.

 

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

        # Have the face selected.
        face = ui.selectEntity('Select the fact to create a sketch', 'PlanarFaces').entity

        # Create a sketch.
        sk = root.sketches.add(face)

        # Draw some geometry.
        sk.sketchCurves.sketchLines.addTwoPointRectangle(adsk.core.Point3D.create(0,0,0),
                                                         adsk.core.Point3D.create(8,4,0))
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

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

Anonymous
Not applicable

Thank you for your help Brian, 

 

  I was now able to project to a selected face, offset the projection, and do a simple extrude of the profiles.  I'm having trouble figuring out how to extrude to and entity though.  I want to extrude my profiles to a user selected face, but I'm am getting confused by what inputs I need.  When I looked at the extrude sample code, there is so many different samples in the same code and they all seem to reference each other, so I am not able to follow it.  The sample code:

extent_toentity = adsk.fusion.ToEntityExtentDefinition.create(body1, isChained)
uses a body, and I haven't had any luck figuring out how to get it to use my user selected face instead.  the face is selected with the following code:
 
ui.messageBox('select face')

        face = ui.selectEntity('Select a face''Faces').entity
 
Please help me understand what I need to do to generate the correct code.
 
Thank you so much.
0 Likes
Message 4 of 8

BrianEkins
Mentor
Mentor

Here's an example of extruding to a face.  And do you mind accepting my second post as a solution.  The first one I made from an account I'm not actively using.  I've asked for that post to be deleted.

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

        # Have the profile selected.
        profile = ui.selectEntity('Select the profile to extrude', 'Profiles').entity

        # Have the face to extrude to selected.
        face = ui.selectEntity('Select the face to extrude to', 'Faces').entity

        extrudes = root.features.extrudeFeatures
        input = extrudes.createInput(profile, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
        toEntityExtent = adsk.fusion.ToEntityExtentDefinition.create(face, True)
        input.setOneSideExtent(toEntityExtent, adsk.fusion.ExtentDirections.PositiveExtentDirection)
        extrude = extrudes.add(input)
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes
Message 5 of 8

Anonymous
Not applicable

Thank you, I got it to work.  I'm still struggling to figure out the help topic.  For example, I want to have a profile offest and a message box allow the user to type in the offset amount.  It looks like I need to create an addDistanceValueCommandInput. If I'm looking at the reference material correctly, that is accessed through a commandInput, which is accessed through commandcreatedEvent, which is accessed through commandDeffinition.

 

When I try to navigate through all of that I get lost and confused and end up not having a clue what I actually need.

on the addDistanceValueCommendInupt, the sample code on the access from page is returnValue = commandInputs_var.addDistanceValueCommandInput(id, name, initialValue)

 

I don't know what commandInputs_var is referencing.  The sample code I found has so many different things in it that I can't follow it.  The first line that reffers to the DistanceValue Input is

distanceValueInput = tab2ChildInputs.addDistanceValueCommandInput('distanceValue', 'DistanceValue', adsk.core.ValueInput.createByReal(2))

 

I'm not sure what tab2ChildInputs is.  

 

I'm also unclear on weather I need to create a resource folder with Images or not.

 

If you can offer any assistance of how I can navigate the help document or point me in the right direction I would greatly appreciate it.  I am very close to accomplishing my goal, but also very close to giving up because every time I think I'm figuring out something, I end up more confused.  All the online courses and tutorials I've been looking at are also different enough that they have only helped with creating sketches. 

 

So far you have been the biggest help.  I really appreciate it.

Thank you.

0 Likes
Message 6 of 8

brad.bylls
Collaborator
Collaborator

Hi,

I am trying to do the exact same thing.

However, I have discovered that the ui.selectEntity only seems to work in Scripts but not in Add-Ins.

In one of your posts somewhere (I can't find it now) you mentioned that it could be done with selectionInput.

Is there code somewhere that shows how to select the face and get the point where the face was selected?

I don't find anything that shows how to do that.

Thank you.

Brad Bylls
0 Likes
Message 7 of 8

kandennti
Mentor
Mentor

@brad.bylls .

 

If you use SelectionCommandInput, the whole thing will be longer.

#Fusion360API Python script
#Author-
#Description-Selection Filter SketchConstraints like

import adsk.core, adsk.fusion, traceback

_commandId = 'SelectionCommandInputSample'
_SelIptInfo = [
    'selIpt',
    'Select PlanarFace',
    'Select PlanarFace',
    'PlanarFaces']

_txtIptInfo = [
    'txtIpt',
    'Click position when selecting',
    '',
    3,
    True
]

_handlers = []
_app = None
_ui = None

class InputChangedHandler(adsk.core.InputChangedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        eventArgs = adsk.core.InputChangedEventArgs.cast(args)

        global _SelIptInfo
        if eventArgs.input.id != _SelIptInfo[0]:
            return
        
        selIpt = adsk.core.SelectionCommandInput.cast(eventArgs.input)

        global _txtIptInfo
        inputs :adsk.core.CommandInputs = eventArgs.inputs
        txtIpt :adsk.core.TextBoxCommandInput = inputs.itemById(_txtIptInfo[0])

        if selIpt.selectionCount < 1:
            txtIpt.text = '-'
        else:
            # SelectionCommandInput first Selection object.
            sel :adsk.core.Selection = selIpt.selection(0)

            clickPnt :adsk.core.Point3D = sel.point
            txtIpt.text = str(clickPnt.asArray())

class CommandDestroyHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            adsk.terminate()
        except:
            if _ui:
                _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

class CommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            # command
            cmd :adsk.core.Command = args.command
            cmd.isOKButtonVisible = False

            # event
            onDestroy = CommandDestroyHandler()
            cmd.destroy.add(onDestroy)
            _handlers.append(onDestroy)

            onInputChanged = InputChangedHandler()
            cmd.inputChanged.add(onInputChanged)
            _handlers.append(onInputChanged)

            # dialog
            inputs = cmd.commandInputs

            global _SelIptInfo
            selIpt = inputs.addSelectionInput(
                _SelIptInfo[0],
                _SelIptInfo[1],
                _SelIptInfo[2])
            selIpt.addSelectionFilter(_SelIptInfo[3])

            global _txtIptInfo
            txtIpt = inputs.addTextBoxCommandInput(
                _txtIptInfo[0],
                _txtIptInfo[1],
                _txtIptInfo[2],
                _txtIptInfo[3],
                _txtIptInfo[4],
            )

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

def run(context):
    try:
        global _app, _ui
        _app = adsk.core.Application.get()
        _ui = _app.userInterface

        cmdDefs = _ui.commandDefinitions
        cmdDef = cmdDefs.itemById(_commandId)
        if cmdDef:
            cmdDef.deleteMe()

        cmdDef = cmdDefs.addButtonDefinition(_commandId, _commandId, _commandId)

        onCmdCreated = CommandCreatedHandler()
        cmdDef.commandCreated.add(onCmdCreated)
        _handlers.append(onCmdCreated)
        cmdDef.execute()

        adsk.autoTerminate(False)
    except:
        if _ui:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

Look at this part of the InputChangedHandler class.

・・・
        else:
            # SelectionCommandInput first Selection object.
            sel :adsk.core.Selection = selIpt.selection(0)

            clickPnt :adsk.core.Point3D = sel.point
            txtIpt.text = str(clickPnt.asArray())
・・・

The Selection object you are getting in this part is the same as the return value of UserInterface.selectEntity Method.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-c05f97b8-9c8f-404b-8b21-f170d194ef8e 

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-e02e3629-e849-4881-92dc-f6f92bcffced 

 

The method for obtaining the click point coordinates is the same.

0 Likes
Message 8 of 8

brad.bylls
Collaborator
Collaborator

Thank you kandennti.

Brad Bylls
0 Likes