Help with user input in a script

Help with user input in a script

kevn229
Participant Participant
418 Views
2 Replies
Message 1 of 3

Help with user input in a script

kevn229
Participant
Participant

I want to get user input than use that input to specify extrude distance in a script.  If you could change this script, please do, i'm thinking i can then use it as an example.  Thank you in advance.  See below.

 

#Author:  
#Extrudes closed object of lines
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
       
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)

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


        # Create a new sketch on the xz plane (Front plane).
        sketches = rootComp.sketches;
        xzPlane = rootComp.xZConstructionPlane
        sketch = sketches.add(xzPlane)

        # Draw two connected lines.
        lines = sketch.sketchCurves.sketchLines;
        line1 = lines.addByTwoPoints(adsk.core.Point3D.create(0, 0, 0), adsk.core.Point3D.create(0, 7.62, 0))
        line2 = lines.addByTwoPoints(line1.endSketchPoint, adsk.core.Point3D.create(-.635, 7.62, 0))
        line3 = lines.addByTwoPoints(line2.endSketchPoint, adsk.core.Point3D.create(-.635, .635, 0))
        line4 = lines.addByTwoPoints(line3.endSketchPoint, adsk.core.Point3D.create(-5.08, .635, 0))
        line5 = lines.addByTwoPoints(line4.endSketchPoint, adsk.core.Point3D.create(-5.08, 0, 0))
        line6 = lines.addByTwoPoints(line5.endSketchPoint, adsk.core.Point3D.create(0, 0, 0))

   

        # Get the profile defined by the sketch. FOR EXTRUDE
        prof = sketch.profiles.item(0)

        # Create an extrusion input
        extrudes = rootComp.features.extrudeFeatures
        extInput = extrudes.createInput(prof, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
       
        # Define Extrude distance.
        distance = adsk.core.ValueInput.createByReal(30.48)
        extInput.setDistanceExtent(False, distance)

        # Create the extrusion.
        ext = extrudes.add(extInput)



    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
0 Likes
Accepted solutions (1)
419 Views
2 Replies
Replies (2)
Message 2 of 3

kandennti
Mentor
Mentor
Accepted solution

Hi @kevn229 .

 

The inputBox can now be used to input the length of the extrusion.

# Fusion360API Python script

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

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

        # query(
        extrusionLength = query()
        if math.isnan(extrusionLength): return

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

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

        # Create a new sketch on the xz plane (Front plane).
        sketches = rootComp.sketches;
        xzPlane = rootComp.xZConstructionPlane
        sketch = sketches.add(xzPlane)

        # Draw two connected lines.
        lines = sketch.sketchCurves.sketchLines;
        line1 = lines.addByTwoPoints(adsk.core.Point3D.create(0, 0, 0), adsk.core.Point3D.create(0, 7.62, 0))
        line2 = lines.addByTwoPoints(line1.endSketchPoint, adsk.core.Point3D.create(-.635, 7.62, 0))
        line3 = lines.addByTwoPoints(line2.endSketchPoint, adsk.core.Point3D.create(-.635, .635, 0))
        line4 = lines.addByTwoPoints(line3.endSketchPoint, adsk.core.Point3D.create(-5.08, .635, 0))
        line5 = lines.addByTwoPoints(line4.endSketchPoint, adsk.core.Point3D.create(-5.08, 0, 0))
        line6 = lines.addByTwoPoints(line5.endSketchPoint, adsk.core.Point3D.create(0, 0, 0))

   
        # Get the profile defined by the sketch. FOR EXTRUDE
        prof = sketch.profiles.item(0)

        # Create an extrusion input
        extrudes = rootComp.features.extrudeFeatures
        extInput = extrudes.createInput(prof, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
       
        # Define Extrude distance.
        # distance = adsk.core.ValueInput.createByReal(30.48)
        distance = adsk.core.ValueInput.createByReal(extrusionLength)
        extInput.setDistanceExtent(False, distance)

        # Create the extrusion.
        ext = extrudes.add(extInput)

        # fit
        app.activeViewport.fit()

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


def query() -> float:
    # *******
    def is_float(txt) -> bool:
        try:
            float(txt)
            return True
        except:
            return False
    # *******

    app: adsk.core.Application = adsk.core.Application.get()
    ui: adsk.core.UserInterface = app.userInterface
    des: adsk.fusion.Design = app.activeProduct
    unitsMgr: adsk.core.UnitsManager = des.unitsManager

    lengthRatio = unitsMgr.convert(
        1,
        unitsMgr.internalUnits,
        unitsMgr.defaultLengthUnits,
    )
    defaultValue = 30.48 * lengthRatio

    msg = 'Enter the length of the extrusion ({})'.format(
        unitsMgr.defaultLengthUnits
    )

    res, isCancel = ui.inputBox(
        msg,
        '',
        f'{defaultValue}',
    )

    if isCancel: return math.nan
    if not is_float(res): return math.nan

    value = float(res)
    if value == 0: return math.nan

    return value / lengthRatio
Message 3 of 3

kevn229
Participant
Participant

Works perfectly.  Thank you for your time and the fast reply.  You provide an awesome service.

 

Kevin

0 Likes