- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I use a cylinder here as an example, but here is a little background of my goal. I want to copy one revolution of a screw thread that is created in an add-in I am writing to have customized screw threads. Here is a link to the add-in. https://github.com/geodave810/ThreadTune . In OpenSCAD, I was used to being able to modify the diameter, pitch and angle of the threads and decided to see if I could do the same in fusion using the python API & the python code does work. I draw the thread using a thread profile generated from the user input variables along an approximate helix drawn with spiral points. The more spline points I give it, the more accurate it gets. The accuracy is plenty accurate in 3D printing & probably other use cases. The caveat is the more revolutions of the helix the more likely the program will give an error or bog down. I give an option to use either a Center line or an outside helix for the guide rail. I don't think the Center line is accurate enough & will sometimes crash with only 90 total spline points. The more accurate spline using a helix spline for the path on the inside thread & another helix spline along the outside thread as guide rail. My solution is to just draw one revolution of the helix, copy & join all those bodies. My problem is getting it to work. I am new to coding in this API & after a lot of trial & error & searching for examples of this, I decided to see if someone here could help me. Some of this code is generated by Copilot in Bing.
I get this error when running the code in a new design.
Error: Traceback (most recent call last):
File "C:/Users/DDB/AppData/Roaming/Autodesk/Autodesk Fusion 360/API/Scripts/AP_CopyMove/AP_CopyMove.py", line 47, in run
rootComp.features.moveFeatures.add(rootComp.bRepBodies.item(0), move_vector)
TypeError: MoveFeatures.add() takes 2 positional arguments but 3 were given
import adsk.core
import adsk.fusion
import traceback
app = adsk.core.Application.get()
ui = app.userInterface
product = app.activeProduct
design = adsk.fusion.Design.cast(product)
rootComp = design.rootComponent
features = rootComp.features
def run(context):
try:
z_offset = 1.5 # Distance between cylinders in Z direction
num_cylinders = 3
diameter = 5.0
height = 1.0
# Create a cylinder
sketches = rootComp.sketches
xy_plane = rootComp.xYConstructionPlane
sketch = sketches.add(xy_plane)
center_point = adsk.core.Point3D.create(0, 0, 0)
Rad = (diameter / 2.0)
circle = sketch.sketchCurves.sketchCircles.addByCenterRadius(center_point, Rad)
cirProf = sketch.profiles.item(0)
Ht2 = adsk.core.ValueInput.createByReal(height)
extrudes = rootComp.features.extrudeFeatures
extrudeFeature = extrudes.addSimple(cirProf, Ht2, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
# Create a collection of entities for move
bodies = adsk.core.ObjectCollection.create()
bodies.add(extrudeFeature.bodies.item(0))
vector = adsk.core.Vector3D.create(0, 0, 1.0)
transform = adsk.core.Matrix3D.create()
transform.translation = vector
moveFeats = rootComp.features.moveFeatures
moveFeatureInput = moveFeats.createInput(bodies, transform)
moveFeats.add(moveFeatureInput)
# Copy the cylinder in Z direction
z_offset = 1.5
for i in range(1, num_cylinders):
move_vector = adsk.core.Vector3D.create(0, 0, i * z_offset)
rootComp.features.moveFeatures.add(rootComp.bRepBodies.item(0), move_vector)
# Join all cylinders
bodies = adsk.core.ObjectCollection.create()
for i in range(3):
bodies.add(rootComp.bRepBodies.item(i))
join_input = extrudes.createInput(bodies, adsk.fusion.FeatureOperations.JoinFeatureOperation)
extrudes.addSimple(join_input)
except Exception as e:
ui.messageBox("Error: {}".format(traceback.format_exc()))
run(None)
Solved! Go to Solution.