Hallo,teatchers and friends, I am the newbie to learn python and Fusion 360 API. I want to achive some functions. First create a entity (Cylinder or cuboid, in my present example is cuboid now). I need to make an effective digging on the surface of the first body. I have tired to use the advice from the chatGPT and Copilot, but there are always an error. I cant find the similar solution. I hopt that friends who will know to solve this problem.
I have 3 ideas to achieve this.
1: The first is to directly create a sketch by taking the surface of the first body.
2: The second is to create a sketch first, randomly project it to a certain surface of the entity, and cut it.
3: The third is to obtain the data set of the point position on the surface of the entity, then randomly select a point, create a rectangular or circular sketch, and perform entity cutting.
May I ask which method is feasible? The following is a part of my code of the first way.I want this method to work with entities of different shapes. Thank you!
import adsk.fusion, adsk.cam, random, math, adsk.core, os, traceback
app = adsk.core.Application.get()
ui = app.userInterface
doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
design = adsk.fusion.Design.cast(app.activeProduct)
rootComp = design.rootComponent
x0 = random.uniform(10, 20)
y0 = random.uniform(10, 20)
z0 = random.uniform(10, 20)
L = random.uniform(10, 20)
rectanglePlan = random.randint(0,2)
sketch = rootComp.sketches.add(rootComp.xZConstructionPlane)
lines = sketch.sketchCurves.sketchLines
recLines = lines.addTwoPointRectangle(adsk.core.Point3D.create(0, 0, 0), adsk.core.Point3D.create(x0, -z0, 0))
prof = sketch.profiles.item(0)
extrudes = rootComp.features.extrudeFeatures
distance = adsk.core.ValueInput.createByReal(L)
extrudeInput = extrudes.addSimple(prof, distance, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
body_1 =extrudeInput.bodies.item(0)
faces = body_1.faces
faceCollection = adsk.core.ObjectCollection.create()
selectedFace = faces.item(random.randint(0, faces.count-1))
faceCollection.add(selectedFace)
planeInput = rootComp.constructionPlanes.createInput()
planeInput.setByOffset(selectedFace, 0)
newPlane = rootComp.constructionPlanes.add(planeInput)
sketch2 = rootComp.sketches.add(rootComp.xZConstructionPlane)
lines = sketch2.sketchCurves.sketchLines
recLines = lines.addTwoPointRectangle(adsk.core.Point3D.create(0, 0, 0), adsk.core.Point3D.create(x0, y0, 0))
prof = sketch2.profiles.item(0)
sketch2.project(selectedFace)
extrudes = rootComp.features.extrudeFeatures
distance = adsk.core.ValueInput.createByReal(L)
if random.randint(0,1) == 0:
extrudeInput = extrudes.addSimple(prof, distance, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
else:
extrudeInput = extrudes.addSimple(prof, distance, adsk.fusion.FeatureOperations.CutFeatureOperation)
extrudes.add(extrudeInput)