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: 

Getting profiles from sketches in order to loft

1 REPLY 1
SOLVED
Reply
Message 1 of 2
peter6G3RQ
348 Views, 1 Reply

Getting profiles from sketches in order to loft

Hello,

 

I have cobbled together the following script that takes a user selected curve, draws a rectangle at each end based on user parameters with the intention of then lofting the profiles along the original curve. I have had all the parts working at various points but as yet not all together. I struggled for a while to make the rectangles parametrically adjustable but now that I have managed to sort that I'm lost as to how I can get the profiles from them that I need in order to loft. The script as pasted below works perfectly up until

#Create Loft

because I am unable to define the profiles

 

I think that because the sketch is being defined in a separate function(?) that I can't call the profiles with

profile1 = sketch.profiles[0]

as I was before. 

 

I assume (hope) there is a way to grab the profiles from the sketch but I cannot find any way to do it. Any help would be much appreciated.

 

Many thanks in advance

Pete

 

#Description-Select a path to create loft.

import adsk.core, adsk.fusion, traceback, math

def oAddRect(sketches, plane, center, length_params):
    # Reference the width and height user parameters values
    app = adsk.core.Application.get()
    product = app.activeProduct
    design = adsk.fusion.Design.cast(product)
    userParams = design.userParameters
    width = userParams.itemByName('BSOx').value
    height = userParams.itemByName('BSOy').value

    # Create the rectangle
    sketch = sketches.add(plane)
    sketchlines = sketch.sketchCurves.sketchLines;  
    p1 = adsk.core.Point3D.create(-width / 2, -height / 2, 0)
    p2 = adsk.core.Point3D.create(width / 2, height / 2, 0)
    recLines = sketchlines.addTwoPointRectangle(p1, p2)

    # Constrain the Rectangle to stay rectangular
    constraints = sketch.geometricConstraints
    constraints.addPerpendicular(recLines.item(0), recLines.item(1))
    constraints.addHorizontal(recLines.item(0))    
    constraints.addParallel(recLines.item(0), recLines.item(2))
    constraints.addParallel(recLines.item(1), recLines.item(3))

    # Add construction lines to constrain the rectangle to the center point
    lines = sketch.sketchCurves.sketchLines
    diagonal1 = lines.addByTwoPoints(recLines.item(0).startSketchPoint, recLines.item(2).startSketchPoint)  
    diagonal1.isConstruction = True
    #diagonal1.isVisible = False
    diagonal2 = lines.addByTwoPoints(recLines.item(1).startSketchPoint, recLines.item(3).startSketchPoint)    
    diagonal2.isConstruction = True
    #diagonal2.isVisible = False
  
    # Constrain the rectangle to be centered on the center point
    sketchpoints = sketch.sketchPoints; 
    sk_center = sketchpoints.add(center)
    sk_center.isfixed = True
    constraints.addCoincident(sk_center, diagonal1)
    constraints.addCoincident(sk_center, diagonal2)
    constraints.addCoincident(sk_center, sketch.originPoint)

    # Dimension the rectangle with the user parameters
    w = sketch.sketchDimensions.addDistanceDimension(recLines.item(0).startSketchPoint, recLines.item(0).endSketchPoint,
                                                     adsk.fusion.DimensionOrientations.HorizontalDimensionOrientation,
                                                     adsk.core.Point3D.create(0, height, 0)); 
    w.parameter.expression = 'BSOx'    
    h = sketch.sketchDimensions.addDistanceDimension(recLines.item(1).startSketchPoint, recLines.item(1).endSketchPoint,
                                                     adsk.fusion.DimensionOrientations.VerticalDimensionOrientation,
                                                     adsk.core.Point3D.create(width, 0, 0));     
    h.parameter.expression = 'BSOy'  

def tAddRect(sketches, plane, center, length_params):
    # Reference the width and height user parameters values
    app = adsk.core.Application.get()
    product = app.activeProduct
    design = adsk.fusion.Design.cast(product)
    userParams = design.userParameters
    width = userParams.itemByName('BSTx').value
    height = userParams.itemByName('BSTy').value

    # Create the rectangle
    sketch = sketches.add(plane)
    sketchlines = sketch.sketchCurves.sketchLines;  
    p1 = adsk.core.Point3D.create(-width / 2, -height / 2, 0)
    p2 = adsk.core.Point3D.create(width / 2, height / 2, 0)
    recLines = sketchlines.addTwoPointRectangle(p1, p2)

    # Constrain the Rectangle to stay rectangular
    constraints = sketch.geometricConstraints
    constraints.addPerpendicular(recLines.item(0), recLines.item(1))
    constraints.addHorizontal(recLines.item(0))    
    constraints.addParallel(recLines.item(0), recLines.item(2))
    constraints.addParallel(recLines.item(1), recLines.item(3))

    # Add construction lines to constrain the rectangle to the center point
    lines = sketch.sketchCurves.sketchLines
    diagonal1 = lines.addByTwoPoints(recLines.item(0).startSketchPoint, recLines.item(2).startSketchPoint)  
    diagonal1.isConstruction = True
    #diagonal1.isVisible = False
    diagonal2 = lines.addByTwoPoints(recLines.item(1).startSketchPoint, recLines.item(3).startSketchPoint)    
    diagonal2.isConstruction = True
    #diagonal2.isVisible = False
  
    # Constrain the rectangle to be centered on the center point
    sketchpoints = sketch.sketchPoints; 
    sk_center = sketchpoints.add(center)
    sk_center.isfixed = True
    constraints.addCoincident(sk_center, diagonal1)
    constraints.addCoincident(sk_center, diagonal2)
    constraints.addCoincident(sk_center, sketch.originPoint)

    # Dimension the rectangle with the user parameters
    w = sketch.sketchDimensions.addDistanceDimension(recLines.item(0).startSketchPoint, recLines.item(0).endSketchPoint,
                                                     adsk.fusion.DimensionOrientations.HorizontalDimensionOrientation,
                                                     adsk.core.Point3D.create(0, height, 0)); 
    w.parameter.expression = 'BSTx'    
    h = sketch.sketchDimensions.addDistanceDimension(recLines.item(1).startSketchPoint, recLines.item(1).endSketchPoint,
                                                     adsk.fusion.DimensionOrientations.VerticalDimensionOrientation,
                                                     adsk.core.Point3D.create(width, 0, 0));     
    h.parameter.expression = 'BSTy'  

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 create loft', 'Edges,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)
        
        # create planes at each end
        planes = comp.constructionPlanes
        planeInput = planes.createInput()
        planeInput.setByDistanceOnPath(selObj, adsk.core.ValueInput.createByReal(0))
        plane1 = planes.add(planeInput)
        planeInput.setByDistanceOnPath(selObj, adsk.core.ValueInput.createByReal(1))
        plane2 = planes.add(planeInput)
        
        #create sketch for origin
        sketches = comp.sketches;
        center = adsk.core.Point3D.create(0, 0, 0)
        oAddRect(sketches, plane1, center, ['BSOx', 'BSOy'])

        

        #create sketch for termination
        sketches = comp.sketches;
        center = adsk.core.Point3D.create(0, 0, 0)
        tAddRect(sketches, plane2, center, ['BSTx', 'BSTy'])


        #Create Loft
        loft1 = comp.features.loftFeatures
        loftInput = loft1.createInput(adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
        loftSectionsObj = loftInput.loftSections
        loftSectionsObj.add(profile1)
        loftSectionsObj.add(profile2)
        loftPath = loftInput.centerLineOrRails
        loftPath.addCenterLine(path)
        loftInput.isSolid = True

        # Create loft feature
        loft1.add(loftInput)


        app.activeViewport.refresh()

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

 

Labels (3)
1 REPLY 1
Message 2 of 2
peter6G3RQ
in reply to: peter6G3RQ

Managed to resolve it with the following:

 

 

        #create sketch for taper and termination
        sketches = comp.sketches;
        center = adsk.core.Point3D.create(0, 0, 0)
        tAddRect(sketches, plane2, center, ['BSTx', 'BSTy'])
        #ui.messageBox(str(sketches.count))
        cnt = sketches.count
        tSk = sketches.item(cnt -1)
        tPro = tSk.profiles[0]

 

 

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