Importing STEP files and moving to certain position, Invalid Entity?

Importing STEP files and moving to certain position, Invalid Entity?

joshua_t_badger
Explorer Explorer
293 Views
3 Replies
Message 1 of 4

Importing STEP files and moving to certain position, Invalid Entity?

joshua_t_badger
Explorer
Explorer

So I'm trying to automate importing STEP files and trying to move them to certain coordinates. I am able to get the .step files imported and get to the BRepBody instances that come in. I move them over to the root composition's bodies.

 

Here's the code where problems start occurring.

    def reposition_pixel(self, coords, body):
        app.log(f"{body.isValid=}, {body.objectType=}, {(body.parentComponent == root_comp)=}")
        # Create a Move feature to move the body
        object_collection = adsk.core.ObjectCollection.create()
        app.log(f"{object_collection.isValid=}, {object_collection.count=}")
        object_collection.add(body)
        app.log(f"{object_collection.isValid=}, {object_collection.count=}")

        origin_point = body.boundingBox.minPoint
        destination_coord = [c * POS_MULT for c in coords]

        # y came first, then x, because of how they were accessed
        destination_point = adsk.core.Point3D.create(
            destination_coord[1] * POS_MULT,
            destination_coord[0] * POS_MULT * -1,
            0,
        )
        move_input = root_comp.features.moveFeatures.createInput2(object_collection)
        app.log(f"{move_input.isValid=}")
        app.log(f"{origin_point.objectType=} {origin_point.isValid=}")
        app.log(f"{destination_point.objectType=} {destination_point.isValid=}")

        move_input.defineAsPointToPoint(  # this is where the problem occurs
            origin_point, destination_point,
        )
        root_comp.features.moveFeatures.add(move_input)

I get various log messages (something about my debugger is not working right now, so I'm using good ol' print debugging), and then I get a traceback.

 body.isValid=True, body.objectType='adsk::fusion::BRepBody', (body.parentComponent == root_comp)=True
 object_collection.isValid=False, object_collection.count=0
 object_collection.isValid=False, object_collection.count=1
 coord=(0, 6)
 move_input.isValid=True
 origin_point.objectType='adsk::core::Point3D' origin_point.isValid=True
 destination_point.objectType='adsk::core::Point3D' destination_point.isValid=True
 Failed:
Traceback (most recent call last):
  File "/Users/username/Library/Application Support/Autodesk/Autodesk Fusion 360/API/Scripts/Thing Creator/Thing Creator.py", line 40, in run
    builder.build_thing_from_json_data()
  File "/Users/username/Library/Application Support/Autodesk/Autodesk Fusion 360/API/Scripts/Thing Creator/Thing Creator.py", line 79, in build_thing_from_json_data
    self.reposition_pixel(pixel_info, pixel)
  File "/Users/username/Library/Application Support/Autodesk/Autodesk Fusion 360/API/Scripts/Thing Creator/Thing Creator.py", line 134, in reposition_pixel
    move_input.defineAsPointToPoint(
  File "/Users/username/Library/Application Support/Autodesk/webdeploy/production/bd07983cd9e69cddc8d92b9be6507c3dbbec9409/Autodesk Fusion.app/Contents/Api/Python/packages/adsk/fusion.py", line 36928, in defineAsPointToPoint
    return _fusion.MoveFeatureInput_defineAsPointToPoint(self, originPoint, targetPoint)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: 3 : Invalid entity

I can see that everything but the object collection is valid, and I have NO idea why not.

 

Is there a simpler way to just move a body from one defined point to another?

0 Likes
294 Views
3 Replies
Replies (3)
Message 2 of 4

BrianEkins
Mentor
Mentor

The problem is that the point-to-point move wants to create a parametric result. Because of that, the two input points need to be an entity that defines a point. For example, a construction point, B-Rep vertex, or sketch point. Then, if a change is made to the model that causes that point to move, the body will update and move.

 

Instead, you can use the "Free Move" option of the move feature. This isn't parametric and lets you do any move or rotation. Here's a modified version of your program that uses this. (FYI, I'm just typing the code here and haven't tested it.)

    def reposition_pixel(self, coords, body):
        app.log(f"{body.isValid=}, {body.objectType=}, {(body.parentComponent == root_comp)=}")
        # Create a Move feature to move the body
        object_collection = adsk.core.ObjectCollection.create()
        app.log(f"{object_collection.isValid=}, {object_collection.count=}")
        object_collection.add(body)
        app.log(f"{object_collection.isValid=}, {object_collection.count=}")

        origin_point = body.boundingBox.minPoint
        destination_coord = [c * POS_MULT for c in coords]

        # y came first, then x, because of how they were accessed
        destination_point = adsk.core.Point3D.create(
            destination_coord[1] * POS_MULT,
            destination_coord[0] * POS_MULT * -1,
            0,
        )
        move_input = root_comp.features.moveFeatures.createInput2(object_collection)
        app.log(f"{move_input.isValid=}")
        app.log(f"{origin_point.objectType=} {origin_point.isValid=}")
        app.log(f"{destination_point.objectType=} {destination_point.isValid=}")

        matrix = adsk.core.Matrix3D.create()
        matrix.translation = origin_point.vectorTo(destination_point)
        move_input.defineAsFreeMove(matrix)
        root_comp.features.moveFeatures.add(move_input)

 

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes
Message 3 of 4

BrianEkins
Mentor
Mentor

I would also consider having each STEP import be a separate component instead of having them all in the root component. If you do that, you can accomplish the same result by setting the transformation property of the occurrence representing that component.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes
Message 4 of 4

joshua_t_badger
Explorer
Explorer

Your suggestion for FreeMove worked out of the box, no code changes needed. 🙂 Thank you!

 

One last question: there's not a good way to programatically create groups in Bodies to group certain objects together, is there?

0 Likes