How to swap a sketch from a component to other

How to swap a sketch from a component to other

en9y37
Advocate Advocate
631 Views
3 Replies
Message 1 of 4

How to swap a sketch from a component to other

en9y37
Advocate
Advocate

Hi there.

 

I'm looking for a method to move a sketch from one component to other one via API. The result expected is the same as if drag it manually in the user interface like this:

 

BEFORE:

en9y37_0-1670252156063.png

 

AFTER:

en9y37_1-1670252214108.png

Any idea about this?

 

Thanks in advance

 

 

0 Likes
Accepted solutions (1)
632 Views
3 Replies
Replies (3)
Message 2 of 4

kandennti
Mentor
Mentor
Accepted solution

Hi @en9y37 .

 

I could not find that function in the API.

Not in a good way, but it was possible using text commands.

 

The following sample creates a new occurrence, sketches and extrudes it.
Then move the sketch to the root component.

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

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

        # create test data
        app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
        create_test_data()

        # select sketch
        des: adsk.fusion.Design = app.activeProduct
        root: adsk.fusion.Component = des.rootComponent

        fromOcc: adsk.fusion.Occurrence = root.occurrences[-1]
        nativeSketch: adsk.fusion.Sketch = fromOcc.component.sketches[-1]
        targetSketch: adsk.fusion.Sketch = nativeSketch.createForAssemblyContext(fromOcc)

        sels: adsk.core.Selections = ui.activeSelections
        sels.clear()
        sels.add(targetSketch)
        dump_sketch_info(targetSketch, 'before')
        app.activeViewport.fit()
        ui.messageBox('before')

        # exec cut command
        app.executeTextCommand(u'Commands.Start CutCommand')

        # select root component
        sels.clear()
        sels.add(root)

        # exec paste command
        app.executeTextCommand(u'Commands.Start PasteCommand')
        dump_sketch_info(targetSketch, 'after')

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


def dump_sketch_info(skt: adsk.fusion.Sketch, msg: str):
    msgLst = [
        f'**{msg}**',
        f'Component Name : {skt.parentComponent.name}',
    ]

    try:
        msgLst.append(f'entityToken : {skt.entityToken}')
    except:
        msgLst.append( 'error')

    adsk.core.Application.get().log('\n'.join(msgLst))


def create_test_data():
    app: adsk.core.Application = adsk.core.Application.get()
    des: adsk.fusion.Design = app.activeProduct
    root: adsk.fusion.Component = des.rootComponent

    occ: adsk.fusion.Occurrence = root.occurrences.addNewComponent(
        adsk.core.Matrix3D.create()
    )
    comp: adsk.fusion.Component = occ.component

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

    extFeats: adsk.fusion.ExtrudeFeatures = comp.features.extrudeFeatures
    extFeats.addSimple(
        skt.profiles[0],
        adsk.core.ValueInput.createByReal(5),
        adsk.fusion.FeatureOperations.NewBodyFeatureOperation
    )

After executing the script, you can confirm that it has been moved by operating undo.

 

However, since the original sketch object's entityToken property, etc. cannot be obtained, there may be some problems later on.

 

Message 3 of 4

en9y37
Advocate
Advocate

Hi @kandennti

 

Thanks for your response. I've given a try and works properly. Nice work around!!!

 

Message 4 of 4

BrianEkins
Mentor
Mentor

Unfortunately, the API does not currently support the ability to move a sketch from one component to another.

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