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: 

Is it possible to Rotate a body by multiple angles as you can do in the Move UI

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
nnamfoh
1543 Views, 2 Replies

Is it possible to Rotate a body by multiple angles as you can do in the Move UI

I want to move an object by multiple angles, around all 3 axes (the design ones, not the axes of an other component). I have the code below but I get weird results because it keeps turning around the axes of the component not the RootComponent. This feature exists in the UI but is it available from the python API?

def rotateBody(design, ui, x,y, z, body, parentComponent):
    occurenceOfComp = design.rootComponent.allOccurrencesByComponent(parentComponent).item(0)     
    actualOrigin = parentComponent.originConstructionPoint.createForAssemblyContext(occurenceOfComp).geometry
    features = parentComponent.features
    moveBody = body
    if x<0.000009 and x>-0.000009:
        moveBody = body
    else:
        ui.messageBox("Moving X")        
        entities1 = adsk.core.ObjectCollection.create()
        entities1.add(moveBody)
        transform = adsk.core.Matrix3D.create()
        transform.setToRotation(x,design.rootComponent.xConstructionAxis.geometry.getData()[2], actualOrigin)
        #Create a move feature
        moveFeats = features.moveFeatures
        moveFeatureInput = moveFeats.createInput(entities1, transform)
        move =moveFeats.add(moveFeatureInput) 
        moveBody = move.bodies.item(0)
        if design.snapshots.hasPendingSnapshot:
            design.snapshots.add()
    if y<0.000009 and y>-0.000009:
        moveBody = body
    else: 
        ui.messageBox("Moving Y")
        entities1 = adsk.core.ObjectCollection.create()
        entities1.add(moveBody)
        transform = adsk.core.Matrix3D.create()
        transform.setToRotation(y,design.rootComponent.yConstructionAxis.geometry.getData()[2], actualOrigin)
        #Create a move feature
        moveFeats = features.moveFeatures
        moveFeatureInput = moveFeats.createInput(entities1, transform)
        move =moveFeats.add(moveFeatureInput) 
        moveBody = move.bodies.item(0)
        if design.snapshots.hasPendingSnapshot:
            design.snapshots.add()
    if z<0.000009 and z>-0.000009:
        moveBody = body
    else:     
        ui.messageBox("Moving Z")
        entities1 = adsk.core.ObjectCollection.create()
        entities1.add(moveBody)
        transform = adsk.core.Matrix3D.create()
        transform.setToRotation(z,design.rootComponent.zConstructionAxis.geometry.getData()[2], actualOrigin)
        #Create a move feature
        moveFeats = features.moveFeatures
        moveFeatureInput = moveFeats.createInput(entities1, transform)
        move =moveFeats.add(moveFeatureInput) 
        moveBody = move.bodies.item(0)
        if design.snapshots.hasPendingSnapshot:
            design.snapshots.add()

Capture.PNG

Tags (2)
2 REPLIES 2
Message 2 of 3
ekinsb
in reply to: nnamfoh

It is possible to get the equivalent result using the API to create a Move feature as you do in the UI.  One thing that's important to be aware of is what's actually happening in the UI command.  When you edit any of the fields it's in edit mode for that particular distance or angle change and whatever you enter is applied to the model.  When you edit a different value, you're then applying those changes to the already modified body.  For example, if you set the X angle to 45 deg Fusion will rotate the body around the X-axis of the triad and then if you set the Y angle to 45 deg Fusion will rotation the body around the Y-axis of the triad.  Now if you set the X angle to 15 deg, Fusion will rotate the body around the X-axis an additional 15 degrees.  You're not changing the 45 deg rotation to 15 but applying an additional 15 deg rotation.  So, changing the values in the dialog is applying a series of sequential transforms and when you click ok you get the final result.

 

One way to easily see this is to specify 45 as the X angle and then 45 as the Y angle and click OK.  Look at and remember the result.  Now undo that and run the Move feature again but this time specify 45 as the Y angle and then 45 as the X angle and click OK.  You'll get a different result because the angles were applied in a different order.

 

In the API you define the distances and angles through a matrix.  I don't know how familiar you are with transformation matrices but they're commonly used in computer graphics.  Using matrix functionality you can build up a matrix using multiple rotations and then use that to create a Move feature.  The code below demonstrates this by rotating 45 degrees around the model X axis and then 45 degrees around the model Y axis.

 

def multipleRotations():
    ui = None
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface
        
        body = adsk.fusion.BRepBody.cast(ui.selectEntity('Select a body', 'Bodies').entity)
        if body:
            trans = adsk.core.Matrix3D.create()
            
            rotX = adsk.core.Matrix3D.create()
            rotX.setToRotation(math.pi/4, adsk.core.Vector3D.create(1,0,0), adsk.core.Point3D.create(0,0,0))
            trans.transformBy(rotX)
            
            rotY = adsk.core.Matrix3D.create()
            rotY.setToRotation(math.pi/4, adsk.core.Vector3D.create(0,1,0), adsk.core.Point3D.create(0,0,0))
            trans.transformBy(rotY)
            
            des = adsk.fusion.Design.cast(app.activeProduct)
            root = des.rootComponent
            
            ents = adsk.core.ObjectCollection.create()
            ents.add(body)
            moveInput = root.features.moveFeatures.createInput(ents, trans)
            root.features.moveFeatures.add(moveInput)
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
    

Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 3 of 3
nnamfoh
in reply to: ekinsb

I see, that is problematic for my purposes. I'm trying to calculate the angles needed to align to objects within a set of angular bounds. Unfortunatly this means I can't garuntee the order in which users do the rotation themselves and what results they will get. Oh well, I can use this code to create a ghost that is valid under these bounds.

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