Use certain sketch in Fusion360 for Pipe by code

Use certain sketch in Fusion360 for Pipe by code

rocco.mayr
Enthusiast Enthusiast
517 Views
2 Replies
Message 1 of 3

Use certain sketch in Fusion360 for Pipe by code

rocco.mayr
Enthusiast
Enthusiast

I created a sketch in Fusion 360 and now I want to use this sketch in my code to create a pipe.

 

I have this base code:

def run(context):
    _ui = None
    try:
        _app = adsk.core.Application.get()
        _ui = _app.userInterface
        design = _app.activeProduct

        # Get the root component of the active design.
        rootComp = design.rootComponent

        # create sketch
        crv = initSktCircle(rootComp)

        # create pipe
        initPipe(crv)

def initSktCircle(comp :adsk.fusion.Component):
    skt :adsk.fusion.Sketch = comp.sketches.add(comp.xYConstructionPlane)

    pnt3D = adsk.core.Point3D
    crvs :adsk.fusion.SketchCurves = skt.sketchCurves
    crv = crvs.sketchCircles.addByCenterRadius(pnt3D.create(-5.0,-5,0), 4.0)

    return crv

def initPipe(path):

    sels :adsk.core.Selections = _ui.activeSelections
    sels.clear()
    sels.add(path)

    txtCmds = [
        u'Commands.Start PrimitivePipe', # show dialog
        u'Commands.SetDouble SWEEP_POP_ALONG 1.0', # input distance
        u'Commands.SetDouble SectionRadius 0.5', # input radius
        u'NuCommands.CommitCmd' # execute command
    ]
    
    for cmd in txtCmds:
        _app.executeTextCommand(cmd)

    sels.clear()

 

Now how could I bring my sketch in the code? I tried:

 

crv = rootComp.sketches.item(0).sketchCurves.sketchCircles

# create pipe
initPipe(crv)

 

 

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

kandennti
Mentor
Mentor
Accepted solution

Hi @rocco.mayr .

 

Processed for all circles in the specified sketch.

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

_app: adsk.core.Application = None
_ui: adsk.core.UserInterface = None


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

        # Get the root component of the active design.
        rootComp = design.rootComponent

        # create pipe
        for crv in rootComp.sketches.item(0).sketchCurves.sketchCircles:
            initPipe(crv)

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


def initPipe(path):
    global _app, _ui
    sels: adsk.core.Selections = _ui.activeSelections
    sels.clear()
    sels.add(path)

    txtCmds = [
        u'Commands.Start PrimitivePipe',  # show dialog
        u'Commands.SetDouble SWEEP_POP_ALONG 1.0',  # input distance
        u'Commands.SetDouble SectionRadius 0.5',  # input radius
        u'NuCommands.CommitCmd'  # execute command
    ]

    for cmd in txtCmds:
        _app.executeTextCommand(cmd)

    sels.clear()
Message 3 of 3

rocco.mayr
Enthusiast
Enthusiast
Accepted solution

Works Thx.

 

What also works is:

crv = rootComp.sketches.item(0).sketchCurves.sketchCircles.item(0)
initPipe(crv)