This was a strange one. I thought I saw the problem but then edited your code to fix that, but it still failed. My original thought is that when you create a sketch on a face, it automatically projects the edges of the face into the sketch, so in this case, the sketch will have a circle in it. You were creating a second circle, and I thought this might be causing the problem, but it still failed. I tried a few other things, and it still failed. Then a few more, and it worked, but it wasn't obvious what change fixed it. Finally, I found that moving the line that gets the profile later in the code fixed the problem. I think what's happening is that you get the profile from the sketch and then draw the line for the path in the same sketch. I think making a change to the sketch invalidates the profile. Getting the profile after drawing the line works. Here's my code that does this, plus a few other changes that clean it up a bit.
def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
product = app.activeProduct
design = adsk.fusion.Design.cast(product)
root:adsk.fusion.Component = design.rootComponent
# Create a cylinder
sketch: adsk.fusion.Sketch = root.sketches.add(root.xYConstructionPlane)
sketch.sketchCurves.sketchCircles.addByCenterRadius(adsk.core.Point3D.create(0, 0, 0), 2)
profile:adsk.fusion.Profile = sketch.profiles.item(0)
extrudes:adsk.fusion.ExtrudeFeatures = root.features.extrudeFeatures
extrudeInput = extrudes.createInput(profile, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
extrudeInput.setOneSideExtent(
adsk.fusion.DistanceExtentDefinition.create(adsk.core.ValueInput.createByString("100 mm")),
adsk.fusion.ExtentDirections.NegativeExtentDirection)
extrude = extrudes.add(extrudeInput)
cylinder:adsk.fusion.BRepBody = root.bRepBodies.item(0)
# Create another sketch on one of the cylinder's faces
face = extrude.startFaces[0]
other_sketch: adsk.fusion.Sketch = root.sketches.add(face)
# Get the circle that was automatically projected from the face.
circle = other_sketch.sketchCurves.sketchCircles[0]
# Draw lines from the center of the circle to the edge.
constraints = other_sketch.geometricConstraints
lines = other_sketch.sketchCurves.sketchLines
line1 = lines.addByTwoPoints(circle.centerSketchPoint, adsk.core.Point3D.create(2, 0, 0))
constraints.addHorizontal(line1)
constraints.addCoincident(line1.endSketchPoint, circle)
line2 = lines.addByTwoPoints(circle.centerSketchPoint, adsk.core.Point3D.create(0, 2, 0))
constraints.addVertical(line2)
constraints.addCoincident(line2.endSketchPoint, circle)
# Create a path
vector: adsk.core.Vector3D = adsk.core.Vector3D.create(0, 0, -3)
path_vector:adsk.core.Vector3D = vector.copy()
path_end_point:adsk.core.Point3D = adsk.core.Point3D.create(
circle.centerSketchPoint.geometry.x + path_vector.x,
circle.centerSketchPoint.geometry.y + path_vector.y,
circle.centerSketchPoint.geometry.z + path_vector.z,
)
path_line:adsk.fusion.SketchLine = other_sketch.sketchCurves.sketchLines.addByTwoPoints(
circle.centerSketchPoint.geometry, path_end_point
)
path:adsk.fusion.Path = adsk.fusion.Path.create(path_line, adsk.fusion.ChainedCurveOptions.noChainedCurves)
# Get the minimum area profile, since there are two.
profile:adsk.fusion.Profile = min(other_sketch.profiles, key=lambda x: x.areaProperties().area)
# Create the sweep
sweeps:adsk.fusion.SweepFeatures = profile.parentSketch.parentComponent.features.sweepFeatures
sweepInput:adsk.fusion.SweepFeatureInput = sweeps.createInput(
profile, path, adsk.fusion.FeatureOperations.CutFeatureOperation)
sweepInput.twistAngle = adsk.core.ValueInput.createByString('90 deg')
sweepInput.taperAngle = adsk.core.ValueInput.createByString('0 deg')
sweepInput.orientation = adsk.fusion.SweepOrientationTypes.PerpendicularOrientationType
sweepInput.participantBodies = [cylinder]
sweepFeature:adsk.fusion.SweepFeature = sweeps.add(sweepInput)
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
---------------------------------------------------------------
Brian EkinsInventor and Fusion 360 API Expert
Website/Blog:
https://EkinsSolutions.com