Help on executing an API custom command

Help on executing an API custom command

2way3dtext
Contributor Contributor
745 Views
2 Replies
Message 1 of 3

Help on executing an API custom command

2way3dtext
Contributor
Contributor

The following code asks the user to select the desired path and diameter of the pipe, however, nothing occurs after all the inputs are selected.

 

Would this be an issue with how the inputs are defined?

 

# Select all of the inputs.

um = des.unitsManager

 

path_Input = inputs.addSelectionInput('path_Input', 'Path', 'Select path of pipe')

path_Input.setSelectionLimits(1,1)

 

pipeDiameter_input = inputs.addValueInput('pipeDiameter_Input', 'Diameter', um.defaultLengthUnits, adsk.core.ValueInput.createByString('1'))

 

# Event handler for the execute event.

class RoundedEndPipeCommandExecuteHandler(adsk.core.CommandEventHandler):

    def __init__(self):

        super().__init__()

    def notify(self, args):

        eventArgs = adsk.core.CommandEventArgs.cast(args)

        inputs = eventArgs.command.commandInputs

 

        # Get the values of all of the inputs.

        path = inputs.itemById('path_Input').selection(0).entity

        pipeDiameter = inputs.itemById('pipeDiameter_Input').value

 

        CreateRoundedEndPipe(path, pipeDiameter)     

 

 

I will be very grateful for any suggestions or feedback on fixing this issue.

0 Likes
Accepted solutions (2)
746 Views
2 Replies
Replies (2)
Message 2 of 3

kandennti
Mentor
Mentor
Accepted solution

Hi @2way3dtext .

 

There are many things going on, but the yellow part is an error because it is None.
In addition, when I reached the green part "except:", the error message was not displayed because ui was also None.

1.png

Also, when I use UserInterface.selectEntity with the command dialog dialogs displayed, it crashes. (red part)
It has been reported somewhere in the forum that this method is undesirable.

 

 

I can't explain it in detail, but I've got it working.
Please compare.

・・・

#Create Rounded End Pipe
def CreateRoundedEndPipe(path, pipeDiameter) :
    try:
        pipeRadius =pipeDiameter/2

        product = _app.activeProduct
        design = adsk.fusion.Design.cast(product)
        
        comp = design.rootComponent
        selObj: adsk.fusion.BRepEdge = path

        # 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 profile_1
        planes = comp.constructionPlanes
        planeInput_1 = planes.createInput()
        planeInput_1.setByDistanceOnPath(selObj, adsk.core.ValueInput.createByReal(0))
        plane_1 = planes.add(planeInput_1)
        
        sketches = comp.sketches
        sketch_1 = sketches.add(plane_1)
        
        center_1 = plane_1.geometry.origin
        center_1 = sketch_1.modelToSketchSpace(center_1)
        sketch_1.sketchCurves.sketchCircles.addByCenterRadius(center_1, pipeRadius)
        profile_1 = sketch_1.profiles[0]

        # create profile_2
        planeInput_2 = planes.createInput()
        planeInput_2.setByDistanceOnPath(selObj, adsk.core.ValueInput.createByReal(1))
        plane_2 = planes.add(planeInput_2)

        sketches_2 = comp.sketches
        sketch_2 = sketches_2.add(plane_2)

        center_2 = plane_2.geometry.origin
        center_2 = sketch_2.modelToSketchSpace(center_2)
        circle_2 = sketch_2.sketchCurves.sketchCircles
        sketch_2.sketchCurves.sketchCircles.addByCenterRadius(center_2, pipeRadius)

        sk_2 = comp.sketches.add(plane_2)

        circles_2 = sk_2.sketchCurves.sketchCircles
        circle_2 = circles_2.addByCenterRadius(adsk.core.Point3D.create(0, 0, 0), pipeRadius)

        lines_2 = sk_2.sketchCurves.sketchLines
        axisLine_2 = lines_2.addByTwoPoints(adsk.core.Point3D.create(-pipeRadius, 0, 0), adsk.core.Point3D.create(pipeRadius, 0, 0))
        profile_2 = sk_2.profiles.item(0)
        
        # create profile_3
        sketches_3 = comp.sketches
        sketch_3 = sketches.add(plane_1)
        
        center_3 = plane_1.geometry.origin
        center_3 = sketch_1.modelToSketchSpace(center_1)
        sketch_3.sketchCurves.sketchCircles.addByCenterRadius(center_1, pipeRadius)
        profile_3 = sketch_1.profiles[0]  

        sk_3 = comp.sketches.add(plane_1)

        circles_3 = sk_3.sketchCurves.sketchCircles
        circle_3 = circles_3.addByCenterRadius(adsk.core.Point3D.create(0, 0, 0), pipeRadius)

        lines_3 = sk_3.sketchCurves.sketchLines
        axisLine_3 = lines_3.addByTwoPoints(adsk.core.Point3D.create(-pipeRadius, 0, 0), adsk.core.Point3D.create(pipeRadius, 0, 0))
        profile_3 = sk_3.profiles.item(0)      
        
        # Create an revolution input to be able to define the input needed for a revolution
        # while specifying the profile and that a new component is to be created
        revolves = comp.features.revolveFeatures
        revInput_2 = revolves.createInput(profile_2, axisLine_2, adsk.fusion.FeatureOperations.NewComponentFeatureOperation)
        revInput_3 = revolves.createInput(profile_3, axisLine_3, adsk.fusion.FeatureOperations.NewComponentFeatureOperation)

        # Define that the extent is an angle of 2*pi to get a sphere
        angle = adsk.core.ValueInput.createByReal(2*math.pi)
        revInput_2.setAngleExtent(False, angle)
        revInput_3.setAngleExtent(False, angle)

        # Create the extrusion.
        ext_2 = revolves.add(revInput_2)
        ext_2 = revolves.add(revInput_3)

        
        # create sweep
        sweepFeats = feats.sweepFeatures
        sweepInput = sweepFeats.createInput(profile_1, path, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
        sweepInput.orientation = adsk.fusion.SweepOrientationTypes.PerpendicularOrientationType
        sweepFeat = sweepFeats.add(sweepInput)
        
        _app.activeViewport.refresh()

    except:
        if _ui:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
・・・
# Event handler for the commandCreated event.
class MyCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
・・・
            path_Input = inputs.addSelectionInput('path_Input', 'Path', 'Select path of pipe')
            path_Input.setSelectionLimits(1,1)
            path_Input.addSelectionFilter('Edges')
            path_Input.addSelectionFilter('SketchCurves')
・・・
0 Likes
Message 3 of 3

2way3dtext
Contributor
Contributor
Accepted solution

Hello @kandennti

 

Thank you so much for looking over my code and pointing out the lines where the errors arise!

 

When I revised the code just as you have shown me, the add-in worked perfectly.

 

Thank you again for your generous time and help.