The "execute()" function is ignored

The "execute()" function is ignored

SaeedHamza
Advisor Advisor
557 Views
3 Replies
Message 1 of 4

The "execute()" function is ignored

SaeedHamza
Advisor
Advisor

Hi,

 

I'm trying to make a script to scale a body using a custom axis.

The idea is to align the body to one of the 3 axes, scale it and then return it to its proper position.

Now since the align tool isn't accessible through the API object model, I used this method below that I saw @BrianEkins used in this post : API access to Align Components Function - Autodesk Community - Fusion 360

 

        ui.activeSelections.clear
        ui.activeSelections.add(alignment_edge)
        ui.activeSelections.add(rootComp.xConstructionAxis)
       
        alignCommand = ui.commandDefinitions.itemById('AlignGeometryCmd')
        alignCommand.execute()
 
It works like a charm when it's alone, but the moment I make something else it gets ignored, for example, when I try to scale after the alignment, the script only applies the scale in the original position of the body, without the alignment even showing in the timeline ...
I even tried to just create a sketch on the xy plane from the root component, as a completely separate action after the alignment, and the same thing happened, the sketch was created but the alignment was ignored.
 
Moreover, I even tried the "transaction.Commit" after the alignment to force Fusion to apply it, but no use ...
 
Any ideas why this is happening???
Thanks in advance.
 
Here is my code :
import tempfile
import adsk.core, adsk.fusion, adsk.cam, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        
        design = adsk.fusion.Design.cast(app.activeProduct)

        rootComp = design.rootComponent
        features = rootComp.features
        scales = features.scaleFeatures

        alignment_edge_sele = ui.selectEntity('Select BRepEdge entities of any shape', 'Edges')
        alignment_edge = alignment_edge_sele.entity

        ui.activeSelections.clear
        ui.activeSelections.add(alignment_edge)
        ui.activeSelections.add(rootComp.xConstructionAxis)

        app.executeTextCommand('Transaction.Start Saeed')

        alignCommand = ui.commandDefinitions.itemById('AlignGeometryCmd')
        alignCommand.execute()

        app.executeTextCommand('Transaction.Commit')

        xScale = adsk.core.ValueInput.createByReal(0.5)
        yScale = adsk.core.ValueInput.createByReal(1.00)
        zScale = adsk.core.ValueInput.createByReal(1.00)

        point = alignment_edge.startVertex
        objcol = adsk.core.ObjectCollection.create()
        objcol.add(alignment_edge.body)
        factor = adsk.core.ValueInput.createByReal(1.00)
        scale_input = scales.createInput(objcol, point, factor)
        scale_input.setToNonUniform(xScale, yScale, zScale)

        scales.add(scale_input)

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

Saeed Hamza
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature

0 Likes
Accepted solutions (1)
558 Views
3 Replies
Replies (3)
Message 2 of 4

kandennti
Mentor
Mentor
Accepted solution

Hi @SaeedHamza .

 

Since the commandDefinition.execute method is processed together at the end of the script, it will not be executed at the intended timing even if you try to execute it in the middle of the script.

 

Instead, this would give the desired result.

・・・
        ui.activeSelections.add(rootComp.xConstructionAxis)

        # app.executeTextCommand('Transaction.Start Saeed')

        # alignCommand = ui.commandDefinitions.itemById('AlignGeometryCmd')
        # alignCommand.execute()
        app.executeTextCommand('Commands.Start AlignGeometryCmd')

        # app.executeTextCommand('Transaction.Commit')

        xScale = adsk.core.ValueInput.createByReal(0.5)
・・・
Message 3 of 4

SaeedHamza
Advisor
Advisor

thank you @kandennti, this is exactly what I needed.

 

But does this mean that it has to be through using text commands when executing a native F360 command?

Saeed Hamza
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature

0 Likes
Message 4 of 4

SaeedHamza
Advisor
Advisor

I finished the code, so I thought might as well share it

Thanks for the help @kandennti 

 

One thing that annoyed me is that the script needs 2 selections, a reference (stationary) point, and an edge as the custom axis, and when the point selection comes after the edge selection, it won't be cleared ...

So I guess the last entity selected can't be cleared from the active selections, that's where the "activeSelections.clear" fails.

That was solved basically by making the script select the point first and then the edge.

 

import tempfile
import adsk.core, adsk.fusion, adsk.cam, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        
        design = adsk.fusion.Design.cast(app.activeProduct)

        rootComp = design.rootComponent
        features = rootComp.features
        scales = features.scaleFeatures
        sketches = rootComp.sketches
        planes = rootComp.constructionPlanes

        ref_vert_sele = ui.selectEntity('Select BRepVertex entities', 'Vertices')
        ref_vert = ref_vert_sele.entity

        alignment_edge_sele = ui.selectEntity('Select BRepEdge entities of any shape', 'Edges')
        alignment_edge = alignment_edge_sele.entity

        pnt1 = ref_vert.geometry

        body = ref_vert.body
        vertices = body.vertices
        for vertex in vertices :
            if vertex.geometry.distanceTo(pnt1) == 0.00 :
                ref_id = vertex.tempId

        ref_face1 = alignment_edge.faces[0]
        plane_input = planes.createInput()
        offset = adsk.core.ValueInput.createByReal(0.00)
        plane_input.setByOffset(ref_face1, offset)
        ref_plane = planes.add(plane_input)

        ui.activeSelections.clear
        ui.activeSelections.add(alignment_edge)
        ui.activeSelections.add(rootComp.xConstructionAxis)

        for i in range(0,2000,1) :
            print(ui.commandDefinitions.item(i).id)


        ui.activeSelections.add(alignment_edge)
        ui.activeSelections.add(rootComp.xConstructionAxis)

        app.executeTextCommand('Commands.Start AlignGeometryCmd')

        xScale = adsk.core.ValueInput.createByReal(0.5)
        yScale = adsk.core.ValueInput.createByReal(1.00)
        zScale = adsk.core.ValueInput.createByReal(1.00)

        point = alignment_edge.endVertex
        objcol = adsk.core.ObjectCollection.create()
        objcol.add(alignment_edge.body)
        factor = adsk.core.ValueInput.createByReal(1.00)
        scale_input = scales.createInput(objcol, point, factor)
        scale_input.setToNonUniform(xScale, yScale, zScale)

        scales.add(scale_input)

        ref_face2 = alignment_edge.faces[0]

        ui.activeSelections.clear
        ui.activeSelections.add(ref_plane)
        ui.activeSelections.add(ref_face2)

        app.executeTextCommand('Commands.Start AlignGeometryCmd')

        ref_plane.isLightBulbOn = False

        body = alignment_edge.body
        vertices = body.vertices
        for vertex in vertices :
            if vertex.tempId == ref_id :
                pnt2 = vertex.geometry

        trans: adsk.core.Matrix3D = adsk.core.Matrix3D.create()
        trans.translation = pnt2.vectorTo(pnt1)

        objcol = adsk.core.ObjectCollection.create()
        objcol.add(alignment_edge.body)

        moveFeats = features.moveFeatures
        moveFeatureInput = moveFeats.createInput(objcol, trans)
        moveFeats.add(moveFeatureInput)

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

 

Saeed Hamza
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature