How to create a sketch offset using the Fusion 360 API

How to create a sketch offset using the Fusion 360 API

alan.pieroway
Explorer Explorer
1,968 Views
2 Replies
Message 1 of 3

How to create a sketch offset using the Fusion 360 API

alan.pieroway
Explorer
Explorer

See my code below, if anyone can help it would be appreciated.
Like most here, I'm just starting down the Fusion 360 API coding path.

The .offset command is terribly documented. I've googled this, and read every possible answer.  
I'm surprised there are not more examples of every possible API command. 😞

I am creating random squares, and for each one I need to create an offset (in or out, it doesn't matter) by some value such as 1mm.

 

In my particular scenario, I can easily just make a 2nd rectangle for each that is slightly smaller or larger but that seems like a cheat. I'd like be able to use the .offset() command as well to determine if it is the better way to go.

How can this be achieved.  I am struggling with the first parameter, curves, and how to get them.

 

Thanks,

Alan 

 

 

 

import adsk.core, adsk.fusion, traceback
import random

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

        # Create a document.
        doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)

        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)

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

        # Create a sketch
        sketches = rootComp.sketches
        sketch1 = sketches.add(rootComp.yZConstructionPlane)
        print(sketch1.revisionId)
 
        # Get sketch lines
        sketchLines = sketch1.sketchCurves.sketchLines
 
        mx = 100
        my = 100
        offset = 2

        for i in range(0, 100, 10): 

                # Calculate the limits
                x = random.randint(0,mx)
                y = random.randint(0,my)
                h = random.randint(20,mx-20)
                w = random.randint(20,my-20)

                # Create sketch rectangle
                centerPoint = adsk.core.Point3D.create(x, y, 0)
                cornerPoint = adsk.core.Point3D.create(x+h, y+w, 0)
                sketch = sketchLines.addCenterPointRectangle(centerPoint, cornerPoint)

                #create an offset here, but how?

    except:
        if ui:

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

 

0 Likes
Accepted solutions (1)
1,969 Views
2 Replies
Replies (2)
Message 2 of 3

kandennti
Mentor
Mentor
Accepted solution

Hi @alan.pieroway .

 

Is this the right way to handle it?

・・・

        mx = 100
        my = 100
        offset = 2

        # create ObjectCollection
        objs = adsk.core.ObjectCollection.create()

        # speed up
        sketch1.isComputeDeferred = True

        for i in range(0, 100, 10): 

            # Calculate the limits
            x = random.randint(0,mx)
            y = random.randint(0,my)
            h = random.randint(20,mx-20)
            w = random.randint(20,my-20)

            # Create sketch rectangle
            centerPoint = adsk.core.Point3D.create(x, y, 0)
            cornerPoint = adsk.core.Point3D.create(x+h, y+w, 0)
            lines = sketchLines.addCenterPointRectangle(centerPoint, cornerPoint)

            # Assign to ObjectCollection
            objs.clear()
            [objs.add(l) for l in lines]

            # https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-fe28f2e1-bfbf-4521-ac35-14d677d38441
            # inner offset
            sketch1.offset(objs, centerPoint, offset)

            # outer offset
            outerPoint = adsk.core.Point3D.create(x+h*2, y+w*2, 0)
            sketch1.offset(objs, outerPoint, offset)
        
        sketch1.isComputeDeferred = False

    except:
        if ui:
・・・
0 Likes
Message 3 of 3

alan.pieroway
Explorer
Explorer

works great! thank you! 🙂

0 Likes