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: 

Divide Path into equal segments

2 REPLIES 2
Reply
Message 1 of 3
GeoffPacker7777
716 Views, 2 Replies

Divide Path into equal segments

Hello,

 

I'm just starting off in Fusion, although experienced in solidworks/2d CAD. Enjoying the transition, great tool and a great price!

 

I am a complete novice a programming and trying to write a script in python to divide a path (not a single line) in equal sections. My workflow is to use the plane on path, followed by intersect tool in the sketch project/include and lastly points at each intersection.

 

My main focus is the plane on path bit. Ideally I would a requester box with select path and no division, but I think that is a bit beyond me at the moment!

 

Below is as far as I have managed to get, and it works up to a point!

 

 

 

#Author-Geoff Packer 
#Description-Select a path and divide using planes

import adsk.core, adsk.fusion, traceback
    
def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)
        if not design:
            ui.messageBox('It is not supported in current workspace, please change to MODEL workspace and try again.')
            return
        
        sel = ui.selectEntity('Select a path to divide', 'SketchCurves')
        selObj = sel.entity
        
        comp = design.rootComponent
        
        # create path
        feats = comp.features
        chainedOption = adsk.fusion.ChainedCurveOptions.connectedChainedCurves
        if adsk.fusion.BRepEdge.cast(selObj):
            chainedOption = adsk.fusion.ChainedCurveOptions.tangentChainedCurves
        path = adsk.fusion.Path.create(selObj, chainedOption)
        path = feats.createPath(selObj)
        
        # Get the root component of the active design
        rootComp = design.rootComponent
        
        # Get construction planes
        planes = rootComp.constructionPlanes
        
        # Create construction plane input
        planeInput = planes.createInput()
        
        # Add construction plane by distance on path
        No_of_Div = 17-1
        d2 = adsk.core.ValueInput.createByReal(1 / No_of_Div)
        d3 = adsk.core.ValueInput.createByReal( 2 * (1 / No_of_Div))
        d4 = adsk.core.ValueInput.createByReal( 3 * (1 / No_of_Div))        
        d5 = adsk.core.ValueInput.createByReal( 4 * (1 / No_of_Div))        
        d6 = adsk.core.ValueInput.createByReal( 5 * (1 / No_of_Div))
        d7 = adsk.core.ValueInput.createByReal( 6 * (1 / No_of_Div))
        d8 = adsk.core.ValueInput.createByReal( 7 * (1 / No_of_Div))
        d9 = adsk.core.ValueInput.createByReal( 8 * (1 / No_of_Div))
        d10 = adsk.core.ValueInput.createByReal( 9 * (1 / No_of_Div))
        d11 = adsk.core.ValueInput.createByReal( 10 * (1 / No_of_Div))
        d12 = adsk.core.ValueInput.createByReal( 11 * (1 / No_of_Div))
        d13 = adsk.core.ValueInput.createByReal( 12 * (1 / No_of_Div)) 
        d14 = adsk.core.ValueInput.createByReal( 13 * (1 / No_of_Div)) 
        d15 = adsk.core.ValueInput.createByReal( 14 * (1 / No_of_Div)) 
        d16 = adsk.core.ValueInput.createByReal( 15 * (1 / No_of_Div)) 
        #d17 = adsk.core.ValueInput.createByReal( 16 * (1 / No_of_Div)) 
        #d18 = adsk.core.ValueInput.createByReal( 17 * (1 / No_of_Div)) 
        #d19 = adsk.core.ValueInput.createByReal( 18 * (1 / No_of_Div))
        #d20 = adsk.core.ValueInput.createByReal( 19 * (1 / No_of_Div)) 
        #d21 = adsk.core.ValueInput.createByReal( 20 * (1 / No_of_Div)) 
        #d22 = adsk.core.ValueInput.createByReal( 21 * (1 / No_of_Div)) 
        #d23 = adsk.core.ValueInput.createByReal( 22 * (1 / No_of_Div))          
        planeInput.setByDistanceOnPath(selObj, d2)
        planes.add(planeInput)
        planeInput.setByDistanceOnPath(selObj, d3)
        planes.add(planeInput)
        planeInput.setByDistanceOnPath(selObj, d4)
        planes.add(planeInput)
        planeInput.setByDistanceOnPath(selObj, d5)
        planes.add(planeInput)
        planeInput.setByDistanceOnPath(selObj, d6)
        planes.add(planeInput)
        planeInput.setByDistanceOnPath(selObj, d7)
        planes.add(planeInput)        
        planeInput.setByDistanceOnPath(selObj, d8)
        planes.add(planeInput)        
        planeInput.setByDistanceOnPath(selObj, d9)
        planes.add(planeInput)        
        planeInput.setByDistanceOnPath(selObj, d10)
        planes.add(planeInput)
        planeInput.setByDistanceOnPath(selObj, d11)
        planes.add(planeInput)
        planeInput.setByDistanceOnPath(selObj, d12)
        planes.add(planeInput)
        planeInput.setByDistanceOnPath(selObj, d13)
        planes.add(planeInput)
        planeInput.setByDistanceOnPath(selObj, d14)
        planes.add(planeInput)
        planeInput.setByDistanceOnPath(selObj, d15)
        planes.add(planeInput)
        planeInput.setByDistanceOnPath(selObj, d16)
        planes.add(planeInput)
        #planeInput.setByDistanceOnPath(selObj, d17)
        #planes.add(planeInput)
 
        app.activeViewport.refresh()

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

 

Except I can only select a single line and not a path of multiple curves.

 

I have variable called path, which I have tried using in the lines like this: 

planeInput.setByDistanceOnPath(path, d2)

But I get errors

I have created this by copying and pasting and a little of my own work. I realise that the code is a bit crude, but we all have to start somewhere!

Can anyone advise how to select a joined curve like the plane on path command?

 

Thanks Geoff

 

2 REPLIES 2
Message 2 of 3
ekinsb
in reply to: GeoffPacker7777

You should be able to create and use a Path to create a distance on path type of construction plane.  However, I just tried it and it's not working with a path.  I've logged a defect and we'll work on getting it fixed.  Here's my simplified script that I used to test with and that I would expect to work once we make the fix.

 

def planesAlongPath():
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        
        curve = ui.selectEntity('Select curve on path', 'SketchCurves').entity
        
        if curve:
            path = adsk.fusion.Path.create(curve, adsk.fusion.ChainedCurveOptions.connectedChainedCurves)
            des = adsk.fusion.Design.cast(app.activeProduct)
            root = des.rootComponent            
            numPlanes = 10
            
            for i in range(numPlanes):
                planeInput = root.constructionPlanes.createInput()
                planeInput.setByDistanceOnPath(path, adsk.core.ValueInput.createByReal(i/(numPlanes-1)))
                
                plane = root.constructionPlanes.add(planeInput)          
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 3 of 3
GeoffPacker7777
in reply to: ekinsb

Thanks Brian for the really speedy response

 

I thought the range function was the way to go but was concentrating on the selecting part to start with.

 

I'll have to keep doing this manually for now!

 

How will I know when the defect is fixed? 

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