Transformation on occurrence getting reset?

Transformation on occurrence getting reset?

JesusFreke
Advocate Advocate
1,832 Views
4 Replies
Message 1 of 5

Transformation on occurrence getting reset?

JesusFreke
Advocate
Advocate

I've been experimenting with the API a bit, and have run into a strange problem.

 

I've created a minimally reproducible example here: https://hastebin.com/dataxefizi.py

 

It creates a sphere as a new component, and then sets the component's occurrance's transform.

 

And then, it creates another sphere as another new component, which seems to reset the transformation on the first sphere.

 

The first print on line 34 prints "-5.0", while the second print on line 37 prints "0.0".

 

Why is the transformation on the first component's occurrence getting reset in this case?

 

0 Likes
Accepted solutions (1)
1,833 Views
4 Replies
Replies (4)
Message 2 of 5

masounmardini
Contributor
Contributor

The problem is the last line of sphere def

you refer to item 0

0 Likes
Message 3 of 5

BrianEkins
Mentor
Mentor
Accepted solution

My guess is that the position of the component is being reset.  It's the same as in the UI when you see this dialog, except with the API, it's not displaying the dialog and is resetting everything to its previous position.

 

MovedComponent.png

 

Before creating the next occurrence, you can add the code below, which will capture the current position in a snapshot and then it won't move back. "des" is a reference to the currently active Design.

 

des.snapshots.add()
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 4 of 5

JesusFreke
Advocate
Advocate

@masounmardini wrote:

The problem is the last line of sphere def

you refer to item 0


Thanks for taking a look! I don't think that's it though. Since the revolve feature is using 

NewComponentFeatureOperation, feature.parentComponent should be a new, separate component, and root.allOccurrencesByComponent should return a list with a single item, containing the single occurrance of the new component. I added some logging to my previous example and confirmed that this was the case.

0 Likes
Message 5 of 5

JesusFreke
Advocate
Advocate

BrianEkins, that did the trick, thanks!

 

Here's the updated, working example, for posterity 🙂

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

app = adsk.core.Application.get()

root = adsk.fusion.Design.cast(app.activeProduct).rootComponent

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


def sphere(radius) -> adsk.fusion.Occurrence:
    sketch = root.sketches.add(root.xYConstructionPlane)

    center_point = adsk.core.Point3D.create(0, 0, 0)
    start_point = adsk.core.Point3D.create(radius/10.0, 0, 0)
    arc = sketch.sketchCurves.sketchArcs.addByCenterStartSweep(center_point, start_point, math.pi)
    sketch.sketchCurves.sketchLines.addByTwoPoints(arc.startSketchPoint, arc.endSketchPoint)

    revolves = root.features.revolveFeatures
    revolve_input = revolves.createInput(sketch.profiles.item(0), root.xConstructionAxis,
                                         adsk.fusion.FeatureOperations.NewComponentFeatureOperation)

    angle = adsk.core.ValueInput.createByReal(math.pi)
    revolve_input.isSolid = True
    revolve_input.setAngleExtent(True, angle)

    feature = revolves.add(revolve_input)  # type: adsk.fusion.Feature

    return root.allOccurrencesByComponent(feature.parentComponent)[0]

def run(context):
    ui = None
    try:
        first = sphere(10)
        transform = first.transform
        transform.translation = adsk.core.Vector3D.create(-5, 0, 0)
        first.transform = transform
        design.snapshots.add()
        print(first.transform.translation.x)

        second = sphere(10)
        print(first.transform.translation.x)


    except:
        print(traceback.format_exc())