Rotate imported STEP file using API (fully scripted)

Anonymous

Rotate imported STEP file using API (fully scripted)

Anonymous
Not applicable

Hi,

I'm trying to import a .step file into a document, and then rotate it about the z-axis. I've successfully imported the file, but have been struggling to rotate it through the API by a fully automatically process.

 

The furthest I've gotten is to import the file, select the body I want to rotate using the 'ui.selectEntity()' command, and then the script finishes rotating it. However, I need a fully automatic script as I'll be doing this for many STP files and importing them and rotating them into various positions.

 

I have been able to do this for bodies that I've extruded using 'brepBody = ext.bodies.item(0)' as my bRepBody input, so I'm quite sure its not an issue with how I use the rotate tool. I think the issue is how the body of a .stp file (and possibly all non-fusion files?) is selected through the script.

I'm using Python, and my script is attached with Attempts left in as comments.

 

Thanks in advance,

Mike

0 Likes
Reply
Accepted solutions (1)
1,435 Views
3 Replies
Replies (3)

goyals
Autodesk
Autodesk

I am not sure the reason of failure of approach 3A is because no body present in root component in the step design you imported. May be you can check that. In case body is present in one of child component of root then you need to iterate on the root component children to get there. For example if the body is present in the first child component of root then you can try below script. Thanks.

occs = root.occurrences

occ = occs[0]

child_component = occ.component

child_bodies = child_component.brepBodies

body = child_bodies[0]

 



Shyam Goyal
Sr. Software Dev. Manager
0 Likes

Anonymous
Not applicable

Hi, thanks for the quick response @goyals,

Still can't get it working having tried the child component lines (and tried for different items of occurrence and child bodies). Have attached a screenshot of tree in UI, though from what I understand it might not be that useful.

Also attached updated script, which returns an error with line 82, which says:

"RuntimeError: 2 : InternalValidationError :

Xl::Utils::getObjectPath(pBody,objPath,nullptr,compPath)"

Seen another forum question about occurrence of child components with creating for assembly context and wondered if that could offer a solution here? Message 5 of:

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/can-t-get-correct-instance-of-occurance-of...

Sorry if I'm making a meal of this, am fairly new to Fusion API so lacking understanding beyond more simple tasks.

Tried to attach the 'SmallCube.step' file as well but had "The attachment's smallcube.step content type (application/octet-stream) does not match its file extension and has been removed."

Thanks,

Mike

0 Likes

BrianEkins
Mentor
Mentor
Accepted solution

There are two ways to accomplish the rotation.  First, because the imported step is added to a separate component, you can rotate the occurrence that represents that component.  Second, you can do what you were trying to do and rotate the body in the component.  Which one you choose would depend on what you'll be doing with the model and in many cases it probably doesn't matter.  Here's some code that demonstrates both approaches, just change the if statement between True and False to get it to run the block of code for the type you want to see.

def importRotate(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        
        design = app.activeProduct
        rootComp = design.rootComponent
        
        ################################## IMPORT THE CREATED STP FILE #####################################

        # Get import manager
        importManager = app.importManager

        # Get step import options
        fileName = "C:/Temp/SimpleBlock.step"
        stpFileName = fileName
        stpOptions = importManager.createSTEPImportOptions(stpFileName)
        stpOptions.isViewFit = False

        # Import step file to root component
        result = importManager.importToTarget2(stpOptions, rootComp)

        # Get the resulting occurrence.
        occ = adsk.fusion.Occurrence.cast(result.item(0))

        if False:
            # Rotate the occurrence.
            angle = math.pi * 0.25
            mat = adsk.core.Matrix3D.create()
            mat.setWithCoordinateSystem(adsk.core.Point3D.create(0,0,0),
                                        adsk.core.Vector3D.create(math.cos(angle), math.sin(angle), 0),
                                        adsk.core.Vector3D.create(math.cos(angle + (math.pi/2)), math.sin(angle + (math.pi/2)), 0),
                                        adsk.core.Vector3D.create(0,0,1))
            occ.transform = mat
        else:
            # Rotate the body.
            bodyComp = occ.component
            body = bodyComp.bRepBodies.item(0)

            inputEnts = adsk.core.ObjectCollection.create()
            inputEnts.add(body)
            angle = math.pi * 0.25
            mat = adsk.core.Matrix3D.create()
            mat.setToRotation(angle, adsk.core.Vector3D.create(0,0,1), adsk.core.Point3D.create(0,0,0))
            moveInput = bodyComp.features.moveFeatures.createInput(inputEnts, mat)
            bodyComp.features.moveFeatures.add(moveInput)
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes