Select Multiple Profiles from UI and add to ObjectCollection

Select Multiple Profiles from UI and add to ObjectCollection

richop88
Participant Participant
807 Views
2 Replies
Message 1 of 3

Select Multiple Profiles from UI and add to ObjectCollection

richop88
Participant
Participant

Hello again,

 

I think I'm close, and massively miles away at the same time.  I have successfully extruded one profile from UI; now I want to extrude multiple, based on user selection, add to collection, and iterate to extrude by different amounts each time, like steps. 

 

Here's what I've got thus far, any help would be great:

 

import adsk.core, adsk.fusion, traceback

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

        design  :adsk.fusion.Design = app.activeProduct
        rootComp :adsk.fusion.Component = design.rootComponent
        # Get extrude features
        extrudes = rootComp.features.extrudeFeatures
        # Asks for amount of Profiles
        (Amount) = ui.inputBox('Enter Profile Amount', 'Profile Amount', '12')
        # Create an object collection to use an input.
        profs = adsk.core.ObjectCollection.create()

        profs.Length = ('12') # Do I have to set array lenght in python??
        # Asks for first Profile
        faceSel = ui.selectEntity('Select a profile', 'Profiles')
        if faceSel:
            face = adsk.fusion.Profiles.cast(faceSel.entity)

        #Need to add multiple profiles, specified by profile Amount

        #Psudo code: For x in profs, add profile entity
            # Get the extrusion Amount.
            (diam, cancelled) = ui.inputBox('Enter extrusion Amount', 'Extrusion Amount', '100.0 mm')
            if cancelled:
                return

        prof = faceSel.entity

        # Add all of the profiles to the collection.
        #for prof in faceSel:
        #    profs.add(prof)

        # Create extrude                                                     _|
        #                                                                  _| |
        #Psudocode for each profile in profs add extrusion amount plus x _| | |
        #                                                              _| | | |
        # From Extrude Samples: A simple way of creating typical extrusions (extrusion that goes from the profile plane the specified distance).
        # Define a distance extent of 5 cm
        distance = adsk.core.ValueInput.createByReal(5)
        extrude1 = extrudes.addSimple(prof, distance, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
        # Get the extrusion body
        body1 = extrude1.bodies.item(0)
        body1.name = "simple"

        # Get the state of the extrusion
        health = extrude1.healthState
        if health == adsk.fusion.FeatureHealthStates.WarningFeatureHealthState or health == adsk.fusion.FeatureHealthStates.ErrorFeatureHealthState:
            message = extrude1.errorOrWarningMessage

        # Get the state of timeline object
        timeline = design.timeline
        timelineObj = timeline.item(timeline.count - 1);
        health = timelineObj.healthState
        message = timelineObj.errorOrWarningMessage


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

richop88
Participant
Participant

ok, can't edit original comment with updated code, but think I'm freaking close now, the selection of the profiles aren't going red to signify being selected anymore though:

 

import adsk.core, adsk.fusion, traceback
import math
def run(context):
    try:
        global app, ui
        app = adsk.core.Application.get()
        ui = app.userInterface

        design  :adsk.fusion.Design = app.activeProduct
        rootComp :adsk.fusion.Component = design.rootComponent
        # Get extrude features
        extrudes = rootComp.features.extrudeFeatures

        # Asks for amount of Profiles
        (amt, cancelled) = ui.inputBox('Enter Profile Amount', 'Profile Amount', '6')
        if cancelled:
            return
        # Create an object collection to use an input.
        profs = adsk.core.ObjectCollection.create()
        # Get the extrusion Amount.
        (extrudeAmount, cancelled) = ui.inputBox('Enter extrusion Amount', 'Extrusion Amount', '100.00mm')
        if cancelled:
            return
        #profs.Length = ('12') # Do I have to set array lenght in python??
        # Asks for first Profile
        for x in range(int(amt)):
            faceSel = ui.selectEntity('Select a profile', 'Profiles')
            if faceSel:
                face = adsk.fusion.Profiles.cast(faceSel.entity)
                profs.add(faceSel.entity)
                if cancelled:
                    return
                    #Need to add multiple profiles, specified by profile Amount
                    #Psudo code: For x in profs, add profile entity

        prof = faceSel.entity

        # Add all of the profiles to the collection.
        #for prof in faceSel:
        #    profs.add(prof)

        # Create extrude                                                     _|
        #                                                                  _| |
        #Psudocode for each profile in profs add extrusion amount plus x _| | |
        #                                                              _| | | |
        # From Extrude Samples: A simple way of creating typical extrusions (extrusion that goes from the profile plane the specified distance).
        # Define a distance extent of 5 cm

        for x in range(int(amt)):
            #distance = adsk.core.ValueInput.createByReal(100)#works
            distance = adsk.core.ValueInput.createByReal(float(extrudeAmount + (x+1))) # Put Function of X here
            extrude1 = extrudes.addSimple(prof, distance, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
            # Get the extrusion body
            #Need to Cycle Profs, don\t know if it's like other languages and can just cycle through
            #using braces? profs[x] maybe?

            body1 = extrude1.bodies.item(0)
            body1.name = "simple"

            # Get the state of the extrusion
            health = extrude1.healthState
            if health == adsk.fusion.FeatureHealthStates.WarningFeatureHealthState or health == adsk.fusion.FeatureHealthStates.ErrorFeatureHealthState:
                message = extrude1.errorOrWarningMessage

                # Get the state of timeline object
                timeline = design.timeline
                timelineObj = timeline.item(timeline.count - 1);
                health = timelineObj.healthState
                message = timelineObj.errorOrWarningMessage


    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
0 Likes
Message 3 of 3

richop88
Participant
Participant

Alright think I've done it, super easy step sample. I've come across a Stair making or Step Making script on this forum and couldn't just copy and paste the code, even when reconstructing the answers. So here's my small contribution to Fusion360 script eco-system... Enjoy/Hack/Employ me if you really want to. 

 

# Fusion360API Python script
# SketchCurve Extrude Multiple Steps
# Author: (Arc) Richard Carrigan 2020 richop88 at gmail dot com
# Portfolio:https://www.behance.net/hoppingaround

import math def run(context): try: global app, ui app = adsk.core.Application.get() ui = app.userInterface design :adsk.fusion.Design = app.activeProduct rootComp :adsk.fusion.Component = design.rootComponent # Get extrude features extrudes = rootComp.features.extrudeFeatures # Asks for amount of Profiles (amt, cancelled) = ui.inputBox('Enter Profile Amount', 'Profile Amount', '6') if cancelled: return # Create an object collection to use an input. profs = adsk.core.ObjectCollection.create() # Get the extrusion Amount. (extrudeAmount, cancelled) = ui.inputBox('Enter extrusion Amount', 'Extrusion Amount', '10.00') if cancelled: return #profs.Length = ('12') # Do I have to set array lenght in python?? # Asks for first Profile for x in range(int(amt)): faceSel = ui.selectEntity('Select a profile', 'Profiles') if faceSel: face = adsk.fusion.Profiles.cast(faceSel.entity) profs.add(faceSel.entity) if cancelled: return #Need to add multiple profiles, specified by profile Amount #Psudo code: For x in profs, add profile entity prof = faceSel.entity # Add all of the profiles to the collection. #for prof in faceSel: # profs.add(prof) # Create extrude _| # _| | #Psudocode for each profile in profs add extrusion amount plus x _| | | # _| | | | # From Extrude Samples: A simple way of creating typical extrusions (extrusion that goes from the profile plane the specified distance). # Define a distance extent of 5 cm for x in range(int(amt)): #distance = adsk.core.ValueInput.createByReal(100)#works distance = adsk.core.ValueInput.createByReal(float(extrudeAmount)+(x+1.0)) # Put Function of X here extrude1 = extrudes.addSimple(profs[x], distance, adsk.fusion.FeatureOperations.NewBodyFeatureOperation) # Get the extrusion body #Need to Cycle Profs, don\t know if it's like other languages and can just cycle through #using braces? profs[x] maybe? body1 = extrude1.bodies.item(0) body1.name = "simple" # Get the state of the extrusion health = extrude1.healthState if health == adsk.fusion.FeatureHealthStates.WarningFeatureHealthState or health == adsk.fusion.FeatureHealthStates.ErrorFeatureHealthState: message = extrude1.errorOrWarningMessage # Get the state of timeline object timeline = design.timeline timelineObj = timeline.item(timeline.count - 1); health = timelineObj.healthState message = timelineObj.errorOrWarningMessage except: if ui: ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))