Drive Revolute Joint and Combine (Cut) Two Bodies Multiple Times

Drive Revolute Joint and Combine (Cut) Two Bodies Multiple Times

lukas94R89KL
Participant Participant
745 Views
5 Replies
Message 1 of 6

Drive Revolute Joint and Combine (Cut) Two Bodies Multiple Times

lukas94R89KL
Participant
Participant

 

 

I am essentially trying to cut out a gear from another gear I already formed. For that I can use a combine (cut) operation followed by a step in the revolute joint. I can then do another combine-command (before which I have to select: Capture Position) and thereby cut out an approximate shape of a gear. I did this manually a few times and it worked, but took ages so I tried to use the API. Here is what I came up with:

 

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

combineFeatures = None
def cut(targetBody: adsk.fusion.MeshBody, toolBodies: adsk.core.ObjectCollection) -> adsk.fusion.MeshBody:
# Create New Combine Feature
    combineFeatureInput = combineFeatures.createInput(targetBody, toolBodies)
    # Set Up
    combineFeatureInput.operation = 1 # Cut
    combineFeatureInput.isKeepToolBodies = True
    combineFeatureInput.isNewComponent = False
    # Add To CombineFeatures
    return combineFeatures.add(combineFeatureInput)

def run(context):
  ui = None
  try:
# Get Base
    app = adsk.core.Application.get()
    ui  = app.userInterface
    des = adsk.fusion.Design.cast(app.activeProduct)
# Get Revolute Joint (Gear2)
    root = des.rootComponent
    joint = root.asBuiltJoints.itemByName('Rev1')
    rev = adsk.fusion.RevoluteJointMotion.cast(joint.jointMotion)
# Get The Components (Gear1 & Gear2)
    occs = root.occurrences
    comp1 = occs.item(0).component
    comp2 = occs.item(1).component
# Get The Gear-Mesh-Bodies
    gear1 = comp1.bRepBodies[0]
    gear2 = comp2.bRepBodies[0]
# Pack gear1 In Collection (As Combine-Cut Requires toolBodies As A Collection)
    cutters = adsk.core.ObjectCollection.create()
    cutters.add(gear1)
# Get List Of All Combine Features/Commands & Set Globally
    features = comp2.features
    global combineFeatures
    combineFeatures = features.combineFeatures
# Set Up To Group All Cut-Commands For Easy Deletion
    cutFeature = None
    firstCutIndex = None
# Cutting
    for i in range(10):
    # Revolve
        rev.rotationValue = i * (math.pi/180) # increment revolve by 1deg
        adsk.doEvents() # force revolution update
    # Cut
        cutFeature = cut(gear2, cutters)
        if firstCutIndex == None:
            firstCutIndex = cutFeature.timelineObject.index # set start index for grouping
        time.sleep(1) # timeout to see each cut
# Group All Commands
    lastCutIndex = cutFeature.timelineObject.index
    timelineGroups = des.timeline.timelineGroups
    timelineGroup = timelineGroups.add(firstCutIndex, lastCutIndex)
    timelineGroup.name = 'Revolve-Cuts'
  except:
    if ui:
      ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

I could swear, that it already worked in between. Earlier I was able to see the correct cuts, but when the script terminated, all cuts became invisible but the first one. Now it rotates, but does not show any cutting. I kind of suspect that I need to capture the position somehow or update my gear2 any other way for the program to mind the rotation. Any help appreciated!

0 Likes
Accepted solutions (2)
746 Views
5 Replies
Replies (5)
Message 3 of 6

lukas94R89KL
Participant
Participant
That‘s amazing! Yours is much better than mine. I‘ll take a look at it. I still wonder why my code is not working. This API seems pretty complicated to me.
0 Likes
Message 4 of 6

lukas94R89KL
Participant
Participant

Hello @kandennti

your solution is much nicer than mine, yet I have a problem with it: It does not allow for more complicated scenarios, like having a asymmetric 3D gear (e.g. a helical gear or something even more complicated), or having two gears that rotate in different axles and/or translate while rotating. I would really like to keep my approach driving the joint and then cutting the two parts, as it is much more versatile. I just don't understand why exactly my code is not working. I send the file I use to test the script I send earlier.

Thanks, Lukas

0 Likes
Message 5 of 6

lukas94R89KL
Participant
Participant
I updated the code. It should be more readable now.
0 Likes
Message 6 of 6

kandennti
Mentor
Mentor
Accepted solution

@lukas94R89KL -San.

 

If you do not execute the snapshots.add method after moving the occurrence, it will return to its original position.

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/transformation-on-occurrence-getting-reset... 

You will also get a warning dialog in the GUI.

 

I think a modification like this would give you the process you want.

・・・
# Cutting
    for i in range(10):
    # Revolve
        rev.rotationValue = i * (math.pi/180) # increment revolve by 1deg
        try:
           des.snapshots.add()
        except:
           pass
        adsk.doEvents() # force revolution update
・・・