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: 

Python script to create a chain of lines

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
cadTPLWJ
2615 Views, 4 Replies

Python script to create a chain of lines

Code below is a script example.

Suppose I want lines between the points instead of the spline line, how would I do that?

Do I have to individually name all the little lines or can I make some kind of line collection?

 

import adsk.core, adsk.fusion, traceback

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

        doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
        design = app.activeProduct

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

        # Create a new sketch on the xy plane.
        sketch = rootComp.sketches.add(rootComp.xYConstructionPlane)

        # Create an object collection for the points.
        points = adsk.core.ObjectCollection.create()

        # Define the points the spline with fit through.
        points.add(adsk.core.Point3D.create(0, 0, 0))
        points.add(adsk.core.Point3D.create(5, 1, 0))
        points.add(adsk.core.Point3D.create(6, 4, 3))
        points.add(adsk.core.Point3D.create(7, 6, 6))
        points.add(adsk.core.Point3D.create(2, 3, 0))
        points.add(adsk.core.Point3D.create(0, 1, 0))

        # Create the spline.
        sketch.sketchCurves.sketchFittedSplines.add(points)
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

 

4 REPLIES 4
Message 2 of 5
NeutronAPI
in reply to: cadTPLWJ

Hello,

 

There is no API to create a chain of lines like what we create spline. However you should be able to create the lines one by one. Please refer to the codes as below:

 

import adsk.core, adsk.fusion, traceback

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

        doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
        design = app.activeProduct

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

        # Create a new sketch on the xy plane.
        sketch = rootComp.sketches.add(rootComp.xYConstructionPlane)

        # Create an object collection for the points.
        points = adsk.core.ObjectCollection.create()

        # Define the points the spline with fit through.
        points.add(adsk.core.Point3D.create(0, 0, 0))
        points.add(adsk.core.Point3D.create(5, 1, 0))
        points.add(adsk.core.Point3D.create(6, 4, 3))
        points.add(adsk.core.Point3D.create(7, 6, 6))
        points.add(adsk.core.Point3D.create(2, 3, 0))
        points.add(adsk.core.Point3D.create(0, 1, 0))

        for i in range(points.count-1):
            pt1 = points.item(i)
            pt2 = points.item(i+1)
            sketch.sketchCurves.sketchLines.addByTwoPoints(pt1, pt2)
        
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

Thanks,

Marshal

Message 3 of 5
ekinsb
in reply to: NeutronAPI

The code that Marshal gave you is good and will work.  However, the result may be slightly different than what you expect.  It creates five completely independent lines.  Their endpoints happen to be at the same coordinate but if you use the mouse to drag one of the end points you'll find that the lines are not connected.  Here's a slight change to the above code so that existing sketch points are used to create the new lines.

 

        for i in range(points.count-1):
            if i == 0:
                pt1 = points.item(i)
            else:
                pt1 = lastLine.endSketchPoint

            pt2 = points.item(i+1)
            lastLine = sketch.sketchCurves.sketchLines.addByTwoPoints(pt1, pt2)

Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 4 of 5
JeromeBriot
in reply to: ekinsb

Same code for a closed polygon:

 

    lines = sketch.sketchCurves.sketchLines

    pt1 = points.item(0)
    pt2 = points.item(1)
    firstLine = lines.addByTwoPoints(pt1, pt2)
    lastLine = firstLine

    for i in range(1,points.count-1):
        pt1 = lastLine.endSketchPoint
        pt2 = points.item(i+1)
        lastLine = lines.addByTwoPoints(pt1, pt2)

    pt1 = lastLine.endSketchPoint
    pt2 = firstLine.startSketchPoint
    lines.addByTwoPoints(pt1, pt2)

 

Message 5 of 5
buginaframe
in reply to: ekinsb

Hi Brian,

 

this is cool...it works, but could you explain what's happening, I try to wrap my head around it but I'm having a hard time.

Let's say I was trying to make a sketch of a closed square. I wrote this:

 

points = adsk.core.ObjectCollection.create()

 

points.add(adsk.core.Point3D.create(0,0,0))

points.add(adsk.core.Point3D.create(0,2,0))

points.add(adsk.core.Point3D.create(2,2,0))

points.add(adsk.core.Point3D.create(2,0,0))

points.add(adsk.core.Point3D.create(0,0,0))

 

Then I added your lines and it works, I have a closed square in a sketch. But...how? So if I am not mistaken: adsk.core.ObjectCollection.create() creates an object called "points" that will include the points below. How does it put it in my sketch?

 

Also,  can you break down your loop? I'm sorry if I'm asking too much or if I sound like a newb...I did coding in college 12 years ago and I'm trying to dig this back to the surface of my memory but it's hidden way deep in there

 

Thanks a lot, I appreciate it 🙂

 

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